|
|
|
@ -17,6 +17,7 @@ BitMapObject *LoadTGAFile(const char *filename) {
@@ -17,6 +17,7 @@ BitMapObject *LoadTGAFile(const char *filename) {
|
|
|
|
|
long imageSize; |
|
|
|
|
int colorMode; |
|
|
|
|
unsigned char colorSwap; |
|
|
|
|
size_t res; |
|
|
|
|
|
|
|
|
|
// Open the TGA file.
|
|
|
|
|
filePtr = fopen(filename, "rb"); |
|
|
|
@ -25,12 +26,12 @@ BitMapObject *LoadTGAFile(const char *filename) {
@@ -25,12 +26,12 @@ BitMapObject *LoadTGAFile(const char *filename) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Read the two first bytes we don't need.
|
|
|
|
|
fread(&ucharBad, sizeof(unsigned char), 1, filePtr); |
|
|
|
|
fread(&ucharBad, sizeof(unsigned char), 1, filePtr); |
|
|
|
|
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; |
|
|
|
|
fread(&imageTypeCode, sizeof(unsigned char), 1, filePtr); |
|
|
|
|
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).
|
|
|
|
@ -40,36 +41,36 @@ BitMapObject *LoadTGAFile(const char *filename) {
@@ -40,36 +41,36 @@ BitMapObject *LoadTGAFile(const char *filename) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Read 13 bytes of data we don't need.
|
|
|
|
|
fread(&sintBad, sizeof(short int), 1, filePtr); |
|
|
|
|
fread(&sintBad, sizeof(short int), 1, filePtr); |
|
|
|
|
fread(&ucharBad, sizeof(unsigned char), 1, filePtr); |
|
|
|
|
fread(&sintBad, sizeof(short int), 1, filePtr); |
|
|
|
|
fread(&sintBad, sizeof(short int), 1, filePtr); |
|
|
|
|
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; |
|
|
|
|
fread(&imageWidth, sizeof(short int), 1, filePtr); |
|
|
|
|
fread(&imageHeight, sizeof(short int), 1, filePtr); |
|
|
|
|
tgaFile->width = imageWidth; |
|
|
|
|
tgaFile->height = 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; |
|
|
|
|
fread(&bitCount, sizeof(unsigned char), 1, filePtr); |
|
|
|
|
res = fread(&bitCount, sizeof(unsigned char), 1, filePtr); |
|
|
|
|
|
|
|
|
|
// Read one byte of data we don't need.
|
|
|
|
|
fread(&ucharBad, sizeof(unsigned char), 1, filePtr); |
|
|
|
|
res = fread(&ucharBad, sizeof(unsigned char), 1, filePtr); |
|
|
|
|
|
|
|
|
|
// Color mode -> 3 = BGR, 4 = BGRA.
|
|
|
|
|
colorMode = bitCount / 8; |
|
|
|
|
imageSize = tgaFile->width * tgaFile->height * colorMode; |
|
|
|
|
imageSize = static_cast<long int>(tgaFile->width * tgaFile->height * static_cast<long unsigned int>(colorMode & 32)); |
|
|
|
|
|
|
|
|
|
// Allocate memory for the image data.
|
|
|
|
|
tgaFile->imageData = (unsigned char*)malloc(sizeof(unsigned char)*imageSize); |
|
|
|
|
tgaFile->imageData = static_cast<unsigned char*>(malloc(sizeof(unsigned char) * static_cast<long unsigned int>(imageSize))); |
|
|
|
|
|
|
|
|
|
// Read the image data.
|
|
|
|
|
fread(tgaFile->imageData, sizeof(unsigned char), imageSize, filePtr); |
|
|
|
|
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) { |
|
|
|
|