Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * resize.c
- *
- * Computer Science 50
- * Problem Set 4
- *
- * Resizes a bmp by a factor ranging from 0 to 100 inclusive.
- * I am starting with the code supplied by CS50 course called
- * copy.c
- *
- * Changes to the code of copy.c written by
- * Jon Hjerting
- * March 2016
- *
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "bmp.h"
- int main(int argc, char* argv[])
- {
- // ensure proper usage
- if (argc != 4)
- {
- printf("Usage: ./test.c n infile outfile\n");
- return 1;
- }
- // check n value
- int n = atoi(argv[1]);
- if (n < 1 || n > 100)
- {
- printf("n must be an integer in the range [1, 100]");
- return 5;
- }
- // remember filenames
- char* infile = argv[2];
- char* outfile = argv[3];
- // open input file
- FILE* inptr = fopen(infile, "r");
- if (inptr == NULL)
- {
- // abort if file failed to open
- printf("Could not open %s.\n", infile);
- return 2;
- }
- // open output file
- FILE* outptr = fopen(outfile, "w");
- if (outptr == NULL)
- {
- // abort if opening new file failed
- 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 padding for original
- int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // original width and height
- int oldWidth = bi.biWidth;
- int oldHeight = abs(bi.biHeight);
- // update width
- bi.biWidth *= n;
- // update height
- bi.biHeight *= n;
- // determine new padding for enlarged file
- int newPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // update biSizeImage
- bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + newPadding) * abs(bi.biHeight);
- // update bfSize
- bf.bfSize = bi.biSizeImage + bf.bfOffBits;
- // write outfile's BITMAPFILEHEADER
- fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
- // write outfile's BITMAPINFOHEADER
- fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
- // pixel counter for position in array
- int pixelNo;
- // array for storing one line of pixels
- RGBTRIPLE lineArray[bi.biWidth];
- int arraySize = sizeof(RGBTRIPLE) * bi.biWidth;
- // iterate over infile's scanlines
- for (int line = 0; line < oldHeight; line++)
- {
- // counter for inserting pixel in lineArray
- pixelNo = 0;
- // iterate over pixels in scanline
- for (int pixel = 0; pixel < oldWidth; pixel++)
- {
- // temporary storage
- RGBTRIPLE triple;
- // read RGB triple from infile
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- // repeat n times to enlarge width by n
- for (int repeatpixel = 0; repeatpixel < n; repeatpixel++)
- {
- // write RGB triple to lineArray
- lineArray[pixelNo] = triple;
- pixelNo++;
- }
- }
- // repeat n times writing line to file
- for (int repeatLine = 0; repeatLine < n; repeatLine++)
- {
- // write lineArray to file
- fwrite(lineArray, arraySize, 1, outptr);
- // add padding to outfile
- for (int k = 0; k < newPadding; k++)
- {
- fputc(0x00, outptr);
- }
- }
- // skip over padding, if any
- fseek(inptr, padding, SEEK_CUR);
- }
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- // that's all folks
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement