0

My code works correctly. large creates and correctly. but it simply won't pass check50

 /**
 * copy.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Copies a BMP piece by piece, just because.
 */

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

#include "bmp.h"

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

    int n = atoi(argv[1]);

    if ( n < 0 || n > 100)
    {
        printf("n must be a positive integer less than 100\n");
        return 2;
    }

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

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

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    BITMAPINFOHEADER bitemp;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
    bitemp = 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;
    }

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

    int inpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int outpadding = (4 - (bitemp.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    bi.biSizeImage = (bi.biWidth * abs(bi.biHeight) * 3) + outpadding*abs(bi.biHeight);
    bf.bfSize = bi.biSizeImage * bf.bfOffBits;

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

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

    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bitemp.biHeight); i < biHeight; i++)
    {
        if (i % n !=0)
        {
            fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE) + inpadding), SEEK_CUR);
        }
        // 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 k = 0; k < n; k++)
            {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
        }

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

        // then add it back (to demonstrate how)
        for (int l = 0; l < outpadding; l++)
        {
            fputc(0x00, outptr);
        }
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // that's all folks
    return 0;
}
2
  • It would be helpful to point out what pset this is and what program you're having problem with, preferably in the question title. (I can understand what you're asking about but it is nice to make it clearer)
    – thomas
    Aug 29, 2016 at 18:21
  • Also, be sure to test with different numbers to resize, your code should not be working.
    – thomas
    Aug 29, 2016 at 18:42

2 Answers 2

1

There is definitely an issue with your code. You should first test with scale factor of 1, make sure the headers are right, then check the image data, using the peek and xxd tools. Once those are right (they may already be), repeat with scale of 2 and then 3. You can use the staff version (described in instructions) to generate a file to compare to.

Also, consider the image file. For example, the 1x1 pixel file is a poor choice to test with. It doesn't exercise the code very well and doesn't have enough data diversity to see many problems.

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

0

the problem was bi.biSizeImage. I was writing bitemp.biSizeImage to new output file and so what I calculated as bi.biSizeImage is actually bitemp.biSizeImage.

You must log in to answer this question.

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