[comp.sys.mac.programmer] Problems with color CopyBits

rtp1@tank.uchicago.edu (raymond thomas pierrehumbert) (03/15/90)

I posted something about this a while ago but got no responses.  Our
news server has been a little flakey, so I thought I'd try again.  
Please send responses to me by email.

   I want to write a routine that will take a color raster image
(i.e. a buffer of 8-bit pixels) and display it to the screen using
the current palette.  The pixel values are meant to correspond to
the color you get from the SetEntryColor call to the Palette 
manager.  I am trying to do the display by making a Pixmap, 
putting in the image as BaseAddr, and doing a copybits to the 
portpixmap.  It doesn't work.  The colors I get on the screen
are all messed up, and differ from one call to another, suggesting
some problem with how I am setting the Pixmaps palette.  I know
it should be possible to do this, as NCSA Image does it;  I have
their source, but can't understand what is going on.  
     Can somebody send me a code fragment that does what I want?  My
own code for displaying the pixel image follows.  What am I doing
wrong?

showbytes(a,rowbytes,nrows)
	unsigned char *a;
	short rowbytes,nrows;
{
	PixMapHandle mypixh;
	ColorTable **ctab;
	BitMap *mybitptr,*windowbitptr;
	Rect vportRect;
	CGrafPort *portptr;
	RGBColor black,white;
	int i;
	black.red=0;black.green=0;black.blue=0;
	white.red=65535;white.green=65535;white.blue=65535;
	portptr = (CGrafPtr) theWindow;
	mypixh = NewPixMap();
	ctab = (**((*portptr).portPixMap)).pmTable;
	HandToHand(ctab);
	for(i=0;i<(**ctab).ctSize;i++) {
		(**ctab).ctTable[i].value = i;
		(**ctab).transIndex -= 32768;}
	vportRect.left = idev(xorg);vportRect.right=idev(xorg+xsize);
	vportRect.top = jdev(yorg+ysize);vportRect.bottom=jdev(yorg);
	(**mypixh).baseAddr = (Ptr) a;
	(**mypixh).rowBytes = rowbytes + 32768;/*Set high bit since it's a pixmap*/
	(**mypixh).bounds.top =0;(**mypixh).bounds.bottom=nrows-1;
	(**mypixh).bounds.left = 0; (**mypixh).bounds.right = rowbytes-1;
	(**mypixh).pixelSize = 8;
	(**mypixh).pixelType = 0;
	(**mypixh).cmpCount = 1;
	(**mypixh).cmpSize = 8;
	(**mypixh).vRes = 72;
	(**mypixh).hRes = 72; 
	(**mypixh).planeBytes = 0;
	(**mypixh).pmTable = ctab;
	(**mypixh).pmVersion = 0;
	(**mypixh).packType = 0;
	(**mypixh).packSize = 0;
	windowbitptr = (BitMap*) &((*theWindow).portBits);
	mybitptr = (BitMap*) *mypixh;
	RGBForeColor(&black);RGBBackColor(&white);
	CopyBits ( mybitptr, 
			windowbitptr,
			&((**mypixh).bounds), &vportRect, srcCopy, 0L);
	PmForeColor(color);
}

/*Following routine is used to install the palette*/
SetUpPalette()
{
	RGBColor theColor;
	PaletteHandle MyPaletteH;
	FILE *palfile;
	int i,j;
	unsigned short inbuf[3][256];
	MyPaletteH=NewPalette(256,0L,pmTolerant,0);
	SetPalette(theWindow,MyPaletteH,0L);
	palfile = fopen("default.pal","rb");
	for(i=0;i<3;i++) for(j=0;j<256;j++) fread(&(inbuf[i][j]),1,1,palfile);
	
	for(i=0;i<256;i++){
	theColor.red = inbuf[0][i];
	theColor.green = inbuf[1][i];
	theColor.blue = inbuf[2][i];
	SetEntryColor(MyPaletteH,i,&theColor);};
	ActivatePalette(theWindow);
}
/* end SetUpPalette */