0
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <math.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;
    }

    // remember filenames
    int n = atol(argv[1]);
    char *infile = argv[2];
    char *outfile = argv[3];

    if (n < 0)
    {
        fprintf(stderr, "Cannot have an enlarge value below 0\n");
        return -1;
    }

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



    //Increases the number of pixels in the image
    int width = bi.biWidth;
    int height = bi.biHeight;
    bi.biWidth = round(bi.biWidth*n);
    bi.biHeight = round(bi.biHeight*n);

        // determine padding for scanlines
    int inPadding = (4 - (width * sizeof(RGBTRIPLE)) % 4) % 4;
    int outPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

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

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

    RGBTRIPLE array[bi.biWidth];

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

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

            array[j] = triple;
        }

        for (int c = 0; c < n; c++)
        {
            // iterate over pixels in scanline
            for (int a = 0; a < width; a++)
            {
                for (int b = 0; b < n; b++)
                {
                    // write RGB triple to outfile
                    fwrite(&array[a], sizeof(RGBTRIPLE), 1, outptr);\
                }
            }


                // then add it back (to demonstrate how)
                for (int k = 0; k < outPadding; k++)
                {
                fputc(0x00, outptr);
                }
            }
            // skip over prior padding, if any
            fseek(inptr, inPadding, SEEK_CUR);
    }


    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}

it compiles and resizes perfectly but when run through check50 it produces these errors.

:) 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
    Header field bfSize doesn't match. Expected 0xae, not 0x5a
:( resizes small.bmp correctly when n is 3
    Header field bfSize doesn't match. Expected 0x132, not 0x5a
:( resizes small.bmp correctly when n is 4
    Header field bfSize doesn't match. Expected 0x1e6, not 0x5a
:( resizes small.bmp correctly when n is 5
    Header field bfSize doesn't match. Expected 0x306, not 0x5a
:( resizes large.bmp correctly when n is 2
    Header field bfSize doesn't match. Expected 0x6f6, not 0x1e6
:( resizes smiley.bmp correctly when n is 2
    Header field bfSize doesn't match. Expected 0x336, not 0xf6
:( resizes smiley.bmp correctly when n is 3
    Header field bfSize doesn't match. Expected 0x6f6, not 0xf6
1

Hmmm.....

So here's my question for you. WHICH fields are supposed to be updated in the headers when resizing to something other than 1?

Hint: check50 is giving you a clue.

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

3
  • I went back and watched the video and edited the code to calculate Bi.biSizeImage and bf.bfSize but its still resulting in errors bi.biSizeImage = ((sizeof(RGBTRIPLE)*bi.biWidth*n)+outPadding)*abs(bi.biHeight); bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); :( resizes small.bmp correctly when n is 2 Header field bfSize doesn't match. Expected 0xae, not 0x11a :( resizes small.bmp correctly when n is 3 Header field bfSize doesn't match. Expected 0x132, not 0x318 Jan 18 '19 at 1:03
  • without seeing the current code, I wouldn't know how to answer, except to say that it sounds like biSizeImage isn't being calculated correctly. Maybe there's a scaling issue? ;-)
    – Cliff B
    Jan 18 '19 at 1:18
  • Ah, it worked when I went back and checked, turned out the formula still had a *n on it tacked on Jan 18 '19 at 2:02

You must log in to answer this question.

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