fixed #1240: Moved particle files to Particle folder. Fixed a bug in tiff decoding. Refactored CCTextureCache::adddImage.

This commit is contained in:
James Chen 2012-05-25 16:52:47 +08:00
parent 6477a02022
commit c902e884f4
6 changed files with 119 additions and 99 deletions

View File

@ -85,7 +85,7 @@ public:
*/
bool initWithImageData(void * pData,
int nDataLen,
EImageFormat eFmt = kFmtPng,
EImageFormat eFmt = kFmtUnKnown,
int nWidth = 0,
int nHeight = 0,
int nBitsPerComponent = 8);

View File

@ -37,6 +37,17 @@ THE SOFTWARE.
#include <ctype.h>
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS)
NS_CC_BEGIN
// premultiply alpha, or the effect will wrong when want to use other pixel format in CCTexture2D,
// such as RGB888, RGB5A1
#define CC_RGB_PREMULTIPLY_APLHA(vr, vg, vb, va) \
(unsigned)(((unsigned)((unsigned char)(vr) * ((unsigned char)(va) + 1)) >> 8) | \
((unsigned)((unsigned char)(vg) * ((unsigned char)(va) + 1) >> 8) << 8) | \
((unsigned)((unsigned char)(vb) * ((unsigned char)(va) + 1) >> 8) << 16) | \
((unsigned)(unsigned char)(va) << 24))
// on ios, we should use platform/ios/CCImage_ios.mm instead
typedef struct
@ -61,8 +72,6 @@ static void pngReadCallback(png_structp png_ptr, png_bytep data, png_size_t leng
}
}
NS_CC_BEGIN
//////////////////////////////////////////////////////////////////////////
// Impliment CCImage
//////////////////////////////////////////////////////////////////////////
@ -129,6 +138,51 @@ bool CCImage::initWithImageData(void * pData,
bRet = _initWithRawData(pData, nDataLen, nWidth, nHeight, nBitsPerComponent);
break;
}
else
{
// if it is a png file buffer.
if (nDataLen > 8)
{
unsigned char* pHead = (unsigned char*)pData;
if ( pHead[0] == 0x89
&& pHead[1] == 0x50
&& pHead[2] == 0x4E
&& pHead[3] == 0x47
&& pHead[4] == 0x0D
&& pHead[5] == 0x0A
&& pHead[6] == 0x1A
&& pHead[7] == 0x0A)
{
bRet = _initWithPngData(pData, nDataLen);
break;
}
}
// if it is a tiff file buffer.
if (nDataLen > 2)
{
unsigned char* pHead = (unsigned char*)pData;
if ( (pHead[0] == 0x49 && pHead[1] == 0x49)
|| (pHead[0] == 0x4d && pHead[1] == 0x4d)
)
{
bRet = _initWithTiffData(pData, nDataLen);
break;
}
}
// if it is a jpeg file buffer.
if (nDataLen > 2)
{
unsigned char* pHead = (unsigned char*)pData;
if ( pHead[0] == 0xff
&& pHead[1] == 0xd8)
{
bRet = _initWithJpgData(pData, nDataLen);
break;
}
}
}
} while (0);
return bRet;
}
@ -309,14 +363,6 @@ bool CCImage::_initWithPngData(void * pData, int nDatalen)
if (m_bHasAlpha)
{
// premultiply alpha, or the effect will wrong when want to use other pixel format in CCTexture2D,
// such as RGB888, RGB5A1
#define CC_RGB_PREMULTIPLY_APLHA(vr, vg, vb, va) \
(unsigned)(((unsigned)((unsigned char)(vr) * ((unsigned char)(va) + 1)) >> 8) | \
((unsigned)((unsigned char)(vg) * ((unsigned char)(va) + 1) >> 8) << 8) | \
((unsigned)((unsigned char)(vb) * ((unsigned char)(va) + 1) >> 8) << 16) | \
((unsigned)(unsigned char)(va) << 24))
unsigned int *tmp = (unsigned int *)m_pData;
for(unsigned int i = 0; i < m_nHeight; i++)
{
@ -455,7 +501,6 @@ bool CCImage::_initWithTiffData(void* pData, int nDataLen)
bool bRet = false;
do
{
// set the read call back function
tImageSource imageSource;
imageSource.data = (unsigned char*)pData;
@ -471,14 +516,15 @@ bool CCImage::_initWithTiffData(void* pData, int nDataLen)
CC_BREAK_IF(NULL == tif);
uint32 w, h;
uint16 bitsPerSample, samplePerPixel;
uint16 bitsPerSample, samplePerPixel, planarConfig, extraSample;
size_t npixels;
uint32* raster;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bitsPerSample);
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplePerPixel);
TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &planarConfig);
npixels = w * h;
m_bHasAlpha = true;
@ -486,16 +532,33 @@ bool CCImage::_initWithTiffData(void* pData, int nDataLen)
m_nHeight = h;
m_nBitsPerComponent = 8;
m_pData = new unsigned char[npixels*sizeof (uint32)];
raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32));
if (raster != NULL)
{
if (TIFFReadRGBAImageOriented(tif, w, h, raster, ORIENTATION_TOPLEFT, 0))
{
memcpy(m_pData, raster, npixels * sizeof (uint32));
}
_TIFFfree(raster);
m_pData = new unsigned char[npixels * sizeof (uint32)];
uint32* raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32));
if (raster != NULL)
{
if (TIFFReadRGBAImageOriented(tif, w, h, raster, ORIENTATION_TOPLEFT, 0))
{
unsigned char* src = (unsigned char*)raster;
unsigned int* tmp = (unsigned int*)m_pData;
/* the raster data is pre-multiplied by the alpha component
after invoking TIFFReadRGBAImageOriented
for(int j = 0; j < m_nWidth * m_nHeight * 4; j += 4)
{
*tmp++ = CC_RGB_PREMULTIPLY_APLHA( src[j], src[j + 1],
src[j + 2], src[j + 3] );
}
*/
m_bPreMulti = true;
memcpy(m_pData, raster, npixels*sizeof (uint32));
}
_TIFFfree(raster);
}
TIFFClose(tif);
bRet = true;

