0

Whenever I run my code to resize an image, it resizes correctly (with and without padding). However, whenever I check my code with check50, it returns unexpected end of input. I've tried different things, but can't figure out why this is the case.

My code is below. Thanks in advance for any help :)

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



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

    //checks is scale is in range
    if (n<0 || n>100)
    {
        printf("Please use a number between 0 and 100.\n");
        return 1;
    }

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

    //width
    int originalWidth = bi.biWidth;
    bi.biWidth = originalWidth * n;

    //height
    int originalHeight = bi.biHeight;
    bi.biHeight =  originalHeight * n; 

    //padding
    int originalPadding = (4 - (originalWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int newPadding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    //ImageSize
    //int originalImageSize = bi.biSizeImage;
    bi.biSizeImage = (newPadding + bi.biWidth) * abs(bi.biHeight) * 3;

    //originalSize
    //int originalSize = bf.bfSize;
    bf.bfSize = bi.biSizeImage + 54;


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

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

    // iterate over infile's scanlines/height
    for (int i = 0; i < abs(originalHeight); i++)
    {
        //enlarge height
        for(int b=0; b<n; b++)
        {
            fseek(inptr, (54 + ((originalWidth * 3 + originalPadding) * i)), SEEK_SET);
            // skip over padding, if any

            // iterate over pixels over width
            for (int j = 0; j < originalWidth; j++)
            {
                RGBTRIPLE triple;

                // read's colors
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

                for (int m=0; m<n; m++)
                {
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }

            }

            //print new padding
            for (int k = 0; k < newPadding; k++)
            {
                fputc(0x00, outptr);
            }
        }

    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    return 0;
}
0

fixed it!

order of operations issue with declaring bi.biSizeImage.

You must log in to answer this question.

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