0

My program seems to resize correctly but when i compare headers it seems that there's a bug, i use check 50 and it says that resize.c doesnt compile and the rest of tests give me a yellow poker face with the phrase "can't check until a frown turns upside down". can someone please tell me how i can fix my code?

/**
 * 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)
    {
        fprintf(stderr, "Usage: ./resize n infile outfile\n");
        return 1;
    }

    int n = atoi(argv[1]);

    if(n < 0 || n > 100)
    {
        fprintf(stderr, "n must be a positive integer less than or equal to 100\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_infile;
    fread(&bf_infile, sizeof(BITMAPFILEHEADER), 1, inptr);

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi_infile;
    fread(&bi_infile, sizeof(BITMAPINFOHEADER), 1, inptr);

    // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
    if (bf_infile.bfType != 0x4d42 || bf_infile.bfOffBits != 54 || bi_infile.biSize != 40 ||
        bi_infile.biBitCount != 24 || bi_infile.biCompression != 0)
    {
        fclose(outptr);
        fclose(inptr);
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }

    // create outfile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi_outfile = bi_infile;

    // creat outfile's BITMAPFILEHEADER
    BITMAPFILEHEADER bf_outfile = bf_infile;

    // update outfile's bi.Width
    bi_outfile.biWidth = bi_infile.biWidth * n;

    // update outfile's bi.Height
    bi_outfile.biHeight = bi_infile.biHeight * n;

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

    int padding_outfile = (4 - (bi_outfile.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    // update outfile's BITMAPINFOHEADER's size image
    bi_outfile.biSizeImage = (bi_outfile.biWidth * sizeof(RGBTRIPLE) + padding_outfile) * abs(bi_outfile.biHeight);

    // update outfile's BITMAPFILEHEADER's size
    bf_outfile.bfSize = bi_outfile.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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

    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        for(int j = 0; j < n; j++)
        {
            // set cursor to the beginning of line
            fseek(inptr, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (bi.biWidth * 3 + padding_infile) * i, SEEK_SET);

            // iterate over pixels in scanline
            for (int k = 0; k < bi.biWidth; k++)
            {
                // temporary storage
                RGBTRIPLE triple;

                // read RGB triple from infile
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

                // yterate over pixel n times
                for (int o = 0; o < n; o++)
                {
                    // write RGB triple to outfile
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }
            }

            // add padding
            for (int l = 0; l < padding_outfile; l++)
            {
                fputc(0x00, outptr);
            }
        }
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}
4
0

First, there are some simple issues with your code causing compile errors. If you read them carefully, they should be obvious. They're simple, so I'll leave them for you to resolve.

Next, a question. Which header should be written to the output file first?

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 .