Technical Q&As


QD50 - Creating Gray Scaled Images > 8 bits
(17-November-97)


Q I want to create grey-scaled images at resolutions greater than 8 bits, and display them on the Mac. What is the best way to go about this?

A Quickdraw doesn't have any inherent support for grey-scaled PixMaps, so you need to build a custom data structure to manipulate the grey-scale image, and copy this image to an offscreen GWorld when you want to draw it to the screen.

The Quickdraw color table for the offscreen should ramp from white at location 0 to black at location 255. Quickdraw always assumes that white and black will be in these locations, and does not perform correctly when this isn't the case. However, this is the opposite of the intensity value in a grey-scaled image, where black would be at 0, and white at 255.

The other thing that is required is a routine to convert from the custom data structure to the QuickDraw offscreen. This conversion can be accomplished by taking the top 8 bits of each grey pixel, inverting them and copying it into the offscreen GWorld. A good source of info would be "Drawing in GWorlds for Speed and Versatility" in Develop issue 10. The following snippet of code accumulates four pixels worth of data, converting from a 16-bit grey pixel to an 8-bit color index.

UInt16 *sourceGreyPtr;
UInt32 *destPixelsPtr;
UInt16 pixel1, pixel2, pixel3, pixel4;
UInt32 pixelOutput;

{
	pixel1 = sourceGreyPtr[0];
	pixel2 = sourceGreyPtr[1];
	pixel3 = sourceGreyPtr[2];
	pixel4 = sourceGreyPtr[3];
	// Shift each pixel to its location, take the complement, and mask out 
the correct byte
	pixel1 = ~(pixel1 << 16) & 0xFF000000;
	pixel2 = ~(pixel2 << 8)  & 0x00FF0000;
	pixel3 = ~(pixel3)       & 0x0000FF00;
	pixel4 = ~(pixel4 >> 8)  & 0x000000FF;
	pixelOutput = (pixel1 << 24) | (pixel2 << 16) | (pixel3 << 8) | (pixel4);
	*destPixelsPtr = pixelOutput;
	sourceGreyPtr +=4;                           // Advances 8 bytes
	destPixelsPtr +=1;                           // Advances 4 bytes
}
Quickdraw GX does include a luminance-based color model (gxGraySpace).



-- Tim Carroll
Worldwide Developer Technical Support

Technical Q&As
Previous Question | Contents | Next Question

To contact us, please use the Contact Us page.