axmol/cocos2dx/platform/uphone/CCXUIImage_uphone.cpp

345 lines
8.1 KiB
C++
Raw Normal View History

2010-07-23 14:36:16 +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.
****************************************************************************/
2010-08-25 17:21:38 +08:00
#include "CCXUIImage_uphone.h"
2010-07-23 14:36:16 +08:00
2010-08-12 14:46:58 +08:00
//#include <ImageToolKit/IT_ImageLoader.h>
2010-07-23 15:13:47 +08:00
#include <TG3.h>
2010-08-12 14:46:58 +08:00
#include "png.h"
2010-07-23 15:13:47 +08:00
2010-08-12 14:46:58 +08:00
//using namespace ImageToolKit;
2010-07-23 14:36:16 +08:00
using namespace std;
2010-08-04 15:46:12 +08:00
namespace cocos2d {
2010-07-23 14:36:16 +08:00
2010-08-26 12:04:22 +08:00
typedef struct
{
unsigned char* data;
int size;
int offset;
}tImageSource;
// because we do not want to include "png.h" in CCXUIImage_uphone.h, so we implement
// the function as a static function
static void pngReadCallback(png_structp png_ptr, png_bytep data, png_size_t length)
{
tImageSource* isource = (tImageSource*)png_get_io_ptr(png_ptr);
if((int)(isource->offset + length) <= isource->size)
{
memcpy(data, isource->data+isource->offset, length);
isource->offset += length;
}
else
{
png_error(png_ptr, "pngReaderCallback failed");
}
}
2010-07-23 14:36:16 +08:00
UIImage::UIImage(void)
{
m_pBitmap = NULL;
2010-09-18 14:14:51 +08:00
m_imageInfo.hasAlpha = false;
m_imageInfo.isPremultipliedAlpha = false;
m_imageInfo.height = 0;
m_imageInfo.width = 0;
m_imageInfo.data = NULL;
m_imageInfo.bitsPerComponent = 0;
2010-07-23 14:36:16 +08:00
}
2010-09-18 14:14:51 +08:00
2010-09-09 17:24:55 +08:00
UIImage::UIImage(TBitmap *bitmap)
{
2010-09-18 14:14:51 +08:00
if (bitmap)
{
m_pBitmap = bitmap->DupBitmapTo32();
// init imageinfo
m_imageInfo.data = m_pBitmap->GetDataPtr();
m_imageInfo.height = m_pBitmap->GetHeight();
m_imageInfo.width = m_pBitmap->GetWidth();
m_imageInfo.hasAlpha = m_pBitmap->HasAlphaData();
// uphone only support predefined
m_imageInfo.isPremultipliedAlpha = true;
m_imageInfo.bitsPerComponent = m_pBitmap->GetDepth();
}
2010-09-09 17:24:55 +08:00
}
2010-07-23 14:36:16 +08:00
2010-07-27 15:21:21 +08:00
UIImage::UIImage(int nX, int nY, void *buffer)
2010-07-23 14:36:16 +08:00
{
}
UIImage::~UIImage(void)
{
2010-07-30 18:17:13 +08:00
if (m_pBitmap)
{
2010-09-18 14:14:51 +08:00
// the m_imageInfo's data points to m_pBitmap,
// so we don't release m_imageInfo's data
2010-07-30 18:17:13 +08:00
m_pBitmap->Destroy();
}
2010-09-18 14:14:51 +08:00
else
2010-07-23 14:36:16 +08:00
{
2010-09-18 14:14:51 +08:00
if (m_imageInfo.data)
2010-08-02 10:29:00 +08:00
{
2010-09-18 14:14:51 +08:00
delete []m_imageInfo.data;
2010-08-02 10:29:00 +08:00
}
2010-09-18 14:14:51 +08:00
}
}
2010-08-02 10:29:00 +08:00
2010-09-18 14:14:51 +08:00
bool UIImage::initWithContentsOfFile(const string &strPath)
{
// use libpng load image
return loadPng(strPath.c_str());
2010-07-23 14:36:16 +08:00
}
2010-07-27 15:21:21 +08:00
unsigned int UIImage::width(void)
2010-07-23 14:36:16 +08:00
{
2010-09-18 14:14:51 +08:00
return m_imageInfo.width;
2010-07-23 14:36:16 +08:00
}
2010-07-27 15:21:21 +08:00
unsigned int UIImage::height(void)
2010-07-23 14:36:16 +08:00
{
2010-09-18 14:14:51 +08:00
return m_imageInfo.height;
2010-07-23 14:36:16 +08:00
}
bool UIImage::isAlphaPixelFormat(void)
{
2010-09-18 14:14:51 +08:00
return m_imageInfo.hasAlpha;
2010-07-23 14:36:16 +08:00
}
// now, uphone only support premultiplied data
// so, we only return true
bool UIImage::isPremultipliedAlpha(void)
{
2010-09-18 17:49:02 +08:00
return m_imageInfo.isPremultipliedAlpha;
}
2010-07-27 15:21:21 +08:00
// compute how many bits every color component
int UIImage::CGImageGetBitsPerComponent(void)
2010-07-23 14:36:16 +08:00
{
2010-09-18 14:14:51 +08:00
return m_imageInfo.bitsPerComponent;
2010-07-23 14:36:16 +08:00
}
// 0 -> it is a mask
2010-07-27 15:21:21 +08:00
// 1 -> other
int UIImage::CGImageGetColorSpace(void)
2010-07-23 14:36:16 +08:00
{
2010-07-27 15:21:21 +08:00
int nRet = 1;
2010-09-18 14:14:51 +08:00
if (m_imageInfo.bitsPerComponent == 8)
2010-07-23 14:36:16 +08:00
{
2010-07-27 15:21:21 +08:00
nRet = 0;
2010-07-23 14:36:16 +08:00
}
2010-07-27 15:21:21 +08:00
return nRet;
2010-07-23 14:36:16 +08:00
}
2010-07-27 09:35:25 +08:00
2010-09-04 12:00:39 +08:00
unsigned char* UIImage::getRGBA8888Data(void)
2010-07-27 09:35:25 +08:00
{
2010-09-18 14:14:51 +08:00
return m_imageInfo.data;
2010-07-27 09:35:25 +08:00
}
2010-08-12 14:46:58 +08:00
bool UIImage::loadPng(const char* strFileName)
{
2010-09-18 14:14:51 +08:00
FILE *fp;
unsigned char *buffer = NULL;
bool bRet = true;
2010-08-12 14:46:58 +08:00
fp = NULL;
2010-09-18 14:14:51 +08:00
do
{
// open file
fp = fopen(strFileName, "rb");
if (!fp)
{
bRet = false;
break;
}
// compute the length of file
fseek(fp,0,SEEK_END);
int size = ftell(fp);
fseek(fp,0,SEEK_SET);
// allocate enough memory to save the data of file
buffer = new unsigned char[size];
if (! buffer)
{
bRet = false;
break;
}
// read data
fread(buffer, sizeof(unsigned char), size, fp);
bRet = loadPngFromStream(buffer, size);
delete[] buffer;
} while (0);
if (fp)
{
fclose(fp);
}
return bRet;
2010-08-12 14:46:58 +08:00
}
2010-08-26 12:04:22 +08:00
bool UIImage::loadPngFromStream(unsigned char *data, int nLength)
{
char header[8];
png_structp png_ptr;
png_infop info_ptr;
Int32 pos;
Int32 interlaceType;
png_bytep * rowPointers;
tImageSource imageSource;
pos = 0;
// read 8 bytes from the beginning of stream
unsigned char *tmp = data;
memcpy(header, tmp, 8);
// close the file if it's not a png
if (png_sig_cmp((png_bytep)header, 0, 8))
{
return false;
}
// init png_struct
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
return false;
}
// init png_info
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_read_struct(&png_ptr, NULL, NULL);
return false;
}
// if something wrong,close file and return
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, NULL, NULL);
png_destroy_info_struct(png_ptr, &info_ptr);
return false;
}
// set the read call back function
imageSource.data = data;
imageSource.size = nLength;
imageSource.offset = 0;
png_set_read_fn(png_ptr, &imageSource, pngReadCallback);
2010-09-24 10:54:06 +08:00
// read png info
png_read_info(png_ptr, info_ptr);
2010-08-26 12:04:22 +08:00
2010-09-18 14:14:51 +08:00
// init image info
2010-09-24 10:54:06 +08:00
m_imageInfo.height = info_ptr->height;
m_imageInfo.width = info_ptr->width;
2010-09-18 17:49:02 +08:00
m_imageInfo.isPremultipliedAlpha = false;
2010-09-18 14:14:51 +08:00
// we use CCX_RGA or CCX_RGB to save data
// so the bitsPerComponet is 32, and it also
// has the alpha data
m_imageInfo.bitsPerComponent = 32;
m_imageInfo.hasAlpha = true;
2010-08-26 12:04:22 +08:00
2010-09-24 10:54:06 +08:00
// convert to appropriate format, we now only support RGBA8888
if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
{
png_set_packing(png_ptr);
png_set_palette_to_rgb(png_ptr);
}
if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY && info_ptr->bit_depth < 8)
{
png_set_expand_gray_1_2_4_to_8(png_ptr);
}
if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
{
png_set_gray_to_rgb(png_ptr);
}
if (info_ptr->bit_depth == 16)
{
png_set_strip_16(png_ptr);
}
// expand paletted or RGB images with transparency to full alpha channels so the data will be
// available as RGBA quatets
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
{
png_set_tRNS_to_alpha(png_ptr);
}
// add the alpha channel if it has not
if (info_ptr->color_type == PNG_COLOR_TYPE_RGB || info_ptr->color_type == PNG_COLOR_TYPE_GRAY)
{
png_set_add_alpha(png_ptr, 255, PNG_FILLER_AFTER);
}
// allocate memory and read data
m_imageInfo.data = new unsigned char[m_imageInfo.height * m_imageInfo.width * 4];
rowPointers = (png_bytep*)png_mem_alloc(sizeof(png_bytep) * m_imageInfo.height);
for (int i = 0; i < m_imageInfo.height; ++i)
{
rowPointers[i] = (png_bytep)png_mem_alloc(m_imageInfo.width * 4);
}
png_read_image(png_ptr, rowPointers);
// copy data to image info
int bytesPerRow = m_imageInfo.width * 4;
for (int j = 0; j < m_imageInfo.height; ++j)
{
memcpy(m_imageInfo.data + j * bytesPerRow, rowPointers[j], bytesPerRow);
}
2010-08-26 12:04:22 +08:00
// release
png_destroy_read_struct(&png_ptr, NULL, NULL);
png_destroy_info_struct(png_ptr, &info_ptr);
return true;
}
bool UIImage::save(const std::string &strFileName, int nFormat)
{
/// @todo uiimage::save
return false;
}
2010-08-26 13:47:47 +08:00
bool UIImage::initWithData(unsigned char *pBuffer, int nLength)
{
2010-09-18 14:14:51 +08:00
return loadPngFromStream(pBuffer, nLength);
}
2010-08-18 16:49:49 +08:00
2010-08-26 13:47:47 +08:00
bool UIImage::initWithBuffer(int tx, int ty, unsigned char *pBuffer)
{
/// @todo
return false;
}
2010-08-04 15:46:12 +08:00
}//namespace cocos2d