My implementation creates the headers properly (verified with xxd), so I know it's somewhere in this loop that I'm missing something. I've been trying all sorts of variations of this code, but cannot get it to work. Can anyone see why this for loop would not work?

    // iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    // temporary storage for each scanline
    int scanline_size = sizeof(new_bi.biWidth) * sizeof(RGBTRIPLE);
    RGBTRIPLE scanline[scanline_size];
    int p = 0;

    // 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 triple to scanline array n times
        for (int k = 0; k < n; k++)
            scanline[p] = triple;

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

    // write scanline array to outptr n times
    for (int k = 0; k < n; k++)
        fwrite(&scanline, scanline_size, 1, outptr);

        // add padding
        for (int j = 0; j < new_padding; j++)
            fputc(0x00, outptr);

Per Cliff B's comment, I moved the fseek outside of the multiplier loop, and used the correct padding variables this time around.


2 Answers 2


I see two issues after //write scanline array to outptr n times.

Let's say you are scaling up by 3, (n=3). Inside that for loop, you write out scanline, then skip forward over the padding in the input file, and then "add it back" to the output file.

Here's the first problem. When you do this, you end up skipping forward in the input file 'n' times! (3 in this case). That means that you've skipped forward into the next line of pixels!

The second problem is the output padding. Since you use the same variable for skipping the input padding as writing out to the output file, that tells me you either didn't recalculate the size of the padding for the output file or just didn't use the correct variable. This means that the output file will have the wrong padding unless the multiplier is a multiple of 4.

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

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)


Ok. I finally got it working. Thank you to Cliff B on the guidance. When I calculated the scanline size I used sizeof(new_bi.biWidth), but that gives me the size of a biWidth (i.e. the size was always 4 bytes, the size of a LONG per bmp.h). I changed the scanline_size to be: scanline_size = new_bi.biWidth * sizeof(RGBTRIPLE)

You must log in to answer this question.

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