Some way to create dithered image with own palette?

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
truhlik_fredy
Forum Newbie
Posts: 1
Joined: Mon Sep 28, 2009 11:37 am

Some way to create dithered image with own palette?

Post by truhlik_fredy »

I need to do very specific job, to make very long story short:
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); 
 
But imagecopy doesn't have any dithering options (perhaps some hidden, available trough some global variables). Only function that I know uses dithering is imagetruecolortopalette, but there I can only chose number of colours in the palette and the functions itself decides witch palette to use. But I need use my palette so badly that It's better for me use non dither function than dither functions using absolutely useless palette. My palette or nothing, just I need get better results with dithering. Perhaps if somebody has implemented colour quantization and dithering in pure PHP. Many of the images are 128x64 pixels big and never bigger than 256x256. So perhaps some slow manual dithering in pure PHP could be possible if somebody done it already. Or if somebody know some workaround for imagetruecolortopalette to use custom palette.

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.
Post Reply