home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / Concentration / Unique.c < prev   
Encoding:
C/C++ Source or Header  |  1994-02-24  |  2.8 KB  |  128 lines  |  [TEXT/KAHL]

  1. /*
  2.  * This is some stuff I was writing to try to detect duplicate images.  It
  3.  * works for ICON/ICON and SICN/SICN tests.  It doesn't work when trying to
  4.  * detect whether any regular ICON is the same as the B/W image in a cicn.
  5.  * I don't know why.  Anyway, this code isn't part of the application, so
  6.  * it's necessary to make sure there aren't any duplicate images in the resource
  7.  * fork.
  8.  */
  9.  
  10. # include    "TransSkel.h"
  11. # if ENABLE_DEBUG
  12. # include    "Debug.h"
  13. # endif
  14.  
  15. # include    "Concentration.h"
  16.  
  17.  
  18.  
  19. static void
  20. GetCIconInfo (Handle h, Ptr *p, short *len)
  21. {
  22. CIconHandle    c = (CIconHandle) h;
  23. short    ht, maskSize;
  24.  
  25.     ht = (**c).iconPMap.bounds.bottom - (**c).iconPMap.bounds.top;
  26.     maskSize = (**c).iconMask.rowBytes * ht;
  27.     *p = (Ptr) (&(**c).iconMaskData + maskSize);
  28.     *len = (**c).iconBMap.rowBytes * ht;
  29. }
  30.  
  31.  
  32. static Boolean
  33. PtrCompare (Ptr p1, Ptr p2, short size)
  34. {
  35. short    i;
  36.  
  37.     for (i = 0; i < size; i++)
  38.     {
  39.         if (p1[i] != p2[i])
  40.             return (false);
  41.     }
  42.     return (true);
  43. }
  44.  
  45.  
  46. static Boolean
  47. HandleCompare (Handle h1, Handle h2, short size)
  48. {
  49.     return (PtrCompare (*h1, *h2, size));    /* PtrCompare doesn't move memory */
  50. }
  51.         
  52.  
  53. /*
  54.  * Compare a new ICON or cicn hunk of data to see if its equivalent is already
  55.  * in the large piece array.  If a given piece has a data handle that's the same
  56.  * size as the new data, compare the bytes directly.  This takes case of ICON/ICON
  57.  * and cicn/cicn test.  For ICON/cicn comparisons, compare the ICON against the
  58.  * B/W bitmap from the cicn.  (It's necessary to do this because a cicn B/W bitmap
  59.  * may look identical to an ICON on a B/W monitor.
  60.  */
  61.  
  62. Boolean
  63. UniqueIcon (Piece pieceArray[], short nPieces, Handle h, short type)
  64. {
  65. Piece    *p;
  66. Handle    h2;
  67. short    size;
  68. short    i;
  69. Ptr        cPtr;
  70. short    cSize;
  71.  
  72.     size = GetHandleSize (h);
  73.     for (i = 0; i < nPieces; i++)
  74.     {
  75.         p = &pieceArray[i];
  76.         h2 = p->pieceData;
  77.         if (size == GetHandleSize (h2))
  78.         {
  79.             if (HandleCompare (h, h2, size))
  80.                 return (false);
  81.         }
  82.         if (type == iconType && p->pieceType == cicnType)        /* ICON vs. cicn */
  83.         {
  84.             GetCIconInfo (p->pieceData, &cPtr, &cSize);
  85.             if (size == cSize && PtrCompare (*h, cPtr, size))
  86. {
  87. # if ENABLE_DEBUG
  88. DisplayCString ("cicn/ICON duplicate found.\r");
  89. # endif
  90.                 return (false);
  91. }
  92.         }
  93.         else if (type == cicnType && p->pieceType == iconType)    /* cicn vs. ICON */
  94.         {
  95.             GetCIconInfo (h, &cPtr, &cSize);
  96.             if (size == cSize && PtrCompare (cPtr, *(p->pieceData), size))
  97. {
  98. # if ENABLE_DEBUG
  99. DisplayCString ("cicn/ICON duplicate found.\r");
  100. # endif
  101.                 return (false);
  102. }
  103.         }
  104.     }
  105.     return (true);
  106. }
  107.         
  108.  
  109. Boolean
  110. UniqueSicn (Piece pieceArray[], short nPieces, Handle h, short type)
  111. {
  112. Handle    h2;
  113. short    size;
  114. short    i;
  115.  
  116.     size = GetHandleSize (h);
  117.     for (i = 0; i < nPieces; i++)
  118.     {
  119.         h2 = pieceArray[i].pieceData;
  120.         if (size == GetHandleSize (h2))
  121.         {
  122.             if (HandleCompare (h, h2, size))
  123.                 return (false);
  124.         }
  125.     }
  126.     return (true);
  127. }
  128.