0

I have multiple errors when using check50 and I am not sure why. I realize that multiple people already addressed similar error as mine, but I do not understand the explanations. I would appreciate if someone would be able to directly point to where I need to add/delete/edit parts of my code. Help is very appreciative, and as always, thank you.

Code:

// Copies a BMP file
// resizes a BMP via input of "n"

#include <stdio.h>
#include <stdlib.h>
#include <ctype.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;
    }

    // recieve resize factor, "n"
    int n = atoi(argv[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;
    }

    // changing parameters to n times
    BITMAPFILEHEADER nbf = bf;
    BITMAPINFOHEADER nbi = bi;
    nbi.biWidth *= n;
    nbi.biHeight *= n;

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

    nbi.biSizeImage = (nbi.biWidth * sizeof(RGBTRIPLE) + npadding) * abs(nbi.biHeight);
    nbf.bfSize = nbi.biSizeImage + nbf.bfOffBits;



    // 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
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        RGBTRIPLE line[nbi.biWidth];
        int lineIterator = 0;
        // 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); // [R G padding]


            // before writing to outfile we need to modify the pixels

            // write RGB triple to outfile ... n*w
            for (int x = 0; x < n; x++)
                line[lineIterator] = triple;
        }


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

        // then add it back (to demonstrate how)
        for (int k = 0; k < n; k++)
        {
            fwrite(line, sizeof(RGBTRIPLE), nbi.biWidth, outptr);

            // then add it back (to demonstrate how)
            for (int x = 0; x < npadding; x++)
            {
                fputc(0x00, outptr);
            }
        }
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}

Errors:

:) resize.c and bmp.h exist.
:) resize.c compiles.
:( doesn't resize small.bmp when n is 1
    Byte 5 of pixel data doesn't match. Expected 0xff, not 0x00
:( resizes small.bmp correctly when n is 2
    Header field bfSize doesn't match. Expected 0xae, not 0xb4
:( resizes small.bmp correctly when n is 3
    Header field bfSize doesn't match. Expected 0x132, not 0x144
:( resizes small.bmp correctly when n is 4
    Header field bfSize doesn't match. Expected 0x1e6, not 0x20a
:( resizes small.bmp correctly when n is 5
    Byte 4 of pixel data doesn't match. Expected 0x00, not 0x41
:( resizes large.bmp correctly when n is 2
    Byte 4 of pixel data doesn't match. Expected 0x00, not 0x24
:( resizes smiley.bmp correctly when n is 2
    Byte 4 of pixel data doesn't match. Expected 0xff, not 0x75
:( resizes smiley.bmp correctly when n is 3
    Byte 4 of pixel data doesn't match. Expected 0xff, not 0xf7
1
int npadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

should use nbi, not bi. That's why n=1 and n=5 don't complain about the header, those values are always equal then.

lineIterator is never incremented and stays at 0. Increment it, for example using

            for (int x = 0; x < n; x++, lineIterator++)
                line[lineIterator] = triple;

or

            for (int x = 0; x < n; x++)
                line[lineIterator++] = triple;

Second version is a bit tricky: ++ postfix operator as I used there increments the variable, but the expression evaluates to its pre-increment value, which is correct here. Prefix ++ would increment the same way, but evaluate to post-increment value.

There are more alternatives, like

            for (int x = 0; x < n; x++)
                line[j * n + x] = triple;
| improve this answer | |
  • i did fix the npadding issue, but the reason why I set lineIterator to 0 is because I need to declare it as a variable before putting it in a for loop. I have the for loop you mentioned after comment: // write RGB triple to outfile ... n*w I still have the same errors, any ideas as to why this is? – tsinelnikova Aug 10 '18 at 11:31
  • I don't question the int lineIterator = 0; line - that's fine. But your code does not change that variable later. I offered three ways to fix that, one of them not requiring that variable at all, does none of them work for you? – Blauelf Aug 10 '18 at 13:07
  • Ah, yes it finally works! Thank you very much for walking it through with me! – tsinelnikova Aug 10 '18 at 18:34

You must log in to answer this question.

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