0

Been stuck on Pset3 Resize (Less) for days... I think i got my headers right, but i am unable to resize the image size properly.

When i try to resize small.bmp, it goes from this:

enter image description here

to this:

enter image description here

Will someone help to look at my code to guide me in the right direction? Thank you!

// Resizes a bmp file by a value of n

#include <stdio.h>
#include <stdlib.h>

#include "bmp.h"

int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 4)
    {
        fprintf(stderr, "Usage: resize n infile outfile\n");
        return 1;
    }

    //user input of n
    int n = atoi(argv[1]);

    //ensure n is less than or equal to 100, and n must be positive
    if (n < 0 || n > 100)
    {
        printf("Resize value must be positive and less than 100");
        return 1;
    }


    // remember filenames
    char *infile = argv[2];
    char *outfile = argv[3];

    // open input file
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

    // open output file
    FILE *outptr = fopen(outfile, "w");
    if (outptr == NULL)
    {
        fclose(inptr);
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 3;
    }

    // read infile's BITMAPFILEHEADER
    BITMAPFILEHEADER bf, bfNew;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
    // new file header
    bfNew = bf;

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi, biNew;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
    // new info header
    biNew = bi;

    // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
    if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
        bi.biBitCount != 24 || bi.biCompression != 0)
    {
        fclose(outptr);
        fclose(inptr);
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }
    // write new outfile with *=n
    biNew.biWidth = bi.biWidth * n;
    biNew.biHeight = bi.biHeight * n;



    // determine padding for scanlines
    int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    //determine new padding for scalines
    int newPadding = (4 - (biNew.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    //new image size
    biNew.biSizeImage = ((sizeof(RGBTRIPLE) * biNew.biWidth) + newPadding) * abs (biNew.biHeight);
    bfNew.bfSize = biNew.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); //size of BITMAPFILEHEADER and BITMAPINFOHEADER is 54

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

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

    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        //create a loop to print pixels by n factor

        for (int m = 0; m < n; m++)

        {

        // 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, loop for width
            for (int r = 0; r < n; r++)
            {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }

        }


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

        // then add the new padding back
        for (int k = 0; k < newPadding; k++)
        {
            fputc(0x00, outptr);
        }
        // re reading row m
        if (m < n - 1)
        {
        long offset = bi.biWidth*sizeof(RGBTRIPLE)+padding;
        fseek(inptr, -offset, SEEK_CUR);
    }
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}
}
1

There's certainly an error in your code. It's also a great demonstration of why its important to properly format and indent. Rather than give it away, I'll suggest that you run the code through style50 and then run it through debug50 step by step and watch what happens.

If you step through very carefully, it should become obvious where the problem lies, and that the fix is very easy.

If you still can't find the problem, leave a comment with a description of what you did and what you learned, and we'll go from there! ;-)

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

9
  • Hi Cliff B, thanks for your reply and guidance. I'm very new to programming so its great that your comments pushes me to find out the errors for myself. However, I am still quite lost as to how I should debug properly. I have corrected the style of my code (I usually only correct it just before i submit but maybe its time to change) and I have run debug50 to the best of my knowledge on how I can use it. However, I am still clueless as debug50 does not seem to show me what's wrong with the lines of codes, or I am unable to make sense of the information.
    – freshwound
    Feb 23 '19 at 10:30
  • Here's another hint. How many times does the code execute the main for loop? Run it under debug50 and use the "step into" tool to go one line of code at a time. Keep going until the program exits, noting how many times the main loop executes. You would also find some value in looking at the file's actual data instead of the images. Look for patterns or anything unusual.
    – Cliff B
    Feb 23 '19 at 10:35
  • Also, understand that the very most important thing that you're learning now is how to debug a program. This is one of the most important skills you can have as a programmer. It may be frustrating right now, but keep at it. With every problem, every "failure", every glitch, you'll learn more and more, and that's far more valuable than almost any other skill you'll learn - at least, in my humble opinion. So, don't be deterred by the process, EMBRACE IT WITH A BIG SMILE!!!! This is the best way to learn debugging and programming - by fixing lots of problems! :-)
    – Cliff B
    Feb 23 '19 at 10:40
  • Hi Cliff B, thank you for your words of encouragement, especially when I'm this close to just giving up the whole course... I've run the debug50 and looked at each line to the best of my understanding of programming... it seems to me that the main loop int i is only executed once, which I am unsure why it is a problem?
    – freshwound
    Feb 23 '19 at 11:36
  • Good! That's the first step. You've identified that the loop only runs once. A for loop that runs only once is usually a problem. Does the code go back to the for loop definition line or does something else happen? What is the very last line that actually executes? What does that line actually do? I'm hoping that you'll understand what's happening. An experienced programmer would recognize the issue immediately, but you're not an experienced programmer yet. So, it's important that you work through this and gain this experience. ;-)
    – Cliff B
    Feb 23 '19 at 11:56

You must log in to answer this question.

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