home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 18.9 KB | 685 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWCPat.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: © 1993, 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #if defined(FW_PROFILER_CALLS) && defined(__MWERKS__)
- #pragma profile on
- #endif
-
- #ifndef FWCPAT_H
- #include "FWCPat.h"
- #endif
-
- #ifndef FWCOLOR_H
- #include "FWColor.h"
- #endif
-
- #ifndef FWGRGLOB_H
- #include "FWGrGlob.h"
- #endif
-
- #ifndef FWGRUTIL_H
- #include "FWGrUtil.h"
- #endif
-
- #ifndef FWMEMHLP_H
- #include "FWMemHlp.h"
- #endif
-
- #if defined(FW_BUILD_WIN) && !defined(FWWINDIB_H)
- #include "FWWinDIB.h"
- #endif
-
- // ----- Foundation Includes -----
-
- #ifndef FWSTREAM_H
- #include "FWStream.h"
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWGraphics_ColorPattern
- #endif
-
- FW_DEFINE_CLASS_M1(FW_CColorPatternRep, FW_CPatternRep)
-
- FW_REGISTER_ARCHIVABLE_CLASS(FW_LColorPatternRep, FW_CColorPatternRep, FW_CColorPatternRep::Read, FW_CPatternRep::Write)
-
- //========================================================================================
- // Forward Class Declarations
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::FW_CColorPatternRep
- //----------------------------------------------------------------------------------------
-
- FW_CColorPatternRep::FW_CColorPatternRep():
- fPlatformColorPattern(NULL)
- {
- FW_PixelPattern black =
- {
- {
- {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
- {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
- {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
- {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
- {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
- {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
- {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
- {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}
- }
- };
-
- FW_CColor colors[2] = { FW_kRGBWhite, FW_kRGBBlack };
- MakePixelPattern(black, 2, colors);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::FW_CColorPatternRep
- //----------------------------------------------------------------------------------------
-
- FW_CColorPatternRep::FW_CColorPatternRep(FW_PlatformColorPattern platformColorPattern) :
- fPlatformColorPattern(platformColorPattern)
- {
- FW_ASSERT(fPlatformColorPattern != NULL);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::FW_CColorPatternRep
- //----------------------------------------------------------------------------------------
-
- FW_CColorPatternRep::FW_CColorPatternRep(FW_CReadableStream& archive) :
- fPlatformColorPattern(NULL)
- {
- // Pattern data
- FW_PixelPattern patData;
- archive.Read(patData.fData, sizeof(patData));
-
- // Color count
- unsigned short nbColors;
- archive >> nbColors;
-
- // Colors
- FW_CColor* colors = new FW_CColor[nbColors];
- FW_TRY
- {
- for(int i = 0; i < nbColors; i ++)
- archive >> colors[i];
-
- // Create the pattern
- MakePixelPattern(patData, nbColors, colors);
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- delete[] colors;
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- // Clean up
- delete[] colors;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::FW_CColorPatternRep
- //----------------------------------------------------------------------------------------
-
- FW_CColorPatternRep::FW_CColorPatternRep(const FW_PixelPattern& pixels, short nbColors, const FW_CColor* colorTable) :
- fPlatformColorPattern(NULL)
- {
- #ifdef FW_BUILD_WIN
- fWinCachedBitmap = NULL;
- #endif
-
- MakePixelPattern(pixels, nbColors, colorTable);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::~FW_CColorPatternRep
- //----------------------------------------------------------------------------------------
-
- FW_CColorPatternRep::~FW_CColorPatternRep()
- {
- #ifdef FW_BUILD_MAC
- if (fPlatformColorPattern)
- ::DisposePixPat(fPlatformColorPattern);
- #endif
-
- #ifdef FW_BUILD_WIN
- if (fPlatformColorPattern != NULL)
- FW_CPrivWinDIB::Free(fPlatformColorPattern);
-
- WinForgetCachedBitmap();
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::IsBlack
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CColorPatternRep::IsBlack() const
- {
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::IsEqual
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CColorPatternRep::IsEqual(const FW_CGraphicCountedPtrRep* other) const
- {
- if (other == this)
- return TRUE;
-
- FW_CColorPatternRep* patternRep = FW_DYNAMIC_CAST(FW_CColorPatternRep, other);
- if (patternRep == NULL)
- return FALSE;
-
- // Compare the pixels
- FW_PixelPattern pixData1;
- GetPixelData(pixData1);
-
- FW_PixelPattern pixData2;
- patternRep->GetPixelData(pixData2);
-
- if(::memcmp(pixData1.fData, pixData2.fData, sizeof(FW_PixelPattern)) != 0)
- return FALSE;
-
- // Compare the color tables
- FW_CColor* colors1 = NULL;
- unsigned short nbColors1 = GetColorTable(colors1);
-
- FW_CColor* colors2 = NULL;
- unsigned short nbColors2 = patternRep->GetColorTable(colors2);
-
- if(nbColors1 != nbColors2)
- return FALSE;
-
- if(::memcmp(colors1, colors2, sizeof(FW_CColor) * nbColors1) != 0)
- return FALSE;
-
- return TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::Copy
- //----------------------------------------------------------------------------------------
-
- FW_PPattern FW_CColorPatternRep::Copy() const
- {
- FW_PlatformColorPattern platformColorPattern;
-
- #ifdef FW_BUILD_MAC
- platformColorPattern = ::NewPixPat();
- ::CopyPixPat(fPlatformColorPattern, platformColorPattern);
- #endif
-
- #ifdef FW_BUILD_WIN
- platformColorPattern = FW_CPrivWinDIB::CreateCopy(fPlatformColorPattern);
- #endif
-
- return FW_PPattern(platformColorPattern);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::Flatten
- //----------------------------------------------------------------------------------------
-
- void FW_CColorPatternRep::Flatten(FW_CWritableStream& archive) const
- {
- // Pixel data
- FW_PixelPattern pixData;
- GetPixelData(pixData);
- archive.Write(pixData.fData, sizeof(pixData));
-
- // ----- Colors
- FW_CColor* colors = NULL;
- unsigned short nbColors = GetColorTable(colors);
- FW_TRY
- {
- archive << nbColors;
- for(int i = 0; i < nbColors; i++)
- archive << colors[i];
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- // Clean up
- delete[] colors;
-
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- // Clean up
- delete[] colors;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::Invert
- //----------------------------------------------------------------------------------------
-
- void FW_CColorPatternRep::Invert()
- {
- // Get current pattern info
- FW_PixelPattern pixData;
- GetPixelData(pixData);
-
- // ----- Look for minimum and maximun -----
- unsigned char max = 0x00;
- int i;
-
- for (i = 0; i < 8 * 8; i++)
- {
- unsigned char c = pixData.fDataRaw[i];
- if (c > max)
- max = c;
- }
-
- // ----- Invert it -----
- for (i = 0; i < 8 * 8; i++)
- pixData.fDataRaw[i] = max - pixData.fDataRaw[i];
-
- // Update the pattern
- SetPixelData(pixData);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::FlipVerticaly
- //----------------------------------------------------------------------------------------
-
- void FW_CColorPatternRep::FlipVerticaly()
- {
- // Get current pattern info
- FW_PixelPattern pixData;
- GetPixelData(pixData);
-
- // Flip it
- for (short i = 0; i < 4; i++)
- for (short j = 0; j < 2; j++)
- {
- unsigned long l = pixData.fDataLongs[i][j];
- pixData.fDataLongs[i][j] = pixData.fDataLongs[7 - i][j];
- pixData.fDataLongs[7 - i][j] = l;
- }
-
- // Update the pattern
- SetPixelData(pixData);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::FlipHorizontaly
- //----------------------------------------------------------------------------------------
-
- void FW_CColorPatternRep::FlipHorizontaly()
- {
- // Get current pattern info
- FW_PixelPattern pixData;
- GetPixelData(pixData);
-
- // Flip it
- for (short i = 0; i < 8; i++)
- for (short j = 0; j < 4; j++)
- {
- unsigned char c = pixData.fData[i][j];
- pixData.fData[i][j] = pixData.fData[i][7 - j];
- pixData.fData[i][7 - j] = c;
- }
-
- // Update the pattern
- SetPixelData(pixData);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::ShiftRight
- //----------------------------------------------------------------------------------------
-
- void FW_CColorPatternRep::ShiftRight()
- {
- // Get current pattern info
- FW_PixelPattern pixData;
- GetPixelData(pixData);
-
- // Shift it
- for (short i = 0; i < 8; i++)
- {
- unsigned char c = pixData.fData[i][7];
- for(short j = 7; j > 0; j--)
- pixData.fData[i][j] = pixData.fData[i][j - 1];
- pixData.fData[i][0] = c;
- }
-
- // Update the pattern
- SetPixelData(pixData);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::ShiftLeft
- //----------------------------------------------------------------------------------------
-
- void FW_CColorPatternRep::ShiftLeft()
- {
- // Get current pattern info
- FW_PixelPattern pixData;
- GetPixelData(pixData);
-
- // Shift it
- for (short i = 0; i < 8; i++)
- {
- unsigned char c = pixData.fData[i][0];
- for(short j = 0; j < 7; j++)
- pixData.fData[i][j] = pixData.fData[i][j + 1];
- pixData.fData[i][7] = c;
- }
-
- // Update the pattern
- SetPixelData(pixData);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::ShiftUp
- //----------------------------------------------------------------------------------------
-
- void FW_CColorPatternRep::ShiftUp()
- {
- // Get current pattern info
- FW_PixelPattern pixData;
- GetPixelData(pixData);
-
- // Shift it
- unsigned long temp0 = pixData.fDataLongs[0][0];
- unsigned long temp1 = pixData.fDataLongs[0][1];
-
- for (short i = 0; i < 7; i++)
- {
- pixData.fDataLongs[i][0] = pixData.fDataLongs[i + 1][0];
- pixData.fDataLongs[i][1] = pixData.fDataLongs[i + 1][1];
- }
-
- pixData.fDataLongs[7][0] = temp0;
- pixData.fDataLongs[7][1] = temp1;
-
- // Update the pattern
- SetPixelData(pixData);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::ShiftDown
- //----------------------------------------------------------------------------------------
-
- void FW_CColorPatternRep::ShiftDown()
- {
- // Get current pattern info
- FW_PixelPattern pixData;
- GetPixelData(pixData);
-
- // Shift it
- unsigned long temp0 = pixData.fDataLongs[7][0];
- unsigned long temp1 = pixData.fDataLongs[7][1];
-
- for (short i = 7; i > 0; i--)
- {
- pixData.fDataLongs[i][0] = pixData.fDataLongs[i - 1][0];
- pixData.fDataLongs[i][1] = pixData.fDataLongs[i - 1][1];
- }
-
- pixData.fDataLongs[0][0] = temp0;
- pixData.fDataLongs[0][1] = temp1;
-
- // Update the pattern
- SetPixelData(pixData);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::Read
- //----------------------------------------------------------------------------------------
-
- void* FW_CColorPatternRep::Read(FW_CReadableStream& archive)
- {
- return new FW_CColorPatternRep(archive);
- }
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::MacSetInCurPort
- //----------------------------------------------------------------------------------------
-
- void FW_CColorPatternRep::MacSetInCurPort() const
- {
- FW_ASSERT(fPlatformColorPattern != NULL);
-
- FW_PlatformColorPattern platformColorPattern = ::NewPixPat();
- ::CopyPixPat(fPlatformColorPattern, platformColorPattern);
- ::PenPixPat(platformColorPattern);
- }
- #endif
-
- #ifdef FW_BUILD_WIN
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::WinCreatePatternBrush
- //----------------------------------------------------------------------------------------
-
- HBRUSH FW_CColorPatternRep::WinCreatePatternBrush() const
- {
- HBRUSH hBrush = NULL;
-
- // Convert into a DDB for effeciency
- if (fWinCachedBitmap == NULL)
- ((FW_CColorPatternRep*) this)->fWinCachedBitmap = FW_CPrivWinDIB::ConvertToBitmap(fPlatformColorPattern);
-
- // Create the brush
- if(fWinCachedBitmap != NULL)
- hBrush = ::CreatePatternBrush(fWinCachedBitmap);
-
- return hBrush;
- }
- #endif
-
- #ifdef FW_BUILD_WIN
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::WinForgetCachedBitmap
- //----------------------------------------------------------------------------------------
-
- void FW_CColorPatternRep::WinForgetCachedBitmap()
- {
- if (fWinCachedBitmap != NULL)
- {
- ::DeleteObject(fWinCachedBitmap);
- fWinCachedBitmap = NULL;
- }
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::MacNewPixPat
- //----------------------------------------------------------------------------------------
-
- PixPatHandle FW_CColorPatternRep::MacNewPixPat(const void* pixels,
- short rowBytes, short pixelSize,
- short withPower, short heightPower,
- short nbColors, const FW_CColor* colorTable)
- {
- FW_ASSERT(pixels != NULL);
- FW_ASSERT(colorTable != NULL);
- FW_ASSERT((rowBytes & 0x0001) == 0);
- FW_ASSERT(pixelSize == 1 || pixelSize == 2 || pixelSize == 4 || pixelSize == 8);
- FW_ASSERT(withPower != 0);
- FW_ASSERT(heightPower != 0);
- FW_ASSERT(nbColors <= (1 << pixelSize));
-
- PixPatHandle pixPat = ::NewPixPat();
-
- (*pixPat)->patType = 1;
-
- // ----- Creates the pixMap -----
- PixMapHandle pixMap = (*pixPat)->patMap;
-
- (*pixMap)->rowBytes = rowBytes | 0x8000;
- (*pixMap)->bounds.left = 0;
- (*pixMap)->bounds.top = 0;
- (*pixMap)->bounds.right = 1 << withPower;
- (*pixMap)->bounds.bottom = 1 << heightPower;
- (*pixMap)->pixelSize = pixelSize;
- (*pixMap)->cmpCount = 1;
- (*pixMap)->cmpSize = pixelSize;
-
- // ----- Set up the color table -----
- CTabHandle hColors = (*pixMap)->pmTable;
- Size colorTabSize = sizeof(ColorTable) + (nbColors * sizeof(ColorSpec));
- ::SetHandleSize((Handle)hColors, colorTabSize);
-
- ::HLock((Handle)hColors);
- CTabPtr pColors = *hColors;
- pColors->ctSeed = ::GetCTSeed();
- pColors->ctFlags = 0;
- pColors->ctSize = nbColors - 1;
-
- for (short i = 0; i<nbColors; i++)
- {
- pColors->ctTable[i].value = i;
- pColors->ctTable[i].rgb = colorTable[i];
- }
- ::HUnlock((Handle)hColors);
-
- // ----- Creates the pattern data -----
- Size dataSize = (Size)rowBytes * 8;
- Handle patData = (*pixPat)->patData;
- ::SetHandleSize(patData, dataSize);
-
- ::HLock(patData);
- ::BlockMove(pixels, *patData, dataSize);
- ::HUnlock(patData);
-
- return pixPat;
- }
- #endif
-
- #ifdef FW_BUILD_WIN
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::WinFlipPixelCopy
- //----------------------------------------------------------------------------------------
-
- void FW_CColorPatternRep::WinFlipPixelCopy(const void* source, void* dest)
- {
- unsigned long* sourceL = (unsigned long*) source;
- unsigned long* destL = (unsigned long*) dest;
-
- for(int i = 0; i < 16; i += 2)
- {
- destL[14 - i] = sourceL[i];
- destL[15 - i] = sourceL[i + 1];
- }
- }
-
- #endif
-
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::MakePixelPattern
- //----------------------------------------------------------------------------------------
-
- void FW_CColorPatternRep::MakePixelPattern(const FW_PixelPattern& pixels, short nbColors, const FW_CColor* colorTable)
- {
- #ifdef FW_BUILD_MAC
- fPlatformColorPattern = FW_CColorPatternRep::MacNewPixPat(&pixels, 8, 8, 3, 3, nbColors, colorTable);
- #endif
-
- #ifdef FW_BUILD_WIN
- // We need to flip the bits because DIBs are stored bottom-to-top
- FW_PixelPattern pixData;
- WinFlipPixelCopy(pixels.fData, pixData.fData);
-
- // Create a DIB
- fPlatformColorPattern = FW_CPrivWinDIB::CreateDIB(8, 8, 8, nbColors, colorTable, pixData.fData);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::GetPixelData
- //----------------------------------------------------------------------------------------
-
- void FW_CColorPatternRep::GetPixelData(FW_PixelPattern& pixData) const
- {
- #ifdef FW_BUILD_MAC
- Handle patData = (*fPlatformColorPattern)->patData;
-
- ::HLock(patData);
- BlockMove((FW_PixelPattern*)*patData, pixData.fDataRaw, 64);
- ::HUnlock(patData);
- #endif
- #ifdef FW_BUILD_WIN
- const BITMAPINFO* bmi = (const BITMAPINFO*) ::GlobalLock(fPlatformColorPattern);
- FW_CPrivWinDIB::PixelBufferPtr pixels = FW_CPrivWinDIB::GetPixelBuffer(bmi);
- WinFlipPixelCopy(pixels, pixData.fData);
- ::GlobalUnlock(fPlatformColorPattern);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::SetPixelData
- //----------------------------------------------------------------------------------------
-
- void FW_CColorPatternRep::SetPixelData(const FW_PixelPattern& pixData)
- {
- #ifdef FW_BUILD_MAC
- Handle patData = (*fPlatformColorPattern)->patData;
-
- ::HLock(patData);
- ::BlockMove(pixData.fDataRaw, (FW_PixelPattern*)*patData, 64);
- ::HUnlock(patData);
-
- ::PixPatChanged(fPlatformColorPattern);
- #endif
- #ifdef FW_BUILD_WIN
- const BITMAPINFO* bmi = (const BITMAPINFO*) ::GlobalLock(fPlatformColorPattern);
- FW_CPrivWinDIB::PixelBufferPtr pixels = FW_CPrivWinDIB::GetPixelBuffer(bmi);
- WinFlipPixelCopy(pixData.fData, pixels);
- ::GlobalUnlock(fPlatformColorPattern);
-
- WinForgetCachedBitmap();
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColorPatternRep::GetColorTable
- //----------------------------------------------------------------------------------------
-
- unsigned short FW_CColorPatternRep::GetColorTable(FW_CColor*& colors) const
- {
- #ifdef FW_BUILD_MAC
- PixMapHandle pixMap = (*fPlatformColorPattern)->patMap;
-
- CTabHandle hColors = (*pixMap)->pmTable;
- FW_CAcquireLockedSystemHandle lock((FW_PlatformHandle)hColors);
- CTabPtr pColors = *hColors;
-
- unsigned short nbColors = pColors->ctSize + 1;
- colors = new FW_CColor[nbColors];
- for (unsigned short i = 0; i<nbColors; i++)
- colors[i] = pColors->ctTable[i].rgb;
-
- return nbColors;
- #endif
- #ifdef FW_BUILD_WIN
- return FW_CPrivWinDIB::GetColorTable(fPlatformColorPattern, colors);
- #endif
- }
-
-