Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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