Yes, Image.getSamples()/Image.setSamples() are quite inefficient methods. These methods are intended for occasional use. Unfortunately, most of the overhead in these routines is caused by the task of creating a new Array element (See the documentation for the
JS_SetElement() API call on MDN). This task, which is not trivial, has to be executed once for each pixel. Dynamic languages are very nice and powerful, but often the price to pay is performance problems like this one.
Instead of calling these methods, you can convert your image into a Matrix object by calling:
Matrix Image.toMatrix( [Rect rect=0[, int channel=-1]] )and work directly on the Matrix object. To get a matrix element, use:
Number Matrix.at( int row, int col )and to set elements:
void Matrix.at( int row, int col, Number value )Once you are done working with the matrix, you can convert back to an image with:
Image Matrix.toImage()Both methods are extremely efficient because they are fully implemented as native C++ code.