Color buried treasure

Dan Ingalls DanI at
Fri Feb 26 06:16:32 UTC 1999

>> > I still have to build the renderer to change all colors in the source to 
>> > those quantizated by Heckbert's algorithm.
>> This should be pretty trivial - BitBlt uses a color map that can be set
>> accordingly from your source depth to the right 256 colors.
>How does this work? Does it support dithering on the way?

I'm happy to say that this is actually documented in the class comment of BitBlt.  I quote...

	"When blitting from a 32 or 16 bit deep Form to one 8 bits or less, the default is truncation.  This will produce very strange colors, since truncation of the high bits does not produce the nearest encoded color.  Supply a 512 long colorMap, and red, green, and blue will be shifted down to 3 bits each, and mapped.  The message copybits...stdColors will use the best map to the standard colors for destinations of depths 8, 4, 2 and 1.  Two other sized of colorMaps are allowed, 4096 (4 bits per color) and 32786 (five bits per color)."

So to use this feature, it is necessary that each of your 32-bit (really 24-bit) colors have a unique RGB triple when truncated to 3, 4, or 5 bits per color.

If so, then build a bitmap of the right size (512, 4096 or 32768) and put i into all 4 bytes of the entry that your i-th color will map to.  Then just blt it into a ColorForm of depth 8 using that as the color map, and every pixel that was color i in 32 bits will have a pixel value of i in the 8-bit form.  All that remains is to supply the 256-entry color array for the colorForm.  Obviously the j-th entry should be the j-th color produced by your partitioning.

If not, then this feature will not do the whole job for you.  But it's so simple and fast that it may be worth figuring out how to trick it into doing what you want.  One way for sure is to say you're willing to settle for 15-bit color accuracy instead of 24.  If so, then just blt the 32-bit form onto a 16-bit form.  This will truncate to 5 bits per color.  If you then run your same algorithm it will give you 256 distinct colors in a 15-bit space.  Then you KNOW you can use a 32k entry map to get those into an 8-bit form.

Hope this helps.

	- Dan

More information about the Squeak-dev mailing list