cocos2d的资源压缩主要介绍的是图片的压缩算法,最近刚好在弄,所以贴出源代码供大家去学习。
- bool CCTexture2D::initPremultipliedATextureWithImage(CCImage *image, unsigned int width, unsigned int height)
- {
- unsigned char* tempData = image->getData();
- unsigned int* inPixel32 = NULL;
- unsigned char* inPixel8 = NULL;
- unsigned short* outPixel16 = NULL;
- bool hasAlpha = image->hasAlpha();
- CCSize imageSize = CCSizeMake((float)(image->getWidth()), (float)(image->getHeight()));
- CCTexture2DPixelFormat pixelFormat;
- size_t bpp = image->getBitsPerComponent();
- // compute pixel format
- if(hasAlpha)
- {
- pixelFormat = g_defaultAlphaPixelFormat;
- }
- else
- {
- if (bpp >= 8)
- {
- pixelFormat = kCCTexture2DPixelFormat_RGB888;
- }
- else
- {
- pixelFormat = kCCTexture2DPixelFormat_RGB565;
- }
- }
- // Repack the pixel data into the right format
- unsigned int length = width * height;
- if (pixelFormat == kCCTexture2DPixelFormat_RGB565)
- {
- if (hasAlpha)
- {
- // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB"
- tempData = new unsigned char[width * height * 2];
- outPixel16 = (unsigned short*)tempData;
- inPixel32 = (unsigned int*)image->getData();
- for(unsigned int i = 0; i < length; ++i, ++inPixel32)
- {
- *outPixel16++ =
- ((((*inPixel32 >> 0) & 0xFF) >> 3) << 11) | // R
- ((((*inPixel32 >> 8) & 0xFF) >> 2) << 5) | // G
- ((((*inPixel32 >> 16) & 0xFF) >> 3) << 0); // B
- }
- }
- else
- {
- // Convert "RRRRRRRRRGGGGGGGGBBBBBBBB" to "RRRRRGGGGGGBBBBB"
- tempData = new unsigned char[width * height * 2];
- outPixel16 = (unsigned short*)tempData;
- inPixel8 = (unsigned char*)image->getData();
- for(unsigned int i = 0; i < length; ++i)
- {
- *outPixel16++ =
- (((*inPixel8++ & 0xFF) >> 3) << 11) | // R
- (((*inPixel8++ & 0xFF) >> 2) << 5) | // G
- (((*inPixel8++ & 0xFF) >> 3) << 0); // B
- }
- }
- }
- else if (pixelFormat == kCCTexture2DPixelFormat_RGBA4444)
- {
- // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRGGGGBBBBAAAA"
- inPixel32 = (unsigned int*)image->getData();
- tempData = new unsigned char[width * height * 2];
- outPixel16 = (unsigned short*)tempData;
- for(unsigned int i = 0; i < length; ++i, ++inPixel32)
- {
- *outPixel16++ =
- ((((*inPixel32 >> 0) & 0xFF) >> 4) << 12) | // R
- ((((*inPixel32 >> 8) & 0xFF) >> 4) << 8) | // G
- ((((*inPixel32 >> 16) & 0xFF) >> 4) << 4) | // B
- ((((*inPixel32 >> 24) & 0xFF) >> 4) << 0); // A
- }
- }
- else if (pixelFormat == kCCTexture2DPixelFormat_RGB5A1)
- {
- // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGBBBBBA"
- inPixel32 = (unsigned int*)image->getData();
- tempData = new unsigned char[width * height * 2];
- outPixel16 = (unsigned short*)tempData;
- for(unsigned int i = 0; i < length; ++i, ++inPixel32)
- {
- *outPixel16++ =
- ((((*inPixel32 >> 0) & 0xFF) >> 3) << 11) | // R
- ((((*inPixel32 >> 8) & 0xFF) >> 3) << 6) | // G
- ((((*inPixel32 >> 16) & 0xFF) >> 3) << 1) | // B
- ((((*inPixel32 >> 24) & 0xFF) >> 7) << 0); // A
- }
- }
- else if (pixelFormat == kCCTexture2DPixelFormat_A8)
- {
- // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "AAAAAAAA"
- inPixel32 = (unsigned int*)image->getData();
- tempData = new unsigned char[width * height];
- unsigned char *outPixel8 = tempData;
- for(unsigned int i = 0; i < length; ++i, ++inPixel32)
- {
- *outPixel8++ = (*inPixel32 >> 24) & 0xFF; // A
- }
- }
- if (hasAlpha && pixelFormat == kCCTexture2DPixelFormat_RGB888)
- {
- // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRRRRGGGGGGGGBBBBBBBB"
- inPixel32 = (unsigned int*)image->getData();
- tempData = new unsigned char[width * height * 3];
- unsigned char *outPixel8 = tempData;
- for(unsigned int i = 0; i < length; ++i, ++inPixel32)
- {
- *outPixel8++ = (*inPixel32 >> 0) & 0xFF; // R
- *outPixel8++ = (*inPixel32 >> 8) & 0xFF; // G
- *outPixel8++ = (*inPixel32 >> 16) & 0xFF; // B
- }
- }
- initWithData(tempData, pixelFormat, width, height, imageSize);
- if (tempData != image->getData())
- {
- delete [] tempData;
- }
- m_bHasPremultipliedAlpha = image->isPremultipliedAlpha();
- return true;
- }