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