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.
 
 
 
 
 
 

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