[comp.sys.mac.programmer] Reading a PICT file into a window

jhsu@Neon.Stanford.EDU (Jeffrey H. Hsu) (06/30/90)

Can somebody tell me how to read a PICT document into a scrollable window? I 
can deal with the PICT as a resource but not as a file. 

Any help would be greatly appreciated. Thank you.

Jeffrey Hsu

minow@mountn.dec.com (Martin Minow) (06/30/90)

In article <1990Jun29.215003.26596@Neon.Stanford.EDU> jhsu@Neon.Stanford.EDU
(Jeffrey H. Hsu) writes:
>Can somebody tell me how to read a PICT document into a scrollable window? I 
>can deal with the PICT as a resource but not as a file. 

I can't help with the scrollable part, but here's some code I use to read
a PICT file into an offscreen bitmap.  (The code is incomplete, but it
should give you the basic structure.)  There's a Tech Note that explains
what's going on.

Martin Minow
minow@bolt.enet.dec.com


extern int	picture_file;		/* PICT file, already open	*/

void		read_background_picture(void);
pascal void	read_picture_data(Ptr, int);


/*
 * Called indirectly to read a chunk of picture data.
 */
pascal void
read_picture_data(data_ptr, byte_count)
Ptr		data_ptr;
int		byte_count;
{
	OSErr		status;
	long		count;
	
	count = byte_count;
	status = FSRead(picture_file, &count, data_ptr);
	if (status != noErr)
	    status_error("\pReading background picture", status);
}

void
read_background_picture()
{
	PicHandle	handle;
	QDProcs		procedures;
	OSErr		status;
	long		size;
	long		place;
	int		picture_width, picture_height;
	int		screen_width, screen_height;
	Rect		box;
	
	SetStdProcs(&procedures);
	((GrafPtr) overhead_window)->grafProcs = &procedures;
	/*
	 * Use our function to read chunks of picture data
	 */
	procedures.getPicProc = (Ptr) read_picture_data;
	handle = (PicHandle) NewHandle(sizeof (Picture));
	/*
	 * Rewind the file and skip over the MacDraw header block.
	 */
	if ((status = SetFPos(picture_file, fsFromStart, 512L)) != noErr) {
	    status_error("\pCan't rewind picture file", status);
	    return;
	}
	HLock(handle);
	/*
	 * Start by reading the header information.
	 */
	read_picture_data((Ptr) *handle, sizeof (Picture));
	HUnlock(handle);
	/*
	 * Center the picture in the window and read it in.
	 */
	box = (*handle)->picFrame;
	OffsetRect(			/* Normalize topleft to 0,0	*/
	    &box,
	    -box.left,
	    -box.top
	);
	screen_width = thePort->portRect.right - thePort->portRect.left;
	screen_height = thePort->portRect.bottom - thePort->portRect.top;
	picture_width = box.right - box.left;
	picture_height = box.bottom - box.top;
	OffsetRect(			/* Now, center it nicely	*/
	    &box,
	    (screen_width - picture_width) / 2,
	    (screen_height - picture_height) / 2
	);
	DrawPicture(handle, &box);
	((GrafPtr) overhead_window)->grafProcs = NIL;
	DisposHandle((Handle) handle);
	/*
	 * Check for errors by getting the file position and
	 * checking that it is at the end of file.
	 */
	if ((status = GetEOF(picture_file, &size)) != noErr
	 || (status = GetFPos(picture_file, &place)) != noErr)
	     status_error("\pCan't get EOF or file position", status);
	if (size != place)
	    status_error("\pDidn't read entire picture, lost", size - place);
}