Guest User

Untitled

a guest
Oct 27th, 2016
202
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * copy.c
  3. *
  4. * Computer Science 50
  5. * Problem Set 4
  6. *
  7. * Copies a BMP piece by piece, just because.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. #include "bmp.h"
  14.  
  15. int main(int argc, char* argv[])
  16. {
  17. // ensure proper usage
  18. if (argc != 4)
  19. {
  20. printf("Usage: ./copy infile outfile resize by\n");
  21. return 1;
  22. }
  23.  
  24. int resizeby = atoi(argv[1]);
  25.  
  26. if(resizeby > 100 || resizeby < 0)
  27. {
  28. printf("Usage: resize number must be between 1 and 100 \n");
  29. return 1;
  30. }
  31.  
  32. // remember filenames
  33. char* infile = argv[2];
  34. char* outfile = argv[3];
  35.  
  36. // open input file
  37. FILE* inptr = fopen(infile, "r");
  38. if (inptr == NULL)
  39. {
  40. printf("Could not open %s.\n", infile);
  41. return 2;
  42. }
  43.  
  44. // open output file
  45. FILE* outptr = fopen(outfile, "w");
  46. if (outptr == NULL)
  47. {
  48. fclose(inptr);
  49. fprintf(stderr, "Could not create %s.\n", outfile);
  50. return 3;
  51. }
  52.  
  53. // read infile's BITMAPFILEHEADER
  54. BITMAPFILEHEADER bf;
  55. fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  56.  
  57. // read infile's BITMAPINFOHEADER
  58. BITMAPINFOHEADER bi;
  59. fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  60.  
  61. // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  62. if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  63. bi.biBitCount != 24 || bi.biCompression != 0)
  64. {
  65. fclose(outptr);
  66. fclose(inptr);
  67. fprintf(stderr, "Unsupported file format.\n");
  68. return 4;
  69. }
  70.  
  71.  
  72. int originalsize;
  73. int originalwidth;
  74. int originalheight;
  75. int originalred;
  76. int originalgreen;
  77. int originalblue;
  78.  
  79. int verticalred[bi.biWidth * resizeby * 2];
  80. int verticalgreen[bi.biWidth * resizeby * 2];
  81. int verticalblue[bi.biWidth * resizeby * 2];
  82.  
  83. // determine padding for scanlines
  84. int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  85.  
  86.  
  87. // write outfile's BITMAPFILEHEADER
  88. fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  89.  
  90.  
  91.  
  92. originalsize = bi.biSizeImage;
  93. originalwidth = bi.biWidth;
  94. originalheight = bi.biHeight;
  95.  
  96. bi.biWidth = bi.biWidth * resizeby;
  97. bi.biHeight = bi.biHeight * resizeby;
  98.  
  99. //outfile scanlines
  100. int paddingb = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  101.  
  102.  
  103. bi.biSizeImage = ((bi.biWidth) * abs(bi.biHeight) * sizeof(RGBTRIPLE)) + paddingb;
  104. bf.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) + bi.biSizeImage;
  105.  
  106.  
  107.  
  108.  
  109. // write outfile's BITMAPINFOHEADER
  110. fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  111.  
  112.  
  113.  
  114.  
  115. // iterate over infile's scanlines
  116. for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  117. {
  118.  
  119. // iterate over pixels in scanline
  120. for (int j = 0; j < originalwidth; j++)
  121. {
  122.  
  123.  
  124. // temporary storage
  125. RGBTRIPLE triple;
  126.  
  127.  
  128. // read RGB triple from infile
  129. fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  130.  
  131. originalred = triple.rgbtRed;
  132. originalgreen = triple.rgbtGreen;
  133. originalblue = triple.rgbtBlue;
  134.  
  135. verticalred[j] = triple.rgbtRed;
  136. verticalgreen[j] = triple.rgbtGreen;
  137. verticalblue[j] = triple.rgbtBlue;
  138.  
  139.  
  140.  
  141. // write RGB triple to outfile
  142. for (int aa = 0; aa < resizeby; aa++)
  143. {
  144. fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  145. }
  146.  
  147. // skip over padding, if any
  148. fseek(inptr, padding, SEEK_CUR);
  149.  
  150. // then add it back (to demonstrate how)
  151. for (int k = 0; k < paddingb; k++)
  152. {
  153. fputc(0x00, outptr);
  154. }
  155. }
  156.  
  157.  
  158.  
  159. //resize vertical
  160.  
  161. //vertical storage
  162. RGBTRIPLE vertical;
  163.  
  164. for (int dd = 0; dd < resizeby - 1; dd++)
  165. {
  166. for (int bb = 0; bb < originalwidth; bb++)
  167. {
  168. for (int cc = 0; cc < resizeby; cc++)
  169. {
  170. vertical.rgbtRed = verticalred[bb];
  171. vertical.rgbtGreen = verticalgreen[bb];
  172. vertical.rgbtBlue = verticalblue[bb];
  173.  
  174. fwrite(&vertical, sizeof(RGBTRIPLE), 1, outptr);
  175. }
  176.  
  177. }
  178. }
  179.  
  180.  
  181. }
  182.  
  183. // close infile
  184. fclose(inptr);
  185.  
  186. // close outfile
  187. fclose(outptr);
  188.  
  189. // that's all folks
  190. return 0;
  191. }
RAW Paste Data