home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / Pict2Ascii 1.03 / Utils / GreyGWorld.cp next >
Encoding:
Text File  |  1997-05-19  |  3.9 KB  |  147 lines  |  [TEXT/CWIE]

  1. // =================================================================================
  2. //    GreyGWorld.cp                            ©1997 BB's Team inc. All rights reserved
  3. // =================================================================================
  4. #include "GreyGWorld.h"
  5.  
  6.  
  7. // ---------------------------------------------------------------------------------
  8. //        • default ctor
  9. // ---------------------------------------------------------------------------------
  10. GreyGWorld::GreyGWorld (void)
  11.     : mGW (nil), mPM (nil), mLock (0)
  12. {}
  13.  
  14.  
  15. // ---------------------------------------------------------------------------------
  16. //        • Rect ctor
  17. // ---------------------------------------------------------------------------------
  18. GreyGWorld::GreyGWorld (Rect &r)
  19.     : mGW (nil), mPM (nil), mLock (0)
  20. {
  21.     SetRect (r);
  22. }
  23.  
  24.  
  25. // ---------------------------------------------------------------------------------
  26. //        • SetRect
  27. // ---------------------------------------------------------------------------------
  28. void GreyGWorld::SetRect (Rect&r)
  29. {
  30.     MakeRoom();
  31.  
  32.     // Get a gray palette (from white to black)
  33.     CTabHandle theColorTable;
  34.     theColorTable = (CTabHandle) ::GetResource ('clut', 128);
  35.     if (!theColorTable)
  36.         return;
  37.  
  38.     // Build the offscreen
  39.     GWorldPtr    theGW;
  40.     ::NewGWorld (&theGW, 8, &r, theColorTable, nil ,0);
  41.     ::ReleaseResource ( (Handle)theColorTable );
  42.  
  43.     if ( theGW!=nil ) {
  44.         mGW = theGW;
  45.         mPM = ::GetGWorldPixMap (mGW);
  46.         mStep = (**mPM).rowBytes & 0x7fff;
  47.     }
  48.  
  49. }
  50.  
  51.  
  52. // ---------------------------------------------------------------------------------
  53. //        • dtor
  54. // ---------------------------------------------------------------------------------
  55. GreyGWorld::~GreyGWorld ()
  56. {
  57.     MakeRoom();
  58. }
  59.  
  60.  
  61. // ---------------------------------------------------------------------------------
  62. //        • MakeRoom
  63. // ---------------------------------------------------------------------------------
  64. void GreyGWorld::MakeRoom (void)
  65. {
  66.     if ( IsOK() ) {
  67.         Unlock();
  68.         DisposeGWorld (mGW);
  69.         mGW = nil;
  70.         mPM  =nil;
  71.     }
  72. }
  73.  
  74.  
  75. // ---------------------------------------------------------------------------------
  76. //        • Lock
  77. // ---------------------------------------------------------------------------------
  78. void GreyGWorld::Lock(void)
  79. {
  80.     if ( IsOK() && !mLock )
  81.         ::LockPixels (mPM);
  82.     mLock++;
  83. }
  84.  
  85.  
  86. // ---------------------------------------------------------------------------------
  87. //        • Unlock
  88. // ---------------------------------------------------------------------------------
  89. void GreyGWorld::Unlock(void)
  90. {
  91.     mLock--;
  92.     if (mLock < 0)
  93.         mLock=0;
  94.     if (IsOK() && mLock!=0)
  95.         ::UnlockPixels (mPM);
  96. }
  97.  
  98.  
  99. // ---------------------------------------------------------------------------------
  100. //        • IsOK
  101. // ---------------------------------------------------------------------------------
  102. Boolean GreyGWorld::IsOK (void)
  103. {
  104.     return mGW!=nil;
  105. }
  106.  
  107.  
  108. // ---------------------------------------------------------------------------------
  109. //        • Bounds
  110. // ---------------------------------------------------------------------------------
  111. Rect GreyGWorld::Bounds(void)
  112. {
  113.     return (**mPM).bounds;
  114. }
  115.  
  116.  
  117. // ---------------------------------------------------------------------------------
  118. //        • ComputeGrey
  119. // ---------------------------------------------------------------------------------
  120. float GreyGWorld::ComputeGrey (void)
  121. {
  122.     return ComputeGrey ((**mPM).bounds);
  123. }
  124.  
  125.  
  126. // ---------------------------------------------------------------------------------
  127. //        • ComputeGrey
  128. // ---------------------------------------------------------------------------------
  129. float GreyGWorld::ComputeGrey (Rect &r)
  130. {
  131.     // address of the leftmost pixel in the pixmap of
  132.     // the line alined with the top of the Rect (sic)
  133.     unsigned char *p = (unsigned char *) GetPixBaseAddr (mPM) + mStep*r.top;
  134.     
  135.     Int32 theSum = 0;
  136.  
  137.     for (Int16 y = r.top  ; y<r.bottom ; y++) {
  138.         for (Int16 x = r.left ; x<r.right  ; x++)
  139.             theSum += p[x];
  140.         p += mStep;
  141.     }
  142.  
  143.     // The grey ramp used for the palette goes from white to black
  144.     // We have computed the inverse of the lightness
  145.     return 1. - theSum/(255.*(r.right-r.left)*(r.bottom-r.top));
  146. }
  147.