gBitmap.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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/color.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. #ifdef TORQUE_OS_IOS
  65. , PVR2 = 8,
  66. PVR2A = 9,
  67. PVR4 = 10,
  68. PVR4A = 11
  69. #endif
  70. };
  71. enum Constants {
  72. c_maxMipLevels = 12 //(2^(12 + 1) = 2048)
  73. };
  74. public:
  75. static GBitmap *load(const char *path);
  76. static ResourceObject * findBmpResource(const char * path);
  77. GBitmap();
  78. GBitmap(const GBitmap&);
  79. GBitmap(const U32 in_width,
  80. const U32 in_height,
  81. const bool in_extrudeMipLevels = false,
  82. const BitmapFormat in_format = RGB);
  83. virtual ~GBitmap();
  84. void allocateBitmap(const U32 in_width,
  85. const U32 in_height,
  86. const bool in_extrudeMipLevels = false,
  87. const BitmapFormat in_format = RGB);
  88. void extrudeMipLevels(bool clearBorders = false);
  89. void extrudeMipLevelsDetail();
  90. GBitmap *createPowerOfTwoBitmap();
  91. void copyRect(const GBitmap *src, const RectI &srcRect, const Point2I &dstPoint);
  92. BitmapFormat getFormat() const;
  93. bool setFormat(BitmapFormat fmt);
  94. U32 getNumMipLevels() const;
  95. U32 getWidth(const U32 in_mipLevel = 0) const;
  96. U32 getHeight(const U32 in_mipLevel = 0) const;
  97. U8* getAddress(const S32 in_x, const S32 in_y, const U32 mipLevel = U32(0));
  98. const U8* getAddress(const S32 in_x, const S32 in_y, const U32 mipLevel = U32(0)) const;
  99. const U8* getBits(const U32 in_mipLevel = 0) const;
  100. U8* getWritableBits(const U32 in_mipLevel = 0);
  101. bool getColorBGRA(const U32 x, const U32 y, ColorI& rColor) const;
  102. bool setColorBGRA(const U32 x, const U32 y, ColorI& rColor);
  103. bool getColor(const U32 x, const U32 y, ColorI& rColor) const;
  104. bool setColor(const U32 x, const U32 y, ColorI& rColor);
  105. /// Note that on set palette, the bitmap deletes its palette.
  106. GPalette const* getPalette() const;
  107. void setPalette(GPalette* in_pPalette);
  108. //-------------------------------------- Internal data/operators
  109. static U32 sBitmapIdSource;
  110. void deleteImage();
  111. BitmapFormat internalFormat;
  112. public:
  113. U8* pBits; // Master bytes
  114. U32 byteSize;
  115. U32 width; // Top level w/h
  116. U32 height;
  117. U32 bytesPerPixel;
  118. U32 numMipLevels;
  119. U32 mipLevelOffsets[c_maxMipLevels];
  120. bool mForce16Bit;//-Mat some paletted images will always be 16bit
  121. GPalette* pPalette; ///< Note that this palette pointer is ALWAYS
  122. /// owned by the bitmap, and will be
  123. /// deleted on exit, or written out on a
  124. /// write.
  125. //-------------------------------------- Input/Output interface
  126. public:
  127. bool readJPEG(Stream& io_rStream); // located in bitmapJpeg.cc
  128. bool writeJPEG(Stream& io_rStream) const;
  129. bool readPNG(Stream& io_rStream); // located in bitmapPng.cc
  130. bool writePNG(Stream& io_rStream, const bool compressHard = false) const;
  131. bool writePNGUncompressed(Stream& io_rStream) const;
  132. bool readMSBmp(Stream& io_rStream); // located in bitmapMS.cc
  133. bool writeMSBmp(Stream& io_rStream) const; // located in bitmapMS.cc
  134. #ifdef TORQUE_OS_IOS
  135. bool readPNGiPhone(Stream& io_rStream); // located in iPhoneUtil.mm
  136. bool readPvr(Stream& io_rStream); // located in bitmapPvr.cc for IPHONE
  137. bool writePvr(Stream& io_rStreeam) const;
  138. #endif
  139. bool read(Stream& io_rStream);
  140. bool write(Stream& io_rStream) const;
  141. private:
  142. bool _writePNG(Stream& stream, const U32, const U32, const U32) const;
  143. static const U32 csFileVersion;
  144. };
  145. //------------------------------------------------------------------------------
  146. //-------------------------------------- Inlines
  147. //
  148. inline GBitmap::BitmapFormat GBitmap::getFormat() const
  149. {
  150. return internalFormat;
  151. }
  152. inline U32 GBitmap::getNumMipLevels() const
  153. {
  154. return numMipLevels;
  155. }
  156. inline U32 GBitmap::getWidth(const U32 in_mipLevel) const
  157. {
  158. AssertFatal(in_mipLevel < numMipLevels,
  159. avar("GBitmap::getWidth: mip level out of range: (%d, %d)",
  160. in_mipLevel, numMipLevels));
  161. U32 retVal = width >> in_mipLevel;
  162. return (retVal != 0) ? retVal : 1;
  163. }
  164. inline U32 GBitmap::getHeight(const U32 in_mipLevel) const
  165. {
  166. AssertFatal(in_mipLevel < numMipLevels,
  167. avar("Bitmap::getHeight: mip level out of range: (%d, %d)",
  168. in_mipLevel, numMipLevels));
  169. U32 retVal = height >> in_mipLevel;
  170. return (retVal != 0) ? retVal : 1;
  171. }
  172. inline const GPalette* GBitmap::getPalette() const
  173. {
  174. AssertFatal(getFormat() == Palettized,
  175. "Error, incorrect internal format to return a palette");
  176. return pPalette;
  177. }
  178. inline const U8* GBitmap::getBits(const U32 in_mipLevel) const
  179. {
  180. AssertFatal(in_mipLevel < numMipLevels,
  181. avar("GBitmap::getBits: mip level out of range: (%d, %d)",
  182. in_mipLevel, numMipLevels));
  183. return &pBits[mipLevelOffsets[in_mipLevel]];
  184. }
  185. inline U8* GBitmap::getWritableBits(const U32 in_mipLevel)
  186. {
  187. AssertFatal(in_mipLevel < numMipLevels,
  188. avar("GBitmap::getWritableBits: mip level out of range: (%d, %d)",
  189. in_mipLevel, numMipLevels));
  190. return &pBits[mipLevelOffsets[in_mipLevel]];
  191. }
  192. inline U8* GBitmap::getAddress(const S32 in_x, const S32 in_y, const U32 mipLevel)
  193. {
  194. return (getWritableBits(mipLevel) + ((in_y * getWidth(mipLevel)) + in_x) * bytesPerPixel);
  195. }
  196. inline const U8* GBitmap::getAddress(const S32 in_x, const S32 in_y, const U32 mipLevel) const
  197. {
  198. return (getBits(mipLevel) + ((in_y * getWidth(mipLevel)) + in_x) * bytesPerPixel);
  199. }
  200. extern void (*bitmapExtrude5551)(const void *srcMip, void *mip, U32 height, U32 width);
  201. extern void (*bitmapExtrudeRGB)(const void *srcMip, void *mip, U32 height, U32 width);
  202. extern void (*bitmapConvertRGB_to_5551)(U8 *src, U32 pixels);
  203. extern void (*bitmapExtrudePaletted)(const void *srcMip, void *mip, U32 height, U32 width);
  204. void bitmapExtrudeRGB_c(const void *srcMip, void *mip, U32 height, U32 width);
  205. #endif //_GBITMAP_H_