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