Debian Codegraph @

src/main/dither-ed.c

raw symbols SCIP
1 /*
2 *
3 * Error diffusion and closely related adaptive hybrid 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_Hcxx . . $ `<built-in>:411:9`!
27 #include <config.h>cxx . . $ `<file>/config.h`/
28 #endif
29 #include <gutenprint/gutenprint.h>cxx . . $ `<file>/include/gutenprint/gutenprint.h`/
30 #include "gutenprint-internal.h"cxx . . $ `<file>/src/main/gutenprint-internal.h`/
31 #include <gutenprint/gutenprint-intl-internal.h>cxx . . $ `<file>/include/gutenprint/gutenprint-intl-internal.h`/
32 #include <string.h>
33 #include "dither-impl.h"cxx . . $ `<file>/src/main/dither-impl.h`/
34 #include "dither-inlined-functions.h"cxx . . $ `<file>/src/main/dither-inlined-functions.h`/
35
36 /*
37 * Add the error to the input value. Notice that we micro-optimize this
38 * to save a division when appropriate. This has yielded measurable
39 * improvement -- rlk 20030119
40 */
41
42 #define UPDATE_COLORcxx . . $ `src/main/dither-ed.c:42:9`!(color, dither) ( \
43 ((dither) >= 0)? (color) + ((dither) >> 3) : (color) - ((-(dither)) >> 3))
44
45 /*
46 * For Floyd-Steinberg, distribute the error residual. We spread the
47 * error to nearby points, spreading more broadly in lighter regions to
48 * achieve more uniform distribution of color. The actual distribution
49 * is a triangular function.
50 */
51
52 static inlinecxx . . $ `src/main/dither-impl.h:41:9`! int
53 update_dithercxx . . $ update_dither(184ba8231280be24).(stpi_dither_tcxx . . $ stpi_dither_t# *d, int channel, int width,
54 int direction, int *error0, int *error1)
55 {
56 int r = CHANNELcxx . . $ `src/main/dither-impl.h:178:9`!(d, channel).vcxx . . $ dither_channel#v.;
57 int o = CHANNELcxx . . $ `src/main/dither-impl.h:178:9`!(d, channel).ocxx . . $ dither_channel#o.;
58 int tmp = r;
59 int i, dist, dist1;
60 int delta, delta1;
61 int offset;
62 (void)width;
63
64 if (tmp == 0)
65 return error0[direction];
66 if (tmp > 65535)
67 tmp = 65535;
68 if (d->spreadcxx . . $ dither#spread. >= 16 || o >= 2048)
69 {
70 tmp += tmp;
71 tmp += tmp;
72 error1[0] += tmp;
73 return error0[direction] + tmp;
74 }
75 else
76 {
77 int tmpo = o << 5;
78 offset = ((65535 - tmpo) >> d->spreadcxx . . $ dither#spread.) +
79 ((tmp & d->spread_maskcxx . . $ dither#spread_mask.) > (tmpo & d->spread_maskcxx . . $ dither#spread_mask.));
80 }
81 switch (offset)
82 {
83 case 0:
84 tmp += tmp;
85 tmp += tmp;
86 error1[0] += tmp;
87 return error0[direction] + tmp;
88 case 1:
89 error1[-1] += tmp;
90 error1[1] += tmp;
91 tmp += tmp;
92 error1[0] += tmp;
93 tmp += tmp;
94 return error0[direction] + tmp;
95 default:
96 tmp += tmp;
97 tmp += tmp;
98 dist = tmp / d->offset0_tablecxx . . $ dither#offset0_table.[offset];
99 dist1 = tmp / d->offset1_tablecxx . . $ dither#offset1_table.[offset];
100 delta = dist;
101 delta1 = dist1;
102 for (i = -offset; i; i++)
103 {
104 error1[i] += delta;
105 error1[-i] += delta;
106 error0[i] += delta1;
107 error0[-i] += delta1;
108 delta1 += dist1;
109 delta += dist;
110 }
111 error1[0] += delta;
112 return error0[direction];
113 }
114 }
115
116
117 /*
118 * Print a single dot. This routine has become awfully complicated
119 * awfully fast!
120 */
121
122 static inlinecxx . . $ `src/main/dither-impl.h:41:9`! int
123 print_colorcxx . . $ print_color(672fed82386fe8fe).(const stpi_dither_tcxx . . $ stpi_dither_t# *d, stpi_dither_channel_tcxx . . $ stpi_dither_channel_t# *dc, int x, int y,
124 unsigned char bit, int length, int dontprint, int stpi_dither_type,
125 const unsigned char *mask)
126 {
127 int base = dc->bcxx . . $ dither_channel#b.;
128 int density = dc->ocxx . . $ dither_channel#o.;
129 int adjusted = dc->vcxx . . $ dither_channel#v.;
130 unsigned randomizer = dc->randomizercxx . . $ dither_channel#randomizer.;
131 stp_dither_matrix_impl_tcxx . . $ stp_dither_matrix_impl_t# *pick_matrix = &(dc->pickcxx . . $ dither_channel#pick.);
132 stp_dither_matrix_impl_tcxx . . $ stp_dither_matrix_impl_t# *dither_matrix = &(dc->dithermatcxx . . $ dither_channel#dithermat.);
133 unsigned rangepoint = 32768;
134 unsigned vmatrix;
135 int i;
136 int j;
137 unsigned char *tptr;
138 unsigned bits;
139 unsigned v;
140 int levels = dc->nlevelscxx . . $ dither_channel#nlevels. - 1;
141 int dither_value = adjusted;
142 stpi_ink_defn_tcxx . . $ stpi_ink_defn_t# *lower;
143 stpi_ink_defn_tcxx . . $ stpi_ink_defn_t# *upper;
144
145 (void)y;
146
147 if (base <= 0 || density <= 0 ||
148 (adjusted <= 0 && !(stpi_dither_type & D_ADAPTIVE_BASEcxx . . $ `src/main/dither-impl.h:46:9`!)))
149 return adjusted;
150 if (density > 65535)
151 density = 65535;
152
153 /*
154 * Look for the appropriate range into which the input value falls.
155 * Notice that we use the input, not the error, to decide what dot type
156 * to print (if any). We actually use the "density" input to permit
157 * the caller to use something other that simply the input value, if it's
158 * desired to use some function of overall density, rather than just
159 * this color's input, for this purpose.
160 */
161 for (i = levels; i >= 0; i--)
162 {
163 stpi_dither_segment_tcxx . . $ stpi_dither_segment_t# *dd = &(dc->rangescxx . . $ dither_channel#ranges.[i]);
164
165 if (density <= dd->lowercxx . . $ dither_segment#lower.->rangecxx . . $ ink_defn#range.)
166 continue;
167
168 /*
169 * If we're using an adaptive dithering method, decide whether
170 * to use the Floyd-Steinberg or the ordered method based on the
171 * input value.
172 */
173 if (stpi_dither_type & D_ADAPTIVE_BASEcxx . . $ `src/main/dither-impl.h:46:9`!)
174 {
175 stpi_dither_type -= D_ADAPTIVE_BASEcxx . . $ `src/main/dither-impl.h:46:9`!;
176
177 if (i < levels || base <= d->adaptive_limitcxx . . $ dither#adaptive_limit.)
178 {
179 stpi_dither_type = D_ORDEREDcxx . . $ `src/main/dither-impl.h:49:9`!;
180 dither_value = base;
181 }
182 else if (adjusted <= 0)
183 return adjusted;
184 }
185 /*
186 * Where are we within the range. If we're going to print at
187 * all, this determines the probability of printing the darker
188 * vs. the lighter ink. If the inks are identical (same value
189 * and darkness), it doesn't matter.
190 *
191 * We scale the input linearly against the top and bottom of the
192 * range.
193 */
194
195 lower = dd->lowercxx . . $ dither_segment#lower.;
196 upper = dd->uppercxx . . $ dither_segment#upper.;
197
198 /*
199 * Reduce the randomness as the base value increases, to get
200 * smoother output in the midtones. Idea suggested by
201 * Thomas Tonino.
202 */
203 if (stpi_dither_type & D_ORDERED_BASEcxx . . $ `src/main/dither-impl.h:48:9`!)
204 {
205 rangepoint = density - dd->lowercxx . . $ dither_segment#lower.->rangecxx . . $ ink_defn#range.;
206 if (dd->range_spancxx . . $ dither_segment#range_span. < 65535)
207 rangepoint = rangepoint * 65535 / dd->range_spancxx . . $ dither_segment#range_span.;
208 vmatrix = 0;
209 }
210 else
211 {
212
213 /*
214 * Compute the virtual dot size that we're going to print.
215 * This is somewhere between the two candidate dot sizes.
216 * This is scaled between the high and low value.
217 */
218
219 unsigned virtual_value;
220 rangepoint = density - dd->lowercxx . . $ dither_segment#lower.->rangecxx . . $ ink_defn#range.;
221 if (dd->range_spancxx . . $ dither_segment#range_span. < 65535)
222 rangepoint = rangepoint * 65535 / dd->range_spancxx . . $ dither_segment#range_span.;
223 if (dd->value_spancxx . . $ dither_segment#value_span. == 0)
224 virtual_value = upper->valuecxx . . $ ink_defn#value.;
225 else /* if (dd->range_span == 0) */
226 virtual_value = (upper->valuecxx . . $ ink_defn#value. + lower->valuecxx . . $ ink_defn#value.) / 2;
227 /*
228 else
229 virtual_value = lower->value + (dd->value_span * rangepoint / 65535);
230 */
231 randomizer = 0;
232 if (randomizer > 0)
233 {
234 if (base > d->d_cutoffcxx . . $ dither#d_cutoff.)
235 randomizer = 0;
236 else if (base > d->d_cutoffcxx . . $ dither#d_cutoff. / 2)
237 randomizer = randomizer * 2 * (d->d_cutoffcxx . . $ dither#d_cutoff. - base) / d->d_cutoffcxx . . $ dither#d_cutoff.;
238 }
239
240 /*
241 * Compute the comparison value to decide whether to print at
242 * all. If there is no randomness, simply divide the virtual
243 * dotsize by 2 to get standard "pure" Floyd-Steinberg (or "pure"
244 * matrix dithering, which degenerates to a threshold).
245 */
246 if (randomizer == 0)
247 vmatrix = virtual_value / 2;
248 else
249 {
250 /*
251 * First, compute a value between 0 and 65535 that will be
252 * scaled to produce an offset from the desired threshold.
253 */
254 vmatrix = ditherpointcxx . . $ ditherpoint(44214aea3cf6f5be).(d, dither_matrix, x);
255 /*
256 * Now, scale the virtual dot size appropriately. Note that
257 * we'll get something evenly distributed between 0 and
258 * the virtual dot size, centered on the dot size / 2,
259 * which is the normal threshold value.
260 */
261 vmatrix = vmatrix * virtual_value / 65535;
262 if (randomizer != 65535)
263 {
264 /*
265 * We want vmatrix to be scaled between 0 and
266 * virtual_value when randomizer is 65535 (fully random).
267 * When it's less, we want it to scale through part of
268 * that range. In all cases, it should center around
269 * virtual_value / 2.
270 *
271 * vbase is the bottom of the scaling range.
272 */
273 unsigned vbase = virtual_value * (65535u - randomizer) /
274 131070u;
275 vmatrix = vmatrix * randomizer / 65535;
276 vmatrix += vbase;
277 }
278 } /* randomizer != 0 */
279 }
280 /*
281 * After all that, printing is almost an afterthought.
282 * Pick the actual dot size (using a matrix here) and print it.
283 */
284 if (dither_value > 0 && dither_value >= vmatrix)
285 {
286 stpi_ink_defn_tcxx . . $ stpi_ink_defn_t# *subc;
287
288 if (dd->is_same_inkcxx . . $ dither_segment#is_same_ink.)
289 subc = upper;
290 else
291 {
292 if (rangepoint >= ditherpointcxx . . $ ditherpoint(44214aea3cf6f5be).(d, pick_matrix, x))
293 subc = upper;
294 else
295 subc = lower;
296 }
297 v = subc->valuecxx . . $ ink_defn#value.;
298 if (!mask || (*(mask + d->ptr_offsetcxx . . $ dither#ptr_offset.) & bit))
299 {
300 if (dc->ptrcxx . . $ dither_channel#ptr.)
301 {
302 tptr = dc->ptrcxx . . $ dither_channel#ptr. + d->ptr_offsetcxx . . $ dither#ptr_offset.;
303
304 /*
305 * Lay down all of the bits in the pixel.
306 */
307 if (dontprint < v)
308 {
309 bits = subc->bitscxx . . $ ink_defn#bits.;
310 set_row_endscxx . . $ set_row_ends(64d265e6bb66a9f0).(dc, x);
311 for (j = 1; j <= bits; j += j, tptr += length)
312 {
313 if (j & bits)
314 tptr[0] |= bit;
315 }
316 }
317 }
318 }
319 if (stpi_dither_type & D_ORDERED_BASEcxx . . $ `src/main/dither-impl.h:48:9`!)
320 {
321 double adj = -(int) v;
322 adj /= 2.0;
323 adjusted = adj;
324 }
325 else
326 {
327 double adj = v;
328 adjusted -= adj;
329 }
330 }
331 return adjusted;
332 }
333 return adjusted;
334 }
335
336 static int
337 shared_ed_initializercxx . . $ shared_ed_initializer(be8159a627dfccd9).(stpi_dither_tcxx . . $ stpi_dither_t# *d,
338 int row,
339 int duplicate_line,
340 int zero_mask,
341 int length,
342 int direction,
343 int ****error,
344 int **ndither)
345 {
346 int i, j;
347 for (i = 0; i < CHANNEL_COUNTcxx . . $ `src/main/dither-impl.h:179:9`!(d); i++)
348 CHANNELcxx . . $ `src/main/dither-impl.h:178:9`!(d, i).error_rowscxx . . $ dither_channel#error_rows. = 2;
349 if (!duplicate_line)
350 {
351 if ((zero_mask & ((1 << CHANNEL_COUNTcxx . . $ `src/main/dither-impl.h:179:9`!(d)) - 1)) !=
352 ((1 << CHANNEL_COUNTcxx . . $ `src/main/dither-impl.h:179:9`!(d)) - 1))
353 d->last_line_was_emptycxx . . $ dither#last_line_was_empty. = 0;
354 else
355 d->last_line_was_emptycxx . . $ dither#last_line_was_empty.++;
356 }
357 else if (d->last_line_was_emptycxx . . $ dither#last_line_was_empty.)
358 d->last_line_was_emptycxx . . $ dither#last_line_was_empty.++;
359 if (d->last_line_was_emptycxx . . $ dither#last_line_was_empty. >= 5)
360 return 0;
361 else if (d->last_line_was_emptycxx . . $ dither#last_line_was_empty. == 4)
362 {
363 for (i = 0; i < CHANNEL_COUNTcxx . . $ `src/main/dither-impl.h:179:9`!(d); i++)
364 for (j = 0; j < d->error_rowscxx . . $ dither#error_rows.; j++)
365 memsetcxx . . $ memset(d1de1e4fb215f1c6).(stpi_dither_get_errlinecxx . . $ stpi_dither_get_errline(9dcb8238a6bf38ad).(d, row + j, i), 0,
366 d->dst_widthcxx . . $ dither#dst_width. * sizeof(int));
367 return 0;
368 }
369 d->ptr_offsetcxx . . $ dither#ptr_offset. = (direction == 1) ? 0 : length - 1;
370
371 *error = stp_malloccxx . . $ stp_malloc(bfcfa4d6b7f7ef64).(CHANNEL_COUNTcxx . . $ `src/main/dither-impl.h:179:9`!(d) * sizeof(int **));
372 *ndither = stp_malloccxx . . $ stp_malloc(bfcfa4d6b7f7ef64).(CHANNEL_COUNTcxx . . $ `src/main/dither-impl.h:179:9`!(d) * sizeof(int));
373 for (i = 0; i < CHANNEL_COUNTcxx . . $ `src/main/dither-impl.h:179:9`!(d); i++)
374 {
375 (*error)[i] = stp_malloccxx . . $ stp_malloc(bfcfa4d6b7f7ef64).(d->error_rowscxx . . $ dither#error_rows. * sizeof(int *));
376 for (j = 0; j < d->error_rowscxx . . $ dither#error_rows.; j++)
377 {
378 (*error)[i][j] = stpi_dither_get_errlinecxx . . $ stpi_dither_get_errline(9dcb8238a6bf38ad).(d, row + j, i);
379 if (j == d->error_rowscxx . . $ dither#error_rows. - 1)
380 memsetcxx . . $ memset(d1de1e4fb215f1c6).((*error)[i][j], 0, d->dst_widthcxx . . $ dither#dst_width. * sizeof(int));
381 if (direction == -1)
382 (*error)[i][j] += d->dst_widthcxx . . $ dither#dst_width. - 1;
383 }
384 (*ndither)[i] = (*error)[i][0][0];
385 }
386 return 1;
387 }
388
389 static void
390 shared_ed_deinitializercxx . . $ shared_ed_deinitializer(fb07b27433a67d38).(stpi_dither_tcxx . . $ stpi_dither_t# *d,
391 int ***error,
392 int *ndither)
393 {
394 int i;
395 for (i = 0; i < CHANNEL_COUNTcxx . . $ `src/main/dither-impl.h:179:9`!(d); i++)
396 {
397 STP_SAFE_FREEcxx . . $ `include/gutenprint/util.h:210:9`!(error[i]);
398 }
399 STP_SAFE_FREEcxx . . $ `include/gutenprint/util.h:210:9`!(error);
400 STP_SAFE_FREEcxx . . $ `include/gutenprint/util.h:210:9`!(ndither);
401 }
402
403 void
404 stpi_dither_edcxx . . $ stpi_dither_ed(525e4b1c00087d37).(stp_vars_tcxx . . $ stp_vars_t# *v,
405 int row,
406 const unsigned short *raw,
407 int duplicate_line,
408 int zero_mask,
409 const unsigned char *mask)
410 {
411 stpi_dither_tcxx . . $ stpi_dither_t# *d = (stpi_dither_tcxx . . $ stpi_dither_t# *) stp_get_component_datacxx . . $ stp_get_component_data(2bc0070dfdce2f04).(v, "Dither");
412 int x,
413 length;
414 unsigned char bit;
415 int i;
416 int *ndither;
417 int ***error;
418
419 int terminate;
420 int direction = row & 1 ? 1 : -1;
421 int xerror, xstep, xmod;
422
423 length = (d->dst_widthcxx . . $ dither#dst_width. + 7) / 8;
424 if (d->stpi_dither_typecxx . . $ dither#stpi_dither_type. & D_ADAPTIVE_BASEcxx . . $ `src/main/dither-impl.h:46:9`!)
425 for (i = 0; i < CHANNEL_COUNTcxx . . $ `src/main/dither-impl.h:179:9`!(d); i++)
426 if (CHANNELcxx . . $ `src/main/dither-impl.h:178:9`!(d, i).nlevelscxx . . $ dither_channel#nlevels. > 1)
427 {
428 stpi_dither_orderedcxx . . $ stpi_dither_ordered(525e4b1c00087d37).(v, row, raw, duplicate_line, zero_mask, mask);
429 return;
430 }
431 if (!shared_ed_initializercxx . . $ shared_ed_initializer(be8159a627dfccd9).(d, row, duplicate_line, zero_mask, length,
432 direction, &error, &ndither))
433 return;
434
435 x = (direction == 1) ? 0 : d->dst_widthcxx . . $ dither#dst_width. - 1;
436 bit = 1 << (7 - (x & 7));
437 xstep = CHANNEL_COUNTcxx . . $ `src/main/dither-impl.h:179:9`!(d) * (d->src_widthcxx . . $ dither#src_width. / d->dst_widthcxx . . $ dither#dst_width.);
438 xmod = d->src_widthcxx . . $ dither#src_width. % d->dst_widthcxx . . $ dither#dst_width.;
439 xerror = (xmod * x) % d->dst_widthcxx . . $ dither#dst_width.;
440 terminate = (direction == 1) ? d->dst_widthcxx . . $ dither#dst_width. : -1;
441
442 if (direction == -1)
443 raw += (CHANNEL_COUNTcxx . . $ `src/main/dither-impl.h:179:9`!(d) * (d->src_widthcxx . . $ dither#src_width. - 1));
444
445 for (; x != terminate; x += direction)
446 {
447 for (i = 0; i < CHANNEL_COUNTcxx . . $ `src/main/dither-impl.h:179:9`!(d); i++)
448 {
449 if (CHANNELcxx . . $ `src/main/dither-impl.h:178:9`!(d, i).ptrcxx . . $ dither_channel#ptr.)
450 {
451 CHANNELcxx . . $ `src/main/dither-impl.h:178:9`!(d, i).vcxx . . $ dither_channel#v. = raw[i];
452 CHANNELcxx . . $ `src/main/dither-impl.h:178:9`!(d, i).ocxx . . $ dither_channel#o. = CHANNELcxx . . $ `src/main/dither-impl.h:178:9`!(d, i).vcxx . . $ dither_channel#v.;
453 CHANNELcxx . . $ `src/main/dither-impl.h:178:9`!(d, i).bcxx . . $ dither_channel#b. = CHANNELcxx . . $ `src/main/dither-impl.h:178:9`!(d, i).vcxx . . $ dither_channel#v.;
454 CHANNELcxx . . $ `src/main/dither-impl.h:178:9`!(d, i).vcxx . . $ dither_channel#v. = UPDATE_COLORcxx . . $ `src/main/dither-ed.c:42:9`!(CHANNELcxx . . $ `src/main/dither-impl.h:178:9`!(d, i).v, ndither[i]);
455 CHANNELcxx . . $ `src/main/dither-impl.h:178:9`!(d, i).vcxx . . $ dither_channel#v. = print_colorcxx . . $ print_color(672fed82386fe8fe).(d, &(CHANNELcxx . . $ `src/main/dither-impl.h:178:9`!(d, i)), x, row, bit,
456 length, 0, d->stpi_dither_typecxx . . $ dither#stpi_dither_type.,
457 mask);
458 ndither[i] = update_dithercxx . . $ update_dither(184ba8231280be24).(d, i, d->src_widthcxx . . $ dither#src_width.,
459 direction, error[i][0], error[i][1]);
460 }
461 }
462 ADVANCE_BIDIRECTIONALcxx . . $ `src/main/dither-impl.h:242:9`!(d, bit, raw, direction, CHANNEL_COUNTcxx . . $ `src/main/dither-impl.h:179:9`!(d), xerror,
463 xstep, xmod, error, d->error_rows);
464 }
465 shared_ed_deinitializercxx . . $ shared_ed_deinitializer(fb07b27433a67d38).(d, error, ndither);
466 if (direction == -1)
467 stpi_dither_reverse_row_endscxx . . $ stpi_dither_reverse_row_ends(eba3149742ad601b).(d);
468 }