[android] fixed #353: support jpg on android

This commit is contained in:
minggo 2011-02-23 13:43:45 +08:00
parent 31f9969674
commit 3dde0ebcf5
3 changed files with 68 additions and 5 deletions

View File

@ -62,7 +62,7 @@ bool HelloWorld::init()
this->addChild(pLabel, 1); this->addChild(pLabel, 1);
// add "HelloWorld" splash screen" // add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png"); CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.jpg");
// position the sprite on the center of the screen // position the sprite on the center of the screen
pSprite->setPosition( ccp(size.width/2, size.height/2) ); pSprite->setPosition( ccp(size.width/2, size.height/2) );

View File

@ -32,6 +32,9 @@ THE SOFTWARE.
#include <android/log.h> #include <android/log.h>
#define LOG_TAG "UIImage"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
//using namespace ImageToolKit; //using namespace ImageToolKit;
using namespace std; using namespace std;
namespace cocos2d { namespace cocos2d {
@ -216,7 +219,7 @@ bool UIImage::loadPngFromStream(unsigned char *data, int nLength)
// if something wrong,close file and return // if something wrong,close file and return
if (setjmp(png_jmpbuf(png_ptr))) if (setjmp(png_jmpbuf(png_ptr)))
{ {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL); png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return false; return false;
} }
@ -279,8 +282,68 @@ bool UIImage::loadPngFromStream(unsigned char *data, int nLength)
bool UIImage::loadJpgFromStream(unsigned char *data, unsigned long nSize) bool UIImage::loadJpgFromStream(unsigned char *data, unsigned long nSize)
{ {
/// @todo: libjpeg of android not support jpeg_mem_src() /* these are standard libjpeg structures for reading(decompression) */
return false; struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
/* libjpeg data structure for storing one row, that is, scanline of an image */
JSAMPROW row_pointer[1];
unsigned long location = 0;
unsigned int i = 0;
/* here we set up the standard libjpeg error handler */
cinfo.err = jpeg_std_error( &jerr );
/* setup decompression process and source, then read JPEG header */
jpeg_create_decompress( &cinfo );
/* this makes the library read from infile */
jpeg_mem_src( &cinfo, data, nSize );
/* reading the image header which contains image information */
jpeg_read_header( &cinfo, true );
// we only support RGB or grayscale
if (cinfo.jpeg_color_space != JCS_RGB)
{
if (cinfo.jpeg_color_space == JCS_GRAYSCALE || cinfo.jpeg_color_space == JCS_YCbCr)
{
cinfo.out_color_space = JCS_RGB;
}
}
else
{
return false;
}
/* Start decompression jpeg here */
jpeg_start_decompress( &cinfo );
/* init image info */
m_imageInfo.width = cinfo.image_width;
m_imageInfo.height = cinfo.image_height;
m_imageInfo.hasAlpha = false;
m_imageInfo.isPremultipliedAlpha = false;
m_imageInfo.bitsPerComponent = 8;
m_imageInfo.data = new unsigned char[cinfo.output_width*cinfo.output_height*cinfo.output_components];
/* now actually read the jpeg into the raw buffer */
row_pointer[0] = new unsigned char[cinfo.output_width*cinfo.output_components];
/* read one scan line at a time */
while( cinfo.output_scanline < cinfo.image_height )
{
jpeg_read_scanlines( &cinfo, row_pointer, 1 );
for( i=0; i<cinfo.image_width*cinfo.num_components;i++)
m_imageInfo.data[location++] = row_pointer[0][i];
}
/* wrap up decompression, destroy objects, free pointers and close open files */
jpeg_finish_decompress( &cinfo );
jpeg_destroy_decompress( &cinfo );
delete row_pointer[0];
return true;
} }
bool UIImage::save(const std::string &strFileName, int nFormat) bool UIImage::save(const std::string &strFileName, int nFormat)

View File

@ -1 +1 @@
017fff05a72479d95819cf89ac5cf6125d562960 b595fbd4d9aca89e30aab2b4216457f20ec58b9d