0

code doesn't pass last 3 check50 test(when f is 0.5)

:) 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

:) resizes small.bmp correctly when n is 3

:) resizes small.bmp correctly when n is 4

:) resizes small.bmp correctly when n is 5

:) resizes large.bmp correctly when n is 2

:) resizes smiley.bmp correctly when n is 2

:) resizes smiley.bmp correctly when n is 3

:( resizes 6x6-pixel BMP to 3x3 correctly when f is 0.5

Image has more bytes than expected.

:( resizes 12x12-pixel BMP to 6x6 correctly when f is 0.5

Image has more bytes than expected.

:( resizes 18x18-pixel BMP to 9x9 correctly when f is 0.5

Image has more bytes than expected.  

It works fine visually, however the final three tests still fail.

int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 4)
    {
        fprintf(stderr, "Usage: copy infile outfile\n");
        return 1;
    }
    float f = atof(argv[1]);
    if(f<= 0.0 && f>= 100.0)
    {
        printf("f is not in range\n");
        return 1;
    }

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

    BITMAPFILEHEADER nbf = bf;
    BITMAPINFOHEADER nbi = bi;
    int nf = ceil(f);
    nbi.biWidth = ceil(bi.biWidth*f);
    nbi.biHeight = ceil(bi.biHeight*f);
    int newpadding = (4 - (nbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    nbi.biSizeImage = ((sizeof(RGBTRIPLE))*(nbi.biWidth) + newpadding)* abs(nbi.biHeight);
    nbf.bfSize = nbi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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

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

    // iterate over infile's scanlines
    if(f >= 1)
    {
        for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
        {
            for(int m = 0; m < nf; m++)
            {
                // 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 n times
                    for(int l = 0; l < nf; l++)
                        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 < newpadding; k++)
                {
                    fputc(0x00, outptr);
                }
                if( m < (nf-1))
                {
                    fseek(inptr, -((bi.biWidth * sizeof(RGBTRIPLE)) + padding), SEEK_CUR);
                }
            }
        }
    }
    else if( f < 1 )
    {
        int p = 1;
        float f2 = 1/f;
        int nf2 = ceil(f2);
        for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
        {
            // 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
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                if(p < nf2)
                {
                    fseek(outptr, -(sizeof(RGBTRIPLE)), SEEK_CUR);
                    p++;
                }
                else if( p == nf2)
                {
                    p = 1;
                }
            }

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

            // then add it back (to demonstrate how)
            for (int k = 0; k < newpadding; k++)
            {
                fputc(0x00, outptr);
            }
            fseek(inptr, ((bi.biWidth * sizeof(RGBTRIPLE)) + padding)*(nf2-1), SEEK_CUR);
        }
    }
    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}

1 Answer 1

0

Hmmm.... Interesting that all of the tests that shrink the image have the same problem.

Have you looked at the raw output data instead of the image? Did you look at the number of bytes per line? Is the padding right? And finally, did you count the number of output lines in the data?????????? Or did you just run check50 and find that something's broken? What is the file size of the output file? What SHOULD the size be? (Easily calculated based on header size, image size, padding, etc. Hint: calculate the file size from that data for a known good file before predicting the size of an unknown.)

Maybe you just looked at the image. Did you know that the headers will control what is displayed? If the headers are correct, if the file has extraneous data, it will NOT be displayed.

So, the errors say there are more bytes than expected. Any ideas? Have you looked at the extra data yet? How many bytes are supposed to be in the output file? How many bytes are really there?

That should get you going! Yes, it may sound a little snarky, but it's more valuable that you learn what questions to ask when something goes wrong, starting with knowing exactly WHAT is wrong, then what code controls that, then what is the code that caused the error, and finally, how to fix it.

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

1

You must log in to answer this question.

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