You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

862 lines
24 KiB

  1. /* test shit */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <SDL_image.h>
  8. #define MAXWIDTH 512
  9. #define MAXHEIGHT 512
  10. #define true 1
  11. #define false 0
  12. int WIDTH, HEIGHT;
  13. static inline float get(float *src, int x, int y)
  14. {
  15. return src[y * WIDTH + x];
  16. }
  17. static inline void put(float *src, int x, int y, float p)
  18. {
  19. src[y * WIDTH + x] = p;
  20. }
  21. static float *new(void)
  22. {
  23. return malloc(WIDTH * HEIGHT * sizeof(float));
  24. }
  25. static float *copy(float *src)
  26. {
  27. float *dest = malloc(WIDTH * HEIGHT * sizeof(float));
  28. memcpy(dest, src, WIDTH * HEIGHT * sizeof(float));
  29. return dest;
  30. }
  31. #define N 5
  32. #define NN ((N * 2 + 1))
  33. static void makegauss(float mat[NN][NN], float sigma, float dx, float dy)
  34. {
  35. float t = 0;
  36. int i, j;
  37. sigma = 2. * sigma * sigma;
  38. for(j = 0; j < NN; j++)
  39. for(i = 0; i < NN; i++)
  40. {
  41. float a = (float)(i - N) + dx;
  42. float b = (float)(j - N) + dy;
  43. mat[i][j] = pow(M_E, - (a * a + b * b) / sigma);
  44. t += mat[i][j];
  45. }
  46. for(j = 0; j < NN; j++)
  47. for(i = 0; i < NN; i++)
  48. mat[i][j] /= t;
  49. }
  50. static float *gauss(float *src, float mat[NN][NN])
  51. {
  52. float *dest = new();
  53. int x, y, i, j;
  54. for(y = N; y < HEIGHT - N; y++)
  55. for(x = N; x < WIDTH - N; x++)
  56. {
  57. float p = 0;
  58. for(j = 0; j < NN; j++)
  59. for(i = 0; i < NN; i++)
  60. p += get(src, x + i - N, y + j - N) * mat[i][j];
  61. put(dest, x, y, p);
  62. }
  63. return dest;
  64. }
  65. static float *fullgauss(float *src, float mat[NN][NN])
  66. {
  67. float *dest = new();
  68. int x, y, i, j;
  69. for(y = 0; y < HEIGHT; y++)
  70. for(x = 0; x < WIDTH; x++)
  71. {
  72. float p = 0;
  73. for(j = 0; j < NN; j++)
  74. for(i = 0; i < NN; i++)
  75. if(x + i >= N && x + i < WIDTH + N
  76. && y + j >= N && y + j < HEIGHT + N)
  77. p += get(src, x + i - N, y + j - N) * mat[i][j];
  78. put(dest, x, y, p);
  79. }
  80. return dest;
  81. }
  82. static float fulldist(float *p1, float *p2)
  83. {
  84. float error = 0.;
  85. int x, y;
  86. for(y = 0; y < HEIGHT; y++)
  87. for(x = 0; x < WIDTH; x++)
  88. {
  89. float t = get(p1, x, y) - get(p2, x, y);
  90. error += t * t;
  91. }
  92. return error / (WIDTH * HEIGHT);
  93. }
  94. static float dist(float *p1, float *p2)
  95. {
  96. float error = 0.;
  97. int x, y;
  98. for(y = N; y < HEIGHT - N; y++)
  99. for(x = N; x < WIDTH - N; x++)
  100. {
  101. float t = get(p1, x, y) - get(p2, x, y);
  102. error += t * t;
  103. }
  104. return error / ((WIDTH - N) * (HEIGHT - N));
  105. }
  106. static float *load(char *name)
  107. {
  108. SDL_Surface *tmp, *surface;
  109. uint32_t *pixels;
  110. float *floats;
  111. int x, y;
  112. tmp = IMG_Load(name);
  113. if(!tmp)
  114. return NULL;
  115. WIDTH = tmp->w > MAXWIDTH ? MAXWIDTH : tmp->w;
  116. HEIGHT = tmp->h > MAXHEIGHT ? MAXHEIGHT : tmp->h;
  117. floats = malloc(WIDTH * HEIGHT * sizeof(float));
  118. if(!floats)
  119. return NULL;
  120. surface = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT, 32,
  121. 0xff0000, 0xff00, 0xff, 0x0);
  122. pixels = (uint32_t *)surface->pixels;
  123. SDL_BlitSurface(tmp, NULL, surface, NULL);
  124. SDL_FreeSurface(tmp);
  125. for(y = 0; y < HEIGHT; y++)
  126. for(x = 0; x < WIDTH; x++)
  127. {
  128. int green = (pixels[y * surface->pitch / 4 + x] >> 8) & 0xff;
  129. put(floats, x, y, (float)green / 0xff);
  130. }
  131. return floats;
  132. }
  133. static void save(float *src, char *name)
  134. {
  135. SDL_Surface *surface;
  136. uint32_t *pixels;
  137. int x, y;
  138. surface = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT, 32,
  139. 0xff0000, 0xff00, 0xff, 0x0);
  140. pixels = (uint32_t *)surface->pixels;
  141. for(y = 0; y < HEIGHT; y++)
  142. for(x = 0; x < WIDTH; x++)
  143. {
  144. float p = 255 * get(src, x, y);
  145. uint32_t i = p < 0 ? 0 : p > 255 ? 255 : p;
  146. pixels[surface->pitch / 4 * y + x] = (i << 16) | (i << 8) | i;
  147. }
  148. SDL_SaveBMP(surface, name);
  149. }
  150. static float *ostromoukhov(float *src)
  151. {
  152. static int const table[][3] =
  153. {
  154. {13, 0, 5}, {13, 0, 5}, {21, 0, 10}, {7, 0, 4},
  155. {8, 0, 5}, {47, 3, 28}, {23, 3, 13}, {15, 3, 8},
  156. {22, 6, 11}, {43, 15, 20}, {7, 3, 3}, {501, 224, 211},
  157. {249, 116, 103}, {165, 80, 67}, {123, 62, 49}, {489, 256, 191},
  158. {81, 44, 31}, {483, 272, 181}, {60, 35, 22}, {53, 32, 19},
  159. {237, 148, 83}, {471, 304, 161}, {3, 2, 1}, {481, 314, 185},
  160. {354, 226, 155}, {1389, 866, 685}, {227, 138, 125}, {267, 158, 163},
  161. {327, 188, 220}, {61, 34, 45}, {627, 338, 505}, {1227, 638, 1075},
  162. {20, 10, 19}, {1937, 1000, 1767}, {977, 520, 855}, {657, 360, 551},
  163. {71, 40, 57}, {2005, 1160, 1539}, {337, 200, 247}, {2039, 1240, 1425},
  164. {257, 160, 171}, {691, 440, 437}, {1045, 680, 627}, {301, 200, 171},
  165. {177, 120, 95}, {2141, 1480, 1083}, {1079, 760, 513}, {725, 520, 323},
  166. {137, 100, 57}, {2209, 1640, 855}, {53, 40, 19}, {2243, 1720, 741},
  167. {565, 440, 171}, {759, 600, 209}, {1147, 920, 285}, {2311, 1880, 513},
  168. {97, 80, 19}, {335, 280, 57}, {1181, 1000, 171}, {793, 680, 95},
  169. {599, 520, 57}, {2413, 2120, 171}, {405, 360, 19}, {2447, 2200, 57},
  170. {11, 10, 0}, {158, 151, 3}, {178, 179, 7}, {1030, 1091, 63},
  171. {248, 277, 21}, {318, 375, 35}, {458, 571, 63}, {878, 1159, 147},
  172. {5, 7, 1}, {172, 181, 37}, {97, 76, 22}, {72, 41, 17},
  173. {119, 47, 29}, {4, 1, 1}, {4, 1, 1}, {4, 1, 1},
  174. {4, 1, 1}, {4, 1, 1}, {4, 1, 1}, {4, 1, 1},
  175. {4, 1, 1}, {4, 1, 1}, {65, 18, 17}, {95, 29, 26},
  176. {185, 62, 53}, {30, 11, 9}, {35, 14, 11}, {85, 37, 28},
  177. {55, 26, 19}, {80, 41, 29}, {155, 86, 59}, {5, 3, 2},
  178. {5, 3, 2}, {5, 3, 2}, {5, 3, 2}, {5, 3, 2},
  179. {5, 3, 2}, {5, 3, 2}, {5, 3, 2}, {5, 3, 2},
  180. {5, 3, 2}, {5, 3, 2}, {5, 3, 2}, {5, 3, 2},
  181. {305, 176, 119}, {155, 86, 59}, {105, 56, 39}, {80, 41, 29},
  182. {65, 32, 23}, {55, 26, 19}, {335, 152, 113}, {85, 37, 28},
  183. {115, 48, 37}, {35, 14, 11}, {355, 136, 109}, {30, 11, 9},
  184. {365, 128, 107}, {185, 62, 53}, {25, 8, 7}, {95, 29, 26},
  185. {385, 112, 103}, {65, 18, 17}, {395, 104, 101}, {4, 1, 1}
  186. };
  187. float *dest = new();
  188. float *tmp = copy(src);
  189. int x, y;
  190. for(y = 0; y < HEIGHT; y++)
  191. {
  192. for(x = 0; x < WIDTH; x++)
  193. {
  194. int x1 = (y & 1) ? WIDTH - 1 - x + 1 : x - 1;
  195. int x2 = (y & 1) ? WIDTH - 1 - x : x;
  196. int x3 = (y & 1) ? WIDTH - 1 - x - 1 : x + 1;
  197. float p = get(tmp, x2, y);
  198. float q = p > 0.5 ? 1. : 0.;
  199. float error = (p - q);
  200. int i = p * 255.9999;
  201. put(dest, x2, y, q);
  202. if(i > 127)
  203. i = 255 - i;
  204. if(i < 0)
  205. i = 0;
  206. error /= table[i][0] + table[i][1] + table[i][2];
  207. if(x < WIDTH - 1)
  208. put(tmp, x3, y, get(tmp, x3, y) + error * table[i][0]);
  209. if(y < HEIGHT - 1)
  210. {
  211. if(x > 0)
  212. put(tmp, x1, y + 1,
  213. get(tmp, x1, y + 1) + error * table[i][1]);
  214. put(tmp, x2, y + 1, get(tmp, x2, y + 1) + error * table[i][2]);
  215. }
  216. }
  217. }
  218. free(tmp);
  219. return dest;
  220. }
  221. /* Dither using error diffusion and Floyd-Steinberg-like coefficients:
  222. X a b
  223. c d e f g
  224. h i j k l
  225. */
  226. static float *ed(float *src, int serpentine,
  227. int a, int b, int c, int d, int e, int f,
  228. int g, int h, int i, int j, int k, int l)
  229. {
  230. float *dest = new();
  231. float *tmp = copy(src);
  232. int x, y, n;
  233. n = a + b + c + d + e + f + g + h + i + j + k + l;
  234. for(y = 0; y < HEIGHT; y++)
  235. {
  236. int swap = serpentine && (y & 1);
  237. for(x = 0; x < WIDTH; x++)
  238. {
  239. int x0 = swap ? WIDTH - 1 - x + 2 : x - 2;
  240. int x1 = swap ? WIDTH - 1 - x + 1 : x - 1;
  241. int x2 = swap ? WIDTH - 1 - x : x;
  242. int x3 = swap ? WIDTH - 1 - x - 1 : x + 1;
  243. int x4 = swap ? WIDTH - 1 - x - 2 : x + 2;
  244. float p = get(tmp, x2, y);
  245. float q = p > 0.5 ? 1. : 0.;
  246. float error = (p - q) / n;
  247. put(dest, x2, y, q);
  248. if(x < WIDTH - 1)
  249. put(tmp, x3, y, get(tmp, x3, y) + error * a);
  250. if(x < WIDTH - 2)
  251. put(tmp, x4, y, get(tmp, x4, y) + error * b);
  252. if(y < HEIGHT - 1)
  253. {
  254. if(x > 0)
  255. {
  256. put(tmp, x1, y + 1,
  257. get(tmp, x1, y + 1) + error * d);
  258. if(x > 1)
  259. put(tmp, x0, y + 1,
  260. get(tmp, x0, y + 1) + error * c);
  261. }
  262. put(tmp, x2, y + 1, get(tmp, x2, y + 1) + error * e);
  263. if(x < WIDTH - 1)
  264. {
  265. put(tmp, x3, y + 1,
  266. get(tmp, x3, y + 1) + error * f);
  267. if(x < WIDTH - 2)
  268. put(tmp, x4, y + 1,
  269. get(tmp, x4, y + 1) + error * g);
  270. }
  271. }
  272. if(y < HEIGHT - 2)
  273. {
  274. if(x > 0)
  275. {
  276. put(tmp, x1, y + 2,
  277. get(tmp, x1, y + 2) + error * h);
  278. if(x > 1)
  279. put(tmp, x0, y + 2,
  280. get(tmp, x0, y + 2) + error * i);
  281. }
  282. put(tmp, x2, y + 2, get(tmp, x2, y + 2) + error * j);
  283. if(x < WIDTH - 1)
  284. {
  285. put(tmp, x3, y + 2,
  286. get(tmp, x3, y + 2) + error * k);
  287. if(x < WIDTH - 2)
  288. put(tmp, x4, y + 2,
  289. get(tmp, x4, y + 2) + error * l);
  290. }
  291. }
  292. }
  293. }
  294. free(tmp);
  295. return dest;
  296. }
  297. /* XXX */
  298. static float *dbs(float *src, float *orig, float sigma, float dx, float dy)
  299. {
  300. float mat[NN][NN];
  301. float *dest, *tmp, *tmp2;
  302. float error;
  303. makegauss(mat, sigma, 0., 0.);
  304. tmp = fullgauss(src, mat);
  305. makegauss(mat, sigma, dx, dy);
  306. dest = copy(orig);
  307. tmp2 = fullgauss(dest, mat);
  308. error = dist(tmp, tmp2);
  309. for(;;)
  310. {
  311. int changes = 0;
  312. int x, y, i, j, n;
  313. for(y = 0; y < HEIGHT; y++)
  314. for(x = 0; x < WIDTH; x++)
  315. {
  316. float d, d2, e, best = 0.;
  317. int opx = -1, opy = -1;
  318. d = get(dest, x, y);
  319. /* Compute the effect of a toggle */
  320. e = 0.;
  321. for(j = -N; j < N + 1; j++)
  322. {
  323. if(y + j < 0 || y + j >= HEIGHT)
  324. continue;
  325. for(i = -N; i < N + 1; i++)
  326. {
  327. float m, p, q1, q2;
  328. if(x + i < 0 || x + i >= WIDTH)
  329. continue;
  330. m = mat[i + N][j + N];
  331. p = get(tmp, x + i, y + j);
  332. q1 = get(tmp2, x + i, y + j);
  333. q2 = q1 - m * d + m * (1. - d);
  334. e += (q1 - p) * (q1 - p) - (q2 - p) * (q2 - p);
  335. }
  336. }
  337. if(e > best)
  338. {
  339. best = e;
  340. opx = opy = 0;
  341. }
  342. /* Compute the effect of swaps */
  343. for(n = 0; n < 8; n++)
  344. {
  345. static int const step[] =
  346. { 0, 1, 0, -1, -1, 0, 1, 0, -1, -1, -1, 1, 1, -1, 1, 1 };
  347. int idx = step[n * 2], idy = step[n * 2 + 1];
  348. if(y + idy < 0 || y + idy >= HEIGHT
  349. || x + idx < 0 || x + idx >= WIDTH)
  350. continue;
  351. d2 = get(dest, x + idx, y + idy);
  352. if(d2 == d)
  353. continue;
  354. e = 0.;
  355. for(j = -N; j < N + 1; j++)
  356. {
  357. if(y + j < 0 || y + j >= HEIGHT)
  358. continue;
  359. if(j - idy + N < 0 || j - idy + N >= NN)
  360. continue;
  361. for(i = -N; i < N + 1; i++)
  362. {
  363. float ma, mb, p, q1, q2;
  364. if(x + i < 0 || x + i >= WIDTH)
  365. continue;
  366. if(i - idx + N < 0 || i - idx + N >= NN)
  367. continue;
  368. ma = mat[i + N][j + N];
  369. mb = mat[i - idx + N][j - idy + N];
  370. p = get(tmp, x + i, y + j);
  371. q1 = get(tmp2, x + i, y + j);
  372. q2 = q1 - ma * d + ma * d2 - mb * d2 + mb * d;
  373. e += (q1 - p) * (q1 - p) - (q2 - p) * (q2 - p);
  374. }
  375. }
  376. if(e > best)
  377. {
  378. best = e;
  379. opx = idx;
  380. opy = idy;
  381. }
  382. }
  383. /* Apply the change if interesting */
  384. if(best <= 0.)
  385. continue;
  386. if(opx || opy)
  387. {
  388. d2 = get(dest, x + opx, y + opy);
  389. put(dest, x + opx, y + opy, d);
  390. }
  391. else
  392. d2 = 1. - d;
  393. put(dest, x, y, d2);
  394. for(j = -N; j < N + 1; j++)
  395. for(i = -N; i < N + 1; i++)
  396. {
  397. float m = mat[i + N][j + N];
  398. if(y + j >= 0 && y + j < HEIGHT
  399. && x + i >= 0 && x + i < WIDTH)
  400. {
  401. float t = get(tmp2, x + i, y + j);
  402. put(tmp2, x + i, y + j, t + m * (d2 - d));
  403. }
  404. if((opx || opy) && y + opy + j >= 0 && y + opy + j < HEIGHT
  405. && x + opx + i >= 0 && x + opx + i < WIDTH)
  406. {
  407. float t = get(tmp2, x + opx + i, y + opy + j);
  408. put(tmp2, x + opx + i, y + opy + j, t + m * (d - d2));
  409. }
  410. }
  411. changes++;
  412. }
  413. fprintf(stderr, "did %i changes\n", changes);
  414. if(changes == 0)
  415. break;
  416. }
  417. free(tmp);
  418. free(tmp2);
  419. return dest;
  420. }
  421. static void study(float *src, float *dest, float sigma, float precision)
  422. {
  423. # define Z 3
  424. float mat[NN][NN];
  425. float *tmp, *tmp2;
  426. float e0, best = 0., fx = -1., fy = -1., step = 2.;
  427. int dx, dy;
  428. makegauss(mat, sigma, 0., 0.);
  429. tmp = gauss(src, mat);
  430. tmp2 = gauss(dest, mat);
  431. e0 = dist(tmp, tmp2);
  432. free(tmp2);
  433. while(step > precision)
  434. {
  435. float bfx = 0., bfy = 0., e;
  436. best = 9999.;
  437. for(dy = 0; dy <= Z; dy++)
  438. for(dx = 0; dx <= Z; dx++)
  439. {
  440. makegauss(mat, sigma, fx + step * dx / Z, fy + step * dy / Z);
  441. tmp2 = gauss(dest, mat);
  442. e = dist(tmp, tmp2);
  443. free(tmp2);
  444. if(e < best)
  445. {
  446. best = e;
  447. bfx = fx + step * dx / Z;
  448. bfy = fy + step * dy / Z;
  449. }
  450. }
  451. fx = bfx - step / Z;
  452. fy = bfy - step / Z;
  453. step = step * 2 / Z;
  454. }
  455. free(tmp);
  456. printf("E = %g E_min = %g dx = %g dy = %g\n",
  457. 1000 * e0, 1000 * best, fx, fy);
  458. fflush(stdout);
  459. }
  460. static float *merge(float *im1, float *im2, float t)
  461. {
  462. float *dest = new();
  463. int x, y;
  464. for(y = 0; y < HEIGHT; y++)
  465. for(x = 0; x < WIDTH; x++)
  466. put(dest, x, y, t * get(im1, x, y) + (1. - t) * get(im2, x, y));
  467. return dest;
  468. }
  469. static void usage(char *argv[])
  470. {
  471. fprintf(stderr, "Usage: %s <mode> [ARGS...]\n", argv[0]);
  472. fprintf(stderr, "Allowed modes:\n");
  473. fprintf(stderr, " -1 <src> raster FS displacement study on src\n");
  474. fprintf(stderr, " -2 <src1> <src2> raster FS displacement study on blends of src1 and src2\n");
  475. fprintf(stderr, " -3 <src> quick (a,b,c,d) ED kernel analysis on src\n");
  476. fprintf(stderr, " -4 <src> exhaustive (a,b,c,d) ED kernel analysis on src\n");
  477. }
  478. int main(int argc, char *argv[])
  479. {
  480. float mat0[NN][NN];
  481. float mat[NN][NN];
  482. float *src;
  483. float sigma;
  484. int mode, dx, dy, i;
  485. if(argc < 2)
  486. {
  487. fprintf(stderr, "%s: too few arguments\n", argv[0]);
  488. usage(argv);
  489. return EXIT_FAILURE;
  490. }
  491. if(argv[1][0] != '-' || !(mode = atoi(argv[1] + 1)))
  492. {
  493. fprintf(stderr, "%s: invalid mode `%s'\n", argv[0], argv[1]);
  494. usage(argv);
  495. return EXIT_FAILURE;
  496. }
  497. src = load(argv[2]);
  498. if(!src)
  499. return 2;
  500. switch(mode)
  501. {
  502. case 1:
  503. {
  504. float *dest = ed(src, false, 7, 0, 1, 3, 5, 0, 0, 0, 0, 0, 0, 0);
  505. study(src, dest, 1.2, 0.001);
  506. free(dest);
  507. free(src);
  508. }
  509. break;
  510. case 2:
  511. {
  512. float *src2, *dest, *tmp;
  513. if(argc < 3)
  514. return 3;
  515. src2 = load(argv[2]);
  516. if(!src2)
  517. return 4;
  518. for(i = 0; i <= 100; i++)
  519. {
  520. tmp = merge(src, src2, (float)i / 100.);
  521. dest = ed(tmp, false, 7, 0, 1, 3, 5, 0, 0, 0, 0, 0, 0, 0);
  522. study(tmp, dest, 1.2, 0.001);
  523. free(dest);
  524. free(tmp);
  525. }
  526. free(src2);
  527. free(src);
  528. }
  529. break;
  530. case 3:
  531. case 4:
  532. {
  533. float *dest, *tmp, *tmp2;
  534. int a, b, c, d, e;
  535. makegauss(mat0, 1.2, 0, 0);
  536. for(a = 0; a <= 16; a++)
  537. for(b = 0; b <= 16; b++)
  538. for(c = 0; c <= 16; c++)
  539. for(d = 0; d <= 16; d++)
  540. for(e = 0; e <= 16; e++)
  541. {
  542. if(a + b + c + d + e != 16)
  543. continue;
  544. /* Slightly shuffle our coefficients to avoid waiting until
  545. * 75% progress before having an idea of what's going on. */
  546. int a2 = a, b2 = b, c2 = c, d2 = d, e2 = e;
  547. #define SHUFFLE(p,q,n) \
  548. if(p+q) { int tmp = p+q; p = (p+n) % (tmp+1); q = tmp-p; }
  549. SHUFFLE(c2, d2, 7); SHUFFLE(b2, d2, 5);
  550. SHUFFLE(a2, d2, 3); SHUFFLE(b2, c2, 2);
  551. SHUFFLE(a2, e2, 3); SHUFFLE(b2, e2, 2);
  552. SHUFFLE(a2, c2, 4); SHUFFLE(a2, b2, 6);
  553. SHUFFLE(c2, e2, 9); SHUFFLE(d2, e2, 7);
  554. SHUFFLE(a2, d2, 9); SHUFFLE(a2, b2, 7);
  555. #if 0
  556. if(b2 > c2) continue;
  557. if(d2 > c2) continue;
  558. if(d2 > a2) continue;
  559. #endif
  560. /* We only want 4-cell kernels for now */
  561. if(b2) continue;
  562. //printf("K = %d,%d,%d,%d,%d ", a2, b2, c2, d2, e2);
  563. printf("K = %d,%d,%d,%d ", a2, c2, d2, e2);
  564. dest = ed(src, false, a2, 0, b2, c2, d2, e2, 0, 0, 0, 0, 0, 0);
  565. if(mode == 3)
  566. {
  567. tmp = gauss(src, mat0);
  568. tmp2 = gauss(dest, mat0);
  569. printf("E = %.5g\n", 1000. * dist(tmp, tmp2));
  570. free(tmp);
  571. free(tmp2);
  572. }
  573. else
  574. study(src, dest, 1.2, 0.01);
  575. fflush(stdout);
  576. free(dest);
  577. }
  578. free(src);
  579. }
  580. break;
  581. #if 0
  582. tmp = ed(src, 7, 0, 0, 3, 5, 1, 0, 0, 0, 0, 0, 0);
  583. //dest = dbs(src, tmp, 0., 0.);
  584. dest = dbs(src, tmp, 0.20, 0.30);
  585. //dest = dbs(src, tmp, 0.158718, 0.283089);
  586. //dest = copy(tmp);
  587. free(tmp);
  588. study(src, dest, 1.2, 0.00001);
  589. save(dest, "output.bmp");
  590. free(dest);
  591. #endif
  592. #if 0
  593. //dest = ed(src, 5, 0, 0, 3, 5, 3, 0, 0, 0, 0, 0, 0);
  594. //dest = ed(src, false, 7, 0, 0, 3, 5, 1, 0, 0, 0, 0, 0, 0);
  595. //dest = ed(src, true, 7, 0, 0, 3, 5, 1, 0, 0, 0, 0, 0, 0);
  596. //dest = ed(src, true, 7, 0, 0, 4, 5, 0, 0, 0, 0, 0, 0, 0);
  597. //dest = ed(src, 7, 5, 3, 5, 7, 5, 3, 1, 3, 5, 3, 1);
  598. //dest = ed(src, 7, 0, 1, 3, 5, 0, 0, 0, 0, 0, 0, 0);
  599. //dest = ed(src, 7, 0, 1, 4, 4, 0, 0, 0, 0, 0, 0, 0);
  600. //dest = ed(src, 2, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0);
  601. //dest = ed(src, 2911, 0, 1373, 3457, 2258, 0, 0, 0, 0, 0, 0, 0);
  602. dest = ostromoukhov(src);
  603. //dest = ed(src, 7, 0, 0, 4, 5, 0, 0, 0, 0, 0, 0, 0);
  604. //printf("%s: ", argv[1]);
  605. //study(src, dest, 1.2, 0.001);
  606. save(dest, "output.bmp");
  607. free(dest);
  608. #endif
  609. #if 0
  610. # define STEP 32
  611. dest = ed(src, 7, 0, 0, 3, 5, 1, 0, 0, 0, 0, 0, 0);
  612. makegauss(mat, 1.2, 0., 0.);
  613. tmp = gauss(src, mat);
  614. for(dy = 0; dy < STEP; dy++)
  615. {
  616. for(dx = 0; dx < STEP; dx++)
  617. {
  618. float fy = 2. / STEP * (dy - STEP / 2.);
  619. float fx = 2. / STEP * (dx - STEP / 2.);
  620. makegauss(mat, 1.2, fx, fy);
  621. tmp2 = gauss(dest, mat);
  622. printf("%g %g %g\n", fy, fx, 1000. * dist(tmp, tmp2));
  623. fflush(stdout);
  624. free(tmp2);
  625. }
  626. printf("\n");
  627. }
  628. save(dest, "output.bmp");
  629. #endif
  630. #if 0
  631. tmp = gauss(src, 0., 0.);
  632. for(a = 0; a < 16; a++)
  633. for(b = 0; b < 16; b++)
  634. for(c = 0; c < 16; c++)
  635. for(d = 0; d < 16; d++)
  636. {
  637. if(a + b + c + d != 16)
  638. continue;
  639. dest = ed(src, a, 0, 0, b, c, d, 0, 0, 0, 0, 0, 0);
  640. tmp2 = gauss(dest, 0., 0.);
  641. printf("%.5g: (%02d %02d %02d %02d)\n",
  642. 1000. * dist(tmp, tmp2), a, b, c, d);
  643. free(dest);
  644. free(tmp2);
  645. }
  646. save(dest, "output.bmp");
  647. #endif
  648. #if 0
  649. printf("%s: ", argv[1]);
  650. makegauss(mat0, 1.2, 0, 0);
  651. tmp = gauss(src, mat0);
  652. dest = ed(src, false, 7, 0, 0, 3, 5, 1, 0, 0, 0, 0, 0, 0);
  653. tmp2 = gauss(dest, mat0);
  654. printf("%.5g ", 1000. * dist(tmp, tmp2));
  655. free(tmp2);
  656. makegauss(mat, 1.2, 0.16, 0.28);
  657. tmp2 = gauss(dest, mat);
  658. printf("%.5g ", 1000. * dist(tmp, tmp2));
  659. free(tmp2);
  660. free(dest);
  661. dest = ed(src, false, 7, 5, 3, 5, 7, 5, 3, 1, 3, 5, 3, 1);
  662. tmp2 = gauss(dest, mat0);
  663. printf("%.5g ", 1000. * dist(tmp, tmp2));
  664. free(tmp2);
  665. makegauss(mat, 1.2, 0.26, 0.76);
  666. tmp2 = gauss(dest, mat);
  667. printf("%.5g ", 1000. * dist(tmp, tmp2));
  668. free(tmp2);
  669. free(dest);
  670. dest = ostromoukhov(src);
  671. tmp2 = gauss(dest, mat0);
  672. printf("%.5g ", 1000. * dist(tmp, tmp2));
  673. free(tmp2);
  674. makegauss(mat, 1.2, 0.0, 0.19);
  675. tmp2 = gauss(dest, mat);
  676. printf("%.5g ", 1000. * dist(tmp, tmp2));
  677. free(tmp2);
  678. free(dest);
  679. dest = ed(src, true, 2911, 0, 0, 1373, 3457, 2258, 0, 0, 0, 0, 0, 0);
  680. tmp2 = gauss(dest, mat0);
  681. printf("%.5g ", 1000. * dist(tmp, tmp2));
  682. free(tmp2);
  683. makegauss(mat, 1.2, 0.0, 0.34);
  684. tmp2 = gauss(dest, mat);
  685. printf("%.5g ", 1000. * dist(tmp, tmp2));
  686. free(tmp2);
  687. free(dest);
  688. printf("\n");
  689. #endif
  690. #if 0
  691. printf("%s\n", argv[1]);
  692. dest = ed(src, false, 7, 0, 0, 3, 5, 1, 0, 0, 0, 0, 0, 0);
  693. study(src, dest, 1.2, 0.01);
  694. free(dest);
  695. dest = ed(src, false, 7, 5, 3, 5, 7, 5, 3, 1, 3, 5, 3, 1);
  696. study(src, dest, 1.2, 0.01);
  697. free(dest);
  698. dest = ostromoukhov(src);
  699. study(src, dest, 1.2, 0.01);
  700. free(dest);
  701. dest = ed(src, true, 2911, 0, 0, 1373, 3457, 2258, 0, 0, 0, 0, 0, 0);
  702. study(src, dest, 1.2, 0.01);
  703. free(dest);
  704. printf("\n");
  705. #endif
  706. #if 0
  707. //dest = ostromoukhov(src);
  708. //dest = ed(src, true, 7, 0, 0, 3, 5, 1, 0, 0, 0, 0, 0, 0);
  709. tmp = new();//ed(src, 7, 0, 0, 3, 5, 1, 0, 0, 0, 0, 0, 0);
  710. dest = dbs(src, tmp, 0., 0.);
  711. for(sigma = 0.8; sigma < 2; sigma *= 1.03)
  712. //for(sigma = 0.8; sigma < 2.; sigma *= 1.01)
  713. {
  714. printf("%g ", sigma);
  715. study(src, dest, sigma, 0.01);
  716. }
  717. #endif
  718. }
  719. #if 0
  720. tmp = new();
  721. a = 0;
  722. for(sigma = 0.8; sigma < 2; sigma *= 1.03)
  723. {
  724. char buf[1024];
  725. printf("%i: %g\n", a, sigma);
  726. dest = dbs(src, tmp, sigma, 0., 0.);
  727. sprintf(buf, "output-dbs-%i.bmp", a++);
  728. save(dest, buf);
  729. }
  730. #endif
  731. return 0;
  732. }