home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / QuickDraw / CopyDeepMask / CopyDeepMask.c next >
Encoding:
C/C++ Source or Header  |  1993-12-03  |  5.3 KB  |  221 lines  |  [TEXT/MPS ]

  1. /*****************************************************************************************
  2.      Application:        CopyDeepMask Demo
  3.      
  4.      Description:        This program demonstrates the use of CopyDeepMask using 2 PICTS
  5.                          one a photograph and the other a triangular mask.     It uses 2 offscreen
  6.                          gworlds to hold the source and mask pixmaps.  CopyDeepMask is then used
  7.                          to create the masked image and display it in the application window.  The
  8.                          source, mask, and destination rectangles are all the same size in order
  9.                          avoid altering pixel sizes.
  10.                          
  11.      Programmer:            Kevin Mellander
  12.      Organization:        Apple Computer, Inc.
  13.      Department            Developer Technical Support, DTS
  14.      Language/Envir.        C/Think C Version 6.0.1
  15.      Date Created        11/22/93                              
  16.  
  17.  *****************************************************************************************/
  18.  
  19. #include <Quickdraw.h>
  20. #include <QDOffscreen.h>
  21. #include <Windows.h>
  22. #include <Dialogs.h>
  23. #include <Menus.h>
  24. #include <Types.h>
  25. #include <Memory.h>
  26. #include <Fonts.h>
  27. #include <OSEvents.h>
  28. #include <ToolUtils.h>
  29.  
  30. #define windID 128
  31. #define inFront -1
  32. #define sleepTime 30
  33.  
  34. #define     srcPicID    130
  35. #define     maskPicID    131
  36.  
  37. Boolean        continueThis;
  38. WindowPtr    mainWindow;
  39.  
  40. void InitToolbox(void);
  41. void doEventLoop(void);
  42. void doDrag(WindowPtr,Point);
  43. void doUpdateEvent(void);
  44.  
  45.  
  46. void main(void)
  47. {
  48.     
  49.     InitToolbox();
  50.         
  51.     mainWindow = GetNewCWindow(windID,nil,(WindowPtr)inFront);
  52.     
  53.     continueThis = true;
  54.     
  55.     doEventLoop();
  56. }
  57.  
  58. void InitToolbox(void)
  59. {
  60.     MoreMasters();
  61.     MoreMasters();
  62.     MoreMasters();
  63.     
  64.     MaxApplZone();
  65.  
  66.     InitGraf((Ptr)&qd.thePort);
  67.     InitFonts();
  68.     InitWindows();
  69.     InitMenus();
  70.     TEInit();
  71.     InitCursor();
  72.     InitDialogs(nil);
  73.     FlushEvents(everyEvent, 0);
  74.     
  75. }
  76.  
  77. void doEventLoop(void)
  78. {
  79.     EventRecord    event;
  80.     WindowPtr    window;
  81.     short        hitArea;
  82.         
  83.     
  84.     while (continueThis)
  85.     {    
  86.         if (WaitNextEvent(everyEvent,&event,sleepTime,nil))
  87.             
  88.             if (event.what == updateEvt)    
  89.                 doUpdateEvent();
  90.                 
  91.             else if (event.what == mouseDown)
  92.             {
  93.                 hitArea = FindWindow(event.where,&window);
  94.                 
  95.                 if (hitArea == inDrag)
  96.                     doDrag(window,event.where);
  97.                     
  98.                 else if (hitArea == inGoAway)
  99.                     if (TrackGoAway (window,event.where))
  100.                         return;
  101.             }
  102.             
  103.     } 
  104. }
  105.         
  106. void doDrag (WindowPtr winPtr,Point mouseLoc)
  107. {
  108.     Rect     dragRect;
  109.     Rect    bndsRect;
  110.     
  111.     bndsRect = qd.screenBits.bounds; /*screenBits is a QuickDraw global variable with the same structure as portBits (BitMap)*/
  112.     InsetRect(&dragRect,10,10);
  113.     DragWindow(winPtr,mouseLoc,&bndsRect);
  114.     
  115. }
  116.  
  117. void doUpdateEvent(void)
  118. {
  119.  
  120.     Rect            bndsRectSrc,bndsRectMask,srcRect,maskRect,destRect;
  121.     QDErr            gWorldErr;
  122.     GWorldPtr        offscreenGWorldSource;
  123.     GWorldPtr        offscreenGWorldMask;
  124.     short            pixelDepthSource = 32;
  125.     short            pixelDepthMask = 1;
  126.     GWorldFlags        flags = 0;
  127.     GDHandle        currGDevice;
  128.     GWorldPtr        currGWorldPort;
  129.     PicHandle        srcPicHdl,maskPicHdl;
  130.     PixMapHandle    srcPMHdl,maskPMHdl;
  131.     Boolean            pmLock;
  132.  
  133.     
  134.     //Set up the bounds rectangle for the source and mask gWorlds    
  135.     bndsRectSrc.top = 32;
  136.     bndsRectSrc.left = 64;
  137.     bndsRectSrc.bottom = 160;
  138.     bndsRectSrc.right = 192;
  139.     
  140.     bndsRectMask = bndsRectSrc;
  141.     
  142.     //Make the source,mask and destination rectangles the same 
  143.     // size as the associated gWorlds
  144.     srcRect = bndsRectSrc;
  145.     maskRect = bndsRectSrc;
  146.     destRect = bndsRectSrc;
  147.     
  148.     //Fetch the current port and gdevice and save them for later
  149.     GetGWorld(&currGWorldPort,&currGDevice);
  150.         
  151.     //Set up our source and mask gWorlds
  152.     gWorldErr = NewGWorld(&offscreenGWorldSource,pixelDepthSource,&bndsRectSrc,0,nil,flags);
  153.     if(gWorldErr != noErr)
  154.         DebugStr("\pthe first NewGWorld call failed");
  155.     else 
  156.     {
  157.         //lock the offscreen buffer
  158.         srcPMHdl = GetGWorldPixMap(offscreenGWorldSource);
  159.         pmLock = LockPixels(srcPMHdl);
  160.         if (!pmLock)
  161.             DebugStr("\pthe first LockPixels call failed");
  162.     }
  163.         
  164.     gWorldErr = NewGWorld(&offscreenGWorldMask,pixelDepthMask,&bndsRectMask,0,nil,flags);
  165.     if(gWorldErr != noErr)
  166.         DebugStr("\pthe second NewGWorld call failed"); 
  167.     else 
  168.     {
  169.         //lock the offscreen buffer
  170.         maskPMHdl = GetGWorldPixMap(offscreenGWorldMask);
  171.         pmLock = LockPixels(maskPMHdl);
  172.         if (!pmLock)
  173.             DebugStr("\pthe second LockPixels call failed");
  174.     }
  175.         
  176.     //Set the current graphics world to my offscreen source and draw into it
  177.     SetGWorld((CGrafPtr) offscreenGWorldSource,nil);
  178.     srcPicHdl = GetPicture(srcPicID);
  179.     if (srcPicHdl==nil)
  180.         DebugStr("\pthe first call to GetPicture failed");
  181.     EraseRect(&srcRect);
  182.     DrawPicture(srcPicHdl,&srcRect);
  183.     
  184.     //Set the current graphics world to my offscreen mask and draw into it
  185.     SetGWorld((CGrafPtr)offscreenGWorldMask,nil);
  186.     GetPicture(maskPicID);
  187.     maskPicHdl = GetPicture(maskPicID);
  188.     if (maskPicHdl==nil)
  189.         DebugStr("\pthe second call to GetPicture failed");
  190.     EraseRect(&maskRect);
  191.     DrawPicture(maskPicHdl,&maskRect);
  192.  
  193.     //Set the current graphics port to my application window for drawing into
  194.     SetGWorld((CGrafPtr)mainWindow,nil);
  195.     BeginUpdate(mainWindow);
  196.  
  197.     //Now for the whole point of this. . .call CopyDeepMask
  198.     CopyDeepMask(
  199.                 (BitMap *) &(offscreenGWorldSource->portPixMap),
  200.                 (BitMap *) &(offscreenGWorldMask->portPixMap),
  201.                 &(mainWindow->portBits),
  202.                 &srcRect,
  203.                 &maskRect,
  204.                 &destRect,
  205.                 srcCopy,
  206.                 nil);
  207.                 
  208.     EndUpdate(mainWindow);
  209.     
  210.     //Unlock the offscreen buffer
  211.     UnlockPixels(srcPMHdl);
  212.     UnlockPixels(maskPMHdl);
  213.     
  214.     //Restore the saved port and gdevice
  215.     SetGWorld(currGWorldPort,currGDevice);
  216.         
  217.     //Dispose of the gWorlds
  218.     DisposeGWorld(offscreenGWorldSource);
  219.     DisposeGWorld(offscreenGWorldMask);
  220.  
  221. }