Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include "bmp.h"
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.     // ensure proper usage
  9.     if (argc != 4)
  10.     {
  11.         fprintf(stderr, "Usage: ./copy infile outfile\n");
  12.         return 1;
  13.     }
  14.  
  15.  
  16.     // look for the resize number
  17.         int n = atoi(argv[1]);
  18.         if (n < 1 || n > 100)
  19.         {
  20.             printf("usage: first argument must be between 1 and 100");
  21.             return 2;
  22.         }
  23.    
  24.     // remember filenames
  25.     char *infile = argv[2];
  26.     char *outfile = argv[3];
  27.  
  28.     // open input file
  29.     FILE *inptr = fopen(infile, "r");
  30.     if (inptr == NULL)
  31.     {
  32.         fprintf(stderr, "Could not open %s.\n", infile);
  33.         return 3;
  34.     }
  35.  
  36.     // open output file
  37.     FILE *outptr = fopen(outfile, "w");
  38.     if (outptr == NULL)
  39.     {
  40.         fclose(inptr);
  41.         fprintf(stderr, "Could not create %s.\n", outfile);
  42.         return 4;
  43.     }
  44.  
  45.     // read infile's BITMAPFILEHEADER
  46.     BITMAPFILEHEADER bf, new_bf;
  47.     fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  48.     new_bf = bf;
  49.  
  50.     // read infile's BITMAPINFOHEADER
  51.     BITMAPINFOHEADER bi, new_bi;
  52.     fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  53.     new_bi = bi;
  54.  
  55.     // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  56.     if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  57.         bi.biBitCount != 24 || bi.biCompression != 0)
  58.     {
  59.         fclose(outptr);
  60.         fclose(inptr);
  61.         fprintf(stderr, "Unsupported file format.\n");
  62.         return 4;
  63.     }
  64.    
  65.     // update the new bitmap header for the outfile
  66.     new_bi.biWidth = bi.biWidth * n;
  67.     new_bi.biHeight = bi.biHeight * n;
  68.    
  69.     // determine padding for scanlines needed for SizeImage calculation
  70.     int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  71.     int new_padding = (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  72.    
  73.     // update the image size as well as the file size
  74.     new_bi.biSizeImage = ((sizeof(RGBTRIPLE) * new_bi.biWidth) + new_padding) * abs(new_bi.biHeight);
  75.     new_bf.bfSize = 54 + new_bi.biSizeImage;
  76.    
  77.     // write outfile's BITMAPFILEHEADER
  78.     fwrite(&new_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  79.  
  80.     // write outfile's BITMAPINFOHEADER
  81.     fwrite(&new_bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  82.  
  83.     // iterate over infile's scanlines
  84.     for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  85.     {
  86.         // write to outfile by a factor of n
  87.         for (int j = 0; j < n; j++)
  88.         {
  89.             // iterate over pixels in scanline
  90.             for (int k = 0; k < bi.biWidth; j++)
  91.             {
  92.                 // temporary storage
  93.                 RGBTRIPLE triple;
  94.    
  95.                 // read RGB triple from infile
  96.                 fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  97.    
  98.                 // write RGB triple to outfile, by the factor n
  99.                 for (int m = 0; m < n; m++)
  100.                 {
  101.                     fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  102.                 }
  103.                
  104.             }
  105.            
  106.             // skip over padding, if any
  107.             fseek(inptr, padding, SEEK_CUR);
  108.    
  109.             // then add it back (to demonstrate how)
  110.             for (int l = 0; l < padding; l++)
  111.             {
  112.                 fputc(0x00, outptr);
  113.             }
  114.         }
  115.     }
  116.  
  117.     // close infile
  118.     fclose(inptr);
  119.  
  120.     // close outfile
  121.     fclose(outptr);
  122.  
  123.     // success
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement