10 | 10 |
#include "gifsicle.h"
|
11 | 11 |
#include <assert.h>
|
12 | 12 |
#include <string.h>
|
13 | |
|
14 | |
static inline uint32_t rgb_distance(const Gif_Color* x, const Gif_Color* y) {
|
15 | |
return (x->gfc_red - y->gfc_red) * (x->gfc_red - y->gfc_red)
|
16 | |
+ (x->gfc_green - y->gfc_green) * (x->gfc_green - y->gfc_green)
|
17 | |
+ (x->gfc_blue - y->gfc_blue) * (x->gfc_blue - y->gfc_blue);
|
18 | |
}
|
19 | |
|
20 | |
static inline unsigned kd3_distance(const int x[3], const int y[3]) {
|
21 | |
return (x[0] - y[0]) * (x[0] - y[0])
|
22 | |
+ (x[1] - y[1]) * (x[1] - y[1])
|
23 | |
+ (x[2] - y[2]) * (x[2] - y[2]);
|
24 | |
}
|
25 | |
|
26 | |
static inline void kd3_luminance_transform(int x[3]) {
|
|
13 |
#include <stdlib.h>
|
|
14 |
#include <limits.h>
|
|
15 |
#include <ctype.h>
|
|
16 |
#include <math.h>
|
|
17 |
|
|
18 |
/* kcolor: a 3D vector, each component has 15 bits of precision */
|
|
19 |
/* 15 bits means KC_MAX * KC_MAX always fits within a signed 32-bit
|
|
20 |
integer, and a 3-D squared distance always fits within an unsigned 32-bit
|
|
21 |
integer. */
|
|
22 |
#define KC_MAX 0x7FFF
|
|
23 |
#define KC_WHOLE 0x8000
|
|
24 |
#define KC_HALF 0x4000
|
|
25 |
#define KC_BITS 15
|
|
26 |
typedef struct kcolor {
|
|
27 |
int32_t a[3];
|
|
28 |
} kcolor;
|
|
29 |
|
|
30 |
/* Invariant: (0<=x<256) ==> (srgb_revgamma[srgb_gamma[x] >> 7] <= x). */
|
|
31 |
|
|
32 |
static const uint16_t srgb_gamma_table_256[256] = {
|
|
33 |
0, 10, 20, 30, 40, 50, 60, 70,
|
|
34 |
80, 90, 99, 110, 120, 132, 144, 157,
|
|
35 |
170, 184, 198, 213, 229, 246, 263, 281,
|
|
36 |
299, 319, 338, 359, 380, 403, 425, 449,
|
|
37 |
473, 498, 524, 551, 578, 606, 635, 665,
|
|
38 |
695, 727, 759, 792, 825, 860, 895, 931,
|
|
39 |
968, 1006, 1045, 1085, 1125, 1167, 1209, 1252,
|
|
40 |
1296, 1341, 1386, 1433, 1481, 1529, 1578, 1629,
|
|
41 |
1680, 1732, 1785, 1839, 1894, 1950, 2007, 2065,
|
|
42 |
2123, 2183, 2244, 2305, 2368, 2432, 2496, 2562,
|
|
43 |
2629, 2696, 2765, 2834, 2905, 2977, 3049, 3123,
|
|
44 |
3198, 3273, 3350, 3428, 3507, 3587, 3668, 3750,
|
|
45 |
3833, 3917, 4002, 4088, 4176, 4264, 4354, 4444,
|
|
46 |
4536, 4629, 4723, 4818, 4914, 5011, 5109, 5209,
|
|
47 |
5309, 5411, 5514, 5618, 5723, 5829, 5936, 6045,
|
|
48 |
6154, 6265, 6377, 6490, 6604, 6720, 6836, 6954,
|
|
49 |
7073, 7193, 7315, 7437, 7561, 7686, 7812, 7939,
|
|
50 |
8067, 8197, 8328, 8460, 8593, 8728, 8863, 9000,
|
|
51 |
9139, 9278, 9419, 9560, 9704, 9848, 9994, 10140,
|
|
52 |
10288, 10438, 10588, 10740, 10893, 11048, 11204, 11360,
|
|
53 |
11519, 11678, 11839, 12001, 12164, 12329, 12495, 12662,
|
|
54 |
12831, 13000, 13172, 13344, 13518, 13693, 13869, 14047,
|
|
55 |
14226, 14406, 14588, 14771, 14955, 15141, 15328, 15516,
|
|
56 |
15706, 15897, 16089, 16283, 16478, 16675, 16872, 17071,
|
|
57 |
17272, 17474, 17677, 17882, 18088, 18295, 18504, 18714,
|
|
58 |
18926, 19138, 19353, 19569, 19786, 20004, 20224, 20445,
|
|
59 |
20668, 20892, 21118, 21345, 21573, 21803, 22034, 22267,
|
|
60 |
22501, 22736, 22973, 23211, 23451, 23692, 23935, 24179,
|
|
61 |
24425, 24672, 24920, 25170, 25421, 25674, 25928, 26184,
|
|
62 |
26441, 26700, 26960, 27222, 27485, 27749, 28016, 28283,
|
|
63 |
28552, 28823, 29095, 29368, 29643, 29920, 30197, 30477,
|
|
64 |
30758, 31040, 31324, 31610, 31897, 32185, 32475, 32767
|
|
65 |
};
|
|
66 |
|
|
67 |
static const uint16_t srgb_revgamma_table_256[256] = {
|
|
68 |
0, 1628, 2776, 3619, 4309, 4904, 5434, 5914,
|
|
69 |
6355, 6765, 7150, 7513, 7856, 8184, 8497, 8798,
|
|
70 |
9086, 9365, 9634, 9895, 10147, 10393, 10631, 10864,
|
|
71 |
11091, 11312, 11528, 11739, 11946, 12148, 12347, 12541,
|
|
72 |
12732, 12920, 13104, 13285, 13463, 13639, 13811, 13981,
|
|
73 |
14149, 14314, 14476, 14637, 14795, 14951, 15105, 15257,
|
|
74 |
15408, 15556, 15703, 15848, 15991, 16133, 16273, 16412,
|
|
75 |
16549, 16685, 16819, 16953, 17084, 17215, 17344, 17472,
|
|
76 |
17599, 17725, 17849, 17973, 18095, 18217, 18337, 18457,
|
|
77 |
18575, 18692, 18809, 18925, 19039, 19153, 19266, 19378,
|
|
78 |
19489, 19600, 19710, 19819, 19927, 20034, 20141, 20247,
|
|
79 |
20352, 20457, 20560, 20664, 20766, 20868, 20969, 21070,
|
|
80 |
21170, 21269, 21368, 21466, 21564, 21661, 21758, 21854,
|
|
81 |
21949, 22044, 22138, 22232, 22326, 22418, 22511, 22603,
|
|
82 |
22694, 22785, 22875, 22965, 23055, 23144, 23232, 23321,
|
|
83 |
23408, 23496, 23583, 23669, 23755, 23841, 23926, 24011,
|
|
84 |
24095, 24180, 24263, 24347, 24430, 24512, 24595, 24676,
|
|
85 |
24758, 24839, 24920, 25001, 25081, 25161, 25240, 25319,
|
|
86 |
25398, 25477, 25555, 25633, 25710, 25788, 25865, 25941,
|
|
87 |
26018, 26094, 26170, 26245, 26321, 26396, 26470, 26545,
|
|
88 |
26619, 26693, 26766, 26840, 26913, 26986, 27058, 27130,
|
|
89 |
27202, 27274, 27346, 27417, 27488, 27559, 27630, 27700,
|
|
90 |
27770, 27840, 27910, 27979, 28048, 28117, 28186, 28255,
|
|
91 |
28323, 28391, 28459, 28527, 28594, 28661, 28728, 28795,
|
|
92 |
28862, 28928, 28995, 29061, 29127, 29192, 29258, 29323,
|
|
93 |
29388, 29453, 29518, 29582, 29646, 29711, 29775, 29838,
|
|
94 |
29902, 29965, 30029, 30092, 30155, 30217, 30280, 30342,
|
|
95 |
30404, 30466, 30528, 30590, 30652, 30713, 30774, 30835,
|
|
96 |
30896, 30957, 31017, 31078, 31138, 31198, 31258, 31318,
|
|
97 |
31378, 31437, 31497, 31556, 31615, 31674, 31733, 31791,
|
|
98 |
31850, 31908, 31966, 32024, 32082, 32140, 32198, 32255,
|
|
99 |
32313, 32370, 32427, 32484, 32541, 32598, 32654, 32711
|
|
100 |
};
|
|
101 |
|
|
102 |
static uint16_t* gamma_tables[2] = {
|
|
103 |
(uint16_t*) srgb_gamma_table_256,
|
|
104 |
(uint16_t*) srgb_revgamma_table_256
|
|
105 |
};
|
|
106 |
|
|
107 |
|
|
108 |
static inline void kc_clear(kcolor* x) {
|
|
109 |
x->a[0] = x->a[1] = x->a[2] = 0;
|
|
110 |
}
|
|
111 |
|
|
112 |
static inline void kc_clamp(kcolor* x) {
|
|
113 |
int i;
|
|
114 |
for (i = 0; i < 3; ++i) {
|
|
115 |
if (x->a[i] < 0)
|
|
116 |
x->a[i] = 0;
|
|
117 |
if (x->a[i] > KC_MAX)
|
|
118 |
x->a[i] = KC_MAX;
|
|
119 |
}
|
|
120 |
}
|
|
121 |
|
|
122 |
static inline void kc_set8g(kcolor* x, int a0, int a1, int a2) {
|
|
123 |
x->a[0] = gamma_tables[0][a0];
|
|
124 |
x->a[1] = gamma_tables[0][a1];
|
|
125 |
x->a[2] = gamma_tables[0][a2];
|
|
126 |
}
|
|
127 |
|
|
128 |
static inline void kc_revgamma_transform(kcolor* x) {
|
|
129 |
int d;
|
|
130 |
for (d = 0; d != 3; ++d) {
|
|
131 |
int c = gamma_tables[1][x->a[d] >> 7];
|
|
132 |
while (c < 0x7F80 && x->a[d] >= gamma_tables[0][(c + 0x80) >> 7])
|
|
133 |
c += 0x80;
|
|
134 |
x->a[d] = c;
|
|
135 |
}
|
|
136 |
}
|
|
137 |
|
|
138 |
static const char* __attribute__((used)) kc_debug_str(kcolor x) {
|
|
139 |
static int whichbuf = 0;
|
|
140 |
static char buf[4][8];
|
|
141 |
whichbuf = (whichbuf + 1) % 4;
|
|
142 |
kc_revgamma_transform(&x);
|
|
143 |
sprintf(buf[whichbuf], "#%02X%02X%02X",
|
|
144 |
x.a[0] >> 7, x.a[1] >> 7, x.a[2] >> 7);
|
|
145 |
return buf[whichbuf];
|
|
146 |
}
|
|
147 |
|
|
148 |
void kc_set_gamma(int type, double gamma) {
|
|
149 |
#if HAVE_POW
|
|
150 |
static int cur_type = KC_GAMMA_SRGB;
|
|
151 |
static double cur_gamma = 2.2;
|
|
152 |
int i, j;
|
|
153 |
if (type == cur_type && (type != KC_GAMMA_NUMERIC || gamma == cur_gamma))
|
|
154 |
return;
|
|
155 |
if (type == KC_GAMMA_SRGB) {
|
|
156 |
if (gamma_tables[0] != srgb_gamma_table_256) {
|
|
157 |
Gif_DeleteArray(gamma_tables[0]);
|
|
158 |
Gif_DeleteArray(gamma_tables[1]);
|
|
159 |
}
|
|
160 |
gamma_tables[0] = (uint16_t*) srgb_gamma_table_256;
|
|
161 |
gamma_tables[1] = (uint16_t*) srgb_revgamma_table_256;
|
|
162 |
} else {
|
|
163 |
if (gamma_tables[0] == srgb_gamma_table_256) {
|
|
164 |
gamma_tables[0] = Gif_NewArray(uint16_t, 256);
|
|
165 |
gamma_tables[1] = Gif_NewArray(uint16_t, 256);
|
|
166 |
}
|
|
167 |
for (j = 0; j != 256; ++j) {
|
|
168 |
gamma_tables[0][j] = (int) (pow(i/255.0, gamma) * 32767);
|
|
169 |
gamma_tables[1][j] = (int) (pow(i/256.0, 1/gamma) * 32767);
|
|
170 |
for (i = 0; i != 2; ++i)
|
|
171 |
while (j && gamma_tables[i][j] <= gamma_tables[i][j-1]
|
|
172 |
&& gamma_tables[i][j] < 3267)
|
|
173 |
++gamma_tables[i][j];
|
|
174 |
}
|
|
175 |
}
|
|
176 |
cur_type = type;
|
|
177 |
cur_gamma = gamma;
|
|
178 |
#else
|
|
179 |
(void) type, (void) gamma;
|
|
180 |
#endif
|
|
181 |
}
|
|
182 |
|
|
183 |
#if 0
|
|
184 |
static void kc_test_gamma() {
|
|
185 |
int x, y, z;
|
|
186 |
for (x = 0; x != 256; ++x)
|
|
187 |
for (y = 0; y != 256; ++y)
|
|
188 |
for (z = 0; z != 256; ++z) {
|
|
189 |
kcolor k;
|
|
190 |
kc_set8g(&k, x, y, z);
|
|
191 |
kc_revgamma_transform(&k);
|
|
192 |
if ((k.a[0] >> 7) != x || (k.a[1] >> 7) != y
|
|
193 |
|| (k.a[2] >> 7) != z) {
|
|
194 |
kcolor kg;
|
|
195 |
kc_set8g(&kg, x, y, z);
|
|
196 |
fprintf(stderr, "#%02X%02X%02X ->g #%04X%04X%04X ->revg #%02X%02X%02X!\n",
|
|
197 |
x, y, z, kg.a[0], kg.a[1], kg.a[2],
|
|
198 |
k.a[0] >> 7, k.a[1] >> 7, k.a[2] >> 7);
|
|
199 |
assert(0);
|
|
200 |
}
|
|
201 |
}
|
|
202 |
}
|
|
203 |
#endif
|
|
204 |
|
|
205 |
static inline uint32_t kc_distance(const kcolor* x, const kcolor* y) {
|
|
206 |
return (x->a[0] - y->a[0]) * (x->a[0] - y->a[0])
|
|
207 |
+ (x->a[1] - y->a[1]) * (x->a[1] - y->a[1])
|
|
208 |
+ (x->a[2] - y->a[2]) * (x->a[2] - y->a[2]);
|
|
209 |
}
|
|
210 |
|
|
211 |
static inline int kc_luminance(const kcolor* x) {
|
|
212 |
return (306 * x->a[0] + 601 * x->a[1] + 117 * x->a[2]) >> 10;
|
|
213 |
}
|
|
214 |
|
|
215 |
static inline void kc_luminance_transform(kcolor* x) {
|
27 | 216 |
/* For grayscale colormaps, use distance in luminance space instead of
|
28 | 217 |
distance in RGB space. The weights for the R,G,B components in
|
29 | 218 |
luminance space are 0.299,0.587,0.114. Using the proportional factors
|
30 | 219 |
306, 601, and 117 we get a scaled gray value between 0 and 255 *
|
31 | 220 |
1024. Thanks to Christian Kumpf, <kumpf@igd.fhg.de>, for providing a
|
32 | 221 |
patch. */
|
33 | |
int gray = (306 * x[0] + 601 * x[1] + 117 * x[2]) >> 10;
|
34 | |
x[0] = x[1] = x[2] = gray;
|
35 | |
}
|
|
222 |
x->a[0] = x->a[1] = x->a[2] = kc_luminance(x);
|
|
223 |
}
|
|
224 |
|
36 | 225 |
|
37 | 226 |
typedef struct Gif_Histogram {
|
38 | 227 |
Gif_Color *c;
|
|
264 | 453 |
} adaptive_slot;
|
265 | 454 |
|
266 | 455 |
Gif_Colormap *
|
267 | |
colormap_median_cut(Gif_Color *hist, int nhist, int adapt_size, int complain)
|
|
456 |
colormap_median_cut(Gif_Color* hist, int nhist, Gt_OutputData* od)
|
268 | 457 |
{
|
|
458 |
int adapt_size = od->colormap_size;
|
269 | 459 |
adaptive_slot *slots = Gif_NewArray(adaptive_slot, adapt_size);
|
270 | 460 |
Gif_Colormap *gfcm = Gif_NewFullColormap(adapt_size, 256);
|
271 | 461 |
Gif_Color *adapt = gfcm->col;
|
|
277 | 467 |
|
278 | 468 |
if (adapt_size < 2 || adapt_size > 256)
|
279 | 469 |
fatal_error("adaptive palette size must be between 2 and 256");
|
280 | |
if (adapt_size >= nhist && complain)
|
|
470 |
if (adapt_size >= nhist && !od->colormap_fixed)
|
281 | 471 |
warning(1, "trivial adaptive palette (only %d colors in source)", nhist);
|
282 | 472 |
if (adapt_size >= nhist)
|
283 | 473 |
adapt_size = nhist;
|
|
381 | 571 |
for (i = 0; i < nadapt; i++) {
|
382 | 572 |
double red_total = 0, green_total = 0, blue_total = 0;
|
383 | 573 |
Gif_Color *slice = &hist[ slots[i].first ];
|
|
574 |
kcolor k;
|
384 | 575 |
for (j = 0; j < slots[i].count; j++) {
|
385 | |
red_total += slice[j].gfc_red * slice[j].pixel;
|
386 | |
green_total += slice[j].gfc_green * slice[j].pixel;
|
387 | |
blue_total += slice[j].gfc_blue * slice[j].pixel;
|
388 | |
}
|
389 | |
adapt[i].gfc_red = (uint8_t)(red_total / slots[i].pixel);
|
390 | |
adapt[i].gfc_green = (uint8_t)(green_total / slots[i].pixel);
|
391 | |
adapt[i].gfc_blue = (uint8_t)(blue_total / slots[i].pixel);
|
|
576 |
kc_set8g(&k, slice[j].gfc_red, slice[j].gfc_green, slice[j].gfc_blue);
|
|
577 |
red_total += k.a[0] * (double) slice[j].pixel;
|
|
578 |
green_total += k.a[1] * (double) slice[j].pixel;
|
|
579 |
blue_total += k.a[2] * (double) slice[j].pixel;
|
|
580 |
}
|
|
581 |
k.a[0] = (int) (red_total / slots[i].pixel);
|
|
582 |
k.a[1] = (int) (green_total / slots[i].pixel);
|
|
583 |
k.a[2] = (int) (blue_total / slots[i].pixel);
|
|
584 |
kc_revgamma_transform(&k);
|
|
585 |
adapt[i].gfc_red = (uint8_t) (k.a[0] >> 7);
|
|
586 |
adapt[i].gfc_green = (uint8_t) (k.a[1] >> 7);
|
|
587 |
adapt[i].gfc_blue = (uint8_t) (k.a[2] >> 7);
|
392 | 588 |
adapt[i].haspixel = 0;
|
393 | 589 |
}
|
394 | 590 |
|
|
400 | 596 |
|
401 | 597 |
|
402 | 598 |
static Gif_Colormap *
|
403 | |
colormap_diversity(Gif_Color *hist, int nhist, int adapt_size, int complain,
|
|
599 |
colormap_diversity(Gif_Color *hist, int nhist, Gt_OutputData* od,
|
404 | 600 |
int blend)
|
405 | 601 |
{
|
406 | |
uint32_t *min_dist = Gif_NewArray(uint32_t, nhist);
|
|
602 |
int adapt_size = od->colormap_size;
|
|
603 |
uint32_t* min_dist = Gif_NewArray(uint32_t, nhist);
|
|
604 |
uint32_t* min_dither_dist = Gif_NewArray(uint32_t, nhist);
|
407 | 605 |
int *closest = Gif_NewArray(int, nhist);
|
|
606 |
kcolor* gchist = Gif_NewArray(kcolor, nhist); /* gamma-corrected */
|
408 | 607 |
Gif_Colormap *gfcm = Gif_NewFullColormap(adapt_size, 256);
|
409 | 608 |
Gif_Color *adapt = gfcm->col;
|
410 | 609 |
int nadapt = 0;
|
|
416 | 615 |
|
417 | 616 |
if (adapt_size < 2 || adapt_size > 256)
|
418 | 617 |
fatal_error("adaptive palette size must be between 2 and 256");
|
419 | |
if (adapt_size > nhist && complain)
|
|
618 |
if (adapt_size > nhist && !od->colormap_fixed)
|
420 | 619 |
warning(1, "trivial adaptive palette (only %d colors in source)", nhist);
|
421 | 620 |
if (adapt_size > nhist)
|
422 | 621 |
adapt_size = nhist;
|
|
444 | 643 |
|
445 | 644 |
/* 1. initialize min_dist and sort the colors in order of popularity. */
|
446 | 645 |
for (i = 0; i < nhist; i++)
|
447 | |
min_dist[i] = 0x7FFFFFFF;
|
|
646 |
min_dist[i] = min_dither_dist[i] = 0xFFFFFFFF;
|
448 | 647 |
|
449 | 648 |
qsort(hist, nhist, sizeof(Gif_Color), popularity_sort_compare);
|
|
649 |
|
|
650 |
/* 1.5. gamma-correct hist colors */
|
|
651 |
for (i = 0; i < nhist; ++i)
|
|
652 |
kc_set8g(&gchist[i], hist[i].gfc_red, hist[i].gfc_green,
|
|
653 |
hist[i].gfc_blue);
|
450 | 654 |
|
451 | 655 |
/* 2. choose colors one at a time */
|
452 | 656 |
for (nadapt = 0; nadapt < adapt_size; nadapt++) {
|
|
461 | 665 |
/* 2.1a. want most popular unchosen color; we've sorted them on
|
462 | 666 |
popularity, so we're done! */
|
463 | 667 |
|
464 | |
} else {
|
|
668 |
} else if (od->dither_type == dither_none) {
|
465 | 669 |
/* 2.1b. choose based on diversity from unchosen colors */
|
466 | 670 |
for (i = chosen + 1; i != nhist; ++i)
|
467 | 671 |
if (min_dist[i] > min_dist[chosen])
|
468 | 672 |
chosen = i;
|
|
673 |
|
|
674 |
} else {
|
|
675 |
/* 2.1c. choose based on diversity from unchosen colors, but allow
|
|
676 |
dithered combinations to stand in for colors, particularly early
|
|
677 |
on in the color finding process */
|
|
678 |
#if HAVE_POW
|
|
679 |
/* Weight assigned to dithered combinations drops as we proceed. */
|
|
680 |
double dweight = 0.05 + pow(0.25, 1 + (nadapt - 1) / 3.);
|
|
681 |
#else
|
|
682 |
double dweight = nadapt < 4 ? 0.25 : 0.125;
|
|
683 |
#endif
|
|
684 |
double max_dist = min_dist[chosen] + min_dither_dist[chosen] * dweight;
|
|
685 |
for (i = chosen + 1; i != nhist; ++i)
|
|
686 |
if (min_dist[i] != 0) {
|
|
687 |
double dist = min_dist[i] + min_dither_dist[i] * dweight;
|
|
688 |
if (dist > max_dist) {
|
|
689 |
chosen = i;
|
|
690 |
max_dist = dist;
|
|
691 |
}
|
|
692 |
}
|
469 | 693 |
}
|
470 | 694 |
|
471 | 695 |
/* 2.2. add the color */
|
472 | |
min_dist[chosen] = 0;
|
|
696 |
min_dist[chosen] = min_dither_dist[chosen] = 0;
|
473 | 697 |
closest[chosen] = nadapt;
|
|
698 |
adapt[nadapt] = hist[chosen];
|
|
699 |
adapt[nadapt].pixel = chosen;
|
|
700 |
adapt[nadapt].haspixel = 0;
|
474 | 701 |
|
475 | 702 |
/* 2.3. adjust the min_dist array */
|
476 | 703 |
for (i = 0; i < nhist; ++i)
|
477 | 704 |
if (min_dist[i]) {
|
478 | |
uint32_t dist = rgb_distance(&hist[i], &hist[chosen]);
|
|
705 |
uint32_t dist = kc_distance(&gchist[i], &gchist[chosen]);
|
479 | 706 |
if (dist < min_dist[i]) {
|
480 | 707 |
min_dist[i] = dist;
|
481 | 708 |
closest[i] = nadapt;
|
482 | 709 |
}
|
483 | 710 |
}
|
|
711 |
|
|
712 |
/* 2.4. also account for dither distances */
|
|
713 |
if (od->dither_type != dither_none && nadapt > 0 && nadapt < 64)
|
|
714 |
for (j = 0; j < nadapt; ++j) {
|
|
715 |
kcolor x = gchist[chosen], *y = &gchist[adapt[j].pixel];
|
|
716 |
/* penalize combinations with large luminance difference */
|
|
717 |
double dL = fabs(kc_luminance(&x) - kc_luminance(y));
|
|
718 |
dL = (dL > 8192 ? dL * 4 / 32767. : 1);
|
|
719 |
/* create combination */
|
|
720 |
for (i = 0; i < 3; ++i)
|
|
721 |
x.a[i] = (x.a[i] + y->a[i]) >> 1;
|
|
722 |
/* track closeness of combination to other colors */
|
|
723 |
for (i = 0; i < nhist; ++i)
|
|
724 |
if (min_dist[i]) {
|
|
725 |
double dist = kc_distance(&gchist[i], &x) * dL;
|
|
726 |
if (dist < min_dither_dist[i])
|
|
727 |
min_dither_dist[i] = (uint32_t) dist;
|
|
728 |
}
|
|
729 |
}
|
484 | 730 |
}
|
485 | 731 |
|
486 | 732 |
/* 3. make the new palette by choosing one color from each slot. */
|
487 | |
if (!blend) {
|
488 | |
for (i = 0; i < nadapt; i++) {
|
489 | |
for (j = 0; j < nhist; j++)
|
490 | |
if (closest[j] == i && !min_dist[j])
|
491 | |
match = j;
|
492 | |
adapt[i] = hist[match];
|
493 | |
adapt[i].haspixel = 0;
|
494 | |
}
|
495 | |
|
496 | |
} else {
|
|
733 |
if (blend) {
|
497 | 734 |
for (i = 0; i < nadapt; i++) {
|
498 | 735 |
double red_total = 0, green_total = 0, blue_total = 0;
|
499 | 736 |
uint32_t pixel_total = 0, mismatch_pixel_total = 0;
|
500 | 737 |
for (j = 0; j < nhist; j++)
|
501 | 738 |
if (closest[j] == i) {
|
502 | |
uint32_t pixel = hist[j].pixel;
|
503 | |
red_total += hist[j].gfc_red * pixel;
|
504 | |
green_total += hist[j].gfc_green * pixel;
|
505 | |
blue_total += hist[j].gfc_blue * pixel;
|
|
739 |
double pixel = hist[j].pixel;
|
|
740 |
red_total += gchist[j].a[0] * pixel;
|
|
741 |
green_total += gchist[j].a[1] * pixel;
|
|
742 |
blue_total += gchist[j].a[2] * pixel;
|
506 | 743 |
pixel_total += pixel;
|
507 | 744 |
if (min_dist[j])
|
508 | 745 |
mismatch_pixel_total += pixel;
|
|
516 | 753 |
else {
|
517 | 754 |
/* Favor, by a smallish amount, the color the plain diversity
|
518 | 755 |
algorithm would pick. */
|
519 | |
uint32_t pixel = hist[match].pixel * 2;
|
520 | |
red_total += hist[match].gfc_red * pixel;
|
521 | |
green_total += hist[match].gfc_green * pixel;
|
522 | |
blue_total += hist[match].gfc_blue * pixel;
|
|
756 |
double pixel = hist[match].pixel * 2;
|
|
757 |
kcolor k;
|
|
758 |
red_total += gchist[match].a[0] * pixel;
|
|
759 |
green_total += gchist[match].a[1] * pixel;
|
|
760 |
blue_total += gchist[match].a[2] * pixel;
|
523 | 761 |
pixel_total += pixel;
|
524 | |
adapt[i].gfc_red = (uint8_t)(red_total / pixel_total);
|
525 | |
adapt[i].gfc_green = (uint8_t)(green_total / pixel_total);
|
526 | |
adapt[i].gfc_blue = (uint8_t)(blue_total / pixel_total);
|
|
762 |
k.a[0] = (int) (red_total / pixel_total);
|
|
763 |
k.a[1] = (int) (green_total / pixel_total);
|
|
764 |
k.a[2] = (int) (blue_total / pixel_total);
|
|
765 |
kc_revgamma_transform(&k);
|
|
766 |
adapt[i].gfc_red = (uint8_t) (k.a[0] >> 7);
|
|
767 |
adapt[i].gfc_green = (uint8_t) (k.a[1] >> 7);
|
|
768 |
adapt[i].gfc_blue = (uint8_t) (k.a[2] >> 7);
|
527 | 769 |
}
|
528 | 770 |
adapt[i].haspixel = 0;
|
529 | 771 |
}
|
530 | 772 |
}
|
531 | 773 |
|
532 | 774 |
Gif_DeleteArray(min_dist);
|
|
775 |
Gif_DeleteArray(min_dither_dist);
|
533 | 776 |
Gif_DeleteArray(closest);
|
|
777 |
Gif_DeleteArray(gchist);
|
534 | 778 |
gfcm->ncol = nadapt;
|
535 | 779 |
return gfcm;
|
536 | 780 |
}
|
537 | 781 |
|
538 | 782 |
|
539 | |
Gif_Colormap *
|
540 | |
colormap_blend_diversity(Gif_Color *hist, int nhist, int adapt_size,
|
541 | |
int complain)
|
542 | |
{
|
543 | |
return colormap_diversity(hist, nhist, adapt_size, complain, 1);
|
544 | |
}
|
545 | |
|
546 | |
Gif_Colormap *
|
547 | |
colormap_flat_diversity(Gif_Color *hist, int nhist, int adapt_size,
|
548 | |
int complain)
|
549 | |
{
|
550 | |
return colormap_diversity(hist, nhist, adapt_size, complain, 0);
|
|
783 |
Gif_Colormap* colormap_blend_diversity(Gif_Color* hist, int nhist,
|
|
784 |
Gt_OutputData* od) {
|
|
785 |
return colormap_diversity(hist, nhist, od, 1);
|
|
786 |
}
|
|
787 |
|
|
788 |
Gif_Colormap* colormap_flat_diversity(Gif_Color* hist, int nhist,
|
|
789 |
Gt_OutputData* od) {
|
|
790 |
return colormap_diversity(hist, nhist, od, 0);
|
551 | 791 |
}
|
552 | 792 |
|
553 | 793 |
|
|
556 | 796 |
**/
|
557 | 797 |
|
558 | 798 |
typedef struct kd3_item {
|
559 | |
int a[3];
|
|
799 |
kcolor k;
|
560 | 800 |
int index;
|
561 | 801 |
} kd3_item;
|
562 | 802 |
|
|
565 | 805 |
int offset;
|
566 | 806 |
} kd3_treepos;
|
567 | 807 |
|
568 | |
typedef struct kd3_tree {
|
|
808 |
struct kd3_tree {
|
569 | 809 |
kd3_treepos* tree;
|
570 | 810 |
int ntree;
|
571 | 811 |
int disabled;
|
|
573 | 813 |
int nitems;
|
574 | 814 |
int items_cap;
|
575 | 815 |
int maxdepth;
|
576 | |
unsigned (*distance)(const int[3], const int[3]);
|
577 | |
void (*transform)(int[3]);
|
|
816 |
void (*transform)(kcolor*);
|
578 | 817 |
unsigned* xradius;
|
579 | |
} kd3_tree;
|
580 | |
|
581 | |
void kd3_init(kd3_tree* kd3, unsigned (*distance)(const int[3], const int[3]),
|
582 | |
void (*transform)(int[3])) {
|
|
818 |
};
|
|
819 |
|
|
820 |
void kd3_init(kd3_tree* kd3, void (*transform)(kcolor*)) {
|
583 | 821 |
kd3->tree = NULL;
|
584 | 822 |
kd3->items = Gif_NewArray(kd3_item, 256);
|
585 | 823 |
kd3->nitems = 0;
|
586 | 824 |
kd3->items_cap = 256;
|
587 | |
kd3->distance = distance;
|
588 | 825 |
kd3->transform = transform;
|
589 | 826 |
kd3->xradius = NULL;
|
590 | 827 |
kd3->disabled = -1;
|
|
596 | 833 |
Gif_DeleteArray(kd3->xradius);
|
597 | 834 |
}
|
598 | 835 |
|
599 | |
void kd3_add(kd3_tree* kd3, int a0, int a1, int a2) {
|
600 | |
assert(!kd3->tree);
|
|
836 |
void kd3_add_transformed(kd3_tree* kd3, const kcolor* k) {
|
601 | 837 |
if (kd3->nitems == kd3->items_cap) {
|
602 | 838 |
kd3->items_cap *= 2;
|
603 | 839 |
Gif_ReArray(kd3->items, kd3_item, kd3->items_cap);
|
604 | 840 |
}
|
605 | |
kd3->items[kd3->nitems].a[0] = a0;
|
606 | |
kd3->items[kd3->nitems].a[1] = a1;
|
607 | |
kd3->items[kd3->nitems].a[2] = a2;
|
608 | |
if (kd3->transform)
|
609 | |
kd3->transform(kd3->items[kd3->nitems].a);
|
|
841 |
kd3->items[kd3->nitems].k = *k;
|
610 | 842 |
kd3->items[kd3->nitems].index = kd3->nitems;
|
611 | 843 |
++kd3->nitems;
|
612 | 844 |
}
|
613 | 845 |
|
614 | |
void kd3_add_8to15(kd3_tree* kd3, int a0, int a1, int a2) {
|
615 | |
kd3_add(kd3, (a0 << 7) | (a0 >> 1), (a1 << 7) | (a1 >> 1),
|
616 | |
(a2 << 7) | (a2 >> 1));
|
|
846 |
void kd3_add8g(kd3_tree* kd3, int a0, int a1, int a2) {
|
|
847 |
kcolor k;
|
|
848 |
kc_set8g(&k, a0, a1, a2);
|
|
849 |
if (kd3->transform)
|
|
850 |
kd3->transform(&k);
|
|
851 |
kd3_add_transformed(kd3, &k);
|
617 | 852 |
}
|
618 | 853 |
|
619 | 854 |
static int kd3_item_compar_0(const void* a, const void* b) {
|
620 | 855 |
const kd3_item* aa = (const kd3_item*) a, *bb = (const kd3_item*) b;
|
621 | |
return aa->a[0] - bb->a[0];
|
|
856 |
return aa->k.a[0] - bb->k.a[0];
|
622 | 857 |
}
|
623 | 858 |
|
624 | 859 |
static int kd3_item_compar_1(const void* a, const void* b) {
|
625 | 860 |
const kd3_item* aa = (const kd3_item*) a, *bb = (const kd3_item*) b;
|
626 | |
return aa->a[1] - bb->a[1];
|
|
861 |
return aa->k.a[1] - bb->k.a[1];
|
627 | 862 |
}
|
628 | 863 |
|
629 | 864 |
static int kd3_item_compar_2(const void* a, const void* b) {
|
630 | 865 |
const kd3_item* aa = (const kd3_item*) a, *bb = (const kd3_item*) b;
|
631 | |
return aa->a[2] - bb->a[2];
|
|
866 |
return aa->k.a[2] - bb->k.a[2];
|
632 | 867 |
}
|
633 | 868 |
|
634 | 869 |
static int (*kd3_item_compars[])(const void*, const void*) = {
|
|
637 | 872 |
|
638 | 873 |
static int kd3_build_range(kd3_tree* kd3, kd3_item* items,
|
639 | 874 |
int l, int r, int n, int depth) {
|
640 | |
int m, aindex = depth % 3;
|
|
875 |
int m, nl, nr, aindex = depth % 3;
|
641 | 876 |
if (depth > kd3->maxdepth)
|
642 | 877 |
kd3->maxdepth = depth;
|
643 | 878 |
while (n >= kd3->ntree) {
|
|
651 | 886 |
}
|
652 | 887 |
|
653 | 888 |
qsort(&items[l], r - l, sizeof(kd3_item), kd3_item_compars[aindex]);
|
|
889 |
|
|
890 |
/* pick pivot: a color component to split */
|
654 | 891 |
m = l + ((r - l) >> 1);
|
655 | |
while (m > l && items[m].a[aindex] == items[m-1].a[aindex])
|
|
892 |
while (m > l && items[m].k.a[aindex] == items[m-1].k.a[aindex])
|
656 | 893 |
--m;
|
|
894 |
if (m == l) { /* don't split entirely to the right (infinite loop) */
|
|
895 |
m = l + ((r - l) >> 1);
|
|
896 |
while (m < r && items[m].k.a[aindex] == items[m-1].k.a[aindex])
|
|
897 |
++m;
|
|
898 |
}
|
657 | 899 |
if (m == l)
|
658 | |
kd3->tree[n].pivot = items[m].a[aindex];
|
|
900 |
kd3->tree[n].pivot = items[m].k.a[aindex];
|
659 | 901 |
else
|
660 | |
kd3->tree[n].pivot = items[m-1].a[aindex]
|
661 | |
+ ((items[m].a[aindex] - items[m-1].a[aindex]) >> 1);
|
662 | |
int nl = kd3_build_range(kd3, items, l, m, n+1, depth+1);
|
|
902 |
kd3->tree[n].pivot = items[m-1].k.a[aindex]
|
|
903 |
+ ((items[m].k.a[aindex] - items[m-1].k.a[aindex]) >> 1);
|
|
904 |
|
|
905 |
/* recurse */
|
|
906 |
nl = kd3_build_range(kd3, items, l, m, n+1, depth+1);
|
663 | 907 |
kd3->tree[n].offset = 1+nl;
|
664 | |
int nr = kd3_build_range(kd3, items, m, r, n+1+nl, depth+1);
|
|
908 |
nr = kd3_build_range(kd3, items, m, r, n+1+nl, depth+1);
|
665 | 909 |
return 1+nl+nr;
|
666 | 910 |
}
|
667 | 911 |
|
668 | 912 |
static int kd3_item_all_compar(const void* a, const void* b) {
|
669 | 913 |
const kd3_item* aa = (const kd3_item*) a, *bb = (const kd3_item*) b;
|
670 | |
if (aa->a[0] - bb->a[0])
|
671 | |
return aa->a[0] - bb->a[0];
|
672 | |
else if (aa->a[1] - bb->a[1])
|
673 | |
return aa->a[1] - bb->a[1];
|
|
914 |
if (aa->k.a[0] - bb->k.a[0])
|
|
915 |
return aa->k.a[0] - bb->k.a[0];
|
|
916 |
else if (aa->k.a[1] - bb->k.a[1])
|
|
917 |
return aa->k.a[1] - bb->k.a[1];
|
674 | 918 |
else
|
675 | |
return aa->a[2] - bb->a[2];
|
676 | |
}
|
677 | |
|
678 | |
void kd3_print(kd3_tree* kd3, int depth, kd3_treepos* p, int* a, int* b) {
|
|
919 |
return aa->k.a[2] - bb->k.a[2];
|
|
920 |
}
|
|
921 |
|
|
922 |
#if 0
|
|
923 |
static void kd3_print_depth(kd3_tree* kd3, int depth, kd3_treepos* p,
|
|
924 |
int* a, int* b) {
|
679 | 925 |
int i;
|
680 | |
char x[10][6];
|
|
926 |
char x[6][10];
|
681 | 927 |
for (i = 0; i != 3; ++i) {
|
682 | |
if (a[i] == 0x80000000)
|
|
928 |
if (a[i] == INT_MIN)
|
683 | 929 |
sprintf(x[2*i], "*");
|
684 | 930 |
else
|
685 | 931 |
sprintf(x[2*i], "%d", a[i]);
|
686 | |
if (b[i] == 0x7FFFFFFF)
|
|
932 |
if (b[i] == INT_MAX)
|
687 | 933 |
sprintf(x[2*i+1], "*");
|
688 | 934 |
else
|
689 | 935 |
sprintf(x[2*i+1], "%d", b[i]);
|
|
692 | 938 |
x[0], x[1], x[2], x[3], x[4], x[5]);
|
693 | 939 |
if (p->offset < 0) {
|
694 | 940 |
if (p->pivot >= 0) {
|
695 | |
assert(kd3->items[p->pivot].a[0] >= a[0]);
|
696 | |
assert(kd3->items[p->pivot].a[1] >= a[1]);
|
697 | |
assert(kd3->items[p->pivot].a[2] >= a[2]);
|
698 | |
assert(kd3->items[p->pivot].a[0] < b[0]);
|
699 | |
assert(kd3->items[p->pivot].a[1] < b[1]);
|
700 | |
assert(kd3->items[p->pivot].a[2] < b[2]);
|
701 | |
printf(" ** @%d: <%d,%d,%d>\n", p->pivot, kd3->items[p->pivot].a[0], kd3->items[p->pivot].a[1], kd3->items[p->pivot].a[2]);
|
|
941 |
assert(kd3->items[p->pivot].k.a[0] >= a[0]);
|
|
942 |
assert(kd3->items[p->pivot].k.a[1] >= a[1]);
|
|
943 |
assert(kd3->items[p->pivot].k.a[2] >= a[2]);
|
|
944 |
assert(kd3->items[p->pivot].k.a[0] < b[0]);
|
|
945 |
assert(kd3->items[p->pivot].k.a[1] < b[1]);
|
|
946 |
assert(kd3->items[p->pivot].k.a[2] < b[2]);
|
|
947 |
printf(" ** @%d: <%d,%d,%d>\n", p->pivot, kd3->items[p->pivot].k.a[0], kd3->items[p->pivot].k.a[1], kd3->items[p->pivot].k.a[2]);
|
702 | 948 |
}
|
703 | 949 |
} else {
|
704 | |
int aindex = depth % 3;
|
|
950 |
int aindex = depth % 3, x[3];
|
705 | 951 |
assert(p->pivot >= a[aindex]);
|
706 | 952 |
assert(p->pivot < b[aindex]);
|
707 | 953 |
printf((aindex == 0 ? " | <%d,_,_>\n" :
|
708 | 954 |
aindex == 1 ? " | <_,%d,_>\n" : " | <_,_,%d>\n"), p->pivot);
|
709 | |
int x[3];
|
710 | 955 |
memcpy(x, b, sizeof(int) * 3);
|
711 | 956 |
x[aindex] = p->pivot;
|
712 | |
kd3_print(kd3, depth + 1, p + 1, a, x);
|
|
957 |
kd3_print_depth(kd3, depth + 1, p + 1, a, x);
|
713 | 958 |
memcpy(x, a, sizeof(int) * 3);
|
714 | 959 |
x[aindex] = p->pivot;
|
715 | |
kd3_print(kd3, depth + 1, p + p->offset, x, b);
|
716 | |
}
|
717 | |
}
|
718 | |
|
719 | |
void kd3_build(kd3_tree* kd3) {
|
720 | |
kd3_item* items;
|
721 | |
int i, j, delta;
|
722 | |
assert(!kd3->tree);
|
723 | |
|
724 | |
// create xradius
|
|
960 |
kd3_print_depth(kd3, depth + 1, p + p->offset, x, b);
|
|
961 |
}
|
|
962 |
}
|
|
963 |
|
|
964 |
static void kd3_print(kd3_tree* kd3) {
|
|
965 |
int a[3], b[3];
|
|
966 |
a[0] = a[1] = a[2] = INT_MIN;
|
|
967 |
b[0] = b[1] = b[2] = INT_MAX;
|
|
968 |
kd3_print_depth(kd3, 0, kd3->tree, a, b);
|
|
969 |
}
|
|
970 |
#endif
|
|
971 |
|
|
972 |
void kd3_build_xradius(kd3_tree* kd3) {
|
|
973 |
int i, j;
|
|
974 |
/* create xradius */
|
|
975 |
if (kd3->xradius)
|
|
976 |
return;
|
725 | 977 |
kd3->xradius = Gif_NewArray(unsigned, kd3->nitems);
|
726 | 978 |
for (i = 0; i != kd3->nitems; ++i)
|
727 | 979 |
kd3->xradius[i] = (unsigned) -1;
|
728 | 980 |
for (i = 0; i != kd3->nitems; ++i)
|
729 | 981 |
for (j = i + 1; j != kd3->nitems; ++j) {
|
730 | |
unsigned dist = kd3->distance(kd3->items[i].a, kd3->items[j].a);
|
|
982 |
unsigned dist = kc_distance(&kd3->items[i].k, &kd3->items[j].k);
|
731 | 983 |
unsigned radius = dist / 4;
|
732 | 984 |
if (radius < kd3->xradius[i])
|
733 | 985 |
kd3->xradius[i] = radius;
|
734 | 986 |
if (radius < kd3->xradius[j])
|
735 | 987 |
kd3->xradius[j] = radius;
|
736 | 988 |
}
|
737 | |
|
738 | |
// create tree
|
|
989 |
}
|
|
990 |
|
|
991 |
void kd3_build(kd3_tree* kd3) {
|
|
992 |
kd3_item* items;
|
|
993 |
int i, delta;
|
|
994 |
assert(!kd3->tree);
|
|
995 |
|
|
996 |
/* create tree */
|
739 | 997 |
kd3->tree = Gif_NewArray(kd3_treepos, 256);
|
740 | 998 |
kd3->ntree = 256;
|
741 | 999 |
kd3->maxdepth = 0;
|
742 | 1000 |
|
743 | |
// create copy of items; remove duplicates
|
|
1001 |
/* create copy of items; remove duplicates */
|
744 | 1002 |
items = Gif_NewArray(kd3_item, kd3->nitems);
|
745 | 1003 |
memcpy(items, kd3->items, sizeof(kd3_item) * kd3->nitems);
|
746 | 1004 |
qsort(items, kd3->nitems, sizeof(kd3_item), kd3_item_all_compar);
|
747 | 1005 |
for (i = 0, delta = 1; i != kd3->nitems - delta; ++i)
|
748 | |
if (items[i].a[0] == items[i+delta].a[0]
|
749 | |
&& items[i].a[1] == items[i+delta].a[1]
|
750 | |
&& items[i].a[2] == items[i+delta].a[2])
|
|
1006 |
if (items[i].k.a[0] == items[i+delta].k.a[0]
|
|
1007 |
&& items[i].k.a[1] == items[i+delta].k.a[1]
|
|
1008 |
&& items[i].k.a[2] == items[i+delta].k.a[2])
|
751 | 1009 |
++delta, --i;
|
752 | 1010 |
else if (delta > 1)
|
753 | 1011 |
items[i+1] = items[i+delta];
|
|
759 | 1017 |
}
|
760 | 1018 |
|
761 | 1019 |
void kd3_disable(kd3_tree* kd3, int i) {
|
762 | |
assert(kd3->disabled < 0);
|
763 | |
kd3->disabled = i;
|
|
1020 |
assert((unsigned) i < (unsigned) kd3->nitems);
|
|
1021 |
if (kd3->items[i].index >= 0) {
|
|
1022 |
kd3->items[i].index = kd3->disabled;
|
|
1023 |
kd3->disabled = -i - 2;
|
|
1024 |
}
|
|
1025 |
}
|
|
1026 |
|
|
1027 |
void kd3_enable(kd3_tree* kd3, int i) {
|
|
1028 |
int* pprev = &kd3->disabled;
|
|
1029 |
assert((unsigned) i < (unsigned) kd3->nitems);
|
|
1030 |
while (*pprev != -1 && *pprev != -i - 2)
|
|
1031 |
pprev = &kd3->items[-*pprev - 2].index;
|
|
1032 |
if (*pprev == -i - 2) {
|
|
1033 |
*pprev = kd3->items[i].index;
|
|
1034 |
kd3->items[i].index = i;
|
|
1035 |
}
|
764 | 1036 |
}
|
765 | 1037 |
|
766 | 1038 |
void kd3_enable_all(kd3_tree* kd3) {
|
767 | |
kd3->disabled = -1;
|
768 | |
}
|
769 | |
|
770 | |
int kd3_closest_transformed(const kd3_tree* kd3, const int a[3]) {
|
771 | |
assert(kd3->tree);
|
|
1039 |
while (kd3->disabled != -1) {
|
|
1040 |
int i = -kd3->disabled - 2;
|
|
1041 |
kd3->disabled = kd3->items[i].index;
|
|
1042 |
kd3->items[i].index = i;
|
|
1043 |
}
|
|
1044 |
}
|
|
1045 |
|
|
1046 |
int kd3_closest_transformed(const kd3_tree* kd3, const kcolor* k) {
|
772 | 1047 |
const kd3_treepos* stack[32];
|
773 | 1048 |
uint8_t state[32];
|
774 | 1049 |
int stackpos = 0;
|
775 | 1050 |
int result = -1;
|
776 | 1051 |
unsigned mindist = (unsigned) -1;
|
|
1052 |
assert(kd3->tree);
|
777 | 1053 |
stack[0] = kd3->tree;
|
778 | 1054 |
state[0] = 0;
|
779 | 1055 |
|
780 | 1056 |
while (stackpos >= 0) {
|
|
1057 |
const kd3_treepos* p;
|
781 | 1058 |
assert(stackpos < 32);
|
782 | |
const kd3_treepos* p = stack[stackpos];
|
|
1059 |
p = stack[stackpos];
|
783 | 1060 |
|
784 | 1061 |
if (p->offset < 0) {
|
785 | |
if (p->pivot >= 0 && kd3->disabled != p->pivot) {
|
786 | |
unsigned dist = kd3->distance(kd3->items[p->pivot].a, a);
|
|
1062 |
if (p->pivot >= 0 && kd3->items[p->pivot].index >= 0) {
|
|
1063 |
unsigned dist = kc_distance(&kd3->items[p->pivot].k, k);
|
787 | 1064 |
if (dist < mindist) {
|
788 | 1065 |
mindist = dist;
|
789 | 1066 |
result = p->pivot;
|
|
792 | 1069 |
if (--stackpos >= 0)
|
793 | 1070 |
++state[stackpos];
|
794 | 1071 |
} else if (state[stackpos] == 0) {
|
795 | |
if (a[stackpos % 3] < p->pivot)
|
|
1072 |
if (k->a[stackpos % 3] < p->pivot)
|
796 | 1073 |
stack[stackpos + 1] = p + 1;
|
797 | 1074 |
else
|
798 | 1075 |
stack[stackpos + 1] = p + p->offset;
|
799 | 1076 |
++stackpos;
|
800 | 1077 |
state[stackpos] = 0;
|
801 | 1078 |
} else {
|
802 | |
int delta = a[stackpos % 3] - p->pivot;
|
|
1079 |
int delta = k->a[stackpos % 3] - p->pivot;
|
803 | 1080 |
if (state[stackpos] == 1
|
804 | 1081 |
&& (unsigned) (delta * delta) < mindist) {
|
805 | 1082 |
if (delta < 0)
|
|
816 | 1093 |
return result;
|
817 | 1094 |
}
|
818 | 1095 |
|
819 | |
int kd3_closest(const kd3_tree* kd3, int a0, int a1, int a2) {
|
820 | |
int a[3] = {a0, a1, a2};
|
|
1096 |
int kd3_closest(const kd3_tree* kd3, kcolor k) {
|
821 | 1097 |
if (kd3->transform)
|
822 | |
kd3->transform(a);
|
823 | |
return kd3_closest_transformed(kd3, a);
|
824 | |
}
|
825 | |
|
826 | |
int kd3_closest_8to15(const kd3_tree* kd3, int a0, int a1, int a2) {
|
827 | |
return kd3_closest(kd3, (a0 << 7) | (a0 >> 1), (a1 << 7) | (a1 >> 1),
|
828 | |
(a2 << 7) | (a2 >> 1));
|
|
1098 |
kd3->transform(&k);
|
|
1099 |
return kd3_closest_transformed(kd3, &k);
|
|
1100 |
}
|
|
1101 |
|
|
1102 |
int kd3_closest8(const kd3_tree* kd3, int a0, int a1, int a2) {
|
|
1103 |
kcolor k;
|
|
1104 |
kc_set8g(&k, a0, a1, a2);
|
|
1105 |
if (kd3->transform)
|
|
1106 |
kd3->transform(&k);
|
|
1107 |
return kd3_closest_transformed(kd3, &k);
|
829 | 1108 |
}
|
830 | 1109 |
|
831 | 1110 |
|
|
842 | 1121 |
|
843 | 1122 |
/* find closest colors in new colormap */
|
844 | 1123 |
for (i = 0; i < ncol; i++) {
|
845 | |
map[i] = col[i].pixel = kd3_closest_8to15(kd3, col[i].gfc_red, col[i].gfc_green, col[i].gfc_blue);
|
|
1124 |
map[i] = col[i].pixel = kd3_closest8(kd3, col[i].gfc_red, col[i].gfc_green, col[i].gfc_blue);
|
846 | 1125 |
col[i].haspixel = 1;
|
847 | 1126 |
}
|
848 | 1127 |
|
|
864 | 1143 |
#define DITHER_ITEM2ERR (1<<(DITHER_SHIFT-7))
|
865 | 1144 |
#define N_RANDOM_VALUES 512
|
866 | 1145 |
|
867 | |
typedef struct color_erritem {
|
868 | |
int32_t a[3];
|
869 | |
} color_erritem;
|
870 | |
|
871 | 1146 |
void
|
872 | 1147 |
colormap_image_floyd_steinberg(Gif_Image *gfi, uint8_t *all_new_data,
|
873 | 1148 |
Gif_Colormap *old_cm, kd3_tree* kd3,
|
|
879 | 1154 |
int dither_direction = 0;
|
880 | 1155 |
int transparent = gfi->transparent;
|
881 | 1156 |
int i, j, k;
|
882 | |
color_erritem *err, *err1;
|
|
1157 |
kcolor *err, *err1;
|
883 | 1158 |
|
884 | 1159 |
/* Initialize distances */
|
885 | 1160 |
for (i = 0; i < old_cm->ncol; ++i) {
|
886 | 1161 |
Gif_Color* c = &old_cm->col[i];
|
887 | |
c->pixel = kd3_closest_8to15(kd3, c->gfc_red, c->gfc_green, c->gfc_blue);
|
|
1162 |
c->pixel = kd3_closest8(kd3, c->gfc_red, c->gfc_green, c->gfc_blue);
|
888 | 1163 |
c->haspixel = 1;
|
889 | 1164 |
}
|
890 | 1165 |
|
|
893 | 1168 |
|
894 | 1169 |
/* Initialize Floyd-Steinberg error vectors to small random values, so we
|
895 | 1170 |
don't get artifacts on the top row */
|
896 | |
err = Gif_NewArray(color_erritem, width + 2);
|
897 | |
err1 = Gif_NewArray(color_erritem, width + 2);
|
|
1171 |
err = Gif_NewArray(kcolor, width + 2);
|
|
1172 |
err1 = Gif_NewArray(kcolor, width + 2);
|
898 | 1173 |
/* Use the same random values on each call in an attempt to minimize
|
899 | 1174 |
"jumping dithering" effects on animations */
|
900 | 1175 |
if (!random_values) {
|
|
909 | 1184 |
}
|
910 | 1185 |
/* err1 initialized below */
|
911 | 1186 |
|
|
1187 |
kd3_build_xradius(kd3);
|
|
1188 |
|
912 | 1189 |
/* Do the image! */
|
913 | 1190 |
for (j = 0; j < gfi->height; j++) {
|
914 | 1191 |
int d0, d1, d2, d3; /* used for error diffusion */
|
|
931 | 1208 |
/* Do a single row */
|
932 | 1209 |
while (x >= 0 && x < width) {
|
933 | 1210 |
int e;
|
934 | |
color_erritem use, usexf;
|
|
1211 |
kcolor use;
|
935 | 1212 |
|
936 | 1213 |
/* the transparent color never gets adjusted */
|
937 | 1214 |
if (*data == transparent)
|
938 | 1215 |
goto next;
|
939 | 1216 |
|
940 | |
/* use Floyd-Steinberg errors to adjust actual color */
|
941 | |
for (k = 0; k < 3; ++k) {
|
942 | |
use.a[k] = old_cm->col[*data].gfc_array[k];
|
943 | |
use.a[k] = (use.a[k] << 7) | (use.a[k] >> 1);
|
|
1217 |
/* find desired new color */
|
|
1218 |
kc_set8g(&use, old_cm->col[*data].gfc_red, old_cm->col[*data].gfc_green,
|
|
1219 |
old_cm->col[*data].gfc_blue);
|
|
1220 |
if (kd3->transform)
|
|
1221 |
kd3->transform(&use);
|
|
1222 |
/* use Floyd-Steinberg errors to adjust */
|
|
1223 |
for (k = 0; k < 3; ++k)
|
944 | 1224 |
use.a[k] += (err[x+1].a[k] & ~(DITHER_ITEM2ERR-1)) / DITHER_ITEM2ERR;
|
945 | |
use.a[k] = max(use.a[k], 0);
|
946 | |
use.a[k] = min(use.a[k], (255 << 7) | (255 >> 1));
|
947 | |
}
|
948 | |
|
949 | |
usexf = use;
|
950 | |
if (kd3->transform)
|
951 | |
kd3->transform(usexf.a);
|
|
1225 |
kc_clamp(&use);
|
|
1226 |
|
952 | 1227 |
e = old_cm->col[*data].pixel;
|
953 | |
if (kd3->distance(kd3->items[e].a, usexf.a) < kd3->xradius[e])
|
|
1228 |
if (kc_distance(&kd3->items[e].k, &use) < kd3->xradius[e])
|
954 | 1229 |
*new_data = e;
|
955 | 1230 |
else
|
956 | |
*new_data = kd3_closest_transformed(kd3, usexf.a);
|
|
1231 |
*new_data = kd3_closest_transformed(kd3, &use);
|
957 | 1232 |
histogram[*new_data]++;
|
958 | 1233 |
|
959 | 1234 |
/* calculate and propagate the error between desired and selected color.
|
|
961 | 1236 |
image artifacts caused by error accumulation (the fact that the
|
962 | 1237 |
error terms might not sum to the error). */
|
963 | 1238 |
for (k = 0; k < 3; ++k) {
|
964 | |
e = (use.a[k] - kd3->items[*new_data].a[k]) * DITHER_ITEM2ERR;
|
|
1239 |
e = (use.a[k] - kd3->items[*new_data].k.a[k]) * DITHER_ITEM2ERR;
|
965 | 1240 |
if (e) {
|
966 | 1241 |
err [x+d0].a[k] += ((e * 7) & ~15) / 16;
|
967 | 1242 |
err1[x+d1].a[k] += ((e * 3) & ~15) / 16;
|
|
980 | 1255 |
|
981 | 1256 |
/* change dithering directions */
|
982 | 1257 |
{
|
983 | |
color_erritem *temp = err1;
|
|
1258 |
kcolor *temp = err1;
|
984 | 1259 |
err1 = err;
|
985 | 1260 |
err = temp;
|
986 | 1261 |
dither_direction = !dither_direction;
|
|
993 | 1268 |
}
|
994 | 1269 |
|
995 | 1270 |
|
996 | |
/* return value 1 means run the image_changer again */
|
|
1271 |
typedef struct odselect_planitem {
|
|
1272 |
uint8_t plan;
|
|
1273 |
uint16_t frac;
|
|
1274 |
} odselect_planitem;
|
|
1275 |
|
|
1276 |
static int* ordered_dither_lum;
|
|
1277 |
|
|
1278 |
static void plan_from_cplan(uint8_t* plan, int nplan,
|
|
1279 |
const odselect_planitem* cp, int ncp, int whole) {
|
|
1280 |
int i, cfrac_subt = 0, planpos = 0, end_planpos;
|
|
1281 |
for (i = 0; i != ncp; ++i) {
|
|
1282 |
cfrac_subt += cp[i].frac;
|
|
1283 |
end_planpos = cfrac_subt * nplan / whole;
|
|
1284 |
while (planpos != end_planpos)
|
|
1285 |
plan[planpos++] = cp[i].plan;
|
|
1286 |
}
|
|
1287 |
assert(planpos == nplan);
|
|
1288 |
}
|
|
1289 |
|
|
1290 |
static int ordered_dither_plan_compare(const void* xa, const void* xb) {
|
|
1291 |
const uint8_t* a = (const uint8_t*) xa;
|
|
1292 |
const uint8_t* b = (const uint8_t*) xb;
|
|
1293 |
if (ordered_dither_lum[*a] != ordered_dither_lum[*b])
|
|
1294 |
return ordered_dither_lum[*a] - ordered_dither_lum[*b];
|
|
1295 |
else
|
|
1296 |
return *a - *b;
|
|
1297 |
}
|
|
1298 |
|
|
1299 |
static int kc_line_closest(const kcolor* p0, const kcolor* p1,
|
|
1300 |
const kcolor* ref, double* t, unsigned* dist) {
|
|
1301 |
kcolor p01, p0ref;
|
|
1302 |
unsigned den;
|
|
1303 |
int d;
|
|
1304 |
for (d = 0; d != 3; ++d) {
|
|
1305 |
p01.a[d] = p1->a[d] - p0->a[d];
|
|
1306 |
p0ref.a[d] = ref->a[d] - p0->a[d];
|
|
1307 |
}
|
|
1308 |
den = (unsigned)
|
|
1309 |
(p01.a[0]*p01.a[0] + p01.a[1]*p01.a[1] + p01.a[2]*p01.a[2]);
|
|
1310 |
if (den == 0)
|
|
1311 |
return 0;
|
|
1312 |
/* NB: We've run out of bits of precision. We can calculate the
|
|
1313 |
denominator in unsigned arithmetic, but the numerator might
|
|
1314 |
be negative, or it might be so large that it is unsigned.
|
|
1315 |
Calculate the numerator as a double. */
|
|
1316 |
*t = ((double) p01.a[0]*p0ref.a[0] + p01.a[1]*p0ref.a[1]
|
|
1317 |
+ p01.a[2]*p0ref.a[2]) / den;
|
|
1318 |
if (*t < 0 || *t > 1)
|
|
1319 |
return 0;
|
|
1320 |
for (d = 0; d != 3; ++d)
|
|
1321 |
p01.a[d] = (int) (p01.a[d] * *t) + p0->a[d];
|
|
1322 |
*dist = kc_distance(&p01, ref);
|
|
1323 |
return 1;
|
|
1324 |
}
|
|
1325 |
|
|
1326 |
static int kc_plane_closest(const kcolor* p0, const kcolor* p1,
|
|
1327 |
const kcolor* p2, const kcolor* ref,
|
|
1328 |
double* t, unsigned* dist) {
|
|
1329 |
kcolor p0ref, p01, p02;
|
|
1330 |
double n[3], pvec[3], det, qvec[3], u, v;
|
|
1331 |
int d;
|
|
1332 |
|
|
1333 |
/* Calculate the non-unit normal of the plane determined by the input
|
|
1334 |
colors (p0-p2) */
|
|
1335 |
for (d = 0; d != 3; ++d) {
|
|
1336 |
p0ref.a[d] = ref->a[d] - p0->a[d];
|
|
1337 |
p01.a[d] = p1->a[d] - p0->a[d];
|
|
1338 |
p02.a[d] = p2->a[d] - p0->a[d];
|
|
1339 |
}
|
|
1340 |
n[0] = p01.a[1]*p02.a[2] - p01.a[2]*p02.a[1];
|
|
1341 |
n[1] = p01.a[2]*p02.a[0] - p01.a[0]*p02.a[2];
|
|
1342 |
n[2] = p01.a[0]*p02.a[1] - p01.a[1]*p02.a[0];
|
|
1343 |
|
|
1344 |
/* Moeller-Trumbore ray tracing algorithm: trace a ray from `ref` along
|
|
1345 |
normal `n`; convert to barycentric coordinates to see if the ray
|
|
1346 |
intersects with the triangle. */
|
|
1347 |
pvec[0] = n[1]*p02.a[2] - n[2]*p02.a[1];
|
|
1348 |
pvec[1] = n[2]*p02.a[0] - n[0]*p02.a[2];
|
|
1349 |
pvec[2] = n[0]*p02.a[1] - n[1]*p02.a[0];
|
|
1350 |
|
|
1351 |
det = pvec[0]*p01.a[0] + pvec[1]*p01.a[1] + pvec[2]*p01.a[2];
|
|
1352 |
if (det > 0.0001 && det < 0.0001)
|
|
1353 |
return 0;
|
|
1354 |
det = 1 / det;
|
|
1355 |
|
|
1356 |
u = (p0ref.a[0]*pvec[0] + p0ref.a[1]*pvec[1] + p0ref.a[2]*pvec[2]) * det;
|
|
1357 |
if (u < 0 || u > 1)
|
|
1358 |
return 0;
|
|
1359 |
|
|
1360 |
qvec[0] = p0ref.a[1]*p01.a[2] - p0ref.a[2]*p01.a[1];
|
|
1361 |
qvec[1] = p0ref.a[2]*p01.a[0] - p0ref.a[0]*p01.a[2];
|
|
1362 |
qvec[2] = p0ref.a[0]*p01.a[1] - p0ref.a[1]*p01.a[0];
|
|
1363 |
|
|
1364 |
v = (n[0]*qvec[0] + n[1]*qvec[1] + n[2]*qvec[2]) * det;
|
|
1365 |
if (v < 0 || v > 1 || u + v > 1)
|
|
1366 |
return 0;
|
|
1367 |
|
|
1368 |
/* Now we know at there is a point in the triangle that is closer to
|
|
1369 |
`ref` than any point along its edges. Return the barycentric
|
|
1370 |
coordinates for that point and the distance to that point. */
|
|
1371 |
t[0] = u;
|
|
1372 |
t[1] = v;
|
|
1373 |
v = (p02.a[0]*qvec[0] + p02.a[1]*qvec[1] + p02.a[2]*qvec[2]) * det;
|
|
1374 |
*dist = (unsigned) (v * v * (n[0]*n[0] + n[1]*n[1] + n[2]*n[2]) + 0.5);
|
|
1375 |
return 1;
|
|
1376 |
}
|
|
1377 |
|
|
1378 |
static void limit_ordered_dither_plan(uint8_t* plan, int nplan, int nc,
|
|
1379 |
const kcolor* want, kd3_tree* kd3) {
|
|
1380 |
unsigned mindist, dist;
|
|
1381 |
int ncp = 0, nbestcp = 0, i, j, k;
|
|
1382 |
double t[2];
|
|
1383 |
odselect_planitem cp[256], bestcp[16];
|
|
1384 |
nc = nc <= 16 ? nc : 16;
|
|
1385 |
|
|
1386 |
/* sort colors */
|
|
1387 |
cp[0].plan = plan[0];
|
|
1388 |
cp[0].frac = 1;
|
|
1389 |
for (ncp = i = 1; i != nplan; ++i)
|
|
1390 |
if (plan[i - 1] == plan[i])
|
|
1391 |
++cp[ncp - 1].frac;
|
|
1392 |
else {
|
|
1393 |
cp[ncp].plan = plan[i];
|
|
1394 |
cp[ncp].frac = 1;
|
|
1395 |
++ncp;
|
|
1396 |
}
|
|
1397 |
|
|
1398 |
/* calculate plan */
|
|
1399 |
mindist = (unsigned) -1;
|
|
1400 |
for (i = 0; i != ncp; ++i) {
|
|
1401 |
/* check for closest single color */
|
|
1402 |
dist = kc_distance(&kd3->items[cp[i].plan].k, want);
|
|
1403 |
if (dist < mindist) {
|
|
1404 |
bestcp[0].plan = cp[i].plan;
|
|
1405 |
bestcp[0].frac = KC_WHOLE;
|
|
1406 |
nbestcp = 1;
|
|
1407 |
mindist = dist;
|
|
1408 |
}
|
|
1409 |
|
|
1410 |
for (j = i + 1; nc >= 2 && j < ncp; ++j) {
|
|
1411 |
/* check for closest blend of two colors */
|
|
1412 |
if (kc_line_closest(&kd3->items[cp[i].plan].k,
|
|
1413 |
&kd3->items[cp[j].plan].k,
|
|
1414 |
want, &t[0], &dist)
|
|
1415 |
&& dist < mindist) {
|
|
1416 |
bestcp[0].plan = cp[i].plan;
|
|
1417 |
bestcp[1].plan = cp[j].plan;
|
|
1418 |
bestcp[1].frac = (int) (KC_WHOLE * t[0]);
|
|
1419 |
bestcp[0].frac = KC_WHOLE - bestcp[1].frac;
|
|
1420 |
nbestcp = 2;
|
|
1421 |
mindist = dist;
|
|
1422 |
}
|
|
1423 |
|
|
1424 |
for (k = j + 1; nc >= 3 && k < ncp; ++k)
|
|
1425 |
/* check for closest blend of three colors */
|
|
1426 |
if (kc_plane_closest(&kd3->items[cp[i].plan].k,
|
|
1427 |
&kd3->items[cp[j].plan].k,
|
|
1428 |
&kd3->items[cp[k].plan].k,
|
|
1429 |
want, &t[0], &dist)
|
|
1430 |
&& dist < mindist) {
|
|
1431 |
bestcp[0].plan = cp[i].plan;
|
|
1432 |
bestcp[1].plan = cp[j].plan;
|
|
1433 |
bestcp[1].frac = (int) (KC_WHOLE * t[0]);
|
|
1434 |
bestcp[2].plan = cp[k].plan;
|
|
1435 |
bestcp[2].frac = (int) (KC_WHOLE * t[1]);
|
|
1436 |
bestcp[0].frac = KC_WHOLE - bestcp[1].frac - bestcp[2].frac;
|
|
1437 |
nbestcp = 3;
|
|
1438 |
mindist = dist;
|
|
1439 |
}
|
|
1440 |
}
|
|
1441 |
}
|
|
1442 |
|
|
1443 |
plan_from_cplan(plan, nplan, bestcp, nbestcp, KC_WHOLE);
|
|
1444 |
}
|
|
1445 |
|
|
1446 |
static void set_ordered_dither_plan(uint8_t* plan, int nplan, int nc,
|
|
1447 |
Gif_Color* gfc, kd3_tree* kd3) {
|
|
1448 |
kcolor want, cur, err;
|
|
1449 |
int i, d;
|
|
1450 |
|
|
1451 |
kc_set8g(&want, gfc->gfc_red, gfc->gfc_green, gfc->gfc_blue);
|
|
1452 |
if (kd3->transform)
|
|
1453 |
kd3->transform(&want);
|
|
1454 |
|
|
1455 |
kc_clear(&err);
|
|
1456 |
for (i = 0; i != nplan; ++i) {
|
|
1457 |
for (d = 0; d != 3; ++d)
|
|
1458 |
cur.a[d] = want.a[d] + err.a[d];
|
|
1459 |
kc_clamp(&cur);
|
|
1460 |
plan[i] = kd3_closest_transformed(kd3, &cur);
|
|
1461 |
for (d = 0; d != 3; ++d)
|
|
1462 |
err.a[d] += want.a[d] - kd3->items[plan[i]].k.a[d];
|
|
1463 |
}
|
|
1464 |
|
|
1465 |
qsort(plan, nplan, 1, ordered_dither_plan_compare);
|
|
1466 |
|
|
1467 |
if (nc < nplan && plan[0] != plan[nplan-1]) {
|
|
1468 |
int ncp = 1;
|
|
1469 |
for (i = 1; i != nplan; ++i)
|
|
1470 |
ncp += plan[i-1] != plan[i];
|
|
1471 |
if (ncp > nc)
|
|
1472 |
limit_ordered_dither_plan(plan, nplan, nc, &want, kd3);
|
|
1473 |
}
|
|
1474 |
|
|
1475 |
gfc->haspixel = 1;
|
|
1476 |
}
|
|
1477 |
|
|
1478 |
static void pow2_ordered_dither(Gif_Image* gfi, uint8_t* all_new_data,
|
|
1479 |
Gif_Colormap* old_cm, kd3_tree* kd3,
|
|
1480 |
uint32_t* histogram, const uint8_t* matrix,
|
|
1481 |
uint8_t* plan) {
|
|
1482 |
int mws, nplans, i, x, y;
|
|
1483 |
for (mws = 0; (1 << mws) != matrix[0]; ++mws)
|
|
1484 |
/* nada */;
|
|
1485 |
for (nplans = 0; (1 << nplans) != matrix[2]; ++nplans)
|
|
1486 |
/* nada */;
|
|
1487 |
|
|
1488 |
for (y = 0; y != gfi->height; ++y) {
|
|
1489 |
uint8_t *data, *new_data, *thisplan;
|
|
1490 |
data = gfi->img[y];
|
|
1491 |
new_data = all_new_data + y * gfi->width;
|
|
1492 |
|
|
1493 |
for (x = 0; x != gfi->width; ++x)
|
|
1494 |
/* the transparent color never gets adjusted */
|
|
1495 |
if (data[x] != gfi->transparent) {
|
|
1496 |
thisplan = &plan[data[x] << nplans];
|
|
1497 |
if (!old_cm->col[data[x]].haspixel)
|
|
1498 |
set_ordered_dither_plan(thisplan, 1 << nplans, matrix[3],
|
|
1499 |
&old_cm->col[data[x]], kd3);
|
|
1500 |
i = matrix[4 + ((x + gfi->left) & (matrix[0] - 1))
|
|
1501 |
+ (((y + gfi->top) & (matrix[1] - 1)) << mws)];
|
|
1502 |
new_data[x] = thisplan[i];
|
|
1503 |
histogram[new_data[x]]++;
|
|
1504 |
}
|
|
1505 |
}
|
|
1506 |
}
|
|
1507 |
|
|
1508 |
static void colormap_image_ordered(Gif_Image* gfi, uint8_t* all_new_data,
|
|
1509 |
Gif_Colormap* old_cm, kd3_tree* kd3,
|
|
1510 |
uint32_t* histogram,
|
|
1511 |
const uint8_t* matrix) {
|
|
1512 |
int mw = matrix[0], mh = matrix[1], nplan = matrix[2];
|
|
1513 |
uint8_t* plan = Gif_NewArray(uint8_t, nplan * old_cm->ncol);
|
|
1514 |
int i, x, y;
|
|
1515 |
|
|
1516 |
/* Written with reference to Joel Ylilouma's versions. */
|
|
1517 |
|
|
1518 |
/* Initialize colors */
|
|
1519 |
for (i = 0; i != old_cm->ncol; ++i)
|
|
1520 |
old_cm->col[i].haspixel = 0;
|
|
1521 |
|
|
1522 |
/* Initialize luminances, create luminance sorter */
|
|
1523 |
ordered_dither_lum = Gif_NewArray(int, kd3->nitems);
|
|
1524 |
for (i = 0; i != kd3->nitems; ++i)
|
|
1525 |
ordered_dither_lum[i] = kc_luminance(&kd3->items[i].k);
|
|
1526 |
|
|
1527 |
/* Do the image! */
|
|
1528 |
if ((mw & (mw - 1)) == 0 && (mh & (mh - 1)) == 0
|
|
1529 |
&& (nplan & (nplan - 1)) == 0)
|
|
1530 |
pow2_ordered_dither(gfi, all_new_data, old_cm, kd3, histogram,
|
|
1531 |
matrix, plan);
|
|
1532 |
else
|
|
1533 |
for (y = 0; y != gfi->height; ++y) {
|
|
1534 |
uint8_t *data, *new_data, *thisplan;
|
|
1535 |
data = gfi->img[y];
|
|
1536 |
new_data = all_new_data + y * gfi->width;
|
|
1537 |
|
|
1538 |
for (x = 0; x != gfi->width; ++x)
|
|
1539 |
/* the transparent color never gets adjusted */
|
|
1540 |
if (data[x] != gfi->transparent) {
|
|
1541 |
thisplan = &plan[nplan * data[x]];
|
|
1542 |
if (!old_cm->col[data[x]].haspixel)
|
|
1543 |
set_ordered_dither_plan(thisplan, nplan, matrix[3],
|
|
1544 |
&old_cm->col[data[x]], kd3);
|
|
1545 |
i = matrix[4 + (x + gfi->left) % mw
|
|
1546 |
+ ((y + gfi->top) % mh) * mw];
|
|
1547 |
new_data[x] = thisplan[i];
|
|
1548 |
histogram[new_data[x]]++;
|
|
1549 |
}
|
|
1550 |
}
|
|
1551 |
|
|
1552 |
/* delete temporary storage */
|
|
1553 |
Gif_DeleteArray(ordered_dither_lum);
|
|
1554 |
Gif_DeleteArray(plan);
|
|
1555 |
}
|
|
1556 |
|
|
1557 |
|
|
1558 |
static void dither(Gif_Image* gfi, uint8_t* new_data, Gif_Colormap* old_cm,
|
|
1559 |
kd3_tree* kd3, uint32_t* histogram, Gt_OutputData* od) {
|
|
1560 |
if (od->dither_type == dither_default
|
|
1561 |
|| od->dither_type == dither_floyd_steinberg)
|
|
1562 |
colormap_image_floyd_steinberg(gfi, new_data, old_cm, kd3, histogram);
|
|
1563 |
else if (od->dither_type == dither_ordered
|
|
1564 |
|| od->dither_type == dither_ordered_new)
|
|
1565 |
colormap_image_ordered(gfi, new_data, old_cm, kd3, histogram,
|
|
1566 |
od->dither_data);
|
|
1567 |
else
|
|
1568 |
colormap_image_posterize(gfi, new_data, old_cm, kd3, histogram);
|
|
1569 |
}
|
|
1570 |
|
|
1571 |
/* return value 1 means run the dither again */
|
997 | 1572 |
static int
|
998 | 1573 |
try_assign_transparency(Gif_Image *gfi, Gif_Colormap *old_cm, uint8_t *new_data,
|
999 | 1574 |
Gif_Colormap *new_cm, int *new_ncol,
|
|
1058 | 1633 |
}
|
1059 | 1634 |
|
1060 | 1635 |
void
|
1061 | |
colormap_stream(Gif_Stream *gfs, Gif_Colormap *new_cm,
|
1062 | |
colormap_image_func image_changer)
|
|
1636 |
colormap_stream(Gif_Stream* gfs, Gif_Colormap* new_cm, Gt_OutputData* od)
|
1063 | 1637 |
{
|
1064 | 1638 |
kd3_tree kd3;
|
1065 | 1639 |
int background_transparent = gfs->images[0]->transparent >= 0;
|
|
1089 | 1663 |
|| new_col[j].gfc_red != new_col[j].gfc_blue)
|
1090 | 1664 |
new_gray = 0;
|
1091 | 1665 |
if (new_gray)
|
1092 | |
kd3_init(&kd3, kd3_distance, kd3_luminance_transform);
|
|
1666 |
kd3_init(&kd3, kc_luminance_transform);
|
1093 | 1667 |
else
|
1094 | |
kd3_init(&kd3, kd3_distance, NULL);
|
|
1668 |
kd3_init(&kd3, NULL);
|
1095 | 1669 |
for (j = 0; j < new_cm->ncol; ++j)
|
1096 | |
kd3_add_8to15(&kd3, new_col[j].gfc_red, new_col[j].gfc_green, new_col[j].gfc_blue);
|
|
1670 |
kd3_add8g(&kd3, new_col[j].gfc_red, new_col[j].gfc_green,
|
|
1671 |
new_col[j].gfc_blue);
|
1097 | 1672 |
kd3_build(&kd3);
|
1098 | 1673 |
|
1099 | 1674 |
for (imagei = 0; imagei < gfs->nimages; imagei++) {
|
|
1115 | 1690 |
do {
|
1116 | 1691 |
for (j = 0; j < 256; j++)
|
1117 | 1692 |
histogram[j] = 0;
|
1118 | |
image_changer(gfi, new_data, gfcm, &kd3, histogram);
|
|
1693 |
dither(gfi, new_data, gfcm, &kd3, histogram, od);
|
1119 | 1694 |
} while (try_assign_transparency(gfi, gfcm, new_data, new_cm, &new_ncol,
|
1120 | 1695 |
&kd3, histogram));
|
1121 | 1696 |
|
|
1160 | 1735 |
gfs->background = gfs->images[0]->transparent;
|
1161 | 1736 |
else if (gfs->global && gfs->background < gfs->global->ncol) {
|
1162 | 1737 |
Gif_Color *c = &gfs->global->col[gfs->background];
|
1163 | |
gfs->background = kd3_closest_8to15(&kd3, c->gfc_red, c->gfc_green, c->gfc_blue);
|
|
1738 |
gfs->background = kd3_closest8(&kd3, c->gfc_red, c->gfc_green, c->gfc_blue);
|
1164 | 1739 |
new_col[gfs->background].pixel++;
|
1165 | 1740 |
}
|
1166 | 1741 |
|
|
1226 | 1801 |
}
|
1227 | 1802 |
}
|
1228 | 1803 |
}
|
|
1804 |
|
|
1805 |
|
|
1806 |
/* Halftone algorithms */
|
|
1807 |
|
|
1808 |
static const uint8_t dither_matrix_o3x3[4 + 3*3] = {
|
|
1809 |
3, 3, 9, 9,
|
|
1810 |
2, 6, 3,
|
|
1811 |
5, 0, 8,
|
|
1812 |
1, 7, 4
|
|
1813 |
};
|
|
1814 |
|
|
1815 |
static const uint8_t dither_matrix_o4x4[4 + 4*4] = {
|
|
1816 |
4, 4, 16, 16,
|
|
1817 |
0, 8, 3, 10,
|
|
1818 |
12, 4, 14, 6,
|
|
1819 |
2, 11, 1, 9,
|
|
1820 |
15, 7, 13, 5
|
|
1821 |
};
|
|
1822 |
|
|
1823 |
static const uint8_t dither_matrix_o8x8[4 + 8*8] = {
|
|
1824 |
8, 8, 64, 64,
|
|
1825 |
0, 48, 12, 60, 3, 51, 15, 63,
|
|
1826 |
32, 16, 44, 28, 35, 19, 47, 31,
|
|
1827 |
8, 56, 4, 52, 11, 59, 7, 55,
|
|
1828 |
40, 24, 36, 20, 43, 27, 39, 23,
|
|
1829 |
2, 50, 14, 62, 1, 49, 13, 61,
|
|
1830 |
34, 18, 46, 30, 33, 17, 45, 29,
|
|
1831 |
10, 58, 6, 54, 9, 57, 5, 53,
|
|
1832 |
42, 26, 38, 22, 41, 25, 37, 21
|
|
1833 |
};
|
|
1834 |
|
|
1835 |
static const uint8_t dither_matrix_ro64x64[4 + 64*64] = {
|
|
1836 |
64, 64, 16, 16,
|
|
1837 |
6, 15, 2, 15, 2, 14, 1, 13, 2, 14, 5, 13, 0, 14, 0, 9, 6, 10, 7, 13, 6, 13, 3, 10, 5, 15, 4, 11, 0, 11, 6, 10, 7, 12, 7, 13, 0, 9, 6, 15, 6, 10, 0, 15, 1, 15, 0, 8, 0, 15, 6, 15, 7, 15, 7, 9, 1, 15, 3, 8, 1, 8, 0, 14,
|
|
1838 |
9, 3, 10, 5, 10, 6, 10, 5, 9, 6, 9, 2, 9, 4, 13, 4, 13, 3, 8, 3, 10, 1, 13, 6, 11, 1, 12, 3, 14, 5, 15, 3, 8, 3, 8, 3, 12, 4, 11, 3, 13, 3, 8, 4, 9, 6, 12, 4, 11, 6, 11, 3, 10, 0, 12, 1, 11, 7, 12, 4, 12, 4, 11, 5,
|
|
1839 |
1, 14, 0, 10, 2, 9, 2, 11, 1, 8, 1, 8, 3, 9, 4, 15, 7, 13, 7, 14, 7, 14, 0, 10, 0, 14, 7, 9, 0, 11, 1, 15, 0, 11, 0, 11, 3, 15, 7, 14, 6, 10, 5, 8, 0, 11, 0, 8, 7, 11, 0, 15, 0, 12, 1, 13, 6, 9, 0, 15, 4, 9, 1, 8,
|
|
1840 |
10, 5, 15, 6, 13, 6, 14, 7, 14, 4, 12, 5, 15, 6, 10, 2, 11, 3, 10, 3, 11, 3, 13, 6, 11, 5, 14, 3, 14, 6, 8, 5, 14, 5, 14, 7, 10, 7, 11, 3, 13, 3, 13, 2, 14, 4, 15, 5, 15, 3, 8, 4, 11, 4, 9, 5, 12, 3, 8, 5, 15, 2, 13, 5,
|
|
1841 |
3, 10, 2, 9, 5, 15, 4, 9, 0, 11, 7, 11, 0, 14, 5, 11, 5, 14, 7, 15, 6, 9, 0, 9, 0, 8, 4, 14, 6, 12, 0, 11, 4, 15, 5, 8, 6, 10, 6, 11, 6, 10, 3, 12, 5, 9, 6, 9, 5, 12, 6, 12, 7, 14, 0, 14, 2, 15, 5, 8, 2, 10, 3, 9,
|
|
1842 |
14, 7, 14, 7, 8, 1, 12, 2, 14, 4, 15, 3, 11, 5, 12, 2, 8, 0, 10, 3, 12, 1, 13, 5, 14, 5, 11, 0, 11, 3, 13, 7, 8, 1, 12, 3, 15, 3, 14, 2, 15, 3, 11, 6, 15, 1, 12, 1, 9, 3, 9, 3, 11, 3, 11, 7, 11, 5, 12, 0, 14, 6, 13, 6,
|
|
1843 |
5, 10, 6, 12, 3, 15, 3, 8, 7, 10, 0, 15, 7, 10, 5, 8, 1, 14, 5, 10, 7, 14, 2, 14, 0, 14, 1, 8, 3, 8, 5, 10, 5, 15, 0, 8, 6, 14, 0, 8, 2, 15, 4, 11, 6, 10, 0, 8, 5, 9, 4, 14, 1, 15, 3, 10, 1, 15, 0, 15, 5, 8, 7, 13,
|
|
1844 |
15, 3, 8, 0, 9, 6, 12, 6, 15, 0, 11, 4, 14, 3, 15, 3, 10, 5, 12, 3, 10, 3, 9, 6, 9, 5, 13, 5, 15, 6, 14, 0, 9, 0, 12, 4, 11, 3, 15, 4, 11, 6, 15, 2, 15, 3, 14, 4, 13, 2, 11, 1, 8, 5, 12, 7, 9, 4, 8, 5, 12, 2, 11, 0,
|
|
1845 |
0, 13, 5, 11, 5, 14, 5, 15, 7, 9, 5, 8, 2, 10, 3, 15, 6, 9, 3, 12, 5, 11, 6, 14, 3, 13, 6, 13, 0, 9, 1, 11, 3, 8, 3, 9, 4, 9, 6, 14, 7, 8, 6, 12, 7, 13, 6, 9, 5, 13, 3, 15, 5, 14, 3, 15, 4, 12, 4, 12, 2, 13, 0, 15,
|
|
1846 |
10, 7, 14, 1, 8, 2, 10, 2, 12, 0, 13, 1, 13, 5, 11, 6, 12, 3, 9, 6, 15, 1, 9, 1, 10, 6, 10, 3, 15, 6, 15, 5, 15, 7, 12, 6, 12, 0, 9, 3, 12, 3, 11, 3, 8, 3, 13, 2, 9, 3, 9, 6, 10, 2, 11, 6, 9, 0, 9, 1, 9, 6, 10, 4,
|
|
1847 |
3, 10, 4, 9, 4, 9, 6, 13, 3, 15, 0, 8, 4, 10, 0, 14, 5, 15, 7, 15, 2, 12, 7, 10, 5, 15, 7, 8, 0, 9, 0, 8, 6, 14, 4, 15, 2, 15, 1, 15, 0, 9, 5, 14, 0, 12, 7, 10, 6, 8, 6, 11, 0, 13, 7, 14, 1, 13, 2, 10, 5, 9, 0, 14,
|
|
1848 |
14, 6, 13, 1, 12, 3, 8, 3, 9, 6, 12, 4, 15, 2, 8, 5, 9, 1, 10, 1, 8, 5, 15, 0, 8, 2, 12, 3, 12, 5, 12, 5, 11, 2, 10, 2, 8, 5, 10, 5, 12, 4, 9, 3, 8, 4, 13, 1, 15, 0, 12, 3, 8, 4, 10, 3, 9, 5, 13, 5, 13, 3, 11, 4,
|
|
1849 |
4, 12, 3, 10, 6, 9, 0, 14, 0, 8, 1, 9, 1, 9, 6, 15, 0, 10, 2, 9, 6, 10, 7, 10, 2, 14, 4, 8, 0, 13, 4, 8, 1, 11, 6, 14, 7, 14, 0, 8, 4, 14, 7, 14, 4, 9, 2, 12, 0, 12, 3, 8, 5, 9, 6, 14, 0, 9, 2, 14, 5, 10, 1, 15,
|
|
1850 |
10, 2, 15, 6, 14, 3, 11, 6, 15, 4, 15, 4, 13, 5, 11, 3, 12, 4, 14, 5, 12, 0, 13, 3, 8, 6, 15, 2, 10, 6, 13, 2, 12, 6, 8, 3, 10, 3, 14, 5, 8, 0, 10, 3, 13, 2, 9, 6, 10, 6, 15, 4, 13, 2, 8, 3, 14, 6, 11, 7, 13, 0, 10, 5,
|
|
1851 |
2, 13, 1, 9, 5, 10, 3, 15, 6, 10, 0, 15, 4, 15, 4, 15, 6, 9, 0, 14, 1, 9, 6, 12, 0, 11, 5, 9, 0, 14, 5, 14, 6, 9, 3, 11, 7, 11, 1, 15, 1, 9, 3, 13, 5, 12, 0, 9, 2, 9, 6, 13, 2, 9, 6, 13, 7, 11, 7, 8, 0, 13, 3, 9,
|
|
1852 |
9, 6, 13, 6, 13, 0, 8, 5, 14, 2, 11, 7, 11, 2, 11, 2, 12, 3, 11, 4, 13, 4, 10, 3, 15, 5, 13, 0, 11, 6, 10, 0, 13, 3, 15, 7, 14, 3, 8, 4, 12, 6, 9, 6, 10, 3, 14, 5, 13, 5, 11, 3, 13, 6, 8, 2, 14, 3, 15, 3, 11, 4, 12, 6,
|
|
1853 |
4, 10, 7, 11, 1, 10, 2, 14, 7, 14, 0, 14, 4, 15, 0, 8, 0, 15, 5, 15, 7, 11, 7, 14, 6, 12, 0, 8, 1, 15, 1, 14, 2, 8, 0, 11, 4, 8, 5, 11, 1, 15, 0, 12, 7, 14, 0, 11, 4, 8, 2, 8, 0, 14, 2, 14, 4, 9, 4, 10, 7, 15, 5, 14,
|
|
1854 |
14, 0, 13, 2, 14, 5, 11, 5, 10, 2, 10, 6, 11, 3, 12, 5, 11, 7, 11, 1, 12, 3, 11, 3, 9, 3, 12, 4, 11, 6, 9, 5, 14, 5, 12, 7, 14, 2, 12, 1, 8, 4, 11, 7, 10, 3, 14, 4, 14, 0, 14, 5, 11, 7, 11, 5, 15, 1, 14, 0, 10, 3, 10, 0,
|
|
1855 |
5, 12, 7, 10, 7, 11, 0, 14, 5, 14, 1, 14, 3, 8, 0, 15, 0, 9, 5, 9, 1, 8, 6, 15, 6, 15, 7, 13, 7, 9, 4, 11, 0, 10, 6, 10, 7, 12, 7, 15, 0, 9, 7, 14, 2, 11, 0, 12, 7, 11, 7, 13, 0, 9, 6, 14, 2, 11, 1, 15, 0, 8, 0, 10,
|
|
1856 |
10, 3, 15, 3, 15, 3, 8, 4, 11, 3, 11, 6, 15, 6, 11, 5, 12, 5, 13, 0, 15, 4, 8, 1, 11, 3, 10, 3, 15, 3, 14, 1, 14, 6, 13, 3, 11, 3, 11, 3, 13, 6, 8, 3, 12, 5, 8, 4, 13, 3, 8, 2, 13, 4, 8, 3, 14, 7, 8, 5, 14, 4, 15, 5,
|
|
1857 |
3, 9, 0, 10, 0, 12, 3, 11, 7, 10, 6, 8, 7, 12, 0, 11, 1, 8, 5, 12, 5, 12, 7, 11, 2, 14, 0, 10, 0, 12, 0, 15, 2, 9, 5, 9, 0, 15, 4, 14, 6, 9, 7, 11, 6, 10, 0, 9, 1, 9, 0, 14, 4, 12, 5, 9, 6, 10, 4, 11, 6, 12, 5, 12,
|
|
1858 |
12, 6, 15, 4, 8, 4, 15, 7, 15, 3, 14, 0, 9, 3, 13, 5, 14, 5, 11, 1, 9, 0, 15, 3, 8, 7, 12, 7, 8, 5, 9, 4, 12, 6, 12, 1, 10, 6, 9, 0, 15, 3, 14, 0, 15, 3, 13, 5, 15, 5, 9, 6, 10, 0, 14, 0, 13, 1, 15, 1, 8, 3, 8, 1,
|
|
1859 |
2, 8, 7, 15, 0, 11, 1, 12, 1, 12, 0, 15, 0, 11, 2, 13, 1, 10, 5, 8, 7, 13, 6, 10, 0, 11, 4, 12, 7, 10, 1, 9, 1, 9, 0, 12, 2, 11, 7, 15, 1, 11, 0, 9, 2, 8, 4, 12, 2, 12, 6, 14, 4, 14, 3, 13, 3, 15, 1, 14, 4, 15, 6, 12,
|
|
1860 |
13, 6, 11, 3, 12, 4, 8, 4, 9, 4, 9, 5, 12, 6, 10, 5, 14, 7, 14, 3, 10, 2, 13, 3, 14, 5, 8, 2, 13, 1, 15, 6, 15, 4, 8, 4, 15, 5, 10, 1, 14, 5, 12, 4, 13, 5, 11, 1, 9, 5, 9, 3, 9, 0, 10, 7, 10, 7, 11, 6, 10, 3, 9, 0,
|
|
1861 |
3, 15, 5, 8, 1, 9, 5, 8, 4, 13, 6, 8, 4, 15, 6, 10, 6, 14, 7, 11, 0, 9, 1, 12, 0, 8, 6, 12, 6, 10, 3, 12, 7, 13, 7, 11, 3, 8, 0, 13, 7, 12, 0, 15, 4, 12, 5, 8, 4, 15, 5, 10, 0, 15, 3, 15, 1, 8, 1, 15, 4, 14, 6, 15,
|
|
1862 |
10, 6, 14, 0, 13, 4, 13, 1, 9, 1, 13, 3, 11, 2, 12, 1, 8, 3, 12, 3, 14, 6, 8, 4, 15, 4, 8, 3, 15, 1, 9, 6, 11, 2, 14, 3, 13, 7, 10, 4, 10, 3, 8, 7, 11, 2, 12, 1, 11, 1, 14, 3, 9, 6, 9, 7, 13, 5, 11, 7, 10, 2, 9, 3,
|
|
1863 |
1, 9, 0, 14, 7, 12, 7, 14, 5, 9, 6, 10, 6, 14, 6, 15, 1, 14, 7, 10, 3, 12, 0, 14, 5, 9, 6, 8, 1, 12, 0, 13, 2, 8, 4, 9, 6, 15, 0, 14, 4, 10, 7, 11, 5, 14, 2, 8, 4, 15, 3, 8, 7, 13, 1, 9, 0, 15, 0, 14, 5, 14, 0, 14,
|
|
1864 |
15, 4, 10, 6, 11, 1, 10, 2, 14, 3, 15, 1, 10, 3, 10, 3, 8, 4, 12, 3, 10, 6, 9, 6, 14, 2, 12, 3, 11, 4, 9, 6, 12, 5, 15, 0, 10, 3, 8, 4, 14, 1, 14, 3, 11, 2, 12, 7, 11, 2, 12, 6, 10, 1, 13, 4, 11, 4, 10, 5, 11, 2, 11, 7,
|
|
1865 |
0, 8, 1, 11, 4, 8, 4, 10, 6, 11, 3, 10, 6, 10, 6, 8, 1, 11, 1, 15, 7, 15, 6, 9, 1, 9, 7, 8, 0, 14, 5, 12, 3, 13, 4, 12, 0, 15, 5, 12, 1, 12, 7, 9, 6, 8, 3, 15, 0, 13, 6, 15, 7, 15, 7, 13, 2, 15, 1, 14, 2, 15, 0, 8,
|
|
1866 |
12, 5, 13, 5, 15, 1, 12, 1, 15, 1, 14, 6, 14, 2, 14, 2, 14, 6, 11, 6, 11, 3, 12, 2, 13, 6, 15, 3, 9, 5, 8, 2, 8, 5, 9, 1, 9, 4, 9, 2, 8, 4, 13, 1, 13, 0, 11, 6, 11, 6, 9, 2, 11, 3, 10, 1, 10, 6, 9, 6, 11, 7, 15, 5,
|
|
1867 |
5, 12, 0, 9, 5, 8, 5, 9, 0, 10, 2, 13, 4, 8, 5, 14, 2, 9, 2, 15, 4, 8, 2, 8, 0, 12, 6, 12, 0, 10, 5, 15, 4, 9, 3, 13, 0, 9, 4, 15, 1, 14, 1, 9, 0, 9, 2, 14, 6, 12, 0, 15, 7, 9, 2, 9, 0, 15, 6, 10, 3, 15, 3, 13,
|
|
1868 |
9, 1, 15, 5, 12, 3, 14, 0, 15, 7, 8, 5, 14, 1, 11, 1, 13, 6, 10, 7, 13, 0, 14, 5, 10, 7, 11, 3, 15, 6, 11, 1, 12, 2, 8, 4, 13, 5, 10, 2, 10, 6, 13, 5, 13, 4, 10, 5, 9, 3, 8, 4, 12, 1, 12, 5, 11, 6, 14, 3, 11, 7, 11, 7,
|
|
1869 |
2, 11, 4, 12, 7, 8, 3, 15, 3, 11, 4, 11, 7, 10, 1, 10, 2, 10, 0, 11, 4, 12, 0, 10, 1, 8, 7, 11, 1, 10, 1, 15, 7, 10, 2, 11, 1, 9, 6, 14, 2, 11, 1, 14, 4, 12, 2, 13, 1, 9, 5, 8, 7, 11, 7, 10, 7, 15, 4, 13, 3, 10, 1, 11,
|
|
1870 |
13, 5, 8, 0, 15, 3, 11, 6, 12, 7, 15, 3, 15, 1, 15, 4, 14, 5, 13, 6, 8, 1, 15, 6, 13, 5, 12, 2, 15, 4, 9, 4, 15, 3, 14, 6, 14, 5, 9, 3, 12, 6, 11, 5, 8, 2, 9, 5, 12, 5, 15, 1, 13, 3, 14, 2, 8, 0, 8, 0, 12, 7, 15, 5,
|
|
1871 |
0, 14, 1, 8, 2, 8, 2, 15, 2, 9, 6, 12, 0, 12, 7, 12, 7, 13, 0, 15, 6, 14, 5, 11, 1, 12, 5, 8, 5, 8, 1, 14, 0, 10, 4, 12, 6, 9, 6, 8, 0, 9, 0, 15, 4, 15, 5, 13, 1, 15, 5, 12, 7, 10, 6, 14, 4, 9, 6, 15, 0, 15, 7, 13,
|
|
1872 |
10, 4, 13, 5, 13, 5, 11, 5, 12, 5, 9, 3, 9, 4, 8, 3, 9, 3, 10, 6, 11, 1, 15, 2, 8, 5, 15, 1, 13, 2, 11, 6, 14, 6, 8, 2, 15, 3, 14, 3, 13, 6, 9, 6, 9, 0, 10, 2, 10, 7, 11, 1, 15, 3, 9, 3, 13, 3, 10, 3, 9, 4, 10, 1,
|
|
1873 |
4, 11, 7, 10, 2, 14, 4, 15, 3, 15, 2, 15, 7, 10, 2, 14, 1, 8, 0, 15, 2, 8, 1, 12, 2, 13, 1, 8, 7, 12, 6, 11, 1, 10, 4, 11, 2, 15, 0, 13, 0, 12, 7, 11, 5, 12, 7, 8, 6, 15, 4, 12, 7, 10, 6, 10, 0, 10, 0, 11, 4, 12, 7, 10,
|
|
1874 |
13, 2, 14, 2, 10, 7, 11, 1, 11, 7, 11, 7, 12, 3, 11, 7, 13, 4, 11, 6, 12, 5, 11, 6, 10, 7, 13, 4, 11, 3, 15, 3, 14, 4, 15, 3, 8, 5, 10, 7, 10, 6, 15, 3, 9, 3, 15, 1, 11, 3, 9, 0, 15, 1, 15, 3, 15, 4, 13, 5, 8, 3, 15, 3,
|
|
1875 |
3, 9, 1, 12, 4, 14, 0, 11, 7, 15, 1, 11, 3, 14, 0, 11, 5, 10, 2, 10, 2, 8, 6, 14, 7, 11, 0, 10, 7, 14, 7, 15, 6, 9, 6, 9, 1, 12, 7, 9, 4, 8, 7, 13, 0, 13, 6, 12, 6, 8, 1, 9, 1, 15, 4, 12, 0, 12, 4, 10, 0, 13, 0, 12,
|
|
1876 |
13, 5, 10, 6, 9, 1, 13, 6, 11, 0, 14, 7, 11, 7, 14, 5, 15, 2, 13, 5, 15, 4, 9, 2, 13, 3, 14, 7, 10, 3, 10, 3, 12, 3, 12, 2, 8, 5, 15, 3, 13, 3, 11, 3, 10, 4, 9, 3, 15, 1, 14, 4, 11, 6, 10, 3, 8, 4, 13, 0, 10, 4, 8, 5,
|
|
1877 |
5, 8, 3, 13, 4, 14, 1, 13, 7, 9, 2, 14, 4, 14, 4, 12, 7, 10, 6, 10, 5, 12, 2, 11, 6, 12, 7, 13, 4, 11, 2, 11, 3, 8, 5, 15, 2, 10, 1, 9, 2, 12, 0, 12, 0, 10, 1, 12, 0, 9, 7, 10, 0, 15, 4, 9, 0, 8, 1, 14, 4, 14, 4, 13,
|
|
1878 |
12, 2, 8, 6, 10, 0, 8, 5, 13, 3, 8, 5, 10, 3, 11, 2, 15, 0, 13, 3, 8, 2, 15, 6, 9, 1, 8, 3, 14, 1, 15, 5, 14, 7, 8, 0, 12, 6, 15, 5, 8, 5, 9, 4, 15, 5, 8, 5, 15, 4, 12, 3, 9, 6, 12, 3, 13, 5, 11, 7, 10, 1, 8, 3,
|
|
1879 |
4, 15, 2, 11, 2, 10, 6, 11, 3, 14, 7, 15, 0, 10, 0, 14, 3, 12, 4, 8, 7, 9, 3, 11, 0, 13, 2, 12, 2, 13, 5, 8, 5, 8, 2, 11, 4, 12, 2, 10, 7, 8, 6, 10, 7, 12, 7, 11, 0, 15, 7, 8, 7, 15, 0, 9, 2, 12, 6, 13, 0, 9, 4, 9,
|
|
1880 |
8, 3, 12, 5, 15, 5, 13, 1, 8, 7, 8, 3, 12, 5, 9, 5, 9, 6, 14, 1, 12, 3, 15, 7, 10, 7, 8, 5, 8, 5, 15, 0, 13, 1, 14, 7, 9, 1, 14, 7, 13, 2, 12, 3, 8, 1, 13, 3, 8, 4, 12, 0, 11, 3, 12, 5, 11, 5, 9, 3, 12, 6, 14, 2,
|
|
1881 |
5, 13, 2, 8, 6, 12, 7, 10, 5, 14, 4, 11, 2, 11, 2, 13, 2, 13, 5, 10, 5, 9, 0, 10, 7, 13, 4, 15, 2, 9, 1, 9, 5, 9, 6, 10, 4, 12, 5, 12, 4, 10, 3, 14, 3, 15, 1, 8, 4, 12, 5, 8, 5, 14, 7, 11, 7, 15, 5, 11, 2, 13, 1, 9,
|
|
1882 |
10, 0, 13, 6, 10, 2, 15, 3, 8, 1, 12, 2, 14, 6, 8, 5, 10, 7, 13, 1, 13, 1, 12, 7, 9, 1, 9, 2, 12, 5, 12, 4, 12, 3, 12, 2, 9, 0, 8, 1, 15, 1, 10, 7, 10, 6, 12, 4, 11, 1, 14, 2, 11, 0, 14, 1, 9, 2, 12, 2, 8, 5, 13, 5,
|
|
1883 |
6, 10, 7, 11, 0, 9, 3, 9, 5, 11, 0, 8, 0, 13, 2, 13, 4, 13, 5, 9, 4, 8, 2, 8, 0, 10, 0, 14, 6, 9, 6, 9, 4, 14, 7, 9, 0, 11, 7, 10, 7, 11, 7, 14, 4, 14, 5, 15, 4, 8, 4, 11, 5, 14, 0, 8, 1, 14, 7, 14, 2, 11, 7, 13,
|
|
1884 |
12, 3, 13, 3, 14, 5, 15, 6, 12, 0, 15, 4, 11, 5, 9, 5, 9, 2, 14, 1, 12, 1, 13, 5, 15, 4, 11, 7, 12, 1, 13, 2, 8, 1, 12, 0, 15, 6, 14, 2, 14, 3, 10, 1, 11, 3, 11, 0, 12, 1, 15, 2, 11, 1, 12, 4, 9, 4, 10, 2, 13, 6, 10, 2,
|
|
1885 |
2, 8, 4, 8, 4, 9, 0, 9, 2, 15, 5, 9, 6, 11, 7, 14, 0, 9, 2, 12, 2, 8, 0, 10, 1, 9, 7, 8, 2, 9, 1, 11, 2, 11, 2, 8, 1, 9, 1, 11, 7, 13, 6, 11, 1, 11, 0, 9, 2, 13, 4, 14, 1, 15, 2, 8, 7, 15, 0, 13, 6, 9, 4, 13,
|
|
1886 |
13, 5, 13, 1, 14, 0, 12, 6, 9, 6, 12, 0, 13, 0, 11, 3, 13, 6, 11, 7, 12, 5, 14, 6, 14, 5, 15, 3, 13, 6, 14, 7, 15, 6, 15, 5, 13, 4, 14, 5, 8, 2, 14, 3, 14, 6, 14, 5, 10, 6, 10, 2, 9, 5, 15, 5, 11, 1, 8, 5, 13, 3, 11, 2,
|
|
1887 |
2, 8, 6, 8, 0, 9, 1, 15, 0, 11, 4, 15, 7, 8, 4, 8, 1, 13, 7, 11, 1, 13, 5, 15, 1, 15, 2, 9, 0, 13, 5, 12, 3, 12, 2, 8, 1, 10, 1, 13, 5, 15, 3, 9, 3, 9, 2, 12, 5, 14, 6, 13, 1, 9, 6, 9, 1, 8, 4, 15, 7, 10, 7, 15,
|
|
1888 |
14, 5, 14, 3, 12, 4, 10, 7, 12, 7, 8, 2, 12, 1, 13, 2, 11, 4, 13, 3, 8, 5, 8, 1, 10, 7, 12, 6, 9, 5, 8, 0, 8, 7, 14, 5, 13, 6, 8, 4, 8, 2, 12, 6, 13, 5, 8, 7, 8, 3, 10, 3, 13, 4, 12, 1, 13, 4, 11, 1, 14, 3, 8, 1,
|
|
1889 |
2, 13, 6, 10, 0, 9, 1, 15, 0, 12, 4, 11, 2, 9, 0, 9, 0, 13, 1, 8, 0, 11, 5, 9, 6, 13, 2, 15, 2, 12, 7, 12, 6, 15, 2, 14, 1, 13, 1, 14, 4, 11, 2, 14, 1, 9, 0, 15, 1, 12, 5, 14, 6, 13, 2, 15, 3, 9, 1, 15, 7, 8, 1, 14,
|
|
1890 |
11, 7, 13, 3, 15, 6, 8, 4, 8, 4, 14, 2, 14, 6, 13, 6, 11, 4, 14, 4, 15, 4, 14, 3, 10, 2, 10, 5, 9, 6, 9, 3, 9, 1, 11, 6, 9, 4, 8, 4, 15, 0, 8, 4, 13, 4, 9, 4, 9, 5, 9, 3, 10, 2, 9, 5, 12, 6, 8, 4, 12, 0, 11, 4,
|
|
1891 |
4, 10, 4, 13, 1, 12, 7, 8, 0, 13, 4, 12, 2, 13, 1, 14, 7, 15, 3, 8, 7, 12, 3, 10, 4, 9, 4, 11, 7, 8, 2, 11, 2, 10, 0, 12, 1, 14, 6, 14, 7, 13, 7, 11, 3, 10, 0, 10, 4, 9, 3, 13, 0, 13, 7, 8, 7, 9, 7, 11, 2, 8, 1, 14,
|
|
1892 |
15, 0, 11, 1, 9, 6, 15, 3, 10, 7, 9, 1, 9, 7, 10, 7, 10, 1, 12, 5, 8, 1, 15, 7, 13, 3, 12, 2, 13, 2, 14, 7, 14, 5, 10, 4, 8, 4, 11, 1, 10, 3, 14, 0, 14, 6, 14, 5, 13, 2, 8, 7, 10, 7, 13, 2, 14, 2, 13, 2, 14, 5, 11, 6,
|
|
1893 |
7, 11, 5, 13, 1, 11, 7, 8, 5, 14, 4, 13, 1, 14, 5, 10, 2, 15, 4, 13, 7, 11, 7, 10, 1, 8, 5, 9, 4, 12, 7, 10, 1, 13, 7, 13, 7, 11, 0, 14, 5, 14, 2, 14, 4, 12, 4, 13, 7, 13, 7, 8, 2, 14, 2, 13, 5, 9, 5, 9, 2, 8, 1, 10,
|
|
1894 |
12, 0, 8, 2, 13, 4, 12, 1, 11, 1, 10, 1, 10, 4, 12, 2, 11, 5, 8, 1, 15, 3, 14, 1, 13, 4, 14, 2, 9, 2, 12, 2, 8, 4, 8, 2, 12, 3, 10, 6, 11, 0, 11, 7, 10, 1, 9, 2, 10, 0, 13, 1, 11, 7, 10, 6, 13, 1, 12, 1, 12, 6, 14, 7,
|
|
1895 |
2, 14, 4, 15, 7, 14, 5, 8, 4, 13, 2, 10, 4, 13, 0, 13, 1, 11, 1, 12, 7, 12, 6, 13, 4, 13, 4, 13, 2, 12, 2, 13, 1, 13, 7, 10, 2, 11, 2, 10, 7, 15, 7, 11, 5, 13, 2, 8, 2, 14, 4, 9, 2, 14, 0, 8, 1, 8, 7, 10, 5, 15, 6, 14,
|
|
1896 |
11, 7, 10, 2, 11, 1, 13, 0, 11, 1, 15, 7, 8, 1, 9, 4, 13, 4, 8, 4, 8, 3, 10, 0, 9, 0, 9, 2, 9, 7, 9, 6, 10, 7, 13, 1, 13, 7, 14, 6, 11, 3, 12, 2, 8, 2, 12, 5, 11, 5, 12, 2, 10, 5, 14, 4, 12, 4, 13, 2, 9, 1, 10, 0,
|
|
1897 |
2, 12, 0, 13, 7, 13, 2, 12, 4, 8, 1, 12, 4, 13, 6, 11, 7, 13, 7, 13, 1, 9, 7, 10, 2, 10, 1, 9, 2, 10, 1, 11, 6, 9, 4, 13, 2, 10, 0, 10, 2, 14, 0, 13, 7, 10, 7, 10, 0, 12, 0, 9, 0, 13, 2, 13, 1, 9, 0, 15, 2, 14, 2, 13,
|
|
1898 |
11, 7, 8, 4, 10, 2, 10, 7, 12, 1, 11, 7, 8, 2, 13, 1, 10, 0, 10, 2, 13, 6, 14, 0, 14, 7, 13, 6, 14, 5, 13, 4, 15, 1, 10, 2, 13, 7, 13, 7, 9, 4, 9, 6, 13, 3, 13, 3, 8, 4, 13, 4, 10, 6, 10, 5, 12, 4, 10, 7, 11, 6, 9, 6,
|
|
1899 |
4, 14, 6, 11, 7, 13, 6, 10, 4, 8, 4, 11, 6, 8, 5, 13, 7, 14, 7, 14, 1, 9, 0, 12, 1, 9, 1, 12, 4, 14, 7, 10, 4, 13, 7, 13, 7, 11, 4, 10, 1, 11, 7, 13, 4, 12, 1, 10, 4, 12, 2, 8, 2, 12, 2, 10, 2, 12, 1, 12, 4, 12, 2, 12,
|
|
1900 |
8, 2, 13, 2, 8, 1, 13, 1, 12, 1, 12, 2, 12, 2, 11, 1, 9, 2, 11, 3, 12, 4, 8, 4, 12, 4, 10, 6, 10, 1, 13, 2, 11, 2, 10, 1, 12, 0, 14, 2, 14, 4, 9, 2, 8, 1, 13, 4, 8, 0, 12, 4, 11, 6, 12, 6, 11, 7, 10, 7, 11, 2, 10, 7
|
|
1901 |
};
|
|
1902 |
|
|
1903 |
/*static const uint8_t dither_matrix_halftone8[4 + 8*8] = {
|
|
1904 |
8, 8, 64, 3,
|
|
1905 |
60, 53, 42, 26, 27, 43, 54, 61,
|
|
1906 |
52, 41, 25, 13, 14, 28, 44, 55,
|
|
1907 |
40, 24, 12, 5, 6, 15, 29, 45,
|
|
1908 |
39, 23, 4, 0, 1, 7, 16, 30,
|
|
1909 |
38, 22, 11, 3, 2, 8, 17, 31,
|
|
1910 |
51, 37, 21, 10, 9, 18, 32, 46,
|
|
1911 |
59, 50, 36, 20, 19, 33, 47, 56,
|
|
1912 |
63, 58, 49, 35, 34, 48, 57, 62
|
|
1913 |
}; */
|
|
1914 |
|
|
1915 |
static const uint8_t dither_matrix_diagonal45_8[4 + 8*8] = {
|
|
1916 |
8, 8, 64, 2,
|
|
1917 |
16, 32, 48, 56, 40, 24, 8, 0,
|
|
1918 |
36, 52, 60, 44, 28, 12, 4, 20,
|
|
1919 |
49, 57, 41, 25, 9, 1, 17, 33,
|
|
1920 |
61, 45, 29, 13, 5, 21, 37, 53,
|
|
1921 |
42, 26, 10, 2, 18, 34, 50, 58,
|
|
1922 |
30, 14, 6, 22, 38, 54, 62, 46,
|
|
1923 |
11, 3, 19, 35, 51, 59, 43, 27,
|
|
1924 |
7, 23, 39, 55, 63, 47, 31, 15
|
|
1925 |
};
|
|
1926 |
|
|
1927 |
|
|
1928 |
typedef struct halftone_pixelinfo {
|
|
1929 |
int x;
|
|
1930 |
int y;
|
|
1931 |
double distance;
|
|
1932 |
double angle;
|
|
1933 |
} halftone_pixelinfo;
|
|
1934 |
|
|
1935 |
static inline halftone_pixelinfo* halftone_pixel_make(int w, int h) {
|
|
1936 |
int x, y, k;
|
|
1937 |
halftone_pixelinfo* hp = Gif_NewArray(halftone_pixelinfo, w * h);
|
|
1938 |
for (y = k = 0; y != h; ++y)
|
|
1939 |
for (x = 0; x != w; ++x, ++k) {
|
|
1940 |
hp[k].x = x;
|
|
1941 |
hp[k].y = y;
|
|
1942 |
hp[k].distance = -1;
|
|
1943 |
}
|
|
1944 |
return hp;
|
|
1945 |
}
|
|
1946 |
|
|
1947 |
static inline void halftone_pixel_combine(halftone_pixelinfo* hp,
|
|
1948 |
double cx, double cy) {
|
|
1949 |
double d = (hp->x - cx) * (hp->x - cx) + (hp->y - cy) * (hp->y - cy);
|
|
1950 |
if (hp->distance < 0 || d < hp->distance) {
|
|
1951 |
hp->distance = d;
|
|
1952 |
hp->angle = atan2(hp->y - cy, hp->x - cx);
|
|
1953 |
}
|
|
1954 |
}
|
|
1955 |
|
|
1956 |
static inline int halftone_pixel_compare(const void* va, const void* vb) {
|
|
1957 |
const halftone_pixelinfo* a = (const halftone_pixelinfo*) va;
|
|
1958 |
const halftone_pixelinfo* b = (const halftone_pixelinfo*) vb;
|
|
1959 |
if (fabs(a->distance - b->distance) > 0.01)
|
|
1960 |
return a->distance < b->distance ? -1 : 1;
|
|
1961 |
else
|
|
1962 |
return a->angle < b->angle ? -1 : 1;
|
|
1963 |
}
|
|
1964 |
|
|
1965 |
static uint8_t* halftone_pixel_matrix(halftone_pixelinfo* hp,
|
|
1966 |
int w, int h, int nc) {
|
|
1967 |
int i;
|
|
1968 |
uint8_t* m = Gif_NewArray(uint8_t, 4 + w * h);
|
|
1969 |
m[0] = w;
|
|
1970 |
m[1] = h;
|
|
1971 |
m[3] = nc;
|
|
1972 |
if (w * h > 255) {
|
|
1973 |
double s = 255. / (w * h);
|
|
1974 |
m[2] = 255;
|
|
1975 |
for (i = 0; i != w * h; ++i)
|
|
1976 |
m[4 + hp[i].x + hp[i].y*w] = (int) (i * s);
|
|
1977 |
} else {
|
|
1978 |
m[2] = w * h;
|
|
1979 |
for (i = 0; i != w * h; ++i)
|
|
1980 |
m[4 + hp[i].x + hp[i].y*w] = i;
|
|
1981 |
}
|
|
1982 |
Gif_DeleteArray(hp);
|
|
1983 |
return m;
|
|
1984 |
}
|
|
1985 |
|
|
1986 |
static uint8_t* make_halftone_matrix_square(int w, int h, int nc) {
|
|
1987 |
halftone_pixelinfo* hp = halftone_pixel_make(w, h);
|
|
1988 |
int i;
|
|
1989 |
for (i = 0; i != w * h; ++i)
|
|
1990 |
halftone_pixel_combine(&hp[i], (w-1)/2.0, (h-1)/2.0);
|
|
1991 |
qsort(hp, w * h, sizeof(*hp), halftone_pixel_compare);
|
|
1992 |
return halftone_pixel_matrix(hp, w, h, nc);
|
|
1993 |
}
|
|
1994 |
|
|
1995 |
static uint8_t* make_halftone_matrix_triangular(int w, int h, int nc) {
|
|
1996 |
halftone_pixelinfo* hp = halftone_pixel_make(w, h);
|
|
1997 |
int i;
|
|
1998 |
for (i = 0; i != w * h; ++i) {
|
|
1999 |
halftone_pixel_combine(&hp[i], (w-1)/2.0, (h-1)/2.0);
|
|
2000 |
halftone_pixel_combine(&hp[i], -0.5, -0.5);
|
|
2001 |
halftone_pixel_combine(&hp[i], w-0.5, -0.5);
|
|
2002 |
halftone_pixel_combine(&hp[i], -0.5, h-0.5);
|
|
2003 |
halftone_pixel_combine(&hp[i], w-0.5, h-0.5);
|
|
2004 |
}
|
|
2005 |
qsort(hp, w * h, sizeof(*hp), halftone_pixel_compare);
|
|
2006 |
return halftone_pixel_matrix(hp, w, h, nc);
|
|
2007 |
}
|
|
2008 |
|
|
2009 |
int set_dither_type(Gt_OutputData* od, const char* name) {
|
|
2010 |
int parm[4], nparm = 0;
|
|
2011 |
const char* comma = strchr(name, ',');
|
|
2012 |
char buf[256];
|
|
2013 |
|
|
2014 |
/* separate arguments from dither name */
|
|
2015 |
if (comma && (size_t) (comma - name) < sizeof(buf)) {
|
|
2016 |
memcpy(buf, name, comma - name);
|
|
2017 |
buf[comma - name] = 0;
|
|
2018 |
name = buf;
|
|
2019 |
}
|
|
2020 |
for (nparm = 0;
|
|
2021 |
comma && *comma && isdigit((unsigned char) comma[1]);
|
|
2022 |
++nparm)
|
|
2023 |
parm[nparm] = strtol(&comma[1], (char**) &comma, 10);
|
|
2024 |
|
|
2025 |
/* parse dither name */
|
|
2026 |
if (od->dither_type == dither_ordered_new)
|
|
2027 |
Gif_DeleteArray(od->dither_data);
|
|
2028 |
od->dither_type = dither_none;
|
|
2029 |
|
|
2030 |
if (strcmp(name, "none") == 0 || strcmp(name, "posterize") == 0)
|
|
2031 |
/* ok */;
|
|
2032 |
else if (strcmp(name, "default") == 0)
|
|
2033 |
od->dither_type = dither_default;
|
|
2034 |
else if (strcmp(name, "floyd-steinberg") == 0
|
|
2035 |
|| strcmp(name, "fs") == 0)
|
|
2036 |
od->dither_type = dither_floyd_steinberg;
|
|
2037 |
else if (strcmp(name, "o3") == 0 || strcmp(name, "o3x3") == 0
|
|
2038 |
|| (strcmp(name, "o") == 0 && nparm >= 1 && parm[0] == 3)) {
|
|
2039 |
od->dither_type = dither_ordered;
|
|
2040 |
od->dither_data = dither_matrix_o3x3;
|
|
2041 |
} else if (strcmp(name, "o4") == 0 || strcmp(name, "o4x4") == 0
|
|
2042 |
|| (strcmp(name, "o") == 0 && nparm >= 1 && parm[0] == 4)) {
|
|
2043 |
od->dither_type = dither_ordered;
|
|
2044 |
od->dither_data = dither_matrix_o4x4;
|
|
2045 |
} else if (strcmp(name, "o8") == 0 || strcmp(name, "o8x8") == 0
|
|
2046 |
|| (strcmp(name, "o") == 0 && nparm >= 1 && parm[0] == 8)) {
|
|
2047 |
od->dither_type = dither_ordered;
|
|
2048 |
od->dither_data = dither_matrix_o8x8;
|
|
2049 |
} else if (strcmp(name, "ro64") == 0 || strcmp(name, "ro64x64") == 0
|
|
2050 |
|| strcmp(name, "o") == 0 || strcmp(name, "ordered") == 0) {
|
|
2051 |
od->dither_type = dither_ordered;
|
|
2052 |
od->dither_data = dither_matrix_ro64x64;
|
|
2053 |
} else if (strcmp(name, "diag45") == 0 || strcmp(name, "diagonal") == 0) {
|
|
2054 |
od->dither_type = dither_ordered;
|
|
2055 |
od->dither_data = dither_matrix_diagonal45_8;
|
|
2056 |
} else if (strcmp(name, "halftone") == 0 || strcmp(name, "half") == 0
|
|
2057 |
|| strcmp(name, "trihalftone") == 0
|
|
2058 |
|| strcmp(name, "trihalf") == 0) {
|
|
2059 |
int size = nparm >= 1 && parm[0] > 0 ? parm[0] : 6;
|
|
2060 |
int ncol = nparm >= 2 && parm[1] > 1 ? parm[1] : 2;
|
|
2061 |
od->dither_type = dither_ordered_new;
|
|
2062 |
od->dither_data = make_halftone_matrix_triangular(size, (int) (size * sqrt(3) + 0.5), ncol);
|
|
2063 |
} else if (strcmp(name, "sqhalftone") == 0 || strcmp(name, "sqhalf") == 0
|
|
2064 |
|| strcmp(name, "squarehalftone") == 0) {
|
|
2065 |
int size = nparm >= 1 && parm[0] > 0 ? parm[0] : 6;
|
|
2066 |
int ncol = nparm >= 2 && parm[1] > 1 ? parm[1] : 2;
|
|
2067 |
od->dither_type = dither_ordered_new;
|
|
2068 |
od->dither_data = make_halftone_matrix_square(size, size, ncol);
|
|
2069 |
} else
|
|
2070 |
return -1;
|
|
2071 |
|
|
2072 |
if (od->dither_type == dither_ordered
|
|
2073 |
&& nparm >= 2 && parm[1] > 1 && parm[1] != od->dither_data[3]) {
|
|
2074 |
int size = od->dither_data[0] * od->dither_data[1];
|
|
2075 |
uint8_t* dd = Gif_NewArray(uint8_t, 4 + size);
|
|
2076 |
memcpy(dd, od->dither_data, 4 + size);
|
|
2077 |
dd[3] = parm[1];
|
|
2078 |
od->dither_data = dd;
|
|
2079 |
od->dither_type = dither_ordered_new;
|
|
2080 |
}
|
|
2081 |
return 0;
|
|
2082 |
}
|