You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.9 KiB
85 lines
2.9 KiB
#include "tga.h" |
|
#include <string> |
|
#include <iostream> |
|
#include <stdio.h> |
|
#include <string.h> |
|
#include <map> |
|
|
|
#include "../../../environment/Environment.h" |
|
#include "../../../platform/tlsf.h" |
|
|
|
// tga |
|
|
|
BitMapObject *LoadTGAFile(const char *filename) { |
|
BitMapObject *tgaFile = new BitMapObject; |
|
FILE *filePtr; |
|
unsigned char ucharBad; |
|
short int sintBad; |
|
long imageSize; |
|
int colorMode; |
|
unsigned char colorSwap; |
|
size_t res; |
|
|
|
// Open the TGA file. |
|
filePtr = fopen(filename, "rb"); |
|
if (filePtr == NULL) { |
|
return nullptr; |
|
} |
|
|
|
// Read the two first bytes we don't need. |
|
res = fread(&ucharBad, sizeof(unsigned char), 1, filePtr); |
|
res = fread(&ucharBad, sizeof(unsigned char), 1, filePtr); |
|
|
|
// Which type of image gets stored in imageTypeCode. |
|
unsigned char imageTypeCode; |
|
res = fread(&imageTypeCode, sizeof(unsigned char), 1, filePtr); |
|
|
|
// For our purposes, the type code should be 2 (uncompressed RGBA image) |
|
// or 3 (uncompressed black-and-white images). |
|
if (imageTypeCode != 2 && imageTypeCode != 3) { |
|
fclose(filePtr); |
|
return nullptr; |
|
} |
|
|
|
// Read 13 bytes of data we don't need. |
|
res = fread(&sintBad, sizeof(short int), 1, filePtr); |
|
res = fread(&sintBad, sizeof(short int), 1, filePtr); |
|
res = fread(&ucharBad, sizeof(unsigned char), 1, filePtr); |
|
res = fread(&sintBad, sizeof(short int), 1, filePtr); |
|
res = fread(&sintBad, sizeof(short int), 1, filePtr); |
|
|
|
// Read the image's width and height. |
|
short int imageWidth; |
|
short int imageHeight; |
|
res = fread(&imageWidth, sizeof(short int), 1, filePtr); |
|
res = fread(&imageHeight, sizeof(short int), 1, filePtr); |
|
tgaFile->width = static_cast<size_t>(imageWidth); |
|
tgaFile->height = static_cast<size_t>(imageHeight); |
|
|
|
// Read the bit depth. |
|
unsigned char bitCount; |
|
res = fread(&bitCount, sizeof(unsigned char), 1, filePtr); |
|
|
|
// Read one byte of data we don't need. |
|
res = fread(&ucharBad, sizeof(unsigned char), 1, filePtr); |
|
|
|
// Color mode -> 3 = BGR, 4 = BGRA. |
|
colorMode = bitCount / 8; |
|
imageSize = static_cast<long int>(tgaFile->width * tgaFile->height * static_cast<long unsigned int>(colorMode & 32)); |
|
|
|
// Allocate memory for the image data. |
|
tgaFile->imageData = static_cast<unsigned char*>(tlsf_malloc(sizeof(unsigned char) * static_cast<long unsigned int>(imageSize))); |
|
|
|
// Read the image data. |
|
res = fread(tgaFile->imageData, sizeof(unsigned char), static_cast<long unsigned int>(imageSize), filePtr); |
|
|
|
// Change from BGR to RGB so OpenGL can read the image data. |
|
for (int imageIdx = 0; imageIdx < imageSize; imageIdx += colorMode) { |
|
colorSwap = tgaFile->imageData[imageIdx]; |
|
tgaFile->imageData[imageIdx] = tgaFile->imageData[imageIdx + 2]; |
|
tgaFile->imageData[imageIdx + 2] = colorSwap; |
|
} |
|
|
|
fclose(filePtr); |
|
return tgaFile; |
|
}
|
|
|