0

My resize program seems to work just like the staff version, the outputs being identical (checked with peek too). However, check50 still seems to detect some error: :) resize.c and bmp.h exist

:) resize.c compiles

:( doesn't resize 1x1-pixel BMP when n is 1

:( resizes 1x1-pixel BMP to 2x2 correctly when n is 2

:( resizes 1x1-pixel BMP to 3x3 correctly when n is 3

:) resizes 1x1-pixel BMP to 4x4 correctly when n is 4

:( resizes 1x1-pixel BMP to 5x5 correctly when n is 5

:) resizes 2x2-pixel BMP to 4x4 correctly when n is 2

https://sandbox.cs50.net/checks/b1b8b35e55d1494e8b1a72fd21772d7a

This is my code:

//copy file headers
BITMAPFILEHEADER of = bf;
BITMAPINFOHEADER oi = bi;

//alter height and width for output file
oi.biHeight = bi.biHeight *n;
oi.biWidth = bi.biWidth *n;

//determine padding
int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int outpad = (4 - (oi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

//correcting size variables
oi.biSizeImage = ((oi.biWidth * abs(oi.biHeight)) * sizeof(RGBTRIPLE)) + (outpad * abs(oi.biHeight));
of.bfSize = bf.bfOffBits + oi.biSizeImage;

// write outfile's BITMAPFILEHEADER
fwrite(&of, sizeof(BITMAPFILEHEADER), 1, outptr);

// write outfile's BITMAPINFOHEADER
fwrite(&oi, sizeof(BITMAPINFOHEADER), 1, outptr);

// determine padding for scanlines


// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    for(int y = 0; y<n; y++){
    // 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
        for(int z = 0; z<n; z++)
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
    }

    // skip over padding, if any

    fseek(outptr, outpad, SEEK_CUR);

    // then add it back (to demonstrate how)
    for (int k = 0; k < outpad; k++)
    {
        fputc(0x00, outptr);
    }
    if(y != n-1){
        fseek(inptr, -bi.biWidth*sizeof(RGBTRIPLE), SEEK_CUR);
    }else{
        fseek(inptr, padding, SEEK_CUR);
    }
}

}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}

I've been having serious trouble with this assignment, so I likely went over a simple mistake. I just cannot understand what's wrong if the outputs are identical to the staff's. Thank you

1 Answer 1

0

It turns out that your program isn't working fine. While the headers are processing correctly, the image area has problems. Have you compared the image content of your files and the output from the staff version using xxd? You'll see that not only is the content different, the amount of data is different.

Looking through your code, I saw this:

        fseek(outptr, outpad, SEEK_CUR);

Why add a line to skip forward in the output file? At the point that this executes, the pointer is at the end of the file. This has the effect of inserting extra bytes into the file, but doesn't guarantee what those bytes will be. If the intent was to skip the padding in the input file, think carefully about that, and what else you're doing in the input file.

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

1
  • Solved it, thanks. Remains of the original file, copy.c. Thanks for your help Mar 14, 2016 at 20:18

You must log in to answer this question.

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