CCImage.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __CC_IMAGE_H__
  22. #define __CC_IMAGE_H__
  23. /// @cond DO_NOT_SHOW
  24. #include "base/CCRef.h"
  25. #include "platform/CCGL.h"
  26. #include "platform/CCStdC.h"
  27. #include <string>
  28. #include <map>
  29. // premultiply alpha, or the effect will wrong when want to use other pixel format in Texture2D,
  30. // such as RGB888, RGB5A1
  31. #define CC_RGB_PREMULTIPLY_ALPHA(vr, vg, vb, va) \
  32. (unsigned)(((unsigned)((unsigned char)(vr) * ((unsigned char)(va) + 1)) >> 8) | \
  33. ((unsigned)((unsigned char)(vg) * ((unsigned char)(va) + 1) >> 8) << 8) | \
  34. ((unsigned)((unsigned char)(vb) * ((unsigned char)(va) + 1) >> 8) << 16) | \
  35. ((unsigned)(unsigned char)(va) << 24))
  36. NS_CC_BEGIN
  37. /**
  38. * @addtogroup platform
  39. * @{
  40. */
  41. /**
  42. @brief Structure which can tell where mipmap begins and how long is it
  43. */
  44. typedef struct _MipmapInfo
  45. {
  46. unsigned char* address = nullptr;
  47. int offset = 0;
  48. int len = 0;
  49. }MipmapInfo;
  50. class Image : public Ref
  51. {
  52. public:
  53. /**
  54. * @js ctor
  55. */
  56. Image();
  57. /** Supported formats for Image */
  58. enum class Format
  59. {
  60. //! JPEG
  61. JPG,
  62. //! PNG
  63. PNG,
  64. //! TIFF
  65. TIFF,
  66. //! WebP
  67. WEBP,
  68. //! PVR
  69. PVR,
  70. //! ETC
  71. ETC,
  72. //! ETC2
  73. ETC2,
  74. //! S3TC
  75. S3TC,
  76. //! ATITC
  77. // ATITC,
  78. //! TGA
  79. TGA,
  80. //! Raw Data
  81. RAW_DATA,
  82. //! Unknown format
  83. UNKNOWN
  84. };
  85. /** @typedef Texture2D::PixelFormat
  86. Possible texture pixel formats
  87. */
  88. enum class PixelFormat
  89. {
  90. //! auto detect the type
  91. AUTO,
  92. //! 32-bit texture: BGRA8888
  93. BGRA8888,
  94. //! 32-bit texture: RGBA8888
  95. RGBA8888,
  96. //! 24-bit texture: RGBA888
  97. RGB888,
  98. //! 16-bit texture without Alpha channel
  99. RGB565,
  100. //! 8-bit textures used as masks
  101. A8,
  102. //! 8-bit intensity texture
  103. I8,
  104. //! 16-bit textures used as masks
  105. AI88,
  106. //! 16-bit textures: RGBA4444
  107. RGBA4444,
  108. //! 16-bit textures: RGB5A1
  109. RGB5A1,
  110. //! 4-bit PVRTC-compressed texture: PVRTC4
  111. PVRTC4,
  112. //! 4-bit PVRTC-compressed texture: PVRTC4 (has alpha channel)
  113. PVRTC4A,
  114. //! 2-bit PVRTC-compressed texture: PVRTC2
  115. PVRTC2,
  116. //! 2-bit PVRTC-compressed texture: PVRTC2 (has alpha channel)
  117. PVRTC2A,
  118. //! ETC-compressed texture: ETC
  119. ETC,
  120. //! ETC-compressed texture: GL_COMPRESSED_RGB8_ETC2
  121. ETC2_RGB,
  122. //! ETC-compressed texture: GL_COMPRESSED_RGBA8_ETC2
  123. ETC2_RGBA,
  124. //! S3TC-compressed texture: S3TC_Dxt1
  125. S3TC_DXT1,
  126. //! S3TC-compressed texture: S3TC_Dxt3
  127. S3TC_DXT3,
  128. //! S3TC-compressed texture: S3TC_Dxt5
  129. S3TC_DXT5,
  130. //! ATITC-compressed texture: ATC_RGB
  131. ATC_RGB,
  132. //! ATITC-compressed texture: ATC_EXPLICIT_ALPHA
  133. ATC_EXPLICIT_ALPHA,
  134. //! ATITC-compressed texture: ATC_INTERPOLATED_ALPHA
  135. ATC_INTERPOLATED_ALPHA,
  136. //! Default texture format: AUTO
  137. DEFAULT = AUTO,
  138. NONE = -1
  139. };
  140. struct PixelFormatInfo {
  141. PixelFormatInfo(GLenum anInternalFormat, GLenum aFormat, GLenum aType, int aBpp, bool aCompressed, bool anAlpha)
  142. : internalFormat(anInternalFormat)
  143. , format(aFormat)
  144. , type(aType)
  145. , bpp(aBpp)
  146. , compressed(aCompressed)
  147. , alpha(anAlpha)
  148. {}
  149. GLenum internalFormat;
  150. GLenum format;
  151. GLenum type;
  152. int bpp;
  153. bool compressed;
  154. bool alpha;
  155. };
  156. typedef std::map<PixelFormat, const PixelFormatInfo> PixelFormatInfoMap;
  157. /**
  158. * Enables or disables premultiplied alpha for PNG files.
  159. *
  160. * @param enabled (default: true)
  161. */
  162. static void setPNGPremultipliedAlphaEnabled(bool enabled) { PNG_PREMULTIPLIED_ALPHA_ENABLED = enabled; }
  163. /** treats (or not) PVR files as if they have alpha premultiplied.
  164. Since it is impossible to know at runtime if the PVR images have the alpha channel premultiplied, it is
  165. possible load them as if they have (or not) the alpha channel premultiplied.
  166. By default it is disabled.
  167. */
  168. static void setPVRImagesHavePremultipliedAlpha(bool haveAlphaPremultiplied);
  169. /**
  170. @brief Load the image from the specified path.
  171. @param path the absolute file path.
  172. @return true if loaded correctly.
  173. */
  174. bool initWithImageFile(const std::string& path);
  175. /**
  176. @brief Load image from stream buffer.
  177. @param data stream buffer which holds the image data.
  178. @param dataLen data length expressed in (number of) bytes.
  179. @return true if loaded correctly.
  180. * @js NA
  181. * @lua NA
  182. */
  183. bool initWithImageData(const unsigned char * data, ssize_t dataLen);
  184. // @warning kFmtRawData only support RGBA8888
  185. bool initWithRawData(const unsigned char * data, ssize_t dataLen, int width, int height, int bitsPerComponent, bool preMulti = false);
  186. // Getters
  187. inline unsigned char* getData() const { return _data; }
  188. inline ssize_t getDataLen() const { return _dataLen; }
  189. inline Format getFileType() const { return _fileType; }
  190. inline PixelFormat getRenderFormat() const { return _renderFormat; }
  191. inline int getWidth() const { return _width; }
  192. inline int getHeight() const { return _height; }
  193. inline int getNumberOfMipmaps() const { return _numberOfMipmaps; }
  194. inline const MipmapInfo* getMipmaps() const { return _mipmaps; }
  195. inline bool hasPremultipliedAlpha() const { return _hasPremultipliedAlpha; }
  196. inline std::string getFilePath() const { return _filePath; }
  197. int getBitPerPixel() const;
  198. bool hasAlpha() const;
  199. bool isCompressed() const;
  200. const PixelFormatInfo& getPixelFormatInfo() const;
  201. /**
  202. @brief Save Image data to the specified file, with specified format.
  203. @param filename the file's absolute path, including file suffix.
  204. @param isToRGB whether the image is saved as RGB format.
  205. */
  206. bool saveToFile(const std::string &filename, bool isToRGB = true);
  207. protected:
  208. bool initWithJpgData(const unsigned char * data, ssize_t dataLen);
  209. bool initWithPngData(const unsigned char * data, ssize_t dataLen);
  210. bool initWithTiffData(const unsigned char * data, ssize_t dataLen);
  211. bool initWithWebpData(const unsigned char * data, ssize_t dataLen);
  212. bool initWithPVRData(const unsigned char * data, ssize_t dataLen);
  213. bool initWithPVRv2Data(const unsigned char * data, ssize_t dataLen);
  214. bool initWithPVRv3Data(const unsigned char * data, ssize_t dataLen);
  215. bool initWithETCData(const unsigned char * data, ssize_t dataLen);
  216. bool initWithETC2Data(const unsigned char * data, ssize_t dataLen);
  217. bool initWithS3TCData(const unsigned char * data, ssize_t dataLen);
  218. typedef struct sImageTGA tImageTGA;
  219. bool initWithTGAData(tImageTGA* tgaData);
  220. bool saveImageToPNG(const std::string& filePath, bool isToRGB = true);
  221. bool saveImageToJPG(const std::string& filePath);
  222. void premultipliedAlpha();
  223. protected:
  224. /**
  225. @brief Determine how many mipmaps can we have.
  226. It's same as define but it respects namespaces
  227. */
  228. static const int MIPMAP_MAX = 16;
  229. /**
  230. @brief Determine whether we premultiply alpha for png files.
  231. */
  232. static bool PNG_PREMULTIPLIED_ALPHA_ENABLED;
  233. unsigned char *_data;
  234. ssize_t _dataLen;
  235. int _width;
  236. int _height;
  237. Format _fileType;
  238. PixelFormat _renderFormat;
  239. MipmapInfo _mipmaps[MIPMAP_MAX]; // pointer to mipmap images
  240. int _numberOfMipmaps;
  241. // false if we can't auto detect the image is premultiplied or not.
  242. bool _hasPremultipliedAlpha;
  243. std::string _filePath;
  244. protected:
  245. // noncopyable
  246. Image(const Image&) = delete;
  247. Image& operator=(const Image&) = delete;
  248. // nonmoveable
  249. Image(Image&&) = delete;
  250. Image& operator=(Image&&) = delete;
  251. /**
  252. * @js NA
  253. * @lua NA
  254. */
  255. virtual ~Image();
  256. Format detectFormat(const unsigned char * data, ssize_t dataLen);
  257. bool isPng(const unsigned char * data, ssize_t dataLen);
  258. bool isJpg(const unsigned char * data, ssize_t dataLen);
  259. bool isTiff(const unsigned char * data, ssize_t dataLen);
  260. bool isWebp(const unsigned char * data, ssize_t dataLen);
  261. bool isPvr(const unsigned char * data, ssize_t dataLen);
  262. bool isEtc(const unsigned char * data, ssize_t dataLen);
  263. bool isEtc2(const unsigned char * data, ssize_t dataLen);
  264. bool isS3TC(const unsigned char * data,ssize_t dataLen);
  265. };
  266. // end of platform group
  267. /// @}
  268. NS_CC_END
  269. /// @endcond
  270. #endif // __CC_IMAGE_H__