Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.25 KB | None | 0 0
  1. /**
  2.  * copy.c
  3.  *
  4.  * Computer Science 50
  5.  * Problem Set 4
  6.  *
  7.  * Copies a BMP piece by piece, just because.
  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 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.     // open input file
  30.     FILE* inptr = fopen(infile, "r");
  31.     if (inptr == NULL)
  32.     {
  33.         printf("Could not open %s.\n", infile);
  34.         return 2;
  35.     }
  36.  
  37.     // open output file
  38.     FILE* outptr = fopen(outfile, "w");
  39.     if (outptr == NULL)
  40.     {
  41.         fclose(inptr);
  42.         fprintf(stderr, "Could not create %s.\n", outfile);
  43.         return 3;
  44.     }
  45.  
  46.     // read infile's BITMAPFILEHEADER
  47.     BITMAPFILEHEADER bf;
  48.     fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  49.  
  50.     // read infile's BITMAPINFOHEADER
  51.     BITMAPINFOHEADER bi;
  52.     fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  53.  
  54.     // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  55.     if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  56.         bi.biBitCount != 24 || bi.biCompression != 0)
  57.     {
  58.         fclose(outptr);
  59.         fclose(inptr);
  60.         fprintf(stderr, "Unsupported file format.\n");
  61.         return 4;
  62.     }
  63.    
  64.     //Save original values just in case (UNLOCK AS NEEDED OR ELSE STUPID UNUSED VARIABLE ERROR)
  65.     LONG old_biWidth = bi.biWidth;
  66.     LONG old_biHeight = bi.biHeight;
  67.     int old_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  68.     //DWORD old_biSizeImage = bi.biSizeImage;
  69.     //DWORD old_bfSize = bf.bfSize;
  70.    
  71.     //Modify headers for outfile
  72.     bi.biWidth = bi.biWidth * n;
  73.     bi.biHeight =  bi.biHeight * n;
  74.     int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  75.     bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);
  76.     bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  77.    
  78.     // write outfile's BITMAPFILEHEADER
  79.     fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  80.  
  81.     // write outfile's BITMAPINFOHEADER
  82.     fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  83.  
  84.     // temporary storage variables
  85.     RGBTRIPLE triple;
  86.     RGBTRIPLE array[bi.biWidth]; //size of modified height
  87.  
  88.     // iterate over infile's scanlines
  89.     for (int i = 0; i < abs(old_biHeight); i++)
  90.     {
  91.         // copy row into array
  92.         for (int j = 0; j < old_biWidth; j++)
  93.         {
  94.             // read RGB triple from infile
  95.             fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  96.             array[j] = triple; //put it in an array
  97.         }
  98.        
  99.         //skip over any padding
  100.         fseek(inptr, old_padding, SEEK_CUR);
  101.  
  102.         for (int w = 0; w < bi.biWidth; w++)
  103.         {
  104.             triple = array[w];
  105.             fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  106.         }
  107.         //add padding new padding
  108.         for (int k = 0; k < padding; k++)
  109.         {
  110.             fputc(0x00, outptr);
  111.         }
  112.          
  113.     }
  114.    
  115.     // close infile
  116.     fclose(inptr);
  117.  
  118.     // close outfile
  119.     fclose(outptr);
  120.  
  121.     // that's all folks
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement