Advertisement
mroker

Untitled

Jul 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.74 KB | None | 0 0
  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.  
  25.     // open input file
  26.     FILE *inptr = fopen(infile, "r");
  27.     if (inptr == NULL)
  28.     {
  29.         fprintf(stderr, "Could not open %s.\n", infile);
  30.         return 2;
  31.     }
  32.  
  33.     // open output file
  34.     FILE *outptr = fopen(outfile, "w");
  35.     if (outptr == NULL)
  36.     {
  37.         fclose(inptr);
  38.         fprintf(stderr, "Could not create %s.\n", outfile);
  39.         return 3;
  40.     }
  41.  
  42.     // read infile's BITMAPFILEHEADER
  43.     BITMAPFILEHEADER bf;
  44.     fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  45.  
  46.     // read infile's BITMAPINFOHEADER
  47.     BITMAPINFOHEADER bi;
  48.     fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  49.  
  50.     // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  51.     if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  52.         bi.biBitCount != 24 || bi.biCompression != 0)
  53.     {
  54.         fclose(outptr);
  55.         fclose(inptr);
  56.         fprintf(stderr, "Unsupported file format.\n");
  57.         return 4;
  58.     }
  59.     //CHANGES TO SCALE UP
  60.     int infilepadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  61.     //calculated previous padding is for infile. multiply this by whatever you choose is outfile.
  62.     //padding *= n;
  63.     //printf("filepadding: %i\n", infilepadding);
  64.  
  65.     int biWidthCopy = bi.biWidth;
  66.     bi.biWidth *= n;
  67.     //printf("biwidth: %i", biWidthCopy);
  68.    
  69.     int outfilepadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  70.    
  71.     //printf("outfilepadding: %i\n", outfilepadding);
  72.  
  73.    
  74.     int biHeightCopy = bi.biHeight;
  75.     bi.biHeight *= n;
  76.    
  77.     bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + outfilepadding) * abs(bi.biHeight);
  78.     bf.bfSize = bi.biSizeImage + sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
  79.    
  80.  
  81.     // write outfile's BITMAPFILEHEADER
  82.     fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  83.  
  84.     // write outfile's BITMAPINFOHEADER
  85.     fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  86.  
  87.    
  88.    
  89.     RGBTRIPLE *array_copy = malloc(sizeof(RGBTRIPLE));
  90.     // iterate over infile's scanlines (***FOR EACH ROW***)
  91.     for(int i = 0, biHeight = abs(biHeightCopy); i < biHeight; i++)
  92.     {
  93.         // iterate over pixels in scanline (***FOR EACH PIXEL IN THE ROW***)
  94.         for (int j = 0; j < biWidthCopy; j++)
  95.         {
  96.            
  97.             // temporary storage
  98.             RGBTRIPLE triple;
  99.            
  100.            
  101.             fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  102.          
  103.             //add pixels for times n amount - WRITE TO ARRAY N TIMES
  104.             for (int k = 0; k < n; k++)
  105.             {
  106.                 array_copy[k] = triple;
  107.             }
  108.            
  109.         }
  110.        
  111.        
  112.        
  113.         //write array to outfile, write outfile padding
  114.         for (int p = 0; p < n; p++)
  115.         {
  116.             fwrite(&array_copy, sizeof(RGBTRIPLE), 1, outptr);
  117.             //printf("freadoutput:%p\n", &triple);
  118.            
  119.            
  120.           //add padding to horizontal row
  121.             for (int j = 0; j < outfilepadding; j++)
  122.             {
  123.                 fputc(0x00, outptr);
  124.             }
  125.         }
  126.            
  127.                // skip over padding, if any
  128.                 fseek(inptr, infilepadding, SEEK_CUR);
  129.     }
  130.    
  131.     //free(&array_copy);
  132.    
  133.    
  134.     // close infile
  135.     fclose(inptr);
  136.  
  137.     // close outfile
  138.     fclose(outptr);
  139.  
  140.     // success
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement