How to "re-calculate" a image from a specified set of colors?

24 Views Asked by At

What i have is 2 images, the goal is to redraw the second image but only use the colors from the first image.

I have a solution for this already but currently its too slow (you can test it here http://167.99.243.66/).

Essentialy it takes all the colours from image1 and then goes pixel by pixel to redraw the second image, here it then searches the for closest color in the colors from image1.

What kills the speed is the fact that for each pixel i need to loop through the whole palette of image1 to find the nearest color (calculating the nearest 2 points in a 3d vector space).

What can i do here to speed this up?