fkey.c

/*
    File:       fkey.c
 
    Written by: Peter Hoddie
 
    Copyright:  © 1992-94 by Apple Computer, Inc., all rights reserved.
 
  Change History (most recent first):
 
  <1>       12/4/94     khs     changed the format of the file to the new look and feel
 
*/
 
 
// INCLUDES
#include <Memory.h>
#include <QuickDraw.h>
#include <GestaltEqu.h>
#include <Scrap.h>
 
#include <ImageCompression.h>
#include <QuickTimeComponents.h>
 
 
 
// GLUE
// an attempt to avoid linking in glue from hell.....
#pragma parameter __D0 CoolGestalt(__D0,__A1)
pascal OSErr CoolGestalt(OSType selector,long *response)
    = {0xA1AD,0x2288}; 
 
// avoid more glue, since THINK C headers always use glue
#pragma parameter __D0 coolGetHandleSize(__A0)
pascal Size coolGetHandleSize(Handle h)
    = 0xA025;
 
 
// MAIN
void main(void)
{
    long response;
    long offset;
    ComponentInstance stdc = 0;
    PicHandle thePict = 0;
    PicHandle newPict = 0;
    PScrapStuff scrapInfo = InfoScrap();
    Handle scrapHandle;
    long scrapOffset = 0;
    Boolean foundPICT = false;
 
    // makes sure Image Compression Manager is available
    if (CoolGestalt(gestaltCompressionMgr, &response) != noErr) {
        SysBeep(1);
        return;
    }
 
    // make sure there is a real pict on the scrap (hate to be fooled by some translation stuff)
    if (!scrapInfo || !(scrapHandle = scrapInfo->scrapHandle) )
        return;
    while (scrapOffset < scrapInfo->scrapSize) {
        long itemSize;
 
        foundPICT = *(long *)(*scrapHandle + scrapOffset) == 'PICT';
        if (foundPICT) break;
 
        itemSize = *(long *)(*scrapHandle + scrapOffset + 4);
        itemSize += (itemSize & 1);         // must be even
        scrapOffset = scrapOffset + 8 + itemSize;
    }
    if (!foundPICT)
        return;
 
    // get the Picture from the scrap
    thePict = (PicHandle)NewHandle(4);
    if (MemError()) goto bail;
    if (GetScrap((Handle)thePict, 'PICT', &offset) <= 0)
        goto bail;
 
    // open the standard compression component
    stdc = OpenDefaultComponent(StandardCompressionType, StandardCompressionSubType);
    if (!stdc)
        goto bail;
 
    // compress the picture with whatever compressor Standard Compression likes
    newPict = (PicHandle)NewHandle(4);
    if (MemError()) goto bail;
    SCDefaultPictHandleSettings(stdc, thePict, 0);
    if (SCCompressPicture(stdc, thePict, newPict) != noErr)
        goto bail;
 
    // make sure it did something useful
    if (coolGetHandleSize((Handle)newPict) < coolGetHandleSize((Handle)thePict)) {
        // fix up the scrap
        ZeroScrap();
        MoveHHi((Handle)newPict);
        HLock((Handle)newPict);
        PutScrap(coolGetHandleSize((Handle)newPict), 'PICT', (Ptr)*newPict);    // cross your fingers
    }
 
bail:
    if (stdc) CloseComponent(stdc);
    if (thePict) KillPicture(thePict);
    if (newPict) KillPicture(newPict);
}