0

I read in the .tga header and the image data block and want to write it into a new file, but it won't and I'm out of ideas on what could be wrong. It's probably something simple, that I keep missing out on.

Help would be very appreciated.

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();
}

It worked when the contents of tgaHelperData were in the tgaFile struct. Now that I split it up, it won't work anymore, I can't open the output file and I'm confused now

Cpt. Crazy
  • 115
  • 1
  • 11
  • Compare the input and output files byte by byte. There are tools that will do that. Figure out where the differences are, then look at the specific part of the code that writes out that part of the file, and you will have your answer. – Sam Varshavchik Nov 16 '19 at 12:23
  • What is `TGAHELPERDATAIme`? `TGAHELPERDATA` read from file is discarded – user7860670 Nov 16 '19 at 12:28
  • @SamVarshavchik I compared the file within the command prompt by using `fc /b "pathOne" "pathTwo`, and it says that the original file is longer than the written one, which could make sense since I left out unimportant meta data after the image data block – Cpt. Crazy Nov 16 '19 at 12:32
  • @VTT Uh, made a mistake right there. Should've been `TGAHELPERDATA`. Fixed it. Why is the struct `TGAHELPERDATA` discarded? Or did you say that, bc 'safeImage(...)' got `TGAHELPERDATAIme' as a parameter, which was a mistake (not in my original code though, so that wasn't it) – Cpt. Crazy Nov 16 '19 at 12:38
  • Well, maybe that "unimportant meta data" isn't as unimportant as you think? – Sam Varshavchik Nov 16 '19 at 12:41
  • @SamVarshavchik but why did it work before? I haven't used that meta data back than either – Cpt. Crazy Nov 16 '19 at 12:42
  • I mean that `TGAHELPERDATA tgaHelperData;` from `loadImage` is discarded and it is not clear where are you getting it from when invoking `safeImage` – user7860670 Nov 16 '19 at 12:51
  • Well, you do have source control, don't you? Revert to the previous version of your code, run it, save the output file, and compare it, byte by byte, with what you have now. If you haven't been using source control, make a mental note to change that, then temporarily put back the code that wrote the "unimportant metadata" back in, run your code and fix whatever needs to be fixed, until the output is byte-identical, then remove the "unimportant metadata" part, and see what happens. – Sam Varshavchik Nov 16 '19 at 12:51
  • @VTT I call the `safeImage` function within the `loadImage` function. At first the `safeImage`funciton is processed and then `tgaHelperData` is discarded – Cpt. Crazy Nov 16 '19 at 12:56
  • @SamVarshavchik I went back to when it worked, and then kind of reversed back by hand to what I thought I had before (the code above) and now it seems to work. No idea what changed xD – Cpt. Crazy Nov 16 '19 at 13:15

0 Answers0