compressed tga loder problem

Started by
-1 comments, last by supagu 20 years, 2 months ago
Having some issues getting my compressed tga loader working. I get half the image properly displayed, then the other half is all black :-/ this function is called after reading the header and determining if its compressed: bool TGA::LoadCompresed( FILE* pFile, TGAHead& header ) { if( header.width <= 0 || header.height <= 0 || header.bpp < 24 ) { printf("something wrong bpp: %i width: %i height: %i\n", header.bpp, header.width, header.height); return false; } char* image = 0; unsigned int bytesPerPixel = header.bpp / 8; image = (char*)malloc( header.width * header.height * bytesPerPixel ); if( !image ) { printf( "failed to alloc mem\n"); return false; } int curByte = 0; int pixelsRead = 0; int noPixels = header.width * header.height; char* pixel = (char*)malloc( bytesPerPixel ); char chunkHead; do { if( fread( &chunkHead, sizeof(char), 1, pFile ) == 0 ) { printf( "failed to from file (chunkHead)\n"); return false; } if( (chunkHead >> 7) == 0x0 ) //RAW - not compressesd { ++chunkHead; //increase by one to determine how many pixels follow for(short counter = 0; counter < chunkHead; ++counter, curByte += bytesPerPixel, ++pixelsRead ) { //Read 1 Pixel if( fread( pixel, 1, bytesPerPixel, pFile) != bytesPerPixel ) { printf( "failed to from file (pixel RAW)\n"); return false; } image[ curByte + 0 ] = pixel[2]; image[ curByte + 1 ] = pixel[1]; image[ curByte + 2 ] = pixel[0]; if( bytesPerPixel >= 4 ) image[ curByte + 3 ] = pixel[3]; } } else //RLE - compressed { chunkHead = chunkHead ^ 128; //drop the 1 at start ++chunkHead; //Read 1 Pixel if( fread( pixel, 1, bytesPerPixel, pFile) != bytesPerPixel ) { printf( "failed to from file (pixel RLE)\n"); return false; } pixelsRead += (unsigned int)chunkHead; for(short counter = 0; counter < chunkHead; ++counter, curByte += bytesPerPixel, ++pixelsRead ) { image[ curByte + 0 ] = pixel[2]; image[ curByte + 1 ] = pixel[1]; image[ curByte + 2 ] = pixel[0]; if( bytesPerPixel >= 4 ) image[ curByte + 3 ] = pixel[3]; } } } while( pixelsRead < noPixels ); if( header.bpp == 32 ) GenerateTexture( image, header.width, header.height, GL_RGBA ); else GenerateTexture( image, header.width, header.height, GL_RGB ); free(image); free(pixel); return true; }

This topic is closed to new replies.

Advertisement