0

In Pset4 resize(less), I try to resize the width of image by using fwrite. gain is integer I use for resize factor.

Originally I was doing like this. I put gain to the third argument of fwrite, and thought it would fwrite the read element "gain" times to achieve resize. However, the picture did magnify to the correct size, but the color is weird.

        // write RGB triple to outfile
        fwrite(&triple, sizeof(RGBTRIPLE), gain, outptr);

Result - gain = 20

So I let the third argument back to 1, and use loop to implement, and the resize work as I want.

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

Result - gain = 20

My question is why the result didn't go the way I want when I use the third argument of fwrite to control resize value? Did I misunderstand something?

1 Answer 1

0

You have to understand exactly what the second and third parameters are.

The second parameter is the size of each element to be written. The third parameter is the number of elements to be written, not the number of times to write the same element.

Say that each element is a char and the third parameter is 4. Then a call to fwrite would write 4 consecutive chars from the source to the target. It would NOT copy one element 4 times.

It also assumes that the programmer knows what they're doing and assumes the input data is valid. So, it's going to read and write the correct amount of data, whether it's valid data or not. ;-)

So, to scale the horizontal line, you need the for loop to write the same element gain times.

Now, you get to figure out how to scale it vertically! ;-D

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thanks for the clear explanation, now I get it!
    – Arel Lin
    Commented Oct 4, 2017 at 8:31

You must log in to answer this question.

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