home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************************************************
- Application: CopyDeepMask Demo
-
- Description: This program demonstrates the use of CopyDeepMask using 2 PICTS
- one a photograph and the other a triangular mask. It uses 2 offscreen
- gworlds to hold the source and mask pixmaps. CopyDeepMask is then used
- to create the masked image and display it in the application window. The
- source, mask, and destination rectangles are all the same size in order
- avoid altering pixel sizes.
-
- Programmer: Kevin Mellander
- Organization: Apple Computer, Inc.
- Department Developer Technical Support, DTS
- Language/Envir. C/Think C Version 6.0.1
- Date Created 11/22/93
-
- *****************************************************************************************/
-
- #include <Quickdraw.h>
- #include <QDOffscreen.h>
- #include <Windows.h>
- #include <Dialogs.h>
- #include <Menus.h>
- #include <Types.h>
- #include <Memory.h>
- #include <Fonts.h>
- #include <OSEvents.h>
- #include <ToolUtils.h>
-
- #define windID 128
- #define inFront -1
- #define sleepTime 30
-
- #define srcPicID 130
- #define maskPicID 131
-
- Boolean continueThis;
- WindowPtr mainWindow;
-
- void InitToolbox(void);
- void doEventLoop(void);
- void doDrag(WindowPtr,Point);
- void doUpdateEvent(void);
-
-
- void main(void)
- {
-
- InitToolbox();
-
- mainWindow = GetNewCWindow(windID,nil,(WindowPtr)inFront);
-
- continueThis = true;
-
- doEventLoop();
- }
-
- void InitToolbox(void)
- {
- MoreMasters();
- MoreMasters();
- MoreMasters();
-
- MaxApplZone();
-
- InitGraf((Ptr)&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitCursor();
- InitDialogs(nil);
- FlushEvents(everyEvent, 0);
-
- }
-
- void doEventLoop(void)
- {
- EventRecord event;
- WindowPtr window;
- short hitArea;
-
-
- while (continueThis)
- {
- if (WaitNextEvent(everyEvent,&event,sleepTime,nil))
-
- if (event.what == updateEvt)
- doUpdateEvent();
-
- else if (event.what == mouseDown)
- {
- hitArea = FindWindow(event.where,&window);
-
- if (hitArea == inDrag)
- doDrag(window,event.where);
-
- else if (hitArea == inGoAway)
- if (TrackGoAway (window,event.where))
- return;
- }
-
- }
- }
-
- void doDrag (WindowPtr winPtr,Point mouseLoc)
- {
- Rect dragRect;
- Rect bndsRect;
-
- bndsRect = qd.screenBits.bounds; /*screenBits is a QuickDraw global variable with the same structure as portBits (BitMap)*/
- InsetRect(&dragRect,10,10);
- DragWindow(winPtr,mouseLoc,&bndsRect);
-
- }
-
- void doUpdateEvent(void)
- {
-
- Rect bndsRectSrc,bndsRectMask,srcRect,maskRect,destRect;
- QDErr gWorldErr;
- GWorldPtr offscreenGWorldSource;
- GWorldPtr offscreenGWorldMask;
- short pixelDepthSource = 32;
- short pixelDepthMask = 1;
- GWorldFlags flags = 0;
- GDHandle currGDevice;
- GWorldPtr currGWorldPort;
- PicHandle srcPicHdl,maskPicHdl;
- PixMapHandle srcPMHdl,maskPMHdl;
- Boolean pmLock;
-
-
- //Set up the bounds rectangle for the source and mask gWorlds
- bndsRectSrc.top = 32;
- bndsRectSrc.left = 64;
- bndsRectSrc.bottom = 160;
- bndsRectSrc.right = 192;
-
- bndsRectMask = bndsRectSrc;
-
- //Make the source,mask and destination rectangles the same
- // size as the associated gWorlds
- srcRect = bndsRectSrc;
- maskRect = bndsRectSrc;
- destRect = bndsRectSrc;
-
- //Fetch the current port and gdevice and save them for later
- GetGWorld(&currGWorldPort,&currGDevice);
-
- //Set up our source and mask gWorlds
- gWorldErr = NewGWorld(&offscreenGWorldSource,pixelDepthSource,&bndsRectSrc,0,nil,flags);
- if(gWorldErr != noErr)
- DebugStr("\pthe first NewGWorld call failed");
- else
- {
- //lock the offscreen buffer
- srcPMHdl = GetGWorldPixMap(offscreenGWorldSource);
- pmLock = LockPixels(srcPMHdl);
- if (!pmLock)
- DebugStr("\pthe first LockPixels call failed");
- }
-
- gWorldErr = NewGWorld(&offscreenGWorldMask,pixelDepthMask,&bndsRectMask,0,nil,flags);
- if(gWorldErr != noErr)
- DebugStr("\pthe second NewGWorld call failed");
- else
- {
- //lock the offscreen buffer
- maskPMHdl = GetGWorldPixMap(offscreenGWorldMask);
- pmLock = LockPixels(maskPMHdl);
- if (!pmLock)
- DebugStr("\pthe second LockPixels call failed");
- }
-
- //Set the current graphics world to my offscreen source and draw into it
- SetGWorld((CGrafPtr) offscreenGWorldSource,nil);
- srcPicHdl = GetPicture(srcPicID);
- if (srcPicHdl==nil)
- DebugStr("\pthe first call to GetPicture failed");
- EraseRect(&srcRect);
- DrawPicture(srcPicHdl,&srcRect);
-
- //Set the current graphics world to my offscreen mask and draw into it
- SetGWorld((CGrafPtr)offscreenGWorldMask,nil);
- GetPicture(maskPicID);
- maskPicHdl = GetPicture(maskPicID);
- if (maskPicHdl==nil)
- DebugStr("\pthe second call to GetPicture failed");
- EraseRect(&maskRect);
- DrawPicture(maskPicHdl,&maskRect);
-
- //Set the current graphics port to my application window for drawing into
- SetGWorld((CGrafPtr)mainWindow,nil);
- BeginUpdate(mainWindow);
-
- //Now for the whole point of this. . .call CopyDeepMask
- CopyDeepMask(
- (BitMap *) &(offscreenGWorldSource->portPixMap),
- (BitMap *) &(offscreenGWorldMask->portPixMap),
- &(mainWindow->portBits),
- &srcRect,
- &maskRect,
- &destRect,
- srcCopy,
- nil);
-
- EndUpdate(mainWindow);
-
- //Unlock the offscreen buffer
- UnlockPixels(srcPMHdl);
- UnlockPixels(maskPMHdl);
-
- //Restore the saved port and gdevice
- SetGWorld(currGWorldPort,currGDevice);
-
- //Dispose of the gWorlds
- DisposeGWorld(offscreenGWorldSource);
- DisposeGWorld(offscreenGWorldMask);
-
- }