0

I posted my code a few weeks ago, and I have been working on it for a some hours today but I am really stuck as to why it is not working. I have not yet written the code for resizing down. It would be great help if anyone can point out where I am going wrong. Thanks in advance.

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

#include "bmp.h"

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

    //store factor and filenames
    double f = atof(argv[1]);
    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 & BITMAPINFOHEADER
    BITMAPFILEHEADER bf;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
    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;
    }

    // copy bf and bi
    BITMAPFILEHEADER cf = bf;
    BITMAPINFOHEADER ci = bi;

    //change dimensions for outfile file
    ci.biWidth *= f;
    ci.biHeight *= f;

    // determine padding for infile & outfile scanlines
    int inpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int outpadding = (4 - (ci.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    //determine SizeImage and Size of outfile
    ci.biSizeImage = (bi.biWidth + outpadding)* abs(ci.biHeight) * sizeof(RGBTRIPLE);
    cf.bfSize = ci.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

     RGBTRIPLE *copies = malloc(sizeof(RGBTRIPLE) * ci.biWidth);

    if (f >= 1)
    {

        int counter = 0;

        // iterate over infile's scanlines
        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);

                for (int k = 0; k < f; k++)
                {
                    copies[counter] = triple;
                    counter++;
                }

            }

            //write copies in outfile f times
            for (int j = 0; j < f; j ++)
            {
                fwrite(copies, (counter*sizeof(RGBTRIPLE)), 1, outptr);

                //write outfile padding
                for (int l = 0; l < outpadding; l++)
                {
                    fputc(0x00, outptr);
                }
            }

            // skip over padding, if any
            fseek(inptr, inpadding, SEEK_CUR);
            counter = 0;
        }
    return 5;
    }



    //
    free(copies);

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}
1

I'd recommend using peek as mentioned in the problem description

You would see that your output headers are the same as the input headers. You change cf/ci to match the dimensions of the output image, but write bf/bi to the output file.

The rest looks as if your programme should work for positive integer values, as in resize/less, while specification now asks for supporting any positive factor up to 100.

Well, the return 5; looks odd, and the checker probably won't like it.

1
  • Thank you so much I was stuck on this for a long time! Aug 24 '19 at 14:44

You must log in to answer this question.

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