Advertisement
mroker

Untitled

Jul 21st, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.81 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.    
  90.    
  91.    
  92.    
  93.    
  94.    
  95.    
  96.     //RGBTRIPLE *array_copy = malloc(sizeof(RGBTRIPLE));
  97.    
  98.    
  99.     // iterate over infile's scanlines (***FOR EACH ROW***)
  100.     for(int i = 0, biHeight = abs(biHeightCopy); i < biHeight; i++)
  101.     {
  102.         RGBTRIPLE* array_copy[bi.biWidth];
  103.          int k = 0;
  104.         // iterate over pixels in scanline (***FOR EACH PIXEL IN THE ROW***)
  105.         for (int j = 0; j < biWidthCopy; j++)
  106.         {
  107.            
  108.             // temporary storage
  109.             RGBTRIPLE triple;
  110.            
  111.             printf("Pixel %i\t", j);
  112.            
  113.             fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  114.             printf("cursor is here: \t%li\n",ftell(inptr));
  115.          
  116.            
  117.             int count = 0;
  118.             //add pixels for times n amount - WRITE TO ARRAY N TIMES
  119.             while (count < n)
  120.             {
  121.                
  122.                 array_copy[k] = &triple;
  123.                // printf("Pass: %i [%d.%d.%d]\t",k,triple.rgbtBlue,triple.rgbtGreen,triple.rgbtRed);
  124.                 printf("%i: [%d.%d.%d]\t", k,array_copy[k]->rgbtBlue,array_copy[k]->rgbtGreen,array_copy[k]->rgbtRed);
  125.                 count++;
  126.                 k++;
  127.                
  128.             }
  129.            
  130.             printf("\n");
  131.            
  132.         }
  133.        
  134.         //print out array
  135.         for (int r = 0; r < 6;r++)
  136.         {
  137.             printf("array copy: [%d.%d.%d]\t", array_copy[r]->rgbtBlue,array_copy[r]->rgbtGreen,array_copy[r]->rgbtRed);
  138.             printf("\n");
  139.         }
  140.        
  141.        
  142.        
  143.         //write array copy to outfile twice vertically
  144.         for (int p = 0; p < n; p++)
  145.         {
  146.            
  147.             //write array_copy to outfile
  148.             for (int x = 0; x < bi.biWidth; x++)
  149.             {
  150.                
  151.            
  152.             fwrite(*array_copy, sizeof(RGBTRIPLE), 1, outptr);
  153.             //printf("freadoutput:%p\n", &triple);
  154.            
  155.             printf("final_copy: %d\n", array_copy[p]->rgbtGreen);
  156.            
  157.             }
  158.            
  159.              //add padding to horizontal row
  160.             for (int j = 0; j < outfilepadding; j++)
  161.             {
  162.                 fputc(0x0, outptr);
  163.             }
  164.          
  165.         }
  166.        
  167.        
  168.            
  169.                // skip over padding, if any
  170.                 fseek(inptr, infilepadding, SEEK_CUR);
  171.     }
  172.    
  173.  
  174.    
  175.     // close infile
  176.     fclose(inptr);
  177.  
  178.     // close outfile
  179.     fclose(outptr);
  180.  
  181.     // success
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement