int oldHeight = bi.biHeight;

int oldWidth = bi.biWidth;

bi.biWidth *= n;

bi.biHeight *= n;

int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);

bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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

// determine padding for scanlines
int oldPadding = (4 - (oldWidth * sizeof(RGBTRIPLE)) % 4) % 4;

RGBTRIPLE scanlines[bi.biWidth][abs(bi.biHeight)];

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(oldHeight); i < biHeight; i++)
    // increase row by n factor
    for(int w = 0; w < n; w++)
        if( w > 0 )
            fseek(inptr, oldHeight * sizeof(RGBTRIPLE), SEEK_CUR);
        // iterate over pixels in scanline
        for (int j = 0; j < oldWidth; j++)
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
            // increase pixels in scanline by n factor
            for(int z = 0; z < n; z++)
                // write RGB triple to outfile
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
    // skip over padding, if any
    fseek(inptr, oldPadding, SEEK_CUR);

    // then add it back (to demonstrate how)
    for (int k = 0; k < padding; k++)
        fputc(0x00, outptr);

As the title says, my resize less program is correct in check50, except when n=2, 3 and 5 and the image is small.bmp, after some testing, i suspect the issue has something to do with the padding, therefor the problem only appears when it's dealing with an image that is not a multiple of 4 or is not an exact copy of the original (n=1). The error results in a multicolor rectangle, with only 5 rows instead of 4 and only the first row is green. I posted a part of the code above, any help is appreciated.

Small.bmp when n = 4

Small.bmp when n = 2

EDIT: I solved the problem, it was the simple fact i left the padding output for loop inside the loop over the infile scanlines and not the one which will create the outfile, therefore i was only making the padding times the original number of rows and not the original * n factor like it was supposed to.

  • Can you add the image produced by 4?
    – Chris
    Dec 18, 2019 at 22:52
  • 1
    Added the images from n = 4 and n = 2
    – Afonso
    Dec 19, 2019 at 0:15

2 Answers 2


The padding seems to be correct. The only thing that is very odd is that instead of creating a new padding and new headers you decided to mark the old ones. Maybe it only changed the pointers and therefore wrote the information wrong. Hope it helps


So I looked further into it and realized that yes it is problem with pointers but not the one I described.

if( w > 0 )
    fseek(inptr, oldHeight * sizeof(RGBTRIPLE), SEEK_CUR);

Is what is causing the problem. (also that if statement does nothing).

What you are suppose to be doing is printing the first line then going down to the second one moving the pointer back to the beginning and printing the same thing over again. Once that is don't you need to move the pointer back to the end of the line so the program can run its course.

You must log in to answer this question.

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