home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / Pict2Ascii 1.03 / Src / ImageLight.cp < prev    next >
Encoding:
Text File  |  1997-05-22  |  3.3 KB  |  164 lines  |  [TEXT/CWIE]

  1. // =================================================================================
  2. //    ImageLight.cp                                    ©1997 BB's Team inc. All rights reserved
  3. // =================================================================================
  4. #include "ImageLight.h"
  5. #include "PL_Utils.h"
  6.  
  7.  
  8. // Constructor 
  9. ImageLight::ImageLight (void)
  10.     : mUpToDate (false)
  11.     , mLight (nil)
  12.     , mGrey (nil)
  13. {
  14.     mWidth = mHeight = mWidthN = mHeightN = 0;
  15. }
  16.  
  17.  
  18. // Destructor
  19. ImageLight::~ImageLight()
  20.     if ( mLight != nil )
  21.         DisposeHandle (mLight);
  22. }
  23.  
  24.  
  25. // The text characteristics has changed
  26. void ImageLight::SetTextTraits (TextTraitsRecord &inTraits)
  27. {
  28.     // Compute one char's sizes in pixels
  29.     Int32 ascent, newWidth, newHeight;
  30.     PL_Utils::ComputeBBox (inTraits, inTraits.size, newWidth, newHeight, ascent);
  31.  
  32.     mUpToDate = mUpToDate && newWidth==mWidth && newHeight==mHeight;
  33.     MakeRoom ();
  34.  
  35.     mWidth = newWidth;
  36.     mHeight= newHeight;
  37.  
  38.     SetLineCol();
  39.  
  40.     return;
  41. }
  42.  
  43. // Make room.
  44. void ImageLight::MakeRoom (Boolean force)
  45. {
  46.     if (mLight==nil)
  47.         return;
  48.  
  49.     if ( force || !mUpToDate ) {
  50.         DisposeHandle (mLight);
  51.         mLight=nil;
  52.     }
  53. }
  54.  
  55.  
  56. // Set lines and columns
  57. void ImageLight::SetLineCol (void)
  58. {
  59.     if (mWidth && mHeight) {
  60.         mWidthN  = mPictWidth  / mWidth;
  61.         mHeightN = mPictHeight / mHeight;
  62.     }
  63. }
  64.     
  65.  
  66. // Set the Grey Offscreen GWorld
  67. void ImageLight::SetGreyWorld (GreyGWorld *inGrey)
  68. {
  69.     mUpToDate = false;
  70.     mGrey = inGrey;
  71.     MakeRoom();
  72.  
  73.     // Compute number of lines and columns in the text
  74.     Rect picFrame = mGrey->Bounds();
  75.     mPictWidth = picFrame.right  - picFrame.left;
  76.     mPictHeight= picFrame.bottom - picFrame.top;
  77.     SetLineCol();
  78. }
  79.  
  80.  
  81. // IsUpToDate
  82. Boolean ImageLight::IsUpToDate (void)
  83. {
  84.     return mUpToDate;
  85. }
  86.  
  87.  
  88. // accessor
  89. float& ImageLight::light (int n)
  90. {
  91.     return ((float*)(*mLight))[n];
  92. }
  93.  
  94.  
  95. // Returns total text size
  96. Int32 ImageLight::GetTextSize (void)
  97. {
  98.     return mHeightN*(mWidthN+1);
  99. }
  100.  
  101.  
  102. // ---------------------------------------------------------------------------------
  103. //        • Update
  104. // ---------------------------------------------------------------------------------
  105. void ImageLight::Update (void)
  106. {
  107.     if (mUpToDate)
  108.         return;
  109.  
  110.     // Find room.
  111.     MakeRoom ();
  112.     PL_Utils::ForceNewHandle ( mLight, sizeof(float)*GetTextSize() );
  113.     if (mLight==nil)
  114.         return;
  115.  
  116.     // lack thereof crashes the 68k-running-on-PPC version !
  117.     mGrey->Lock();
  118.  
  119.     // a rect the size of a character
  120.     Rect theComputingRect;
  121.     ::SetRect (&theComputingRect, 0, 0, mWidth, mHeight);
  122.     
  123.     // Move this rect across the whole picture
  124.     Int32 k = 0;
  125.     for (Int16 i=0 ; i<mHeightN ; i++) {
  126.         for (Int16 j=0 ; j<mWidthN ; j++) {
  127.             light(k++) = mGrey->ComputeGrey (theComputingRect);
  128.             ::OffsetRect (&theComputingRect, mWidth, 0);
  129.         }
  130.  
  131.         // apply a carriage return to the rect !
  132.         ::OffsetRect (&theComputingRect, -mWidthN*mWidth, mHeight);
  133.         light(k) = light(k-1);    // will match '\r' at end of line
  134.         k++;
  135.     }
  136.  
  137.     mGrey->Unlock();
  138.     mUpToDate = true;
  139. }
  140.  
  141.  
  142. // ---------------------------------------------------------------------------------
  143. //        • GetMinMax
  144. // ---------------------------------------------------------------------------------
  145. void ImageLight::GetMinMax (float &ioMin, float &ioMax)
  146. {
  147.     const float epsilon = 0.01;
  148.     float u;
  149.     
  150.     ioMin = ioMax = light(0);
  151.     for (Int32 i = 1 ; i<GetTextSize() ; i++)
  152.         if ( (u=light(i)) > ioMax )
  153.             ioMax = u;
  154.         else if ( u < ioMin )
  155.             ioMin = u;
  156.  
  157.     if (ioMax-ioMin < epsilon)
  158.         if (ioMax < 1-epsilon)
  159.             ioMax += epsilon;
  160.         else
  161.             ioMin -= epsilon;
  162. }
  163.