bmp1.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include<Windows.h>
  5. #define BITMAPFILEHEADERLENGTH 14 // The bmp FileHeader length is 14
  6. #define BM 19778 // The ASCII code for BM
  7. /* Test the file is bmp file or not */
  8. void bmpFileTest(FILE* fpbmp);
  9. /* To get the OffSet of header to data part */
  10. void bmpHeaderPartLength(FILE* fpbmp);
  11. /* To get the width and height of the bmp file */
  12. void BmpWidthHeight(FILE* fpbmp);
  13. //get r,g,b data
  14. void bmpDataPart(FILE* fpbmp);
  15. // output data to corresponding txt file
  16. void bmpoutput(FILE* fpout);
  17. unsigned int OffSet = 0; // OffSet from Header part to Data Part
  18. long width; // The Width of the Data Part
  19. long height; // The Height of the Data Part
  20. unsigned char r[2000][2000], output_r[2000][2000];
  21. unsigned char g[2000][2000], output_g[2000][2000];
  22. unsigned char b[2000][2000], output_b[2000][2000];
  23. int main(int argc, char* argv[])
  24. {
  25. /* Open bmp file */
  26. unsigned char* fp_temp;
  27. FILE* fpbmp;
  28. FILE* fpout;
  29. fpbmp = fopen("1.bmp", "rb");
  30. if (fpbmp == NULL)
  31. {
  32. printf("Open bmp failed!!!\n");
  33. return 1;
  34. }
  35. fpout = fopen("out.bmp", "wb+");
  36. if (fpout == NULL)
  37. {
  38. printf("Open out.bmp failed!!!\n");
  39. return 1;
  40. }
  41. bmpFileTest(fpbmp); //Test the file is bmp file or not
  42. bmpHeaderPartLength(fpbmp); //Get the length of Header Part
  43. BmpWidthHeight(fpbmp); //Get the width and width of the Data Part
  44. //
  45. fseek(fpbmp, 0L, SEEK_SET);
  46. fseek(fpout, 0L, SEEK_SET);
  47. fp_temp =(unsigned char*) malloc(OffSet);
  48. fread(fp_temp, 1, OffSet, fpbmp);
  49. fwrite(fp_temp, 1, OffSet, fpout);
  50. bmpDataPart(fpbmp); //Reserve the data to file
  51. /*
  52. 如果您想对图片进行处理,请您再这里插入处理函数!!!!!!!!!!!!!!!!!!
  53. */
  54. bmpoutput(fpout);
  55. fclose(fpbmp);
  56. fclose(fpout);
  57. return 0;
  58. }
  59. void bmpoutput(FILE* fpout)
  60. {
  61. int i, j = 0;
  62. int stride;
  63. unsigned char* pixout = NULL;
  64. stride = (24 * width + 31) / 8;
  65. stride = stride / 4 * 4;
  66. pixout =(unsigned char*) malloc(stride);
  67. fseek(fpout, OffSet, SEEK_SET);
  68. for (j = 0; j < height; j++)
  69. {
  70. for (i = 0; i < width; i++)
  71. {
  72. pixout[i * 3 + 2] = output_r[height - 1 - j][i];
  73. pixout[i * 3 + 1] = output_g[height - 1 - j][i];
  74. pixout[i * 3] = output_b[height - 1 - j][i];
  75. }
  76. fwrite(pixout, 1, stride, fpout);
  77. }
  78. }
  79. void bmpDataPart(FILE* fpbmp)
  80. {
  81. int i, j = 0;
  82. int stride;
  83. unsigned char* pix = NULL;
  84. FILE* fpr;
  85. FILE* fpg;
  86. FILE* fpb;
  87. if ((fpr = fopen("bmpr.txt", "w+")) == NULL)
  88. {
  89. printf("Failed to construct file bmpr.txt.!!!");
  90. exit(1);
  91. }
  92. if ((fpg = fopen("bmpg.txt", "w+")) == NULL)
  93. {
  94. printf("Failed to construct file bmpg.txt.!!!");
  95. exit(1);
  96. }
  97. if ((fpb = fopen("bmpb.txt", "w+")) == NULL)
  98. {
  99. printf("Failed to construct file bmpb.txt.!!!");
  100. exit(1);
  101. }
  102. fseek(fpbmp, OffSet, SEEK_SET);
  103. stride = (24 * width + 31) / 8;
  104. stride = stride / 4 * 4;
  105. pix =(unsigned char*) malloc(stride);
  106. for (j = 0; j < height; j++)
  107. {
  108. fread(pix, 1, stride, fpbmp);
  109. for (i = 0; i < width; i++)
  110. {
  111. r[height - 1 - j][i] = pix[i * 3 + 2];
  112. g[height - 1 - j][i] = pix[i * 3 + 1];
  113. b[height - 1 - j][i] = pix[i * 3];
  114. output_r[height - 1 - j][i] = pix[i * 3 + 2];
  115. output_g[height - 1 - j][i] = pix[i * 3 + 1];
  116. output_b[height - 1 - j][i] = pix[i * 3];
  117. }
  118. }
  119. for (i = 0; i < height; i++)
  120. {
  121. for (j = 0; j < width - 1; j++)
  122. {
  123. fprintf(fpb, "%4d", b[i][j]);
  124. fprintf(fpg, "%4d", g[i][j]);
  125. fprintf(fpr, "%4d", r[i][j]);
  126. }
  127. fprintf(fpb, "%4d\n", b[i][j]);
  128. fprintf(fpg, "%4d\n", g[i][j]);
  129. fprintf(fpr, "%4d\n", r[i][j]);
  130. }
  131. fclose(fpr);
  132. fclose(fpg);
  133. fclose(fpb);
  134. }
  135. void bmpFileTest(FILE* fpbmp)
  136. {
  137. unsigned short bfType = 0;
  138. fseek(fpbmp, 0L, SEEK_SET);//seek_set 起始位置
  139. fread(&bfType, sizeof(char), 2, fpbmp);
  140. if (BM != bfType)
  141. {
  142. printf("This file is not bmp file.!!!\n");
  143. exit(1);
  144. }
  145. }
  146. /* To get the OffSet of header to data part */
  147. void bmpHeaderPartLength(FILE* fpbmp)
  148. {
  149. fseek(fpbmp, 10L, SEEK_SET);
  150. fread(&OffSet, sizeof(char), 4, fpbmp);
  151. printf("The Header Part is of length %d.\n", OffSet);
  152. }
  153. /* To get the width and height of the bmp file */
  154. void BmpWidthHeight(FILE* fpbmp)
  155. {
  156. fseek(fpbmp, 18L, SEEK_SET);
  157. fread(&width, sizeof(char), 4, fpbmp);
  158. fseek(fpbmp, 22L, SEEK_SET);
  159. fread(&height, sizeof(char), 4, fpbmp);
  160. printf("The Width of the bmp file is %ld.\n", width);
  161. printf("The Height of the bmp file is %ld.\n", height);
  162. }