fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define X_SIZE 32
  5. #define Y_SIZE 32
  6. #define X_BLOCK 4
  7. #define Y_BLOCK 4
  8. #define DIM ((Y_SIZE / Y_BLOCK) * (X_SIZE / X_BLOCK))
  9.  
  10. typedef unsigned char u_char;
  11.  
  12. void load_image_data(char* f_name);
  13. void mesh(void);
  14.  
  15. u_char image[Y_SIZE][X_SIZE];
  16.  
  17. int main()
  18. {
  19. char f_name[256];
  20. int i;
  21.  
  22. for (i = 0; i < 10; i++) {
  23. sprintf_s(f_name, _countof(f_name), "std%d.pgm", i);
  24. load_image_data(f_name);
  25. printf("\nstd%d.pgm\n", i);
  26. mesh();
  27. }
  28. return 0;
  29. }
  30.  
  31. void mesh(void)
  32. {
  33. int fv_mesh[DIM] = {0};
  34. int ix, iy;
  35. int k;
  36.  
  37. for (iy = 0; iy < Y_SIZE; iy++) {
  38. for (ix = 0; ix < X_SIZE; ix++) {
  39. k = (iy / Y_BLOCK) * (X_SIZE / X_BLOCK) + (ix / X_BLOCK);
  40. fv_mesh[k] += image[iy][ix];
  41. }
  42. }
  43. for (k = 0; k < DIM; k++) {
  44. fv_mesh[k] /= X_BLOCK * Y_BLOCK;
  45. }
  46.  
  47. for (iy = 0; iy < Y_SIZE / Y_BLOCK; iy++) {
  48. for (ix = 0; ix < X_SIZE / X_BLOCK; ix++) {
  49. k = iy * (X_SIZE / X_BLOCK) + ix;
  50. printf("%02x ", fv_mesh[k]);
  51. }
  52. printf("\n");
  53. }
  54. }
  55.  
  56. void load_image_data(char* f_name)
  57. {
  58. FILE* fp;
  59. char buf[640];
  60. errno_t err;
  61. int x_size, y_size;
  62. int max_gray;
  63. int ix, iy;
  64.  
  65. err = fopen_s(&fp, f_name, "rb");
  66. if (err) {
  67. fprintf(stderr, "fopen_s\n");
  68. return;
  69. }
  70.  
  71. // P5
  72. fgets(buf, sizeof buf, fp);
  73. if (buf[0] != 'P' || buf[1] != '5') {
  74. fprintf(stderr, "not P5\n");
  75. return;
  76. }
  77.  
  78. // x y
  79. x_size = y_size = 0;
  80. while (x_size == 0) {
  81. fgets(buf, sizeof buf, fp);
  82. if (buf[0] == '#') continue;
  83. sscanf_s(buf, "%d%d", &x_size, &y_size);
  84. }
  85.  
  86. // max
  87. max_gray = 0;
  88. while (max_gray == 0) {
  89. fgets(buf, sizeof buf, fp);
  90. if (buf[0] == '#') continue;
  91. sscanf_s(buf, "%d", &max_gray);
  92. }
  93.  
  94. for (iy = 0; iy < y_size; iy++) {
  95. for (ix = 0; ix < x_size; ix++) {
  96. image[iy][ix] = fgetc(fp);
  97. }
  98. }
  99.  
  100. fclose(fp);
  101. }
  102.  
  103. /*
  104. std0.pgm
  105. ff ff d4 92 79 ac fd ff
  106. ff e2 84 fe fd ad b8 ff
  107. ff 7a c5 ff ff f5 4a ff
  108. ff 61 c5 ff ff f5 31 ff
  109. ff 61 c5 ff ff f5 31 ff
  110. ff 65 c5 ff ff f5 35 ff
  111. ff d0 90 ff ff c3 9d ff
  112. ff ff ba 85 b1 a9 fa ff
  113.  
  114. std1.pgm
  115. ff ff f0 8b b0 ff ff ff
  116. ff ff b9 4d a5 ff ff ff
  117. ff ff ff 7d a5 ff ff ff
  118. ff ff ff 7d a5 ff ff ff
  119. ff ff ff 7d a5 ff ff ff
  120. ff ff ff 7d a5 ff ff ff
  121. ff ff ff 7d a5 ff ff ff
  122. ff ff c2 56 72 cf ff ff
  123.  
  124. std2.pgm
  125. ff ff d4 92 79 82 f7 ff
  126. ff e2 b3 fe fd 72 b5 ff
  127. ff d1 2c 9d ff 9a 91 ff
  128. ff fa 98 c1 f8 41 e7 ff
  129. ff ff ff f8 76 b7 ff ff
  130. ff ff fa 9c b7 fe fb ff
  131. ff f8 99 ef ff e9 a4 ff
  132. ff 81 1a 49 49 2c 91 ff
  133.  
  134. std3.pgm
  135. ff fb 82 88 94 b6 fd ff
  136. ff d2 5a fe fd 34 ef ff
  137. ff e1 96 ff fe 54 f4 ff
  138. ff ff d9 97 91 d3 ff ff
  139. ff ff ff ff e9 5d dd ff
  140. ff ac 76 ff ff 9b 91 ff
  141. ff 70 97 ff ff 8d 9d ff
  142. ff de 8f b1 97 77 fa ff
  143.  
  144. std4.pgm
  145. ff ff ff ff dc 88 ff ff
  146. ff ff ff fa 5e 63 ff ff
  147. ff ff fe a8 ac 63 ff ff
  148. ff ff d0 c0 c1 63 ff ff
  149. ff f3 a6 f6 c1 63 ff ff
  150. ff ab 81 a5 7c 40 b0 ff
  151. ff f5 ef ef b4 5d f1 ff
  152. ff ff ff d7 86 45 bb ff
  153.  
  154. std5.pgm
  155. ff d6 47 8d 8d 9a f8 ff
  156. ff d1 79 9d 9d eb ff ff
  157. ff d1 c4 fd fd ff ff ff
  158. ff c5 75 99 98 aa fd ff
  159. ff 87 d3 ff ff 76 b2 ff
  160. ff c1 bf ff ff 9b 91 ff
  161. ff 61 69 ff ff 8d 9d ff
  162. ff be 8f b1 aa 84 fa ff
  163.  
  164. std6.pgm
  165. ff ff f7 78 91 93 f1 ff
  166. ff f6 93 f0 ff 55 94 ff
  167. ff a8 81 ff ff ce e6 ff
  168. ff 61 ba 9e 97 aa fd ff
  169. ff 61 7a f6 ff 8c 9a ff
  170. ff 65 c5 ff ff f5 31 ff
  171. ff d0 be ff ff e9 6e ff
  172. ff ff bb ae b1 82 d8 ff
  173.  
  174. std7.pgm
  175. ff d6 2d 31 31 2e c8 ff
  176. ff 94 c9 f7 f7 91 f8 ff
  177. ff b7 ff ff ce c4 ff ff
  178. ff ff ff fc 79 ea ff ff
  179. ff ff ff e5 56 ff ff ff
  180. ff ff fe 8a a5 ff ff ff
  181. ff ff fd 1e a5 ff ff ff
  182. ff ff fd 1f c0 ff ff ff
  183.  
  184. std8.pgm
  185. ff ff d4 92 91 99 f1 ff
  186. ff e2 72 fe ff 97 94 ff
  187. ff d1 3b f4 ff 9e b3 ff
  188. ff fa 72 22 7d a8 fd ff
  189. ff f3 b2 a3 61 30 dd ff
  190. ff 73 b3 ff ff d3 41 ff
  191. ff 70 b6 ff ff e9 3e ff
  192. ff f1 64 a7 b1 82 d8 ff
  193.  
  194. std9.pgm
  195. ff fb a9 92 91 bb fd ff
  196. ff be 9b fe ff d5 bf ff
  197. ff 61 c5 ff ff f5 4a ff
  198. ff 9e 8c fc ff c4 2b ff
  199. ff ff 94 8e 97 bb 31 ff
  200. ff fd f5 fe ff f1 5c ff
  201. ff d3 1d eb ff 97 c7 ff
  202. ff f1 88 b1 aa 84 fa ff
  203. */
  204.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty