如何解决为什么我的 TGA 文件以数字代码打开而不是显示我的图像?
我正在使用 C++ 进行个人项目,并且正在尝试写入 TGA 文件。我在读取/打开输入文件时没有问题,但是当我打开生成的输出文件时,这是它显示的内容片段: TGA displaying hex code
我创建了我的代码的一小部分测试程序,它读入一个 TGA 文件并立即将其写回作为一个新的 TGA 文件。这是那个部分:
//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();
因为这可能是我的 fileRead 和 fileWrite 函数的问题,所以我也会附上它们:
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++;
}
}
}
我还想指出,我为图像标题(代码示例中的头部)和图像主体(代码示例中的图像数据)创建了结构。上述函数所在的 Image 类包含一个名为 head 的 Header 对象和一个名为 imageData 的 ImageData 对象。我真的不知道问题是什么,因为我过去使用过这些功能并且它们运行良好。如果您在我的代码中发现任何错误,或者您是否遇到过对您有用的问题,请告诉我。谢谢!
解决方法
我终于弄清楚了我的问题。我的读写函数只包含 b、g 和 r 的像素,但许多 TGA 文件每像素有 32 位,并且每个像素需要第 4 个数据通道。第 4 个通道称为 Alpha 通道。修复要求我简单地添加一行以在 alpha 通道中读写。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。