//============================================================
//============================================================
//==														==
//==				Dump Pict Routines						==
//==														==
//============================================================
//============================================================


#include "UnivUtilities.h"


//extern GrafPtr			offWorkPtr;
//extern BitMap			offWorkBits;
//extern CGrafPort		offCWorkPort;
//extern CGrafPtr			offCWorkPtr;
//extern Rect				replayRect;


//--------------------------------------------------------  Type Definitions

typedef struct
{
	PicHandle	pHand;
	short		gRef;
	Boolean		fileOK;
} GDataRec;

typedef struct
{
	CGrafPort	TRUEPort;
	GDataRec	GDStuff;
} GportPlus, *GPPtr;

//--------------------------------------------------------  Prototypes

void DeathKiss (short, short, StringPtr);
pascal void PutPICTData (Ptr, short);

//--------------------------------------------------------  PutPICTData

pascal void PutPICTData (Ptr dataPtr, short byteCount)

{
	long		longCount;
	GPPtr		myPortPlus;
	
	GetPort((GrafPtr *)&myPortPlus);
	longCount = byteCount;
	
	if (myPortPlus->GDStuff.fileOK)
	{
		if (FSWrite(myPortPlus->GDStuff.gRef, &longCount, dataPtr) != noErr)
			myPortPlus->GDStuff.fileOK = FALSE;
		
		if (myPortPlus->GDStuff.pHand != kNilPointer)
			(**(myPortPlus->GDStuff.pHand)).picSize += longCount;
	}
}

//--------------------------------------------------------  DeathKiss

void DeathKiss (short globalRef, short vrefnum, StringPtr nameStr)

{
	OSErr		theErr;
	
	if (globalRef != 0)
		theErr = FSClose(globalRef);
	
	theErr = FSDelete(nameStr, vrefnum);
	SysBeep(1);
}

//--------------------------------------------------------  PICTOut

void PICTOut (void)

{
	OSErr		err;
	short		i, vrefnum, globalRef;
	long		longCount, longZero, bytesAvail;
	CQDProcs	myProcs;
	QDProcs		myOldProcs;
	Picture		savePictSizeFrame;
	Str255		nameStr;
	GrafPtr		oldPort;
	GportPlus	wPortPlus;
	GPPtr		wPortPlusPtr;
	PicHandle	pictHand;
	SysEnvRec	theWorld;
	BitMapPtr	bitPtr;
	GDHandle	myDev;
	
	err = SysEnvirons(1, &theWorld);
	
	wPortPlusPtr = &wPortPlus;
	globalRef = 0;
	if (GetVInfo(0, (StringPtr)nameStr, &vrefnum, &bytesAvail) != noErr)
	{
		DeathKiss(globalRef, vrefnum, nameStr);
		return;
	}
	
	PasStringCopy("\pScreen 0", nameStr);
	
	do
	{
		err = Create(nameStr, vrefnum, 'GAO.', 'PICT');
		if (err != noErr)
		{
			if (err == dupFNErr)
			{
				nameStr[8]++;
			}
			else
			{
				DeathKiss(globalRef, vrefnum, nameStr);
				return;
			}
		}
	}
	while (err != noErr);
	
	if (FSOpen(nameStr, vrefnum, &globalRef) != 0)
	{
		DeathKiss(globalRef, vrefnum, nameStr);
		return;
	}
	
	longZero = 0L;
	longCount = 4L;
	
	for (i = 0; i < (532 / 4); i++)
	{
		err = FSWrite(globalRef, &longCount, &longZero);
		if (err != noErr)
		{
			DeathKiss(globalRef, vrefnum, nameStr);
			return;
		}
	}
	
	if (SetFPos(globalRef, fsFromStart, 522) != noErr)
	{
		DeathKiss(globalRef, vrefnum, nameStr);
		return;
	}
	
	GetPort(&oldPort);
	
	wPortPlus.GDStuff.gRef = globalRef;
	wPortPlus.GDStuff.pHand = kNilPointer;
	wPortPlus.GDStuff.fileOK = TRUE;
	
	if (theWorld.hasColorQD)
	{
		OpenCPort((CGrafPtr)wPortPlusPtr);
		SetStdCProcs(&myProcs);
		((GrafPtr)wPortPlusPtr)->grafProcs = (QDProcs *)&myProcs;
		myProcs.putPicProc = (QDPtr)PutPICTData;
		myDev = GetMainDevice();
		bitPtr = (BitMapPtr)*(**myDev).gdPMap;
	}
	else
	{
		OpenPort((GrafPtr)wPortPlusPtr);
		SetStdProcs(&myOldProcs);
		((GrafPtr)wPortPlusPtr)->grafProcs = &myOldProcs;
		myOldProcs.putPicProc = (QDPtr)PutPICTData;
		bitPtr = (BitMapPtr)&wPortPlusPtr->TRUEPort.portPixMap;
	}
	
	ClipRect(&bitPtr->bounds);
	pictHand = OpenPicture(&bitPtr->bounds);
	
	if (pictHand != kNilPointer)
	{
		wPortPlus.GDStuff.pHand = pictHand;
		CopyBits(bitPtr, bitPtr, &bitPtr->bounds, &bitPtr->bounds, srcCopy, kNilPointer);
		ClosePicture();
		KillPicture(pictHand);
	}
	else
		wPortPlus.GDStuff.fileOK = FALSE;
	
	((GrafPtr)wPortPlusPtr)->grafProcs = kNilPointer;
	
	SetPort(oldPort);
	
	if (theWorld.hasColorQD)
		CloseCPort((CGrafPtr)wPortPlusPtr);
	else
		ClosePort((GrafPtr)wPortPlusPtr);
	
	if (!(wPortPlus.GDStuff.fileOK))
	{
		DeathKiss(globalRef, vrefnum, nameStr);
		return;
	}
	
	if (SetFPos(globalRef, fsFromStart, 512) != noErr)
	{
		DeathKiss(globalRef, vrefnum, nameStr);
		return;
	}
	
	longCount = sizeof(Picture);
	
	if (FSWrite(globalRef, &longCount, &savePictSizeFrame) != noErr)
	{
		DeathKiss(globalRef, vrefnum, nameStr);
		return;
	}
	
	if (FSClose(globalRef) != noErr)
	{
		DeathKiss(globalRef, vrefnum, nameStr);
		return;
	}
}
