My output is a grey square with a white rectangle at the top and bottom. What is it fwriting?

  // iterate over infile's scanlines (for each row)
for (int i = 0; i < bi.biHeight; i++)
    //iterate over scale factor for rows
    for (int n = 0; n < scale_factor; n++)

       // iterate over pixels (for each pixel/column)
        for (int j = 0; j < bi.biWidth; j++)
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);     //are you sure it is inptr

            //iterate over scale factor (for the scale factor - columns)
            for (int m = 0; m < scale_factor; m++)
            // write RGB triple to outfile
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);

            }                                                           //closes (for the scale factor - columns)
        }               //closes (for every pixel)
    // then add padding to output file.
    for (int k = 0; k < outfile_padding; k++)
        fputc(0x00, outptr);                    //I think this is getting hit too many times
    }                                           // close for outfile padding

                     //skip padding on input file
    }                                                               //close for scale_factor loop
           fseek(inptr, infile_padding, SEEK_CUR);
}                                                               //closes for each row loop

// close infile

// close outfile

// success
return 0;


  • If you select code and click the {} button of the editor, it will add or remove four spaces at the beginning of each line, that's the StackExchange-flavoured markdown for a code block. – Blauelf May 28 '19 at 10:03
  • Does it write at all? bi.biHeight, if it refers to the value in one of the files, should be negative (meaning image is stored top down, positive would be bottom up). And does bi.biHeight refer to input height or output height? – Blauelf May 28 '19 at 10:16
  • Thanks for the tip on the editor, the code block is fixed now. As to your question, the program writes a vertical white strip on the far right, and a horizontal white strip on the bottom, which is not what I expected. I expected to see a bright green square with a white center. bi.biHeight refers to the height of the original image, input height. I changed that i <bi.biHeight statement to abs(bi.biHeight) which made things a bit better. The first row (only) outputs as expected. – KayO133 May 29 '19 at 12:19

For each input line, you currently read bi.biWidth * scale_factor pixels from input. Obviously, the input line does not have that many (unless the factor is 1). One approach is to read the pixels to some array once, and use that one for writing multiple times. Another approach is to use fseek for jumping back the number of pixels just read (you could use SEEK_SET with an absolute position, or SEEK_CUR with a negative number), so you can read the same input line again.

  • Thank you! I've cleared up the loops that led to too many reads. Now I'm working out which of the other two methods I want to use. Cheers. – KayO133 May 31 '19 at 23:57

You must log in to answer this question.

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