0

I have been working with resize for a couple weeks on and off. My pseudo code makes sense, but when I put it into action, it is not working. I only pass a portion of check50. I think it has to do with padding, but cannot figure it out. Here is my code. It would be great to get some insight... thanks in advance!!!

// create a copy of the infile headers, making new outfile headers BITMAPFILEHEADER new_bf; BITMAPINFOHEADER new_bi; new_bf = bf; new_bi = bi;

// identify existing data in bitmap
int crntWidth = bi.biWidth;
int crntHeight = bi.biHeight;

// update bitmap information for outfile with resize data
new_bi.biWidth = crntWidth * resize;
new_bi.biHeight = crntHeight * resize;

// calculating padding for scanlines
int crntPadding = (4 - (crntWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int newPadding = (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

// update image size for outfile
new_bi.biSizeImage = (new_bi.biWidth * sizeof(RGBTRIPLE) + newPadding) * abs(new_bi.biHeight);

// update file size for outfile
new_bf.bfSize = 54 + new_bi.biSizeImage;

// write outfile's BITMAPFILEHEADER
fwrite(&new_bf, sizeof(BITMAPFILEHEADER), 1, outptr);

// write outfile's BITMAPINFOHEADER
fwrite(&new_bi, sizeof(BITMAPINFOHEADER), 1, outptr);

// read and itierate over infile's scanlines
for (int i = 0, new_biHeight = abs(crntHeight); i < new_biHeight; i++)
{
     // itierate over pixels in scanline
    for (int j = 0; j < resize; j++)
    {

    if (j % resize != 0)
    {
        fseek(inptr, -(crntWidth * sizeof(RGBTRIPLE) + newPadding), SEEK_CUR);
    }

    // resize horizontally
    for (int k = 0; k < crntWidth; k++)
    {        
            // issue temporary storage
            RGBTRIPLE triple;

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

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

}
}

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

        // then add it back (to demonstrate how)
        for (int crntPadding = 0; crntPadding < newPadding; crntPadding++)
            {
                fputc(0x00, outptr);     
            }

}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

return 0;

}

1 Answer 1

1

It does have to do with the padding. There are two issues with it - one is a typo and the other is about program structure.

First, look at the following:

if (j % resize != 0)
{
    fseek(inptr, -(crntWidth * sizeof(RGBTRIPLE) + newPadding), SEEK_CUR);
}

You are repositioning the cursor at the beginning of the current line in the INPUT file. Why then, would you use the output file padding? Easy fix.

The structural problem is also simple to fix, but a little harder to spot. The code executes the k for loop and then just after the loop, you are processing the padding for both the input and output files. However, if you carefully look at the curly brace pairings, you will see that the padding code is outside/after the j for loop when it should be inside the j loop. The padding needs to be processed after each line of pixels is sent to the output file. As coded, you are only doing this after processing each line of pixels resize times. So, it fails to add the padding to all but the last copies of each line as it resizes, and causes problems when repositioning on the input file.

You need to move the closing curly brace before the // skip over infile padding, if any comment to just after all of the padding processing. This is the closing curly brace for the j loop.

That should fix your code, assuming you do it right. ;-)

If this answers your question, please click the check mark to accept this and remove the question from the unanswered pool. 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 .