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