UME14

resize(less)/pset4/cs50

Jan 23rd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. // Copies a BMP file
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #include "bmp.h"
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10. // ensure proper usage
  11. if (argc != 4)
  12. {
  13. fprintf(stderr, "Usage: copy infile outfile\n");
  14. return 1;
  15. }
  16.  
  17. // ensure 'n' is between 0 and 100
  18. int num;
  19. if (atoi(argv[1]) > 0 && atoi(argv[1]) <= 100)
  20. {
  21. num = atoi(argv[1]);
  22. }
  23. else
  24. {
  25. return 1;
  26. }
  27.  
  28. // remember filenames
  29. int n = num;
  30. char *infile = argv[2];
  31. char *outfile = argv[3];
  32.  
  33. // open input file
  34. FILE *inptr = fopen(infile, "r");
  35. if (inptr == NULL)
  36. {
  37. fprintf(stderr, "Could not open %s.\n", infile);
  38. return 2;
  39. }
  40.  
  41. // open output file
  42. FILE *outptr = fopen(outfile, "w");
  43. if (outptr == NULL)
  44. {
  45. fclose(inptr);
  46. fprintf(stderr, "Could not create %s.\n", outfile);
  47. return 3;
  48. }
  49.  
  50. // read infile's BITMAPFILEHEADER
  51. BITMAPFILEHEADER bf;
  52. fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  53. BITMAPFILEHEADER bfNew; //new
  54. bfNew = bf; //now fread's data is workable from bfNew
  55.  
  56. // read infile's BITMAPINFOHEADER
  57. BITMAPINFOHEADER bi;
  58. fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  59. BITMAPINFOHEADER biNew;
  60. biNew = bi; //now fread's data is workable from biNew
  61.  
  62. // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  63. if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  64. bi.biBitCount != 24 || bi.biCompression != 0)
  65. {
  66. fclose(outptr);
  67. fclose(inptr);
  68. fprintf(stderr, "Unsupported file format.\n");
  69. return 4;
  70. }
  71.  
  72.  
  73. // updated height & width
  74. biNew.biWidth = bi.biWidth * n;
  75. biNew.biHeight = bi.biHeight * n;
  76.  
  77. // determine padding for scanlines
  78. int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  79. int paddingNew = (4 - (biNew.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; //new padding
  80.  
  81. // update dimension
  82. biNew.biSizeImage = ((sizeof(RGBTRIPLE) * biNew.biWidth) + paddingNew) * abs(biNew.biHeight);
  83. bfNew.bfSize = biNew.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  84.  
  85. // write outfile's BITMAPFILEHEADER
  86. fwrite(&bfNew, sizeof(BITMAPFILEHEADER), 1, outptr);
  87.  
  88. // write outfile's BITMAPINFOHEADER
  89. fwrite(&biNew, sizeof(BITMAPINFOHEADER), 1, outptr);
  90.  
  91.  
  92. /*
  93. //RE-COPY METHOD
  94. // iterate over each scanline
  95. for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  96. {
  97. //FIRST TIME
  98. // for n-1 times
  99. for (int a = 0; a < n - 1; a++)
  100. {
  101. //read, write, padding, cursor_BACK
  102. //iterate over each pixel
  103. for (int b = 0; b < bi.biWidth; b++)
  104. {
  105. // temporary storage
  106. RGBTRIPLE triple;
  107.  
  108. // read RGB triple from infile
  109. fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  110.  
  111. //// for n-times
  112. // write RGB triple to outfile
  113. for (int c = 0; c < n; c++)
  114. {
  115. fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  116. }
  117. //fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  118.  
  119. // write new padding
  120. for (int k = 0; k < paddingNew; k++)
  121. {
  122. fputc(0x00, outptr);
  123. }
  124.  
  125. // send infile cursor back
  126. fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE)), SEEK_CUR);
  127. }
  128. }
  129.  
  130.  
  131. //LAST TIME
  132. //read, write, padding, padding_SKIP
  133. // iterate over pixels in scanline
  134. for (int j = 0; j < bi.biWidth; j++)
  135. {
  136. // temporary storage
  137. RGBTRIPLE triple;
  138.  
  139. // read RGB triple from infile
  140. fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  141.  
  142. // write RGB trip to outfile
  143. fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  144.  
  145. // write new padding
  146. for (int k = 0; k < paddingNew; k++)
  147. {
  148. fputc(0x00, outptr);
  149. }
  150.  
  151. // skip over padding, if any (infile)
  152. fseek(inptr, padding, SEEK_CUR);
  153. }
  154. }
  155. */
  156.  
  157.  
  158.  
  159. //REWRITE METHOD
  160. // iterate over infile's scanlines
  161. for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  162. {
  163. // temporary storage
  164. RGBTRIPLE pixelArr[abs(biNew.biWidth)]; //RGBTRIPLE pixelArr[abs(biNew.biWidth) * sizeof(RGBTRIPLE)];
  165. // Array index counter
  166. int count = 0;
  167.  
  168. // iterate over pixels in scanline
  169. for (int j = 0; j < bi.biWidth; j++)
  170. {
  171. //make an array, fill up and at the end of this loop send values to upper loop
  172. // temporary storage
  173. RGBTRIPLE triple;
  174. //RGBTRIPLE pixelArr[n];
  175.  
  176. // read RGB triple from infile
  177. fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  178.  
  179. // write pixel in array n times
  180. for (int l = 0; l < n; l++, count++)
  181. {
  182. pixelArr[count] = triple;
  183. }
  184. }
  185.  
  186. // for n times scanline
  187. for (int m = 0; m < n; m++) //for (int m = 0; m < n; m++)
  188. {
  189. // write RGB triple array to outfile
  190. fwrite(&pixelArr, sizeof(biNew.biWidth), 1, outptr);
  191.  
  192. // write new padding
  193. for (int k = 0; k < paddingNew; k++)
  194. {
  195. fputc(0x00, outptr);
  196. }
  197. }
  198.  
  199. // skip over padding, if any
  200. fseek(inptr, padding, SEEK_CUR);
  201.  
  202. // fseek?
  203. }
  204.  
  205.  
  206. // close infile
  207. fclose(inptr);
  208.  
  209. // close outfile
  210. fclose(outptr);
  211.  
  212. // success
  213. return 0;
  214. }
Add Comment
Please, Sign In to add comment