Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * copy.c
- *
- * Computer Science 50
- * Problem Set 4
- *
- * Resizes a bmp image by a factor n.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "bmp.h"
- int main(int argc, char* argv[])
- {
- // ensure proper usage
- if (argc != 4)
- {
- printf("Usage: ./copy n infile outfile\n");
- return 1;
- }
- // remember filenames
- char* infile = argv[2];
- char* outfile = argv[3];
- // remembers factor to resize
- int n = atoi(argv[1]);
- if (n <= 0 || n > 100)
- {
- printf("n must be a positive integer less than or equal to 100\n");
- return 2;
- }
- // 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 bfold;
- fread(&bfold, sizeof(BITMAPFILEHEADER), 1, inptr);
- // read infile's BITMAPINFOHEADER
- BITMAPINFOHEADER biold;
- fread(&biold, sizeof(BITMAPINFOHEADER), 1, inptr);
- // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
- if (bfold.bfType != 0x4d42 || bfold.bfOffBits != 54 || biold.biSize != 40 ||
- biold.biBitCount != 24 || biold.biCompression != 0)
- {
- fclose(outptr);
- fclose(inptr);
- fprintf(stderr, "Unsupported file format.\n");
- return 5;
- }
- // creates new headers
- BITMAPFILEHEADER bfnew = bfold;
- BITMAPINFOHEADER binew = biold;
- // calculates resized dimensions
- binew.biWidth = biold.biWidth * n;
- binew.biHeight = biold.biHeight * n;
- // determines the padding of infile
- int padding_old = (4 - (biold.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // determines padding for the new image
- int padding_new = (4 - (binew.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // updates headers for the outfile
- binew.biSizeImage = (binew.biWidth + padding_new) * abs(binew.Height);
- bfnew.bfSize = 54 + 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++)
- {
- for (int a = 0; a < n; a++)
- {
- // iterate over pixels in scanline
- for (int j = 0; j < bi.biWidth; j++)
- {
- // temporary storage
- RGBTRIPLE triple;
- // read RGB triple from infile
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- // write RGB triple to outfile
- for (int l = 0; l < n; l++)
- {
- fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
- }
- }
- // skip over padding, if any
- fseek(inptr, padding_new, SEEK_CUR);
- // then add it back (to demonstrate how)
- for (int k = 0; k < padding_new; k++)
- {
- fputc(0x00, outptr);
- }
- }
- }
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- // that's all folks
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement