C ++:我想编写以前读取的.tga文件

时间:2019-11-16 12:20:09

标签: c++ ofstream tga

我读了.tga标头和图像数据块,并希望将其写入新文件,但是不会,而且我对可能出问题的主意不大。这可能很简单,我一直很想念。

我们将不胜感激。

struct TGAFILE {
    char idLength;
    char colourMapType;
    char imageType;
    short colourMapStart;
    short colourMapNumEntries;
    short xOrigin;
    short yOrigin;
    char bitsPerPixel;
    char* pixelData;
};

struct TGAHELPERDATA {
    short width;
    short height;
    char bitsPerEntry;
    char descriptor;
};

TGAFILE loadImage(const std::string& pathToImage) {
    std::ifstream image(pathToImage, std::ios::binary);
    if (!image.is_open()) {
        throw FileNotFound();
    }
    TGAFILE tgaFile;
    TGAHELPERDATA tgaHelperData;
    image.read(&tgaFile.idLength, sizeof(tgaFile.idLength));
    image.read(&tgaFile.colourMapType, sizeof(tgaFile.colourMapType));
    image.read(&tgaFile.imageType, sizeof(tgaFile.imageType));
    image.read((char*)(&tgaFile.colourMapStart), sizeof(tgaFile.colourMapStart));
    image.read((char*)(&tgaFile.colourMapNumEntries), sizeof(tgaFile.colourMapNumEntries));
    image.read(&tgaHelperData.bitsPerEntry, sizeof(tgaHelperData.bitsPerEntry));
    image.read((char*)(&tgaFile.xOrigin), sizeof(tgaFile.xOrigin));
    image.read((char*)(&tgaFile.yOrigin), sizeof(tgaFile.yOrigin));
    image.read((char*)(&tgaHelperData.width), sizeof(tgaHelperData.width));
    image.read((char*)(&tgaHelperData.height), sizeof(tgaHelperData.height));
    image.read(&tgaFile.bitsPerPixel, sizeof(tgaFile.bitsPerPixel));
    image.read(&tgaHelperData.descriptor, sizeof(tgaHelperData.descriptor));

     int imageDataSize = tgaHelperData.width * tgaHelperData.height * (tgaFile.bitsPerPixel / 8);

    tgaFile.pixelData = new char[imageDataSize];

    int originalPosition = (int)image.tellg();
    image.read(tgaFile.pixelData, imageDataSize);
    safeImage(tgaFile, tgaHelperData, imageDataSize);
    return tgaFile;
}

void safeImage(TGAFILE tgaFile, TGAHELPERDATA tgaHelperData, int imageDataSize){
    std::ofstream output("output.tga", std::ios::binary);
    output.write(&tgaFile.idLength, sizeof(tgaFile.idLength));
    output.write(&tgaFile.colourMapType, sizeof(tgaFile.colourMapType));
    output.write(&tgaFile.imageType, sizeof(tgaFile.imageType));
    output.write(reinterpret_cast<const char*>(&tgaFile.colourMapStart), sizeof(tgaFile.colourMapStart));
    output.write(reinterpret_cast<const char*>(&tgaFile.colourMapNumEntries), sizeof(tgaFile.colourMapNumEntries));
    output.write(&tgaHelperData.bitsPerEntry, sizeof(&tgaHelperData.bitsPerEntry));
    output.write(reinterpret_cast<const char*>(&tgaFile.xOrigin), sizeof(tgaFile.xOrigin));
    output.write(reinterpret_cast<const char*>(&tgaFile.yOrigin), sizeof(tgaFile.yOrigin));
    output.write(reinterpret_cast<const char*>(&tgaHelperData.width), sizeof(tgaHelperData.width));
    output.write(reinterpret_cast<const char*>(&tgaHelperData.height), sizeof(tgaHelperData.height));
    output.write(&tgaFile.bitsPerPixel, sizeof(tgaFile.bitsPerPixel));
    output.write(&tgaHelperData.descriptor, sizeof(tgaHelperData.descriptor));
    output.write(tgaFile.pixelData, imageDataSize);
    output.close();
}

tgaHelperData的内容在tgaFile struct中时,它起作用。现在,我将其拆分,将无法再使用,无法打开输出文件,现在感到困惑

0 个答案:

没有答案