CCImage.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  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 FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __CC_IMAGE_H__
  23. #define __CC_IMAGE_H__
  24. /// @cond DO_NOT_SHOW
  25. #include "base/CCRef.h"
  26. #include "renderer/CCTexture2D.h"
  27. #if CC_USE_WIC
  28. #include "platform/winrt/WICImageLoader-winrt.h"
  29. #endif
  30. // premultiply alpha, or the effect will wrong when want to use other pixel format in Texture2D,
  31. // such as RGB888, RGB5A1
  32. #define CC_RGB_PREMULTIPLY_ALPHA(vr, vg, vb, va) \
  33. (unsigned)(((unsigned)((unsigned char)(vr) * ((unsigned char)(va) + 1)) >> 8) | \
  34. ((unsigned)((unsigned char)(vg) * ((unsigned char)(va) + 1) >> 8) << 8) | \
  35. ((unsigned)((unsigned char)(vb) * ((unsigned char)(va) + 1) >> 8) << 16) | \
  36. ((unsigned)(unsigned char)(va) << 24))
  37. NS_CC_BEGIN
  38. /**
  39. * @addtogroup platform
  40. * @{
  41. */
  42. /**
  43. @brief Structure which can tell where mipmap begins and how long is it
  44. */
  45. typedef struct _MipmapInfo
  46. {
  47. unsigned char* address;
  48. int len;
  49. _MipmapInfo():address(NULL),len(0){}
  50. }MipmapInfo;
  51. class CC_DLL Image : public Ref
  52. {
  53. public:
  54. friend class TextureCache;
  55. /**
  56. * @js ctor
  57. */
  58. Image();
  59. /**
  60. * @js NA
  61. * @lua NA
  62. */
  63. virtual ~Image();
  64. /** Supported formats for Image */
  65. enum class Format
  66. {
  67. //! JPEG
  68. JPG,
  69. //! PNG
  70. PNG,
  71. //! TIFF
  72. TIFF,
  73. //! WebP
  74. WEBP,
  75. //! PVR
  76. PVR,
  77. //! ETC
  78. ETC,
  79. //! S3TC
  80. S3TC,
  81. //! ATITC
  82. ATITC,
  83. //! TGA
  84. TGA,
  85. //! Raw Data
  86. RAW_DATA,
  87. //! Unknown format
  88. UNKNOWN
  89. };
  90. /**
  91. * Enables or disables premultiplied alpha for PNG files.
  92. *
  93. * @param enabled (default: true)
  94. */
  95. static void setPNGPremultipliedAlphaEnabled(bool enabled) { PNG_PREMULTIPLIED_ALPHA_ENABLED = enabled; }
  96. /** treats (or not) PVR files as if they have alpha premultiplied.
  97. Since it is impossible to know at runtime if the PVR images have the alpha channel premultiplied, it is
  98. possible load them as if they have (or not) the alpha channel premultiplied.
  99. By default it is disabled.
  100. */
  101. static void setPVRImagesHavePremultipliedAlpha(bool haveAlphaPremultiplied);
  102. /**
  103. @brief Load the image from the specified path.
  104. @param path the absolute file path.
  105. @return true if loaded correctly.
  106. */
  107. bool initWithImageFile(const std::string& path);
  108. /**
  109. @brief Load image from stream buffer.
  110. @param data stream buffer which holds the image data.
  111. @param dataLen data length expressed in (number of) bytes.
  112. @return true if loaded correctly.
  113. * @js NA
  114. * @lua NA
  115. */
  116. bool initWithImageData(const unsigned char * data, ssize_t dataLen);
  117. // @warning kFmtRawData only support RGBA8888
  118. bool initWithRawData(const unsigned char * data, ssize_t dataLen, int width, int height, int bitsPerComponent, bool preMulti = false);
  119. // Getters
  120. unsigned char * getData() { return _data; }
  121. ssize_t getDataLen() { return _dataLen; }
  122. Format getFileType() { return _fileType; }
  123. Texture2D::PixelFormat getRenderFormat() { return _renderFormat; }
  124. int getWidth() { return _width; }
  125. int getHeight() { return _height; }
  126. int getNumberOfMipmaps() { return _numberOfMipmaps; }
  127. MipmapInfo* getMipmaps() { return _mipmaps; }
  128. bool hasPremultipliedAlpha() { return _hasPremultipliedAlpha; }
  129. CC_DEPRECATED_ATTRIBUTE bool isPremultipliedAlpha() { return _hasPremultipliedAlpha; }
  130. std::string getFilePath() const { return _filePath; }
  131. int getBitPerPixel();
  132. bool hasAlpha();
  133. bool isCompressed();
  134. /**
  135. @brief Save Image data to the specified file, with specified format.
  136. @param filePath the file's absolute path, including file suffix.
  137. @param isToRGB whether the image is saved as RGB format.
  138. */
  139. bool saveToFile(const std::string &filename, bool isToRGB = true);
  140. protected:
  141. #if CC_USE_WIC
  142. bool encodeWithWIC(const std::string& filePath, bool isToRGB, GUID containerFormat);
  143. bool decodeWithWIC(const unsigned char *data, ssize_t dataLen);
  144. #endif
  145. bool initWithJpgData(const unsigned char * data, ssize_t dataLen);
  146. bool initWithPngData(const unsigned char * data, ssize_t dataLen);
  147. bool initWithTiffData(const unsigned char * data, ssize_t dataLen);
  148. bool initWithWebpData(const unsigned char * data, ssize_t dataLen);
  149. bool initWithPVRData(const unsigned char * data, ssize_t dataLen);
  150. bool initWithPVRv2Data(const unsigned char * data, ssize_t dataLen);
  151. bool initWithPVRv3Data(const unsigned char * data, ssize_t dataLen);
  152. bool initWithETCData(const unsigned char * data, ssize_t dataLen);
  153. bool initWithS3TCData(const unsigned char * data, ssize_t dataLen);
  154. bool initWithATITCData(const unsigned char *data, ssize_t dataLen);
  155. typedef struct sImageTGA tImageTGA;
  156. bool initWithTGAData(tImageTGA* tgaData);
  157. bool saveImageToPNG(const std::string& filePath, bool isToRGB = true);
  158. bool saveImageToJPG(const std::string& filePath);
  159. void premultipliedAlpha();
  160. protected:
  161. /**
  162. @brief Determine how many mipmaps can we have.
  163. It's same as define but it respects namespaces
  164. */
  165. static const int MIPMAP_MAX = 16;
  166. /**
  167. @brief Determine whether we premultiply alpha for png files.
  168. */
  169. static bool PNG_PREMULTIPLIED_ALPHA_ENABLED;
  170. unsigned char *_data;
  171. ssize_t _dataLen;
  172. int _width;
  173. int _height;
  174. bool _unpack;
  175. Format _fileType;
  176. Texture2D::PixelFormat _renderFormat;
  177. MipmapInfo _mipmaps[MIPMAP_MAX]; // pointer to mipmap images
  178. int _numberOfMipmaps;
  179. // false if we can't auto detect the image is premultiplied or not.
  180. bool _hasPremultipliedAlpha;
  181. std::string _filePath;
  182. protected:
  183. // noncopyable
  184. Image(const Image& rImg);
  185. Image& operator=(const Image&);
  186. /*
  187. @brief The same result as with initWithImageFile, but thread safe. It is caused by
  188. loadImage() in TextureCache.cpp.
  189. @param fullpath full path of the file.
  190. @param imageType the type of image, currently only supporting two types.
  191. @return true if loaded correctly.
  192. */
  193. bool initWithImageFileThreadSafe(const std::string& fullpath);
  194. Format detectFormat(const unsigned char * data, ssize_t dataLen);
  195. bool isPng(const unsigned char * data, ssize_t dataLen);
  196. bool isJpg(const unsigned char * data, ssize_t dataLen);
  197. bool isTiff(const unsigned char * data, ssize_t dataLen);
  198. bool isWebp(const unsigned char * data, ssize_t dataLen);
  199. bool isPvr(const unsigned char * data, ssize_t dataLen);
  200. bool isEtc(const unsigned char * data, ssize_t dataLen);
  201. bool isS3TC(const unsigned char * data,ssize_t dataLen);
  202. bool isATITC(const unsigned char *data, ssize_t dataLen);
  203. };
  204. // end of platform group
  205. /// @}
  206. NS_CC_END
  207. /// @endcond
  208. #endif // __CC_IMAGE_H__