CCImage在"CCImage.h"中定义,表示一张加载到内存的纹理图片。在其内部的实现中,纹理以每个像素的颜色值保存在内存之中。CCImage通常作为文件和显卡间数据交换的一个工具,因此主要提供了两个方面的功能:一方面是文件的加载与保存,另一方面是内存缓冲区的读写。
我们可以使用CCImage轻松地读写图片文件。目前,CCImage支持PNG、JPEG和TIFF三种主流的图片格式。下面列举与文件读写相关的方法:
/**
@brief Load the image from the specified path.
@param strPath the absolute file path
@param imageType the type of image, now only support tow types.
@return true if load correctly
*/
bool initWithImageFile(const char * strPath, EImageFormat imageType = kFmtPng);
/*
@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);
/**
@brief Save the CCImage data to specified file with specified format.
@param pszFilePath the file's absolute path, including file subfix
@param bIsToRGB if the image is saved as RGB format
*/
bool saveToFile(const char *pszFilePath, bool bIsToRGB = true);
CCImage也提供了读写内存的接口。getData和getDataLen这两个方法提供了获取当前纹理的缓冲区的功能,而initWithImageData方法提供了使用像素数据初始化图片的功能。相关的方法定义如下:
unsigned char * getData() { return m_pData; }
int getDataLen() { return m_nWidth * m_nHeight; }
/**
@brief Load image from stream buffer.
@warning kFmtRawData only support RGBA8888
@param pBuffer stream buffer that hold the image data
@param nLength the length of data(managed in byte)
@param nWidth, nHeight, nBitsPerComponent are used for kFmtRawData
@return true if load correctly
*/
bool initWithImageData(void * pData,
int nDataLen,
EImageFormat eFmt = kFmtUnKnown,
int nWidth = 0,
int nHeight = 0,
int nBitsPerComponent = 8);