fix some issues

This commit is contained in:
dualface 2012-12-29 13:58:02 +08:00
parent b50e4cdf2b
commit 806138d990
5 changed files with 66 additions and 45 deletions

View File

@ -96,12 +96,6 @@ public:
int nHeight = 0,
int nBitsPerComponent = 8);
/**
@brief Load image from webp data.
*/
bool initWithWebpData(void * pData, int nDataLen);
/**
@brief Create image with specified string.
@param pText the text the image will show (cannot be nil).
@ -139,7 +133,8 @@ public:
protected:
bool _initWithJpgData(void *pData, int nDatalen);
bool _initWithPngData(void *pData, int nDatalen);
bool _initWithTiffData(void* pData, int nDataLen);
bool _initWithTiffData(void *pData, int nDataLen);
bool _initWithWebpData(void *pData, int nDataLen);
// @warning kFmtRawData only support RGBA8888
bool _initWithRawData(void *pData, int nDatalen, int nWidth, int nHeight, int nBitsPerComponent);

View File

@ -30,27 +30,27 @@
NS_CC_BEGIN
bool CCImage::initWithWebpData(void * pData, int nDataLen)
bool CCImage::_initWithWebpData(void *pData, int nDataLen)
{
bool bRet = false;
do
{
int width = 0;
int height = 0;
//WebPGetInfo((uint8_t*)pData, nDataLen, &width, &height);
uint8_t* data = WebPDecodeRGBA((uint8_t*)pData, nDataLen, &width, &height);
if(data) {
if(0 == width || 0 == height) {
free(data);
break;
}
m_nBitsPerComponent = 8;
m_nHeight = (short)height;
m_nWidth = (short)width;
m_bHasAlpha = true;
m_pData = (unsigned char*)data;
bRet = true;
}
int height = 0;
uint8_t *data = WebPDecodeRGBA((uint8_t*)pData, nDataLen, &width, &height);
if(!data) break;
if(width == 0 || height == 0)
{
free(data);
break;
}
m_nBitsPerComponent = 8;
m_nHeight = (short)height;
m_nWidth = (short)width;
m_bHasAlpha = true;
m_pData = (unsigned char*)data;
bRet = true;
} while (0);
return bRet;
}

View File

@ -87,7 +87,11 @@ CCImage::CCImage()
CCImage::~CCImage()
{
CC_SAFE_DELETE_ARRAY(m_pData);
// CC_SAFE_DELETE_ARRAY(m_pData);
if (m_pData)
{
free(m_pData);
}
}
bool CCImage::initWithImageFile(const char * strPath, EImageFormat eImgFmt/* = eFmtPng*/)
@ -143,6 +147,11 @@ bool CCImage::initWithImageData(void * pData,
bRet = _initWithTiffData(pData, nDataLen);
break;
}
else if (kFmtWebp == eFmt)
{
bRet = _initWithWebpData(pData, nDataLen);
break;
}
else if (kFmtRawData == eFmt)
{
bRet = _initWithRawData(pData, nDataLen, nWidth, nHeight, nBitsPerComponent);
@ -309,7 +318,8 @@ bool CCImage::_initWithJpgData(void * data, int nSize)
row_pointer[0] = new unsigned char[cinfo.output_width*cinfo.output_components];
CC_BREAK_IF(! row_pointer[0]);
m_pData = new unsigned char[cinfo.output_width*cinfo.output_height*cinfo.output_components];
// m_pData = new unsigned char[cinfo.output_width*cinfo.output_height*cinfo.output_components];
m_pData = (unsigned char*)malloc(cinfo.output_width*cinfo.output_height*cinfo.output_components);
CC_BREAK_IF(! m_pData);
/* now actually read the jpeg into the raw buffer */
@ -424,7 +434,8 @@ bool CCImage::_initWithPngData(void * pData, int nDatalen)
rowbytes = png_get_rowbytes(png_ptr, info_ptr);
m_pData = new unsigned char[rowbytes * m_nHeight];
//m_pData = new unsigned char[rowbytes * m_nHeight];
m_pData = (unsigned char*)malloc(rowbytes * m_nHeight);
CC_BREAK_IF(!m_pData);
for (unsigned short i = 0; i < m_nHeight; ++i)
@ -607,7 +618,8 @@ bool CCImage::_initWithTiffData(void* pData, int nDataLen)
m_nHeight = h;
m_nBitsPerComponent = 8;
m_pData = new unsigned char[npixels * sizeof (uint32)];
//m_pData = new unsigned char[npixels * sizeof (uint32)];
m_pData = (unsigned char*)malloc(npixels * sizeof (uint32));
uint32* raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32));
if (raster != NULL)
@ -656,7 +668,8 @@ bool CCImage::_initWithRawData(void * pData, int nDatalen, int nWidth, int nHeig
// only RGBA8888 supported
int nBytesPerComponent = 4;
int nSize = nHeight * nWidth * nBytesPerComponent;
m_pData = new unsigned char[nSize];
//m_pData = new unsigned char[nSize];
m_pData = (unsigned char*)malloc(nSize);
CC_BREAK_IF(! m_pData);
memcpy(m_pData, pData, nSize);

View File

@ -78,7 +78,8 @@ static bool _initWithImage(CGImageRef cgImage, tImageInfo *pImageinfo)
// change to RGBA8888
pImageinfo->hasAlpha = true;
pImageinfo->bitsPerComponent = 8;
pImageinfo->data = new unsigned char[pImageinfo->width * pImageinfo->height * 4];
//pImageinfo->data = new unsigned char[pImageinfo->width * pImageinfo->height * 4];
pImageinfo->data = (unsigned char*)malloc(pImageinfo->width * pImageinfo->height * 4);
colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pImageinfo->data,
pImageinfo->width,
@ -264,9 +265,16 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl
if (constrainSize.height > 0 && constrainSize.height > dim.height)
{
dim.height = constrainSize.height;
}
}
unsigned char* data = new unsigned char[(int)(dim.width * dim.height * 4)];
dim.width = (int)(dim.width / 2) * 2 + 2;
dim.height = (int)(dim.height / 2) * 2 + 2;
dim.width = (int)(dim.width / 2) * 2 + 2;
dim.height = (int)(dim.height / 2) * 2 + 2;
//unsigned char* data = new unsigned char[(int)(dim.width * dim.height * 4)];
unsigned char* data = (unsigned char*)malloc((int)(dim.width * dim.height * 4));
memset(data, 0, (int)(dim.width * dim.height * 4));
// draw text
@ -276,7 +284,8 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl
if (! context)
{
delete[] data;
//delete[] data;
free(data);
break;
}
@ -336,7 +345,11 @@ CCImage::CCImage()
CCImage::~CCImage()
{
CC_SAFE_DELETE_ARRAY(m_pData);
//CC_SAFE_DELETE_ARRAY(m_pData);
if (m_pData)
{
free(m_pData);
}
}
bool CCImage::initWithImageFile(const char * strPath, EImageFormat eImgFmt/* = eFmtPng*/)
@ -388,6 +401,10 @@ bool CCImage::initWithImageData(void * pData,
{
bRet = _initWithRawData(pData, nDataLen, nWidth, nHeight, nBitsPerComponent);
}
else if (eFmt == kFmtWebp)
{
bRet = _initWithWebpData(pData, nDataLen);
}
else // init with png or jpg file data
{
bRet = _initWithData(pData, nDataLen, &info);
@ -421,7 +438,8 @@ bool CCImage::_initWithRawData(void *pData, int nDatalen, int nWidth, int nHeigh
// only RGBA8888 supported
int nBytesPerComponent = 4;
int nSize = nHeight * nWidth * nBytesPerComponent;
m_pData = new unsigned char[nSize];
//m_pData = new unsigned char[nSize];
m_pData = (unsigned char*)malloc(nSize);
CC_BREAK_IF(! m_pData);
memcpy(m_pData, pData, nSize);

View File

@ -105,7 +105,11 @@ static CCImage::EImageFormat computeImageFormatType(string& filename)
{
ret = CCImage::kFmtTiff;
}
else if ((std::string::npos != filename.find(".webp")) || (std::string::npos != filename.find(".WEBP")))
{
ret = CCImage::kFmtWebp;
}
return ret;
}
@ -445,16 +449,7 @@ CCTexture2D * CCTextureCache::addImage(const char * path)
unsigned long nSize = 0;
unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData(fullpath.c_str(), "rb", &nSize);
bool bRet = false;
if (eImageFormat == CCImage::kFmtWebp)
{
bRet = pImage->initWithWebpData((void*)pBuffer, nSize);
}
else
{
bRet = pImage->initWithImageData((void*)pBuffer, nSize, eImageFormat);
}
bool bRet = pImage->initWithImageData((void*)pBuffer, nSize, eImageFormat);
CC_SAFE_DELETE_ARRAY(pBuffer);
CC_BREAK_IF(!bRet);