Debian Codegraph @

src/main/dither-ordered.c

raw symbols SCIP
1 /*
2 *
3 * Ordered dither algorithm
4 *
5 * Copyright 1997-2003 Michael Sweet ([email protected]) and
6 * Robert Krawitz ([email protected])
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 *
21 * Revision History:
22 *
23 * See ChangeLog
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 #include <gutenprint/gutenprint.h>
30 #include "gutenprint-internal.h"
31 #include <gutenprint/gutenprint-intl-internal.h>
32 #include "dither-impl.h"
33 #include "dither-inlined-functions.h"
34
35
36 typedef struct {
37 size_t channels;
38 double *drops;
39 unsigned short *lut;
40 } stpi_new_ordered_t;
41
42 typedef struct {
43 unsigned short shift;
44 unsigned short mask;
45 unsigned short x_mask;
46 stpi_new_ordered_t *ord_new;
47 } stpi_ordered_t;
48
49 static int
50 compare_channels(const stpi_dither_channel_t *dc1,
51 const stpi_dither_channel_t *dc2)
52 {
53 int i;
54 if (dc1->nlevels != dc2->nlevels)
55 return 0;
56 for (i = 0; i < dc1->nlevels; i++)
57 if (dc1->ranges[i].upper->value != dc2->ranges[i].upper->value)
58 return 0;
59 return 1;
60 }
61
62 static const double dp_fraction = 0.5;
63
64 static void
65 init_dither_channel_new(stpi_dither_channel_t *dc, stp_vars_t *v)
66 {
67 int i, j, k;
68 double bp = 0;
69 double lbp = 0;
70 double lower_bottom = 0;
71 double lower_middle = 0;
72 double lower_top = 0;
73 double upper_bottom = 0;
74 double upper_middle = 0;
75 double upper_top = 0;
76
77 double *breakpoints;
78 double *val;
79 unsigned short *data;
80 stpi_new_ordered_t *ord = stp_malloc(sizeof(stpi_new_ordered_t));
81 ((stpi_ordered_t *) (dc->aux_data))->ord_new = ord;
82 ord->channels = dc->nlevels - 1;
83 ord->drops = stp_malloc(sizeof(double) * (ord->channels + 1));
84 breakpoints = stp_malloc(sizeof(double) * (ord->channels + 1));
85 val = stp_malloc(sizeof(double) * ord->channels);
86 data = stp_malloc(sizeof(unsigned short) * 65536 * ord->channels);
87 ord->lut = data;
88 for (j = 0; j < ord->channels; j++)
89 {
90 stpi_dither_segment_t *dd = &(dc->ranges[j]);
91 ord->drops[j] = (double) dd->upper->value / 65535.0;
92 }
93 ord->drops[ord->channels] = 1;
94 for (j = 0; j < ord->channels; j++)
95 {
96 if (j == 0)
97 breakpoints[j] = 65535 * ord->drops[j] * dp_fraction;
98 else
99 breakpoints[j] = 65535 * ((ord->drops[j] * dp_fraction) +
100 (ord->drops[j - 1] * (1.0 - dp_fraction)));
101 stp_dprintf(STP_DBG_INK, v, " size %.3f bp %5.0f\n",
102 ord->drops[j], breakpoints[j]);
103 }
104 breakpoints[ord->channels] = 65535;
105 j = 0;
106 for (i = 0; i <= ord->channels; i++)
107 {
108 lbp = bp;
109 bp = breakpoints[i];
110 lower_bottom = upper_middle;
111 upper_bottom = 0;
112 lower_middle = upper_top;
113 lower_top = 0;
114 if (i == ord->channels)
115 upper_top = 0;
116 else
117 upper_top = 65535 * dp_fraction;
118 if (i > 0)
119 upper_middle = 65535 - upper_top;
120 while (j <= bp)
121 {
122 double range_point = (j - lbp) / (bp - lbp);
123 double uv, mv, lv;
124 int total_ink = 0;
125 for (k = 0; k < ord->channels; k++)
126 val[k] = 0;
127 uv = lower_top + (upper_top - lower_top) * range_point;
128 mv = lower_middle + (upper_middle - lower_middle) * range_point;
129 lv = lower_bottom + (upper_bottom - lower_bottom) * range_point;
130
131 if (i < ord->channels)
132 val[i] = (unsigned short) uv;
133 if (i > 0)
134 val[i - 1] = (unsigned short) mv;
135 if (i > 1)
136 val[i - 2] = (unsigned short) lv;
137 for (k = ord->channels - 1; k >= 0; k--)
138 {
139 total_ink += val[k];
140 if (total_ink > 65535)
141 total_ink = 65535;
142 data[k] = total_ink;
143 }
144 if ((stp_get_debug_level() & STP_DBG_INK) && (j % 257 == 0))
145 {
146 stp_dprintf(STP_DBG_INK, v, " %5d:", j);
147 for (k = 0; k < ord->channels; k++)
148 stp_dprintf(STP_DBG_INK, v, " %9.3f", val[k]);
149 stp_dprintf(STP_DBG_INK, v, " ");
150 for (k = 0; k < ord->channels; k++)
151 stp_dprintf(STP_DBG_INK, v, " %9.3f", breakpoints[k]);
152 stp_dprintf(STP_DBG_INK, v, " ");
153 for (k = 0; k < ord->channels; k++)
154 stp_dprintf(STP_DBG_INK, v, " %5d", data[k]);
155 stp_dprintf(STP_DBG_INK, v, "\n");
156 }
157 data += ord->channels;
158 j++;
159 }
160 }
161 stp_free(breakpoints);
162 stp_free(val);
163 }
164
165 static inline void
166 print_color_ordered_new(const stpi_dither_t *d, stpi_dither_channel_t *dc,
167 int val, int x, int y, unsigned char bit, int length)
168 {
169 int i;
170 int j;
171 unsigned bits;
172 int levels = dc->nlevels - 1;
173 unsigned dpoint = ditherpoint(d, &(dc->dithermat), x);
174 const stpi_ordered_t *o = (const stpi_ordered_t *) dc->aux_data;
175 const stpi_new_ordered_t *ord = (const stpi_new_ordered_t *) o->ord_new;
176 unsigned short swhere = (unsigned short) val;
177 unsigned short *where = ord ? ord->lut + (val * levels) : &swhere;
178 (void)y;
179
180 /*
181 * Look for the appropriate range into which the input value falls.
182 */
183 for (i = levels - 1; i >= 0; i--)
184 {
185 if (dpoint < where[i])
186 {
187 stpi_dither_segment_t *dd = &(dc->ranges[i]);
188 bits = dd->upper->bits;
189 if (bits)
190 {
191 unsigned char *tptr = dc->ptr + d->ptr_offset;
192
193 /*
194 * Lay down all of the bits in the pixel.
195 */
196 set_row_ends(dc, x);
197 for (j = 1; j <= bits; j += j, tptr += length)
198 {
199 if (j & bits)
200 tptr[0] |= bit;
201 }
202
203 }
204 return;
205 }
206 }
207 }
208
209 static inline void
210 print_color_ordered(const stpi_dither_t *d, stpi_dither_channel_t *dc, int val,
211 int x, int y, unsigned char bit, int length)
212 {
213 int i;
214 int j;
215 unsigned bits;
216 int levels = dc->nlevels - 1;
217 (void)y;
218
219 /*
220 * Look for the appropriate range into which the input value falls.
221 */
222 for (i = levels; i >= 0; i--)
223 {
224 stpi_dither_segment_t *dd = &(dc->ranges[i]);
225
226 if (val > dd->lower->value)
227 {
228 /*
229 * Where are we within the range.
230 */
231
232 unsigned rangepoint = val - dd->lower->value;
233 if (dd->value_span < 65535)
234 rangepoint = rangepoint * 65535 / dd->value_span;
235
236 if (rangepoint >= ditherpoint(d, &(dc->dithermat), x))
237 bits = dd->upper->bits;
238 else
239 bits = dd->lower->bits;
240
241 if (bits)
242 {
243 unsigned char *tptr = dc->ptr + d->ptr_offset;
244
245 /*
246 * Lay down all of the bits in the pixel.
247 */
248 set_row_ends(dc, x);
249 for (j = 1; j <= bits; j += j, tptr += length)
250 {
251 if (j & bits)
252 tptr[0] |= bit;
253 }
254
255 }
256 return;
257 }
258 }
259 }
260
261 static void
262 free_dither_ordered(stpi_dither_t *d)
263 {
264 int i;
265 stpi_dither_channel_t *dc0 = &CHANNEL(d, 0);
266 stpi_ordered_t *o0 = dc0->aux_data;
267 stpi_new_ordered_t *no0 = NULL;
268 if (o0)
269 no0 = o0->ord_new;
270 for (i = CHANNEL_COUNT(d) - 1; i >= 0 ; i--)
271 {
272 stpi_dither_channel_t *dc = &CHANNEL(d, i);
273 if (dc->aux_data)
274 {
275 stpi_ordered_t *ord = (stpi_ordered_t *) dc->aux_data;
276 if (ord->ord_new && (i == 0 || ord->ord_new != no0))
277 {
278 stpi_new_ordered_t *no = (stpi_new_ordered_t *) ord->ord_new;
279 if (no->drops)
280 stp_free(no->drops);
281 if (no->lut)
282 stp_free(no->lut);
283 stp_free(no);
284 }
285 stp_free(dc->aux_data);
286 dc->aux_data = NULL;
287 }
288 }
289 stp_free(d->aux_data);
290 }
291
292 static void
293 init_dither_ordered(stpi_dither_t *d, stp_vars_t *v)
294 {
295 int i;
296 d->aux_data = stp_malloc(1);
297 d->aux_freefunc = &free_dither_ordered;
298 stp_dprintf(STP_DBG_INK, v, "init_dither_ordered\n");
299 for (i = 0; i < CHANNEL_COUNT(d); i++)
300 {
301 stpi_dither_channel_t *dc = &CHANNEL(d, i);
302 stpi_ordered_t *s;
303 dc->aux_data = stp_malloc(sizeof(stpi_ordered_t));
304 s = (stpi_ordered_t *) dc->aux_data;
305 s->ord_new = NULL;
306 if (d->stpi_dither_type & D_ORDERED_SEGMENTED)
307 {
308 s->shift = 16 - dc->signif_bits;
309 s->mask = ((1 << dc->signif_bits) - 1) << s->shift;
310 s->x_mask = ~(s->mask);
311 stp_dprintf(STP_DBG_INK, v, " channel %d: shift %d mask 0x%x x_mask 0x%x\n",
312 i, s->shift, s->mask, s->x_mask);
313 }
314 if (d->stpi_dither_type & D_ORDERED_NEW)
315 {
316 if (dc->nlevels < 2)
317 stp_dprintf(STP_DBG_INK, v, " channel %d ignored\n", i);
318 else if (i == 0 || !compare_channels(&CHANNEL(d, 0), dc))
319 {
320 stp_dprintf(STP_DBG_INK, v, " channel %d\n", i);
321 init_dither_channel_new(dc, v);
322 }
323 else
324 {
325 stp_dprintf(STP_DBG_INK, v, " channel %d duplicated from channel 0\n", i);
326 s->ord_new = ((stpi_ordered_t *) (CHANNEL(d, 0).aux_data))->ord_new;
327 }
328 }
329 }
330 }
331
332 void
333 stpi_dither_ordered(stp_vars_t *v,
334 int row,
335 const unsigned short *raw,
336 int duplicate_line,
337 int zero_mask,
338 const unsigned char *mask)
339 {
340 stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither");
341 int x,
342 length;
343 unsigned char bit;
344 int i;
345 int one_bit_only = 1;
346 int one_level_only = 1;
347
348 int xerror, xstep, xmod;
349 (void)duplicate_line;
350
351 if ((zero_mask & ((1 << CHANNEL_COUNT(d)) - 1)) ==
352 ((1 << CHANNEL_COUNT(d)) - 1))
353 return;
354
355 length = (d->dst_width + 7) / 8;
356
357 bit = 128;
358 xstep = CHANNEL_COUNT(d) * (d->src_width / d->dst_width);
359 xmod = d->src_width % d->dst_width;
360 xerror = 0;
361
362 for (i = 0; i < CHANNEL_COUNT(d); i++)
363 {
364 stpi_dither_channel_t *dc = &(CHANNEL(d, i));
365 if (dc->nlevels != 1)
366 one_level_only = 0;
367 if (dc->nlevels != 1 || dc->ranges[0].upper->bits != 1)
368 one_bit_only = 0;
369 }
370 if (! one_bit_only && ! d->aux_data &&
371 (d->stpi_dither_type & (D_ORDERED_SEGMENTED | D_ORDERED_NEW)))
372 init_dither_ordered(d, v);
373
374 if (one_bit_only)
375 {
376 for (x = 0; x < d->dst_width; x ++)
377 {
378 if (!mask || (*(mask + d->ptr_offset) & bit))
379 {
380 for (i = 0; i < CHANNEL_COUNT(d); i++)
381 {
382 if (raw[i] &&
383 raw[i] >= ditherpoint(d, &(CHANNEL(d, i).dithermat), x))
384 {
385 set_row_ends(&(CHANNEL(d, i)), x);
386 CHANNEL(d, i).ptr[d->ptr_offset] |= bit;
387 }
388 }
389 }
390 ADVANCE_UNIDIRECTIONAL(d, bit, raw, CHANNEL_COUNT(d),
391 xerror, xstep, xmod);
392 }
393 }
394 else if (d->stpi_dither_type & D_ORDERED_SEGMENTED)
395 {
396 for (x = 0; x < d->dst_width; x ++)
397 {
398 if (!mask || (*(mask + d->ptr_offset) & bit))
399 {
400 for (i = 0; i < CHANNEL_COUNT(d); i++)
401 {
402 stpi_dither_channel_t *dc = &CHANNEL(d, i);
403 stpi_ordered_t *s = (stpi_ordered_t *) dc->aux_data;
404 unsigned short bits = raw[i] >> s->shift;
405 unsigned short val = raw[i] << dc->signif_bits;
406 val |= val >> s->shift;
407
408 if (bits)
409 {
410 if (val &&
411 val >= ditherpoint(d, &(CHANNEL(d, i).dithermat), x))
412 {
413 int j;
414 unsigned char *tptr = dc->ptr + d->ptr_offset;
415 set_row_ends(dc, x);
416 for (j = 1; j <= bits; j += j, tptr += length)
417 {
418 if (j & bits)
419 tptr[0] |= bit;
420 }
421 }
422 }
423 else if (CHANNEL(d, i).ptr && val)
424 {
425 if (d->stpi_dither_type & D_ORDERED_NEW)
426 print_color_ordered_new(d, &(CHANNEL(d, i)), val, x,
427 row, bit, length);
428 else
429 print_color_ordered(d, &(CHANNEL(d, i)), val, x,
430 row, bit, length);
431 }
432 }
433 }
434 ADVANCE_UNIDIRECTIONAL(d, bit, raw, CHANNEL_COUNT(d),
435 xerror, xstep, xmod);
436 }
437 }
438 else if (one_level_only || !(d->stpi_dither_type == D_ORDERED_NEW))
439 {
440 for (x = 0; x != d->dst_width; x ++)
441 {
442 if (!mask || (*(mask + d->ptr_offset) & bit))
443 {
444 for (i = 0; i < CHANNEL_COUNT(d); i++)
445 {
446 if (CHANNEL(d, i).ptr && raw[i])
447 print_color_ordered(d, &(CHANNEL(d, i)), raw[i], x, row,
448 bit, length);
449 }
450 }
451 ADVANCE_UNIDIRECTIONAL(d, bit, raw, CHANNEL_COUNT(d), xerror,
452 xstep, xmod);
453 }
454 }
455 else
456 {
457 for (x = 0; x != d->dst_width; x ++)
458 {
459 if (!mask || (*(mask + d->ptr_offset) & bit))
460 {
461 for (i = 0; i < CHANNEL_COUNT(d); i++)
462 {
463 if (CHANNEL(d, i).ptr && raw[i])
464 print_color_ordered_new(d, &(CHANNEL(d, i)), raw[i], x,
465 row, bit, length);
466 }
467 }
468 ADVANCE_UNIDIRECTIONAL(d, bit, raw, CHANNEL_COUNT(d), xerror,
469 xstep, xmod);
470 }
471 }
472 }