I'm working on this code for over two weeks and I still don't know where the problem is. My guess is that I have a problem in the Re-copy method (resizing vertically) or in the padding. I screw up every time I change them. so any help is very appreciated. I'm out of ideas here
/**
* resize.c
*/
#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
int factor = atoi(argv[1]);
char* infile = argv[2];
char* outfile = argv[3];
// check available scale/factor number
if ((factor <= 0) || (factor >= 100))
{
printf("valid scale number is between 0 and 100\n");
return 10 ;
}
// 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);
// 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;
}
// update header info for outfile - change these values of the height and width
// keeping track of both original and new files
BITMAPFILEHEADER out_bf;
BITMAPINFOHEADER out_bi;
// change file size (bf.size -kolo bl padding) - image size(bi.imagesize) - width(biwidth - padding) - height(biheight)
out_bi.biWidth = bi.biWidth * factor ;
out_bi.biHeight = bi.biHeight * factor ;
// padding difference formulas
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int out_padding = (4 - (out_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// continue adjusting header files
out_bi.biSizeImage = (out_bi.biWidth * sizeof(RGBTRIPLE) + out_padding) * abs(out_bi.biHeight) ;
out_bf.bfSize = out_bi.biSizeImage + 54 ; // 54 = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
// write outfile's headers
fwrite(&out_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
fwrite(&out_bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanlines - iterate over infile's scanlines (rows
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
// every row needs to be copied (n) times = factor
for (int v = 0 ; v < factor ; v++)
{
// iterate over pixels in scanline
for (int j = 0; j < bi.biWidth; j++)
{
RGBTRIPLE triple;
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// resize it horizontally
for (int h = 0 ; h < factor ; h++)
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
// skip padding
fseek(inptr, padding, SEEK_CUR);
// add it back
for (int k = 0; k < out_padding; k++)
{
fputc(0x00, outptr);
}
// Re-copy method
fseek(inptr, -(out_bi.biWidth * sizeof(RGBTRIPLE) + out_padding), SEEK_CUR);
}
}
fclose(inptr);
fclose(outptr);
return 0;
}