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"
|
2013-07-25 21:35:00 +08:00
|
|
|
#include "textures/CCTexture2D.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-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;
|
|
|
|
}MipmapInfo;
|
2012-06-20 18:09:11 +08:00
|
|
|
|
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,
|
2013-07-27 22:06:30 +08:00
|
|
|
//! PVR
|
|
|
|
PVR,
|
|
|
|
//! ETC
|
|
|
|
ETC,
|
2013-08-06 11:19:45 +08:00
|
|
|
//! S3TC
|
|
|
|
S3TC,
|
2013-07-26 05:49:43 +08:00
|
|
|
//! Raw Data
|
|
|
|
RAW_DATA,
|
|
|
|
//! Unknown format
|
|
|
|
UNKOWN
|
2013-07-25 20:29:53 +08:00
|
|
|
};
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 15:22:45 +08:00
|
|
|
enum class TextAlign
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 15:22:45 +08:00
|
|
|
CENTER = 0x33, ///< Horizontal center and vertical center.
|
|
|
|
TOP = 0x13, ///< Horizontal center and vertical top.
|
|
|
|
TOP_RIGHT = 0x12, ///< Horizontal right and vertical top.
|
|
|
|
RIGHT = 0x32, ///< Horizontal right and vertical center.
|
2013-07-26 18:07:40 +08:00
|
|
|
BOTTOM_RIGHT = 0x22, ///< Horizontal right and vertical bottom.
|
2013-07-26 15:22:45 +08:00
|
|
|
BOTTOM = 0x23, ///< Horizontal center and vertical bottom.
|
2013-07-26 18:07:40 +08:00
|
|
|
BOTTOM_LEFT = 0x21, ///< Horizontal left and vertical bottom.
|
2013-07-26 15:22:45 +08:00
|
|
|
LEFT = 0x31, ///< Horizontal left and vertical center.
|
|
|
|
TOP_LEFT = 0x11, ///< Horizontal left and vertical top.
|
|
|
|
};
|
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-08-01 21:40:13 +08:00
|
|
|
bool initWithImageFile(const char *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.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-08-06 16:14:36 +08:00
|
|
|
bool initWithImageData(const void * data, int dataLen);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-13 05:46:32 +08:00
|
|
|
// @warning kFmtRawData only support RGBA8888
|
2013-08-06 16:14:36 +08:00
|
|
|
bool initWithRawData(const void * data, int dataLen, int width, int height, int bitsPerComponent, bool preMulti = false);
|
2013-06-13 05:46:32 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2013-08-01 21:40:13 +08:00
|
|
|
@brief Create image with specified string.
|
|
|
|
@param text the text the image will show (cannot be nil).
|
|
|
|
@param width the image width, if 0, the width will match the text's width.
|
|
|
|
@param height the image height, if 0, the height will match the text's height.
|
|
|
|
@param alignMask the test Alignment
|
|
|
|
@param fontName the name of the font used to draw the text. If nil, use the default system font.
|
|
|
|
@param size the font size, if 0, use the system default size.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
|
|
|
bool initWithString(
|
2013-08-01 21:40:13 +08:00
|
|
|
const char * text,
|
|
|
|
int width = 0,
|
|
|
|
int height = 0,
|
|
|
|
TextAlign alignMask = TextAlign::CENTER,
|
|
|
|
const char * fontName = 0,
|
|
|
|
int size = 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,
|
2013-07-29 11:40:18 +08:00
|
|
|
TextAlign eAlignMask = TextAlign::CENTER,
|
2013-05-02 08:11:53 +08:00
|
|
|
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
|
|
|
|
2013-07-27 22:06:30 +08:00
|
|
|
|
|
|
|
// Getters
|
|
|
|
inline unsigned char * getData() { return _data; }
|
|
|
|
inline int getDataLen() { return _dataLen; }
|
|
|
|
inline Format getFileType() {return _fileType; }
|
|
|
|
inline Texture2D::PixelFormat getRenderFormat() { return _renderFormat; }
|
|
|
|
inline int getWidth() { return _width; }
|
|
|
|
inline int getHeight() { return _height; }
|
|
|
|
inline bool isPremultipliedAlpha() { return _preMulti; }
|
|
|
|
inline int getNumberOfMipmaps() { return _numberOfMipmaps; }
|
2013-07-30 11:10:05 +08:00
|
|
|
inline MipmapInfo* getMipmaps() { return _mipmaps; }
|
|
|
|
inline bool hasPremultipliedAlpha() { return _hasPremultipliedAlpha; }
|
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-08-01 21:40:13 +08:00
|
|
|
bool saveToFile(const char *filePath, bool isToRGB = true);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
protected:
|
2013-08-06 11:19:45 +08:00
|
|
|
|
2013-08-06 16:14:36 +08:00
|
|
|
bool initWithJpgData(const void *data, int dataLen);
|
|
|
|
bool initWithPngData(const void *data, int dataLen);
|
|
|
|
bool initWithTiffData(const void *data, int dataLen);
|
|
|
|
bool initWithWebpData(const void *data, int dataLen);
|
|
|
|
bool initWithPVRData(const void *data, int dataLen);
|
|
|
|
bool initWithPVRv2Data(const void *data, int dataLen);
|
|
|
|
bool initWithPVRv3Data(const void *data, int dataLen);
|
|
|
|
bool initWithETCData(const void *data, int dataLen);
|
2013-08-08 14:11:22 +08:00
|
|
|
bool initWithS3TCData(const void *data, int dataLen);
|
2013-08-06 16:14:36 +08:00
|
|
|
|
|
|
|
bool saveImageToPNG(const char *filePath, bool isToRGB = true);
|
|
|
|
bool saveImageToJPG(const char *filePath);
|
2013-08-01 15:53:52 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
@brief Determine how many mipmaps can we have.
|
|
|
|
Its same as define but it respects namespaces
|
|
|
|
*/
|
|
|
|
static const int MIPMAP_MAX = 16;
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned char *_data;
|
2013-07-25 21:35:00 +08:00
|
|
|
int _dataLen;
|
|
|
|
int _width;
|
|
|
|
int _height;
|
2013-07-27 22:06:30 +08:00
|
|
|
Format _fileType;
|
|
|
|
Texture2D::PixelFormat _renderFormat;
|
2013-06-15 14:03:30 +08:00
|
|
|
bool _preMulti;
|
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;
|
2013-07-30 11:10:05 +08:00
|
|
|
// false if we cann't auto detect the image is premultiplied or not.
|
|
|
|
bool _hasPremultipliedAlpha;
|
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-17 17:12:04 +08:00
|
|
|
bool initWithImageFileThreadSafe(const char *fullpath);
|
2013-08-08 14:11:22 +08:00
|
|
|
|
2013-08-06 16:14:36 +08:00
|
|
|
Format detectFormat(const void* data, int dataLen);
|
|
|
|
bool isPng(const void *data, int dataLen);
|
|
|
|
bool isJpg(const void *data, int dataLen);
|
|
|
|
bool isTiff(const void *data, int dataLen);
|
|
|
|
bool isWebp(const void *data, int dataLen);
|
|
|
|
bool isPvr(const void *data, int dataLen);
|
|
|
|
bool isEtc(const void *data, int dataLen);
|
2013-08-08 14:11:22 +08:00
|
|
|
bool isS3TC(const void *data,int 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
|
|
|
|
|
|
|
|
#endif // __CC_IMAGE_H__
|