0

Thought I had done this one, but getting errors with check50, here is my code:

/** *resize.c * * Computer Science 50 * Problem Set 4 * * Resizes a BMP file by a factor of n. */

include

include

include "bmp.h"

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

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

// what is the scale input by user
int n = atoi(argv[1]);

// make sure input is psotive and between 0 and 100
if (n < 0 || n > 100)
{
    printf("Please input a positive integer between 0 and 100");
    return 5;
}

// open input file 
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
    printf("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;
}

// Need to declare new outfile headers
BITMAPFILEHEADER new_bf = bf;
BITMAPINFOHEADER new_bi = bi;


// find out new width and height
new_bi.biWidth = bi.biWidth * n;
new_bi.biHeight = bi.biHeight * n;

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

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

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

// new padding
int new_padding = (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

// new size of image.
new_bi.biSizeImage = (new_bi.biWidth * sizeof(RGBTRIPLE) + new_padding) 
* abs(new_bi.biHeight);

// new size of the file
new_bf.bfSize = new_bi.biSizeImage + 54; 

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    for(int k = 0; k < n; k++)
    { 

     // 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
            for (int i = 0; i < n; i++)
            {
                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 < new_padding; k++)
        {
            fputc(0x00, outptr);
        }


        fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE)
        + padding), SEEK_CUR);  

    }

    fseek(inptr, bi.biWidth * sizeof(RGBTRIPLE) + padding, SEEK_CUR);
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}

Spent ages on it now and looking for some advice as to where I'm going wrong, here is the check50 results and again any help would be much appreciated:

:) resize.c and bmp.h exist

:) resize.c compiles :) doesn't resize 1x1-pixel BMP when n is 1

:( resizes 1x1-pixel BMP to 2x2 correctly when n is 2

:( resizes 1x1-pixel BMP to 3x3 correctly when n is 3

:( resizes 1x1-pixel BMP to 4x4 correctly when n is 4

:( resizes 1x1-pixel BMP to 5x5 correctly when n is 5

:( resizes 2x2-pixel BMP to 4x4 correctly when n is 2

  • What a shame. You had a working version a couple of days back [link]cs50.stackexchange.com/questions/14648/…. The code you posted here is different. The best advice I can give is to walk yourself back to where you were 4 days ago. Good luck. – DinoCoderSaurus Nov 2 '15 at 2:04
  • Not the advice I would offer. – Cliff B Nov 2 '15 at 3:09
  • Thanks @DinoCoderSaurus, unfortunately I got the cs50 check errors with that old code as well and then went about trying to change and understand it to make it work but had no success. The other code seemed to work but was incorrect. – Steve23455 Nov 2 '15 at 10:20
3

Wouldn't it work better if you wrote out the headers after you calculate the new new_bf.bfSize and new_bi.biSizeImage ?

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

| improve this answer | |
  • Thanks Cliff, will give that a try when I get home today – Steve23455 Nov 2 '15 at 10:20
  • All greens after that change, thanks for the help! – Steve23455 Nov 2 '15 at 20:28

You must log in to answer this question.

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