123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- /****************************************************************************
- Copyright (c) 2010-2012 cocos2d-x.org
- Copyright (c) 2013-2016 Chukong Technologies Inc.
- Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
- http://www.cocos2d-x.org
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- #ifndef __CC_IMAGE_H__
- #define __CC_IMAGE_H__
- /// @cond DO_NOT_SHOW
- #include "base/CCRef.h"
- #include "renderer/CCTexture2D.h"
- #if CC_USE_WIC
- #include "platform/winrt/WICImageLoader-winrt.h"
- #endif
- // premultiply alpha, or the effect will wrong when want to use other pixel format in Texture2D,
- // such as RGB888, RGB5A1
- #define CC_RGB_PREMULTIPLY_ALPHA(vr, vg, vb, va) \
- (unsigned)(((unsigned)((unsigned char)(vr) * ((unsigned char)(va) + 1)) >> 8) | \
- ((unsigned)((unsigned char)(vg) * ((unsigned char)(va) + 1) >> 8) << 8) | \
- ((unsigned)((unsigned char)(vb) * ((unsigned char)(va) + 1) >> 8) << 16) | \
- ((unsigned)(unsigned char)(va) << 24))
- NS_CC_BEGIN
- /**
- * @addtogroup platform
- * @{
- */
- /**
- @brief Structure which can tell where mipmap begins and how long is it
- */
- typedef struct _MipmapInfo
- {
- unsigned char* address;
- int len;
- _MipmapInfo():address(NULL),len(0){}
- }MipmapInfo;
- class CC_DLL Image : public Ref
- {
- public:
- friend class TextureCache;
- /**
- * @js ctor
- */
- Image();
- /**
- * @js NA
- * @lua NA
- */
- virtual ~Image();
- /** Supported formats for Image */
- enum class Format
- {
- //! JPEG
- JPG,
- //! PNG
- PNG,
- //! TIFF
- TIFF,
- //! WebP
- WEBP,
- //! PVR
- PVR,
- //! ETC
- ETC,
- //! S3TC
- S3TC,
- //! ATITC
- ATITC,
- //! TGA
- TGA,
- //! Raw Data
- RAW_DATA,
- //! Unknown format
- UNKNOWN
- };
- /**
- * Enables or disables premultiplied alpha for PNG files.
- *
- * @param enabled (default: true)
- */
- static void setPNGPremultipliedAlphaEnabled(bool enabled) { PNG_PREMULTIPLIED_ALPHA_ENABLED = enabled; }
-
- /** treats (or not) PVR files as if they have alpha premultiplied.
- Since it is impossible to know at runtime if the PVR images have the alpha channel premultiplied, it is
- possible load them as if they have (or not) the alpha channel premultiplied.
-
- By default it is disabled.
- */
- static void setPVRImagesHavePremultipliedAlpha(bool haveAlphaPremultiplied);
- /**
- @brief Load the image from the specified path.
- @param path the absolute file path.
- @return true if loaded correctly.
- */
- bool initWithImageFile(const std::string& path);
- /**
- @brief Load image from stream buffer.
- @param data stream buffer which holds the image data.
- @param dataLen data length expressed in (number of) bytes.
- @return true if loaded correctly.
- * @js NA
- * @lua NA
- */
- bool initWithImageData(const unsigned char * data, ssize_t dataLen);
- // @warning kFmtRawData only support RGBA8888
- bool initWithRawData(const unsigned char * data, ssize_t dataLen, int width, int height, int bitsPerComponent, bool preMulti = false);
- // Getters
- unsigned char * getData() { return _data; }
- ssize_t getDataLen() { return _dataLen; }
- Format getFileType() { return _fileType; }
- Texture2D::PixelFormat getRenderFormat() { return _renderFormat; }
- int getWidth() { return _width; }
- int getHeight() { return _height; }
- int getNumberOfMipmaps() { return _numberOfMipmaps; }
- MipmapInfo* getMipmaps() { return _mipmaps; }
- bool hasPremultipliedAlpha() { return _hasPremultipliedAlpha; }
- CC_DEPRECATED_ATTRIBUTE bool isPremultipliedAlpha() { return _hasPremultipliedAlpha; }
- std::string getFilePath() const { return _filePath; }
- int getBitPerPixel();
- bool hasAlpha();
- bool isCompressed();
- /**
- @brief Save Image data to the specified file, with specified format.
- @param filePath the file's absolute path, including file suffix.
- @param isToRGB whether the image is saved as RGB format.
- */
- bool saveToFile(const std::string &filename, bool isToRGB = true);
- protected:
- #if CC_USE_WIC
- bool encodeWithWIC(const std::string& filePath, bool isToRGB, GUID containerFormat);
- bool decodeWithWIC(const unsigned char *data, ssize_t dataLen);
- #endif
- bool initWithJpgData(const unsigned char * data, ssize_t dataLen);
- bool initWithPngData(const unsigned char * data, ssize_t dataLen);
- bool initWithTiffData(const unsigned char * data, ssize_t dataLen);
- bool initWithWebpData(const unsigned char * data, ssize_t dataLen);
- bool initWithPVRData(const unsigned char * data, ssize_t dataLen);
- bool initWithPVRv2Data(const unsigned char * data, ssize_t dataLen);
- bool initWithPVRv3Data(const unsigned char * data, ssize_t dataLen);
- bool initWithETCData(const unsigned char * data, ssize_t dataLen);
- bool initWithS3TCData(const unsigned char * data, ssize_t dataLen);
- bool initWithATITCData(const unsigned char *data, ssize_t dataLen);
- typedef struct sImageTGA tImageTGA;
- bool initWithTGAData(tImageTGA* tgaData);
- bool saveImageToPNG(const std::string& filePath, bool isToRGB = true);
- bool saveImageToJPG(const std::string& filePath);
-
- void premultipliedAlpha();
-
- protected:
- /**
- @brief Determine how many mipmaps can we have.
- It's same as define but it respects namespaces
- */
- static const int MIPMAP_MAX = 16;
- /**
- @brief Determine whether we premultiply alpha for png files.
- */
- static bool PNG_PREMULTIPLIED_ALPHA_ENABLED;
- unsigned char *_data;
- ssize_t _dataLen;
- int _width;
- int _height;
- bool _unpack;
- Format _fileType;
- Texture2D::PixelFormat _renderFormat;
- MipmapInfo _mipmaps[MIPMAP_MAX]; // pointer to mipmap images
- int _numberOfMipmaps;
- // false if we can't auto detect the image is premultiplied or not.
- bool _hasPremultipliedAlpha;
- std::string _filePath;
- protected:
- // noncopyable
- Image(const Image& rImg);
- Image& operator=(const Image&);
-
- /*
- @brief The same result as with initWithImageFile, but thread safe. It is caused by
- loadImage() in TextureCache.cpp.
- @param fullpath full path of the file.
- @param imageType the type of image, currently only supporting two types.
- @return true if loaded correctly.
- */
- bool initWithImageFileThreadSafe(const std::string& fullpath);
-
- Format detectFormat(const unsigned char * data, ssize_t dataLen);
- bool isPng(const unsigned char * data, ssize_t dataLen);
- bool isJpg(const unsigned char * data, ssize_t dataLen);
- bool isTiff(const unsigned char * data, ssize_t dataLen);
- bool isWebp(const unsigned char * data, ssize_t dataLen);
- bool isPvr(const unsigned char * data, ssize_t dataLen);
- bool isEtc(const unsigned char * data, ssize_t dataLen);
- bool isS3TC(const unsigned char * data,ssize_t dataLen);
- bool isATITC(const unsigned char *data, ssize_t dataLen);
- };
- // end of platform group
- /// @}
- NS_CC_END
- /// @endcond
- #endif // __CC_IMAGE_H__
|