Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * resize.c
- *
- * Computer Science 50
- * Problem Set 5
- *
- * Resizes a BMP.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "bmp.h"
- int main(int argc, char* argv[])
- {
- // ensure proper usage
- if (argc != 4)
- {
- printf("Usage: ./resize scale infile outfile\n");
- return 1;
- }
- // remember filenames and scale factor
- char* infile = argv[2];
- char* outfile = argv[3];
- // turn the scale into an int
- int scale = atoi(argv[1]);
- // 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 bf;
- fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
- // read infile's BITMAPINFOHEADER
- BITMAPINFOHEADER bi;
- fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
- BITMAPINFOHEADER old_bi = bi;
- // 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 outfile's header
- printf("Old file size: %i\n", bf.bfSize);
- bf.bfSize *= scale;
- printf("New file size: %i\n\n", bf.bfSize);
- printf("Old image size: %i\n", bi.biSizeImage);
- bi.biSizeImage *= scale;
- printf("New image size: %i\n\n", bi.biSizeImage);
- printf("Old image width: %i\n", bi.biWidth);
- bi.biWidth *= scale;
- printf("New image width: %i\n\n", bi.biWidth);
- printf("Old image height: %i\n", bi.biHeight);
- bi.biHeight *= scale;
- printf("New image height: %i\n\n", bi.biHeight);
- // write outfile's BITMAPFILEHEADER
- fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
- // write outfile's BITMAPINFOHEADER
- fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
- // determine padding for scanlines
- int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- int old_padding = (4 - (old_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // iterate over infile's scanlines
- for (int i = 0, biHeight = abs(old_bi.biHeight); i < biHeight; i++)
- {
- // repeat the row scale number of times
- for (int m = 0; m < scale; m++)
- {
- // get current position
- int f_pos = ftell(inptr);
- printf("%i\n", f_pos);
- // 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);
- // write RGB triple to outfile scale number of times
- for (int k = 0; k < scale; k++)
- {
- 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 l = 0; l < padding; l++)
- {
- fputc(0x00, outptr);
- }
- // go back to original line in infile
- long offset = -(old_bi.biWidth * sizeof(RGBTRIPLE)) - old_padding;
- fseek(inptr, offset, SEEK_CUR);
- // get current position
- f_pos = ftell(inptr);
- printf("%i\n", f_pos);
- }
- }
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- // that's all folks
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement