I am currently writing a program that will resize a bitmap file by a factor of n(n being a value the user enters). The program takes in three values: the resize value, the input file and the output file. The program compiles and everything, however, when I run the program, I keep getting "bogus header data"within the output file.Here is the code I have:
Code:
char* number=argv[1];
char* infile = argv[2];
char* outfile = argv[3];
factor = (int) number;
if (factor>100 && factor<0)
{
printf("Please enter in a number greater than 0 and less than 100 ");
return 2;
}
// open input file
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
printf("Could not open %s.\n", infile);
return 3;
}
// open output file
FILE* outptr = fopen(outfile, "w");
if (outptr == NULL)
{
fclose(inptr);
fprintf(stderr, "Could not create %s.\n", outfile);
return 4;
}
// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
Oldwidth=bi.biWidth;
OldHeight=bi.biHeight;
oldSizeImage=bi.biSizeImage;
bi.biWidth=bi.biWidth*factor;
bi.biHeight=bi.biHeight*factor;
oldpadding= (4-(Oldwidth*sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage=(bi.biWidth * bi.biHeight) * 3 ;
// 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;
}
RGBTRIPLE *buffer = malloc(sizeof(RGBTRIPLE) * bi.biWidth);
// 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;
int element=0;
// iterate over infile's scanlines
for (int i = 0; i<abs(OldHeight); i++)
{
// iterate over pixels in scanline
for (int j = 0; j < Oldwidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
//iterate over each pixel factor times
for (int k = 0; k <factor; k++)
{
buffer[element] = triple;
element++;
}
// skip over padding, if any
fseek(inptr, padding, SEEK_CUR);
}
for (int r = 0; r < factor; r++)
{
// write RGB triple to outfile
fwrite(buffer, sizeof(RGBTRIPLE), bi.biWidth, outptr);
// write padding to outfile
for (int p = 0; p < padding; p++)
fputc(0x00, outptr);
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
return 0;
any help would be greatly appreciated. Not sure if I should have included my bmp.h.But if anyone needs to see it to make a determination on what maybe the problem, let me know. Thanks