Advertisement
Guest User

resize.c

a guest
May 7th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.50 KB | None | 0 0
  1. /**
  2.  * copy.c
  3.  *
  4.  * Computer Science 50
  5.  * Problem Set 4
  6.  *
  7.  * Resizes a bmp image by a factor n.
  8.  */
  9.        
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. #include "bmp.h"
  14.  
  15. int main(int argc, char* argv[])
  16. {
  17.     // ensure proper usage
  18.     if (argc != 4)
  19.     {
  20.         printf("Usage: ./copy n infile outfile\n");
  21.         return 1;
  22.     }
  23.  
  24.     // remember filenames
  25.     char* infile = argv[2];
  26.     char* outfile = argv[3];
  27.    
  28.     // remembers factor to resize
  29.     int n = atoi(argv[1]);
  30.     if (n <= 0 || n > 100)
  31.     {
  32.         printf("n must be a positive integer less than or equal to 100\n");
  33.         return 2;
  34.     }
  35.  
  36.     // open input file
  37.     FILE* inptr = fopen(infile, "r");
  38.     if (inptr == NULL)
  39.     {
  40.         printf("Could not open %s.\n", infile);
  41.         return 3;
  42.     }
  43.  
  44.     // open output file
  45.     FILE* outptr = fopen(outfile, "w");
  46.     if (outptr == NULL)
  47.     {
  48.         fclose(inptr);
  49.         fprintf(stderr, "Could not create %s.\n", outfile);
  50.         return 4;
  51.     }
  52.  
  53.     // read infile's BITMAPFILEHEADER
  54.     BITMAPFILEHEADER bfold;
  55.     fread(&bfold, sizeof(BITMAPFILEHEADER), 1, inptr);
  56.  
  57.     // read infile's BITMAPINFOHEADER
  58.     BITMAPINFOHEADER biold;
  59.     fread(&biold, sizeof(BITMAPINFOHEADER), 1, inptr);
  60.  
  61.     // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  62.     if (bfold.bfType != 0x4d42 || bfold.bfOffBits != 54 || biold.biSize != 40 ||
  63.         biold.biBitCount != 24 || biold.biCompression != 0)
  64.     {
  65.         fclose(outptr);
  66.         fclose(inptr);
  67.         fprintf(stderr, "Unsupported file format.\n");
  68.         return 5;
  69.     }
  70.    
  71.     // creates new headers
  72.     BITMAPFILEHEADER bfnew = bfold;
  73.     BITMAPINFOHEADER binew = biold;
  74.    
  75.     // calculates resized dimensions
  76.     binew.biWidth = biold.biWidth * n;
  77.     binew.biHeight = biold.biHeight * n;
  78.    
  79.     // determines the padding of infile
  80.     int padding_old =  (4 - (biold.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  81.    
  82.     // determines padding for the new image
  83.     int padding_new = (4 - (binew.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  84.    
  85.     // updates headers for the outfile
  86.     binew.biSizeImage = (binew.biWidth + padding_new) * abs(binew.Height);
  87.     bfnew.bfSize = 54 + binew.biSizeImage;
  88.    
  89.     // write outfile's BITMAPFILEHEADER
  90.     fwrite(&bfnew, sizeof(BITMAPFILEHEADER), 1, outptr);
  91.  
  92.     // write outfile's BITMAPINFOHEADER
  93.     fwrite(&binew, sizeof(BITMAPINFOHEADER), 1, outptr);
  94.  
  95.     // iterate over infile's scanlines
  96.     for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  97.     {
  98.         for (int a = 0; a < n; a++)
  99.         {
  100.             // iterate over pixels in scanline
  101.             for (int j = 0; j < bi.biWidth; j++)
  102.             {
  103.                 // temporary storage
  104.                 RGBTRIPLE triple;
  105.  
  106.                 // read RGB triple from infile
  107.                 fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  108.  
  109.                 // write RGB triple to outfile
  110.                 for (int l = 0; l < n; l++)
  111.                 {
  112.                     fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  113.                 }
  114.             }
  115.  
  116.             // skip over padding, if any
  117.             fseek(inptr, padding_new, SEEK_CUR);
  118.  
  119.             // then add it back (to demonstrate how)
  120.             for (int k = 0; k < padding_new; k++)
  121.             {
  122.                 fputc(0x00, outptr);
  123.             }
  124.         }
  125.     }
  126.  
  127.     // close infile
  128.     fclose(inptr);
  129.  
  130.     // close outfile
  131.     fclose(outptr);
  132.  
  133.     // that's all folks
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement