[comp.sys.mac.programmer] Help with setting pixel color

peterson@peterson.applicon.UUCP (07/20/88)

I would like to be able to set a pixel to a color using the color index
number rather than RGB (as in SetPixel(RGB Color)).

I can use SetPixel, but it is much too slow for filling a window with
pixels (as in a grey scale picture).  Maybe I am missing "the" way to
draw an image into a window.  Can anyone help? I am new to color quickdraw!

Thanks...  Joe Peterson
           Schlumberger
	   Billerica, Ma.

(uucp)  ...mit-eddie!applicon!peterson

beard@ux1.lbl.gov (Patrick C Beard) (07/30/88)

In article <77900003@peterson> peterson@peterson.applicon.UUCP writes:
>
>I would like to be able to set a pixel to a color using the color index
>number rather than RGB (as in SetPixel(RGB Color)).

The best way to do this in a "by the rules" manner is to create a pixmap
in an offscreen CGrafPort, and then flash it on to the screen via copybits.
I am currently using this method in a program that I've written for displaying
CCD images on a Mac II.  If you need further assistance, please reply
via net mail.

Patrick Beard
Lawrence Berkeley Laboratory
beard@ux1.lbl.gov

t-benw@microsoft.UUCP (Benjamin Waldmin) (08/02/88)

In article <77900003@peterson> peterson@peterson.applicon.UUCP writes:
>
>I would like to be able to set a pixel to a color using the color index
>number rather than RGB (as in SetPixel(RGB Color)).
>
>I can use SetPixel, but it is much too slow for filling a window with
>pixels (as in a grey scale picture).  Maybe I am missing "the" way to
>draw an image into a window.  Can anyone help? I am new to color quickdraw!

Have you read the Palette Manager manual?
If you want to draw an image with certain colors, create a palette with
those colors, and them use PmForeColor to set the color (you'll be setting
a number corresponding to the palette position of your RGB color), and then
use LineTo or something (this is also neat if you have run length encoded
image data - if you have 10 pixels with the same value, do 1 PmForeColor,
and then a Line to 10 pixels down the line).

Unfortunately, I have no sample code, but good luck anyway.

Ben Waldman
Microsoft Corp.
uw-beaver!microsoft!t-benw

Disclaimer: These are my own thoughts, opinions, and ideas, and are not
	sanctioned in any way by my employer.

fry@huma1.HARVARD.EDU (David Fry) (08/02/88)

In article <1668@microsoft.UUCP> t-benw@microsoft.UUCP (Benjamin Waldman) writes:
>In article <77900003@peterson> peterson@peterson.applicon.UUCP writes:
>>
>>I would like to be able to set a pixel to a color using the color index
>>number rather than RGB (as in SetPixel(RGB Color)).
>>
>>I can use SetPixel, but it is much too slow for filling a window with
>>pixels (as in a grey scale picture).  Maybe I am missing "the" way to
>>draw an image into a window.  Can anyone help? I am new to color quickdraw!
>
>Have you read the Palette Manager manual?
>If you want to draw an image with certain colors, create a palette with
>those colors, and them use PmForeColor to set the color (you'll be setting
>a number corresponding to the palette position of your RGB color), and then
>use LineTo or something (this is also neat if you have run length encoded
>image data - if you have 10 pixels with the same value, do 1 PmForeColor,
>and then a Line to 10 pixels down the line).
>
>Unfortunately, I have no sample code, but good luck anyway.

This is certainly the "standard" way of drawing pixel data,
but it is mind-numbingly slow.  Much faster, and quite
adequate, is to create an offscreen PixMap, stuff the pixel
values into data structure, and then CopyBits that onto the
screen.  It's about 2000% faster, if not more.

First determine the color you want the pixel to be, say
MyColor, and then do a Color2Index() call to the index of that
color (or the closest) on the current color table.  Put that
value in the PixMap structure.  If you have an 8-bit image,
say, with only 256 colors, you can call the 256
Color2Index()'s at the beginning of the loop and you need only
move memory, which is very fast, with no toolbox calls, which
is relatively slow.   

If you're squeamish about offscreen PixMaps, and most people
seem to be, I recommend Knaster's "Macintosh Programming
Secrets," which has an extremely lucid account, with sample
code.

David Fry				fry@huma1.harvard.EDU
Department of Mathematics		fry@huma1.bitnet
Harvard University			...!harvard!huma1!fry
Cambridge, MA  02138