2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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__
|
|
|
|
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "cocoa/CCObject.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
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-06-20 14:13:12 +08:00
|
|
|
class CC_DLL Image : public Object
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-26 16:42:11 +08:00
|
|
|
friend class TextureCache;
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Image();
|
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,
|
|
|
|
//! Raw Data
|
|
|
|
RAW_DATA,
|
|
|
|
//! Unknown format
|
|
|
|
UNKOWN
|
2013-07-25 20:29:53 +08:00
|
|
|
};
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
kAlignCenter = 0x33, ///< Horizontal center and vertical center.
|
|
|
|
kAlignTop = 0x13, ///< Horizontal center and vertical top.
|
|
|
|
kAlignTopRight = 0x12, ///< Horizontal right and vertical top.
|
|
|
|
kAlignRight = 0x32, ///< Horizontal right and vertical center.
|
|
|
|
kAlignBottomRight = 0x22, ///< Horizontal right and vertical bottom.
|
|
|
|
kAlignBottom = 0x23, ///< Horizontal center and vertical bottom.
|
|
|
|
kAlignBottomLeft = 0x21, ///< Horizontal left and vertical bottom.
|
|
|
|
kAlignLeft = 0x31, ///< Horizontal left and vertical center.
|
|
|
|
kAlignTopLeft = 0x11, ///< Horizontal left and vertical top.
|
|
|
|
}ETextAlign;
|
2012-12-29 10:27:43 +08:00
|
|
|
|
|
|
|
/**
|
2012-04-19 14:35:52 +08:00
|
|
|
@brief Load the image from the specified path.
|
2012-09-15 06:26:38 +08:00
|
|
|
@param strPath the absolute file path.
|
|
|
|
@param imageType the type of image, currently only supporting two types.
|
|
|
|
@return true if loaded correctly.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-07-26 05:49:43 +08:00
|
|
|
bool initWithImageFile(const char * strPath, Format imageType = Format::PNG);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Load image from stream buffer.
|
|
|
|
|
2012-09-15 06:26:38 +08:00
|
|
|
@warning kFmtRawData only supports RGBA8888.
|
|
|
|
@param pBuffer stream buffer which holds the image data.
|
|
|
|
@param nLength data length expressed in (number of) bytes.
|
|
|
|
@param nWidth, nHeight, nBitsPerComponent are used for kFmtRawData.
|
|
|
|
@return true if loaded correctly.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
|
|
|
bool initWithImageData(void * pData,
|
|
|
|
int nDataLen,
|
2013-07-26 05:49:43 +08:00
|
|
|
Format eFmt = Format::UNKOWN,
|
2012-04-19 14:35:52 +08:00
|
|
|
int nWidth = 0,
|
|
|
|
int nHeight = 0,
|
|
|
|
int nBitsPerComponent = 8);
|
|
|
|
|
2013-06-13 05:46:32 +08:00
|
|
|
// @warning kFmtRawData only support RGBA8888
|
|
|
|
bool initWithRawData(void *pData, int nDatalen, int nWidth, int nHeight, int nBitsPerComponent, bool bPreMulti);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
|
|
|
@brief Create image with specified string.
|
2012-09-15 06:26:38 +08:00
|
|
|
@param pText the text the image will show (cannot be nil).
|
|
|
|
@param nWidth the image width, if 0, the width will match the text's width.
|
|
|
|
@param nHeight the image height, if 0, the height will match the text's height.
|
2012-04-19 14:35:52 +08:00
|
|
|
@param eAlignMask the test Alignment
|
2012-09-15 06:26:38 +08:00
|
|
|
@param pFontName the name of the font used to draw the text. If nil, use the default system font.
|
2012-04-19 14:35:52 +08:00
|
|
|
@param nSize the font size, if 0, use the system default size.
|
|
|
|
*/
|
|
|
|
bool initWithString(
|
|
|
|
const char * pText,
|
|
|
|
int nWidth = 0,
|
|
|
|
int nHeight = 0,
|
|
|
|
ETextAlign eAlignMask = kAlignCenter,
|
|
|
|
const char * pFontName = 0,
|
|
|
|
int nSize = 0);
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
bool initWithStringShadowStroke(
|
|
|
|
const char * pText,
|
|
|
|
int nWidth = 0,
|
|
|
|
int nHeight = 0,
|
|
|
|
ETextAlign eAlignMask = kAlignCenter,
|
|
|
|
const char * pFontName = 0,
|
|
|
|
int nSize = 0,
|
|
|
|
float textTintR = 1,
|
|
|
|
float textTintG = 1,
|
|
|
|
float textTintB = 1,
|
|
|
|
bool shadow = false,
|
|
|
|
float shadowOffsetX = 0.0,
|
|
|
|
float shadowOffsetY = 0.0,
|
|
|
|
float shadowOpacity = 0.0,
|
|
|
|
float shadowBlur = 0.0,
|
|
|
|
bool stroke = false,
|
|
|
|
float strokeR = 1,
|
|
|
|
float strokeG = 1,
|
|
|
|
float strokeB = 1,
|
|
|
|
float strokeSize = 1
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
#endif
|
2013-04-26 09:22:26 +08:00
|
|
|
|
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.
|
|
|
|
@param pszFilePath the file's absolute path, including file suffix.
|
|
|
|
@param bIsToRGB whether the image is saved as RGB format.
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
bool saveToFile(const char *pszFilePath, bool bIsToRGB = true);
|
2013-07-23 18:26:26 +08:00
|
|
|
|
|
|
|
// Getters
|
|
|
|
inline unsigned char * getData() { return _data; }
|
|
|
|
inline int getDataLen() { return _width * _height; }
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
inline bool hasAlpha() { return _hasAlpha; }
|
|
|
|
inline bool isPremultipliedAlpha() { return _preMulti; }
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
inline unsigned short getWidth() { return _width; };
|
|
|
|
inline unsigned short getHeight() { return _height; };
|
|
|
|
inline int getBitsPerComponent() { return _bitsPerComponent; };
|
|
|
|
//
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
protected:
|
2013-07-26 05:49:43 +08:00
|
|
|
bool initWithJpgData(void *pData, int nDatalen);
|
|
|
|
bool initWithPngData(void *pData, int nDatalen);
|
|
|
|
bool initWithTiffData(void *pData, int nDataLen);
|
|
|
|
bool initWithWebpData(void *pData, int nDataLen);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 05:49:43 +08:00
|
|
|
bool saveImageToPNG(const char *pszFilePath, bool bIsToRGB = true);
|
|
|
|
bool saveImageToJPG(const char *pszFilePath);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
unsigned short _width;
|
|
|
|
unsigned short _height;
|
|
|
|
int _bitsPerComponent;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned char *_data;
|
|
|
|
bool _hasAlpha;
|
|
|
|
bool _preMulti;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-05-01 07:36:14 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
private:
|
|
|
|
// noncopyable
|
2013-06-20 14:13:12 +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-07-26 05:49:43 +08:00
|
|
|
bool initWithImageFileThreadSafe(const char *fullpath, Format imageType = Format::PNG);
|
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
|
|
|
|
|
|
|
|
#endif // __CC_IMAGE_H__
|