Guest User

Untitled

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