TGA file loader...error reading
I'm working on a .tga file loader class, and I keep running into problems when I try to read the actual image data.
Everything seems to be fine before that point; I get correct values for image width and height (in this case 256 x 256) and I get 24 bit pixel depth.
So when I try to read the image data I use two loops, which should loop 256*256 times (altogether), or is there something wrong with that logic?
I read 3 bytes (24 bits/8 bits/byte) each time, so I should read (256*256)*3 bytes total, correct? That comes out to 196,608 bytes, and the file is 196,652 bytes long.
As far as I can tell, I am only reading about 18 bytes before I get to the image data, but my loop always fails to read at about 9,600 or so reads (28k bytes). This is how I am allocating and reading the image data:
Code:
int BytesToRead = (header.ImgSpec.PixDepth)/8;
MapData.pixels = new int *[header.ImgSpec.ImgWidth];
for (int i=0;i<header.ImgSpec.ImgWidth+1;i++)
MapData.pixels[i] = new int[header.ImgSpec.ImgHeight];
for (int i = 0;i<header.ImgSpec.ImgWidth;i++)
{
for (int x=0;x<header.ImgSpec.ImgHeight;x++)
{
if ( _read( fl, &TempMapData,BytesToRead)<1)//read successful?
throw TgaErr(2); //read was not successful!
MapData.pixels[i][x] = TempMapData;
TempMapData = 0;
}
}
Now as I look at it I realize I might not be checking correctly to see if the read fails, but I'm fairly sure that will work...It ends up throwing an exception every time, which the function which called this one catches, and returns an error...
I've worked through this in console mode using cout statements at each read to see what they turn up, and theres about 10 before this one, but none of them go above 2 bytes as far as i know.