home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Sample Code / Image Compression Mgr / Compress Picture FKEY / fkey.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-07  |  2.6 KB  |  105 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        fkey.c
  3.  
  4.     Written by:    Peter Hoddie
  5.  
  6.     Copyright:    © 1992-94 by Apple Computer, Inc., all rights reserved.
  7.  
  8.   Change History (most recent first):
  9.  
  10.   <1>         12/4/94        khs        changed the format of the file to the new look and feel
  11.  
  12. */
  13.  
  14.  
  15. // INCLUDES
  16. #include <Memory.h>
  17. #include <QuickDraw.h>
  18. #include <GestaltEqu.h>
  19. #include <Scrap.h>
  20.  
  21. #include <ImageCompression.h>
  22. #include <QuickTimeComponents.h>
  23.  
  24.  
  25.  
  26. // GLUE
  27. // an attempt to avoid linking in glue from hell.....
  28. #pragma parameter __D0 CoolGestalt(__D0,__A1)
  29. pascal OSErr CoolGestalt(OSType selector,long *response)
  30.     = {0xA1AD,0x2288}; 
  31.  
  32. // avoid more glue, since THINK C headers always use glue
  33. #pragma parameter __D0 coolGetHandleSize(__A0)
  34. pascal Size coolGetHandleSize(Handle h)
  35.     = 0xA025;
  36.  
  37.  
  38. // MAIN
  39. void main(void)
  40. {
  41.     long response;
  42.     long offset;
  43.     ComponentInstance stdc = 0;
  44.     PicHandle thePict = 0;
  45.     PicHandle newPict = 0;
  46.      PScrapStuff scrapInfo = InfoScrap();
  47.     Handle scrapHandle;
  48.     long scrapOffset = 0;
  49.     Boolean foundPICT = false;
  50.  
  51.     // makes sure Image Compression Manager is available
  52.     if (CoolGestalt(gestaltCompressionMgr, &response) != noErr) {
  53.         SysBeep(1);
  54.         return;
  55.     }
  56.  
  57.     // make sure there is a real pict on the scrap (hate to be fooled by some translation stuff)
  58.     if (!scrapInfo || !(scrapHandle = scrapInfo->scrapHandle) )
  59.         return;
  60.     while (scrapOffset < scrapInfo->scrapSize) {
  61.         long itemSize;
  62.  
  63.         foundPICT = *(long *)(*scrapHandle + scrapOffset) == 'PICT';
  64.         if (foundPICT) break;
  65.  
  66.         itemSize = *(long *)(*scrapHandle + scrapOffset + 4);
  67.         itemSize += (itemSize & 1);            // must be even
  68.         scrapOffset = scrapOffset + 8 + itemSize;
  69.     }
  70.     if (!foundPICT)
  71.         return;
  72.  
  73.     // get the Picture from the scrap
  74.     thePict = (PicHandle)NewHandle(4);
  75.     if (MemError()) goto bail;
  76.     if (GetScrap((Handle)thePict, 'PICT', &offset) <= 0)
  77.         goto bail;
  78.  
  79.     // open the standard compression component
  80.     stdc = OpenDefaultComponent(StandardCompressionType, StandardCompressionSubType);
  81.     if (!stdc)
  82.         goto bail;
  83.  
  84.     // compress the picture with whatever compressor Standard Compression likes
  85.     newPict = (PicHandle)NewHandle(4);
  86.     if (MemError()) goto bail;
  87.     SCDefaultPictHandleSettings(stdc, thePict, 0);
  88.     if (SCCompressPicture(stdc, thePict, newPict) != noErr)
  89.         goto bail;
  90.  
  91.     // make sure it did something useful
  92.     if (coolGetHandleSize((Handle)newPict) < coolGetHandleSize((Handle)thePict)) {
  93.         // fix up the scrap
  94.         ZeroScrap();
  95.         MoveHHi((Handle)newPict);
  96.         HLock((Handle)newPict);
  97.         PutScrap(coolGetHandleSize((Handle)newPict), 'PICT', (Ptr)*newPict);    // cross your fingers
  98.     }
  99.  
  100. bail:
  101.     if (stdc) CloseComponent(stdc);
  102.     if (thePict) KillPicture(thePict);
  103.     if (newPict) KillPicture(newPict);
  104. }
  105.