Guest User

Untitled

a guest
Jul 20th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 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. // remember filenames
  18. char *infile = argv[2];
  19. char *outfile = argv[3];
  20.  
  21. // open input file
  22. FILE *inptr = fopen(infile, "r");
  23. if (inptr == NULL)
  24. {
  25. fprintf(stderr, "Could not open %s.\n", infile);
  26. return 2;
  27. }
  28.  
  29. // open output file
  30. FILE *outptr = fopen(outfile, "w");
  31. if (outptr == NULL)
  32. {
  33. fclose(inptr);
  34. fprintf(stderr, "Could not create %s.\n", outfile);
  35. return 3;
  36. }
  37.  
  38. float size = atof(argv[1]);
  39. if (size == 0)
  40. {
  41. fprintf(stderr, "Wrong float number for size\n");
  42. return 1;
  43. }
  44.  
  45. // read infile's BITMAPFILEHEADER
  46. BITMAPFILEHEADER bf;
  47. fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  48.  
  49. // read infile's BITMAPINFOHEADER
  50. BITMAPINFOHEADER bi;
  51. fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  52.  
  53. // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  54. if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  55. bi.biBitCount != 24 || bi.biCompression != 0)
  56. {
  57. fclose(outptr);
  58. fclose(inptr);
  59. fprintf(stderr, "Unsupported file format.\n");
  60. return 4;
  61. }
  62.  
  63. //copying bitmapfileheader & bitmapinfoheader into new variables to change its values
  64. BITMAPFILEHEADER new_bf = bf;
  65. BITMAPINFOHEADER new_bi = bi;
  66. //declarations of padding
  67. float padding, new_padding;
  68.  
  69. //declaration for step that will be used in case of size < 0
  70. float step;
  71.  
  72. //changing value of width & height & size at make image larger case
  73. if (size >= 1)
  74. {
  75. //giving width & height new values according to new size
  76. new_bi.biWidth *= size;
  77. new_bi.biHeight *= size;
  78. // determine padding for scanlines
  79. padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  80. new_padding = (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  81. new_bi.biSizeImage = (sizeof(RGBTRIPLE) * new_bi.biWidth + new_padding) * abs(new_bi.biHeight);
  82. new_bf.bfSize = new_bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  83.  
  84. // write outfile's BITMAPFILEHEADER
  85. fwrite(&new_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  86. // write outfile's BITMAPINFOHEADER
  87. fwrite(&new_bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  88. // iterate over infile's scanlines
  89. for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  90. {
  91. // resize image vertically by repeating ntimes according to the new size
  92. for (int repeat = 0; repeat < size; repeat++)
  93. {
  94. // iterate over pixels in scanline
  95. for (int j = 0; j < bi.biWidth; j++)
  96. {
  97. // temporary storage
  98. RGBTRIPLE triple;
  99.  
  100. // read RGB triple from infile
  101. fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  102.  
  103. //loop through the new width of new image, to write the byte multiple times according to new size
  104. for (int k = 0; k < size; k++)
  105. {
  106. // write RGB triple to outfile
  107. fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  108. }
  109. }
  110.  
  111. // skip over padding, if any
  112. fseek(inptr, padding, SEEK_CUR);
  113.  
  114. // then add it back (to demonstrate how)
  115. for (int k = 0; k < new_padding; k++)
  116. {
  117. fputc(0x00, outptr);
  118. }
  119.  
  120. //getting the cursor back to the start of line, to repeat vertically
  121. if (repeat < size -1)
  122. {
  123. fseek(inptr, -(bi.biWidth * 3 + padding), SEEK_CUR);
  124. }
  125. }
  126. }
  127. }
  128.  
  129. else if (size < 1)
  130. {
  131. //step is float number
  132. step = 1 / size;
  133. //giving width & height new values according to new size
  134. new_bi.biWidth /= step;
  135. new_bi.biHeight /= step;
  136. // determine padding for scanlines
  137. padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  138. new_padding = (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  139. new_bi.biSizeImage = (sizeof(RGBTRIPLE) * new_bi.biWidth + new_padding) * abs(new_bi.biHeight);
  140. new_bf.bfSize = new_bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  141.  
  142. // write outfile's BITMAPFILEHEADER
  143. fwrite(&new_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  144. // write outfile's BITMAPINFOHEADER
  145. fwrite(&new_bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  146. // iterate over infile's scanlines
  147. for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight ; i += step)
  148. {
  149. // iterate over pixels in scanline
  150. for (int j = 0; j < bi.biWidth ; j += step)
  151. {
  152. // temporary storage
  153. RGBTRIPLE triple;
  154.  
  155. // read RGB triple from infile
  156. fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  157.  
  158. // write RGB triple to outfile
  159. fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  160.  
  161. //moving to the next place according to the step size
  162. fseek(inptr, step , SEEK_CUR);
  163. }
  164.  
  165. // skip over padding, if any
  166. fseek(inptr, padding, SEEK_CUR);
  167.  
  168. // then add it back (to demonstrate how)
  169. for (int k = 0; k < new_padding; k++)
  170. {
  171. fputc(0x00, outptr);
  172. }
  173.  
  174. //getting the cursor reading another line without writing, to repeat vertically
  175. for (int repeat = 0; repeat < step - 1; repeat++)
  176. {
  177. fseek(inptr, (bi.biWidth * sizeof(RGBTRIPLE)) + padding, SEEK_CUR);
  178. }
  179.  
  180. }
  181. }
  182.  
  183. // close infile
  184. fclose(inptr);
  185.  
  186. // close outfile
  187. fclose(outptr);
  188.  
  189. // success
  190. return 0;
  191. }
Add Comment
Please, Sign In to add comment