View File

@ -365,69 +365,27 @@ CCTexture2D * CCTextureCache::addImage(const char * path)
{
texture = this->addPVRImage(fullpath.c_str());
}
// Issue #886: TEMPORARY FIX FOR TRANSPARENT JPEGS IN IOS4
else if (std::string::npos != lowerCase.find(".jpg") || std::string::npos != lowerCase.find(".jpeg"))
{
CCImage image;
CCFileData data(fullpath.c_str(), "rb");
unsigned long nSize = data.getSize();
unsigned char* pBuffer = data.getBuffer();
CC_BREAK_IF(! image.initWithImageData((void*)pBuffer, nSize, CCImage::kFmtJpg));
texture = new CCTexture2D();
texture->initWithImage(&image, resolution);
if( texture )
{
#if CC_ENABLE_CACHE_TEXTTURE_DATA
// cache the texture file name
VolatileTexture::addImageTexture(texture, fullpath.c_str(), CCImage::kFmtJpg);
#endif
m_pTextures->setObject(texture, pathKey.c_str());
// autorelease prevents possible crash in multithreaded environments
texture->autorelease();
}
else
{
CCLOG("cocos2d: Couldn't add image:%s in CCTextureCache", path);
}
}
else if (std::string::npos != lowerCase.find(".tif") || std::string::npos != lowerCase.find(".tiff"))
{
CCImage image;
CCFileData data(fullpath.c_str(), "rb");
unsigned long nSize = data.getSize();
unsigned char* pBuffer = data.getBuffer();
CC_BREAK_IF(! image.initWithImageData((void*)pBuffer, nSize, CCImage::kFmtTiff));
texture = new CCTexture2D();
texture->initWithImage(&image, resolution);
if( texture )
{
#if CC_ENABLE_CACHE_TEXTTURE_DATA
// cache the texture file name
VolatileTexture::addImageTexture(texture, fullpath.c_str(), CCImage::kFmtTiff);
#endif
m_pTextures->setObject(texture, pathKey.c_str());
// autorelease prevents possible crash in multithreaded environments
texture->autorelease();
}
else
{
CCLOG("cocos2d: Couldn't add image:%s in CCTextureCache", path);
}
}
else
{
// prevents overloading the autorelease pool
CCImage::EImageFormat eImageFormat = CCImage::kFmtUnKnown;
if (std::string::npos != lowerCase.find(".png"))
{
eImageFormat = CCImage::kFmtPng;
}
else if (std::string::npos != lowerCase.find(".jpg") || std::string::npos != lowerCase.find(".jpeg"))
{
eImageFormat = CCImage::kFmtJpg;
}
else if (std::string::npos != lowerCase.find(".tif") || std::string::npos != lowerCase.find(".tiff"))
{
eImageFormat = CCImage::kFmtTiff;
}
CCImage image;
CCFileData data(fullpath.c_str(), "rb");
unsigned long nSize = data.getSize();
unsigned char* pBuffer = data.getBuffer();
CC_BREAK_IF(! image.initWithImageData((void*)pBuffer, nSize, CCImage::kFmtPng));
CC_BREAK_IF(! image.initWithImageData((void*)pBuffer, nSize, eImageFormat));
texture = new CCTexture2D();
texture->initWithImage(&image, resolution);
@ -436,7 +394,7 @@ CCTexture2D * CCTextureCache::addImage(const char * path)
{
#if CC_ENABLE_CACHE_TEXTTURE_DATA
// cache the texture file name
VolatileTexture::addImageTexture(texture, fullpath.c_str(), CCImage::kFmtPng);
VolatileTexture::addImageTexture(texture, fullpath.c_str(), eImageFormat);
#endif
m_pTextures->setObject(texture, pathKey.c_str());
@ -448,7 +406,6 @@ CCTexture2D * CCTextureCache::addImage(const char * path)
CCLOG("cocos2d: Couldn't add image:%s in CCTextureCache", path);
}
}
} while (0);
}

