I posted my code a few weeks ago, and I have been working on it for a some hours today but I am really stuck as to why it is not working. I have not yet written the code for resizing down. It would be great help if anyone can point out where I am going wrong. Thanks in advance.
#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-factor infile outfile\n");
return 1;
}
//store factor and filenames
double f = atof(argv[1]);
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 & BITMAPINFOHEADER
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
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;
}
// copy bf and bi
BITMAPFILEHEADER cf = bf;
BITMAPINFOHEADER ci = bi;
//change dimensions for outfile file
ci.biWidth *= f;
ci.biHeight *= f;
// determine padding for infile & outfile scanlines
int inpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int outpadding = (4 - (ci.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//determine SizeImage and Size of outfile
ci.biSizeImage = (bi.biWidth + outpadding)* abs(ci.biHeight) * sizeof(RGBTRIPLE);
cf.bfSize = ci.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER & BITMAPINFOHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
RGBTRIPLE *copies = malloc(sizeof(RGBTRIPLE) * ci.biWidth);
if (f >= 1)
{
int counter = 0;
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
// 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);
for (int k = 0; k < f; k++)
{
copies[counter] = triple;
counter++;
}
}
//write copies in outfile f times
for (int j = 0; j < f; j ++)
{
fwrite(copies, (counter*sizeof(RGBTRIPLE)), 1, outptr);
//write outfile padding
for (int l = 0; l < outpadding; l++)
{
fputc(0x00, outptr);
}
}
// skip over padding, if any
fseek(inptr, inpadding, SEEK_CUR);
counter = 0;
}
return 5;
}
//
free(copies);
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}