[comp.sys.mac.programmer] Help wanted displaying color Picts - setting Color Table

slewis@aero.org (Steven Lewis) (03/28/91)

/*           
	This Code will create a window and Display either Black and White
	or Color Picts.
	
	The Problem is that when the Color Pict uses a non- standard Color
	table, the colors are incorrect.
	
	What is required to make the Color Table match the Picture's color
	table?
	What is required to restore the Color Table when the Window is Deactivated

              */
#include <Macheaders>
#include <SysErr.h>
#include <stdio.h>

static FILE *PICTfile;
static pascal void      read_picture_data(Ptr, int);
static int TestForColor(void);
void read_picture(WindowPtr window,PicHandle handle,Rect *box);

/* return 1 if system is color, 0 for B&W  */
int TestForColor(void)
{
	int depth;

	SysEnvRec MyRec;
	GDHandle MaxDevice;

	/* Test System Capabilities */
	SysEnvirons(1,&MyRec);
		if(!MyRec.hasColorQD)  return(0);
		
	/* Test monitor Depth -- See Knaster Page 215 */
	MaxDevice = GetMaxDevice(&screenBits.bounds);
	depth = (*(* MaxDevice)->gdPMap)->pixelSize;
	if(depth == 1) return(0);
	return(1);
}


/*
 * Called indirectly to read a chunk of picture data.
 */
pascal void read_picture_data(Ptr  data_ptr,int  byte_count)
{
    OSErr        status;
    long        count;
    
    count = byte_count;
    fread(data_ptr,count,1,PICTfile);
}



/*
 * read_picture() reads the picture from the PICT file,
 * constructing the window at the proper size.
 */

void read_picture(WindowPtr window,PicHandle handle,Rect *box)
{
    QDProcs          procedures;
    CQDProcs         Cprocedures;
    
    SetPort((CGrafPtr)window);
  
    /*
     * Read the MacDraw header record -- that was a
     * good idea, but it didn't work as the headers
     * are garbage in the PICT I'm using. make_window.h
     */


     if(TestForColor()) {
	    SetStdCProcs(&Cprocedures);
	    ((CGrafPtr)window)->grafProcs = (QDProcs *)&Cprocedures;
 	    Cprocedures.getPicProc = (Ptr)read_picture_data;
 	}
	else {
	    SetStdProcs(&procedures);
	    ((CGrafPtr)window)->grafProcs = &procedures;
 	    procedures.getPicProc = (Ptr)read_picture_data;
	}
    DrawPicture(handle,box);
    ((CGrafPtr)window)->grafProcs = (void *)0L;
}

#define TEST_Pictures /* Test main   */
#ifdef TEST_Pictures
 
main()
{
 	WindowPtr      window;
 	Rect TheRect;
 	Rect WindowRect;
 	PicHandle handle;
    char             header[1024];
	
	InitGraf(&thePort);					/* initialize QuickDraw, 'thePort' holds
	FlushEvents(everyEvent, 0);			/* get rid of pending events in the queue	*/
	InitWindows();						/* initialize the Window Manager			*/
	MaxApplZone();                      /* SE Cannot run with this */
 
	/* Open a Pict File - Spuerpaint, Giffer ... */
 	PICTfile = fopen("anulka.pic","rb"); /*  */
	/*  PICTfile = fopen("ASP.pic","rb"); /*  */
	
    read_picture_data((Ptr) header, 512); /* Skip the header */
    handle = (PicHandle) NewHandle(sizeof (Picture));
    HLock(handle);
    read_picture_data((Ptr) *handle, sizeof (Picture));  /* read data */
    WindowRect = TheRect = (**handle).picFrame;                       /* get size */
    HUnlock(handle);
	OffsetRect(&WindowRect,50,50); /* make room for the menu bar */
	
 	if(TestForColor())
	 	window = NewCWindow(NULL, &WindowRect, "\pPicture",0,documentProc,
				     (WindowPtr)-1L,1, 0L);
	else			  
	 	window = NewWindow(NULL, &WindowRect, "\pPicture",0,documentProc,
				     (WindowPtr)-1L,1, 0L);
 
    ShowWindow(window);
 	read_picture(window,handle,&TheRect);

	fclose(PICTfile);
    DisposHandle((Handle) handle);
 	
	while(!Button()); /* wait for mouse click */
 	
}
#endif TEST_Pictures