Guest User

Untitled

a guest
May 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include "bmp.h"
  5. int main(int argc, char *argv[]) {
  6.  
  7. // обеспечить правильное использование
  8. if (argc != 3) {
  9. printf("Usage: ./copy infile outfilen");
  10. return 1;
  11. }
  12. // запомнить имена файлов
  13. char* infile = argv[1];
  14. char* outfile = argv[2];
  15.  
  16. // открыть входной файл
  17. FILE* inptr = fopen(infile, "r");
  18. if (inptr == NULL) {
  19. printf("Could not open %s.n", infile);
  20. return 2;
  21. }
  22. // открыть выходной файл
  23. FILE* outptr = fopen(outfile, "w");
  24. if (outptr == NULL) {
  25. fclose(inptr);
  26. fprintf(stderr, "Could not create %s.n", outfile);
  27. return 3;
  28. }
  29. // чтение файла infile типа BITMAPFILEHEADER
  30. BITMAPFILEHEADER bf;
  31. fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  32.  
  33. // чтение файла infile типа BITMAPINFOHEADER
  34. BITMAPINFOHEADER bi;
  35. fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  36.  
  37. // убедитесь, что infile (вероятно) 24-разрядный несжатый BMP 4.0
  38. if(bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  39. bi.biBitCount != 24 || bi.biCompression != 0) {
  40. fclose(outptr);
  41. fclose(inptr);
  42. fprintf(stderr, "Unsupported file format.n");
  43. return 4;
  44. }
  45. // запись outfile в BITMAPFILEHEADER
  46. fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  47.  
  48. // запись outfile в BITMAPINFOHEADER
  49. fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  50.  
  51. // определение отступов для сканирования строк
  52. int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  53.  
  54. // перебрать строки в infile
  55. for(int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++) {
  56.  
  57. // итерация по пикселям в scanline
  58. for(int j = 0; j < bi.biWidth; j++) {
  59.  
  60. // временное хранилище
  61. RGBTRIPLE triple;
  62.  
  63. // считать RGB тройной от infile
  64. fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  65.  
  66. // Если RGB полностью красный то заменить на белый
  67. if(triple.rgbtRed == 0xff && triple.rgbtGreen == 0x00 && triple.rgbtBlue == 0x00) {
  68. triple.rgbtBlue = 0xff;
  69. triple.rgbtGreen = 0xff;
  70. triple.rgbtRed = 0xff;
  71. }
  72. // записать RGB тройной для outfile
  73. fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  74. }
  75. // пропустить отступ, если он есть
  76. fseek(inptr, padding, SEEK_CUR);
  77.  
  78. // затем добавьте его обратно (чтобы продемонстрировать, как)
  79. for (int k = 0; k < padding; k++) {
  80. fputc(0x00, outptr);
  81. }
  82. }
  83. // закрыть infile
  84. fclose(inptr);
  85.  
  86. // закрыть outfile
  87. fclose(outptr);
  88.  
  89. return 0;
  90. }
  91.  
  92. #include <stdint.h>
  93.  
  94. /**
  95. * Common Data Types
  96. *
  97. * The data types in this section are essentially aliases for C/C++
  98. * primitive data types.
  99. *
  100. * Adapted from http://msdn.microsoft.com/en-us/library/cc230309.aspx.
  101. * See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h.
  102. */
  103. typedef uint8_t BYTE;
  104. typedef uint32_t DWORD;
  105. typedef int32_t LONG;
  106. typedef uint16_t WORD;
  107.  
  108. /**
  109. * BITMAPFILEHEADER
  110. *
  111. * The BITMAPFILEHEADER structure contains information about the type, size,
  112. * and layout of a file that contains a DIB [device-independent bitmap].
  113. *
  114. * Adapted from http://msdn.microsoft.com/en-us/library/dd183374(VS.85).aspx.
  115. */
  116. typedef struct
  117. {
  118. WORD bfType;
  119. DWORD bfSize;
  120. WORD bfReserved1;
  121. WORD bfReserved2;
  122. DWORD bfOffBits;
  123. } __attribute__((__packed__))
  124. BITMAPFILEHEADER;
  125.  
  126. /**
  127. * BITMAPINFOHEADER
  128. *
  129. * The BITMAPINFOHEADER structure contains information about the
  130. * dimensions and color format of a DIB [device-independent bitmap].
  131. *
  132. * Adapted from http://msdn.microsoft.com/en-us/library/dd183376(VS.85).aspx.
  133. */
  134. typedef struct
  135. {
  136. DWORD biSize;
  137. LONG biWidth;
  138. LONG biHeight;
  139. WORD biPlanes;
  140. WORD biBitCount;
  141. DWORD biCompression;
  142. DWORD biSizeImage;
  143. LONG biXPelsPerMeter;
  144. LONG biYPelsPerMeter;
  145. DWORD biClrUsed;
  146. DWORD biClrImportant;
  147. } __attribute__((__packed__))
  148. BITMAPINFOHEADER;
  149.  
  150. /**
  151. * RGBTRIPLE
  152. *
  153. * This structure describes a color consisting of relative intensities of
  154. * red, green, and blue.
  155. *
  156. * Adapted from http://msdn.microsoft.com/en-us/library/aa922590.aspx.
  157. */
  158. typedef struct
  159. {
  160. BYTE rgbtBlue;
  161. BYTE rgbtGreen;
  162. BYTE rgbtRed;
  163. } __attribute__((__packed__))
  164. RGBTRIPLE;
Add Comment
Please, Sign In to add comment