Created
May 6, 2016 04:26
-
-
Save abor123/022b246babcac9ae3414339929e4c637 to your computer and use it in GitHub Desktop.
CS50 Resize Code
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
/** | |
* copy.c | |
* | |
* Computer Science 50 | |
* Problem Set 4 | |
* | |
* Copies a BMP piece by piece, 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: ./resize n infile outfile\n"); | |
return 1; | |
} | |
// remember 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 old_bf; | |
fread(&old_bf, sizeof(BITMAPFILEHEADER), 1, inptr); | |
// read infile's BITMAPINFOHEADER | |
BITMAPINFOHEADER old_bi; | |
fread(&old_bi, sizeof(BITMAPINFOHEADER), 1, inptr); | |
// ensure infile is (likely) a 24-bit uncompressed BMP 4.0 | |
if (old_bf.bfType != 0x4d42 || old_bf.bfOffBits != 54 || old_bi.biSize != 40 || | |
old_bi.biBitCount != 24 || old_bi.biCompression != 0) | |
{ | |
fclose(outptr); | |
fclose(inptr); | |
fprintf(stderr, "Unsupported file format.\n"); | |
return 4; | |
} | |
// create variable for the new header files | |
BITMAPFILEHEADER new_bf; | |
BITMAPINFOHEADER new_bi; | |
// determine padding for scanlines | |
int old_padding = (4 - (old_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; | |
int new_padding = (4 - ((old_bi.biWidth * n) * sizeof(RGBTRIPLE)) % 4) % 4; | |
// write outfile's BITMAPFILEHEADER | |
fwrite(&old_bf, sizeof(BITMAPFILEHEADER), 1, outptr); | |
// write outfile's BITMAPINFOHEADER | |
fwrite(&old_bi, sizeof(BITMAPINFOHEADER), 1, outptr); | |
// New element of BITMAPFILEHEADER | |
new_bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (old_bi.biWidth * n * sizeof(RGBTRIPLE) + new_padding) * abs(old_bi.biHeight) * n; | |
// New elements of BITMAPINFOHEADER | |
new_bi.biHeight = old_bi.biHeight * n; | |
new_bi.biWidth = old_bi.biWidth * n; | |
new_bi.biSizeImage = (old_bi.biWidth * n * sizeof(RGBTRIPLE) + new_padding) * abs(old_bi.biHeight) * n; | |
// write outfile's BITMAPFILEHEADER | |
//fwrite(&new_bf, sizeof(BITMAPFILEHEADER), 1, outptr); | |
// write outfile's BITMAPINFOHEADER | |
//fwrite(&new_bi, sizeof(BITMAPINFOHEADER), 1, outptr); | |
// iterate over infile's scanlines | |
for (int i = 0, biHeight = abs(old_bi.biHeight); i < biHeight; i++) | |
{ | |
//fseek(inptr, old_bi.biWidth * sizeof(RGBTRIPLE), SEEK_CUR); | |
// iterate over pixels in scanline | |
for (int j = 0; j < old_bi.biWidth; j++) | |
{ | |
// temporary storage | |
RGBTRIPLE triple; | |
// read RGB triple from infile | |
fread(&triple, sizeof(RGBTRIPLE), 1, inptr); | |
// resize width n times | |
for (int k = 0; k < n; k++) | |
{ | |
// write RGB triple to outfile | |
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr); | |
} | |
} | |
// skip over padding, if any | |
fseek(inptr, old_padding, SEEK_CUR); | |
// then add it back (to demonstrate how) | |
for (int m = 0; m < new_padding; m++) | |
{ | |
fputc(0x00, outptr); | |
} | |
} | |
// 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