0

I can't seem to figure out how to resolve "Header field biWidth doesn't match." error from the check50. I've gone through my code, but cannot figure out what to change in regards to my biWidth header.

Here's the log I'm getting on several of them from check50:

:( resizes small.bmp correctly when n is 2 Header field biWidth doesn't match. Expected 0x6, not 0x3

Log running ./resize 2 small.bmp outfile.bmp... checking that program exited with status 0...

 // Resizes a BMP file

#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 infile outfile\n");
    return 1;
}

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

if (n <= 0 || n >= 100)
{
    fprintf(stderr, "n must be between  0-100. Try again.");
    return 1;
}

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

// establish old and new values for bi & bf variables
int new_Width = bi.biWidth * n;
int new_Height = bi.biHeight * n;
int new_padding = (4 - (new_Width * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = ((sizeof(RGBTRIPLE) * new_Width) + new_padding) * abs(new_Height);
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);


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

// write outfile's BITMAPINFOHEADER
fwrite(&bi, 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++)
{
     // iterate over each row n times
    for (int x = 0; x < n; x++)
    {

        // 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 to outfile n times
            for (int z = 0; z < n; z++)
            {
            // write RGB triple to outfile
            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 < new_padding; k++)
    {
        fputc(0x00, outptr);
    }

    if (x < n-1)
    {
        fseek(inptr, -((bi.biWidth * 3) + padding), SEEK_CUR);
    }
}

}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;

}

2
  • Can you show the exact error message (including log) of check50? – Peter Pesch Nov 1 '18 at 20:27
  • 1
    I updated the question and added an example of the log. – Beyn18 Nov 1 '18 at 20:45
1

At the moment, the bitmapinfoheader you write to the output file contains the same biWidth and biHeight as your input file.

check50 is complaining about that unchanged biWidth ...

2
  • Thank you! I fixed that error. Now I'm getting that the bfSize doesn't match. Do I approach that more or less in the same manner? – Beyn18 Nov 1 '18 at 21:40
  • Yes. You will have to do that for several fields of both headers for the new field. But do keep in mind that you'll probably need some of the old values while resizing the image ... – Peter Pesch Nov 2 '18 at 0:04

You must log in to answer this question.

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