Re: how to convert 32 bits color to 256 color

Search this archive.

From: Sergio Masci (sergio@xcprod.com)
Date: Thu 18 Jul 2002 - 06:51:34 IDT


> If I use 16M color mode.
> How I convert 16M color to 256 color.
> I know use gl_getpixelrgb serial function to get rgc color.
> Then I don't how to do.
> Convert 16M color to 256 gray I average rgb color (ex : gray =
(r+g+b)/3 ).
> Is it a right method?

find the 256 most common colours
convert all others colours to one of these 256 colours by finding closest
match

difference between two colours is

p1 = r1*r1 + g1*g1 + b1*b1

p2 = r2*r2 + g2*g2 + b2*b2

d = abs(p1 - p2)

p1 is the position of pixel 1 in the colour space
p2 is the position of pixel 2 in the colour space

rN is the red component of pixel N
gN is the green component of pixel N
bN is the blue component of pixel N

pixel N is closer in colour to pixel V then pixel W if

abs(pV - pN) < abs(pW - pN)

You can easily precalculate the position of all the 256 most common colours
in  your 256 colour table to reduce computation times.

A more complex solution involves pushing the difference between the original
pixel colour and the closest pixel colour onto the next three pixels
(weigthed).
i.e.
consider a group of pixels
p00  p10
p01  p11

p00 is to be converted, to pixel X. The difference in colour between the
original pixel p00 and the new colour of pixel p00 is transfered to pixels
p10, p01 and p11. The way the colour is distributed amoungst the other three
pixels is done by adding a SPECIFIC fraction of the difference of the colour
to each of the pixels. Each of the pixels p10, p01 and P11 has a specific
fraction assigned to it, I cannot remember off hand what the fractions are
but these should be easy enough to find using google (look for error
diffusion, colour dithering, floyd steinberg or some combination of these).
There are several other methods for spreading errors around (averaging out
the colours) but I can't remember what they are called right now.

Regards
Sergio


------------------------------------------------------------------
Unsubscribe:  To:   listbot@svgalib.org
              Body: unsubscribe linux-svgalib


This archive was generated by hypermail 2.1.4 : Wed 21 Jan 2004 - 22:10:25 IST