fork download
  1. // mesh_3a.c
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #define X_SIZE 32
  7. #define Y_SIZE 32
  8. #define HIGH 255
  9.  
  10. typedef unsigned char u_char;
  11.  
  12. void save_image_data(char* f_name);
  13. void normalize(void);
  14. void disp_img(u_char img[][X_SIZE]);
  15. void cut_img(int n);
  16. void load_image_data(char* f_name);
  17.  
  18. u_char image[Y_SIZE][X_SIZE * 10];
  19. u_char img_a[Y_SIZE][X_SIZE];
  20. u_char img_b[Y_SIZE][X_SIZE];
  21.  
  22. int main()
  23. {
  24. char f_name[256];
  25. int i;
  26.  
  27. load_image_data("number2.pgm");
  28. for (i = 0; i < 10; i++) {
  29. cut_img(i);
  30. printf("\nimg_a:%d\n", i);
  31. disp_img(img_a);
  32.  
  33. normalize();
  34. printf("\nimg_b:%d\n", i);
  35. disp_img(img_b);
  36.  
  37. sprintf_s(f_name, _countof(f_name), "num%d.pgm", i);
  38. save_image_data(f_name);
  39. }
  40.  
  41. return 0;
  42. }
  43.  
  44. void save_image_data(char* f_name)
  45. {
  46. FILE* fp;
  47. errno_t err;
  48. int ix, iy;
  49.  
  50. err = fopen_s(&fp, f_name, "wb");
  51. if (err) {
  52. fprintf(stderr, "fopen_s\n");
  53. return;
  54. }
  55.  
  56. fputs("P5\n", fp);
  57. fputs("# Created by Image Processing\n", fp);
  58. fprintf(fp, "%d %d\n", X_SIZE, Y_SIZE);
  59. fprintf(fp, "%d\n", HIGH);
  60.  
  61. for (iy = 0; iy < Y_SIZE; iy++) {
  62. for (ix = 0; ix < X_SIZE; ix++) {
  63. fputc(img_b[iy][ix], fp);
  64. }
  65. }
  66.  
  67. fclose(fp);
  68. }
  69.  
  70. void normalize(void)
  71. {
  72. int xs = X_SIZE;
  73. int xe = -1;
  74. int ys = Y_SIZE;
  75. int ye = -1;
  76. int ix, iy;
  77. int iw, ih;
  78. int ax, ay;
  79. int max;
  80.  
  81. // Y走査
  82. for (iy = 0; iy < Y_SIZE; iy++) {
  83. for (ix = 0; ix < X_SIZE; ix++) {
  84. if (img_a[iy][ix]) continue;
  85. if (iy < ys) ys = iy;
  86. if (ye < iy) ye = iy;
  87. }
  88. }
  89. // X走査
  90. for (ix = 0; ix < X_SIZE; ix++) {
  91. for (iy = 0; iy < Y_SIZE; iy++) {
  92. if (img_a[iy][ix]) continue;
  93. if (ix < xs) xs = ix;
  94. if (xe < ix) xe = ix;
  95. }
  96. }
  97. iw = xe - xs + 1;
  98. ih = ye - ys + 1;
  99. printf("xs=%d xe=%d ys=%d ye=%d, w=%d h=%d\n", xs, xe, ys, ye, iw, ih);
  100.  
  101. if (iw < ih) {
  102. max = ih;
  103. xs += (iw - max) / 2;
  104. } else {
  105. max = iw;
  106. ys += (ih - max) / 2;
  107. }
  108. for (iy = 0; iy < Y_SIZE; iy++) {
  109. for (ix = 0; ix < X_SIZE; ix++) {
  110. ax = xs + ix * max / X_SIZE;
  111. ay = ys + iy * max / Y_SIZE;
  112. if (ax < 0 || X_SIZE <= ax || ay < 0 || Y_SIZE <= ay) {
  113. img_b[iy][ix] = 0xff;
  114. } else {
  115. img_b[iy][ix] = img_a[ay][ax];
  116. }
  117. }
  118. }
  119. }
  120.  
  121. void disp_img(u_char img[][X_SIZE])
  122. {
  123. int ix, iy;
  124.  
  125. for (iy = 0; iy < Y_SIZE; iy++) {
  126. for (ix = 0; ix < X_SIZE; ix++) {
  127. printf("%02x", img[iy][ix]);
  128. }
  129. printf("\n");
  130. }
  131. }
  132.  
  133. void cut_img(int n)
  134. {
  135. int ix, iy;
  136.  
  137. for (iy = 0; iy < Y_SIZE; iy++) {
  138. for (ix = 0; ix < X_SIZE; ix++) {
  139. img_a[iy][ix] = image[iy][X_SIZE * n + ix];
  140. }
  141. }
  142. }
  143.  
  144. void load_image_data(char* f_name)
  145. {
  146. FILE* fp;
  147. char buf[640];
  148. errno_t err;
  149. int x_size, y_size;
  150. int max_gray;
  151. int ix, iy;
  152.  
  153. err = fopen_s(&fp, f_name, "rb");
  154. if (err) {
  155. fprintf(stderr, "fopen_s\n");
  156. return;
  157. }
  158.  
  159. // P5
  160. fgets(buf, sizeof buf, fp);
  161. if (buf[0] != 'P' || buf[1] != '5') {
  162. fprintf(stderr, "not P5\n");
  163. return;
  164. }
  165.  
  166. // x y
  167. x_size = y_size = 0;
  168. while (x_size == 0) {
  169. fgets(buf, sizeof buf, fp);
  170. if (buf[0] == '#') continue;
  171. sscanf_s(buf, "%d%d", &x_size, &y_size);
  172. }
  173.  
  174. // max
  175. max_gray = 0;
  176. while (max_gray == 0) {
  177. fgets(buf, sizeof buf, fp);
  178. if (buf[0] == '#') continue;
  179. sscanf_s(buf, "%d", &max_gray);
  180. }
  181.  
  182. for (iy = 0; iy < y_size; iy++) {
  183. for (ix = 0; ix < x_size; ix++) {
  184. image[iy][ix] = fgetc(fp);
  185. }
  186. }
  187.  
  188. fclose(fp);
  189. }
  190.  
  191. /*
  192. img_a:9
  193. ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  194. ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  195. ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  196. ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  197. ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  198. ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  199. ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  200. ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  201. ffffffffff0000000000ffffffffffffffffffffffffffffffffffffffffffff
  202. ffffff0000ffffffffff00ffffffffffffffffffffffffffffffffffffffffff
  203. ffff0000ffffffffffffff00ffffffffffffffffffffffffffffffffffffffff
  204. ffff00ffffffffffffffffff00ffffffffffffffffffffffffffffffffffffff
  205. ff0000ffffffffffffffffff00ffffffffffffffffffffffffffffffffffffff
  206. ff0000ffffffffffffffffff0000ffffffffffffffffffffffffffffffffffff
  207. ff0000ffffffffffffffffff0000ffffffffffffffffffffffffffffffffffff
  208. ff0000ffffffffffffffffff0000ffffffffffffffffffffffffffffffffffff
  209. ffff0000ffffffffffffff000000ffffffffffffffffffffffffffffffffffff
  210. ffffff0000ffffffffff00ff0000ffffffffffffffffffffffffffffffffffff
  211. ffffffff000000000000ffff0000ffffffffffffffffffffffffffffffffffff
  212. ffffffffffffffffffffffff0000ffffffffffffffffffffffffffffffffffff
  213. ffffffffffffffffffffffff0000ffffffffffffffffffffffffffffffffffff
  214. ffffffffffffffffffffffff00ffffffffffffffffffffffffffffffffffffff
  215. ffff000000ffffffffffff0000ffffffffffffffffffffffffffffffffffffff
  216. ffff000000ffffffffffff00ffffffffffffffffffffffffffffffffffffffff
  217. ffff0000ffffffffffff0000ffffffffffffffffffffffffffffffffffffffff
  218. ffffff00ffffffffff0000ffffffffffffffffffffffffffffffffffffffffff
  219. ffffffff0000000000ffffffffffffffffffffffffffffffffffffffffffffff
  220. ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  221. ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  222. ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  223. ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  224. ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  225. xs=1 xe=13 ys=8 ye=26, w=13 h=19
  226.  
  227. img_b:9
  228. ffffffffffffffffffffffff000000000000000000ffffffffffffffffffffff
  229. ffffffffffffffffffffffff000000000000000000ffffffffffffffffffffff
  230. ffffffffffffffffff000000ffffffffffffffffff00ffffffffffffffffffff
  231. ffffffffffffffffff000000ffffffffffffffffff00ffffffffffffffffffff
  232. ffffffffffffff00000000ffffffffffffffffffffff0000ffffffffffffffff
  233. ffffffffffffff00000000ffffffffffffffffffffff0000ffffffffffffffff
  234. ffffffffffffff0000ffffffffffffffffffffffffffffff0000ffffffffffff
  235. ffffffffffff000000ffffffffffffffffffffffffffffff0000ffffffffffff
  236. ffffffffffff000000ffffffffffffffffffffffffffffff0000ffffffffffff
  237. ffffffffffff000000ffffffffffffffffffffffffffffff000000ffffffffff
  238. ffffffffffff000000ffffffffffffffffffffffffffffff000000ffffffffff
  239. ffffffffffff000000ffffffffffffffffffffffffffffff000000ffffffffff
  240. ffffffffffff000000ffffffffffffffffffffffffffffff000000ffffffffff
  241. ffffffffffff000000ffffffffffffffffffffffffffffff000000ffffffffff
  242. ffffffffffffff00000000ffffffffffffffffffffff0000000000ffffffffff
  243. ffffffffffffff00000000ffffffffffffffffffffff0000000000ffffffffff
  244. ffffffffffffffffff000000ffffffffffffffffff00ffff000000ffffffffff
  245. ffffffffffffffffffffff00000000000000000000ffffff000000ffffffffff
  246. ffffffffffffffffffffff00000000000000000000ffffff000000ffffffffff
  247. ffffffffffffffffffffffffffffffffffffffffffffffff000000ffffffffff
  248. ffffffffffffffffffffffffffffffffffffffffffffffff000000ffffffffff
  249. ffffffffffffffffffffffffffffffffffffffffffffffff000000ffffffffff
  250. ffffffffffffffffffffffffffffffffffffffffffffffff0000ffffffffffff
  251. ffffffffffffffffffffffffffffffffffffffffffffffff0000ffffffffffff
  252. ffffffffffffff0000000000ffffffffffffffffffff00000000ffffffffffff
  253. ffffffffffffff0000000000ffffffffffffffffffff00000000ffffffffffff
  254. ffffffffffffff0000000000ffffffffffffffffffff0000ffffffffffffffff
  255. ffffffffffffff00000000ffffffffffffffffffff000000ffffffffffffffff
  256. ffffffffffffff00000000ffffffffffffffffffff000000ffffffffffffffff
  257. ffffffffffffffffff0000ffffffffffffffff000000ffffffffffffffffffff
  258. ffffffffffffffffff0000ffffffffffffffff000000ffffffffffffffffffff
  259. ffffffffffffffffffffff0000000000000000ffffffffffffffffffffffffff
  260. */
  261.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty