If you wish to increase the speed, you may want to sort the pallet and then implement a better search algorithm when it looks for an image with the closest brightness.
And if you wish to run this script with your own set of images, please make sure all the images are
- 24 bits per pixel, uncompressed BMP files
- gray scaled
var demo = new MosaicBuilder("60x45" /* image name prefix */, 23 /* number of image files */); demo.buildMosaic("60x45.bmp" /* image name */);