home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / Sources / FWBitmap.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  12.9 KB  |  471 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWBitmap.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWBITMAP_H
  13. #include "FWBitmap.h"
  14. #endif
  15.  
  16. #ifndef SLBITMAP_H
  17. #include "SLBitmap.h"
  18. #endif
  19.  
  20. #ifndef SLGRGLOB_H
  21. #include "SLGrGlob.h"
  22. #endif
  23.  
  24. #ifndef FWMEMHLP_H
  25. #include "FWMemHlp.h"
  26. #endif
  27.  
  28. #ifndef FWCOLOR_H
  29. #include "FWColor.h"
  30. #endif
  31.  
  32. #ifndef FWGC_H
  33. #include "FWGC.h"
  34. #endif
  35.  
  36. #ifndef FWACQUIR_H
  37. #include "FWAcquir.h"
  38. #endif
  39.  
  40. #ifndef FWODGEOM_H
  41. #include "FWODGeom.h"
  42. #endif
  43.  
  44. #ifndef FWGRUTIL_H
  45. #include "FWGrUtil.h"
  46. #endif
  47.  
  48. #ifndef FWWINRES_H
  49. #include "FWWinRes.h"
  50. #endif
  51.  
  52. #ifndef FWPALETE_H
  53. #include "FWPalete.h"
  54. #endif
  55.  
  56. #ifndef SLWINDIB_H
  57. #include "FWWinDIB.h"
  58. #endif
  59.  
  60. #ifndef FWSOMENV_H
  61. #include "FWSOMEnv.h"
  62. #endif
  63.  
  64. #if defined(FW_BUILD_WIN) && !defined(FWGRUTIL_H)
  65. #include "FWGrUtil.h"
  66. #endif
  67.  
  68. #if defined(FW_BUILD_MAC) && !defined(FWRESACC_H)
  69. #include "FWResAcc.h"
  70. #endif
  71.  
  72. #if defined(FW_BUILD_MAC) && !defined(FWPICTUR_H)
  73. #include "FWPictur.h"
  74. #endif
  75.  
  76. #if defined(FW_BUILD_MAC) && !defined(FWRESSIN_H)
  77. #include "FWResSin.h"
  78. #endif
  79.  
  80. #if defined(FW_BUILD_MAC) && !defined(__PICTUTILS__)
  81. #include "PictUtils.h"
  82. #endif
  83.  
  84. // ----- Foundation Includes -----
  85.  
  86. #ifndef FWSTREAM_H
  87. #include "FWStream.h"
  88. #endif
  89.  
  90. #ifdef FW_BUILD_MAC
  91. #pragma segment FWGraphics_Bitmap
  92. #endif
  93.  
  94. //========================================================================================
  95. //    Template instantiation
  96. //========================================================================================
  97.  
  98. #include "FWGrRef.tpp"
  99.  
  100. FW_DEFINE_AUTO_TEMPLATE(FW_TGrRefPtr, FW_HBitmap)
  101.  
  102. #ifdef FW_USE_TEMPLATE_PRAGMAS
  103.  
  104. #pragma template_access public
  105. #pragma template FW_TGrRefPtr<FW_HBitmap>
  106.  
  107. #else
  108.  
  109. template class FW_TGrRefPtr<FW_HBitmap>;
  110.  
  111. #endif
  112.  
  113. //========================================================================================
  114. //    class FW_CBitmap
  115. //========================================================================================
  116.  
  117. FW_DEFINE_AUTO(FW_CBitmap)
  118.     
  119. //----------------------------------------------------------------------------------------
  120. //    FW_PrivAcquireGrRep
  121. //----------------------------------------------------------------------------------------
  122.  
  123. void FW_PrivAcquireGrRep(FW_HBitmap rep)
  124. {
  125.     if (rep != 0)
  126.         FW_PrivBitmap_Acquire(rep);
  127. }
  128.  
  129. //----------------------------------------------------------------------------------------
  130. //    FW_PrivReleaseGrRep
  131. //----------------------------------------------------------------------------------------
  132.  
  133. void FW_PrivReleaseGrRep(FW_HBitmap rep)
  134. {
  135.     if (rep != 0)
  136.         FW_PrivBitmap_Release(rep);
  137. }
  138.  
  139. //----------------------------------------------------------------------------------------
  140. //    FW_CBitmap::FW_CBitmap
  141. //----------------------------------------------------------------------------------------
  142.  
  143. FW_CBitmap::FW_CBitmap()
  144. {
  145.     FW_END_CONSTRUCTOR
  146. }
  147.  
  148. //----------------------------------------------------------------------------------------
  149. //    FW_CBitmap::FW_CBitmap
  150. //----------------------------------------------------------------------------------------
  151.  
  152. FW_CBitmap::FW_CBitmap(short width, short height, short pixelSize, FW_Palette palette /* = NULL */)
  153. {
  154. #ifdef FW_BUILD_MAC
  155.     FW_ASSERT(pixelSize == 0 || pixelSize == 1 || pixelSize == 2 || pixelSize == 4 ||
  156.               pixelSize == 8 || pixelSize == 16 || pixelSize == 32);
  157. #endif
  158. #ifdef FW_BUILD_WIN
  159.     FW_ASSERT(pixelSize == 0 || pixelSize == 1 || pixelSize == 2 || pixelSize == 4 ||
  160.               pixelSize == 8 || pixelSize == 16 || pixelSize == 24);
  161. #endif
  162.  
  163.     FW_PlatformError error;
  164.     FW_HBitmap rep = FW_PrivBitmap_CreateFromBits(width, height, pixelSize, palette,
  165.                         NULL, 0, 0, &error);
  166.     FW_FailOnError(error);
  167.     SetRep(rep);
  168.     FW_END_CONSTRUCTOR
  169. }
  170.  
  171. //----------------------------------------------------------------------------------------
  172. //    FW_CBitmap::FW_CBitmap
  173. //----------------------------------------------------------------------------------------
  174.  
  175. FW_CBitmap::FW_CBitmap(void* image, long imageSize, short rowBytes,
  176.                        short width, short height, short pixelSize, FW_Palette palette /* = NULL */)
  177. {
  178.     FW_ASSERT(image != 0);
  179.  
  180. #ifdef FW_BUILD_MAC
  181.     FW_ASSERT(pixelSize == 0 || pixelSize == 1 || pixelSize == 2 || pixelSize == 4 ||
  182.               pixelSize == 8 || pixelSize == 16 || pixelSize == 32);
  183. #endif
  184. #ifdef FW_BUILD_WIN
  185.     FW_ASSERT(pixelSize == 0 || pixelSize == 1 || pixelSize == 2 || pixelSize == 4 ||
  186.               pixelSize == 8 || pixelSize == 16 || pixelSize == 24);
  187. #endif
  188.  
  189.     FW_PlatformError error;
  190.     FW_HBitmap rep = FW_PrivBitmap_CreateFromBits(width, height, pixelSize, palette,
  191.                         image, imageSize, rowBytes, &error);
  192.     FW_FailOnError(error);
  193.     SetRep(rep);
  194.     FW_END_CONSTRUCTOR
  195. }
  196.  
  197. #ifdef FW_BUILD_MAC
  198.  
  199. const FW_SRect FW_CBitmap::gWholePicture    =    {    0, 0, 0, 0    };
  200.  
  201. //----------------------------------------------------------------------------------------
  202. //    FW_CBitmap::FW_CBitmap
  203. //----------------------------------------------------------------------------------------
  204.  
  205. FW_CBitmap::FW_CBitmap(const FW_CPicture& picture, const FW_CColor& fillColor, const FW_CRect& pictPart)
  206. {
  207.     FW_CRect bounds = pictPart;
  208.  
  209. #if 0
  210.     if(&pictPart == &gWholePicture)
  211.         picture.GetPictBounds(bounds);
  212. #else
  213.     if(pictPart == gWholePicture)
  214.         picture.GetPictBounds(bounds);
  215. #endif
  216.     
  217.     FW_PlatformError error;
  218.     FW_HBitmap rep = FW_PrivBitmap_MacCreateFromPicture(picture, fillColor, bounds, &error);
  219.     FW_FailOnError(error);
  220.     SetRep(rep);
  221.     FW_END_CONSTRUCTOR
  222. }
  223.  
  224. #endif
  225.  
  226. #ifdef FW_BUILD_MAC
  227.  
  228. //----------------------------------------------------------------------------------------
  229. //    FW_CBitmap::MacGetAsPicture
  230. //----------------------------------------------------------------------------------------
  231.  
  232. FW_CPicture FW_CBitmap::MacGetAsPicture(const FW_SRect& pictPart) const
  233. {
  234.     FW_CRect bounds = pictPart;
  235. #if 0
  236.     if(&pictPart == &gWholePicture)
  237.         GetBitmapBounds(bounds);
  238. #else
  239.     if (FW_CRect(gWholePicture)==pictPart)
  240.         GetBitmapBounds(bounds);
  241. #endif
  242.  
  243.     FW_PlatformError error;
  244.     FW_HPicture hPict = FW_PrivBitmap_MacGetAsPicture(fRep, bounds, &error);
  245.     FW_FailOnError(error);
  246.  
  247.     FW_CPicture pict;
  248.     pict.SetRep(hPict);
  249.     return pict;
  250. }
  251.  
  252. #endif
  253.  
  254. //----------------------------------------------------------------------------------------
  255. //    FW_CBitmap::FW_CBitmap
  256. //----------------------------------------------------------------------------------------
  257.  
  258. FW_CBitmap::FW_CBitmap(FW_PlatformBitmap platformBitmap)
  259. {
  260.     FW_PlatformError error;
  261.     FW_HBitmap rep = FW_PrivBitmap_CreateFromPlatformBitmap(platformBitmap, &error);
  262.     FW_FailOnError(error);
  263.     SetRep(rep);
  264.     FW_END_CONSTRUCTOR
  265. }
  266.  
  267. //----------------------------------------------------------------------------------------
  268. //    FW_CBitmap::FW_CBitmap
  269. //----------------------------------------------------------------------------------------
  270.  
  271. FW_CBitmap::FW_CBitmap(FW_PResourceFile& resourceFile, FW_ResourceId resId)
  272. {
  273.     FW_PlatformError error;
  274.     FW_HBitmap rep = FW_PrivBitmap_CreateFromResource(resourceFile, resId, &error);
  275.     FW_FailOnError(error);
  276.     SetRep(rep);
  277.     FW_END_CONSTRUCTOR
  278. }
  279.  
  280. //----------------------------------------------------------------------------------------
  281. //    FW_CBitmap::FW_CBitmap
  282. //----------------------------------------------------------------------------------------
  283.  
  284. FW_CBitmap::FW_CBitmap(FW_CReadableStream& stream, FW_Boolean bDIBHeader)
  285. {
  286.     FW_PlatformError error;
  287.     FW_HBitmap rep = FW_PrivBitmap_Read(stream, bDIBHeader, &error);
  288.     FW_FailOnError(error);
  289.     SetRep(rep);
  290.     FW_END_CONSTRUCTOR
  291. }
  292.  
  293. //----------------------------------------------------------------------------------------
  294. //    FW_CBitmap::FW_CBitmap
  295. //----------------------------------------------------------------------------------------
  296.  
  297. FW_CBitmap::FW_CBitmap(const FW_CBitmap& other) :
  298.     FW_TGrRefPtr<FW_HBitmap>(other)
  299. {
  300.     FW_END_CONSTRUCTOR
  301. }
  302.  
  303. //----------------------------------------------------------------------------------------
  304. //    FW_CBitmap::FW_CBitmap
  305. //----------------------------------------------------------------------------------------
  306.  
  307. FW_CBitmap::~FW_CBitmap()
  308. {
  309.     FW_START_DESTRUCTOR
  310. }
  311.  
  312. //----------------------------------------------------------------------------------------
  313. //    FW_CBitmap::operator=
  314. //----------------------------------------------------------------------------------------
  315.  
  316. FW_CBitmap& FW_CBitmap::operator=(const FW_CBitmap& other)
  317. {
  318.     FW_TGrRefPtr<FW_HBitmap>::operator=(other);
  319.     return *this;
  320. }
  321.  
  322. //----------------------------------------------------------------------------------------
  323. //    FW_CBitmap::Copy
  324. //----------------------------------------------------------------------------------------
  325.  
  326. FW_CBitmap FW_CBitmap::Copy(const FW_CRect& srcRect) const
  327. {
  328.     FW_PlatformError error;
  329.     FW_HBitmap rep = FW_PrivBitmap_Copy(fRep, srcRect, &error);
  330.     FW_FailOnError(error);
  331.     
  332.     FW_CBitmap bitmap;
  333.     bitmap.SetRep(rep);
  334.  
  335.     return bitmap;
  336. }
  337.  
  338. //----------------------------------------------------------------------------------------
  339. //    FW_CBitmap::GetPalette
  340. //----------------------------------------------------------------------------------------
  341.  
  342. FW_Palette FW_CBitmap::GetPalette() const
  343.     FW_PlatformError error;
  344.     FW_Palette palette = FW_PrivBitmap_GetPalette(fRep, &error);
  345.     FW_FailOnError(error);
  346.     return palette;
  347. }
  348.  
  349. //----------------------------------------------------------------------------------------
  350. //    FW_CBitmap::GetBitmapBounds
  351. //----------------------------------------------------------------------------------------
  352.  
  353. void FW_CBitmap::GetBitmapBounds(FW_SGraphicContext& gc, FW_SRect& bounds) const
  354.     FW_PrivBitmap_GetBitmapBoundsGC(gc.fEnvironment, fRep, gc, bounds); 
  355.     FW_FailOnEvError(gc.fEnvironment);
  356. }
  357.         
  358. //========================================================================================
  359. //    Stream I/O
  360. //========================================================================================
  361.  
  362. //----------------------------------------------------------------------------------------
  363. //    operator>>
  364. //----------------------------------------------------------------------------------------
  365.  
  366. FW_CReadableStream& operator>>(FW_CReadableStream& stream, FW_CBitmap& bitmap)
  367. {
  368.     FW_PlatformError error;
  369.     FW_HBitmap rep = FW_PrivBitmap_Read(stream, TRUE, &error);
  370.     FW_FailOnError(error);
  371.     bitmap.SetRep(rep);
  372.     return stream;
  373. }
  374.  
  375. //----------------------------------------------------------------------------------------
  376. //    operator<<
  377. //----------------------------------------------------------------------------------------
  378.  
  379. FW_CWritableStream& operator<<(FW_CWritableStream& stream, const FW_CBitmap& bitmap)
  380. {
  381.     FW_PlatformError error;
  382.     FW_PrivBitmap_Write(bitmap.fRep, stream, TRUE, &error);
  383.     FW_FailOnError(error);
  384.     return stream;
  385. }
  386.  
  387. //========================================================================================
  388. //    class FW_CBitmapContext
  389. //========================================================================================
  390.  
  391. FW_DEFINE_AUTO(FW_CBitmapContext)
  392.  
  393. //----------------------------------------------------------------------------------------
  394. //    FW_CBitmapContext::FW_CBitmapContext
  395. //----------------------------------------------------------------------------------------
  396.  
  397. FW_CBitmapContext::FW_CBitmapContext(Environment* ev, FW_CBitmap& bitmap) :
  398.     FW_CGraphicContext(ev),
  399.     fBitmap(bitmap),
  400.     fGraphicDevice(NULL)
  401. {
  402.     FW_ASSERT(bitmap != NULL);
  403.     FW_PlatformBitmap    platformBitmap = bitmap.GetPlatformBitmap();
  404.     ODPlatformCanvas    platformCanvas;
  405.  
  406. #ifdef FW_BUILD_WIN
  407.     platformCanvas = ::CreateCompatibleDC(NULL);
  408.     fOldBitmap = (HBITMAP) ::SelectObject(platformCanvas, platformBitmap);
  409.     
  410.     if(fOldBitmap == NULL)
  411.         FW_Failure(FW_xGraphicException);
  412. #endif
  413. #ifdef FW_BUILD_MAC
  414.     platformCanvas = (ODPlatformCanvas) platformBitmap;
  415.     bitmap.MacLockPixels();
  416. #endif
  417.     fGraphicDevice = FW_PrivGDev_CreateFromPlatformCanvas(ev, platformCanvas);
  418.     FW_FailOnEvError(ev);
  419.     FW_PrivGDev_SetResolution(fGraphicDevice, FW_kFixed72, FW_kFixed72);
  420.  
  421.     FW_CRect bounds;
  422.     bitmap.GetBitmapBounds(bounds);
  423.     
  424.     FW_CAcquiredODShape aqClipShape = ::FW_NewODShape(ev, bounds);
  425.  
  426.     InitGraphicContext(fGraphicDevice,
  427.                        NULL,
  428.                        aqClipShape);
  429.  
  430.     FW_END_CONSTRUCTOR
  431. }                      
  432.  
  433. //----------------------------------------------------------------------------------------
  434. //    FW_CBitmapContext::~FW_CBitmapContext
  435. //----------------------------------------------------------------------------------------
  436.  
  437. FW_CBitmapContext::~FW_CBitmapContext()
  438. {
  439.     FW_START_DESTRUCTOR
  440. #ifdef FW_BUILD_MAC
  441.     fBitmap.MacUnlockPixels();
  442. #endif
  443.  
  444. #ifdef FW_BUILD_WIN
  445.     // Save the DC for later use
  446.     HDC hdcBitmap = FW_PrivGDev_GetPlatformCanvas(fGraphicDevice);
  447. #endif
  448.  
  449.     // ----- Terminate the superclass
  450.     TerminateGraphicContext();
  451.  
  452. #ifdef FW_BUILD_WIN
  453.     ::SelectObject(hdcBitmap, fOldBitmap);
  454.     ::DeleteDC(hdcBitmap);
  455. #endif
  456.  
  457.     // ----- Delete the device
  458.     FW_PrivGDev_Release(fGraphicDevice);
  459. }
  460.  
  461. //----------------------------------------------------------------------------------------
  462. //    FW_CBitmapContext::Reset
  463. //----------------------------------------------------------------------------------------
  464.  
  465. void FW_CBitmapContext::Reset()
  466. {
  467.     // Nothing to do [HLX] Am i sure ?
  468. }
  469.