ADC Home > Reference Library > Technical Q&As > Carbon > Graphics & Imaging >

Not Recommended Documentclose button

Important: The information in this document is Not Recommended and should not be used for new development.

Current information on this Reference Library topic can be found here:

Creating Gray Scaled Images > 8 bits

Q: I want to create gray-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 gray-scaled PixMaps, so you need to build a custom data structure to manipulate the gray-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 gray-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 gray 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 gray 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).

[Nov 17 1997]

Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.