Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * resize.c
- *
- * Computer Science 50
- * Problem Set 4
- *
- * Resizes the file whose name is passed to it as a commandline argument.
- */
- #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
- char* infile = argv[2];
- char* outfile = argv[3];
- // change n into a number so we can use it as such
- int n = atoi(argv[1]);
- if (n <= 0 || n > 100)
- {
- printf("The value provided as the size must be that of 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 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;
- }
- // remember the old padding
- int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // remember dimensions of original image
- int img_height = bi.biHeight;
- int img_width = bi.biWidth;
- // calculate dimensions of resized image
- bi.biHeight *= n;
- bi.biWidth *= n;
- int resized_height = bi.biHeight;
- int resized_width = bi.biWidth;
- // determine scanline padding
- int new_padding = (4 - (resized_width * sizeof(RGBTRIPLE)) % 4) % 4;
- // update header info
- bi.biSizeImage = ((resized_width * sizeof(RGBTRIPLE) + new_padding) * abs(resized_height));
- bf.bfSize = bi.biSizeImage + (sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER));
- // write outfile's BITMAPFILEHEADER
- fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
- // write outfile's BITMAPINFOHEADER
- fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
- // iterate over infile's scanlines
- for (int i = 0, biHeight = abs(img_height); i < biHeight; i++)
- {
- // iterate over pixels in scanline
- for (int j = 0; j < img_width; j++)
- {
- // temporary storage
- RGBTRIPLE triple;
- // read RGB triple from infile
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- for (int index = 0; index <= n - 1; index++)
- {
- fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
- }
- }
- for (int k = 0; k < new_padding; k++)
- {
- fputc(0x00, outptr);
- }
- fseek(inptr, padding, SEEK_CUR);
- // send infile cursor back
- // before reading the next RGBTRIPLE data
- long offset = img_width * sizeof(RGBTRIPLE);
- fseek(inptr, - 1 * offset, SEEK_CUR);
- // vertical resize
- // for each row
- for (int l = 0; l < biHeight; l++)
- {
- // temporary storage for triples data
- RGBTRIPLE triple;
- int m;
- for (int iii = 0; iii < n - 1; iii++)
- {
- for (m = 0; m < img_width; m++)
- {
- // read triples data from infile
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- // for n - 1 times:
- // write pixels and padding to outfile:
- for (int index = 0; index < n; index++)
- {
- // write pixels to outfile
- fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
- }
- }
- // write padding to outfile
- for (int k = 0; k < new_padding; k++)
- {
- fputc(0x00, outptr);
- }
- // send infile cursor back
- fseek(inptr, -1 * (offset * m), SEEK_CUR);
- }
- // write pixels and padding to outfile
- // skip over infile padding:
- for (m = 0; m < img_width; m++)
- {
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- for (int index = 0; index < n; index++)
- {
- fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
- }
- }
- for (int k = 0; k < new_padding; k++)
- {
- fputc(0x00, outptr);
- }
- 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