mirror of
https://github.com/aaronsgiles/JPEGView.git
synced 2024-06-18 09:30:46 +00:00
1 line
6.6 KiB
C
1 line
6.6 KiB
C
|
/*********************************************************/
/* This source code copyright (c) 1991-2001, Aaron Giles */
/* See the Read Me file for licensing information. */
/* Contact email: mac@aarongiles.com */
/*********************************************************/
#if THINK_C
#include "THINK.Header"
#elif applec
#pragma load ":Headers:MPW.Header"
#elif __MWERKS__
//#include "MW.Header"
#else
#include "JPEGView.h"
#endif
#include <string.h>
/*
* AllocatePort()
*
* Purpose: Allocates a port (actually a dialog record) from our global pool
* Inputs: none
* Returns: a pointer to the memory location
*
*/
CGrafPtr AllocatePort(void)
{
int i;
for (i = 0; i < kMaxPorts; i++)
if (!gPortPool[i].window.port.portBits.baseAddr) {
gPortPool[i].window.port.portBits.baseAddr = (Ptr)0xffffffff;
return (CGrafPtr)&gPortPool[i];
}
return nil;
}
/*
* DecallocatePort(thePort)
*
* Purpose: Deallocates a port from our global pool
* Inputs: thePort = the port to deallocate
* Returns: nothing
*
*/
void DeallocatePort(CGrafPtr thePort)
{
Ptr theStart = StripAddress(gPortPool);
Ptr theEnd = theStart + kMaxPorts * sizeof(DialogRecord);
Ptr theAdr = StripAddress(thePort);
short i;
if ((theAdr >= theStart) && (theAdr < theEnd))
for (i = 0; i < sizeof(DialogRecord); i++) *theAdr++ = 0;
}
/*
* CheckedNewHandle(theSize, canForce)
*
* Purpose: Allocates a handle, either in the application heap or in the system heap
* Inputs: theSize = the size of the handle to allocate
* canForce = true if we're allowed to delete GWorlds to force memory
* Returns: a new handle, or nil if not enough memory
*
*/
Handle CheckedNewHandle(Size theSize, Boolean canForce)
{
OSErr theErr = noErr;
Handle theHandle;
if (!IsMemAvailable(theSize) || !(theHandle = NewHandle(theSize))) {
theHandle = TempNewHandle(theSize, &theErr);
if (theErr != noErr && canForce) {
if (!MakeMemAvailable(theSize) || !(theHandle = NewHandle(theSize)))
return nil;
}
}
return (theErr == noErr) ? theHandle : nil;
}
/*
* AnyNewHandle(theSize)
*
* Purpose: Allocates a handle, either in the application heap or in the system heap
* Inputs: theSize = the size of the handle to allocate
* Returns: a new handle, or nil if not enough memory
*
*/
Handle AnyNewHandle(Size theSize)
{
OSErr theErr = noErr;
Handle theHandle;
if (!(theHandle = NewHandle(theSize))) theHandle = TempNewHandle(theSize, &theErr);
return (theErr == noErr) ? theHandle : nil;
}
/*
* ClearMem(thePtr, theSize)
*
* Purpose: Zeroes out a block of memory
* Inputs: thePtr = pointer to the memory
* theSize = the size of the block to clear
* Returns: nothing
*
*/
void ClearMem(Ptr thePtr, register Size theSize)
{
register union {
uchar *cPtr;
ushort *sPtr;
ulong *lPtr;
} p;
p.cPtr = (uchar *)thePtr;
if (theSize && ((long)p.cPtr & 1)) *p.cPtr++ = 0, theSize--;
if (theSize && ((long)p.sPtr & 2)) *p.sPtr++ = 0, theSize -= 2;
while (theSize >= 4) *p.lPtr++ = 0, theSize -= 4;
if (theSize >= 2) *p.sPtr++ = 0, theSize -= 2;
if (theSize) *p.cPtr++ = 0;
// memset(thePtr, 0, theSize);
}
/*
* GWorldSize(theGWorld)
*
* Purpose: Returns the real size of the given GWorld
* Inputs: theGWorld = a pointer to the GWorld
* Returns: the size, in bytes
*
*/
long GWorldSize(GWorldPtr theGWorld)
{
long theSize = sizeof(CGrafPort) + sizeof(GDevice) + 4096;
PixMapHandle thePixMap;
if (!theGWorld) return 0;
thePixMap = GetGWorldPixMap(theGWorld);
if (thePixMap) {
if ((*thePixMap)->pmTable) {
theSize += (sizeof(ColorTable) + sizeof(ColorSpec) *
((*(*thePixMap)->pmTable)->ctSize + 1));
theSize += (1L << (*gMainMonitor)->oldres) * (1L << (*gMainMonitor)->oldres) *
(1L << (*gMainMonitor)->oldres);
}
theSize += (long)((*thePixMap)->rowBytes & 0x3fff) *
(long)Height(&(*thePixMap)->bounds);
}
return theSize;
}
/*
* EstGWorldSize(bounds, depth)
*
* Purpose: Returns an estimate of the GWo
|