gBitmap.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _GBITMAP_H_
  23. #define _GBITMAP_H_
  24. //Includes
  25. #ifndef _PLATFORM_H_
  26. #include "platform/platform.h"
  27. #endif
  28. #ifndef _RESMANAGER_H_
  29. #include "io/resource/resourceManager.h"
  30. #endif
  31. #ifndef _COLOR_H_
  32. #include "graphics/gColor.h"
  33. #endif
  34. //-------------------------------------- Forward decls.
  35. class Stream;
  36. class GPalette;
  37. class RectI;
  38. extern ResourceInstance* constructBitmapBMP(Stream& stream);
  39. extern ResourceInstance* constructBitmapPNG(Stream& stream);
  40. extern ResourceInstance* constructBitmapJPEG(Stream& stream);
  41. #ifdef TORQUE_OS_IOS
  42. extern ResourceInstance* constructBitmapPVR(Stream& stream);
  43. #endif
  44. //------------------------------------------------------------------------------
  45. //-------------------------------------- GBitmap
  46. //
  47. class GBitmap: public ResourceInstance
  48. {
  49. //-------------------------------------- public enumerants and structures
  50. public:
  51. /// BitmapFormat and UsageHint are
  52. /// written to the stream in write(...),
  53. /// be sure to maintain compatability
  54. /// if they are changed.
  55. enum BitmapFormat {
  56. Palettized = 0,
  57. Intensity = 1,
  58. RGB = 2,
  59. RGBA = 3,
  60. Alpha = 4,
  61. RGB565 = 5,
  62. RGB5551 = 6,
  63. Luminance = 7,
  64. LuminanceAlpha = 8
  65. #ifdef TORQUE_OS_IOS
  66. , PVR2 = 9,
  67. PVR2A = 10,
  68. PVR4 = 11,
  69. PVR4A = 12
  70. #endif
  71. };
  72. enum Constants {
  73. c_maxMipLevels = 12 //(2^(12 + 1) = 2048)
  74. };
  75. public:
  76. static GBitmap *load(const char *path);
  77. static ResourceObject * findBmpResource(const char * path);
  78. GBitmap();
  79. GBitmap(const GBitmap&);
  80. GBitmap(const U32 in_width,
  81. const U32 in_height,
  82. const bool in_extrudeMipLevels = false,
  83. const BitmapFormat in_format = RGB);
  84. virtual ~GBitmap();
  85. void allocateBitmap(const U32 in_width,
  86. const U32 in_height,
  87. const bool in_extrudeMipLevels = false,
  88. const BitmapFormat in_format = RGB);
  89. void extrudeMipLevels(bool clearBorders = false);
  90. void extrudeMipLevelsDetail();
  91. GBitmap *createPowerOfTwoBitmap();
  92. void copyRect(const GBitmap *src, const RectI &srcRect, const Point2I &dstPoint);
  93. BitmapFormat getFormat() const;
  94. bool setFormat(BitmapFormat fmt);
  95. U32 getNumMipLevels() const;
  96. U32 getWidth(const U32 in_mipLevel = 0) const;
  97. U32 getHeight(const U32 in_mipLevel = 0) const;
  98. U8* getAddress(const S32 in_x, const S32 in_y, const U32 mipLevel = U32(0));
  99. const U8* getAddress(const S32 in_x, const S32 in_y, const U32 mipLevel = U32(0)) const;
  100. const U8* getBits(const U32 in_mipLevel = 0) const;
  101. U8* getWritableBits(const U32 in_mipLevel = 0);
  102. bool getColorBGRA(const U32 x, const U32 y, ColorI& rColor) const;
  103. bool setColorBGRA(const U32 x, const U32 y, ColorI& rColor);
  104. bool getColor(const U32 x, const U32 y, ColorI& rColor) const;
  105. bool setColor(const U32 x, const U32 y, ColorI& rColor);
  106. /// Note that on set palette, the bitmap deletes its palette.
  107. GPalette const* getPalette() const;
  108. void setPalette(GPalette* in_pPalette);
  109. //-------------------------------------- Internal data/operators
  110. static U32 sBitmapIdSource;
  111. void deleteImage();
  112. void clearImage(const ColorF blendColor);
  113. BitmapFormat internalFormat;
  114. public:
  115. U8* pBits; // Master bytes
  116. U32 byteSize;
  117. U32 width; // Top level w/h
  118. U32 height;
  119. U32 bytesPerPixel;
  120. U32 numMipLevels;
  121. U32 mipLevelOffsets[c_maxMipLevels];
  122. bool mForce16Bit;//-Mat some paletted images will always be 16bit
  123. GPalette* pPalette; ///< Note that this palette pointer is ALWAYS
  124. /// owned by the bitmap, and will be
  125. /// deleted on exit, or written out on a
  126. /// write.
  127. bool mergeLayer(const Point2I pos, const GBitmap* layer, const ColorF blendColor);
  128. //-------------------------------------- Input/Output interface
  129. public:
  130. bool readJPEG(Stream& io_rStream); // located in bitmapJpeg.cc
  131. bool writeJPEG(Stream& io_rStream) const;
  132. bool readPNG(Stream& io_rStream); // located in bitmapPng.cc
  133. bool writePNG(Stream& io_rStream, const bool compressHard = false) const;
  134. bool writePNGUncompressed(Stream& io_rStream) const;
  135. bool readMSBmp(Stream& io_rStream); // located in bitmapMS.cc
  136. bool writeMSBmp(Stream& io_rStream) const; // located in bitmapMS.cc
  137. #ifdef TORQUE_OS_IOS
  138. bool readPNGiPhone(Stream& io_rStream); // located in iPhoneUtil.mm
  139. bool readPvr(Stream& io_rStream); // located in bitmapPvr.cc for IPHONE
  140. bool writePvr(Stream& io_rStreeam) const;
  141. #endif
  142. bool read(Stream& io_rStream);
  143. bool write(Stream& io_rStream) const;
  144. private:
  145. bool _writePNG(Stream& stream, const U32, const U32, const U32) const;
  146. static const U32 csFileVersion;
  147. };
  148. //------------------------------------------------------------------------------
  149. //-------------------------------------- Inlines
  150. //
  151. inline GBitmap::BitmapFormat GBitmap::getFormat() const
  152. {
  153. return internalFormat;
  154. }
  155. inline U32 GBitmap::getNumMipLevels() const
  156. {
  157. return numMipLevels;
  158. }
  159. inline U32 GBitmap::getWidth(const U32 in_mipLevel) const
  160. {
  161. AssertFatal(in_mipLevel < numMipLevels,
  162. avar("GBitmap::getWidth: mip level out of range: (%d, %d)",
  163. in_mipLevel, numMipLevels));
  164. U32 retVal = width >> in_mipLevel;
  165. return (retVal != 0) ? retVal : 1;
  166. }
  167. inline U32 GBitmap::getHeight(const U32 in_mipLevel) const
  168. {
  169. AssertFatal(in_mipLevel < numMipLevels,
  170. avar("Bitmap::getHeight: mip level out of range: (%d, %d)",
  171. in_mipLevel, numMipLevels));
  172. U32 retVal = height >> in_mipLevel;
  173. return (retVal != 0) ? retVal : 1;
  174. }
  175. inline const GPalette* GBitmap::getPalette() const
  176. {
  177. AssertFatal(getFormat() == Palettized,
  178. "Error, incorrect internal format to return a palette");
  179. return pPalette;
  180. }
  181. inline const U8* GBitmap::getBits(const U32 in_mipLevel) const
  182. {
  183. AssertFatal(in_mipLevel < numMipLevels,
  184. avar("GBitmap::getBits: mip level out of range: (%d, %d)",
  185. in_mipLevel, numMipLevels));
  186. return &pBits[mipLevelOffsets[in_mipLevel]];
  187. }
  188. inline U8* GBitmap::getWritableBits(const U32 in_mipLevel)
  189. {
  190. AssertFatal(in_mipLevel < numMipLevels,
  191. avar("GBitmap::getWritableBits: mip level out of range: (%d, %d)",
  192. in_mipLevel, numMipLevels));
  193. return &pBits[mipLevelOffsets[in_mipLevel]];
  194. }
  195. inline U8* GBitmap::getAddress(const S32 in_x, const S32 in_y, const U32 mipLevel)
  196. {
  197. return (getWritableBits(mipLevel) + ((in_y * getWidth(mipLevel)) + in_x) * bytesPerPixel);
  198. }
  199. inline const U8* GBitmap::getAddress(const S32 in_x, const S32 in_y, const U32 mipLevel) const
  200. {
  201. return (getBits(mipLevel) + ((in_y * getWidth(mipLevel)) + in_x) * bytesPerPixel);
  202. }
  203. extern void (*bitmapExtrude5551)(const void *srcMip, void *mip, U32 height, U32 width);
  204. extern void (*bitmapExtrudeRGB)(const void *srcMip, void *mip, U32 height, U32 width);
  205. extern void (*bitmapConvertRGB_to_5551)(U8 *src, U32 pixels);
  206. extern void (*bitmapExtrudePaletted)(const void *srcMip, void *mip, U32 height, U32 width);
  207. void bitmapExtrudeRGB_c(const void *srcMip, void *mip, U32 height, U32 width);
  208. #endif //_GBITMAP_H_