Hello,
I have searched all over the forums on Gamedev.net (using the nifty search function
). I have yet to find a resolve to my problem.
Well here goes. I got the uncompressed TGA to load ggrrreeeaaaatttt! It's the compressed that's making me mad. When I run the program with a compressed image I get a Degug Error that says DAMAGE: after Normal block (#18) at 0x006F5294.
When I debug the application the error code the compiler finds is in DbgHeap.c
/* forced failure */
if (!(*_pfnAllocHook)(_HOOK_FREE, pUserData, 0, nBlockUse, 0L, NULL, 0))
{
_RPT0(_CRT_WARN, "Client hook free failure.\n");
return;
}
Here is my code for the compressed TGA. It's essentially based on NeHe's code and
this websites code.
// Gets the tga image data if it is compressed
int GLImage::tgaGetCompressedData()
{
// Allocate memory for the image data
tgaImageData = (unsigned char*)malloc(tgaImageSize);
// Check to make sure the allocation of memory was a success
if(tgaImageData == NULL)
return 0;
int currentPixel = 0; // Current pixel that is being read from data
int currentByte = 0; // Current byte we are writing into image data
// Storage space for one pixel
unsigned char * colorBuffer = (unsigned char *)malloc(tgaImageBytes);
// Begin the uncompression loop
do
{
int chunkHeader = 0; // Variables to store the value of te ID chunk
// Attemp to read the chunk's header
if(fread(&chunkHeader, sizeof(int), 1, file) == NULL)
{
if(tgaImageData != NULL)
free(tgaImageData);
if(colorBuffer != NULL)
free(colorBuffer);
return 0;
}
if(chunkHeader < 128) // If the chunk is a 'RAW' chunk
{
chunkHeader++; // Add on to the value to get the total number of RAW pixels
// Start RAW pixel reading loop
for(short i = 0; i < chunkHeader; i++)
{
// Try to read one pixel and if it fails return false
if(fread(colorBuffer, 1, tgaImageBytes, file) != (size_t)tgaImageBytes)
{
if(tgaImageData != NULL)
free(tgaImageData);
if(colorBuffer != NULL)
free (colorBuffer);
return 0;
}
tgaImageData[currentByte] = colorBuffer[2]; // Write the 'R' byte
tgaImageData[currentByte + 1] = colorBuffer[1]; // Write the 'G' byte
tgaImageData[currentByte + 2] = colorBuffer[0]; // Write the 'B' byte
// If the image is 32bpp
if(tgaImageBytes == 4)
tgaImageData[currentByte + 3] = colorBuffer[3]; // Write the 'A' byte
currentByte += tgaImageBytes; // Increment the byte counter
currentPixel++; // Increment the pixel counter
// If the program read to many pixels
if(currentPixel > tgaImagePixels)
{
if(tgaImageData != NULL)
free(tgaImageData);
if(colorBuffer != NULL)
free (colorBuffer);
return 0;
}
}
}
// If it's an RLE header
else
{
chunkHeader -= 127; // Subtract 127 to get rid of the ID bit
// Read the next pixel and check if an error occurred
if(fread(colorBuffer, 1, tgaImageBytes, file) != (size_t)tgaImageBytes)
{
if(tgaImageData != NULL)
free(tgaImageData);
if(colorBuffer != NULL)
free (colorBuffer);
return 0;
}
// Start the RLE pixel reading loop
for(short j = 0; j < chunkHeader; j++)
{
tgaImageData[currentByte] = colorBuffer[2]; // Write the 'R' byte
tgaImageData[currentByte + 1] = colorBuffer[1]; // Write the 'G' byte
tgaImageData[currentByte + 2] = colorBuffer[0]; // Write the 'B' byte
// If the image is 32bpp
if(tgaImageBytes == 4)
tgaImageData[currentByte + 3] = colorBuffer[3]; // Write the 'A' byte
currentByte += tgaImageBytes; // Increment the byte counter
currentPixel++; // Increment the pixel counter
// If the program read to many pixels
if(currentPixel > tgaImagePixels)
{
if(tgaImageData != NULL)
free(tgaImageData);
if(colorBuffer != NULL)
free (colorBuffer);
return 0;
}
}
}
} while(currentPixel < tgaImagePixels);
// Release the colorBuffer memory
free(colorBuffer);
return 1;
}
If it is of any use I generate the RLE compression using the GIMP image editing program.
ThAnKs in AdVaNcE!
Edited by - Viscous-Flow on October 22, 2001 12:23:19 AM