axmol/cocos2dx/platform/CCImage.h

250 lines
9.3 KiB
C
Raw Normal View History

/****************************************************************************
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__
#include "cocoa/CCObject.h"
#include "textures/CCTexture2D.h"
// 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
2012-06-20 18:09:11 +08:00
/**
* @addtogroup platform
* @{
*/
/**
@brief Structure which can tell where mipmap begins and how long is it
*/
typedef struct _MipmapInfo {
unsigned char* address;
int len;
}MipmapInfo;
2012-06-20 18:09:11 +08:00
class CC_DLL Image : public Object
{
public:
2013-06-26 16:42:11 +08:00
friend class TextureCache;
Image();
virtual ~Image();
/**
@brief Determine how many mipmaps can we have.
Its same as define but it respects namespaces
*/
enum {
CC_MIPMAP_MAX = 16,
};
/** Supported formats for Image */
enum class Format
{
//! JPEG
JPG,
//! PNG
PNG,
//! TIFF
TIFF,
//! WebP
WEBP,
//! PVR
PVR,
//! ETC
ETC,
//! Raw Data
RAW_DATA,
//! Unknown format
UNKOWN
};
2013-07-26 15:22:45 +08:00
enum class TextAlign
{
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
/**
@brief Load the image from the specified path.
@param strPath the absolute file path.
@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 initWithImageFile(const char * strPath);
/**
@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.
*/
bool initWithImageData(void * data, int dataLen);
// @warning kFmtRawData only support RGBA8888
bool initWithRawData(void *data, int dataLen, int nWidth, int nHeight, int nBitsPerComponent = 8, bool bPreMulti = false);
/**
@brief Create image with specified string.
@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.
@param eAlignMask the test Alignment
@param pFontName the name of the font used to draw the text. If nil, use the default system font.
@param nSize the font size, if 0, use the system default size.
*/
bool initWithString(
const char * pText,
int nWidth = 0,
int nHeight = 0,
2013-07-26 15:22:45 +08:00
TextAlign eAlignMask = TextAlign::CENTER,
const char * pFontName = 0,
int nSize = 0);
2013-04-26 09:22:26 +08:00
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
2013-04-26 09:22:26 +08:00
bool initWithStringShadowStroke(
const char * pText,
int nWidth = 0,
int nHeight = 0,
TextAlign eAlignMask = TextAlign::CENTER,
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
// 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; }
inline MipmapInfo* getMipmaps() { return _mipmaps; }
int getBitPerPixel();
bool hasAlpha();
bool isCompressed();
/**
@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.
*/
bool saveToFile(const char *pszFilePath, bool bIsToRGB = true);
protected:
2013-07-29 14:04:51 +08:00
bool initWithJpgData(void *data, int dataLen);
bool initWithPngData(void *data, int dataLen);
bool initWithTiffData(void *data, int dataLen);
bool initWithWebpData(void *data, int dataLen);
bool initWithPVRData(void *data, int dataLen);
bool initWithPVRv2Data(void *data, int dataLen);
bool initWithPVRv3Data(void *data, int dataLen);
bool initWithETCData(void *data, int dataLen);
bool saveImageToPNG(const char *pszFilePath, bool bIsToRGB = true);
bool saveImageToJPG(const char *pszFilePath);
2013-07-19 15:37:54 +08:00
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
2013-07-29 14:04:51 +08:00
bool iosSaveToFile(const char *pszFilePath, bool bIsToRGB = true);
2013-07-19 15:37:54 +08:00
#endif
unsigned char *_data;
int _dataLen;
int _width;
int _height;
Format _fileType;
Texture2D::PixelFormat _renderFormat;
bool _preMulti;
MipmapInfo _mipmaps[CC_MIPMAP_MAX]; // pointer to mipmap images
int _numberOfMipmaps;
private:
// noncopyable
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);
Format detectFormat(void* data, int dataLen);
bool isPng(void *data, int dataLen);
bool isJpg(void *data, int dataLen);
bool isTiff(void *data, int dataLen);
bool isWebp(void *data, int dataLen);
bool isPvr(void *data, int dataLen);
bool isEtc(void *data, int dataLen);
2013-07-29 14:04:51 +08:00
bool testFormatForPvrTCSupport(int format);
};
2012-06-20 18:09:11 +08:00
// end of platform group
/// @}
NS_CC_END
#endif // __CC_IMAGE_H__