Once again I find myself stumped by CS50. In the below code I have done my best to try to mathematically edit the Bitmap Headers using a variable I named "scalefactor" which is determined by the user, as well as use that same variable to influence the writing of the pixels, and feel that I am quite close to the solution, but as always missing some obvious and simple part to the program that is resulting in mostly black images. I doubt it's anything to do with the way I determine the header information, but to be sure I've placed that below as well. As always, thank you for the help.

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

if (scalefactor != 1)
    // make necessary changes to BITMAPFILEHEADER
    bf.bfSize = (bf.bfSize - bi.biSize) + (((bi.biWidth + padding) * scalefactor) * (bi.biHeight * scalefactor));

    // make necessary changes to BITMAPINFOHEADER
    bi.biWidth = bi.biWidth * scalefactor;
    bi.biHeight = bi.biHeight * scalefactor;
    bi.biSizeImage = bi.biWidth * bi.biHeight;

// 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, biHeight = abs(bi.biHeight); i < biHeight; i++)
    for (int k = 1; k <= scalefactor; k++)
        // 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
            fwrite(&triple, sizeof(RGBTRIPLE), scalefactor, outptr);

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

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

1 Answer 1


There are a number of problems. First, you haven't calculated the padding for the output file. The padding for the input and output files are not necessarily the same. You have to calculate the padding for the output file. Some of the header values are dependant on this, so this fix will cure some other issues.

Next, your fwrite() doesn't write out multiple copies of an rgptriple. It writes the number of bytes specified from the source to the target. See I don't understand what is going on my resize? Actually probably resize too for more info.

You also write each line scalefactor times, but you aren't adding the padding at the end of each line. The padding is only being added after each set of scalefactor lines.

There may be other issues, but this will get you going.

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

  • I feel that I have solved the issues you mentioned barring the calculation of the padding. I moved the code that inserts padding to inside the loop that iterates the writing of lines, ensuring each line has padding at the end, and added another loop for the fwrite() function to be repeated scalefactor times rather than my mistaken implementation above. That said, I made an attempt to recalculate the padding (as seen in the example above) where I multiply the bi.Width by scalefactor to find padding, but as you have pointed out, this is wrong, though I am not entirely certain why. Oct 6, 2015 at 10:05

You must log in to answer this question.

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