the code is working with small.bmp, but not with large.bmp and i can't understand why. Even resizing smiley.bmp, when n is too big it's not working. Here's the code:
// Copies a BMP file
include
include
include "bmp.h"
int main(int argc, char *argv[]) { // ensure proper usage if (argc != 4) { fprintf(stderr, "Usage: ./resize n infile outfile\n"); return 1; }
// remember filenames
char *infile = argv[2];
char *outfile = argv[3];
int n = atoi(argv[1]);
//ensure n <= 100
if (n < 1 || n > 100)
{
fprintf(stderr, "Usage: ./resize n infile outfile\n");
return 6;
}
// 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
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;
}
//determine oldpadding
int oldpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//save width and height for later usage
int width = bi.biWidth;
int height = abs(bi.biHeight);
//update file headers and padding
bi.biWidth *= n;
bi.biHeight *= n;
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
//create array to store pixels for resizing
RGBTRIPLE *pixels = malloc(sizeof(RGBTRIPLE) * bi.biWidth);
// iterate over infile's scanlines
for (int i = 0, p; i < height; i++)
{
//set p = 0 to iterate over the array where pixels are stored
p = 0;
// iterate over pixels in scanline
for (int j = 0; j < width; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple into the array n times
for (int k = 0; k < n; k++)
{
pixels[p] = triple;
p++;
}
}
// write array to outfile n times and add padding for each line
for (int a = 0; a < n; a++)
{
fwrite(pixels, sizeof(RGBTRIPLE) * bi.biWidth, 1, outptr);
for (int d = 0; d < padding; d++)
{
fputc(0x00, outptr);
}
}
//free memory of array
free(pixels);
// skip over oldpadding, if any
fseek(inptr, oldpadding, SEEK_CUR);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}