I have my own custom palette with about thousands colours, but many times the desired image could have absolutely different colours than my palette and create poor results. Some sort of dithering is required! Pictures are usually small (smaller than 256x256) but I need to get maximum from it.
Now I use something like this (here is just simple example and instead the palette I'm using random generator)
Code: Select all
$src_img=imagecreatefrompng($in);
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
$dst_img=imagecreate($old_x,$old_y);
for ($i=0;$i<5000;$i++) {
$farby[]=imagecolorallocate($dst_img,rand(0,255),rand(0,255),rand(0,255));
}
imagecopy($dst_img,$src_img,0,0,0,0,$old_x,$old_y);
imagedestroy($src_img);
imagepng($dst_img,$out);
imagedestroy($dst_img);
I tried to trick the imagetruecolortopalette in this steeps:
1) Loaded original image A
2) Creating image B with my palette
3) Converting the palette image B to true color
4) Using imagetruecolortopalette on original image A
5) imageColorMatch() on image A to match palette from B
And it shows little bit of dithering, but in absolutely worst way, it's much worse the non dithered. The problem is that even 256 colors are enough for imagetruecolortopalette create almost true-colour looking image (very fine dithering). But when the colours are matching to my palette all the fine colours and shades of the similar colours are blended into 1 colour in mine palette, so still I have this big flat places flood-filled with 1 my colour. But on edges where I put sharpening filters it's disrupting the effect of sharpening and makes even more errors to image than it was before.
If somebody has ideas what I could try (or do), I will be deeply thankful.
Thanks in advance for your inputs.