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