Original Post
ok this is a error with a .tga loader function i have written. The problem i am haveing is pointer related, I think. First off is the code (tga struct and function). Now the problem is after 18 loops though this part of the code: On the 18th loop after the call to read the debugger reads the pointer Colors as being a bad pointer. It also then gives CXX0030 error. personaly i am confused as to how this can happen when the past 17 loops worked fine and despite my best efforts i can come up with any further details or ideas as to what i am doing wrong here and any help is welcome.
#define TGA_RGB 2 // This tells us it's a normal RGB (really BGR) file
#define TGA_A 3 // This tells us it's a ALPHA file
#define TGA_RLE 10 // This tells us that the targa is Run-Length Encoded (RLE)
typedef unsigned char byte;
struct TGAFILE {
unsigned short int ImageWidth;
unsigned short int ImageHight;
unsigned short int channels;
unsigned char *Image;
};
TGAFILE *LoadTGA(const char *filename);
TGAFILE *LoadTGA(const char *filename) {
TGAFILE * tgafile = NULL;
std::ifstream read;
byte length, ImageType, bits;
WORD width = 0, height = 0;
int stride = 0, channels = 0, i = 0;
read.open(filename, std::ios::binary);
tgafile = new TGAFILE;
read.read((char *)&length, sizeof(length));
read.ignore(1);
read.read((char *)&ImageType, sizeof(ImageType));
read.ignore(9);//ignore everything until we get to width
read.read((char *)&width, sizeof(width));
read.read((char *)&height, sizeof(height));
read.read((char *)&bits, sizeof(bits));
read.ignore(length + 1);//now move to the image data
if(ImageType != TGA_RLE) {
if(bits == 24 || bits == 32) {
channels = bits/8;
stride = channels * width;
tgafile->Image = new byte[stride * height];
int x, y;
//read the data into row data
for(y = 0; y < height; y++) {
//get the current line of the image we are on
byte * line = &(tgafile->Image[stride * y]);
//now read the image data into the line buffer
for(x = 0; x < stride; x++) {
read.read((char *)&line[x], sizeof(byte));
}//end x for
//now swap the red and blue values so its in RBG format
for(i = 0; i < stride; i += channels) {
int temp = line;
line = line[i + 2];
line[i + 2] = temp;
}//end i for
}//end y for
}//end bits == 24 || bits == 32 if
else if(bits == 16) {
unsigned short pixels = 0;
int r = 0, g = 0, b = 0;
channels = bits/8;
stride = channels * width;
tgafile->Image = new byte[stride * height];
for(i = 0; i < (width * height); i++) {
//read data a pixel at a time and then swap the values
read.read((char *)&pixels, sizeof(pixels));
b = (pixels & 0x1f) << 3;
g = ((pixels >> 5) & 0x1f) << 3;
r = ((pixels >> 10) & 0x1f) << 3;
//now assigne them to the image data
tgafile->Image[i * 3 + 0] = r;
tgafile->Image[i * 3 + 1] = g;
tgafile->Image[i * 3 + 2] = b;
}//end i for
}//end bits == 16 else if
//if its not any of the above then its a unknown
// type and we must exit
else {
return NULL;
}//end else
}//end ImageType != TGA_RLE if
else {//if it gets here then the file is RLE
byte rleID = 0;
int colorsRead = 0;
channels = bits/8;
stride = channels * width;
bool iseof = false;
tgafile->Image = new byte[stride * height];
byte * Colors = new byte[channels];
//now load the data
while(i < (width * height)) {
read.read((char *)&rleID, sizeof(rleID));
if(rleID < 128) {
rleID++;
while(rleID) {
read.read((char *)&Colors, (sizeof(byte) * channels));
// Store the current pixel in our image array
tgafile->Image[colorsRead + 0] = Colors[2];
tgafile->Image[colorsRead + 1] = Colors[1];
tgafile->Image[colorsRead + 2] = Colors[0];
// If we have a 4 channel 32-bit image, assign one more for the alpha
if(bits == 32) {
tgafile->Image[colorsRead + 3] = Colors[3];
}//end bits == 32 if
// Increase the current pixels read, decrease the amount
// of pixels left, and increase the starting index for the next pixel.
i++;
rleID--;
colorsRead += channels;
}//end rleID while
}//end rleID < 128 if
else {
// Minus the 128 ID + 1 (127) to get the color count that needs to be read
rleID -= 127;
read.read((char *)&Colors, (sizeof(byte) * channels));
while(rleID) {
// Assign the current pixel to the current index in our pixel array
tgafile->Image[colorsRead + 0] = Colors[2];
tgafile->Image[colorsRead + 1] = Colors[1];
tgafile->Image[colorsRead + 2] = Colors[0];
// If we have a 4 channel 32-bit image, assign one more for the alpha
if(bits == 32)
tgafile->Image[colorsRead + 3] = Colors[3];
// Increase the current pixels read, decrease the amount
// of pixels left, and increase the starting index for the next pixel.
i++;
rleID--;
colorsRead += channels;
}//end rleID while
}//end rleID < 128 else
}//end i while
}//end ImageType == TGA_RLE else
read.close();
tgafile->channels = channels;
tgafile->ImageWidth = width;
tgafile->ImageHight = height;
return tgafile;
}//end LoadTGA
while(rleID) {
read.read((char *)&Colors, (sizeof(byte) * channels));
iseof = read.eof();
// Store the current pixel in our image array
tgafile->Image[colorsRead + 0] = Colors[2];
tgafile->Image[colorsRead + 1] = Colors[1];
tgafile->Image[colorsRead + 2] = Colors[0];
// If we have a 4 channel 32-bit image, assign one more for the alpha
if(bits == 32) {
tgafile->Image[colorsRead + 3] = Colors[3];
}//end bits == 32 if
// Increase the current pixels read, decrease the amount
// of pixels left, and increase the starting index for the next pixel.
i++;
rleID--;
colorsRead += channels;
}//end rleID while