I am struggling with this program. I cannot even check the headers with peek (I am receiving the message "could not recognize sample.bmp"). I am sure my program has several issues, but I would like to know first if I am updating my headers correctly. Then, is the loop right? thank you for you time.
I changed sizeimage : new_bi.biSizeImage = (new_width * sizeofRGBTRiple + newpadding) * abs(new height), but I have the same problem. I double check the new variables (paper and pencil) with the following example: original figure: 3 x 3, factor: 4; so, new width = 12, sizeofRGB= 3, no padding (because the width is 12), new height (abs)= 12 , the final calculation for new Sizeimage: (12 x 3 + 0) * 12 = 432, then new bf = 54 + 432 = 486. I think these results are correct; therefore, the formulas are also correct?
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char* argv[])
{
// ensure proper usage
if (argc != 4)
{
printf("Usage: ./copy infile outfile\n");
return 1;
// ensure infile is (likely) a 24-bit uncompressed BMP 4.0
}
// remember filenames
char* infile = argv[2];
char* outfile = argv[3];
int n = atoi(argv[1]);
if (n > 100 || n < 1)
{
printf("n must be between 1 and 100");
}
// 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);
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;
}
// update header
BITMAPFILEHEADER new_bf = bf;
BITMAPINFOHEADER new_bi = bi;
new_bi.biHeight = bi.biHeight * n;
new_bi.biWidth = bi.biWidth * n;
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int new_padding = (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
new_bi.biSizeImage = (new_bi.biWidth * sizeof(RGBTRIPLE) + new_padding) * abs(new_bi.biHeight);
new_bf.bfSize = sizeof(bf) + new_bi.biSizeImage;
// write outfile's BITMAPFILEHEADER
fwrite(&new_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&new_bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanlines
(This was only the first part)
bfsize
, it would benew_bf.bfSize = sizeof(bf) + sizeof(bi) + new_bi.biSizeImage
– wallek876 Jul 18 '15 at 11:14