2011-01-15 18:05:35 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-03-22 16:36:03 +08:00
|
|
|
#ifndef __CC_IMAGE_H__
|
2012-03-27 13:48:14 +08:00
|
|
|
#define __CC_IMAGE_H__
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCCommon.h"
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2011-02-19 18:16:45 +08:00
|
|
|
NS_CC_BEGIN;
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2011-03-09 14:40:05 +08:00
|
|
|
class CC_DLL CCImage
|
2011-01-15 18:05:35 +08:00
|
|
|
{
|
|
|
|
public:
|
2011-03-07 17:11:57 +08:00
|
|
|
CCImage();
|
2011-03-28 10:44:14 +08:00
|
|
|
~CCImage();
|
2011-01-15 18:05:35 +08:00
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
2011-01-19 14:23:26 +08:00
|
|
|
kFmtJpg = 0,
|
|
|
|
kFmtPng,
|
2011-07-27 16:55:04 +08:00
|
|
|
kFmtRawData,
|
2011-12-22 15:38:31 +08:00
|
|
|
kFmtUnKnown
|
2011-01-15 18:05:35 +08:00
|
|
|
}EImageFormat;
|
|
|
|
|
2011-01-19 14:23:26 +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;
|
|
|
|
|
2011-01-15 18:05:35 +08:00
|
|
|
/**
|
2011-01-19 14:23:26 +08:00
|
|
|
@brief Load the image from the specified path.
|
2011-01-15 18:05:35 +08:00
|
|
|
@param strPath the absolute file path
|
|
|
|
@param imageType the type of image, now only support tow types.
|
|
|
|
@return true if load correctly
|
|
|
|
*/
|
2011-01-19 14:23:26 +08:00
|
|
|
bool initWithImageFile(const char * strPath, EImageFormat imageType = kFmtPng);
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2011-11-22 16:47:24 +08:00
|
|
|
/*
|
|
|
|
@brief The same meaning as initWithImageFile, but it is thread safe. It is casued by
|
|
|
|
loadImage() in CCTextureCache.cpp.
|
|
|
|
@param fullpath full path of the file
|
|
|
|
@param imageType the type of image, now only support tow types.
|
|
|
|
@return true if load correctly
|
|
|
|
*/
|
|
|
|
bool initWithImageFileThreadSafe(const char *fullpath, EImageFormat imageType = kFmtPng);
|
|
|
|
|
2011-01-15 18:05:35 +08:00
|
|
|
/**
|
2011-01-19 14:23:26 +08:00
|
|
|
@brief Load image from stream buffer.
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2011-07-27 16:55:04 +08:00
|
|
|
@warning kFmtRawData only support RGBA8888
|
2011-01-15 18:05:35 +08:00
|
|
|
@param pBuffer stream buffer that hold the image data
|
|
|
|
@param nLength the length of data(managed in byte)
|
2011-07-27 16:55:04 +08:00
|
|
|
@param nWidth, nHeight, nBitsPerComponent are used for kFmtRawData
|
2011-01-15 18:05:35 +08:00
|
|
|
@return true if load correctly
|
|
|
|
*/
|
2011-07-27 16:55:04 +08:00
|
|
|
bool initWithImageData(void * pData,
|
|
|
|
int nDataLen,
|
|
|
|
EImageFormat eFmt = kFmtPng,
|
|
|
|
int nWidth = 0,
|
|
|
|
int nHeight = 0,
|
|
|
|
int nBitsPerComponent = 8);
|
2011-01-19 14:23:26 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Create image with specified string.
|
|
|
|
@param pText the text which the image show, nil cause init fail
|
|
|
|
@param nWidth the image width, if 0, the width match the text's width
|
|
|
|
@param nHeight the image height, if 0, the height match the text's height
|
|
|
|
@param eAlignMask the test Alignment
|
|
|
|
@param pFontName the name of the font which use to draw the text. If nil, use the default system font.
|
|
|
|
@param nSize the font size, if 0, use the system default size.
|
|
|
|
*/
|
2011-02-17 14:31:52 +08:00
|
|
|
bool initWithString(
|
2011-01-19 14:23:26 +08:00
|
|
|
const char * pText,
|
|
|
|
int nWidth = 0,
|
|
|
|
int nHeight = 0,
|
|
|
|
ETextAlign eAlignMask = kAlignCenter,
|
2011-02-23 18:22:05 +08:00
|
|
|
const char * pFontName = 0,
|
2011-01-19 14:23:26 +08:00
|
|
|
int nSize = 0);
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2011-03-28 10:44:14 +08:00
|
|
|
unsigned char * getData() { return m_pData; }
|
2011-01-15 18:05:35 +08:00
|
|
|
int getDataLen() { return m_nWidth * m_nHeight; }
|
|
|
|
|
|
|
|
bool hasAlpha() { return m_bHasAlpha; }
|
|
|
|
bool isPremultipliedAlpha() { return m_bPreMulti; }
|
|
|
|
|
2011-01-19 14:23:26 +08:00
|
|
|
void release();
|
|
|
|
|
2011-01-15 18:05:35 +08:00
|
|
|
/**
|
2011-03-07 17:11:57 +08:00
|
|
|
@brief Save the CCImage data to specified file with specified format.
|
2011-07-27 16:55:04 +08:00
|
|
|
@param pszFilePath the file's absolute path, including file subfix
|
|
|
|
@param bIsToRGB if the image is saved as RGB format
|
2011-01-15 18:05:35 +08:00
|
|
|
*/
|
2011-07-27 16:55:04 +08:00
|
|
|
bool saveToFile(const char *pszFilePath, bool bIsToRGB = true);
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2011-03-28 10:44:14 +08:00
|
|
|
CC_SYNTHESIZE_READONLY(short, m_nWidth, Width);
|
|
|
|
CC_SYNTHESIZE_READONLY(short, m_nHeight, Height);
|
2011-07-27 16:55:04 +08:00
|
|
|
CC_SYNTHESIZE_READONLY(int, m_nBitsPerComponent, BitsPerComponent);
|
2011-01-15 18:05:35 +08:00
|
|
|
|
|
|
|
protected:
|
2011-07-27 16:55:04 +08:00
|
|
|
bool _initWithJpgData(void *pData, int nDatalen);
|
|
|
|
bool _initWithPngData(void *pData, int nDatalen);
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2011-07-27 16:55:04 +08:00
|
|
|
// @warning kFmtRawData only support RGBA8888
|
|
|
|
bool _initWithRawData(void *pData, int nDatalen, int nWidth, int nHeight, int nBitsPerComponent);
|
|
|
|
|
|
|
|
bool _saveImageToPNG(const char *pszFilePath, bool bIsToRGB = true);
|
|
|
|
bool _saveImageToJPG(const char *pszFilePath);
|
|
|
|
|
|
|
|
unsigned char *m_pData;
|
2011-01-15 18:05:35 +08:00
|
|
|
bool m_bHasAlpha;
|
|
|
|
bool m_bPreMulti;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// noncopyable
|
2011-03-07 17:11:57 +08:00
|
|
|
CCImage(const CCImage& rImg);
|
|
|
|
CCImage & operator=(const CCImage&);
|
2011-01-15 18:05:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END;
|
|
|
|
|
2012-03-22 16:36:03 +08:00
|
|
|
#endif // __CC_IMAGE_H__
|