0

So my code is working fine - images resize exactly as they should (as far as my eye can tell) for images both with and without padding, but my header data is wrong when I compare to staff.bmp. My output is the same as staff.bmp except from biSizeImage and bfSize which have the same values as small.bmp and I am unsure as to why they are not updating correctly. Here is my code up to the point of writing the header files:

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


// remember filenames
char* infile = argv[2];
char* outfile = argv[3];
int n = atoi(argv[1]);
if (n < 1)
{
    printf ("n must be an integer\n");
    return 4;
}

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

// create large's BITMAPFILEHEADER
BITMAPFILEHEADER bflarge = bf;
// create large's BITMAPINFOHEADER
BITMAPINFOHEADER bilarge = bi;

// scale width and height
bilarge.biWidth *= n;
bilarge.biHeight *= n;

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

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

// change biSizeImage and bfSize
bilarge.biSizeImage = (bilarge.biWidth * sizeof(RGBTRIPLE)) * abs(bilarge.biHeight);
bflarge.bfSize = sizeof(bf) + sizeof(bi) + bilarge.biSizeImage;

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

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

Any help as to what is going on would be much appreciated

Thanks!

6
  • biSizeImage is the size of the image including the padding.
    – kzidane
    Sep 2 '15 at 13:00
  • Yes, sorry forgot that the output in this particular case has no padding and thats why i left it out. even with the padding included it still gives the old values
    – GCOTT94
    Sep 2 '15 at 13:35
  • how are you calculating biImageSize now?
    – kzidane
    Sep 2 '15 at 13:45
  • bilarge.biSizeImage = ((bilarge.biWidth * sizeof(RGBTRIPLE) + paddinglarge) * abs(bilarge.biHeight);
    – GCOTT94
    Sep 2 '15 at 13:46
  • and peek still tells you that the do not match their correspondents in the staff's? are you sure you saved and compiled your new source code? are you sure the BMPs were resized by the same factor?
    – kzidane
    Sep 2 '15 at 14:03

You must log in to answer this question.

Browse other questions tagged .