home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / sys / mac / programm / 14371 < prev    next >
Encoding:
Text File  |  1992-08-22  |  5.7 KB  |  229 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!caen!hellgate.utah.edu!fcom.cc.utah.edu!park.uvcc.edu!news.icon.com!stay
  3. From: stay@icon.com (Steve Taylor)
  4. Subject: SeedCFill
  5. Organization: Icon International, Inc., Orem, Utah
  6. Date: Sat, 22 Aug 1992 22:17:21 GMT
  7. Message-ID: <1992Aug22.221721.4483@icon.com>
  8. Lines: 219
  9.  
  10. Gentlebeings,
  11.  
  12. I've spent a week on SeedCFill and I'm having a great deal of
  13. trouble with it.  Included in this message is a sample program
  14. to illustrate only one of the many weird things I can't figure
  15. out from Inside Macintosh.  I also have "Programming Quickdraw"
  16. and that doesn't help much, either.
  17.  
  18. (1) Am I allowed to interchange pixmaps and bitmaps for either of
  19. the first two arguments?  I thought so, but the following code
  20. doesn't work at all if I change the destination bitmap to a
  21. 1-bit pixmap.
  22. (2) What does Think Reference mean by telling me to pass in
  23. an address INSIDE a bitmap?
  24. (3) In the example below, I'm just trying to offset the
  25. destination rect before doing a SeedCFill.  What's wrong?
  26.  
  27. ---------------------------------------------------------
  28. #include <QDOffscreen.h>
  29. #include <QuickDraw.h>
  30.  
  31. //
  32. //    Build a GWorld and return a pointer to it
  33. //
  34.  
  35. static GWorldPtr
  36. BuildAGWorld(short width, short height, short bitDepth) {
  37.     Rect        boundsRect;
  38.     GWorldPtr    tempGWorld;
  39.     QDErr        qErr;
  40.     
  41.     boundsRect.top = boundsRect.left = 0;
  42.     boundsRect.right = width;
  43.     boundsRect.bottom = height;
  44.         
  45.     qErr = NewGWorld(&tempGWorld, bitDepth, &boundsRect, NULL, NULL, 0L);
  46.     return tempGWorld;
  47. }
  48.  
  49. //
  50. //    Create an offscreen bit map, taken from the
  51. //    Dev. CD CalcMask example (by Edgar Lee),
  52. //    which got it from someone else:
  53. //
  54.  
  55. /* Originally written by Forrest Tanaka. */
  56. GrafPtr CreateGrafPort( bounds )
  57. Rect *bounds;
  58. {
  59.     GrafPtr    savedPort;        /* Saved GrafPtr for later restore. */
  60.     GrafPtr    newPort;        /* New GrafPort. */
  61.     Rect    localBounds;    /* Local copy of bounds. */
  62.  
  63.     GetPort( &savedPort );
  64.  
  65.     /* Set the top-left corner of bounds to (0,0). */
  66.     localBounds = *bounds;
  67.     OffsetRect( &localBounds, -bounds->left, -bounds->top );
  68.  
  69.     /* Allocate a new GrafPort. */
  70.     newPort = (GrafPtr)NewPtrClear( sizeof( GrafPort ) );
  71.     
  72.     if (newPort != nil)
  73.     {
  74.         /* Initialize the new port and make the current port. */
  75.         OpenPort( newPort );
  76.  
  77.         /* Initialize and allocate the bitmap. */
  78.         newPort->portBits.bounds = localBounds;
  79.           newPort->portBits.rowBytes = ((localBounds.right + 15) >> 4) << 1;
  80.         newPort->portBits.baseAddr =
  81.                 NewPtrClear( newPort->portBits.rowBytes *
  82.                 (long)localBounds.bottom );
  83.         if (newPort->portBits.baseAddr != nil)
  84.         {
  85.             /* Clean up the new port. */
  86.             newPort->portRect = localBounds;
  87.             ClipRect( &localBounds );
  88.             RectRgn( newPort->visRgn, &localBounds );
  89.             EraseRect( &localBounds );
  90.         }
  91.         else
  92.         {
  93.             /* Allocation failed; deallocate the port. */
  94.             ClosePort( newPort );
  95.             DisposPtr( (Ptr)newPort );
  96.             newPort = nil;
  97.         }
  98.     }
  99.     
  100.     SetPort( savedPort );
  101.     return newPort;
  102. }
  103.  
  104. /* Originally written by Forrest Tanaka. */
  105. void DisposeGrafPort( doomedPort )
  106. GrafPtr doomedPort;
  107. {
  108.     ClosePort( doomedPort );
  109.     DisposPtr( doomedPort->portBits.baseAddr );
  110.     DisposPtr( (Ptr)doomedPort );
  111. }
  112.  
  113. //    My stuff:
  114.  
  115. static void
  116. DoStuff() {
  117.     WindowPtr        theWindow;
  118.     Rect            rBounds = {50, 50, 450, 500};
  119.     Rect            tempRect, destRect;
  120.     PixMapHandle    sourcePM;
  121.     QDErr            qErr;
  122.     GWorldPtr        sourceWorld;
  123.     GrafPtr            destMap, oldPort;
  124.     CGrafPtr        tempGrafPtr;
  125.     GDHandle        tempGDHandle;
  126.     
  127.     GetPort(&oldPort);
  128.     //    New window to view results
  129.     theWindow = NewWindow(NULL, &rBounds, "\p", true
  130.             , plainDBox, (WindowPtr)(-1L), false, 0L);
  131.     SetPort(theWindow);
  132.     
  133.     //    4 bit pixmap for source
  134.     sourceWorld = BuildAGWorld(100, 100, 4);
  135.     
  136.     //    bitmap for mask
  137.     SetRect(&tempRect, 0, 0, 100, 100);
  138.     destMap = CreateGrafPort(&tempRect);
  139.     
  140.     //    save the port
  141.     GetGWorld(&tempGrafPtr, &tempGDHandle);
  142.     qErr = QDError();
  143.     
  144.     //    Clean up the pixmap
  145.     SetGWorld(sourceWorld, NULL);
  146.     qErr = QDError();
  147.     EraseRect(&thePort->portRect);
  148.     
  149.     //    Draw a sample drawing on the pixmap
  150.     SetRect(&tempRect, 10, 10, 20, 20);
  151.     ForeColor(blueColor);
  152.     FillRect(&tempRect, black);
  153.     ForeColor(blackColor);
  154.     
  155.     //    restore the port
  156.     SetGWorld(tempGrafPtr, tempGDHandle);
  157.     qErr = QDError();
  158.     
  159.     //    Now perform the seed fill.  Here's the weird part.
  160.     //    If I leave destRect where it is, the following
  161.     //    SeedCFill works fine.  If I offset destRect
  162.     //    vertically down 29 pixels, it still works fine.
  163.     //    If I offset it down 30 pixels, SeedCFill fails.
  164.     //    (It just fills the destRect with white.)
  165.     //    Why?
  166.     
  167.     SetRect(&tempRect, 0, 0, 30, 30);
  168.     destRect = tempRect;
  169.     //    OffsetRect(&destRect, 0, 29);    //    works
  170.     //    OffsetRect(&destRect, 0, 30);    //    doesn't work
  171.     
  172.     sourcePM = GetGWorldPixMap(sourceWorld);
  173.     LockPixels(sourcePM);
  174.     
  175.     SeedCFill(*sourcePM, &destMap->portBits
  176.             , &tempRect, &destRect, 0, 0, NULL, 0L);
  177.     
  178.     UnlockPixels(sourcePM);
  179.     
  180.     //    Show source
  181.     SetRect(&tempRect, 0, 0, 100, 100);
  182.     destRect = tempRect;
  183.     OffsetRect(&destRect, 10, 10);
  184.     sourcePM = GetGWorldPixMap(sourceWorld);
  185.     LockPixels(sourcePM);
  186.     CopyBits((BitMap *)*sourcePM, &thePort->portBits
  187.             , &tempRect, &destRect, srcCopy, NULL);
  188.     UnlockPixels(sourcePM);
  189.     
  190.     //    Show destination
  191.     SetRect(&tempRect, 0, 0, 100, 100);
  192.     destRect = tempRect;
  193.     OffsetRect(&destRect, 120, 10);
  194.     CopyBits(&destMap->portBits, &thePort->portBits
  195.             , &tempRect, &destRect, srcCopy, NULL);
  196.     
  197.     SetPort(&oldPort);    //    restore original port
  198.     
  199.     DisposeGWorld(sourceWorld);
  200.     DisposeGrafPort(destMap);
  201.     DisposeWindow(theWindow);
  202. }
  203.  
  204. static
  205. InitThings(void) {
  206.     InitGraf(&thePort);
  207.     InitFonts();
  208.     InitWindows();
  209.     InitMenus();
  210.     TEInit();
  211.     InitDialogs(NULL);
  212.     InitCursor();
  213. }
  214.  
  215.  
  216. main() {
  217.     InitThings();
  218.     DoStuff();
  219. }
  220.  
  221. ---------------------------------------------------------------
  222.  
  223. This code runs in Think C. 5.0.2 on a IIci and a grayscale monitor.
  224.  
  225. Thanks.
  226.  
  227. -- Steve Taylor (stay@icon.com)
  228. trACE Development
  229.