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.
 
 
 
 
 
 

1370 lines
40 KiB

  1. /*
  2. * img2twit Image to short text message encoder/decoder
  3. * Copyright (c) 2009 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * This program is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What The Fuck You Want
  9. * To Public License, Version 2, as published by Sam Hocevar. See
  10. * http://sam.zoy.org/wtfpl/COPYING for more details.
  11. */
  12. /* TODO:
  13. * - remove the complicated stuff from get_point/set_point, it's only
  14. * the final packing that really matters.
  15. */
  16. #include "config.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <math.h>
  21. #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
  22. #include <CGAL/Delaunay_triangulation_2.h>
  23. #include <CGAL/natural_neighbor_coordinates_2.h>
  24. #include <pipi.h>
  25. #include "../genethumb/mygetopt.h"
  26. /*
  27. * Format-dependent settings. Change this and you risk making all other
  28. * generated strings unusable.
  29. */
  30. /* Printable ASCII (except space) */
  31. #define RANGE_ASCII 0x0021, 0x007f
  32. /* CJK Unified Ideographs */
  33. #define RANGE_CJK 0x4e00, 0x9fa6
  34. //0x2e80, 0x2e9a, 0x2e9b, 0x2ef4, /* CJK Radicals Supplement */
  35. //0x2f00, 0x2fd6, /* Kangxi Radicals */
  36. //0x3400, 0x4db6, /* CJK Unified Ideographs Extension A */
  37. //0xac00, 0xd7a4, /* Hangul Syllables -- Korean, not Chinese */
  38. //0xf900, 0xfa2e, 0xfa30, 0xfa6b, 0xfa70, 0xfada, /* CJK Compat. Idgphs. */
  39. /* TODO: there's also the U+20000 and U+2f800 planes, but they're
  40. * not supported by the Twitter Javascript filter (yet?). */
  41. /* Stupid symbols and Dingbats shit */
  42. #define RANGE_SYMBOLS 0x25a0, 0x2600, /* Geometric Shapes */ \
  43. 0x2600, 0x269e, 0x26a0, 0x26bd, 0x26c0, 0x26c4, /* Misc. Symbols */ \
  44. 0x2701, 0x2705, 0x2706, 0x270a, 0x270c, 0x2728, 0x2729, 0x274c, \
  45. 0x274d, 0x274e, 0x274f, 0x2753, 0x2756, 0x2757, 0x2758, 0x275f, \
  46. 0x2761, 0x2795, 0x2798, 0x27b0, 0x27b1, 0x27bf /* Dingbats */
  47. /* End of list marker */
  48. #define RANGE_END 0x0, 0x0
  49. /* Pre-defined character ranges XXX: must be _ordered_ */
  50. static const uint32_t unichars_ascii[] = { RANGE_ASCII, RANGE_END };
  51. static const uint32_t unichars_cjk[] = { RANGE_CJK, RANGE_END };
  52. static const uint32_t unichars_symbols[] = { RANGE_SYMBOLS, RANGE_END };
  53. /* The Unicode characters at disposal */
  54. static const uint32_t *unichars;
  55. /* The maximum image size we want to support */
  56. #define RANGE_W 4000
  57. #define RANGE_H 4000
  58. /* How does the algorithm work: one point per cell, or two? XXX: changing
  59. * this value breaks compatibility with other images. */
  60. #define POINTS_PER_CELL 2
  61. /* Start with a random image (1), or with a good estimate (0)? */
  62. #define RANDOM_START 0
  63. /*
  64. * These values can be overwritten at runtime
  65. */
  66. /* Debug mode */
  67. static bool DEBUG_MODE = false;
  68. /* The maximum message length */
  69. static int MAX_MSG_LEN = 140;
  70. /* Iterations per point -- larger means slower but nicer */
  71. static int ITERATIONS_PER_POINT = 50;
  72. /* The range value for point parameters: X Y, red/green/blue, "strength"
  73. * Tested values (on Mona Lisa) are:
  74. * 16 16 5 5 5 2 -> 0.06511725914
  75. * 16 16 6 7 6 1 -> 0.05731491348 *
  76. * 16 16 7 6 6 1 -> 0.06450513783
  77. * 14 14 7 7 6 1 -> 0.0637207893
  78. * 19 19 6 6 5 1 -> 0.06801999094 */
  79. static unsigned int RANGE_X = 16;
  80. static unsigned int RANGE_Y = 16;
  81. static unsigned int RANGE_R = 6;
  82. static unsigned int RANGE_G = 6;
  83. static unsigned int RANGE_B = 6;
  84. static unsigned int RANGE_S = 1;
  85. /*
  86. * These values are computed at runtime
  87. */
  88. static float TOTAL_BITS;
  89. static float HEADER_BITS;
  90. static float DATA_BITS;
  91. static float CELL_BITS;
  92. static int NUM_CHARACTERS;
  93. static int MAX_ITERATIONS;
  94. static unsigned int TOTAL_CELLS;
  95. #define RANGE_XY (RANGE_Y*RANGE_X)
  96. #define RANGE_XY2 (RANGE_Y*RANGE_X*(RANGE_Y*RANGE_X+1)/2)
  97. #define RANGE_RXY (RANGE_Y*RANGE_X*RANGE_R)
  98. #define RANGE_GRXY (RANGE_Y*RANGE_X*RANGE_R*RANGE_G)
  99. #define RANGE_BGRXY (RANGE_Y*RANGE_X*RANGE_R*RANGE_G*RANGE_B)
  100. #define RANGE_SBGRXY (RANGE_Y*RANGE_X*RANGE_R*RANGE_G*RANGE_B*RANGE_S)
  101. #define RANGE_SBGR (RANGE_R*RANGE_G*RANGE_B*RANGE_S)
  102. struct K : CGAL::Exact_predicates_inexact_constructions_kernel {};
  103. typedef CGAL::Delaunay_triangulation_2<K> Delaunay_triangulation;
  104. typedef std::vector<std::pair<K::Point_2, K::FT> > Point_coordinate_vector;
  105. /* Global aspect ratio */
  106. static unsigned int dw, dh;
  107. /* Global point encoding */
  108. static uint32_t points[4096]; /* FIXME: allocate this dynamically */
  109. static int npoints = 0;
  110. /* Global triangulation */
  111. static Delaunay_triangulation dt;
  112. /*
  113. * Bit allocation handling
  114. */
  115. void compute_ranges(int width, int height)
  116. {
  117. TOTAL_BITS = MAX_MSG_LEN * logf(NUM_CHARACTERS) / logf(2);
  118. HEADER_BITS = logf(RANGE_W * RANGE_H) / logf(2);
  119. DATA_BITS = TOTAL_BITS - HEADER_BITS;
  120. #if POINTS_PER_CELL == 2
  121. CELL_BITS = (2 * logf(RANGE_SBGR) + logf(RANGE_XY2)) / logf(2);
  122. //CELL_BITS = 2 * logf(RANGE_SBGRXY) / logf(2);
  123. #else
  124. CELL_BITS = logf(RANGE_SBGRXY) / logf(2);
  125. #endif
  126. TOTAL_CELLS = (int)(DATA_BITS / CELL_BITS);
  127. MAX_ITERATIONS = ITERATIONS_PER_POINT * POINTS_PER_CELL * TOTAL_CELLS;
  128. /* Compute "best" w/h ratio */
  129. dw = 1; dh = TOTAL_CELLS;
  130. for(unsigned int i = 1; i <= TOTAL_CELLS; i++)
  131. {
  132. int j = TOTAL_CELLS / i;
  133. float r = (float)width / (float)height;
  134. float ir = (float)i / (float)j;
  135. float dwr = (float)dw / (float)dh;
  136. if(fabs(logf(r / ir)) < fabs(logf(r / dwr)))
  137. {
  138. dw = i;
  139. dh = TOTAL_CELLS / dw;
  140. }
  141. }
  142. while((dh + 1) * dw <= TOTAL_CELLS) dh++;
  143. while(dh * (dw + 1) <= TOTAL_CELLS) dw++;
  144. }
  145. /*
  146. * Unicode stuff handling
  147. */
  148. /* Return the number of chars in the unichars table */
  149. static int count_unichars(void)
  150. {
  151. int ret = 0;
  152. for(int u = 0; unichars[u] != unichars[u + 1]; u += 2)
  153. ret += unichars[u + 1] - unichars[u];
  154. return ret;
  155. }
  156. /* Get the ith Unicode character in our list */
  157. static uint32_t index2uni(uint32_t i)
  158. {
  159. for(int u = 0; unichars[u] != unichars[u + 1]; u += 2)
  160. if(i < unichars[u + 1] - unichars[u])
  161. return unichars[u] + i;
  162. else
  163. i -= unichars[u + 1] - unichars[u];
  164. return 0; /* Should not happen! */
  165. }
  166. /* Convert a Unicode character to its position in the compact list */
  167. static uint32_t uni2index(uint32_t x)
  168. {
  169. uint32_t ret = 0;
  170. for(int u = 0; unichars[u] != unichars[u + 1]; u += 2)
  171. if(x < unichars[u + 1])
  172. return ret + x - unichars[u];
  173. else
  174. ret += unichars[u + 1] - unichars[u];
  175. return ret; /* Should not happen! */
  176. }
  177. static uint8_t const utf8_trailing[256] =
  178. {
  179. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  180. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  181. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  182. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  183. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  184. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  185. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  186. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
  187. };
  188. static uint32_t const utf8_offsets[6] =
  189. {
  190. 0x00000000UL, 0x00003080UL, 0x000E2080UL,
  191. 0x03C82080UL, 0xFA082080UL, 0x82082080UL
  192. };
  193. static uint32_t fread_utf8(FILE *f)
  194. {
  195. int ch, i = 0, todo = -1;
  196. uint32_t ret = 0;
  197. for(;;)
  198. {
  199. ch = fgetc(f);
  200. if(!ch)
  201. return 0;
  202. if(todo == -1)
  203. todo = utf8_trailing[ch];
  204. ret += ((uint32_t)ch) << (6 * (todo - i));
  205. if(todo == i++)
  206. return ret - utf8_offsets[todo];
  207. }
  208. }
  209. static void fwrite_utf8(FILE *f, uint32_t x)
  210. {
  211. static const uint8_t mark[7] =
  212. {
  213. 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
  214. };
  215. char buf[8];
  216. char *parser = buf;
  217. size_t bytes;
  218. if(x < 0x80)
  219. {
  220. fprintf(f, "%c", x);
  221. return;
  222. }
  223. bytes = (x < 0x800) ? 2 : (x < 0x10000) ? 3 : 4;
  224. parser += bytes;
  225. *parser = '\0';
  226. switch(bytes)
  227. {
  228. case 4: *--parser = (x | 0x80) & 0xbf; x >>= 6;
  229. case 3: *--parser = (x | 0x80) & 0xbf; x >>= 6;
  230. case 2: *--parser = (x | 0x80) & 0xbf; x >>= 6;
  231. }
  232. *--parser = x | mark[bytes];
  233. fprintf(f, "%s", buf);
  234. }
  235. /*
  236. * Our nifty non-power-of-two bitstack handling
  237. */
  238. class bitstack
  239. {
  240. public:
  241. bitstack(int max) { alloc(max); init(0); }
  242. ~bitstack() { delete[] digits; delete[] str; }
  243. char const *tostring()
  244. {
  245. int pos = sprintf(str, "0x%x", digits[msb]);
  246. for(int i = msb - 1; i >= 0; i--)
  247. pos += sprintf(str + pos, "%08x", digits[i]);
  248. return str;
  249. }
  250. void push(uint32_t val, uint32_t range)
  251. {
  252. if(!range)
  253. return;
  254. mul(range);
  255. add(val % range);
  256. }
  257. uint32_t pop(uint32_t range)
  258. {
  259. if(!range)
  260. return 0;
  261. return div(range);
  262. }
  263. bool isempty()
  264. {
  265. for(int i = msb; i >= 0; i--)
  266. if(digits[i])
  267. return false;
  268. return true;
  269. }
  270. private:
  271. bitstack(int max, uint32_t x) { alloc(max); init(x); }
  272. bitstack(bitstack &b)
  273. {
  274. alloc(b.max_size);
  275. msb = b.msb;
  276. memcpy(digits, b.digits, (max_size + 1) * sizeof(uint32_t));
  277. }
  278. bitstack(bitstack const &b)
  279. {
  280. alloc(b.max_size);
  281. msb = b.msb;
  282. memcpy(digits, b.digits, (max_size + 1) * sizeof(uint32_t));
  283. }
  284. void alloc(int max)
  285. {
  286. max_size = max;
  287. digits = new uint32_t[max_size + 1];
  288. str = new char[(max_size + 1) * 8 + 1];
  289. }
  290. void init(uint32_t i)
  291. {
  292. msb = 0;
  293. memset(digits, 0, (max_size + 1) * sizeof(uint32_t));
  294. digits[0] = i;
  295. }
  296. /* Could be done much faster, but we don't care! */
  297. void add(uint32_t x) { add(bitstack(max_size, x)); }
  298. void sub(uint32_t x) { sub(bitstack(max_size, x)); }
  299. void add(bitstack const &_b)
  300. {
  301. /* Copy the operand in case we get added to ourselves */
  302. bitstack b(_b);
  303. uint64_t x = 0;
  304. if(msb < b.msb)
  305. msb = b.msb;
  306. for(int i = 0; i <= msb; i++)
  307. {
  308. uint64_t tmp = (uint64_t)digits[i] + (uint64_t)b.digits[i] + x;
  309. digits[i] = tmp;
  310. if((uint64_t)digits[i] == tmp)
  311. x = 0;
  312. else
  313. {
  314. x = 1;
  315. if(i == msb)
  316. msb++;
  317. }
  318. }
  319. }
  320. void sub(bitstack const &_b)
  321. {
  322. /* Copy the operand in case we get substracted from ourselves */
  323. bitstack b(_b);
  324. uint64_t x = 0;
  325. /* We cannot substract a larger number! */
  326. if(msb < b.msb)
  327. {
  328. init(0);
  329. return;
  330. }
  331. for(int i = 0; i <= msb; i++)
  332. {
  333. uint64_t tmp = (uint64_t)digits[i] - (uint64_t)b.digits[i] - x;
  334. digits[i] = tmp;
  335. if((uint64_t)digits[i] == tmp)
  336. x = 0;
  337. else
  338. {
  339. x = 1;
  340. if(i == msb)
  341. {
  342. /* Error: carry into MSB! */
  343. init(0);
  344. return;
  345. }
  346. }
  347. }
  348. while(msb > 0 && digits[msb] == 0) msb--;
  349. }
  350. void mul(uint32_t x)
  351. {
  352. bitstack b(*this);
  353. init(0);
  354. while(x)
  355. {
  356. if(x & 1)
  357. add(b);
  358. x /= 2;
  359. b.add(b);
  360. }
  361. }
  362. uint32_t div(uint32_t x)
  363. {
  364. bitstack b(*this);
  365. for(int i = msb; i >= 0; i--)
  366. {
  367. uint64_t tmp = b.digits[i] + (((uint64_t)b.digits[i + 1]) << 32);
  368. uint32_t res = tmp / x;
  369. uint32_t rem = tmp % x;
  370. digits[i]= res;
  371. b.digits[i + 1] = 0;
  372. b.digits[i] = rem;
  373. }
  374. while(msb > 0 && digits[msb] == 0) msb--;
  375. return b.digits[0];
  376. }
  377. int msb, max_size;
  378. uint32_t *digits;
  379. char *str;
  380. };
  381. /*
  382. * Point handling
  383. */
  384. static unsigned int det_rand(unsigned int mod)
  385. {
  386. static unsigned long next = 1;
  387. next = next * 1103515245 + 12345;
  388. return ((unsigned)(next / 65536) % 32768) % mod;
  389. }
  390. static inline int range2int(float val, int range)
  391. {
  392. int ret = (int)(val * ((float)range - 0.0001));
  393. return ret < 0 ? 0 : ret > range - 1 ? range - 1 : ret;
  394. }
  395. static inline float int2midrange(int val, int range)
  396. {
  397. return (float)(1 + 2 * val) / (float)(2 * range);
  398. }
  399. static inline float int2fullrange(int val, int range)
  400. {
  401. return range > 1 ? (float)val / (float)(range - 1) : 0.0;
  402. }
  403. static inline void index2cell(int index, int *dx, int *dy)
  404. {
  405. *dx = (index / POINTS_PER_CELL) % dw;
  406. *dy = (index / POINTS_PER_CELL) / dw;
  407. }
  408. static inline void set_point(int index, float x, float y, float r,
  409. float g, float b, float s)
  410. {
  411. int dx, dy;
  412. index2cell(index, &dx, &dy);
  413. float fx = (x - dx * RANGE_X) / RANGE_X;
  414. float fy = (y - dy * RANGE_Y) / RANGE_Y;
  415. int iy = range2int(fy, RANGE_Y);
  416. int ix = range2int(fx, RANGE_X);
  417. int ir = range2int(r, RANGE_R);
  418. int ig = range2int(g, RANGE_G);
  419. int ib = range2int(b, RANGE_B);
  420. int is = range2int(s, RANGE_S);
  421. points[index] = iy + RANGE_Y * (ix + RANGE_X * (ir + RANGE_R *
  422. (ig + (RANGE_G * ib + (RANGE_B * is)))));
  423. }
  424. static inline void get_point(int index, float *x, float *y, float *r,
  425. float *g, float *b, float *s, bool final = false)
  426. {
  427. uint32_t pt = points[index];
  428. int dx, dy;
  429. index2cell(index, &dx, &dy);
  430. float fy = int2midrange(pt % RANGE_Y, RANGE_Y); pt /= RANGE_Y;
  431. float fx = int2midrange(pt % RANGE_X, RANGE_X); pt /= RANGE_X;
  432. *y = (fy + dy) * RANGE_Y /*+ 0.5 * (index & 1)*/;
  433. *x = (fx + dx) * RANGE_X /*+ 0.5 * (index & 1)*/;
  434. if(final)
  435. {
  436. *r = int2fullrange(pt % RANGE_R, RANGE_R); pt /= RANGE_R;
  437. *g = int2fullrange(pt % RANGE_G, RANGE_G); pt /= RANGE_G;
  438. *b = int2fullrange(pt % RANGE_B, RANGE_B); pt /= RANGE_B;
  439. }
  440. else
  441. {
  442. *r = int2midrange(pt % RANGE_R, RANGE_R); pt /= RANGE_R;
  443. *g = int2midrange(pt % RANGE_G, RANGE_G); pt /= RANGE_G;
  444. *b = int2midrange(pt % RANGE_B, RANGE_B); pt /= RANGE_B;
  445. }
  446. *s = int2fullrange(pt % RANGE_S, RANGE_S); pt /= RANGE_S;
  447. }
  448. static void add_point(float x, float y, float r, float g, float b, float s)
  449. {
  450. set_point(npoints, x, y, r, g, b, s);
  451. npoints++;
  452. }
  453. static uint32_t pack_coords(int x1, int y1, int x2, int y2, bool *swap)
  454. {
  455. int k1 = y1 * RANGE_X + x1;
  456. int k2 = y2 * RANGE_X + x2;
  457. /* XXX: this should not happen */
  458. if(k1 == k2)
  459. k1 += (x1 > 0 ? -1 : 1);
  460. *swap = k1 > k2;
  461. if(*swap)
  462. {
  463. int tmp = k1; k1 = k2; k2 = tmp;
  464. }
  465. return k2 * (k2 + 1) / 2 + k1;
  466. }
  467. static void unpack_coords(uint32_t pack, int *x1, int *y1, int *x2, int *y2)
  468. {
  469. int k2 = ((int)sqrt(1.0 + 8 * pack) - 1) / 2;
  470. int k1 = pack - k2 * (k2 + 1) / 2;
  471. *x1 = k1 % RANGE_X;
  472. *y1 = k1 / RANGE_X;
  473. *x2 = k2 % RANGE_X;
  474. *y2 = k2 / RANGE_X;
  475. }
  476. #if RANDOM_START == 1
  477. static void add_random_point()
  478. {
  479. points[npoints] = det_rand(RANGE_SBGRXY);
  480. npoints++;
  481. }
  482. #endif
  483. #define NB_OPS 20
  484. static uint8_t rand_op(void)
  485. {
  486. uint8_t x = det_rand(NB_OPS);
  487. /* Randomly ignore statistically less efficient ops */
  488. if(x == 0)
  489. return rand_op();
  490. if(x == 1 && (RANGE_S == 1 || det_rand(2)))
  491. return rand_op();
  492. if(x <= 5 && det_rand(2))
  493. return rand_op();
  494. //if((x < 10 || x > 15) && !det_rand(4)) /* Favour colour changes */
  495. // return rand_op();
  496. return x;
  497. }
  498. static uint32_t apply_op(uint8_t op, uint32_t val)
  499. {
  500. uint32_t rem, ext;
  501. switch(op)
  502. {
  503. case 0: /* Flip strength value */
  504. case 1:
  505. /* Statistics show that this helps often, but does not reduce
  506. * the error significantly. */
  507. rem = val % RANGE_BGRXY;
  508. ext = val / RANGE_BGRXY;
  509. ext ^= 1;
  510. return ext * RANGE_BGRXY + rem;
  511. case 2: /* Move up; if impossible, down */
  512. ext = val % RANGE_Y;
  513. ext = ext > 0 ? ext - 1 : ext + 1;
  514. return val / RANGE_Y * RANGE_Y + ext;
  515. case 3: /* Move down; if impossible, up */
  516. ext = val % RANGE_Y;
  517. ext = ext < RANGE_Y - 1 ? ext + 1 : ext - 1;
  518. return val / RANGE_Y * RANGE_Y + ext;
  519. case 4: /* Move left; if impossible, right */
  520. rem = val % RANGE_Y;
  521. ext = (val / RANGE_Y) % RANGE_X;
  522. ext = ext > 0 ? ext - 1 : ext + 1;
  523. return (val / RANGE_XY * RANGE_X + ext) * RANGE_Y + rem;
  524. case 5: /* Move left; if impossible, right */
  525. rem = val % RANGE_Y;
  526. ext = (val / RANGE_Y) % RANGE_X;
  527. ext = ext < RANGE_X - 1 ? ext + 1 : ext - 1;
  528. return (val / RANGE_XY * RANGE_X + ext) * RANGE_Y + rem;
  529. case 6: /* Corner 1 */
  530. return apply_op(2, apply_op(4, val));
  531. case 7: /* Corner 2 */
  532. return apply_op(2, apply_op(5, val));
  533. case 8: /* Corner 3 */
  534. return apply_op(3, apply_op(5, val));
  535. case 9: /* Corner 4 */
  536. return apply_op(3, apply_op(4, val));
  537. case 16: /* Double up */
  538. return apply_op(2, apply_op(2, val));
  539. case 17: /* Double down */
  540. return apply_op(3, apply_op(3, val));
  541. case 18: /* Double left */
  542. return apply_op(4, apply_op(4, val));
  543. case 19: /* Double right */
  544. return apply_op(5, apply_op(5, val));
  545. case 10: /* R-- (or R++) */
  546. rem = val % RANGE_XY;
  547. ext = (val / RANGE_XY) % RANGE_R;
  548. ext = ext > 0 ? ext - 1 : ext + 1;
  549. return (val / RANGE_RXY * RANGE_R + ext) * RANGE_XY + rem;
  550. case 11: /* R++ (or R--) */
  551. rem = val % RANGE_XY;
  552. ext = (val / RANGE_XY) % RANGE_R;
  553. ext = ext < RANGE_R - 1 ? ext + 1 : ext - 1;
  554. return (val / RANGE_RXY * RANGE_R + ext) * RANGE_XY + rem;
  555. case 12: /* G-- (or G++) */
  556. rem = val % RANGE_RXY;
  557. ext = (val / RANGE_RXY) % RANGE_G;
  558. ext = ext > 0 ? ext - 1 : ext + 1;
  559. return (val / RANGE_GRXY * RANGE_G + ext) * RANGE_RXY + rem;
  560. case 13: /* G++ (or G--) */
  561. rem = val % RANGE_RXY;
  562. ext = (val / RANGE_RXY) % RANGE_G;
  563. ext = ext < RANGE_G - 1 ? ext + 1 : ext - 1;
  564. return (val / RANGE_GRXY * RANGE_G + ext) * RANGE_RXY + rem;
  565. case 14: /* B-- (or B++) */
  566. rem = val % RANGE_GRXY;
  567. ext = (val / RANGE_GRXY) % RANGE_B;
  568. ext = ext > 0 ? ext - 1 : ext + 1;
  569. return (val / RANGE_BGRXY * RANGE_B + ext) * RANGE_GRXY + rem;
  570. case 15: /* B++ (or B--) */
  571. rem = val % RANGE_GRXY;
  572. ext = (val / RANGE_GRXY) % RANGE_B;
  573. ext = ext < RANGE_B - 1 ? ext + 1 : ext - 1;
  574. return (val / RANGE_BGRXY * RANGE_B + ext) * RANGE_GRXY + rem;
  575. #if 0
  576. case 15: /* Brightness-- */
  577. return apply_op(9, apply_op(11, apply_op(13, val)));
  578. case 16: /* Brightness++ */
  579. return apply_op(10, apply_op(12, apply_op(14, val)));
  580. case 17: /* RG-- */
  581. return apply_op(9, apply_op(11, val));
  582. case 18: /* RG++ */
  583. return apply_op(10, apply_op(12, val));
  584. case 19: /* GB-- */
  585. return apply_op(11, apply_op(13, val));
  586. case 20: /* GB++ */
  587. return apply_op(12, apply_op(14, val));
  588. case 21: /* RB-- */
  589. return apply_op(9, apply_op(13, val));
  590. case 22: /* RB++ */
  591. return apply_op(10, apply_op(14, val));
  592. #endif
  593. default:
  594. return val;
  595. }
  596. }
  597. static void render(pipi_image_t *dst,
  598. int rx, int ry, int rw, int rh, bool final)
  599. {
  600. int lookup[dw * RANGE_X * 2 * dh * RANGE_Y * 2];
  601. pipi_pixels_t *p = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  602. float *data = (float *)p->pixels;
  603. int x, y;
  604. memset(lookup, 0, sizeof(lookup));
  605. dt.clear();
  606. for(int i = 0; i < npoints; i++)
  607. {
  608. float fx, fy, fr, fg, fb, fs;
  609. get_point(i, &fx, &fy, &fr, &fg, &fb, &fs);
  610. dt.insert(K::Point_2(fx + dw * RANGE_X, fy + dh * RANGE_Y));
  611. /* Keep link to point */
  612. lookup[(int)(fx * 2) + dw * RANGE_X * 2 * (int)(fy * 2)] = i;
  613. }
  614. /* Add fake points to close the triangulation */
  615. dt.insert(K::Point_2(0, 0));
  616. dt.insert(K::Point_2(3 * dw * RANGE_X, 0));
  617. dt.insert(K::Point_2(0, 3 * dh * RANGE_Y));
  618. dt.insert(K::Point_2(3 * dw * RANGE_X, 3 * dh * RANGE_Y));
  619. for(y = ry; y < ry + rh; y++)
  620. {
  621. for(x = rx; x < rx + rw; x++)
  622. {
  623. float myx = (float)x * dw * RANGE_X / p->w;
  624. float myy = (float)y * dh * RANGE_Y / p->h;
  625. K::Point_2 m(myx + dw * RANGE_X, myy + dh * RANGE_Y);
  626. Point_coordinate_vector coords;
  627. CGAL::Triple<
  628. std::back_insert_iterator<Point_coordinate_vector>,
  629. K::FT, bool> result =
  630. CGAL::natural_neighbor_coordinates_2(dt, m,
  631. std::back_inserter(coords));
  632. float r = 0.0f, g = 0.0f, b = 0.0f, norm = 0.000000000000001f;
  633. Point_coordinate_vector::iterator it;
  634. for(it = coords.begin(); it != coords.end(); ++it)
  635. {
  636. float fx, fy, fr, fg, fb, fs;
  637. fx = (*it).first.x() - dw * RANGE_X;
  638. fy = (*it).first.y() - dh * RANGE_Y;
  639. if(fx < 0 || fy < 0
  640. || fx > dw * RANGE_X - 1 || fy > dh * RANGE_Y - 1)
  641. continue;
  642. int index = lookup[(int)(fx * 2)
  643. + dw * RANGE_X * 2 * (int)(fy * 2)];
  644. get_point(index, &fx, &fy, &fr, &fg, &fb, &fs, final);
  645. //float k = pow((*it).second * (1.0 + fs), 1.2);
  646. float k = (*it).second * (1.00f + fs);
  647. //float k = (*it).second * (0.60f + fs);
  648. //float k = pow((*it).second, (1.0f + fs));
  649. // Try to attenuate peak artifacts
  650. //k /= (0.1 * (RANGE_X * RANGE_X + RANGE_Y * RANGE_Y)
  651. // + (myx - fx) * (myx - fx) + (myy - fy) * (myy - fy));
  652. // Cute circles
  653. //k = 1.0 / (0.015 * (RANGE_X * RANGE_X + RANGE_Y * RANGE_Y)
  654. // + (myx - fx) * (myx - fx) + (myy - fy) * (myy - fy));
  655. r += k * fr;
  656. g += k * fg;
  657. b += k * fb;
  658. norm += k;
  659. }
  660. data[4 * (x + y * p->w) + 0] = r / norm;
  661. data[4 * (x + y * p->w) + 1] = g / norm;
  662. data[4 * (x + y * p->w) + 2] = b / norm;
  663. data[4 * (x + y * p->w) + 3] = 0.0;
  664. }
  665. }
  666. pipi_release_pixels(dst, p);
  667. }
  668. static void analyse(pipi_image_t *src)
  669. {
  670. pipi_pixels_t *p = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  671. float *data = (float *)p->pixels;
  672. for(unsigned int dy = 0; dy < dh; dy++)
  673. for(unsigned int dx = 0; dx < dw; dx++)
  674. {
  675. float min = 1.1f, max = -0.1f, mr = 0.0f, mg = 0.0f, mb = 0.0f;
  676. float total = 0.0;
  677. int xmin = 0, xmax = 0, ymin = 0, ymax = 0;
  678. int npixels = 0;
  679. for(unsigned int iy = RANGE_Y * dy; iy < RANGE_Y * (dy + 1); iy++)
  680. for(unsigned int ix = RANGE_X * dx; ix < RANGE_X * (dx + 1); ix++)
  681. {
  682. float lum = 0.0f;
  683. lum += data[4 * (ix + iy * p->w) + 0];
  684. lum += data[4 * (ix + iy * p->w) + 1];
  685. lum += data[4 * (ix + iy * p->w) + 2];
  686. lum /= 3;
  687. mr += data[4 * (ix + iy * p->w) + 0];
  688. mg += data[4 * (ix + iy * p->w) + 1];
  689. mb += data[4 * (ix + iy * p->w) + 2];
  690. if(lum < min)
  691. {
  692. min = lum;
  693. xmin = ix;
  694. ymin = iy;
  695. }
  696. if(lum > max)
  697. {
  698. max = lum;
  699. xmax = ix;
  700. ymax = iy;
  701. }
  702. total += lum;
  703. npixels++;
  704. }
  705. total /= npixels;
  706. mr /= npixels;
  707. mg /= npixels;
  708. mb /= npixels;
  709. float wmin, wmax;
  710. if(total < min + (max - min) / 4)
  711. wmin = 1.0, wmax = 0.0;
  712. else if(total < min + (max - min) / 4 * 3)
  713. wmin = 0.0, wmax = 0.0;
  714. else
  715. wmin = 0.0, wmax = 1.0;
  716. #if RANDOM_START == 1
  717. # if POINTS_PER_CELL == 2
  718. add_random_point();
  719. # endif
  720. add_random_point();
  721. #else
  722. /* 0.80 and 0.20 were chosen empirically, it gives a 10% better
  723. * initial distance. Definitely worth it. */
  724. # if POINTS_PER_CELL == 1
  725. if(total < min + (max - min) / 2)
  726. {
  727. # endif
  728. add_point(xmin, ymin,
  729. data[4 * (xmin + ymin * p->w) + 0] * 0.80 + mr * 0.20,
  730. data[4 * (xmin + ymin * p->w) + 1] * 0.80 + mg * 0.20,
  731. data[4 * (xmin + ymin * p->w) + 2] * 0.80 + mb * 0.20,
  732. wmin);
  733. # if POINTS_PER_CELL == 1
  734. }
  735. else
  736. {
  737. # endif
  738. add_point(xmax, ymax,
  739. data[4 * (xmax + ymax * p->w) + 0] * 0.80 + mr * 0.20,
  740. data[4 * (xmax + ymax * p->w) + 1] * 0.80 + mg * 0.20,
  741. data[4 * (xmax + ymax * p->w) + 2] * 0.80 + mb * 0.20,
  742. wmax);
  743. # if POINTS_PER_CELL == 1
  744. }
  745. # endif
  746. #endif
  747. }
  748. }
  749. #define MOREINFO "Try `%s --help' for more information.\n"
  750. int main(int argc, char *argv[])
  751. {
  752. uint32_t unicode_data[2048];
  753. int opstats[2 * NB_OPS];
  754. char const *srcname = NULL, *dstname = NULL;
  755. pipi_image_t *src, *tmp, *dst;
  756. double error = 1.0;
  757. int width, height;
  758. /* Parse command-line options */
  759. for(;;)
  760. {
  761. int option_index = 0;
  762. static struct myoption long_options[] =
  763. {
  764. { "output", 1, NULL, 'o' },
  765. { "length", 1, NULL, 'l' },
  766. { "charset", 1, NULL, 'c' },
  767. { "quality", 1, NULL, 'q' },
  768. { "debug", 0, NULL, 'd' },
  769. { "help", 0, NULL, 'h' },
  770. { NULL, 0, NULL, 0 },
  771. };
  772. int c = mygetopt(argc, argv, "o:l:c:q:dh", long_options, &option_index);
  773. if(c == -1)
  774. break;
  775. switch(c)
  776. {
  777. case 'o':
  778. dstname = myoptarg;
  779. break;
  780. case 'l':
  781. MAX_MSG_LEN = atoi(myoptarg);
  782. if(MAX_MSG_LEN < 16)
  783. {
  784. fprintf(stderr, "Warning: rounding minimum message length to 16\n");
  785. MAX_MSG_LEN = 16;
  786. }
  787. break;
  788. case 'c':
  789. if(!strcmp(myoptarg, "ascii"))
  790. unichars = unichars_ascii;
  791. else if(!strcmp(myoptarg, "cjk"))
  792. unichars = unichars_cjk;
  793. else if(!strcmp(myoptarg, "symbols"))
  794. unichars = unichars_symbols;
  795. else
  796. {
  797. fprintf(stderr, "Error: invalid char block \"%s\".", myoptarg);
  798. fprintf(stderr, "Valid sets are: ascii, cjk, symbols\n");
  799. return EXIT_FAILURE;
  800. }
  801. break;
  802. case 'q':
  803. ITERATIONS_PER_POINT = 10 * atof(myoptarg);
  804. if(ITERATIONS_PER_POINT < 0)
  805. ITERATIONS_PER_POINT = 0;
  806. else if(ITERATIONS_PER_POINT > 200)
  807. ITERATIONS_PER_POINT = 200;
  808. break;
  809. case 'd':
  810. DEBUG_MODE = true;
  811. break;
  812. case 'h':
  813. printf("Usage: img2twit [OPTIONS] SOURCE\n");
  814. printf(" img2twit [OPTIONS] -o DESTINATION\n");
  815. printf("Encode SOURCE image to stdout or decode stdin to DESTINATION.\n");
  816. printf("\n");
  817. printf("Mandatory arguments to long options are mandatory for short options too.\n");
  818. printf(" -o, --output <filename> output resulting image to filename\n");
  819. printf(" -l, --length <size> message length in characters (default 140)\n");
  820. printf(" -c, --charset <block> character set to use (ascii, [cjk], symbols)\n");
  821. printf(" -q, --quality <rate> set image quality (0 - 20) (default 5)\n");
  822. printf(" -d, --debug print debug information\n");
  823. printf(" -h, --help display this help and exit\n");
  824. printf("\n");
  825. printf("Written by Sam Hocevar. Report bugs to <sam@hocevar.net>.\n");
  826. return EXIT_SUCCESS;
  827. default:
  828. fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
  829. printf(MOREINFO, argv[0]);
  830. return EXIT_FAILURE;
  831. }
  832. }
  833. if(myoptind == argc && !dstname)
  834. {
  835. fprintf(stderr, "%s: too few arguments\n", argv[0]);
  836. printf(MOREINFO, argv[0]);
  837. return EXIT_FAILURE;
  838. }
  839. if((myoptind == argc - 1 && dstname) || myoptind < argc - 1)
  840. {
  841. fprintf(stderr, "%s: too many arguments\n", argv[0]);
  842. printf(MOREINFO, argv[0]);
  843. return EXIT_FAILURE;
  844. }
  845. if(myoptind == argc - 1)
  846. srcname = argv[myoptind];
  847. /* Decoding mode: read UTF-8 text from stdin */
  848. if(dstname)
  849. for(MAX_MSG_LEN = 0; ;)
  850. {
  851. uint32_t ch = fread_utf8(stdin);
  852. if(ch == 0xffffffff || ch == '\n')
  853. break;
  854. if(ch <= ' ')
  855. continue;
  856. unicode_data[MAX_MSG_LEN++] = ch;
  857. if(MAX_MSG_LEN >= 2048)
  858. {
  859. fprintf(stderr, "Error: message too long.\n");
  860. return EXIT_FAILURE;
  861. }
  862. }
  863. if(MAX_MSG_LEN == 0)
  864. {
  865. fprintf(stderr, "Error: empty message.\n");
  866. return EXIT_FAILURE;
  867. }
  868. bitstack b(MAX_MSG_LEN); /* We cannot declare this before, because
  869. * MAX_MSG_LEN wouldn't be defined. */
  870. /* Autodetect charset if decoding, otherwise switch to CJK. */
  871. if(dstname)
  872. {
  873. char const *charset;
  874. if(unicode_data[0] >= 0x0021 && unicode_data[0] < 0x007f)
  875. {
  876. unichars = unichars_ascii;
  877. charset = "ascii";
  878. }
  879. else if(unicode_data[0] >= 0x4e00 && unicode_data[0] < 0x9fa6)
  880. {
  881. unichars = unichars_cjk;
  882. charset = "cjk";
  883. }
  884. else if(unicode_data[0] >= 0x25a0 && unicode_data[0] < 0x27bf)
  885. {
  886. unichars = unichars_symbols;
  887. charset = "symbols";
  888. }
  889. else
  890. {
  891. fprintf(stderr, "Error: unable to detect charset\n");
  892. return EXIT_FAILURE;
  893. }
  894. if(DEBUG_MODE)
  895. fprintf(stderr, "Detected charset \"%s\"\n", charset);
  896. }
  897. else if(!unichars)
  898. unichars = unichars_cjk;
  899. pipi_set_gamma(1.0);
  900. /* Precompute bit allocation */
  901. NUM_CHARACTERS = count_unichars();
  902. if(dstname)
  903. {
  904. /* Decoding mode: find each character's index in our character
  905. * list, and push it to our wonderful custom bitstream. */
  906. for(int i = MAX_MSG_LEN; i--; )
  907. b.push(uni2index(unicode_data[i]), NUM_CHARACTERS);
  908. /* Read width and height from bitstream */
  909. src = NULL;
  910. width = b.pop(RANGE_W) + 1;
  911. height = b.pop(RANGE_H) + 1;
  912. }
  913. else
  914. {
  915. /* Argument given: open image for encoding */
  916. src = pipi_load(srcname);
  917. if(!src)
  918. {
  919. fprintf(stderr, "Error loading %s\n", srcname);
  920. return EXIT_FAILURE;
  921. }
  922. width = pipi_get_image_width(src);
  923. height = pipi_get_image_height(src);
  924. }
  925. if(width <= 0 || height <= 0 || width > RANGE_W || height > RANGE_H)
  926. {
  927. fprintf(stderr, "Error: image size %ix%i is out of bounds\n",
  928. width, height);
  929. return EXIT_FAILURE;
  930. }
  931. compute_ranges(width, height);
  932. /* Try to cram some more information into our points as long as it
  933. * does not change the cell distribution. This cannot be too clever,
  934. * because we want the computation to depend only on the source image
  935. * coordinates. */
  936. #define TRY(op, revert) \
  937. do { \
  938. unsigned int olddw = dw, olddh = dh; \
  939. op; compute_ranges(width, height); \
  940. if(dw != olddw || dh != olddh) \
  941. { revert; compute_ranges(width, height); } \
  942. } while(0)
  943. for(int i = 0; i < 2; i++)
  944. {
  945. TRY(RANGE_G++, RANGE_G--);
  946. TRY(RANGE_R++, RANGE_R--);
  947. TRY(RANGE_B++, RANGE_B--);
  948. }
  949. for(int i = 0; i < 10; i++)
  950. {
  951. if((float)width / dw >= (float)height / dh)
  952. {
  953. TRY(RANGE_X++, RANGE_X--);
  954. TRY(RANGE_Y++, RANGE_Y--);
  955. }
  956. else
  957. {
  958. TRY(RANGE_Y++, RANGE_Y--);
  959. TRY(RANGE_X++, RANGE_X--);
  960. }
  961. }
  962. /* Print debug information */
  963. if(DEBUG_MODE)
  964. {
  965. fprintf(stderr, "Message size: %i\n", MAX_MSG_LEN);
  966. fprintf(stderr, "Available characters: %i\n", NUM_CHARACTERS);
  967. fprintf(stderr, "Available bits: %f\n", TOTAL_BITS);
  968. fprintf(stderr, "Width/Height ranges: %ix%i\n", RANGE_W, RANGE_H);
  969. fprintf(stderr, "Image resolution: %ix%i\n", width, height);
  970. fprintf(stderr, "Header bits: %f\n", HEADER_BITS);
  971. fprintf(stderr, "Bits available for data: %f\n", DATA_BITS);
  972. fprintf(stderr, "X/Y/Red/Green/Blue/Extra ranges: %i %i %i %i %i %i\n",
  973. RANGE_X, RANGE_Y, RANGE_R, RANGE_G, RANGE_B, RANGE_S);
  974. fprintf(stderr, "Cell bits: %f\n", CELL_BITS);
  975. fprintf(stderr, "Available cells: %i\n", TOTAL_CELLS);
  976. fprintf(stderr, "Wasted bits: %f\n",
  977. DATA_BITS - CELL_BITS * TOTAL_CELLS);
  978. fprintf(stderr, "Chosen image ratio: %i:%i (wasting %i point cells)\n",
  979. dw, dh, TOTAL_CELLS - dw * dh);
  980. fprintf(stderr, "Total wasted bits: %f\n",
  981. DATA_BITS - CELL_BITS * dw * dh);
  982. }
  983. if(srcname)
  984. {
  985. /* Resize and filter image to better state */
  986. tmp = pipi_gaussian_blur(src, 0.5 * dw * RANGE_X / width);
  987. pipi_free(src);
  988. src = pipi_resize(tmp, dw * RANGE_X, dh * RANGE_Y);
  989. pipi_free(tmp);
  990. /* Analyse image */
  991. analyse(src);
  992. /* Render what we just computed */
  993. tmp = pipi_new(dw * RANGE_X, dh * RANGE_Y);
  994. render(tmp, 0, 0, dw * RANGE_X, dh * RANGE_Y, false);
  995. error = pipi_measure_rmsd(src, tmp);
  996. if(DEBUG_MODE)
  997. fprintf(stderr, "Initial distance: %2.10g\n", error);
  998. memset(opstats, 0, sizeof(opstats));
  999. for(int iter = 0, stuck = 0, failures = 0, success = 0;
  1000. iter < MAX_ITERATIONS /* && stuck < 5 && */;
  1001. iter++)
  1002. {
  1003. if(failures > 500)
  1004. {
  1005. stuck++;
  1006. failures = 0;
  1007. }
  1008. if(!DEBUG_MODE && !(iter % 16))
  1009. fprintf(stderr, "\rEncoding... %i%%",
  1010. iter * 100 / MAX_ITERATIONS);
  1011. pipi_image_t *scrap = pipi_copy(tmp);
  1012. /* Choose a point at random */
  1013. int pt = det_rand(npoints);
  1014. uint32_t oldval = points[pt];
  1015. /* Compute the affected image zone */
  1016. float fx, fy, fr, fg, fb, fs;
  1017. get_point(pt, &fx, &fy, &fr, &fg, &fb, &fs);
  1018. int zonex = (int)fx / RANGE_X - 2;
  1019. int zoney = (int)fy / RANGE_Y - 2;
  1020. int zonew = 4;
  1021. int zoneh = 4;
  1022. if(zonex < 0) { zonew += zonex; zonex = 0; }
  1023. if(zoney < 0) { zoneh += zoney; zoney = 0;; }
  1024. if(zonex + zonew > (int)dw) { zonew = dw - zonex; }
  1025. if(zoney + zoneh > (int)dh) { zoneh = dh - zoney; }
  1026. /* Choose random operations and measure their effect */
  1027. uint8_t op1 = rand_op();
  1028. //uint8_t op2 = rand_op();
  1029. uint32_t candidates[3];
  1030. double besterr = error + 1.0;
  1031. int bestop = -1;
  1032. candidates[0] = apply_op(op1, oldval);
  1033. //candidates[1] = apply_op(op2, oldval);
  1034. //candidates[2] = apply_op(op1, apply_op(op2, oldval));
  1035. for(int i = 0; i < 1; i++)
  1036. //for(int i = 0; i < 3; i++)
  1037. {
  1038. if(oldval == candidates[i])
  1039. continue;
  1040. points[pt] = candidates[i];
  1041. render(scrap, zonex * RANGE_X, zoney * RANGE_Y,
  1042. zonew * RANGE_X, zoneh * RANGE_Y, false);
  1043. double newerr = pipi_measure_rmsd(src, scrap);
  1044. if(newerr < besterr)
  1045. {
  1046. besterr = newerr;
  1047. bestop = i;
  1048. }
  1049. }
  1050. opstats[op1 * 2]++;
  1051. //opstats[op2 * 2]++;
  1052. if(besterr < error)
  1053. {
  1054. points[pt] = candidates[bestop];
  1055. /* Redraw image if the last check wasn't the best one */
  1056. if(bestop != 0)
  1057. render(scrap, zonex * RANGE_X, zoney * RANGE_Y,
  1058. zonew * RANGE_X, zoneh * RANGE_Y, false);
  1059. pipi_free(tmp);
  1060. tmp = scrap;
  1061. if(DEBUG_MODE)
  1062. fprintf(stderr, "%08i -.%08i %2.010g after op%i(%i)\n",
  1063. iter, (int)((error - besterr) * 100000000), error,
  1064. op1, pt);
  1065. error = besterr;
  1066. opstats[op1 * 2 + 1]++;
  1067. //opstats[op2 * 2 + 1]++;
  1068. failures = 0;
  1069. success++;
  1070. /* Save image! */
  1071. char buf[128];
  1072. sprintf(buf, "twit%08i.bmp", success);
  1073. if((success % 10) == 0)
  1074. pipi_save(tmp, buf);
  1075. }
  1076. else
  1077. {
  1078. pipi_free(scrap);
  1079. points[pt] = oldval;
  1080. failures++;
  1081. }
  1082. }
  1083. if(DEBUG_MODE)
  1084. {
  1085. for(int j = 0; j < 2; j++)
  1086. {
  1087. fprintf(stderr, "operation: ");
  1088. for(int i = NB_OPS / 2 * j; i < NB_OPS / 2 * (j + 1); i++)
  1089. fprintf(stderr, "%4i ", i);
  1090. fprintf(stderr, "\nattempts: ");
  1091. for(int i = NB_OPS / 2 * j; i < NB_OPS / 2 * (j + 1); i++)
  1092. fprintf(stderr, "%4i ", opstats[i * 2]);
  1093. fprintf(stderr, "\nsuccesses: ");
  1094. for(int i = NB_OPS / 2 * j; i < NB_OPS / 2 * (j + 1); i++)
  1095. fprintf(stderr, "%4i ", opstats[i * 2 + 1]);
  1096. fprintf(stderr, "\n");
  1097. }
  1098. fprintf(stderr, "Distance: %2.10g\n", error);
  1099. }
  1100. else
  1101. fprintf(stderr, "\r \r");
  1102. #if 0
  1103. dst = pipi_resize(tmp, width, height);
  1104. pipi_free(tmp);
  1105. /* Save image and bail out */
  1106. pipi_save(dst, "lol.bmp");
  1107. pipi_free(dst);
  1108. #endif
  1109. /* Push our points to the bitstream */
  1110. for(int i = 0; i < npoints; i += POINTS_PER_CELL)
  1111. {
  1112. #if POINTS_PER_CELL == 2
  1113. int x1, y1, x2, y2;
  1114. x1 = (points[i] / RANGE_Y) % RANGE_X;
  1115. y1 = points[i] % RANGE_Y;
  1116. x2 = (points[i + 1] / RANGE_Y) % RANGE_X;
  1117. y2 = points[i + 1] % RANGE_Y;
  1118. bool swap;
  1119. uint32_t pack = pack_coords(x1, y1, x2, y2, &swap);
  1120. b.push(points[i + (swap ? 1 : 0)] / RANGE_XY, RANGE_SBGR);
  1121. b.push(points[i + (swap ? 0 : 1)] / RANGE_XY, RANGE_SBGR);
  1122. b.push(pack, RANGE_XY2);
  1123. #else
  1124. b.push(points[i], RANGE_SBGRXY);
  1125. #endif
  1126. }
  1127. b.push(height - 1, RANGE_H);
  1128. b.push(width - 1, RANGE_W);
  1129. /* Pop Unicode characters from the bitstream and print them */
  1130. for(int i = 0; i < MAX_MSG_LEN; i++)
  1131. fwrite_utf8(stdout, index2uni(b.pop(NUM_CHARACTERS)));
  1132. fprintf(stdout, "\n");
  1133. }
  1134. else
  1135. {
  1136. /* Pop points from the bitstream */
  1137. for(int i = dw * dh; i--; )
  1138. {
  1139. #if POINTS_PER_CELL == 2
  1140. uint32_t pack = b.pop(RANGE_XY2);
  1141. uint32_t p2 = b.pop(RANGE_SBGR);
  1142. uint32_t p1 = b.pop(RANGE_SBGR);
  1143. int x1, y1, x2, y2;
  1144. unpack_coords(pack, &x1, &y1, &x2, &y2);
  1145. points[i * 2] = p1 * RANGE_XY + x1 * RANGE_Y + y1;
  1146. points[i * 2 + 1] = p2 * RANGE_XY + x2 * RANGE_Y + y2;
  1147. #else
  1148. points[i] = b.pop(RANGE_SBGRXY);
  1149. #endif
  1150. }
  1151. npoints = dw * dh * POINTS_PER_CELL;
  1152. /* Render these points to a new image */
  1153. dst = pipi_new(width, height);
  1154. render(dst, 0, 0, width, height, true);
  1155. /* Save image and bail out */
  1156. pipi_save(dst, dstname);
  1157. pipi_free(dst);
  1158. }
  1159. return EXIT_SUCCESS;
  1160. }