1

My program isn't working. I have narrowed it down to the padding, but cannot find what is incorrect.

It resizes images that don't need padding perfectly (multiples of 4), and the header information is all correct (checked by peek) but in other cases it creates images with displaced pixels/odd colors, etc.

check50: https://sandbox.cs50.net/checks/b50c42ee0c884758939d16116a6bb5f4

I omitted all of the unnecessary code, and left all the logic.

[some code was hidden per academic honesty]

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    // Store an array in order to repeat each line 3x
    RGBTRIPLE tarray[bi.biWidth];
    // iterate over pixels in scanline
    for (int j = 0; j < bi.biWidth; j++)
    {
        // read RGB triple from infile
        RGBTRIPLE triple;
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
        tarray[j] = triple;

        // write RGB triple to outfile w/ magnification
        for(int k = 0; k < magn; k++){
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }
    }      
    // skip over padding, then add it back
    fseek(inptr, inpadding, SEEK_CUR);
    for (int pad = 0; pad < padding; pad++)
    {
        fputc(0x00, outptr);
    }
    // write each row more than once!    
    for(int j = 0; j < magn-1; j++){        
        for (int k = 0; k < bi.biWidth; k++)
        {
            // write RGB triple to outfile w/ magnification
            for(int l = 0; l < magn; l++){
                fwrite(&tarray[k], sizeof(RGBTRIPLE), 1, outptr);
            }
        }         
        // skip over padding, then add it back
        fseek(inptr, inpadding, SEEK_CUR);
        for (int pad2 = 0; pad2 < padding; pad2++)
        {
            fputc(0x00, outptr);
        }        
    }
}
2
+100

Your padding calculation is fine (I believe). I think the problem lies on the "logic". Once you've completed scanning the first line, you force the file cursor to skip the infile padding:

for (int j = 0; j < bi.biWidth; j++)
{
    // read RGB triple from infile
    ...
    // write RGB triple to outfile w/ magnification
    ...
}      
// skip over padding, then add it back
fseek(inptr, inpadding, SEEK_CUR);

Then you "write each row more than once":

// write each row more than once!    
for(int j = 0; j < magn-1; j++){        
    for (int k = 0; k < bi.biWidth; k++)
    {
        // write RGB triple to outfile w/ magnification
        for(int l = 0; l < magn; l++){
            fwrite(&tarray[k], sizeof(RGBTRIPLE), 1, outptr);
        }
    }

So far, so good... But then, you do this:

// skip over padding, then add it back
fseek(inptr, inpadding, SEEK_CUR);

See whose cursor you are moving? Think carefully exactly where in your algorithm you're standing when you get to this line, and what to do with each padding / cursor.

Give this a thought, and you'll get it. You're almost there!

2
  • Thanks so much! I shouldn't be moving the cursor when I am copying it magn times - only in the initial statement. Bounty in 2 days. Dec 30 '14 at 4:45
  • Bounty awarded. Jan 2 '15 at 6:53

You must log in to answer this question.

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