0

I am in the 4th problem set in cs50 trying to do resize.c.

I worked on it for about 3 days and finally everything is going great the program can resize whatever bitmap photo I throw at it.

I was about to submit it when I thought I should do a check50 first. so I did it and the results were:

:) resize.c and bmp.h exist.
:) resize.c compiles.
:) doesn't resize small.bmp when n is 1
:( resizes small.bmp correctly when n is 2
   Image has more bytes than expected.
:( resizes small.bmp correctly when n is 3
   Image has more bytes than expected.
:( resizes small.bmp correctly when n is 4
   Image has more bytes than expected.
:( resizes small.bmp correctly when n is 5
   Image has more bytes than expected.
:( resizes large.bmp correctly when n is 2
   Image has more bytes than expected.
:( resizes smiley.bmp correctly when n is 2
   Image has more bytes than expected.
:( resizes smiley.bmp correctly when n is 3
   Image has more bytes than expected.

I tried and tried but couldn't figure out what was wrong with the code, so please if you can help it would be very appreciated.

the code is:

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

#include "bmp.h"

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

    // remember filenames and the size to resize by.
    char *infile = argv[2];
    char *outfile = argv[3];
    int new_size = atoi(argv[1]);

    // open input file and if it couldn't open throw an error.
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

    // open output file and if it couldn't open throw an error.
    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;
    }
    // update width and height
    bi.biWidth *= new_size;
    bi.biHeight *= new_size;

    // determine padding for scanlines
    int output_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int input_padding = (4 - (bi.biWidth / new_size * sizeof(RGBTRIPLE)) % 4) % 4;

    // set the bi an bf's sizes.
    bi.biSizeImage = ((3 * bi.biWidth) + output_padding) * abs(bi.biHeight);
    bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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

    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        // creating an array of struct RGBTRIPLE that will hold the current scanline in it.
        RGBTRIPLE current_scanline[bi.biWidth];
        int position = 0;

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

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

            // iterate as many times as new_size, draw the pixle and add it to the current_scanline array.
            for (size_t k = 0; k < new_size; k++)
            {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                current_scanline[position] = triple;
                position++;
            }
        }

        // Add padding for the original scanlines.
        for (int k = 0; k < output_padding; k++)
        {
            fputc(0x00, outptr);
        }

        if (new_size != 1)
        {
            // add extra scanlines times new_size - 1.
            for (int a = 0; a < new_size - 1; a++)
            {
                // iterate over pixels in scanline
                for (int b = 0; b <  bi.biWidth; b++)
                {
                    fwrite(&current_scanline[b], sizeof(RGBTRIPLE), 1, outptr);
                }
                // Add padding for the new scanlines.
                for (int k = 0; k < output_padding; k++)
                {
                    fputc(0x00, outptr);
                }
            }
        }

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

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}
0

Your files might be a bit too long. Does for example ls -l report the same file size for the results of both your output and the staff solution's?

You probably meant

biHeight = abs(bi.biHeight) / new_size

for your for loop, the number of input rows.

1
  • yes, this was the problem. thank you very much. – Malik Albeik Apr 18 '18 at 13:08

You must log in to answer this question.

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