home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 13.0 KB | 479 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWBitmap.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWBITMAP_H
- #include "FWBitmap.h"
- #endif
-
- #ifndef SLBITMAP_H
- #include "SLBitmap.h"
- #endif
-
- #ifndef SLGRGLOB_H
- #include "SLGrGlob.h"
- #endif
-
- #ifndef FWMEMHLP_H
- #include "FWMemHlp.h"
- #endif
-
- #ifndef FWCOLOR_H
- #include "FWColor.h"
- #endif
-
- #ifndef FWGC_H
- #include "FWGC.h"
- #endif
-
- #ifndef FWACQUIR_H
- #include "FWAcquir.h"
- #endif
-
- #ifndef FWODGEOM_H
- #include "FWODGeom.h"
- #endif
-
- #ifndef FWGRUTIL_H
- #include "FWGrUtil.h"
- #endif
-
- #ifndef FWWINRES_H
- #include "FWWinRes.h"
- #endif
-
- #ifndef FWPALETE_H
- #include "FWPalete.h"
- #endif
-
- #ifndef SLWINDIB_H
- #include "FWWinDIB.h"
- #endif
-
- #ifndef FWSOMENV_H
- #include "FWSOMEnv.h"
- #endif
-
- #if defined(FW_BUILD_WIN) && !defined(FWGRUTIL_H)
- #include "FWGrUtil.h"
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(FWRESACC_H)
- #include "FWResAcc.h"
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(FWPICTUR_H)
- #include "FWPictur.h"
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(FWRESSIN_H)
- #include "FWResSin.h"
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__PICTUTILS__)
- #include "PictUtils.h"
- #endif
-
- // ----- Foundation Includes -----
-
- #ifndef FWSTREAM_H
- #include "FWStream.h"
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWGraphics_Bitmap
- #endif
-
- //========================================================================================
- // Template instantiation
- //========================================================================================
-
- #include "FWGrRef.tpp"
-
- FW_DEFINE_AUTO_TEMPLATE(FW_TGrRefPtr, FW_HBitmap)
-
- #ifdef FW_USE_TEMPLATE_PRAGMAS
-
- #pragma template_access public
- #pragma template FW_TGrRefPtr<FW_HBitmap>
-
- #else
-
- template class FW_TGrRefPtr<FW_HBitmap>;
-
- #endif
-
- //========================================================================================
- // class FW_CBitmap
- //========================================================================================
-
- FW_DEFINE_AUTO(FW_CBitmap)
-
- //----------------------------------------------------------------------------------------
- // FW_PrivAcquireGrRep
- //----------------------------------------------------------------------------------------
-
- void FW_PrivAcquireGrRep(FW_HBitmap rep)
- {
- if (rep != 0)
- FW_PrivBitmap_Acquire(rep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivReleaseGrRep
- //----------------------------------------------------------------------------------------
-
- void FW_PrivReleaseGrRep(FW_HBitmap rep)
- {
- if (rep != 0)
- FW_PrivBitmap_Release(rep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmap::FW_CBitmap
- //----------------------------------------------------------------------------------------
-
- FW_CBitmap::FW_CBitmap()
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_ValidatePixelSize
- //----------------------------------------------------------------------------------------
-
- static short FW_ValidatePixelSize(short pixelSize)
- {
- #ifdef FW_BUILD_MAC
- FW_ASSERT(pixelSize == 0 || pixelSize == 1 || pixelSize == 2 || pixelSize == 4 ||
- pixelSize == 8 || pixelSize == 16 || pixelSize == 24 || pixelSize == 32);
- if (pixelSize == 24)
- pixelSize = 32;
- #endif
- #ifdef FW_BUILD_WIN
- FW_ASSERT(pixelSize == 0 || pixelSize == 1 || pixelSize == 2 || pixelSize == 4 ||
- pixelSize == 8 || pixelSize == 16 || pixelSize == 24 || pixelSize == 32);
- if (pixelSize == 32)
- pixelSize = 24;
- #endif
-
- return pixelSize;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmap::FW_CBitmap
- //----------------------------------------------------------------------------------------
-
- FW_CBitmap::FW_CBitmap(short width, short height, short pixelSize, FW_Palette palette /* = NULL */)
- {
- pixelSize = FW_ValidatePixelSize(pixelSize);
-
- FW_PlatformError error;
- FW_HBitmap rep = FW_PrivBitmap_CreateFromBits(width, height, pixelSize, palette,
- NULL, 0, 0, &error);
- FW_FailOnError(error);
- SetRep(rep);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmap::FW_CBitmap
- //----------------------------------------------------------------------------------------
-
- FW_CBitmap::FW_CBitmap(void* image, long imageSize, short rowBytes,
- short width, short height, short pixelSize, FW_Palette palette /* = NULL */)
- {
- FW_ASSERT(image != 0);
-
- pixelSize = FW_ValidatePixelSize(pixelSize);
-
- FW_PlatformError error;
- FW_HBitmap rep = FW_PrivBitmap_CreateFromBits(width, height, pixelSize, palette,
- image, imageSize, rowBytes, &error);
- FW_FailOnError(error);
- SetRep(rep);
- FW_END_CONSTRUCTOR
- }
-
- #ifdef FW_BUILD_MAC
-
- const FW_SRect FW_CBitmap::gWholePicture = { 0, 0, 0, 0 };
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmap::FW_CBitmap
- //----------------------------------------------------------------------------------------
-
- FW_CBitmap::FW_CBitmap(const FW_CPicture& picture, const FW_CColor& fillColor, const FW_CRect& pictPart)
- {
- FW_CRect bounds = pictPart;
-
- #if 0
- if(&pictPart == &gWholePicture)
- picture.GetPictBounds(bounds);
- #else
- if(pictPart == gWholePicture)
- picture.GetPictBounds(bounds);
- #endif
-
- FW_PlatformError error;
- FW_HBitmap rep = FW_PrivBitmap_MacCreateFromPicture(picture, fillColor, bounds, &error);
- FW_FailOnError(error);
- SetRep(rep);
- FW_END_CONSTRUCTOR
- }
-
- #endif
-
- #ifdef FW_BUILD_MAC
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmap::MacGetAsPicture
- //----------------------------------------------------------------------------------------
-
- FW_CPicture FW_CBitmap::MacGetAsPicture(const FW_SRect& pictPart) const
- {
- FW_CRect bounds = pictPart;
- #if 0
- if(&pictPart == &gWholePicture)
- GetBitmapBounds(bounds);
- #else
- if (FW_CRect(gWholePicture)==pictPart)
- GetBitmapBounds(bounds);
- #endif
-
- FW_PlatformError error;
- FW_HPicture hPict = FW_PrivBitmap_MacGetAsPicture(fRep, bounds, &error);
- FW_FailOnError(error);
-
- FW_CPicture pict;
- pict.SetRep(hPict);
- return pict;
- }
-
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmap::FW_CBitmap
- //----------------------------------------------------------------------------------------
-
- FW_CBitmap::FW_CBitmap(FW_PlatformBitmap platformBitmap)
- {
- FW_PlatformError error;
- FW_HBitmap rep = FW_PrivBitmap_CreateFromPlatformBitmap(platformBitmap, &error);
- FW_FailOnError(error);
- SetRep(rep);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmap::FW_CBitmap
- //----------------------------------------------------------------------------------------
-
- FW_CBitmap::FW_CBitmap(FW_PResourceFile& resourceFile, FW_ResourceID resID)
- {
- FW_PlatformError error;
- FW_HBitmap rep = FW_PrivBitmap_CreateFromResource(resourceFile, resID, &error);
- FW_FailOnError(error);
- SetRep(rep);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmap::FW_CBitmap
- //----------------------------------------------------------------------------------------
-
- FW_CBitmap::FW_CBitmap(FW_CReadableStream& stream, FW_Boolean bDIBHeader)
- {
- FW_PlatformError error;
- FW_HBitmap rep = FW_PrivBitmap_Read(stream, bDIBHeader, &error);
- FW_FailOnError(error);
- SetRep(rep);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmap::FW_CBitmap
- //----------------------------------------------------------------------------------------
-
- FW_CBitmap::FW_CBitmap(const FW_CBitmap& other) :
- FW_TGrRefPtr<FW_HBitmap>(other)
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmap::FW_CBitmap
- //----------------------------------------------------------------------------------------
-
- FW_CBitmap::~FW_CBitmap()
- {
- FW_START_DESTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmap::operator=
- //----------------------------------------------------------------------------------------
-
- FW_CBitmap& FW_CBitmap::operator=(const FW_CBitmap& other)
- {
- FW_TGrRefPtr<FW_HBitmap>::operator=(other);
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmap::Copy
- //----------------------------------------------------------------------------------------
-
- FW_CBitmap FW_CBitmap::Copy(const FW_CRect& srcRect) const
- {
- FW_PlatformError error;
- FW_HBitmap rep = FW_PrivBitmap_Copy(fRep, srcRect, &error);
- FW_FailOnError(error);
-
- FW_CBitmap bitmap;
- bitmap.SetRep(rep);
-
- return bitmap;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmap::GetPalette
- //----------------------------------------------------------------------------------------
-
- FW_Palette FW_CBitmap::GetPalette() const
- {
- FW_PlatformError error;
- FW_Palette palette = FW_PrivBitmap_GetPalette(fRep, &error);
- FW_FailOnError(error);
- return palette;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmap::GetBitmapBounds
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmap::GetBitmapBounds(FW_SGraphicContext& gc, FW_SRect& bounds) const
- {
- FW_PrivBitmap_GetBitmapBoundsGC(gc.fEnvironment, fRep, gc, bounds);
- FW_FailOnEvError(gc.fEnvironment);
- }
-
- //========================================================================================
- // Stream I/O
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // operator>>
- //----------------------------------------------------------------------------------------
-
- FW_CReadableStream& operator>>(FW_CReadableStream& stream, FW_CBitmap& bitmap)
- {
- FW_PlatformError error;
- FW_HBitmap rep = FW_PrivBitmap_Read(stream, TRUE, &error);
- FW_FailOnError(error);
- bitmap.SetRep(rep);
- return stream;
- }
-
- //----------------------------------------------------------------------------------------
- // operator<<
- //----------------------------------------------------------------------------------------
-
- FW_CWritableStream& operator<<(FW_CWritableStream& stream, const FW_CBitmap& bitmap)
- {
- FW_PlatformError error;
- FW_PrivBitmap_Write(bitmap.fRep, stream, TRUE, &error);
- FW_FailOnError(error);
- return stream;
- }
-
- //========================================================================================
- // class FW_CBitmapContext
- //========================================================================================
-
- FW_DEFINE_AUTO(FW_CBitmapContext)
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapContext::FW_CBitmapContext
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapContext::FW_CBitmapContext(Environment* ev, FW_CBitmap& bitmap) :
- FW_CGraphicContext(ev),
- fBitmap(bitmap),
- fGraphicDevice(NULL)
- {
- FW_ASSERT(bitmap != NULL);
- FW_PlatformBitmap platformBitmap = bitmap.GetPlatformBitmap();
- ODPlatformCanvas platformCanvas;
-
- #ifdef FW_BUILD_WIN
- platformCanvas = ::CreateCompatibleDC(NULL);
- fOldBitmap = (HBITMAP) ::SelectObject(platformCanvas, platformBitmap);
-
- if(fOldBitmap == NULL)
- FW_Failure(FW_xGraphicException);
- #endif
- #ifdef FW_BUILD_MAC
- platformCanvas = (ODPlatformCanvas) platformBitmap;
- bitmap.MacLockPixels();
- #endif
- fGraphicDevice = FW_PrivGDev_CreateFromPlatformCanvas(ev, platformCanvas);
- FW_FailOnEvError(ev);
- FW_PrivGDev_SetResolution(fGraphicDevice, FW_kFixed72, FW_kFixed72);
-
- FW_CRect bounds;
- bitmap.GetBitmapBounds(bounds);
-
- FW_CAcquiredODShape aqClipShape = ::FW_NewODShape(ev, bounds);
-
- InitGraphicContext(fGraphicDevice,
- NULL,
- aqClipShape);
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapContext::~FW_CBitmapContext
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapContext::~FW_CBitmapContext()
- {
- FW_START_DESTRUCTOR
- #ifdef FW_BUILD_MAC
- fBitmap.MacUnlockPixels();
- #endif
-
- #ifdef FW_BUILD_WIN
- // Save the DC for later use
- HDC hdcBitmap = FW_PrivGDev_GetPlatformCanvas(fGraphicDevice);
- #endif
-
- // ----- Terminate the superclass
- TerminateGraphicContext();
-
- #ifdef FW_BUILD_WIN
- ::SelectObject(hdcBitmap, fOldBitmap);
- ::DeleteDC(hdcBitmap);
- #endif
-
- // ----- Delete the device
- FW_PrivGDev_Release(fGraphicDevice);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapContext::Reset
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapContext::Reset()
- {
- // Nothing to do [HLX] Am i sure ?
- }
-