1

I'm mentally blocked and I was wondering if anyone would take a look at my code and give me a pedagogical clue?

I am struggling to get the correct colour output in my resized.bmp; there have been various problems but the current situation is a correctly resized small.bmp that is completely green. In the last couple of coding iterations I have lost the check50 pass for not resizing when the factor is 1, but I suppose that is incidental at this point.

The relevant code (I hope):

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

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi_infile.biHeight); i < biHeight; i++)
{
    // vertical resize factoring
    for ( int j = 1; j <= factor; j++ )
    {
        // iterate over pixels in scanline
        for ( int k = 0; k < bi_infile.biWidth; k++ )
        {
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            // write RGB triple to outfile
            for ( int l = 0; l < factor; l ++ )
            {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);   
            }  
        }

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

        // add padding for resized image
        for (int m = 0; m < resizedPadding; m++)
        {
            fputc(0x00, outptr);
        }

        if (j < factor)
        {
            fseek(inptr, -bi_infile.biWidth * sizeof(RGBTRIPLE) - scan_padding, SEEK_CUR);
        }
    }
}
2

OK there are two definite mistakes, (could be more but can't tell without the full code).

First, the following lines:

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

should be on the line bellow them, so outside the inner for loop. That's because, when you iterate over the pixels of the scanline, the padding is only at the end of the scanline, but you fseek() after every pixel you read, effectively loosing later pixels. You should fseek() past the padding only when you have read and written factor times each pixel in each scanline.

Now the second mistake is your vertical re-sizing. If you fix the above, and effectively skip the padding of the scanline you are at, you should write this scanline another factor - 1 times. So your problem is that you don't write each scanline vertically factor times. To do that, you should use fseek() with a negative number as input, to move you backwards in your file, at the beginning of the scanline you just were, and write it again at the output file. factor - 1 times. (I say factor - 1 because you have already written the first line.

So here is a pseudo-code of what you should do:

for every scanline
{
    for factor times
    {
        for every pixel in scanline
        {
            read the pixel
            for factor times
            {
                write the pixel
            }
        }
        move past the in_padding
        add the out_padding
        move back to the begging of the scanline (unless you have finished with this scanline)
    }
}

Hopefully this will get you going.


If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

8
  • Hey, thanks for the response. I have updated my op with the code I've come up with while I was away, I think I've addressed one of the issues you mention. I'll take a look at your suggestions now, thanks! – Andrew Smith Sep 19 '15 at 17:27
  • Yeap cool! You fixed the first one. Now go for the second one too! – ChrisG Sep 19 '15 at 17:38
  • Definitely getting there, op updated. Visually everything looks good but check50 is failing thus: :( resizes 1x1-pixel BMP to 4x4 correctly when n is 4 :( resizes 2x2-pixel BMP to 4x4 correctly when n is 2 – Andrew Smith Sep 19 '15 at 18:12
  • Could you paste the rest of the code too so I can execute exactly what you have? – ChrisG Sep 19 '15 at 18:37
  • 1
    int resizedPadding = (4 - (bytesWide % 4)) % 4; ---- BLOODY MODULO!! – Andrew Smith Sep 19 '15 at 19:50

You must log in to answer this question.

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