-
-
Save anonymous/3b63cf0120d32a4754d210524db62cfd to your computer and use it in GitHub Desktop.
resize.c adequately resizes 1x1 and 2x2 images to 4x4, fails resizing 1x1images to 2x2,3x3 or 5x5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* resize.c | |
* | |
* Computer Science 50 | |
* Problem Set 4 | |
* | |
* modification of copy.c | |
* Resizes a .bmp image by a factor of n | |
*/ | |
#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 resize factor, filenames | |
int n = atoi(argv[1]); | |
char* infile = argv[2]; | |
char* outfile = argv[3]; | |
// 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; | |
} | |
// determine infile padding for scanlines | |
int infpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; | |
// create copy of headers so as to modify copies for outfile | |
BITMAPFILEHEADER outbf = bf; | |
BITMAPINFOHEADER outbi = bi; | |
// modify BITMAPINFOHEADER elements Size, Width, Height | |
outbi.biWidth = bi.biWidth * n; | |
outbi.biHeight = bi.biHeight * n; | |
outbi.biSizeImage = (outbi.biWidth * sizeof(RGBTRIPLE)) * abs(outbi.biHeight); | |
// determine outfile padding for scanlines | |
int outfpadding = (4 - (outbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; | |
// Modify BITMAPFILEHEADER elements accounting for padding | |
outbf.bfSize = (((outbi.biWidth * sizeof(RGBTRIPLE)) + outfpadding) * abs(outbi.biHeight)) + 54; | |
// old: outbf.bfSize = (bf.bfSize - 54) * n * n + 54; | |
// start writing outfile | |
// write outfile's BITMAPFILEHEADER | |
fwrite(&outbf, sizeof(BITMAPFILEHEADER), 1, outptr); | |
// write outfile's BITMAPINFOHEADER | |
fwrite(&outbi, sizeof(BITMAPINFOHEADER), 1, outptr); | |
// iterate over infile's scanlines | |
for (int i = 0, infbiHeight = abs(bi.biHeight); i < infbiHeight; i++) | |
{ | |
// copy each scanline n times | |
for (int l = 0, infbiWidth = bi.biWidth; l < n; l++) | |
{ | |
// return to beginning of scanline after second iteration | |
if (l > 0) | |
{ | |
fseek(inptr, -(infbiWidth * 3), SEEK_CUR); | |
} | |
// iterate over pixels in scanline | |
for (int j = 0; j < infbiWidth; j++) | |
{ | |
// temporary storage | |
RGBTRIPLE triple; | |
// read RGB triple from infile | |
fread(&triple, sizeof(RGBTRIPLE), 1, inptr); | |
// write RGB triple to outfile n times | |
for (int t = 0; t < n; t++) | |
{ | |
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr); | |
} | |
} | |
// add outfile-size padding to each line of outfile | |
for (int k = 0; k < outfpadding; k++) | |
{ | |
fputc(0x00, outptr); | |
} | |
} | |
// skip over padding in infile, if any, after copying scanline n times | |
fseek(inptr, infpadding, SEEK_CUR); | |
} | |
// close infile | |
fclose(inptr); | |
// close outfile | |
fclose(outptr); | |
// that's all folks | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment