Advertisement
Guest User

Untitled

a guest
Sep 27th, 2014
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.03 KB | None | 0 0
  1. /**
  2.  * resize.c
  3.  *
  4.  * Computer Science 50
  5.  * Problem Set 5
  6.  *
  7.  * Copies a BMP piece by piece, and creates a another bmp with requested dimensions.
  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 scale infile outfile\n");
  21.         return 1;
  22.     }
  23.    
  24.     // receive scale value and check for validity
  25.     int n = atoi(argv[1]);
  26.     if ( n < 1 || n > 100)
  27.     {
  28.         printf("Enter a value for scale between 1 and 100");
  29.     }
  30.    
  31.     // remember filenames
  32.     char* infile = argv[2];
  33.     char* outfile = argv[3];
  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 2;
  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 3;
  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 4;
  68.     }
  69.    
  70.     // determine padding for scanlines for infile
  71.     int paddingInfile =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  72.    
  73.     // updating BITMAPINFOHEADER height and width varibles inorder to resize
  74.     //int initialHeight = abs(bi.biHeight); // to be used for modifying offset
  75.     int initialWidth = bi.biWidth;
  76.     bi.biHeight *= n;
  77.     bi.biWidth *= n;
  78.    
  79.     // determine padding for scanlines for outfile
  80.     int paddingOutfile =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  81.  
  82.     // update BITMAPINFOHEADER biSizeImage and BITMAPFILEHEADER bfSize
  83.     bi.biSizeImage = ((bi.biWidth)* abs(bi.biHeight) * sizeof(RGBTRIPLE)) + paddingOutfile;
  84.     bf.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) + bi.biSizeImage;
  85.        
  86.     // write outfile's BITMAPFILEHEADER
  87.     fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  88.  
  89.     // write outfile's BITMAPINFOHEADER
  90.     fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  91.    
  92.     // offset variable used to shift fseek
  93.     int offset = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  94.    
  95.     // iterate over infile's scanlines
  96.     for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  97.     {      
  98.         // iterate over pixels in scanline
  99.         for (int j = 0; j < initialWidth; j++)
  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. Iterate n times to copy the pixels (n-1) times horizontally.
  108.             for(int k = 0; k < n; k++)
  109.             {
  110.                 fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  111.             }        
  112.         }
  113.  
  114.         // then add it back
  115.         for (int k = 0; k < paddingOutfile; k++)
  116.         {
  117.             fputc(0x00, outptr);
  118.         }
  119.        
  120.         ////////////////////////TEST///////////////////////////
  121.         //printf("i : %i\n",i);
  122.         //printf("offset : %i\n",offset);
  123.        
  124.         //Modifying offset inorder to change fseek SEEK_SET psition
  125.         if(i%n == (n-1))
  126.         {
  127.             offset += ((initialWidth) * sizeof(RGBTRIPLE)) + paddingInfile;
  128.         }
  129.        
  130.         // skip over padding, if any
  131.         fseek(inptr, offset, SEEK_SET);
  132.     }
  133.  
  134.     // close infile
  135.     fclose(inptr);
  136.  
  137.     // close outfile
  138.     fclose(outptr);
  139.  
  140.     // that's all folks
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement