[comp.sys.mac] Help wanted: LSC, MacPaint

tedj@hpcid.HP.COM (Ted Johnson) (09/29/87)

The following 90 line program is my attempt at using a SFGetFile dialog
to select a MacPaint file, and then using CopyBits to display the file
on the Mac's screen.  This program is meant to be a Lightspeed C version
of Technical Note #86, which gave a Pascal example for reading a MacPaint
file.

Unfortunately, this program displays junk (it looks like "snow" on a
TV screen) on the Mac's screen instead of the MacPaint picture.  Any clues
as to where I'm going wrong would be greatly appreciated!  (I suspect that
my syntax in calling CopyBits() is wrong...)

	-Ted
P.S. I am using LSC 2.01, upgraded to 2.11, on a SE HD20 running
     System/Finder 4.1/5.5.

******************************************************************
Ted C. Johnson
Hewlett-Packard, Design Technology Center
Santa Clara, CA
(408)553-3555
UUCP: hplabs!hpcea!hpcid!tedj
******************************************************************

/*---------------------------cut here-------------------------------*/

/*For the sake of brevity, I have omitted all the manager "#include"s,
  the comments, and the error message handling.*/

#define YES 		1
#define NIL 		0L
#define DefaultVolume 	0
#define MaxFileSize	51840

SFReply theReply;
Point SFGetwhere = { 90, 82 };
Ptr srcPtr, dstPtr, saveDstPtr;
int srcFile, scanLine, errCode, vRef, refNum;
long srcSize;
GrafPort aPort;
BitMap theBitMap;
Rect screen;
char buffer[1000];
Str255 fn;

main()
{
	InitGraf(&thePort);
	
	if (OldFile(&fn, &vRef) != YES) ExitToShell();
        		
	srcPtr = NewPtr(MaxFileSize);
	if (srcPtr == NIL) ExitToShell(); 
	
	errCode = FSOpen(&fn, vRef, &refNum);
	if (errCode != noErr) ExitToShell();
	
	srcSize = 512;
	errCode = FSRead(refNum, &srcSize, srcPtr);
	if (errCode != noErr) ExitToShell();
	
	errCode = GetEOF(refNum, &srcSize);
	if (errCode != noErr) ExitToShell();
	
	srcSize = srcSize - 512;
	
	errCode = FSRead(refNum, &srcSize, srcPtr);
	if (errCode != noErr) ExitToShell(); 
	
	errCode = FSClose(refNum);
	if (errCode != noErr) ExitToShell(); 
	
	dstPtr = NewPtr(MaxFileSize);
	
	if (dstPtr == NIL) ExitToShell(); 
	
	saveDstPtr = dstPtr;
	
	for (scanLine = 1; scanLine <= 720; scanLine++) {
		UnpackBits(srcPtr, dstPtr, 72);
	}
	
	OpenPort(&aPort);
	SetRect(&screen, 0, 0, 576, 720);
	theBitMap.baseAddr = saveDstPtr;
	theBitMap.rowBytes = 72;
	theBitMap.bounds = screen;
	CopyBits(&theBitMap, &(aPort.portBits), &(aPort.portRect), 
			 &(aPort.portRect), srcCopy, NIL);
			 
	do {
		;
	} while (!Button());
}
	
OldFile(fn, vRef)
Str255 *fn;
int *vRef;
{
SFTypeList fileTypes;
int success_flag;

	fileTypes[0] = 'PNTG';
	SFGetFile(SFGetwhere, "\pPick a paint file",
			  0L, 1, fileTypes, 0L, &theReply);
	if (theReply.good == YES) {
		pStrCopy(theReply.fName, fn);
		*vRef = theReply.vRefNum;
		success_flag = YES;
	}
	else {
		success_flag = NO;
	}
	return(success_flag);
}

palmer@tybalt.caltech.edu (David Palmer) (09/29/87)

In article <3320023@hpcid.HP.COM> tedj@hpcid.HP.COM (Ted Johnson) writes:
>
>The following 90 line program is my attempt at using a SFGetFile dialog
>to select a MacPaint file, and then using CopyBits to display the file
>on the Mac's screen.  This program is meant to be a Lightspeed C version
>of Technical Note #86, which gave a Pascal example for reading a MacPaint
>file.
>
>Unfortunately, this program displays junk (it looks like "snow" on a
>TV screen) on the Mac's screen instead of the MacPaint picture.  Any clues
>as to where I'm going wrong would be greatly appreciated!  (I suspect that
>my syntax in calling CopyBits() is wrong...)
...
>	
>	for (scanLine = 1; scanLine <= 720; scanLine++) {
>		UnpackBits(srcPtr, dstPtr, 72);
>	}

If UnpackBits() is defined with srcPtr and dstPtr as VAR, then the line
should be:
		UnpackBits(&srcPtr, &dstPtr, 72);
The loop as it stands does not change srcPtr and dstPtr so you do the
same thing 720 times, and the wrong thing to boot.  In C, you 'call by
name' by passing a pointer to an object.

Stylistic note: most C programmers would write the first line:
	for (scanLine = 0 ; scanLine < 720 ; scanLine++)
because this would allow the use of scanLine as an array index.  (In C,
a 720 element array has indices from 0 to 719, inclusive).  This is unimportant
in this case, but you might as well be consistent.

		David Palmer
		palmer@tybalt.caltech.edu
		...rutgers!cit-vax!tybalt.caltech.edu!palmer
	The opinions expressed are those of an 8000 year old Atlantuan
	priestess named Mrla, and not necessarily those of her channel.

tedj@hpcid.HP.COM (Ted Johnson) (10/01/87)

Thanks!  That was indeed the bug.  For some reason I was under the impression
that since srcPtr and dstPtr were declared as type "Ptr" that I didn't have
to put an "&" in front of them to pass their addresses.   

-Ted