Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

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