-
-
Save anonymous/6717633f6f48c11c6974359db8197f5c to your computer and use it in GitHub Desktop.
resize.c - shared from CS50 IDE
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include "bmp.h" | |
int main(int argc, char* argv[]) | |
{ | |
// ensure proper usage | |
if (argc != 4) | |
{ | |
printf("Usage: ./copy infile outfile\n"); | |
return 1; | |
} | |
// String to an int | |
int rescale = atoi(argv[1]); | |
// ensure rescale is a positive int, less than or equal to 100 | |
if (rescale < 1 || rescale > 100) | |
{ | |
printf("Rescale must be a positive int, less than or equal to 100!\n"); | |
return 2; | |
} | |
// remember filenames | |
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 3; | |
} | |
// open output file | |
FILE* outptr = fopen(outfile, "w"); | |
if (outptr == NULL) | |
{ | |
fclose(inptr); | |
fprintf(stderr, "Could not create %s.\n", outfile); | |
return 4; | |
} | |
// 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 | |
BITMAPFILEHEADER bfnew; | |
BITMAPINFOHEADER binew; | |
bfnew = bf; | |
binew = bi; | |
binew.biWidth *= rescale; | |
binew.biHeight *= rescale; | |
// determine padding for scanline | |
int oldpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; | |
int padding = (4 - (binew.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; | |
// update bi.biSizeImage && bfSize for new image | |
binew.biSizeImage = abs(binew.biHeight) * (abs(binew.biWidth) * sizeof(RGBTRIPLE) + padding); | |
bfnew.bfSize = bfnew.bfOffBits + binew.biSizeImage; | |
// write outfile's BITMAPFILEHEADER | |
fwrite(&bfnew, sizeof(BITMAPFILEHEADER), 1, outptr); | |
// write outfile's BITMAPINFOHEADER | |
fwrite(&binew, sizeof(BITMAPINFOHEADER), 1, outptr); | |
// iterate over infile's scanlines | |
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++) | |
{ | |
// iterate rescale no. times | |
for (int x =0; x < rescale; x++) | |
{ | |
// iterate over pixels in scanline | |
for (int j = 0; j < bi.biWidth; j++) | |
{ | |
// declare a variable that can store negative values | |
long offset = bi.biWidth * sizeof(RGBTRIPLE); | |
// temporary storage | |
RGBTRIPLE triple; | |
// read RGB triple from infile | |
fread(&triple, sizeof(RGBTRIPLE), 1, inptr); | |
for (int l = 0; l < rescale; l++) | |
{ | |
// write RGB triple to outfile | |
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr); | |
} | |
// go back to the begining of scanline | |
fseek(inptr, -offset, SEEK_CUR); | |
// then add it back (to demonstrate how) | |
for (int k = 0; k < padding; k++) | |
{ | |
fputc(0x00, outptr); | |
} | |
// go to the next row | |
fseek(inptr, bi.biWidth * sizeof(RGBTRIPLE) + oldpadding, SEEK_CUR); | |
} | |
} | |
// skip over padding, if any | |
fseek(inptr, oldpadding, SEEK_CUR); | |
} | |
// close infile | |
fclose(inptr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment