I have tried many days on resize. I take a break 1-2 week because of external reasons. I Watched zamyla many times and read many articles on stackexchange. but I could not understand. Here is my code. What is going on with me?
/**
* resize.c
*
* Computer Science 50
* Problem Set 4
*
* Copies a BMP and resize bmp, just because.
*/
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char* argv[])
{
// ensure proper usage
if (argc != 4)
{
printf("Usage: ./copy resize infile outfile\n");
return 1;
}
// remember filenames
char* infile = argv[2];
char* outfile = argv[3];
//Getting resize value
char* s = argv[1];
int resize = atoi(s);
//Check resize value whether positive and less than or equal to 100
if (!resize < 0 || resize > 100)
{
printf("resize must be 0 < resize <= 100");
return 1;
}
// 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;
}
//Out header info
BITMAPFILEHEADER out_bf;
BITMAPINFOHEADER out_bi;
//Updating header info
out_bi.biWidth = bi.biWidth * resize;
out_bi.biHeight = bi.biHeight * resize;
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), resize*1, outptr);
// determine padding for scanlines
int out_padding = (4 - (out_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
out_bi.biSizeImage = bi.biSizeImage * resize + out_padding;
out_bf.bfSize = out_bi.biSizeImage + out_bf.bfOffBits;
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(out_bi.biHeight); i < biHeight; i++)
{
// iterate over pixels in scanline
for (int j = 0; j < out_bi.biWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), resize*1, outptr);
}
// skip over padding, if any
fseek(inptr, out_padding, SEEK_CUR);
// then add it back (to demonstrate how)
for (int k = 0; k < out_padding; k++)
{
fputc(0x00, outptr);
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// that's all folks
return 0;
}