MAXScript FAQ > How To Make It Faster > Never get a single pixel when you can get a whole line |
The getPixels and setPixels functions for reading and writing pixels from / to bitmaps are rather slow.
When changing a large number or even all pixels of a bitmap, it is a good idea to perform the getPixels() and setPixels() just once for each horizontal line and do the rest of the work with the resulting array elements.
In the following examples, a 1000x1000 pixels bitmap should be altered by changing every single pixel. This means reading and writing back one million pixels.
In the first example, every single pixel will be read separately, multiplied by a number and written back. We will stop the time in order to compare to the optimized version.
In the second example, instead of reading every single pixel, will be read every line and process all its pixels inside the array returned by getPixels() before writing the whole line back to the bitmap. This means we will make only 1000 read and 1000 write calls instead of one million reads and one million writes.
Since 3ds Max 2010, the pasteBitmap() function can be used in a custom function: mode to perform per-pixel operations using a MAXScript function. This method does not involve the getPixels() and setPixels() calls but does the same and is also great for bothcompositing two bitmaps together and for manipulating pixels ina single bitmap as in our test example.We simply pass the same bitmap twice. The result is normally stored in the second bitmap, in this case it is identical to the first bitmap so we effectively read and write using one bitmap value:
The following table shows the times in milliseconds for the three examples at different image resolutions:
|
2000x2000 |
1000x1000 |
500x500 |
250x250 |
125x125 |
EXAMPLE 1 |
64657 |
16094 |
3578 |
906 |
203 |
EXAMPLE 2 |
20000 |
4937 |
1266 |
344 |
62 |
EXAMPLE 3 |
15187 |
3828 |
906 |
172 |
47 |
Use the 'flagForeground' node viewport state method
Only calculate once if possible