Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.27 KB | None | 0 0
  1.  /*
  2.  * Computer Science 50
  3. * Problem Set 4
  4.  *
  5. * Copies a BMP piece by piece, just because.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. #include "bmp.h"
  12.  
  13. int main(int argc, char* argv[])
  14. {
  15. // ensure proper usage
  16. if (argc != 4)
  17. {
  18.     printf("Usage: ./resize n infile outfile\n");
  19.     return 1;
  20. }
  21.  
  22. // remember filenames
  23. char* infile = argv[2];
  24. char* outfile = argv[3];
  25. int n = atoi(argv[1]);
  26. // tests n
  27. if (n < 1 || n > 100)
  28.     {
  29.  
  30.         printf("n must between 1 and 100\n");
  31.         return 1;
  32.     }
  33.  
  34. // open input file
  35. FILE* inptr = fopen(infile, "r");
  36. if (inptr == NULL)
  37. {
  38.     printf("Could not open %s.\n", infile);
  39.     return 2;
  40. }
  41.  
  42. // open output file
  43. FILE* outptr = fopen(outfile, "w");
  44. if (outptr == NULL)
  45. {
  46.     fclose(inptr);
  47.     fprintf(stderr, "Could not create %s.\n", outfile);
  48.     return 3;
  49. }
  50.  
  51. // read infile's BITMAPFILEHEADER
  52. BITMAPFILEHEADER bf;
  53. fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  54.  
  55. // read infile's BITMAPINFOHEADER
  56. BITMAPINFOHEADER bi;
  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. // update header
  70. BITMAPFILEHEADER new_bf = bf;  
  71. BITMAPINFOHEADER new_bi = bi;    
  72.  
  73. new_bi.biHeight  = bi.biHeight * n;
  74. new_bi.biWidth = bi.biWidth * n;
  75.  
  76. // determine padding for scanlines
  77. int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  78. int new_padding = (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  79.  
  80. new_bi.biSizeImage = (new_bi.biWidth * sizeof(RGBTRIPLE) + new_padding) * abs(new_bi.biHeight);
  81. new_bf.bfSize = 54 + new_bi.biSizeImage;
  82.  
  83.  
  84.  
  85. // write outfile's BITMAPFILEHEADER
  86. fwrite(&new_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  87.  
  88. // write outfile's BITMAPINFOHEADER
  89. fwrite(&new_bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  90.  
  91.  
  92. // iterate over infile's scanlines
  93. for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  94. {
  95.    // Perform the horizontal resize many times to get a vertical one.
  96.     for (int h = 0; h < n; h++)
  97.     {
  98.  
  99.         // iterate over pixels in scanline
  100.         for (int j = 0; j < bi.biWidth; j++)
  101.         {
  102.             // temporary storage
  103.             RGBTRIPLE triple;
  104.  
  105.             // read RGB triple from infile
  106.             fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  107.  
  108.        
  109.             // write RGB triple to outfile
  110.             for (int t = 0; t < n; t++)
  111.             {
  112.             fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  113.             }
  114.        
  115.         }  
  116.        
  117.            
  118.     // skip over padding, if any
  119.     fseek(inptr, padding, SEEK_CUR);
  120.    
  121.     // then add it back (to demonstrate how)
  122.             for (int k = 0; k < new_padding; k++)
  123.             {
  124.                fputc(0x00, outptr);
  125.             }
  126.     //go back to beginning of scanline
  127.     if(h < n-1)
  128.         {
  129.         fseek(inptr, -1 * ((3 * bi.biWidth) + (padding)), SEEK_CUR);
  130.         }
  131.     else
  132.         {
  133.         fseek(inptr, 0 , SEEK_CUR);    
  134.         }
  135.     }
  136.    
  137.  
  138. }
  139.  
  140. // close infile
  141. fclose(inptr);
  142.  
  143. // close outfile
  144. fclose(outptr);
  145.  
  146. // that's all folks
  147. return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement