home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-05-22 | 3.3 KB | 164 lines | [TEXT/CWIE] |
- // =================================================================================
- // ImageLight.cp ©1997 BB's Team inc. All rights reserved
- // =================================================================================
- #include "ImageLight.h"
- #include "PL_Utils.h"
-
-
- // Constructor
- ImageLight::ImageLight (void)
- : mUpToDate (false)
- , mLight (nil)
- , mGrey (nil)
- {
- mWidth = mHeight = mWidthN = mHeightN = 0;
- }
-
-
- // Destructor
- ImageLight::~ImageLight()
- {
- if ( mLight != nil )
- DisposeHandle (mLight);
- }
-
-
- // The text characteristics has changed
- void ImageLight::SetTextTraits (TextTraitsRecord &inTraits)
- {
- // Compute one char's sizes in pixels
- Int32 ascent, newWidth, newHeight;
- PL_Utils::ComputeBBox (inTraits, inTraits.size, newWidth, newHeight, ascent);
-
- mUpToDate = mUpToDate && newWidth==mWidth && newHeight==mHeight;
- MakeRoom ();
-
- mWidth = newWidth;
- mHeight= newHeight;
-
- SetLineCol();
-
- return;
- }
-
- // Make room.
- void ImageLight::MakeRoom (Boolean force)
- {
- if (mLight==nil)
- return;
-
- if ( force || !mUpToDate ) {
- DisposeHandle (mLight);
- mLight=nil;
- }
- }
-
-
- // Set lines and columns
- void ImageLight::SetLineCol (void)
- {
- if (mWidth && mHeight) {
- mWidthN = mPictWidth / mWidth;
- mHeightN = mPictHeight / mHeight;
- }
- }
-
-
- // Set the Grey Offscreen GWorld
- void ImageLight::SetGreyWorld (GreyGWorld *inGrey)
- {
- mUpToDate = false;
- mGrey = inGrey;
- MakeRoom();
-
- // Compute number of lines and columns in the text
- Rect picFrame = mGrey->Bounds();
- mPictWidth = picFrame.right - picFrame.left;
- mPictHeight= picFrame.bottom - picFrame.top;
- SetLineCol();
- }
-
-
- // IsUpToDate
- Boolean ImageLight::IsUpToDate (void)
- {
- return mUpToDate;
- }
-
-
- // accessor
- float& ImageLight::light (int n)
- {
- return ((float*)(*mLight))[n];
- }
-
-
- // Returns total text size
- Int32 ImageLight::GetTextSize (void)
- {
- return mHeightN*(mWidthN+1);
- }
-
-
- // ---------------------------------------------------------------------------------
- // • Update
- // ---------------------------------------------------------------------------------
- void ImageLight::Update (void)
- {
- if (mUpToDate)
- return;
-
- // Find room.
- MakeRoom ();
- PL_Utils::ForceNewHandle ( mLight, sizeof(float)*GetTextSize() );
- if (mLight==nil)
- return;
-
- // lack thereof crashes the 68k-running-on-PPC version !
- mGrey->Lock();
-
- // a rect the size of a character
- Rect theComputingRect;
- ::SetRect (&theComputingRect, 0, 0, mWidth, mHeight);
-
- // Move this rect across the whole picture
- Int32 k = 0;
- for (Int16 i=0 ; i<mHeightN ; i++) {
- for (Int16 j=0 ; j<mWidthN ; j++) {
- light(k++) = mGrey->ComputeGrey (theComputingRect);
- ::OffsetRect (&theComputingRect, mWidth, 0);
- }
-
- // apply a carriage return to the rect !
- ::OffsetRect (&theComputingRect, -mWidthN*mWidth, mHeight);
- light(k) = light(k-1); // will match '\r' at end of line
- k++;
- }
-
- mGrey->Unlock();
- mUpToDate = true;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • GetMinMax
- // ---------------------------------------------------------------------------------
- void ImageLight::GetMinMax (float &ioMin, float &ioMax)
- {
- const float epsilon = 0.01;
- float u;
-
- ioMin = ioMax = light(0);
- for (Int32 i = 1 ; i<GetTextSize() ; i++)
- if ( (u=light(i)) > ioMax )
- ioMax = u;
- else if ( u < ioMin )
- ioMin = u;
-
- if (ioMax-ioMin < epsilon)
- if (ioMax < 1-epsilon)
- ioMax += epsilon;
- else
- ioMin -= epsilon;
- }
-