I'm struggling with resize.c for quite some time. I've come to the point where it perfectly resizes sometimes (for example, small.bmp with factor 5) but sometimes it doesnt(for example, small.bmp with factor 2 or 3 or 4). Check 50 gives me this:
:) 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

It seems to me that I have no problems with the headers, because when I compare the headers in my output with the ones in the staff implementation's output, they're identical. My guess is that I have some problems with padding calculation or writing. Here's some of my code:

//create header for the new bmp
bfNew = bfOrig;

biNew = biOrig;

//make neccessary changes to the header parameters
biNew.biWidth = biOrig.biWidth * n;
biNew.biHeight = biOrig.biHeight * n;

//calculate padding of original and new file
int paddingOrig =  (4 - (biOrig.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;    
int paddingNew = (4 - (biNew.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;  

//with the padding calculated, now I can calculate new image size
biNew.biSizeImage = (biNew.biWidth * sizeof(RGBTRIPLE) + paddingNew) * abs(biNew.biHeight);
bfNew.bfSize =   biNew.biSizeImage + 54;      

//write the headers in new file
fwrite(&bfNew, sizeof(BITMAPFILEHEADER), 1, output);
fwrite(&biNew, sizeof(BITMAPINFOHEADER), 1, output);

int i, j, k, v, h;

for (i = 0; i < abs(biOrig.biHeight); i++)

//print out the row n times (resize verticaly)
   for (h = 0; h < n; h++)

    //move the file pointer in input one row back, so I can print out the same row again
        if (h != 0)
            fseek(input, -((biOrig.biWidth * sizeof(RGBTRIPLE)) + paddingNew), SEEK_CUR);

        //print out every pixel n times (resize horizontaly)
        for (j = 0; j < biOrig.biWidth; j++)
            RGBTRIPLE triple;
            fread(&triple, sizeof(RGBTRIPLE), 1, input);

            for (v = 0; v < n; v++)
                fwrite(&triple, sizeof(RGBTRIPLE), 1, output);


        //skip the padding in input file
        fseek(input, paddingOrig, SEEK_CUR);

        //write the padding in output file
        for (k = 0; k < paddingNew; k++)
            fputc(0x00, output);

1 Answer 1


In the fseek() call you are making to move the pointer back a row:

fseek(input, -((biOrig.biWidth * sizeof(RGBTRIPLE)) + paddingNew), SEEK_CUR);

You are using the new padding to go back and the original to move forward inside the same loop here:

//skip the padding in input file
        fseek(input, paddingOrig, SEEK_CUR);

I think this is causing that the resize only works in case the new padding and the old padding results the same.

If you want to do it this way you need to use the original padding to move the cursor back.

  • THANK YOU! That was it, check50 works fine now. Can't believe I've spent so much time on such a stupid mistake. Thanks, once again
    – Ice32
    Dec 15, 2014 at 23:07
  • Hi guys! Not sure if anyone is still following this post. Anyway I'm giving it a shot: 1) I got the part regarding the Header, and Infoheader correct. Where I've been struggling was the vertical resizing. I've been following some posts on the discussion board. This was the most helpful code example for me, since I could work my head around what it is trying to achieve. However, when I've implemented it, my program crashed. It seemed to me as if it gets into an infinitive loop. I got no feedback whatsoever. Any thoughts why this might have happened? ps No errors with compiling. thx
    – gado007
    Aug 22, 2015 at 17:26

You must log in to answer this question.

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