Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.34 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. #include "bmp.h"
  13.  
  14. int main(int argc, char * argv[]) {
  15.  // ensure proper usage
  16.   if (argc != 4) {
  17.     printf("Usage: ./copy infile outfile\n");
  18.     return 1;
  19.   }
  20.  
  21.   if (atoi(argv[1]) > 100) {
  22.     printf("Please enter number smaller than 100...");
  23.     return 1;
  24.   }
  25.  
  26.  
  27.   // remember filenames
  28.   int n = atoi(argv[1]);
  29.   char * infile = argv[2];
  30.   char * outfile = argv[3];
  31.  
  32.   // open input file
  33.   FILE * inptr = fopen(infile, "r");
  34.   if (inptr == NULL) {
  35.     printf("Could not open %s.\n", infile);
  36.     return 2;
  37.   }
  38.  
  39.   // open output file
  40.   FILE * outptr = fopen(outfile, "w");
  41.   if (outptr == NULL) {
  42.     fclose(inptr);
  43.     fprintf(stderr, "Could not create %s.\n", outfile);
  44.     return 3;
  45.   }
  46.  
  47.   // read infile's BITMAPFILEHEADER
  48.   BITMAPFILEHEADER bf;
  49.   fread( &bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  50.  
  51.   // read infile's BITMAPINFOHEADER
  52.   BITMAPINFOHEADER bi;
  53.   fread( &bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  54.  
  55.   // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  56.   // I added this here because you need to add the sanity check on bf and bi before you actually use them //
  57.   if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  58.     bi.biBitCount != 24 || bi.biCompression != 0) {
  59.     fclose(outptr);
  60.     fclose(inptr);
  61.     fprintf(stderr, "Unsupported file format.\n");
  62.     return 4;
  63.   }
  64.  
  65.   // copy these file headers so they can be written to output file
  66.   BITMAPFILEHEADER bfo = bf;
  67.   BITMAPINFOHEADER bio = bi;
  68.  
  69.   // new height and width
  70.   bio.biHeight = bi.biHeight * n;
  71.   bio.biWidth = bi.biWidth * n;
  72.  
  73.   // determine padding for scanlines
  74.   // old padding
  75.   int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  76.  
  77.   // new padding
  78.   int newpadding = (4 - bio.biWidth * sizeof(RGBTRIPLE) % 4) % 4;
  79.  
  80.   bio.biSizeImage = ((bio.biWidth) * sizeof(RGBTRIPLE) + (newpadding)) * abs(bio.biHeight);
  81.   bfo.bfSize = (bio.biSizeImage) + 54;
  82.  
  83.  
  84.   // write outfile's BITMAPFILEHEADER
  85.   fwrite( &bfo, sizeof(BITMAPFILEHEADER), 1, outptr);
  86.  
  87.   // write outfile's BITMAPINFOHEADER
  88.   fwrite( &bio, sizeof(BITMAPINFOHEADER), 1, outptr);
  89.  
  90.  
  91.   // value of one scanline
  92.   /*scanLine = (RGBTRIPLE*) malloc(sizeof(RGBTRIPLE) * bio.biWidth);*/
  93.  
  94.   // iterate over infile's scanlines
  95.   // for each row
  96.   for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++) {
  97.      
  98.     // for each pixel in row (resize horizontally)
  99.     // iterate over pixels in scanline
  100.     // this loop throw each pixel in one 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.      
  110.      // write to outile n times
  111.       for (int a = 0; a < n; a++) {
  112.         fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  113.       }
  114.      
  115.       //skip original padding
  116.       fseek(inptr, padding, SEEK_SET);
  117.    
  118.       // then add it back as new padding (to demonstrate how)
  119.       for (int k = 0; k < newpadding; k++) {
  120.         fputc(0x00, outptr);
  121.       }
  122.      
  123.  
  124.     }
  125.  
  126.  
  127.   }
  128.  
  129.   // close infile
  130.   fclose(inptr);
  131.  
  132.   // close outfile
  133.   fclose(outptr);
  134.  
  135.   // that's all folks
  136.   return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement