0

I'm stuck with resize.c :( I managed to update headers and resized image horizontally, but I cannot resize vertically - my code either stretches images in width or produces some weird images like from old TV monitors.

Here is my code:

int scanlinelength = biWidthOrg * (int)sizeof(RGBTRIPLE) * -1;

// iterate over infile's scanlines
for (int i = 0, counter = 1, biHeightRsz = abs(bi.biHeight); i < biHeightRsz; i++)
{

    // iterate over pixels in scanline
    for (int j = 0; j < biWidthOrg; j++)
    {
        // temporary storage
        RGBTRIPLE triple;

        // read RGB triple from infile and store the value in &triple
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

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

        // add padding to outfile, if any
        for (int l = 0; l < paddingRsz; l++)
        fputc(0x00, outptr);
    }

    if (counter >= nfactor)
    {
    counter = 1;
    // skip over padding in infile, if any
    fseek(inptr, paddingOrg, SEEK_CUR);
    }
    else
    {
    // go backward from current position to resize vertically
    fseek(inptr, scanlinelength, SEEK_CUR);
    counter++;
    }

}

What I'm trying to do: after fwrite writes the scanline into outfile, I want to come back in infile to the beginning of the original scanline using fseek and read and write this scanline into outfile again. But it isn't working and I receive resized images which looks like this https://monosnap.com/file/h1ynTiEZKuA1LRjmhJmnPBR6QF5apV

Please, help! Many thanks in advance!

1 Answer 1

1

This isn't just a vertical resize problem. It's a problem where padding != 0. The code inserts padding after every pixel. Padding should only be inserted at the end of each line. Look at your for loop design.

There may be more issues, but this should get you going. If there are other problems that are not closely related, please open a new question. Also, it would help to see the code that modifies all of the header fields and calculates padding.

I'd swear that I just answered this on Slack. :-? If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Yes, that was my question on Slack. :) I think I felt myself a little desperate yesterday after several hours of debugging vertical resize. Thanks a lot! I moved the outfile padding out of the for loop and it helped. It didn't pass check50 though, but at least it works! :) I'm working now on what's wrong for check50.
    – Libby
    Commented Oct 9, 2016 at 9:07

You must log in to answer this question.

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