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 /= (0.1 * (RANGE_X * RANGE_X + RANGE_Y * RANGE_Y) |
---|
753 | // + (myx - fx) * (myx - fx) + (myy - fy) * (myy - fy)); |
---|
754 | |
---|
755 | // Cute circles |
---|
756 | //k = 1.0 / (0.015 * (RANGE_X * RANGE_X + RANGE_Y * RANGE_Y) |
---|
757 | // + (myx - fx) * (myx - fx) + (myy - fy) * (myy - fy)); |
---|
758 | |
---|
759 | r += k * fr; |
---|
760 | g += k * fg; |
---|
761 | b += k * fb; |
---|
762 | norm += k; |
---|
763 | } |
---|
764 | |
---|
765 | data[4 * (x + y * p->w) + 0] = r / norm; |
---|
766 | data[4 * (x + y * p->w) + 1] = g / norm; |
---|
767 | data[4 * (x + y * p->w) + 2] = b / norm; |
---|
768 | data[4 * (x + y * p->w) + 3] = 0.0; |
---|
769 | } |
---|
770 | } |
---|
771 | |
---|
772 | pipi_release_pixels(dst, p); |
---|
773 | } |
---|
774 | |
---|
775 | static void analyse(pipi_image_t *src) |
---|
776 | { |
---|
777 | pipi_pixels_t *p = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32); |
---|
778 | float *data = (float *)p->pixels; |
---|
779 | |
---|
780 | for(unsigned int dy = 0; dy < dh; dy++) |
---|
781 | for(unsigned int dx = 0; dx < dw; dx++) |
---|
782 | { |
---|
783 | float min = 1.1f, max = -0.1f, mr = 0.0f, mg = 0.0f, mb = 0.0f; |
---|
784 | float total = 0.0; |
---|
785 | int xmin = 0, xmax = 0, ymin = 0, ymax = 0; |
---|
786 | int npixels = 0; |
---|
787 | |
---|
788 | for(unsigned int iy = RANGE_Y * dy; iy < RANGE_Y * (dy + 1); iy++) |
---|
789 | for(unsigned int ix = RANGE_X * dx; ix < RANGE_X * (dx + 1); ix++) |
---|
790 | { |
---|
791 | float lum = 0.0f; |
---|
792 | |
---|
793 | lum += data[4 * (ix + iy * p->w) + 0]; |
---|
794 | lum += data[4 * (ix + iy * p->w) + 1]; |
---|
795 | lum += data[4 * (ix + iy * p->w) + 2]; |
---|
796 | lum /= 3; |
---|
797 | |
---|
798 | mr += data[4 * (ix + iy * p->w) + 0]; |
---|
799 | mg += data[4 * (ix + iy * p->w) + 1]; |
---|
800 | mb += data[4 * (ix + iy * p->w) + 2]; |
---|
801 | |
---|
802 | if(lum < min) |
---|
803 | { |
---|
804 | min = lum; |
---|
805 | xmin = ix; |
---|
806 | ymin = iy; |
---|
807 | } |
---|
808 | |
---|
809 | if(lum > max) |
---|
810 | { |
---|
811 | max = lum; |
---|
812 | xmax = ix; |
---|
813 | ymax = iy; |
---|
814 | } |
---|
815 | |
---|
816 | total += lum; |
---|
817 | npixels++; |
---|
818 | } |
---|
819 | |
---|
820 | total /= npixels; |
---|
821 | mr /= npixels; |
---|
822 | mg /= npixels; |
---|
823 | mb /= npixels; |
---|
824 | |
---|
825 | float wmin, wmax; |
---|
826 | |
---|
827 | if(total < min + (max - min) / 4) |
---|
828 | wmin = 1.0, wmax = 0.0; |
---|
829 | else if(total < min + (max - min) / 4 * 3) |
---|
830 | wmin = 0.0, wmax = 0.0; |
---|
831 | else |
---|
832 | wmin = 0.0, wmax = 1.0; |
---|
833 | |
---|
834 | #if RANDOM_START == 1 |
---|
835 | for(int i = 0; i < POINTS_PER_CELL; i++) |
---|
836 | add_random_point(); |
---|
837 | #else |
---|
838 | /* 0.80 and 0.20 were chosen empirically, it gives a 10% better |
---|
839 | * initial distance. Definitely worth it. */ |
---|
840 | if(POINTS_PER_CELL == 2 || total < min + (max - min) / 2) |
---|
841 | add_point(xmin, ymin, |
---|
842 | data[4 * (xmin + ymin * p->w) + 0] * 0.80 + mr * 0.20, |
---|
843 | data[4 * (xmin + ymin * p->w) + 1] * 0.80 + mg * 0.20, |
---|
844 | data[4 * (xmin + ymin * p->w) + 2] * 0.80 + mb * 0.20, |
---|
845 | wmin); |
---|
846 | |
---|
847 | if(POINTS_PER_CELL == 2 || total >= min + (max - min) / 2) |
---|
848 | add_point(xmax, ymax, |
---|
849 | data[4 * (xmax + ymax * p->w) + 0] * 0.80 + mr * 0.20, |
---|
850 | data[4 * (xmax + ymax * p->w) + 1] * 0.80 + mg * 0.20, |
---|
851 | data[4 * (xmax + ymax * p->w) + 2] * 0.80 + mb * 0.20, |
---|
852 | wmax); |
---|
853 | #endif |
---|
854 | } |
---|
855 | } |
---|
856 | |
---|
857 | #define MOREINFO "Try `%s --help' for more information.\n" |
---|
858 | |
---|
859 | int main(int argc, char *argv[]) |
---|
860 | { |
---|
861 | uint32_t unicode_data[2048]; |
---|
862 | int opstats[2 * NB_OPS]; |
---|
863 | char const *srcname = NULL, *dstname = NULL; |
---|
864 | pipi_image_t *src, *tmp, *dst; |
---|
865 | double error = 1.0; |
---|
866 | int width, height; |
---|
867 | |
---|
868 | /* Parse command-line options */ |
---|
869 | for(;;) |
---|
870 | { |
---|
871 | int option_index = 0; |
---|
872 | static struct myoption long_options[] = |
---|
873 | { |
---|
874 | { "output", 1, NULL, 'o' }, |
---|
875 | { "length", 1, NULL, 'l' }, |
---|
876 | { "charset", 1, NULL, 'c' }, |
---|
877 | { "quality", 1, NULL, 'q' }, |
---|
878 | { "debug", 0, NULL, 'd' }, |
---|
879 | { "help", 0, NULL, 'h' }, |
---|
880 | { NULL, 0, NULL, 0 }, |
---|
881 | }; |
---|
882 | int c = mygetopt(argc, argv, "o:l:c:q:dh", long_options, &option_index); |
---|
883 | |
---|
884 | if(c == -1) |
---|
885 | break; |
---|
886 | |
---|
887 | switch(c) |
---|
888 | { |
---|
889 | case 'o': |
---|
890 | dstname = myoptarg; |
---|
891 | break; |
---|
892 | case 'l': |
---|
893 | MAX_MSG_LEN = atoi(myoptarg); |
---|
894 | if(MAX_MSG_LEN < 16) |
---|
895 | { |
---|
896 | fprintf(stderr, "Warning: rounding minimum message length to 16\n"); |
---|
897 | MAX_MSG_LEN = 16; |
---|
898 | } |
---|
899 | break; |
---|
900 | case 'c': |
---|
901 | if(!strcmp(myoptarg, "ascii")) |
---|
902 | unichars = unichars_ascii; |
---|
903 | else if(!strcmp(myoptarg, "cjk")) |
---|
904 | unichars = unichars_cjk; |
---|
905 | else if(!strcmp(myoptarg, "symbols")) |
---|
906 | unichars = unichars_symbols; |
---|
907 | else |
---|
908 | { |
---|
909 | fprintf(stderr, "Error: invalid char block \"%s\".", myoptarg); |
---|
910 | fprintf(stderr, "Valid sets are: ascii, cjk, symbols\n"); |
---|
911 | return EXIT_FAILURE; |
---|
912 | } |
---|
913 | break; |
---|
914 | case 'q': |
---|
915 | ITERATIONS_PER_POINT = 10 * atof(myoptarg); |
---|
916 | if(ITERATIONS_PER_POINT < 0) |
---|
917 | ITERATIONS_PER_POINT = 0; |
---|
918 | else if(ITERATIONS_PER_POINT > 200) |
---|
919 | ITERATIONS_PER_POINT = 200; |
---|
920 | break; |
---|
921 | case 'd': |
---|
922 | DEBUG_MODE = true; |
---|
923 | break; |
---|
924 | case 'h': |
---|
925 | printf("Usage: img2twit [OPTIONS] SOURCE\n"); |
---|
926 | printf(" img2twit [OPTIONS] -o DESTINATION\n"); |
---|
927 | printf("Encode SOURCE image to stdout or decode stdin to DESTINATION.\n"); |
---|
928 | printf("\n"); |
---|
929 | printf("Mandatory arguments to long options are mandatory for short options too.\n"); |
---|
930 | printf(" -o, --output <filename> output resulting image to filename\n"); |
---|
931 | printf(" -l, --length <size> message length in characters (default 140)\n"); |
---|
932 | printf(" -c, --charset <block> character set to use (ascii, [cjk], symbols)\n"); |
---|
933 | printf(" -q, --quality <rate> set image quality (0 - 20) (default 5)\n"); |
---|
934 | printf(" -d, --debug print debug information\n"); |
---|
935 | printf(" -h, --help display this help and exit\n"); |
---|
936 | printf("\n"); |
---|
937 | printf("Written by Sam Hocevar. Report bugs to <sam@hocevar.net>.\n"); |
---|
938 | return EXIT_SUCCESS; |
---|
939 | default: |
---|
940 | fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c); |
---|
941 | printf(MOREINFO, argv[0]); |
---|
942 | return EXIT_FAILURE; |
---|
943 | } |
---|
944 | } |
---|
945 | |
---|
946 | if(myoptind == argc && !dstname) |
---|
947 | { |
---|
948 | fprintf(stderr, "%s: too few arguments\n", argv[0]); |
---|
949 | printf(MOREINFO, argv[0]); |
---|
950 | return EXIT_FAILURE; |
---|
951 | } |
---|
952 | |
---|
953 | if((myoptind == argc - 1 && dstname) || myoptind < argc - 1) |
---|
954 | { |
---|
955 | fprintf(stderr, "%s: too many arguments\n", argv[0]); |
---|
956 | printf(MOREINFO, argv[0]); |
---|
957 | return EXIT_FAILURE; |
---|
958 | } |
---|
959 | |
---|
960 | if(myoptind == argc - 1) |
---|
961 | srcname = argv[myoptind]; |
---|
962 | |
---|
963 | /* Decoding mode: read UTF-8 text from stdin */ |
---|
964 | if(dstname) |
---|
965 | for(MAX_MSG_LEN = 0; ;) |
---|
966 | { |
---|
967 | uint32_t ch = fread_utf8(stdin); |
---|
968 | if(ch == 0xffffffff || ch == '\n') |
---|
969 | break; |
---|
970 | if(ch <= ' ') |
---|
971 | continue; |
---|
972 | unicode_data[MAX_MSG_LEN++] = ch; |
---|
973 | |
---|
974 | if(MAX_MSG_LEN >= 2048) |
---|
975 | { |
---|
976 | fprintf(stderr, "Error: message too long.\n"); |
---|
977 | return EXIT_FAILURE; |
---|
978 | } |
---|
979 | } |
---|
980 | |
---|
981 | if(MAX_MSG_LEN == 0) |
---|
982 | { |
---|
983 | fprintf(stderr, "Error: empty message.\n"); |
---|
984 | return EXIT_FAILURE; |
---|
985 | } |
---|
986 | |
---|
987 | bitstack b(MAX_MSG_LEN); /* We cannot declare this before, because |
---|
988 | * MAX_MSG_LEN wouldn't be defined. */ |
---|
989 | |
---|
990 | /* Autodetect charset if decoding, otherwise switch to CJK. */ |
---|
991 | if(dstname) |
---|
992 | { |
---|
993 | char const *charset; |
---|
994 | |
---|
995 | if(unicode_data[0] >= 0x0021 && unicode_data[0] < 0x007f) |
---|
996 | { |
---|
997 | unichars = unichars_ascii; |
---|
998 | charset = "ascii"; |
---|
999 | } |
---|
1000 | else if(unicode_data[0] >= 0x4e00 && unicode_data[0] < 0x9fa6) |
---|
1001 | { |
---|
1002 | unichars = unichars_cjk; |
---|
1003 | charset = "cjk"; |
---|
1004 | } |
---|
1005 | else if(unicode_data[0] >= 0x25a0 && unicode_data[0] < 0x27bf) |
---|
1006 | { |
---|
1007 | unichars = unichars_symbols; |
---|
1008 | charset = "symbols"; |
---|
1009 | } |
---|
1010 | else |
---|
1011 | { |
---|
1012 | fprintf(stderr, "Error: unable to detect charset\n"); |
---|
1013 | return EXIT_FAILURE; |
---|
1014 | } |
---|
1015 | |
---|
1016 | if(DEBUG_MODE) |
---|
1017 | fprintf(stderr, "Detected charset \"%s\"\n", charset); |
---|
1018 | } |
---|
1019 | else if(!unichars) |
---|
1020 | unichars = unichars_cjk; |
---|
1021 | |
---|
1022 | pipi_set_gamma(1.0); |
---|
1023 | |
---|
1024 | /* Precompute bit allocation */ |
---|
1025 | NUM_CHARACTERS = count_unichars(); |
---|
1026 | |
---|
1027 | if(dstname) |
---|
1028 | { |
---|
1029 | /* Decoding mode: find each character's index in our character |
---|
1030 | * list, and push it to our wonderful custom bitstream. */ |
---|
1031 | for(int i = MAX_MSG_LEN; i--; ) |
---|
1032 | b.push(uni2index(unicode_data[i]), NUM_CHARACTERS); |
---|
1033 | |
---|
1034 | /* The first thing we pop from the stream is the version information */ |
---|
1035 | version = b.pop(RANGE_V); |
---|
1036 | |
---|
1037 | if(version > 1) |
---|
1038 | { |
---|
1039 | fprintf(stderr, "Error: unsupported algorithm version %i\n", |
---|
1040 | version); |
---|
1041 | return EXIT_FAILURE; |
---|
1042 | } |
---|
1043 | |
---|
1044 | /* Read width and height from bitstream */ |
---|
1045 | width = b.pop(RANGE_W) + 1; |
---|
1046 | height = b.pop(RANGE_H) + 1; |
---|
1047 | src = NULL; |
---|
1048 | } |
---|
1049 | else |
---|
1050 | { |
---|
1051 | /* Argument given: open image for encoding */ |
---|
1052 | src = pipi_load(srcname); |
---|
1053 | |
---|
1054 | if(!src) |
---|
1055 | { |
---|
1056 | fprintf(stderr, "Error loading %s\n", srcname); |
---|
1057 | return EXIT_FAILURE; |
---|
1058 | } |
---|
1059 | |
---|
1060 | version = 1; |
---|
1061 | width = pipi_get_image_width(src); |
---|
1062 | height = pipi_get_image_height(src); |
---|
1063 | } |
---|
1064 | |
---|
1065 | if(width <= 0 || height <= 0 || width > RANGE_W || height > RANGE_H) |
---|
1066 | { |
---|
1067 | fprintf(stderr, "Error: image size %ix%i is out of bounds\n", |
---|
1068 | width, height); |
---|
1069 | return EXIT_FAILURE; |
---|
1070 | } |
---|
1071 | |
---|
1072 | compute_ranges(width, height); |
---|
1073 | |
---|
1074 | /* Try to cram some more information into our points as long as it |
---|
1075 | * does not change the cell distribution. This cannot be too clever, |
---|
1076 | * because we want the computation to depend only on the source image |
---|
1077 | * coordinates. */ |
---|
1078 | #define TRY(op, revert) \ |
---|
1079 | do { \ |
---|
1080 | unsigned int olddw = dw, olddh = dh; \ |
---|
1081 | op; compute_ranges(width, height); \ |
---|
1082 | if(dw != olddw || dh != olddh) \ |
---|
1083 | { revert; compute_ranges(width, height); } \ |
---|
1084 | } while(0) |
---|
1085 | |
---|
1086 | for(int i = 0; i < 2; i++) |
---|
1087 | { |
---|
1088 | TRY(RANGE_G++, RANGE_G--); |
---|
1089 | TRY(RANGE_R++, RANGE_R--); |
---|
1090 | TRY(RANGE_B++, RANGE_B--); |
---|
1091 | } |
---|
1092 | |
---|
1093 | for(int i = 0; i < 10; i++) |
---|
1094 | { |
---|
1095 | if((float)width / dw >= (float)height / dh) |
---|
1096 | { |
---|
1097 | TRY(RANGE_X++, RANGE_X--); |
---|
1098 | TRY(RANGE_Y++, RANGE_Y--); |
---|
1099 | } |
---|
1100 | else |
---|
1101 | { |
---|
1102 | TRY(RANGE_Y++, RANGE_Y--); |
---|
1103 | TRY(RANGE_X++, RANGE_X--); |
---|
1104 | } |
---|
1105 | } |
---|
1106 | |
---|
1107 | /* Print debug information */ |
---|
1108 | if(DEBUG_MODE) |
---|
1109 | { |
---|
1110 | fprintf(stderr, "Message size: %i\n", MAX_MSG_LEN); |
---|
1111 | fprintf(stderr, "Available characters: %i\n", NUM_CHARACTERS); |
---|
1112 | fprintf(stderr, "Available bits: %f\n", TOTAL_BITS); |
---|
1113 | fprintf(stderr, "Width/Height ranges: %ix%i\n", RANGE_W, RANGE_H); |
---|
1114 | fprintf(stderr, "Algorithm version: %i\n", RANGE_V); |
---|
1115 | fprintf(stderr, "Image resolution: %ix%i\n", width, height); |
---|
1116 | fprintf(stderr, "Header bits: %f\n", HEADER_BITS); |
---|
1117 | fprintf(stderr, "Bits available for data: %f\n", DATA_BITS); |
---|
1118 | fprintf(stderr, "X/Y/Red/Green/Blue/Extra ranges: %i %i %i %i %i %i\n", |
---|
1119 | RANGE_X, RANGE_Y, RANGE_R, RANGE_G, RANGE_B, RANGE_S); |
---|
1120 | fprintf(stderr, "Cell bits: %f\n", CELL_BITS); |
---|
1121 | fprintf(stderr, "Available cells: %i\n", TOTAL_CELLS); |
---|
1122 | fprintf(stderr, "Wasted bits: %f\n", |
---|
1123 | DATA_BITS - CELL_BITS * TOTAL_CELLS); |
---|
1124 | fprintf(stderr, "Chosen image ratio: %i:%i (wasting %i point cells)\n", |
---|
1125 | dw, dh, TOTAL_CELLS - dw * dh); |
---|
1126 | fprintf(stderr, "Total wasted bits: %f\n", |
---|
1127 | DATA_BITS - CELL_BITS * dw * dh); |
---|
1128 | } |
---|
1129 | |
---|
1130 | if(srcname) |
---|
1131 | { |
---|
1132 | /* Resize and filter image to better state */ |
---|
1133 | tmp = pipi_gaussian_blur(src, 0.25 * dw * RANGE_X / width); |
---|
1134 | pipi_free(src); |
---|
1135 | src = pipi_resize(tmp, dw * RANGE_X, dh * RANGE_Y); |
---|
1136 | pipi_free(tmp); |
---|
1137 | |
---|
1138 | /* Analyse image */ |
---|
1139 | analyse(src); |
---|
1140 | |
---|
1141 | /* Render what we just computed */ |
---|
1142 | tmp = pipi_new(dw * RANGE_X, dh * RANGE_Y); |
---|
1143 | render(tmp, 0, 0, dw * RANGE_X, dh * RANGE_Y); |
---|
1144 | error = pipi_measure_rmsd(src, tmp); |
---|
1145 | |
---|
1146 | if(DEBUG_MODE) |
---|
1147 | fprintf(stderr, "Initial distance: %2.10g\n", error); |
---|
1148 | |
---|
1149 | memset(opstats, 0, sizeof(opstats)); |
---|
1150 | for(int iter = 0, stuck = 0, failures = 0, success = 0; |
---|
1151 | iter < MAX_ITERATIONS /* && stuck < 5 && */; |
---|
1152 | iter++) |
---|
1153 | { |
---|
1154 | if(failures > 500) |
---|
1155 | { |
---|
1156 | stuck++; |
---|
1157 | failures = 0; |
---|
1158 | } |
---|
1159 | |
---|
1160 | if(!DEBUG_MODE && !(iter % 16)) |
---|
1161 | fprintf(stderr, "\rEncoding... %i%%", |
---|
1162 | iter * 100 / MAX_ITERATIONS); |
---|
1163 | |
---|
1164 | pipi_image_t *scrap = pipi_copy(tmp); |
---|
1165 | |
---|
1166 | /* Choose a point at random */ |
---|
1167 | int pt = det_rand(npoints); |
---|
1168 | point_t oldpt = points[pt]; |
---|
1169 | |
---|
1170 | /* Compute the affected image zone */ |
---|
1171 | float fx, fy, fr, fg, fb, fs; |
---|
1172 | get_point(pt, &fx, &fy, &fr, &fg, &fb, &fs); |
---|
1173 | int zonex = (int)fx / RANGE_X - 2; |
---|
1174 | int zoney = (int)fy / RANGE_Y - 2; |
---|
1175 | int zonew = 4; |
---|
1176 | int zoneh = 4; |
---|
1177 | if(zonex < 0) { zonew += zonex; zonex = 0; } |
---|
1178 | if(zoney < 0) { zoneh += zoney; zoney = 0;; } |
---|
1179 | if(zonex + zonew > (int)dw) { zonew = dw - zonex; } |
---|
1180 | if(zoney + zoneh > (int)dh) { zoneh = dh - zoney; } |
---|
1181 | |
---|
1182 | /* Choose random operations and measure their effect */ |
---|
1183 | uint8_t op1 = rand_op(); |
---|
1184 | //uint8_t op2 = rand_op(); |
---|
1185 | |
---|
1186 | apply_op(op1, &points[pt]); |
---|
1187 | |
---|
1188 | /* Check that two points don't fall at the same place */ |
---|
1189 | if(POINTS_PER_CELL == 2) |
---|
1190 | { |
---|
1191 | while(points[pt].x == points[pt ^ 1].x |
---|
1192 | && points[pt].y == points[pt ^ 1].y) |
---|
1193 | { |
---|
1194 | points[pt] = oldpt; |
---|
1195 | op1 = rand_op(); |
---|
1196 | apply_op(op1, &points[pt]); |
---|
1197 | } |
---|
1198 | } |
---|
1199 | |
---|
1200 | render(scrap, zonex * RANGE_X, zoney * RANGE_Y, |
---|
1201 | zonew * RANGE_X, zoneh * RANGE_Y); |
---|
1202 | |
---|
1203 | double newerr = pipi_measure_rmsd(src, scrap); |
---|
1204 | |
---|
1205 | opstats[op1 * 2]++; |
---|
1206 | //opstats[op2 * 2]++; |
---|
1207 | |
---|
1208 | if(newerr < error) |
---|
1209 | { |
---|
1210 | pipi_free(tmp); |
---|
1211 | |
---|
1212 | #if 0 |
---|
1213 | /* Save image! */ |
---|
1214 | if((success % 10) == 0) |
---|
1215 | { |
---|
1216 | char buf[128]; |
---|
1217 | sprintf(buf, "twit%08i.bmp", success); |
---|
1218 | tmp = pipi_new(width, height); |
---|
1219 | render(tmp, 0, 0, width, height); |
---|
1220 | pipi_save(tmp, buf); |
---|
1221 | pipi_free(tmp); |
---|
1222 | } |
---|
1223 | #endif |
---|
1224 | |
---|
1225 | tmp = scrap; |
---|
1226 | |
---|
1227 | if(DEBUG_MODE) |
---|
1228 | fprintf(stderr, "%08i -0.%010i %2.010g after op%i(%i)\n", |
---|
1229 | iter, (int)((error - newerr) * 10000000000L), |
---|
1230 | error, op1, pt); |
---|
1231 | |
---|
1232 | error = newerr; |
---|
1233 | opstats[op1 * 2 + 1]++; |
---|
1234 | //opstats[op2 * 2 + 1]++; |
---|
1235 | failures = 0; |
---|
1236 | success++; |
---|
1237 | } |
---|
1238 | else |
---|
1239 | { |
---|
1240 | pipi_free(scrap); |
---|
1241 | points[pt] = oldpt; |
---|
1242 | failures++; |
---|
1243 | } |
---|
1244 | } |
---|
1245 | |
---|
1246 | if(DEBUG_MODE) |
---|
1247 | { |
---|
1248 | for(int j = 0; j < 2; j++) |
---|
1249 | { |
---|
1250 | fprintf(stderr, "operation: "); |
---|
1251 | for(int i = NB_OPS / 2 * j; i < NB_OPS / 2 * (j + 1); i++) |
---|
1252 | fprintf(stderr, "%4i ", i); |
---|
1253 | fprintf(stderr, "\nattempts: "); |
---|
1254 | for(int i = NB_OPS / 2 * j; i < NB_OPS / 2 * (j + 1); i++) |
---|
1255 | fprintf(stderr, "%4i ", opstats[i * 2]); |
---|
1256 | fprintf(stderr, "\nsuccesses: "); |
---|
1257 | for(int i = NB_OPS / 2 * j; i < NB_OPS / 2 * (j + 1); i++) |
---|
1258 | fprintf(stderr, "%4i ", opstats[i * 2 + 1]); |
---|
1259 | fprintf(stderr, "\n"); |
---|
1260 | } |
---|
1261 | |
---|
1262 | fprintf(stderr, "Distance: %2.10g\n", error); |
---|
1263 | } |
---|
1264 | else |
---|
1265 | fprintf(stderr, "\r \r"); |
---|
1266 | |
---|
1267 | #if 0 |
---|
1268 | dst = pipi_resize(tmp, width, height); |
---|
1269 | pipi_free(tmp); |
---|
1270 | |
---|
1271 | /* Save image and bail out */ |
---|
1272 | pipi_save(dst, "lol.bmp"); |
---|
1273 | pipi_free(dst); |
---|
1274 | #endif |
---|
1275 | |
---|
1276 | /* Push our points to the bitstream */ |
---|
1277 | for(int i = 0; i < npoints; i += POINTS_PER_CELL) |
---|
1278 | { |
---|
1279 | if(POINTS_PER_CELL == 2) |
---|
1280 | { |
---|
1281 | int x1, y1, x2, y2; |
---|
1282 | x1 = points[i].x; |
---|
1283 | y1 = points[i].y; |
---|
1284 | x2 = points[i + 1].x; |
---|
1285 | y2 = points[i + 1].y; |
---|
1286 | |
---|
1287 | bool swap; |
---|
1288 | uint32_t pack = pack_coords(x1, y1, x2, y2, &swap); |
---|
1289 | |
---|
1290 | b.push(points[i + (swap ? 1 : 0)].s, RANGE_S); |
---|
1291 | b.push(points[i + (swap ? 1 : 0)].b, RANGE_B); |
---|
1292 | b.push(points[i + (swap ? 1 : 0)].g, RANGE_G); |
---|
1293 | b.push(points[i + (swap ? 1 : 0)].r, RANGE_R); |
---|
1294 | b.push(points[i + (swap ? 0 : 1)].s, RANGE_S); |
---|
1295 | b.push(points[i + (swap ? 0 : 1)].b, RANGE_B); |
---|
1296 | b.push(points[i + (swap ? 0 : 1)].g, RANGE_G); |
---|
1297 | b.push(points[i + (swap ? 0 : 1)].r, RANGE_R); |
---|
1298 | b.push(pack, RANGE_XY2); |
---|
1299 | } |
---|
1300 | else |
---|
1301 | { |
---|
1302 | b.push(points[i].s, RANGE_S); |
---|
1303 | b.push(points[i].b, RANGE_B); |
---|
1304 | b.push(points[i].g, RANGE_G); |
---|
1305 | b.push(points[i].r, RANGE_R); |
---|
1306 | b.push(points[i].x, RANGE_X); |
---|
1307 | b.push(points[i].y, RANGE_Y); |
---|
1308 | } |
---|
1309 | } |
---|
1310 | b.push(height - 1, RANGE_H); |
---|
1311 | b.push(width - 1, RANGE_W); |
---|
1312 | b.push(version, RANGE_V); |
---|
1313 | |
---|
1314 | /* Pop Unicode characters from the bitstream and print them */ |
---|
1315 | for(int i = 0; i < MAX_MSG_LEN; i++) |
---|
1316 | fwrite_utf8(stdout, index2uni(b.pop(NUM_CHARACTERS))); |
---|
1317 | fprintf(stdout, "\n"); |
---|
1318 | } |
---|
1319 | else |
---|
1320 | { |
---|
1321 | /* Pop points from the bitstream */ |
---|
1322 | for(int i = dw * dh; i--; ) |
---|
1323 | { |
---|
1324 | if(POINTS_PER_CELL == 2) |
---|
1325 | { |
---|
1326 | uint32_t pack = b.pop(RANGE_XY2); |
---|
1327 | int x1, y1, x2, y2; |
---|
1328 | unpack_coords(pack, &x1, &y1, &x2, &y2); |
---|
1329 | |
---|
1330 | points[i * 2 + 1].y = y2; |
---|
1331 | points[i * 2 + 1].x = x2; |
---|
1332 | points[i * 2 + 1].r = b.pop(RANGE_R); |
---|
1333 | points[i * 2 + 1].g = b.pop(RANGE_G); |
---|
1334 | points[i * 2 + 1].b = b.pop(RANGE_B); |
---|
1335 | points[i * 2 + 1].s = b.pop(RANGE_S); |
---|
1336 | points[i * 2].y = y1; |
---|
1337 | points[i * 2].x = x1; |
---|
1338 | points[i * 2].r = b.pop(RANGE_R); |
---|
1339 | points[i * 2].g = b.pop(RANGE_G); |
---|
1340 | points[i * 2].b = b.pop(RANGE_B); |
---|
1341 | points[i * 2].s = b.pop(RANGE_S); |
---|
1342 | } |
---|
1343 | else |
---|
1344 | { |
---|
1345 | points[i].y = b.pop(RANGE_Y); |
---|
1346 | points[i].x = b.pop(RANGE_X); |
---|
1347 | points[i].r = b.pop(RANGE_R); |
---|
1348 | points[i].g = b.pop(RANGE_G); |
---|
1349 | points[i].b = b.pop(RANGE_B); |
---|
1350 | points[i].s = b.pop(RANGE_S); |
---|
1351 | } |
---|
1352 | } |
---|
1353 | npoints = dw * dh * POINTS_PER_CELL; |
---|
1354 | |
---|
1355 | /* Render these points to a new image */ |
---|
1356 | dst = pipi_new(width, height); |
---|
1357 | render(dst, 0, 0, width, height); |
---|
1358 | |
---|
1359 | /* Save image and bail out */ |
---|
1360 | pipi_save(dst, dstname); |
---|
1361 | pipi_free(dst); |
---|
1362 | } |
---|
1363 | |
---|
1364 | return EXIT_SUCCESS; |
---|
1365 | } |
---|
1366 | |
---|