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.
 
 
 
 
 
 

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