0

I have a few problems with my code but my main problem right now is being unable to even open the resized image. I think the problem is with writing updated header info to the outfile. I've thrown in my code here, any help would be greatly appreciated.

BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

//duplicate header files for outfile
BITMAPINFOHEADER obi;
fread(&obi, sizeof(BITMAPINFOHEADER), 1, inptr);
BITMAPFILEHEADER obf;
fread(&obf, sizeof(BITMAPFILEHEADER), 1, inptr);

obi.biWidth = bi.biWidth * n;
obi.biHeight = bi.biHeight * n;
int opadding = (4 - (obi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
obi.biSizeImage = (obi.biWidth * sizeof(RGBTRIPLE) + opadding) * abs(obi.biHeight);
obf.bfSize = obi.biSizeImage + 54; 

// write outfile's headers
fwrite(&obf, sizeof(BITMAPFILEHEADER), 1, outptr);
fwrite(&obi, sizeof(BITMAPINFOHEADER), 1, outptr);

for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    //resize vertically
    for (int vert = 0; vert < n; vert++)
    {
        fseek(inptr, i * (bi.biWidth * sizeof(RGBTRIPLE) + padding) + 54, SEEK_SET);

        // iterate over pixels in scanline
        for (int j = 0; j < bi.biWidth; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            // write RGB triple to outfile
            fwrite(&triple, sizeof(RGBTRIPLE), n, outptr); // write 'n' times of the pixel to outfile 
        }

        // add new padding value to outfile
        for (int k = 0; k < opadding; k++)
        {
            fputc(0x00, outptr);
        }

    }    

    // skip over old padding, if any
    fseek(inptr, padding, SEEK_CUR);

}
2
  • For starters, obf.bbFize is not calculated correctly. Padding appears to have only been calculated once. It needs to be calculated for both the input and output files. Putting that aside, the "invalid or unsupported image" error usually indicates a serious problem with the headers. It's more likely that something elsewhere in the code has affected the headers. There could be other issues causing problems, but there's not enough of the code posted to diagnose the problem. – Cliff B Aug 16 '16 at 21:53
  • Thanks Cliff, I added in the code that resizes vertically and horizontally. Maybe that might show where else I'm going wrong? I'd originally declared the padding for the infile below but didn't include that in the original question. I thought that the infiles padding wasn't actually used in writing to the outfiles headers as the padding is increased when the biWidth is changed. opadding is the int i declared that calculates the outfile padding using the new biWidth. – Lewis Aug 17 '16 at 1:37
2

Duh! Read right through it and missed the problem myself. Should have seen it earlier.

The problem is that the code reads from the input file to populate the two headers for the input file, and then reads again to populate the output file headers. (I won't even mention that they were read in the wrong order.) BUT, since it doesn't reset to the beginning of the file, it's populating the output file headers with garbage and leaving the input file pointer well into the image data.

You need to either reset the input file pointer back to the start of the file, or, preferably, just copy the input file headers to the output file headers. FYI, structures can be copied in their entirety very easily, as in obf = bf;.

If this answers your question, please click on the check mark to accept. Let's keep up on forum mainteance. ;-)

1
  • I think I get it now. Thanks Cliff! – Lewis Aug 17 '16 at 1:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .