I know this question's a repeat, but all the other questions had code and I didn't want to look through that as it felt like cheating.

The way I understand how to resize vertically is to fwrite the same scanline (factor) times, just like each pixel is added (factor) times in each scanline.

The code below when factor>1 only does horizontal scaling (the extra vertical axis is untouched and filled with the same colour).

But when factor<1, it creates an image with correct proportions that changes the colour entirely.

// iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
        // iterate over pixels in scanline
        for (int j = 0; j < bi.biWidth; j++)
            // temporary storage
            RGBTRIPLE triple;

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

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

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

        // then add it back (to demonstrate how)
        for (int k = 0; k < padding; k++)
            fputc(0x00, outptr);

Here's an example of when factor = 2 with large.bmp: enter image description here

Here's the same large.bmp with factor = 0.5: enter image description here (the image is 6px X 6px, from top to bottom green red blue green red blue)

I assumed that the loop with counter r would repeat each row factor times, and I'm still not sure how to resize properly when factor<1. Feeling pretty lost, any help would be appreciated.

  • I was thinking the same thing about not looking at what other people have done, and I know that part of the academic honesty policy is not to not let others show you their code, but I think it kind of defeats the purpose of the cs50 exchange because then nobody could learn from anyone else except by question only? I think it is within reason to take a look at other posts so long as one has thought about it themself and not just copying another which is probably why that rule is there in the first place. Elsewise I don't know why they would have created a separate exchange for cs50 students? – dumbitdownjr Jun 9 '17 at 22:14
  • Also sorry I can't answer your question right now, I am working on this one myself and was just thinking about the same question. – dumbitdownjr Jun 9 '17 at 22:15
  • @dumbitdownjr I suppose this question's outdated now actually, I figured it out yesterday. Do I delete it? – Aryan poonacha Jun 10 '17 at 0:23
  • I suppose you could, but if someone answers your question it may help others with a similar problem. – dumbitdownjr Jun 10 '17 at 20:27

Figured it out, thought I'd answer to help others. Essentially, I had no mechanism to iterate over a particular vertical scanline more than the original image. Additionally, I didn't update my fread() like I needed to. Fix these and you'll get it.

| improve this answer | |
  • Should mark this as the answer if it's the answer. – Andrew Siplas Dec 2 '17 at 21:16
  • @AndrewSiplas Done! – Aryan poonacha Dec 4 '17 at 14:01

You must log in to answer this question.

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