Guest User

Untitled

a guest
Feb 9th, 2017
99
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Copies a BMP piece by piece, just because.
  3.  */
  4.        
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #include "bmp.h"
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     // ensure proper usage
  13.     if (argc != 4)
  14.     {
  15.         fprintf(stderr, "Usage: ./copy infile outfile\n");
  16.         return 1;
  17.     }
  18.  
  19.     // remember filenames
  20.     int n = atoi(argv[1]);
  21.     char *infile = argv[2];
  22.     char *outfile = argv[3];
  23.    
  24.     if(n < 1 || n > 100)
  25.     {
  26.         fprintf(stderr, "Scale must be a value of 1 through 100\n");
  27.     }
  28.    
  29.    
  30.  
  31.     // open input file
  32.     FILE *inptr = fopen(infile, "r");
  33.     if (inptr == NULL)
  34.     {
  35.         fprintf(stderr, "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.     {
  43.         fclose(inptr);
  44.         fprintf(stderr, "Could not create %s.\n", outfile);
  45.         return 3;
  46.     }
  47.  
  48.     // read infile's BITMAPFILEHEADER
  49.     BITMAPFILEHEADER bf;
  50.     fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  51.  
  52.     // read infile's BITMAPINFOHEADER
  53.     BITMAPINFOHEADER bi;
  54.     fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  55.    
  56.     bi.biWidth = bi.biWidth * n;
  57.     bi.biHeight = bi.biHeight * n;
  58.     int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  59.     bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);
  60.     bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  61.  
  62.     // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  63.     if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  64.         bi.biBitCount != 24 || bi.biCompression != 0)
  65.     {
  66.         fclose(outptr);
  67.         fclose(inptr);
  68.         fprintf(stderr, "Unsupported file format.\n");
  69.         return 4;
  70.     }
  71.  
  72.     // write outfile's BITMAPFILEHEADER
  73.     fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  74.  
  75.     // write outfile's BITMAPINFOHEADER
  76.     fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  77.  
  78.     // determine padding for scanlines
  79.  
  80.     // iterate over infile's scanlines
  81.     for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  82.     {
  83.         for (int g = 0; g < n; g++)
  84.         {
  85.             // iterate over pixels in scanline
  86.             for (int j = 0; j < bi.biWidth; j++)
  87.             {
  88.                 // temporary storage
  89.                 RGBTRIPLE triple;
  90.  
  91.                 // read RGB triple from infile
  92.                 fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  93.        
  94.                 // write RGB triple to outfile
  95.                 for(int k = 0; k < n; k++ ){
  96.                     fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  97.                 }
  98.             }
  99.         }
  100.  
  101.         // skip over padding, if any
  102.         fseek(inptr, padding, SEEK_CUR);
  103.  
  104.         // then add it back (to demonstrate how)
  105.         for (int k = 0; k < padding; k++)
  106.         {
  107.             fputc(0x00, outptr);
  108.         }
  109.     }
  110.  
  111.     // close infile
  112.     fclose(inptr);
  113.  
  114.     // close outfile
  115.     fclose(outptr);
  116.  
  117.     // success
  118.     return 0;
  119. }
RAW Paste Data