Advertisement
jpgiergiel

resize pset 4

Aug 17th, 2015
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.40 KB | None | 0 0
  1. /**
  2.  * resize.c
  3.  *
  4.  * Computer Science 50
  5.  * Problem Set 4
  6.  *
  7.  * resize bmp by 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: ./resize factor infile outfile\n");
  21.         return 1;
  22.     }
  23.  
  24.     // remember filenames
  25.     int n = atoi(argv[1]);
  26.     char* infile = argv[2];
  27.     char* outfile = argv[3];
  28.    
  29.     if (n < 1 || n > 100)
  30.     {
  31.         printf("Please enter resize factor between 1 & 100");
  32.         return 2;
  33.     }
  34.  
  35.     // open input file
  36.     FILE* inptr = fopen(infile, "r");
  37.     if (inptr == NULL)
  38.     {
  39.         printf("Could not open %s.\n", infile);
  40.         return 3;
  41.     }
  42.  
  43.     // open output file
  44.     FILE* outptr = fopen(outfile, "w");
  45.     if (outptr == NULL)
  46.     {
  47.         fclose(inptr);
  48.         fprintf(stderr, "Could not create %s.\n", outfile);
  49.         return 4;
  50.     }
  51.  
  52.     // read infile's BITMAPFILEHEADER
  53.     BITMAPFILEHEADER bf;
  54.     fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  55.  
  56.     // read infile's BITMAPINFOHEADER
  57.     BITMAPINFOHEADER bi;
  58.     fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  59.  
  60.     // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  61.     if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  62.         bi.biBitCount != 24 || bi.biCompression != 0)
  63.     {
  64.         fclose(outptr);
  65.         fclose(inptr);
  66.         fprintf(stderr, "Unsupported file format.\n");
  67.         return 5;
  68.     }
  69.    
  70.         // determine padding for scanlines
  71.     int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  72.    
  73.     //create new header files
  74.     BITMAPFILEHEADER newbf = bf;
  75.     BITMAPINFOHEADER newbi = bi;
  76.    
  77.     newbi.biWidth = bi.biWidth * n;
  78.     newbi.biHeight = 0x100000000 - (abs(bi.biHeight) * n);
  79.    
  80.     int newPadding = (4 - (newbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  81.    
  82.     newbi.biSizeImage = (newbi.biWidth * abs(newbi.biHeight)) * sizeof(RGBTRIPLE) + (newPadding * abs(newbi.biHeight));
  83.     newbf.bfSize = newbi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  84.  
  85.     // write outfile's BITMAPFILEHEADER
  86.     fwrite(&newbf, sizeof(BITMAPFILEHEADER), 1, outptr);
  87.  
  88.     // write outfile's BITMAPINFOHEADER
  89.     fwrite(&newbi, sizeof(BITMAPINFOHEADER), 1, outptr);
  90.  
  91.  
  92.  
  93.     // iterate over infile's scanlines
  94.     for (int i = 0, biHeight = abs(newbi.biHeight); i < biHeight; i++)
  95.     {
  96.         // iterate over pixels in scanline
  97.         for (int j = 0; j < n; j++)
  98.         {
  99.             for (int k = 0; k < bi.biWidth; k++)
  100.             {
  101.                 // temporary storage
  102.                 RGBTRIPLE triple;
  103.  
  104.                 // read RGB triple from infile
  105.                 fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  106.  
  107.                 // write RGB triple to outfile
  108.                 for (int l = 0; l < n; l++)
  109.                 {    
  110.                     fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  111.                 }
  112.             }
  113.  
  114.             // skip over padding, if any
  115.             fseek(inptr, padding, SEEK_CUR);
  116.  
  117.             // then add it back (to demonstrate how)
  118.             for (int k = 0; k < newPadding; k++)
  119.             {
  120.                 fputc(0x00, outptr);
  121.             }
  122.         }
  123.     }
  124.  
  125.     // close infile
  126.     fclose(inptr);
  127.  
  128.     // close outfile
  129.     fclose(outptr);
  130.  
  131.     // that's all folks
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement