0

I thought I was confused before I had this "clarification": check50 gives confusing results for pset4 resize. Now I'm flat out lost. For this challenge I actually tried to use a function to hold all the resizing routines. It was the first time I did that and, although it took eons, my image gets resized perfectly. That was my thought until I ran check50. I get the same a lot of people are getting:

$ check50 2015.fall.pset4.resize bmp.h resize.c :) 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/c930c871bca54934a5598f8d74d59fef

I have tried so many different combinations of old / new Padding, abs(), non abs() values... I ALWAYS get the same results from check50 (above). I have declared all variables before main(), along with the resize() function prototype. My main() begins like this:

if (argc != 4){code...}

size = atoi(argv[1]);

if (size < 1 || size > 100){code....}

infile = argv[2];
outfile = argv[3];

resizer(); goes before fclose(inptr); inside main(). I know the problem is the padding, but I'm all out of ideas on how to fix it. I need help. Here is my resizing code:

    void resizer(void)
{
    /*
    * INITIALIZING VARIABLES
    */

    // vars to hold input width / height / padding values
    inWidth = bi.biWidth;
    inHeight = bi.biHeight;
    inPadding =  (4 - (inWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    // vars to hold output width / height / padding values
    height = (bi.biHeight *= size); //update bi.biHeight to the value of itself times size, then assign result to height
    width = (bi.biWidth *= size); //update bi.biWidth to the value of itself times size, then assign result to height
    padding =  (4 - (width * sizeof(RGBTRIPLE)) % 4) % 4;

    // vars to hold bitmap's image size and file size
    sizeImage = bi.biSizeImage; 
    sizeFile = bf.bfSize;

    // vars to hold absolute input / output height / width
    absHeight = abs(height);
    absInHeight = abs(inHeight);
    absWidth = abs(width);

    // update image size and file size (has to be in this order)
    sizeImage = absHeight * ((absWidth * sizeof(RGBTRIPLE)) + padding); // absWidth instead of width
    sizeFile = sizeImage + bf.bfOffBits; 

    /*
    * END INITIALIZING VARIABLES
    */

    // write output BITMAPFILEHEADER and output BITMAPINFOHEADER (has to be in this order)
    fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
    fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

    // iterate over infile's scanlines, abs(bi.biHeight) times
    for (int i = 0; i < absInHeight; i++) //LQ
    {    
        // write each line size times
        for (int sizeWrapper = 0; sizeWrapper < size; sizeWrapper++)
        {
            fseek(inptr, bf.bfOffBits + i * (inPadding + (inWidth * sizeof(RGBTRIPLE))), SEEK_SET);

            // iterate over pixels in scanline, inWidth times
            for (int j = 0; j < inWidth; j++)
            {
                // buffer for temporary swap of new scanline 
                RGBTRIPLE triple;

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

                // write pixels to buffer, size times
                for (int k = 0; k < size; k++)
                {
                    // write buffer to outfile
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr); // 1 instead of width
                }
            }

            // skip over input padding, if any
            fseek(inptr, inPadding, SEEK_CUR); //LQ

            // then add it back (to demonstrate how)
            for (int l = 0; l < padding; l++)
            {
                fputc(0x00, outptr);
            }
        }
    }
}
2
  • 1
    You don't update bi.biSizeImage and bf.bfSize
    – Blauelf
    Nov 18 '16 at 17:59
  • Thanks, @Blauelf, I see the problem now. Nov 18 '16 at 21:07
1

You'll see what your problem is if you do this:

enter image description here

Think about it for a while and come back here again if this doesn't make it click for you... and I can add other tips.

2
  • So, the problem IS the header, not the padding??? offset type name argv[1] argv[2] 2 DWORD bfSize 0000005a 000001e6 34 DWORD biSizeImage 00000024 000001b0 Nov 18 '16 at 20:35
  • 1
    Exactly. You even calculate sizeImage and sizeFile correctly, but you're forgetting to put those inside bf and bi before writing these two into the outfile Nov 18 '16 at 20:40

You must log in to answer this question.

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