axmol/cocos/base/TGAlib.cpp

340 lines
10 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
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-07-27 13:47:29 +08:00
#include <string.h>
2010-07-19 10:50:47 +08:00
#include <stdlib.h>
#include "base/TGAlib.h"
2014-04-27 01:35:57 +08:00
#include "base/CCData.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "platform/CCFileUtils.h"
2010-07-19 10:50:47 +08:00
NS_CC_BEGIN
2010-07-19 10:50:47 +08:00
2013-12-18 18:51:36 +08:00
static bool tgaLoadRLEImageData(unsigned char* Buffer, unsigned long bufSize, tImageTGA *info);
2011-07-08 14:15:51 +08:00
void tgaFlipImage( tImageTGA *info );
// load the image header field from stream
2013-12-18 18:51:36 +08:00
bool tgaLoadHeader(unsigned char* buffer, unsigned long bufSize, tImageTGA *info)
{
2013-12-18 18:51:36 +08:00
bool ret = false;
2010-07-19 10:50:47 +08:00
do
{
size_t step = sizeof(unsigned char) * 2;
CC_BREAK_IF((step + sizeof(unsigned char)) > bufSize);
2013-12-18 18:51:36 +08:00
memcpy(&info->type, buffer + step, sizeof(unsigned char));
2010-07-19 10:50:47 +08:00
step += sizeof(unsigned char) * 2;
step += sizeof(signed short) * 4;
CC_BREAK_IF((step + sizeof(signed short) * 2 + sizeof(unsigned char)) > bufSize);
2013-12-18 18:51:36 +08:00
memcpy(&info->width, buffer + step, sizeof(signed short));
memcpy(&info->height, buffer + step + sizeof(signed short), sizeof(signed short));
memcpy(&info->pixelDepth, buffer + step + sizeof(signed short) * 2, sizeof(unsigned char));
2010-07-19 10:50:47 +08:00
step += sizeof(unsigned char);
step += sizeof(signed short) * 2;
CC_BREAK_IF((step + sizeof(unsigned char)) > bufSize);
unsigned char cGarbage;
2013-12-18 18:51:36 +08:00
memcpy(&cGarbage, buffer + step, sizeof(unsigned char));
2010-07-19 10:50:47 +08:00
2013-12-18 18:51:36 +08:00
info->flipped = 0;
if ( cGarbage & 0x20 )
{
2013-12-18 18:51:36 +08:00
info->flipped = 1;
}
2013-12-18 18:51:36 +08:00
ret = true;
} while (0);
2010-07-19 10:50:47 +08:00
2013-12-18 18:51:36 +08:00
return ret;
2010-07-19 10:50:47 +08:00
}
2013-12-18 18:51:36 +08:00
bool tgaLoadImageData(unsigned char *Buffer, unsigned long bufSize, tImageTGA *info)
{
2013-12-18 18:51:36 +08:00
bool ret = false;
do
{
int mode,total,i;
unsigned char aux;
size_t step = (sizeof(unsigned char) + sizeof(signed short)) * 6;
// mode equal the number of components for each pixel
2013-12-18 18:51:36 +08:00
mode = info->pixelDepth / 8;
// total is the number of unsigned chars we'll have to read
2013-12-18 18:51:36 +08:00
total = info->height * info->width * mode;
size_t dataSize = sizeof(unsigned char) * total;
CC_BREAK_IF((step + dataSize) > bufSize);
2013-12-18 18:51:36 +08:00
memcpy(info->imageData, Buffer + step, dataSize);
// mode=3 or 4 implies that the image is RGB(A). However TGA
// stores it as BGR(A) so we'll have to swap R and B.
if (mode >= 3)
{
for (i=0; i < total; i+= mode)
{
2013-12-18 18:51:36 +08:00
aux = info->imageData[i];
info->imageData[i] = info->imageData[i+2];
info->imageData[i+2] = aux;
}
}
2013-12-18 18:51:36 +08:00
ret = true;
} while (0);
2013-12-18 18:51:36 +08:00
return ret;
2010-07-19 10:50:47 +08:00
}
2013-12-18 18:51:36 +08:00
static bool tgaLoadRLEImageData(unsigned char* buffer, unsigned long bufSize, tImageTGA *info)
2010-07-19 10:50:47 +08:00
{
unsigned int mode,total,i, index = 0;
unsigned char aux[4], runlength = 0;
unsigned int skip = 0, flag = 0;
size_t step = (sizeof(unsigned char) + sizeof(signed short)) * 6;
// mode equal the number of components for each pixel
2013-12-18 18:51:36 +08:00
mode = info->pixelDepth / 8;
// total is the number of unsigned chars we'll have to read
2013-12-18 18:51:36 +08:00
total = info->height * info->width;
for( i = 0; i < total; i++ )
{
// if we have a run length pending, run it
if ( runlength != 0 )
{
// we do, update the run length count
runlength--;
skip = (flag != 0);
}
else
{
// otherwise, read in the run length token
CC_BREAK_IF((step + sizeof(unsigned char)) > bufSize);
2013-12-18 18:51:36 +08:00
memcpy(&runlength, buffer + step, sizeof(unsigned char));
step += sizeof(unsigned char);
// see if it's a RLE encoded sequence
flag = runlength & 0x80;
if ( flag )
{
runlength -= 128;
}
skip = 0;
}
// do we need to skip reading this pixel?
if ( !skip )
{
// no, read in the pixel data
CC_BREAK_IF((step + sizeof(unsigned char) * mode) > bufSize);
2013-12-18 18:51:36 +08:00
memcpy(aux, buffer + step, sizeof(unsigned char) * mode);
step += sizeof(unsigned char) * mode;
// mode=3 or 4 implies that the image is RGB(A). However TGA
// stores it as BGR(A) so we'll have to swap R and B.
if ( mode >= 3 )
{
unsigned char tmp;
tmp = aux[0];
aux[0] = aux[2];
aux[2] = tmp;
}
}
// add the pixel to our image
2013-12-18 18:51:36 +08:00
memcpy(&info->imageData[index], aux, mode);
index += mode;
}
return true;
2010-07-19 10:50:47 +08:00
}
2013-12-18 18:51:36 +08:00
void tgaFlipImage( tImageTGA *info )
2010-07-19 10:50:47 +08:00
{
// mode equal the number of components for each pixel
2013-12-18 18:51:36 +08:00
int mode = info->pixelDepth / 8;
int rowbytes = info->width*mode;
unsigned char *row = (unsigned char *)malloc(rowbytes);
int y;
if (row == nullptr) return;
2013-12-18 18:51:36 +08:00
for( y = 0; y < (info->height/2); y++ )
{
2013-12-18 18:51:36 +08:00
memcpy(row, &info->imageData[y*rowbytes],rowbytes);
memcpy(&info->imageData[y*rowbytes], &info->imageData[(info->height-(y+1))*rowbytes], rowbytes);
memcpy(&info->imageData[(info->height-(y+1))*rowbytes], row, rowbytes);
}
free(row);
2013-12-18 18:51:36 +08:00
info->flipped = 0;
2010-07-19 10:50:47 +08:00
}
tImageTGA* tgaLoadBuffer(unsigned char* buffer, long size)
{
int mode,total;
tImageTGA *info = nullptr;
2012-06-07 14:13:44 +08:00
do
{
CC_BREAK_IF(! buffer);
info = (tImageTGA *)malloc(sizeof(tImageTGA));
// get the file header info
if (! tgaLoadHeader(buffer, size, info))
{
info->status = TGA_ERROR_MEMORY;
break;
}
// check if the image is color indexed
if (info->type == 1)
{
info->status = TGA_ERROR_INDEXED_COLOR;
break;
}
// check for other types (compressed images)
if ((info->type != 2) && (info->type !=3) && (info->type !=10) )
{
info->status = TGA_ERROR_COMPRESSED_FILE;
break;
}
// mode equals the number of image components
mode = info->pixelDepth / 8;
// total is the number of unsigned chars to read
total = info->height * info->width * mode;
// allocate memory for image pixels
info->imageData = (unsigned char *)malloc(sizeof(unsigned char) * total);
// check to make sure we have the memory required
if (info->imageData == nullptr)
{
info->status = TGA_ERROR_MEMORY;
break;
}
bool bLoadImage = false;
// finally load the image pixels
if ( info->type == 10 )
{
bLoadImage = tgaLoadRLEImageData(buffer, size, info);
}
else
{
bLoadImage = tgaLoadImageData(buffer, size, info);
}
// check for errors when reading the pixels
if (! bLoadImage)
{
info->status = TGA_ERROR_READING_FILE;
break;
}
info->status = TGA_OK;
if ( info->flipped )
{
tgaFlipImage( info );
if ( info->flipped )
{
info->status = TGA_ERROR_MEMORY;
}
}
} while(0);
return info;
}
// this is the function to call when we want to load an image
tImageTGA * tgaLoad(const char *filename)
{
Data data = FileUtils::getInstance()->getDataFromFile(filename);
if (!data.isNull())
{
return tgaLoadBuffer(data.getBytes(), data.getSize());
}
return nullptr;
2010-07-19 10:50:47 +08:00
}
// converts RGB to grayscale
2013-12-18 18:51:36 +08:00
void tgaRGBtogreyscale(tImageTGA *info) {
int mode,i,j;
unsigned char *newImageData;
// if the image is already grayscale do nothing
2013-12-18 18:51:36 +08:00
if (info->pixelDepth == 8)
return;
// compute the number of actual components
2013-12-18 18:51:36 +08:00
mode = info->pixelDepth / 8;
// allocate an array for the new image data
newImageData = (unsigned char *)malloc(sizeof(unsigned char) *
2013-12-18 18:51:36 +08:00
info->height * info->width);
if (newImageData == nullptr) {
return;
}
// convert pixels: grayscale = o.30 * R + 0.59 * G + 0.11 * B
2013-12-18 18:51:36 +08:00
for (i = 0,j = 0; j < info->width * info->height; i +=mode, j++)
newImageData[j] =
2013-12-18 18:51:36 +08:00
(unsigned char)(0.30 * info->imageData[i] +
0.59 * info->imageData[i+1] +
0.11 * info->imageData[i+2]);
//free old image data
2013-12-18 18:51:36 +08:00
free(info->imageData);
// reassign pixelDepth and type according to the new image type
2013-12-18 18:51:36 +08:00
info->pixelDepth = 8;
info->type = 3;
// reassigning imageData to the new array.
2013-12-18 18:51:36 +08:00
info->imageData = newImageData;
2010-07-19 10:50:47 +08:00
}
// releases the memory used for the image
2013-12-18 18:51:36 +08:00
void tgaDestroy(tImageTGA *info) {
2013-12-18 18:51:36 +08:00
if (info != nullptr) {
if (info->imageData != nullptr)
{
2013-12-18 18:51:36 +08:00
free(info->imageData);
}
2013-12-18 18:51:36 +08:00
free(info);
}
2010-07-19 10:50:47 +08:00
}
NS_CC_END