2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
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__
|
2015-03-24 20:23:51 +08:00
|
|
|
/// @cond DO_NOT_SHOW
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCRef.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTexture2D.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2016-01-30 05:34:01 +08:00
|
|
|
#if CC_USE_WIC
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "platform/winrt/WICImageLoader-winrt.h"
|
2015-04-16 00:19:38 +08:00
|
|
|
#endif
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// premultiply alpha, or the effect will wrong when want to use other pixel format in Texture2D,
|
2013-06-13 05:46:32 +08:00
|
|
|
// 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))
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup platform
|
|
|
|
* @{
|
|
|
|
*/
|
2013-08-01 15:53:52 +08:00
|
|
|
|
2013-07-25 21:35:00 +08:00
|
|
|
/**
|
2013-08-01 15:53:52 +08:00
|
|
|
@brief Structure which can tell where mipmap begins and how long is it
|
|
|
|
*/
|
2013-08-06 16:14:36 +08:00
|
|
|
typedef struct _MipmapInfo
|
|
|
|
{
|
2013-07-25 21:35:00 +08:00
|
|
|
unsigned char* address;
|
|
|
|
int len;
|
2014-03-20 17:07:28 +08:00
|
|
|
_MipmapInfo():address(NULL),len(0){}
|
2013-07-25 21:35:00 +08:00
|
|
|
}MipmapInfo;
|
2012-06-20 18:09:11 +08:00
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
class CC_DLL Image : public Ref
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-26 16:42:11 +08:00
|
|
|
friend class TextureCache;
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
Image();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-23 18:26:26 +08:00
|
|
|
virtual ~Image();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 05:49:43 +08:00
|
|
|
/** Supported formats for Image */
|
|
|
|
enum class Format
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 05:49:43 +08:00
|
|
|
//! JPEG
|
|
|
|
JPG,
|
|
|
|
//! PNG
|
|
|
|
PNG,
|
|
|
|
//! TIFF
|
|
|
|
TIFF,
|
|
|
|
//! WebP
|
|
|
|
WEBP,
|
2013-07-27 22:06:30 +08:00
|
|
|
//! PVR
|
|
|
|
PVR,
|
|
|
|
//! ETC
|
|
|
|
ETC,
|
2013-08-06 11:19:45 +08:00
|
|
|
//! S3TC
|
|
|
|
S3TC,
|
2013-08-16 11:02:44 +08:00
|
|
|
//! ATITC
|
|
|
|
ATITC,
|
2013-11-29 14:31:42 +08:00
|
|
|
//! TGA
|
|
|
|
TGA,
|
2013-07-26 05:49:43 +08:00
|
|
|
//! Raw Data
|
|
|
|
RAW_DATA,
|
|
|
|
//! Unknown format
|
2015-02-24 01:36:20 +08:00
|
|
|
UNKNOWN
|
2013-07-25 20:29:53 +08:00
|
|
|
};
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2016-05-17 12:06:28 +08:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2012-12-29 10:27:43 +08:00
|
|
|
/**
|
2013-08-01 21:40:13 +08:00
|
|
|
@brief Load the image from the specified path.
|
|
|
|
@param path the absolute file path.
|
|
|
|
@return true if loaded correctly.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-12-24 10:51:47 +08:00
|
|
|
bool initWithImageFile(const std::string& path);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2013-08-01 21:40:13 +08:00
|
|
|
@brief Load image from stream buffer.
|
2013-07-25 21:35:00 +08:00
|
|
|
@param data stream buffer which holds the image data.
|
|
|
|
@param dataLen data length expressed in (number of) bytes.
|
2012-09-15 06:26:38 +08:00
|
|
|
@return true if loaded correctly.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-12-05 17:19:01 +08:00
|
|
|
bool initWithImageData(const unsigned char * data, ssize_t dataLen);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-13 05:46:32 +08:00
|
|
|
// @warning kFmtRawData only support RGBA8888
|
2013-12-05 17:19:01 +08:00
|
|
|
bool initWithRawData(const unsigned char * data, ssize_t dataLen, int width, int height, int bitsPerComponent, bool preMulti = false);
|
2013-06-13 05:46:32 +08:00
|
|
|
|
2013-07-27 22:06:30 +08:00
|
|
|
// Getters
|
2016-09-12 09:44:21 +08:00
|
|
|
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; }
|
2013-05-01 07:36:14 +08:00
|
|
|
|
2013-07-30 11:10:05 +08:00
|
|
|
int getBitPerPixel();
|
|
|
|
bool hasAlpha();
|
|
|
|
bool isCompressed();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-07-23 18:26:26 +08:00
|
|
|
@brief Save Image data to the specified file, with specified format.
|
2013-08-01 21:40:13 +08:00
|
|
|
@param filePath the file's absolute path, including file suffix.
|
|
|
|
@param isToRGB whether the image is saved as RGB format.
|
2013-07-23 18:26:26 +08:00
|
|
|
*/
|
2013-11-15 09:19:16 +08:00
|
|
|
bool saveToFile(const std::string &filename, bool isToRGB = true);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
protected:
|
2016-01-30 05:34:01 +08:00
|
|
|
#if CC_USE_WIC
|
2015-08-14 21:17:51 +08:00
|
|
|
bool encodeWithWIC(const std::string& filePath, bool isToRGB, GUID containerFormat);
|
|
|
|
bool decodeWithWIC(const unsigned char *data, ssize_t dataLen);
|
2015-04-16 00:19:38 +08:00
|
|
|
#endif
|
2013-12-05 17:19:01 +08:00
|
|
|
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);
|
2013-11-29 14:31:42 +08:00
|
|
|
typedef struct sImageTGA tImageTGA;
|
|
|
|
bool initWithTGAData(tImageTGA* tgaData);
|
2013-08-06 16:14:36 +08:00
|
|
|
|
2013-11-15 09:19:16 +08:00
|
|
|
bool saveImageToPNG(const std::string& filePath, bool isToRGB = true);
|
|
|
|
bool saveImageToJPG(const std::string& filePath);
|
2013-08-01 15:53:52 +08:00
|
|
|
|
2014-06-03 15:06:15 +08:00
|
|
|
void premultipliedAlpha();
|
|
|
|
|
2014-03-25 11:13:44 +08:00
|
|
|
protected:
|
2013-08-01 15:53:52 +08:00
|
|
|
/**
|
|
|
|
@brief Determine how many mipmaps can we have.
|
2015-09-22 16:08:23 +08:00
|
|
|
It's same as define but it respects namespaces
|
2013-08-01 15:53:52 +08:00
|
|
|
*/
|
|
|
|
static const int MIPMAP_MAX = 16;
|
2016-05-11 16:35:36 +08:00
|
|
|
/**
|
|
|
|
@brief Determine whether we premultiply alpha for png files.
|
|
|
|
*/
|
|
|
|
static bool PNG_PREMULTIPLIED_ALPHA_ENABLED;
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned char *_data;
|
2013-12-05 17:19:01 +08:00
|
|
|
ssize_t _dataLen;
|
2013-07-25 21:35:00 +08:00
|
|
|
int _width;
|
|
|
|
int _height;
|
2014-03-20 17:07:28 +08:00
|
|
|
bool _unpack;
|
2013-07-27 22:06:30 +08:00
|
|
|
Format _fileType;
|
|
|
|
Texture2D::PixelFormat _renderFormat;
|
2013-08-01 15:53:52 +08:00
|
|
|
MipmapInfo _mipmaps[MIPMAP_MAX]; // pointer to mipmap images
|
2013-07-25 21:35:00 +08:00
|
|
|
int _numberOfMipmaps;
|
2015-09-22 16:08:23 +08:00
|
|
|
// false if we can't auto detect the image is premultiplied or not.
|
2013-07-30 11:10:05 +08:00
|
|
|
bool _hasPremultipliedAlpha;
|
2013-11-29 14:31:42 +08:00
|
|
|
std::string _filePath;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-05-01 07:36:14 +08:00
|
|
|
|
2014-03-25 11:13:44 +08:00
|
|
|
protected:
|
2012-04-19 14:35:52 +08:00
|
|
|
// noncopyable
|
2016-05-11 16:35:36 +08:00
|
|
|
Image(const Image& rImg);
|
|
|
|
Image& operator=(const Image&);
|
2013-06-26 16:42:11 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
@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.
|
|
|
|
*/
|
2013-12-24 15:49:58 +08:00
|
|
|
bool initWithImageFileThreadSafe(const std::string& fullpath);
|
2013-08-08 14:11:22 +08:00
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
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);
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of platform group
|
|
|
|
/// @}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
2015-03-24 20:23:51 +08:00
|
|
|
/// @endcond
|
2012-04-19 14:35:52 +08:00
|
|
|
#endif // __CC_IMAGE_H__
|