I tried following the pseudocode from the walk through and it compiled, however the image did not resize - rather the center filled in green. I hope my only problem is that I fail to skip over the padding at the end (I still don't completely understand the fseek and fputc functions). Does anything else stick out as glaringly incorrect:

// write new dimensiosn for BITMAPINFOHEADER

new_bi.biWidth = bi.biWidth * fac;
new_bi.biHeight = bi.biHeight * fac;

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

// write new size for BITMAPINFOHEADER
new_bi.biSizeImage = ((sizeof(RGBTRIPLE)* new_bi.biWidth) + new_padding)*abs(new_bi.biHeight);

new_BF.bfSize= new_bi.biSizeImage + sizeof(BITMAPINFOHEADER)+sizeof(BITMAPFILEHEADER);

// 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++)
    //increase number of rows by a factor of fac
    for (int j = 0; j < fac; j++)
        // iterate over pixels in scanline
        for (int k = 0; k < bi.biWidth; k++)
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
                   for(int m=0; m < fac; m++)
            // write RGB triple to outfile, factor times
            fwrite(&triple, sizeof(RGBTRIPLE), 1, 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);

Much of your code is still in "copy" style and has to be adjusted.

You need to keep track of state and position in both the input and output file. In many cases the structures are equivalent, but not the same. Make sure to stay "in sync" while preserving the differences.

The headers contain not just the size, so you might want to copy them, like

    BITMAPINFOHEADER new_bi = bi;

Also, you would want to fwrite new_biand new_BF (inconsistent case!) instead of bi and bf.

At the end of your second level of for loops, you could fseek backwards by bi.biWidth * sizeof(RGBTRIPLE) for all but the last iteration, effectively resetting the input file cursor to the begin of the line. There are other options like reading the whole line into memory and re-using that one instead of re-reading the line.

The output padding you write should use new_padding, not padding.


You must log in to answer this question.

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