I am on pset4 and working on resize, less comfortable. I chose to follow the "rewrite" method but it seems that I am going wrong somewhere and I have no idea what to do. Any advice?

// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biWidth *= n;
bi.biHeight *= n;
int newPadding = (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);

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    RGBTRIPLE number[n];

    // 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);

        for (int p = 0, s= 0; p < n; p++)
            number[s] = triple;

    for (int l = 0; l < n; l++)
        // write RGB triple to outfile
        fwrite(&number[l], sizeof(RGBTRIPLE), 1, outptr);
        // then add it back (to demonstrate how)
        for (int k = 0; k < newPadding; k++)
            fputc(0x00, outptr);

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

// close infile

// close outfile

// success
return 0;


1 Answer 1


There are several issues here. First, the code is using the biHeight and biWidth of the output file to control the input file reading.

Next, the code reads the pixels in and writes them to array number[]. (I hope it is declared as a char array and not int.) BUT, it is overwriting the contents of number[] on each pass, so that only the last pixel is left in the array. If you're trying to build up a line of pixels to write out one or more times to scale vertically, it has to be n times the length of a line of pixels. This may need to also include padding, depending on how you handle padding at write time.

Then, in the next for loop, padding is being written out after each pixel, not at the end of each line.

There's a lot to fix here. Happy coding!

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

You must log in to answer this question.

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