0

been on this problem for over a month now. Still cannot resize images as required. When I run code, only the first row is resized. Other pixels return unmatched pixel colours. I would appreciate any help.

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

    // checking for positive interger less than or equal to 100
    int n = atoi(argv[1]);

    if (n > 100 || n < 1)
    {
        fprintf(stderr, "Must be positive and less than 100 %i\n", n);
        return 4    ;
    }

    // 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;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

    // 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;
    }

    // temporary variable to hold the biWidth and biHeight
    int o_width = bi.biWidth;
    int o_height = abs(bi.biHeight);

    bi.biWidth *= n;
    bi.biHeight *= n;

    // determine padding for scanlines
    int o_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int padding = (4 - (o_width * sizeof(RGBTRIPLE)) % 4) % 4;

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

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

    // temporary storage
    RGBTRIPLE triple;

    // iterate over infile's scanlines
    int i, j, k, l, m, o;

    //Iterating through the height
    for (i = 0; i < o_height; i++)
    {
        for (j = 0; j < n ; j++)
        {
            //Iterating through the width
            for (k = 0; k < o_width; k++)
            {
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

                for(l = 0; l < n; l++)
                {
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }
            }

            // Addding the padding for the output file
            for(m = 0; m < o_padding; m++)
            {
                fputc(0x00, outptr);
            }

            // Sending the cursor for the input file back
            fseek(inptr, -1*(o_width*sizeof(RGBTRIPLE)), SEEK_CUR);
        }
        // repeat process for writing the pixels
        for (o = 0; o < o_width; o++)
        {
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            for (o = 0; o < n; o++)
            {
                {
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }
            }
        }
        for(m = 0; m < o_padding; m++)
        {
            fputc(0x00, outptr);
        }

        fseek(inptr, padding, SEEK_CUR);
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0; }
1

First issue: The code has a logical infinite nested loop because the same control variable, o, is used in two loop setups. The result is that the outer loop never completes.

Next, a programming tip. If you see the exact same code repeated as a block, then it should raise a red flag that the second copy of the code is probably not necessary.

In this case, the second block that reads and then writes the pixels out is redundant, and it is causing lines to be written to the output file too many times.

I'm guessing that this code was intended to handle the vertical scaling. Unfortunately it's causing problems. Think about how you could implement the vertical scaling without the second block of code to do the same job. Then, think about how to control how many times to reposition back to the beginning of the current line. What tool would you use to conditionally fseek backwards?

If this doesn't get you going, then I'd suggest that you find one of the interactive chat room forums and get some one on one help. I hang out on CS50 Slack. There are others.

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

1
  • Hello Cliff. Thank you for you the response. Yes, you are right, the goal of the problem set was first to scale an image horizontally pixel by pixel by a variable 'n', add padding and then scale all the rows vertically by the same variable. Unfortunately I cannot write the pseudo code as it was given by the facilitators but at some point we had to repeat the code that writes pixels and padding to the outfile outside the loop hence the repetition. – alanana Aug 28 '18 at 5:02

You must log in to answer this question.

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