Advertisement
Jodyone

resize.c

Apr 17th, 2014
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.87 KB | None | 0 0
  1. /**
  2.  * resize.c
  3.  *
  4.  * Jody W Moore
  5.  *
  6.  * Computer Science 50
  7.  * Problem Set 5
  8.  *
  9.  * Copies a BMP piece by piece, just because.
  10.  */
  11.        
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14.  
  15. #include "bmp.h"
  16.  
  17. int main(int argc, char* argv[])
  18. {
  19.     // ensure proper usage
  20.     if (argc != 4)
  21.     {
  22.         printf("Usage: ./resize n infile outfile\n");
  23.         return 1;
  24.     }
  25.     if (!(argv[1] > 0))
  26.     {
  27.        printf("please enter a positive number\n");
  28.     }
  29.     int n = atoi(argv[1]);
  30.     // remember filenames
  31.     char* infile = argv[2];
  32.     char* outfile = argv[3];
  33.  
  34.     // open input file
  35.     FILE* inptr = fopen(infile, "r");
  36.     if (inptr == NULL)
  37.     {
  38.         printf("Could not open %s.\n", infile);
  39.         return 2;
  40.     }
  41.  
  42.     // open output file
  43.     FILE* outptr = fopen(outfile, "w");
  44.     if (outptr == NULL)
  45.     {
  46.         fclose(inptr);
  47.         fprintf(stderr, "Could not create %s.\n", outfile);
  48.         return 3;
  49.     }
  50.  
  51.     // read infile's BITMAPFILEHEADER
  52.     BITMAPFILEHEADER bf;
  53.     fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  54.    
  55.     // read infile's BITMAPINFOHEADER
  56.     BITMAPINFOHEADER bi;
  57.     fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  58.    
  59.     // local variables for calculations
  60.     int inputHeight = abs(bi.biHeight);
  61.     int outputHeight = bi.biHeight * n;
  62.     int inputWidth = bi.biWidth;
  63.     int outputWidth = inputWidth * n;
  64.     // determine padding for scanlines
  65.     int inputPadding =  (4 - (inputWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  66.     int outputPadding = (4 - (outputWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  67.     // output variables
  68.     int outputImageSize = (outputWidth * sizeof(RGBTRIPLE) + outputPadding) * abs(outputHeight);
  69.     int outputBFSize = outputImageSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  70.      
  71.     // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  72.     if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  73.         bi.biBitCount != 24 || bi.biCompression != 0)
  74.     {
  75.         fclose(outptr);
  76.         fclose(inptr);
  77.         fprintf(stderr, "Unsupported file format.\n");
  78.         return 4;
  79.     }
  80.      // write outfile's BITMAPFILEHEADER and BITMAPINFOHEADER
  81.      if ( n < 100)
  82.      {
  83.           bf.bfSize = outputBFSize;
  84.           bi.biWidth = outputWidth;
  85.           bi.biHeight = outputHeight;
  86.           bi.biSizeImage = outputImageSize;
  87.           fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  88.           fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);  
  89.      }
  90.     // for each row in the input
  91.     for (int row = 0; row < inputHeight; row++)
  92.     {
  93.         // iterate over infile's scanlines repeat n times
  94.         for (int scan = 0; scan < n; scan++)
  95.         {
  96.          
  97.             // iterate over pixels in the row
  98.             for (int rPix = 0; rPix < inputWidth; rPix++)
  99.             {
  100.                
  101.                  // temporary storage
  102.                  RGBTRIPLE triple ;
  103.                  // read a pixel from infile
  104.                  fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  105.                  // write n copies of pixel
  106.                  for (int wPix = 0; wPix < n ; wPix ++)
  107.                  {
  108.                         // write RGB triple to outfile
  109.                       fwrite(&triple, sizeof(RGBTRIPLE),1, outptr);
  110.                  }
  111.             }
  112.              for (int m = 0; m < outputPadding; m++)
  113.             {
  114.                 fputc(0x00, outptr);
  115.             }
  116.             int seeker = n-1 * (inputWidth * sizeof(RGBTRIPLE));
  117.             if ( scan == n-1)
  118.             {
  119.                 fseek(inptr, inputPadding, SEEK_CUR);
  120.             }
  121.             else
  122.             {
  123.                 fseek(inptr,seeker,SEEK_CUR);
  124.             }
  125.          
  126.       }
  127.          
  128.            
  129.        
  130.     }
  131.  
  132.     // close infile
  133.     fclose(inptr);
  134.  
  135.     // close outfile
  136.     fclose(outptr);
  137.  
  138.     // that's all folks
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement