From 3dde0ebcf587a462588edb5a8a8c949b7e8e6dfc Mon Sep 17 00:00:00 2001 From: minggo Date: Wed, 23 Feb 2011 13:43:45 +0800 Subject: [PATCH] [android] fixed #353: support jpg on android --- HelloWorld/HelloWorldScene.cpp | 2 +- .../platform/android/CCXUIImage_android.cpp | 69 ++++++++++++++++++- .../third_party/libs/libjpeg.a.REMOVED.git-id | 2 +- 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/HelloWorld/HelloWorldScene.cpp b/HelloWorld/HelloWorldScene.cpp index 42b5de9607..b5da25c317 100644 --- a/HelloWorld/HelloWorldScene.cpp +++ b/HelloWorld/HelloWorldScene.cpp @@ -62,7 +62,7 @@ bool HelloWorld::init() this->addChild(pLabel, 1); // 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 pSprite->setPosition( ccp(size.width/2, size.height/2) ); diff --git a/cocos2dx/platform/android/CCXUIImage_android.cpp b/cocos2dx/platform/android/CCXUIImage_android.cpp index e3a3784869..6b06cf65c3 100644 --- a/cocos2dx/platform/android/CCXUIImage_android.cpp +++ b/cocos2dx/platform/android/CCXUIImage_android.cpp @@ -32,6 +32,9 @@ THE SOFTWARE. #include +#define LOG_TAG "UIImage" +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) + //using namespace ImageToolKit; using namespace std; namespace cocos2d { @@ -216,7 +219,7 @@ bool UIImage::loadPngFromStream(unsigned char *data, int nLength) // if something wrong,close file and return if (setjmp(png_jmpbuf(png_ptr))) - { + { png_destroy_read_struct(&png_ptr, &info_ptr, NULL); return false; } @@ -279,8 +282,68 @@ bool UIImage::loadPngFromStream(unsigned char *data, int nLength) bool UIImage::loadJpgFromStream(unsigned char *data, unsigned long nSize) { - /// @todo: libjpeg of android not support jpeg_mem_src() - return false; + /* these are standard libjpeg structures for reading(decompression) */ + 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