No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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