0

I'm having trouble with my program. It compiles and everything but 1) large.bmp appears as an "Invalid or Unsupported Image Format" and 2) I fail all the Check50 tests. I've been editing my code for the past week and can't find anything wrong with it; I even passed all the peek tests, meaning that my header information was correctly updated. Can you tell me what I'm doing incorrectly? Thank you!!

/**
* resize.c
*
* Computer Science 50
* Problem Set 4
*
* Resizes 24-bit uncompressed BMPs by a factor of n.
*/

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

#include "bmp.h"

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

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

//checks to see if 'n' input is valid.
if(n < 1 || n > 100)
{
    printf("n must be a positive integer less than or equal to 100.\n");
    return 2;
}

// open input file 
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
    printf("Could not open %s.\n", infile);
    return 3;
}

// open output file
FILE* outptr = fopen(outfile, "w");
if (outptr == NULL)
{
    fclose(inptr);
    fprintf(stderr, "Could not create %s.\n", outfile);
    return 4;
}

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

//update header information (height and width)
BITMAPFILEHEADER newbf = bf;
BITMAPINFOHEADER newbi = bi; 

newbi.biWidth = n * bi.biWidth;

newbi.biHeight = n * bi.biHeight;

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

// update header information (image size and file size)
newbi.biSizeImage = (newbi.biWidth * sizeof(RGBTRIPLE) + newpadding) * abs(newbi.biHeight);
newbf.bfSize = newbi.biSizeImage + 54;

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

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

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    // resize vertically
    for(int v = 0; v < n; v++)
    {
        fseek(outptr, (3 * newbi.biWidth + newpadding), 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                
            // resize horizontally
            for(int horizontal = 0; horizontal < n; horizontal++)
            {
                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);
        }
    }    
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;
}
0

The output files have twice as many rows of pixels as they should have. If you use the xxd tool to look at the data, you will see this. ls -al would also show that the file size differs.

A careful look at the use of fseek() will be useful.

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

You must log in to answer this question.

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