Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

1377 lignes
41 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, bool final = false)
  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. if(final)
  439. {
  440. *r = int2fullrange(points[index].r, RANGE_R);
  441. *g = int2fullrange(points[index].g, RANGE_G);
  442. *b = int2fullrange(points[index].b, RANGE_B);
  443. }
  444. else
  445. {
  446. *r = int2midrange(points[index].r, RANGE_R);
  447. *g = int2midrange(points[index].g, RANGE_G);
  448. *b = int2midrange(points[index].b, RANGE_B);
  449. }
  450. *s = int2fullrange(points[index].s, RANGE_S);
  451. }
  452. static void add_point(float x, float y, float r, float g, float b, float s)
  453. {
  454. set_point(npoints, x, y, r, g, b, s);
  455. npoints++;
  456. }
  457. static uint32_t pack_coords(int x1, int y1, int x2, int y2, bool *swap)
  458. {
  459. int k1 = y1 * RANGE_X + x1;
  460. int k2 = y2 * RANGE_X + x2;
  461. /* XXX: this should not happen */
  462. if(k1 == k2)
  463. k1 += (x1 > 0 ? -1 : 1);
  464. *swap = k1 > k2;
  465. if(*swap)
  466. {
  467. int tmp = k1; k1 = k2; k2 = tmp;
  468. }
  469. return k2 * (k2 + 1) / 2 + k1;
  470. }
  471. static void unpack_coords(uint32_t pack, int *x1, int *y1, int *x2, int *y2)
  472. {
  473. int k2 = ((int)sqrt(1.0 + 8 * pack) - 1) / 2;
  474. int k1 = pack - k2 * (k2 + 1) / 2;
  475. *x1 = k1 % RANGE_X;
  476. *y1 = k1 / RANGE_X;
  477. *x2 = k2 % RANGE_X;
  478. *y2 = k2 / RANGE_X;
  479. }
  480. #if RANDOM_START == 1
  481. static void add_random_point()
  482. {
  483. points[npoints].x = det_rand(RANGE_X);
  484. points[npoints].y = det_rand(RANGE_Y);
  485. points[npoints].r = det_rand(RANGE_R);
  486. points[npoints].g = det_rand(RANGE_G);
  487. points[npoints].b = det_rand(RANGE_B);
  488. points[npoints].s = det_rand(RANGE_S);
  489. npoints++;
  490. }
  491. #endif
  492. #define NB_OPS 20
  493. static uint8_t rand_op(void)
  494. {
  495. uint8_t x = det_rand(NB_OPS);
  496. /* Randomly ignore statistically less efficient ops */
  497. if(x == 0)
  498. return rand_op();
  499. if(x == 1 && (RANGE_S == 1 || det_rand(2)))
  500. return rand_op();
  501. if(x <= 5 && det_rand(2))
  502. return rand_op();
  503. //if((x < 10 || x > 15) && !det_rand(4)) /* Favour colour changes */
  504. // return rand_op();
  505. return x;
  506. }
  507. static void apply_op(uint8_t op, point_t *val)
  508. {
  509. switch(op)
  510. {
  511. case 0: /* Flip strength value */
  512. case 1:
  513. /* Statistics show that this helps often, but does not reduce
  514. * the error significantly. */
  515. val->s ^= 1; break;
  516. case 2: /* Move up; if impossible, down */
  517. val->y = val->y > 0 ? val->y - 1 : val->y + 1; break;
  518. case 3: /* Move down; if impossible, up */
  519. val->y = val->y + 1U < RANGE_Y ? val->y + 1 : val->y - 1; break;
  520. case 4: /* Move left; if impossible, right */
  521. val->x = val->x > 0 ? val->x - 1 : val->x + 1; break;
  522. case 5: /* Move right; if impossible, left */
  523. val->x = val->x + 1U < RANGE_X ? val->x + 1 : val->x - 1; break;
  524. case 6: /* Corner 1 */
  525. val->y = val->y > 0 ? val->y - 1 : val->y + 1;
  526. val->x = val->x > 0 ? val->x - 1 : val->x + 1; break;
  527. case 7: /* Corner 2 */
  528. val->y = val->y > 0 ? val->y - 1 : val->y + 1;
  529. val->x = val->x + 1U < RANGE_X ? val->x + 1 : val->x - 1; break;
  530. case 8: /* Corner 3 */
  531. val->y = val->y + 1U < RANGE_Y ? val->y + 1 : val->y - 1;
  532. val->x = val->x + 1U < RANGE_X ? val->x + 1 : val->x - 1; break;
  533. case 9: /* Corner 4 */
  534. val->y = val->y + 1U < RANGE_Y ? val->y + 1 : val->y - 1;
  535. val->x = val->x > 0 ? val->x - 1 : val->x + 1; break;
  536. case 16: /* Double up */
  537. val->y = val->y > 1 ? val->y - 2 : val->y + 2; break;
  538. case 17: /* Double down */
  539. val->y = val->y + 2U < RANGE_Y ? val->y + 2 : val->y - 2; break;
  540. case 18: /* Double left */
  541. val->x = val->x > 1 ? val->x - 2 : val->x + 2; break;
  542. case 19: /* Double right */
  543. val->x = val->x + 2U < RANGE_X ? val->x + 2 : val->x - 2; break;
  544. case 10: /* R-- (or R++) */
  545. val->r = val->r > 0 ? val->r - 1 : val->r + 1; break;
  546. case 11: /* R++ (or R--) */
  547. val->r = val->r + 1U < RANGE_R ? val->r + 1 : val->r - 1; break;
  548. case 12: /* G-- (or G++) */
  549. val->g = val->g > 0 ? val->g - 1 : val->g + 1; break;
  550. case 13: /* G++ (or G--) */
  551. val->g = val->g + 1U < RANGE_G ? val->g + 1 : val->g - 1; break;
  552. case 14: /* B-- (or B++) */
  553. val->b = val->b > 0 ? val->g - 1 : val->b + 1; break;
  554. case 15: /* B++ (or B--) */
  555. val->b = val->b + 1U < RANGE_B ? val->b + 1 : val->b - 1; break;
  556. #if 0
  557. case 15: /* Brightness-- */
  558. apply_op(9, val); apply_op(11, val); apply_op(13, val); break;
  559. case 16: /* Brightness++ */
  560. apply_op(10, val); apply_op(12, val); apply_op(14, val); break;
  561. case 17: /* RG-- */
  562. apply_op(9, val); apply_op(11, val); break;
  563. case 18: /* RG++ */
  564. apply_op(10, val); apply_op(12, val); break;
  565. case 19: /* GB-- */
  566. apply_op(11, val); apply_op(13, val); break;
  567. case 20: /* GB++ */
  568. apply_op(12, val); apply_op(14, val); break;
  569. case 21: /* RB-- */
  570. apply_op(9, val); apply_op(13, val); break;
  571. case 22: /* RB++ */
  572. apply_op(10, val); apply_op(14, val); break;
  573. #endif
  574. default:
  575. break;
  576. }
  577. }
  578. static void render(pipi_image_t *dst,
  579. int rx, int ry, int rw, int rh, bool final)
  580. {
  581. int lookup[dw * RANGE_X * 2 * dh * RANGE_Y * 2];
  582. pipi_pixels_t *p = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  583. float *data = (float *)p->pixels;
  584. int x, y;
  585. memset(lookup, 0, sizeof(lookup));
  586. dt.clear();
  587. for(int i = 0; i < npoints; i++)
  588. {
  589. float fx, fy, fr, fg, fb, fs;
  590. get_point(i, &fx, &fy, &fr, &fg, &fb, &fs);
  591. dt.insert(K::Point_2(fx + dw * RANGE_X, fy + dh * RANGE_Y));
  592. /* Keep link to point */
  593. lookup[(int)(fx * 2) + dw * RANGE_X * 2 * (int)(fy * 2)] = i;
  594. }
  595. /* Add fake points to close the triangulation */
  596. dt.insert(K::Point_2(0, 0));
  597. dt.insert(K::Point_2(3 * dw * RANGE_X, 0));
  598. dt.insert(K::Point_2(0, 3 * dh * RANGE_Y));
  599. dt.insert(K::Point_2(3 * dw * RANGE_X, 3 * dh * RANGE_Y));
  600. for(y = ry; y < ry + rh; y++)
  601. {
  602. for(x = rx; x < rx + rw; x++)
  603. {
  604. float myx = (float)x * dw * RANGE_X / p->w;
  605. float myy = (float)y * dh * RANGE_Y / p->h;
  606. K::Point_2 m(myx + dw * RANGE_X, myy + dh * RANGE_Y);
  607. Point_coordinate_vector coords;
  608. CGAL::Triple<
  609. std::back_insert_iterator<Point_coordinate_vector>,
  610. K::FT, bool> result =
  611. CGAL::natural_neighbor_coordinates_2(dt, m,
  612. std::back_inserter(coords));
  613. float r = 0.0f, g = 0.0f, b = 0.0f, norm = 0.000000000000001f;
  614. Point_coordinate_vector::iterator it;
  615. for(it = coords.begin(); it != coords.end(); ++it)
  616. {
  617. float fx, fy, fr, fg, fb, fs;
  618. fx = (*it).first.x() - dw * RANGE_X;
  619. fy = (*it).first.y() - dh * RANGE_Y;
  620. if(fx < 0 || fy < 0
  621. || fx > dw * RANGE_X - 1 || fy > dh * RANGE_Y - 1)
  622. continue;
  623. int index = lookup[(int)(fx * 2)
  624. + dw * RANGE_X * 2 * (int)(fy * 2)];
  625. get_point(index, &fx, &fy, &fr, &fg, &fb, &fs, final);
  626. //float k = pow((*it).second * (1.0 + fs), 1.2);
  627. float k = (*it).second * (1.00f + fs);
  628. //float k = (*it).second * (0.60f + fs);
  629. //float k = pow((*it).second, (1.0f + fs));
  630. // Try to attenuate peak artifacts
  631. //k /= (0.1 * (RANGE_X * RANGE_X + RANGE_Y * RANGE_Y)
  632. // + (myx - fx) * (myx - fx) + (myy - fy) * (myy - fy));
  633. // Cute circles
  634. //k = 1.0 / (0.015 * (RANGE_X * RANGE_X + RANGE_Y * RANGE_Y)
  635. // + (myx - fx) * (myx - fx) + (myy - fy) * (myy - fy));
  636. r += k * fr;
  637. g += k * fg;
  638. b += k * fb;
  639. norm += k;
  640. }
  641. data[4 * (x + y * p->w) + 0] = r / norm;
  642. data[4 * (x + y * p->w) + 1] = g / norm;
  643. data[4 * (x + y * p->w) + 2] = b / norm;
  644. data[4 * (x + y * p->w) + 3] = 0.0;
  645. }
  646. }
  647. pipi_release_pixels(dst, p);
  648. }
  649. static void analyse(pipi_image_t *src)
  650. {
  651. pipi_pixels_t *p = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  652. float *data = (float *)p->pixels;
  653. for(unsigned int dy = 0; dy < dh; dy++)
  654. for(unsigned int dx = 0; dx < dw; dx++)
  655. {
  656. float min = 1.1f, max = -0.1f, mr = 0.0f, mg = 0.0f, mb = 0.0f;
  657. float total = 0.0;
  658. int xmin = 0, xmax = 0, ymin = 0, ymax = 0;
  659. int npixels = 0;
  660. for(unsigned int iy = RANGE_Y * dy; iy < RANGE_Y * (dy + 1); iy++)
  661. for(unsigned int ix = RANGE_X * dx; ix < RANGE_X * (dx + 1); ix++)
  662. {
  663. float lum = 0.0f;
  664. lum += data[4 * (ix + iy * p->w) + 0];
  665. lum += data[4 * (ix + iy * p->w) + 1];
  666. lum += data[4 * (ix + iy * p->w) + 2];
  667. lum /= 3;
  668. mr += data[4 * (ix + iy * p->w) + 0];
  669. mg += data[4 * (ix + iy * p->w) + 1];
  670. mb += data[4 * (ix + iy * p->w) + 2];
  671. if(lum < min)
  672. {
  673. min = lum;
  674. xmin = ix;
  675. ymin = iy;
  676. }
  677. if(lum > max)
  678. {
  679. max = lum;
  680. xmax = ix;
  681. ymax = iy;
  682. }
  683. total += lum;
  684. npixels++;
  685. }
  686. total /= npixels;
  687. mr /= npixels;
  688. mg /= npixels;
  689. mb /= npixels;
  690. float wmin, wmax;
  691. if(total < min + (max - min) / 4)
  692. wmin = 1.0, wmax = 0.0;
  693. else if(total < min + (max - min) / 4 * 3)
  694. wmin = 0.0, wmax = 0.0;
  695. else
  696. wmin = 0.0, wmax = 1.0;
  697. #if RANDOM_START == 1
  698. for(int i = 0; i < POINTS_PER_CELL; i++)
  699. add_random_point();
  700. #else
  701. /* 0.80 and 0.20 were chosen empirically, it gives a 10% better
  702. * initial distance. Definitely worth it. */
  703. if(POINTS_PER_CELL == 2 || total < min + (max - min) / 2)
  704. add_point(xmin, ymin,
  705. data[4 * (xmin + ymin * p->w) + 0] * 0.80 + mr * 0.20,
  706. data[4 * (xmin + ymin * p->w) + 1] * 0.80 + mg * 0.20,
  707. data[4 * (xmin + ymin * p->w) + 2] * 0.80 + mb * 0.20,
  708. wmin);
  709. if(POINTS_PER_CELL == 2 || total >= min + (max - min) / 2)
  710. add_point(xmax, ymax,
  711. data[4 * (xmax + ymax * p->w) + 0] * 0.80 + mr * 0.20,
  712. data[4 * (xmax + ymax * p->w) + 1] * 0.80 + mg * 0.20,
  713. data[4 * (xmax + ymax * p->w) + 2] * 0.80 + mb * 0.20,
  714. wmax);
  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 > 200)
  776. ITERATIONS_PER_POINT = 200;
  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 - 20) (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. /* The first thing we pop from the stream is the version information */
  878. version = b.pop(RANGE_V);
  879. if(version > 1)
  880. {
  881. fprintf(stderr, "Error: unsupported algorithm version %i\n",
  882. version);
  883. return EXIT_FAILURE;
  884. }
  885. /* Read width and height from bitstream */
  886. width = b.pop(RANGE_W) + 1;
  887. height = b.pop(RANGE_H) + 1;
  888. src = NULL;
  889. }
  890. else
  891. {
  892. /* Argument given: open image for encoding */
  893. src = pipi_load(srcname);
  894. if(!src)
  895. {
  896. fprintf(stderr, "Error loading %s\n", srcname);
  897. return EXIT_FAILURE;
  898. }
  899. version = 1;
  900. width = pipi_get_image_width(src);
  901. height = pipi_get_image_height(src);
  902. }
  903. if(width <= 0 || height <= 0 || width > RANGE_W || height > RANGE_H)
  904. {
  905. fprintf(stderr, "Error: image size %ix%i is out of bounds\n",
  906. width, height);
  907. return EXIT_FAILURE;
  908. }
  909. compute_ranges(width, height);
  910. /* Try to cram some more information into our points as long as it
  911. * does not change the cell distribution. This cannot be too clever,
  912. * because we want the computation to depend only on the source image
  913. * coordinates. */
  914. #define TRY(op, revert) \
  915. do { \
  916. unsigned int olddw = dw, olddh = dh; \
  917. op; compute_ranges(width, height); \
  918. if(dw != olddw || dh != olddh) \
  919. { revert; compute_ranges(width, height); } \
  920. } while(0)
  921. for(int i = 0; i < 2; i++)
  922. {
  923. TRY(RANGE_G++, RANGE_G--);
  924. TRY(RANGE_R++, RANGE_R--);
  925. TRY(RANGE_B++, RANGE_B--);
  926. }
  927. for(int i = 0; i < 10; i++)
  928. {
  929. if((float)width / dw >= (float)height / dh)
  930. {
  931. TRY(RANGE_X++, RANGE_X--);
  932. TRY(RANGE_Y++, RANGE_Y--);
  933. }
  934. else
  935. {
  936. TRY(RANGE_Y++, RANGE_Y--);
  937. TRY(RANGE_X++, RANGE_X--);
  938. }
  939. }
  940. /* Print debug information */
  941. if(DEBUG_MODE)
  942. {
  943. fprintf(stderr, "Message size: %i\n", MAX_MSG_LEN);
  944. fprintf(stderr, "Available characters: %i\n", NUM_CHARACTERS);
  945. fprintf(stderr, "Available bits: %f\n", TOTAL_BITS);
  946. fprintf(stderr, "Width/Height ranges: %ix%i\n", RANGE_W, RANGE_H);
  947. fprintf(stderr, "Algorithm version: %i\n", RANGE_V);
  948. fprintf(stderr, "Image resolution: %ix%i\n", width, height);
  949. fprintf(stderr, "Header bits: %f\n", HEADER_BITS);
  950. fprintf(stderr, "Bits available for data: %f\n", DATA_BITS);
  951. fprintf(stderr, "X/Y/Red/Green/Blue/Extra ranges: %i %i %i %i %i %i\n",
  952. RANGE_X, RANGE_Y, RANGE_R, RANGE_G, RANGE_B, RANGE_S);
  953. fprintf(stderr, "Cell bits: %f\n", CELL_BITS);
  954. fprintf(stderr, "Available cells: %i\n", TOTAL_CELLS);
  955. fprintf(stderr, "Wasted bits: %f\n",
  956. DATA_BITS - CELL_BITS * TOTAL_CELLS);
  957. fprintf(stderr, "Chosen image ratio: %i:%i (wasting %i point cells)\n",
  958. dw, dh, TOTAL_CELLS - dw * dh);
  959. fprintf(stderr, "Total wasted bits: %f\n",
  960. DATA_BITS - CELL_BITS * dw * dh);
  961. }
  962. if(srcname)
  963. {
  964. /* Resize and filter image to better state */
  965. tmp = pipi_gaussian_blur(src, 0.25 * dw * RANGE_X / width);
  966. pipi_free(src);
  967. src = pipi_resize(tmp, dw * RANGE_X, dh * RANGE_Y);
  968. pipi_free(tmp);
  969. /* Analyse image */
  970. analyse(src);
  971. /* Render what we just computed */
  972. tmp = pipi_new(dw * RANGE_X, dh * RANGE_Y);
  973. render(tmp, 0, 0, dw * RANGE_X, dh * RANGE_Y, false);
  974. error = pipi_measure_rmsd(src, tmp);
  975. if(DEBUG_MODE)
  976. fprintf(stderr, "Initial distance: %2.10g\n", error);
  977. memset(opstats, 0, sizeof(opstats));
  978. for(int iter = 0, stuck = 0, failures = 0, success = 0;
  979. iter < MAX_ITERATIONS /* && stuck < 5 && */;
  980. iter++)
  981. {
  982. if(failures > 500)
  983. {
  984. stuck++;
  985. failures = 0;
  986. }
  987. if(!DEBUG_MODE && !(iter % 16))
  988. fprintf(stderr, "\rEncoding... %i%%",
  989. iter * 100 / MAX_ITERATIONS);
  990. pipi_image_t *scrap = pipi_copy(tmp);
  991. /* Choose a point at random */
  992. int pt = det_rand(npoints);
  993. point_t oldpt = points[pt];
  994. /* Compute the affected image zone */
  995. float fx, fy, fr, fg, fb, fs;
  996. get_point(pt, &fx, &fy, &fr, &fg, &fb, &fs);
  997. int zonex = (int)fx / RANGE_X - 2;
  998. int zoney = (int)fy / RANGE_Y - 2;
  999. int zonew = 4;
  1000. int zoneh = 4;
  1001. if(zonex < 0) { zonew += zonex; zonex = 0; }
  1002. if(zoney < 0) { zoneh += zoney; zoney = 0;; }
  1003. if(zonex + zonew > (int)dw) { zonew = dw - zonex; }
  1004. if(zoney + zoneh > (int)dh) { zoneh = dh - zoney; }
  1005. /* Choose random operations and measure their effect */
  1006. uint8_t op1 = rand_op();
  1007. //uint8_t op2 = rand_op();
  1008. apply_op(op1, &points[pt]);
  1009. /* Check that two points don't fall at the same place */
  1010. if(POINTS_PER_CELL == 2)
  1011. {
  1012. while(points[pt].x == points[pt ^ 1].x
  1013. && points[pt].y == points[pt ^ 1].y)
  1014. {
  1015. points[pt] = oldpt;
  1016. op1 = rand_op();
  1017. apply_op(op1, &points[pt]);
  1018. }
  1019. }
  1020. render(scrap, zonex * RANGE_X, zoney * RANGE_Y,
  1021. zonew * RANGE_X, zoneh * RANGE_Y, false);
  1022. double newerr = pipi_measure_rmsd(src, scrap);
  1023. opstats[op1 * 2]++;
  1024. //opstats[op2 * 2]++;
  1025. if(newerr < error)
  1026. {
  1027. pipi_free(tmp);
  1028. #if 0
  1029. /* Save image! */
  1030. if((success % 10) == 0)
  1031. {
  1032. char buf[128];
  1033. sprintf(buf, "twit%08i.bmp", success);
  1034. tmp = pipi_new(width, height);
  1035. render(tmp, 0, 0, width, height, true);
  1036. pipi_save(tmp, buf);
  1037. pipi_free(tmp);
  1038. }
  1039. #endif
  1040. tmp = scrap;
  1041. if(DEBUG_MODE)
  1042. fprintf(stderr, "%08i -0.%010i %2.010g after op%i(%i)\n",
  1043. iter, (int)((error - newerr) * 10000000000L),
  1044. error, op1, pt);
  1045. error = newerr;
  1046. opstats[op1 * 2 + 1]++;
  1047. //opstats[op2 * 2 + 1]++;
  1048. failures = 0;
  1049. success++;
  1050. }
  1051. else
  1052. {
  1053. pipi_free(scrap);
  1054. points[pt] = oldpt;
  1055. failures++;
  1056. }
  1057. }
  1058. if(DEBUG_MODE)
  1059. {
  1060. for(int j = 0; j < 2; j++)
  1061. {
  1062. fprintf(stderr, "operation: ");
  1063. for(int i = NB_OPS / 2 * j; i < NB_OPS / 2 * (j + 1); i++)
  1064. fprintf(stderr, "%4i ", i);
  1065. fprintf(stderr, "\nattempts: ");
  1066. for(int i = NB_OPS / 2 * j; i < NB_OPS / 2 * (j + 1); i++)
  1067. fprintf(stderr, "%4i ", opstats[i * 2]);
  1068. fprintf(stderr, "\nsuccesses: ");
  1069. for(int i = NB_OPS / 2 * j; i < NB_OPS / 2 * (j + 1); i++)
  1070. fprintf(stderr, "%4i ", opstats[i * 2 + 1]);
  1071. fprintf(stderr, "\n");
  1072. }
  1073. fprintf(stderr, "Distance: %2.10g\n", error);
  1074. }
  1075. else
  1076. fprintf(stderr, "\r \r");
  1077. #if 0
  1078. dst = pipi_resize(tmp, width, height);
  1079. pipi_free(tmp);
  1080. /* Save image and bail out */
  1081. pipi_save(dst, "lol.bmp");
  1082. pipi_free(dst);
  1083. #endif
  1084. /* Push our points to the bitstream */
  1085. for(int i = 0; i < npoints; i += POINTS_PER_CELL)
  1086. {
  1087. if(POINTS_PER_CELL == 2)
  1088. {
  1089. int x1, y1, x2, y2;
  1090. x1 = points[i].x;
  1091. y1 = points[i].y;
  1092. x2 = points[i + 1].x;
  1093. y2 = points[i + 1].y;
  1094. bool swap;
  1095. uint32_t pack = pack_coords(x1, y1, x2, y2, &swap);
  1096. b.push(points[i + (swap ? 1 : 0)].s, RANGE_S);
  1097. b.push(points[i + (swap ? 1 : 0)].b, RANGE_B);
  1098. b.push(points[i + (swap ? 1 : 0)].g, RANGE_G);
  1099. b.push(points[i + (swap ? 1 : 0)].r, RANGE_R);
  1100. b.push(points[i + (swap ? 0 : 1)].s, RANGE_S);
  1101. b.push(points[i + (swap ? 0 : 1)].b, RANGE_B);
  1102. b.push(points[i + (swap ? 0 : 1)].g, RANGE_G);
  1103. b.push(points[i + (swap ? 0 : 1)].r, RANGE_R);
  1104. b.push(pack, RANGE_XY2);
  1105. }
  1106. else
  1107. {
  1108. b.push(points[i].s, RANGE_S);
  1109. b.push(points[i].b, RANGE_B);
  1110. b.push(points[i].g, RANGE_G);
  1111. b.push(points[i].r, RANGE_R);
  1112. b.push(points[i].x, RANGE_X);
  1113. b.push(points[i].y, RANGE_Y);
  1114. }
  1115. }
  1116. b.push(height - 1, RANGE_H);
  1117. b.push(width - 1, RANGE_W);
  1118. b.push(version, RANGE_V);
  1119. /* Pop Unicode characters from the bitstream and print them */
  1120. for(int i = 0; i < MAX_MSG_LEN; i++)
  1121. fwrite_utf8(stdout, index2uni(b.pop(NUM_CHARACTERS)));
  1122. fprintf(stdout, "\n");
  1123. }
  1124. else
  1125. {
  1126. /* Pop points from the bitstream */
  1127. for(int i = dw * dh; i--; )
  1128. {
  1129. if(POINTS_PER_CELL == 2)
  1130. {
  1131. uint32_t pack = b.pop(RANGE_XY2);
  1132. int x1, y1, x2, y2;
  1133. unpack_coords(pack, &x1, &y1, &x2, &y2);
  1134. points[i * 2 + 1].y = y2;
  1135. points[i * 2 + 1].x = x2;
  1136. points[i * 2 + 1].r = b.pop(RANGE_R);
  1137. points[i * 2 + 1].g = b.pop(RANGE_G);
  1138. points[i * 2 + 1].b = b.pop(RANGE_B);
  1139. points[i * 2 + 1].s = b.pop(RANGE_S);
  1140. points[i * 2].y = y1;
  1141. points[i * 2].x = x1;
  1142. points[i * 2].r = b.pop(RANGE_R);
  1143. points[i * 2].g = b.pop(RANGE_G);
  1144. points[i * 2].b = b.pop(RANGE_B);
  1145. points[i * 2].s = b.pop(RANGE_S);
  1146. }
  1147. else
  1148. {
  1149. points[i].y = b.pop(RANGE_Y);
  1150. points[i].x = b.pop(RANGE_X);
  1151. points[i].r = b.pop(RANGE_R);
  1152. points[i].g = b.pop(RANGE_G);
  1153. points[i].b = b.pop(RANGE_B);
  1154. points[i].s = b.pop(RANGE_S);
  1155. }
  1156. }
  1157. npoints = dw * dh * POINTS_PER_CELL;
  1158. /* Render these points to a new image */
  1159. dst = pipi_new(width, height);
  1160. render(dst, 0, 0, width, height, true);
  1161. /* Save image and bail out */
  1162. pipi_save(dst, dstname);
  1163. pipi_free(dst);
  1164. }
  1165. return EXIT_SUCCESS;
  1166. }