resize.c has kept me busy for a while now. It seems to work now. Well at least it resizes smiley.bmp properly. But check 50 is not happy. I have no clue why?!? Has anyone an idea please?? Thank you very much! Here my code, well at least the part I assume is relevant.

// set new dimesion for bitmap & calculate int originalWidth = bi.biWidth; int originalHeight = bi.biHeight; int originalHeight2 = abs(originalHeight); bi.biHeight = originalHeight * factor; bi.biWidth = originalWidth * factor;

// determine padding for scanlines
int in_padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int out_padding = (4- (bi.biWidth * sizeof(RGBTRIPLE))%4) % 4;

//create new image size
bi.biSizeImage = (bi.biWidth * sizeof (RGBTRIPLE) + out_padding) * abs(bi.biHeight);
bf.bfSize = (bi.biSizeImage) + 54;

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

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

// iterate over infile's scanlines
for (int i = 0; i < originalHeight2; i++)
    //write factor each line
    for ( int n = 0; n < factor; n++)
    // iterate over remaining padding
        fseek (inptr, (BHEADER + ((originalWidth * 3 + in_padding) * i)), SEEK_SET);
        for( int j = 0; j < originalWidth; j++)
           //temporary storage
            RGBTRIPLE triple;

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

            // write RGB triple to outfile
            for( int k = 0; k < factor; k++)
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
    // store outfile padding
    for (int l = 0; l < out_padding; l++)
      fputc(0x00, outptr);


// close infile

// close outfile

// that's all folks
return 0;


1 Answer 1


There's one very conspicuous problem. Even though both in_padding and out_padding need to be independently calculated, they are always going to be identical because they have exactly the same formula. The input file padding needs to be calculated with the parameters from the input header. Same for the output file.

A good technique is to start testing with small.bmp and a factor of 1. This produces an exact copy of the input file. Make sure the header is right, then make sure the image is right by checking with xxd, not by looking at the image. Once it can create a perfect copy, change to factor = 2 and repeat the process, first the header and then the image. You can compare the output of your file to the output of the staff sample.

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

  • Thank you, Cliff. I changed my padding formula to this int in_padding = (4 - (originalWidth * sizeof(RGBTRIPLE)) % 4) % 4; int out_padding = (4- (bi.biWidth * sizeof(RGBTRIPLE))%4) % 4; it works now for 1x1 pixel when n is 4 and for 2x2 pixel if n is 2. I have the feeling that my padding forumula is correct now and the problem lies somewhere else. I gonna start playing around and find the problem but if you had a further idea to push me - once more- into the right direction, it would be highly appreciated! Thank you!
    – user10882
    Jun 2, 2016 at 9:31

You must log in to answer this question.

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