I am working on a personal project using C++, and I am trying to write to a TGA file. I have no issues reading/opening the input file, but when I open the output file that I generate this is a snippet of what it displays: TGA displaying hex code
I created a small tester portion of my code, which reads in a TGA file and immediately writes it back out as a new TGA file. This is that section:
//Create TRL template object image and TRL output object image
Pixel pix;
TRLImage trlTemp;
TRLImage trlData;
trlData = trlTemp;
//Load and read data for the TRL template image
ifstream trlTemplate("input/TRL template.tga", ios_base::binary);
trlTemp.fileRead(trlTemplate, pix);
ofstream test("output/test.tga", ios_base::binary);
trlTemp.fileWrite(test);
test.close();
Since it is probably an issue with my fileRead and fileWrite functions, I will attach those too:
void Image::fileRead(ifstream& inFile, Pixel& pix)
{
//read in Header
inFile.read((char*)&head.idLength, sizeof(head.idLength));
inFile.read((char*)&head.colorMapType, sizeof(head.colorMapType));
inFile.read((char*)&head.dataTypeCode, sizeof(head.dataTypeCode));
inFile.read((char*)&head.colorMapOrigin, sizeof(head.colorMapOrigin));
inFile.read((char*)&head.colorMapLength, sizeof(head.colorMapLength));
inFile.read((char*)&head.colorMapDepth, sizeof(head.colorMapDepth));
inFile.read((char*)&head.xOrigin, sizeof(head.xOrigin));
inFile.read((char*)&head.yOrigin, sizeof(head.yOrigin));
inFile.read((char*)&head.width, sizeof(head.width));
inFile.read((char*)&head.height, sizeof(head.height));
inFile.read((char*)&head.bitsPerPixel, sizeof(head.bitsPerPixel));
inFile.read((char*)&head.imageDescriptor, sizeof(head.imageDescriptor));
//read in ImageData
for (int i = 0; i < head.height; i++)
{
for (int j = 0; j < head.width; j++)
{
inFile.read((char*)&pix.b, sizeof(pix.b));
inFile.read((char*)&pix.g, sizeof(pix.g));
inFile.read((char*)&pix.r, sizeof(pix.r));
imageData.pixels.push_back(pix);
}
}
}
void Image::fileWrite(ofstream& outFile)
{
outFile.write((char*)&head.idLength, sizeof(head.idLength));
outFile.write((char*)&head.colorMapType, sizeof(head.colorMapType));
outFile.write((char*)&head.dataTypeCode, sizeof(head.dataTypeCode));
outFile.write((char*)&head.colorMapOrigin, sizeof(head.colorMapOrigin));
outFile.write((char*)&head.colorMapLength, sizeof(head.colorMapLength));
outFile.write((char*)&head.colorMapDepth, sizeof(head.colorMapDepth));
outFile.write((char*)&head.xOrigin, sizeof(head.xOrigin));
outFile.write((char*)&head.yOrigin, sizeof(head.yOrigin));
outFile.write((char*)&head.width, sizeof(head.width));
outFile.write((char*)&head.height, sizeof(head.height));
outFile.write((char*)&head.bitsPerPixel, sizeof(head.bitsPerPixel));
outFile.write((char*)&head.imageDescriptor, sizeof(head.imageDescriptor));
int count = 0;
for (short i = 0; i < head.height; i++)
{
for (short j = 0; j < head.width; j++)
{
outFile.write((char*)&imageData.pixels[count].b, sizeof(imageData.pixels[count].b));
outFile.write((char*)&imageData.pixels[count].g, sizeof(imageData.pixels[count].g));
outFile.write((char*)&imageData.pixels[count].r, sizeof(imageData.pixels[count].r));
count++;
}
}
}
I also want to note that I have structs created for the image header (head, in the code samples) and image body (imageData, in the code samples). The Image class where the above functions are from contains a Header object called head and an ImageData object called imageData. I really have no idea what the issue is because I have used these functions in the past and they worked fine. Please let me know if you catch any errors in my code, or if you have experienced this issue what worked for you. Thanks!