-1

我正在使用 C++ 进行个人项目,并且正在尝试写入 TGA 文件。我在读取/打开输入文件时没有问题,但是当我打开生成的输出文件时,这是它显示的片段: TGA 显示十六进制代码

我创建了我的代码的一小部分测试器,它读入一个 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++;
        }
    }
}
      

我还想指出,我为图像头(head,在代码示例中)和图像主体(imageData,在代码示例中)创建了结构。上述函数所在的 Image 类包含一个名为 head 的 Header 对象和一个名为 imageData 的 ImageData 对象。我真的不知道问题出在哪里,因为我过去曾使用过这些功能并且它们运行良好。如果您在我的代码中发现任何错误,或者您是否遇到过对您有用的问题,请告诉我。谢谢!

4

1 回答 1

-1

我最终弄清楚了我的问题。我的读取和写入函数只包含 b、g 和 r 的像素,但许多 TGA 文件每个像素有 32 位,并且每个像素需要第 4 个数据通道。第 4 个通道称为 Alpha 通道。修复要求我简单地在 alpha 通道中添加一行来读写。

于 2021-07-12T13:48:37.447 回答