View File

@ -1,7 +1,7 @@
#!/bin/bash
# set params
NDK_ROOT_LOCAL=/cygdrive/d/programe/android/ndk/android-ndk-r7b
COCOS2DX_ROOT_LOCAL=/cygdrive/e/cocos2d-x
NDK_ROOT_LOCAL=/cygdrive/e/android/android-ndk-r8
COCOS2DX_ROOT_LOCAL=/cygdrive/f/Project/dumganhar/cocos2d-x
buildexternalsfromsource=

View File

@ -905,7 +905,7 @@ void Issue870::onEnter()
m_background = NULL;
CCParticleSystemQuad *system = new CCParticleSystemQuad();
system->initWithFile("Images/SpinningPeas.plist");
system->initWithFile("Particles/SpinningPeas.plist");
system->setTextureWithRect(CCTextureCache::sharedTextureCache()->addImage("Images/particles.png"), CCRectMake(0,0,32,32));
addChild(system, 10);
m_emitter = system;
@ -946,7 +946,7 @@ void DemoParticleFromFile::onEnter()
m_background = NULL;
m_emitter = new CCParticleSystemQuad();
std::string filename = "Images/" + m_title + ".plist";
std::string filename = "Particles/" + m_title + ".plist";
m_emitter->initWithFile(filename.c_str());
addChild(m_emitter, 10);
@ -1235,7 +1235,7 @@ void ParticleBatchHybrid::onEnter()
removeChild(m_background, true);
m_background = NULL;
m_emitter = CCParticleSystemQuad::particleWithFile("Images/LavaFlow.plist");
m_emitter = CCParticleSystemQuad::particleWithFile("Particles/LavaFlow.plist");
m_emitter->retain();
CCParticleBatchNode *batch = CCParticleBatchNode::batchNodeWithTexture(m_emitter->getTexture());
@ -1283,11 +1283,11 @@ void ParticleBatchMultipleEmitters::onEnter()
removeChild(m_background, true);
m_background = NULL;
CCParticleSystemQuad *emitter1 = CCParticleSystemQuad::particleWithFile("Images/LavaFlow.plist");
CCParticleSystemQuad *emitter1 = CCParticleSystemQuad::particleWithFile("Particles/LavaFlow.plist");
emitter1->setStartColor(ccc4f(1,0,0,1));
CCParticleSystemQuad *emitter2 = CCParticleSystemQuad::particleWithFile("Images/LavaFlow.plist");
CCParticleSystemQuad *emitter2 = CCParticleSystemQuad::particleWithFile("Particles/LavaFlow.plist");
emitter2->setStartColor(ccc4f(0,1,0,1));
CCParticleSystemQuad *emitter3 = CCParticleSystemQuad::particleWithFile("Images/LavaFlow.plist");
CCParticleSystemQuad *emitter3 = CCParticleSystemQuad::particleWithFile("Particles/LavaFlow.plist");
emitter3->setStartColor(ccc4f(0,0,1,1));
CCSize s = CCDirector::sharedDirector()->getWinSize();
@ -1326,7 +1326,7 @@ void ParticleReorder::onEnter()
removeChild(m_background, true);
m_background = NULL;
CCParticleSystem* ignore = CCParticleSystemQuad::particleWithFile("Images/SmallSun.plist");
CCParticleSystem* ignore = CCParticleSystemQuad::particleWithFile("Particles/SmallSun.plist");
CCNode *parent1 = CCNode::node();
CCNode *parent2 = CCParticleBatchNode::batchNodeWithTexture(ignore->getTexture());
ignore->unscheduleUpdate();
@ -1335,13 +1335,13 @@ void ParticleReorder::onEnter()
{
CCNode *parent = ( i==0 ? parent1 : parent2 );
CCParticleSystemQuad *emitter1 = CCParticleSystemQuad::particleWithFile("Images/SmallSun.plist");
CCParticleSystemQuad *emitter1 = CCParticleSystemQuad::particleWithFile("Particles/SmallSun.plist");
emitter1->setStartColor(ccc4f(1,0,0,1));
emitter1->setIsBlendAdditive(false);
CCParticleSystemQuad *emitter2 = CCParticleSystemQuad::particleWithFile("Images/SmallSun.plist");
CCParticleSystemQuad *emitter2 = CCParticleSystemQuad::particleWithFile("Particles/SmallSun.plist");
emitter2->setStartColor(ccc4f(0,1,0,1));
emitter2->setIsBlendAdditive(false);
CCParticleSystemQuad *emitter3 = CCParticleSystemQuad::particleWithFile("Images/SmallSun.plist");
CCParticleSystemQuad *emitter3 = CCParticleSystemQuad::particleWithFile("Particles/SmallSun.plist");
emitter3->setStartColor(ccc4f(0,0,1,1));
emitter3->setIsBlendAdditive(false);
@ -1529,7 +1529,7 @@ void MultipleParticleSystems::onEnter()
CCTextureCache::sharedTextureCache()->addImage("Images/particles.png");
for (int i = 0; i<5; i++) {
CCParticleSystemQuad *particleSystem = CCParticleSystemQuad::particleWithFile("Images/SpinningPeas.plist");
CCParticleSystemQuad *particleSystem = CCParticleSystemQuad::particleWithFile("Particles/SpinningPeas.plist");
particleSystem->setPosition(ccp(i*50 ,i*50));
@ -1588,7 +1588,7 @@ void MultipleParticleSystemsBatched::onEnter()
for (int i = 0; i<5; i++) {
CCParticleSystemQuad *particleSystem = CCParticleSystemQuad::particleWithFile("Images/SpinningPeas.plist");
CCParticleSystemQuad *particleSystem = CCParticleSystemQuad::particleWithFile("Particles/SpinningPeas.plist");
particleSystem->setPositionType(kCCPositionTypeGrouped);
particleSystem->setPosition(ccp(i*50 ,i*50));
@ -1650,7 +1650,7 @@ void AddAndDeleteParticleSystems::onEnter()
for (int i = 0; i<6; i++) {
CCParticleSystemQuad *particleSystem = CCParticleSystemQuad::particleWithFile("Images/Spiral.plist");
CCParticleSystemQuad *particleSystem = CCParticleSystemQuad::particleWithFile("Particles/Spiral.plist");
m_pBatchNode->setTexture(particleSystem->getTexture());
particleSystem->setPositionType(kCCPositionTypeGrouped);
@ -1677,7 +1677,7 @@ void AddAndDeleteParticleSystems::removeSystem(ccTime dt)
unsigned int uRand = rand() % (nChildrenCount - 1);
m_pBatchNode->removeChild((CCNode*)m_pBatchNode->getChildren()->objectAtIndex(uRand), true);
CCParticleSystemQuad *particleSystem = CCParticleSystemQuad::particleWithFile("Images/Spiral.plist");
CCParticleSystemQuad *particleSystem = CCParticleSystemQuad::particleWithFile("Particles/Spiral.plist");
//add new
particleSystem->setPositionType(kCCPositionTypeGrouped);

View File

@ -1 +1 @@
c4d75ac2138f5eb68e89ecdba3c0f0746cda90cf
2e6b4ecbc083689855217489786150bb669823ed