Debian Codegraph @

src/main/dither-main.c

raw symbols SCIP
1 /*
2 *
3 * Dither routine entrypoints
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 #ifdef HAVE_LIMITS_H
33 #include <limits.h>
34 #endif
35 #include <math.h>
36 #include <string.h>
37 #include "dither-impl.h"
38 #include "generic-options.h"
39
40 static const stpi_dither_algorithm_t dither_algos[] =
41 {
42 { "None", N_ ("Default"), -1 },
43 /* TRANSLATORS: "EvenTone" is the proper name, rather than a */
44 /* descriptive name, of this algorithm. */
45 { "EvenTone", N_ ("EvenTone"), D_EVENTONE },
46 { "HybridEvenTone", N_ ("Hybrid EvenTone"), D_HYBRID_EVENTONE },
47 /* Placeholders for future implementation of EvenBetter Screening */
48 /* TRANSLATORS: EvenTone, EvenBetter, and UniTone are proper
49 * names, not descriptive.
50 */
51 { "EBS", N_ ("EvenBetter"), D_INVALID },
52 { "HybridEBS", N_ ("Hybrid EvenBetter"), D_INVALID },
53 /*
54 * Unitone dithering seems to have some numerical stability problems
55 * -- rlk 20050113
56 */
57 #if UNITONE_WORKS
58 { "UniTone", N_ ("UniTone"), D_UNITONE },
59 { "HybridUniTone", N_ ("Hybrid UniTone"), D_HYBRID_UNITONE },
60 #endif
61 { "Adaptive", N_ ("Adaptive Hybrid"), D_ADAPTIVE_HYBRID },
62 { "Ordered", N_ ("Ordered"), D_ORDERED },
63 { "OrderedNew", N_ ("Ordered New"), D_ORDERED_NEW },
64 { "Fast", N_ ("Fast"), D_FAST },
65 { "VeryFast", N_ ("Very Fast"), D_VERY_FAST },
66 { "Floyd", N_ ("Hybrid Floyd-Steinberg"), D_FLOYD_HYBRID },
67 { "Predithered", N_ ("Predithered Input"), D_PREDITHERED },
68 { "Segmented", N_ ("Drop Size Segmented"), D_ORDERED_SEGMENTED },
69 { "SegmentedNew", N_ ("Drop Size Segmented New"),D_ORDERED_SEGMENTED_NEW }
70 };
71
72 static const int num_dither_algos = sizeof(dither_algos)/sizeof(stpi_dither_algorithm_t);
73
74
75 /*
76 * Bayer's dither matrix using Judice, Jarvis, and Ninke recurrence relation
77 * http://www.cs.rit.edu/~sxc7922/Project/CRT.htm
78 */
79
80 static const unsigned sq2[] =
81 {
82 0, 2,
83 3, 1
84 };
85
86 static const stp_parameter_t dither_parameters[] =
87 {
88 {
89 "Density", N_("Density"), "Color=Yes,Category=Output Level Adjustment",
90 N_("Adjust the density (amount of ink) of the print. "
91 "Reduce the density if the ink bleeds through the "
92 "paper or smears; increase the density if black "
93 "regions are not solid."),
94 STP_PARAMETER_TYPE_DOUBLE, STP_PARAMETER_CLASS_OUTPUT,
95 STP_PARAMETER_LEVEL_ADVANCED, 0, 1, STP_CHANNEL_NONE, 1, 0
96 },
97 {
98 "DitherAlgorithm", N_("Dither Algorithm"), "Color=Yes,Category=Screening Adjustment",
99 N_("Choose the dither algorithm to be used.\n"
100 "Adaptive Hybrid usually produces the best all-around quality.\n"
101 "EvenTone is a new, experimental algorithm that often produces excellent results.\n"
102 "Ordered is faster and produces almost as good quality on photographs.\n"
103 "Fast and Very Fast are considerably faster, and work well for text and line art.\n"
104 "Hybrid Floyd-Steinberg generally produces inferior output."),
105 STP_PARAMETER_TYPE_STRING_LIST, STP_PARAMETER_CLASS_OUTPUT,
106 STP_PARAMETER_LEVEL_ADVANCED, 1, 1, STP_CHANNEL_NONE, 1, 0
107 },
108 };
109
110 static const int dither_parameter_count =
111 sizeof(dither_parameters) / sizeof(const stp_parameter_t);
112
113 stp_parameter_list_t
114 stp_dither_list_parameters(const stp_vars_t *v)
115 {
116 stp_parameter_list_t *ret = stp_parameter_list_create();
117 int i;
118 (void)v;
119
120 for (i = 0; i < dither_parameter_count; i++)
121 stp_parameter_list_add_param(ret, &(dither_parameters[i]));
122 return ret;
123 }
124
125 void
126 stp_dither_describe_parameter(const stp_vars_t *v, const char *name,
127 stp_parameter_t *description)
128 {
129 int i;
130 (void)v;
131
132 description->p_type = STP_PARAMETER_TYPE_INVALID;
133 if (name == NULL)
134 return;
135 description->deflt.str = NULL;
136 if (strcmp(name, "Density") == 0)
137 {
138 stp_fill_parameter_settings(description, &(dither_parameters[0]));
139 description->bounds.dbl.upper = 8.0;
140 description->bounds.dbl.lower = 0.1;
141 description->deflt.dbl = 1.0;
142 }
143 else if (strcmp(name, "DitherAlgorithm") == 0)
144 {
145 stp_fill_parameter_settings(description, &(dither_parameters[1]));
146 description->bounds.str = stp_string_list_create();
147 for (i = 0; i < num_dither_algos; i++)
148 {
149 const stpi_dither_algorithm_t *dt = &dither_algos[i];
150 if (dt->id != D_INVALID)
151 stp_string_list_add_string(description->bounds.str,
152 dt->name, gettext(dt->text));
153 }
154 description->deflt.str =
155 stp_string_list_param(description->bounds.str, 0)->name;
156 }
157 else
158 return;
159 }
160
161 #define RETURN_DITHERFUNC(func, v) \
162 do \
163 { \
164 stp_dprintf(STP_DBG_COLORFUNC, v, "ditherfunc %s\n", #func); \
165 return (func); \
166 } while (0)
167
168 static stpi_ditherfunc_t *
169 stpi_set_dither_function(stp_vars_t *v)
170 {
171 const stpi_quality_t *quality = NULL;
172 const char *image_type = stp_get_string_parameter(v, "ImageType");
173 const char *color_correction = stp_get_string_parameter(v,"ColorCorrection");
174 const char *algorithm = stp_get_string_parameter(v, "DitherAlgorithm");
175 stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither");
176 int i;
177 d->stpi_dither_type = -1;
178 if (stp_check_string_parameter(v, "Quality", STP_PARAMETER_ACTIVE))
179 quality = stpi_get_quality_by_name(stp_get_string_parameter(v, "Quality"));
180
181 if (color_correction && strcmp(color_correction, "Predithered") == 0)
182 d->stpi_dither_type = D_PREDITHERED;
183 else if (color_correction && strcmp(color_correction, "Raw") != 0 &&
184 algorithm && strcmp(algorithm, "Segmented") == 0)
185 d->stpi_dither_type = D_ORDERED;
186 else if (algorithm && strcmp(algorithm, "None") != 0)
187 {
188 for (i = 0; i < num_dither_algos; i++)
189 {
190 if (!strcmp(algorithm, gettext(dither_algos[i].name)))
191 {
192 d->stpi_dither_type = dither_algos[i].id;
193 break;
194 }
195 }
196 if (d->stpi_dither_type == -1)
197 {
198 d->stpi_dither_type = D_EVENTONE;
199 /* EvenTone performs poorly if the aspect ratio is greater than 2 */
200 if ((d->stpi_dither_type & (D_EVENTONE | D_UNITONE)) &&
201 (d->x_aspect > 2 || d->y_aspect > 2))
202 d->stpi_dither_type = D_ADAPTIVE_HYBRID;
203 }
204 }
205 else if (image_type && strcmp(image_type, "Text") == 0)
206 d->stpi_dither_type = D_VERY_FAST;
207 else if (quality)
208 {
209 switch (quality->quality_level)
210 {
211 case 0:
212 case 1:
213 d->stpi_dither_type = D_VERY_FAST;
214 break;
215 case 2:
216 case 3:
217 if (image_type && strcmp(image_type, "LineArt") == 0)
218 d->stpi_dither_type = D_VERY_FAST;
219 else
220 d->stpi_dither_type = D_FAST;
221 break;
222 case 4:
223 if (image_type &&
224 (strcmp(image_type, "LineArt") == 0 ||
225 strcmp(image_type, "TextGraphics") == 0))
226 d->stpi_dither_type = D_ADAPTIVE_HYBRID;
227 else
228 d->stpi_dither_type = D_ORDERED;
229 break;
230 case 5:
231 if (image_type &&
232 (strcmp(image_type, "LineArt") == 0 ||
233 strcmp(image_type, "TextGraphics") == 0))
234 d->stpi_dither_type = D_HYBRID_EVENTONE;
235 else if (image_type && (strcmp(image_type, "Photo") == 0))
236 d->stpi_dither_type = D_EVENTONE;
237 else
238 d->stpi_dither_type = D_ORDERED;
239 break;
240 case 6:
241 case 7:
242 case 8:
243 case 9:
244 case 10:
245 default:
246 if (image_type &&
247 (strcmp(image_type, "LineArt") == 0 ||
248 strcmp(image_type, "TextGraphics") == 0))
249 d->stpi_dither_type = D_HYBRID_EVENTONE;
250 else
251 d->stpi_dither_type = D_EVENTONE;
252 break;
253 }
254 /* EvenTone performs poorly if the aspect ratio is greater than 2 */
255 if ((d->stpi_dither_type & (D_EVENTONE | D_UNITONE)) &&
256 (d->x_aspect > 2 || d->y_aspect > 2))
257 d->stpi_dither_type = D_ADAPTIVE_HYBRID;
258 }
259 if (d->stpi_dither_type == -1)
260 d->stpi_dither_type = D_ADAPTIVE_HYBRID;
261 switch (d->stpi_dither_type)
262 {
263 case D_PREDITHERED:
264 RETURN_DITHERFUNC(stpi_dither_predithered, v);
265 case D_VERY_FAST:
266 RETURN_DITHERFUNC(stpi_dither_very_fast, v);
267 case D_ORDERED:
268 case D_ORDERED_SEGMENTED:
269 case D_ORDERED_NEW:
270 case D_ORDERED_SEGMENTED_NEW:
271 case D_FAST:
272 RETURN_DITHERFUNC(stpi_dither_ordered, v);
273 case D_HYBRID_EVENTONE:
274 case D_EVENTONE:
275 RETURN_DITHERFUNC(stpi_dither_et, v);
276 case D_HYBRID_UNITONE:
277 case D_UNITONE:
278 RETURN_DITHERFUNC(stpi_dither_ut, v);
279 default:
280 RETURN_DITHERFUNC(stpi_dither_ed, v);
281 }
282 }
283
284 void
285 stp_dither_set_adaptive_limit(stp_vars_t *v, double limit)
286 {
287 stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither");
288 d->adaptive_limit = limit;
289 }
290
291 void
292 stp_dither_set_ink_spread(stp_vars_t *v, int spread)
293 {
294 stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither");
295 STP_SAFE_FREE(d->offset0_table);
296 STP_SAFE_FREE(d->offset1_table);
297 if (spread >= 16)
298 {
299 d->spread = 16;
300 }
301 else
302 {
303 int max_offset;
304 int i;
305 d->spread = spread;
306 max_offset = (1 << (16 - spread)) + 1;
307 d->offset0_table = stp_malloc(sizeof(int) * max_offset);
308 d->offset1_table = stp_malloc(sizeof(int) * max_offset);
309 for (i = 0; i < max_offset; i++)
310 {
311 d->offset0_table[i] = (i + 1) * (i + 1);
312 d->offset1_table[i] = ((i + 1) * i) / 2;
313 }
314 }
315 d->spread_mask = (1 << d->spread) - 1;
316 }
317
318 void
319 stp_dither_set_randomizer(stp_vars_t *v, int i, double val)
320 {
321 stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither");
322 if (i < 0 || i >= CHANNEL_COUNT(d))
323 return;
324 CHANNEL(d, i).randomizer = val * 65535;
325 }
326
327 static void
328 stpi_dither_free(void *vd)
329 {
330 stpi_dither_t *d = (stpi_dither_t *) vd;
331 int j;
332 if (d->aux_freefunc)
333 (d->aux_freefunc)(d);
334 for (j = 0; j < CHANNEL_COUNT(d); j++)
335 stpi_dither_channel_destroy(&(CHANNEL(d, j)));
336 STP_SAFE_FREE(d->offset0_table);
337 STP_SAFE_FREE(d->offset1_table);
338 stp_dither_matrix_destroy(&(d->dither_matrix));
339 stp_free(d->channel);
340 stp_free(d->channel_index);
341 stp_free(d->subchannel_count);
342 stp_free(d);
343 }
344
345 void
346 stp_dither_init(stp_vars_t *v, stp_image_t *image, int out_width,
347 int xdpi, int ydpi)
348 {
349 int in_width = stp_image_width(image);
350 stpi_dither_t *d = stp_zalloc(sizeof(stpi_dither_t));
351
352 stp_allocate_component_data(v, "Dither", NULL, stpi_dither_free, d);
353
354 d->finalized = 0;
355 d->error_rows = ERROR_ROWS;
356 d->d_cutoff = 4096;
357
358 d->offset0_table = NULL;
359 d->offset1_table = NULL;
360 if (xdpi > ydpi)
361 {
362 d->x_aspect = 1;
363 d->y_aspect = xdpi / ydpi;
364 }
365 else
366 {
367 d->x_aspect = ydpi / xdpi;
368 d->y_aspect = 1;
369 }
370 d->ditherfunc = stpi_set_dither_function(v);
371 d->adaptive_limit = .75 * 65535;
372
373 /*
374 * For hybrid EvenTone we want to use the good matrix. For regular
375 * EvenTone, we don't need to pay the cost.
376 */
377
378 if (d->stpi_dither_type == D_VERY_FAST || d->stpi_dither_type ==D_EVENTONE ||
379 d->stpi_dither_type == D_FAST || d->stpi_dither_type == D_PREDITHERED)
380 {
381 if (stp_check_int_parameter(v, "DitherVeryFastSteps",
382 STP_PARAMETER_ACTIVE))
383 stp_dither_set_iterated_matrix
384 (v, 2, stp_get_int_parameter(v, "DitherVeryFastSteps"), sq2, 0, 2,4);
385 else
386 stp_dither_set_iterated_matrix(v, 2, DITHER_FAST_STEPS, sq2, 0, 2, 4);
387 }
388 else if (stp_check_array_parameter(v, "DitherMatrix",
389 STP_PARAMETER_ACTIVE) &&
390 (stp_dither_matrix_validate_array
391 (stp_get_array_parameter(v, "DitherMatrix"))))
392 {
393 stp_dither_set_matrix_from_dither_array
394 (v, stp_get_array_parameter(v, "DitherMatrix"), 0);
395 }
396 else
397 {
398 stp_array_t *array =
399 stp_find_standard_dither_array(d->y_aspect, d->x_aspect);
400 int transposed = d->y_aspect < d->x_aspect ? 1 : 0;
401 STPI_ASSERT(array, v);
402 stp_dither_set_matrix_from_dither_array(v, array, transposed);
403 stp_array_destroy(array);
404 }
405
406 d->src_width = in_width;
407 d->dst_width = out_width;
408
409 stp_dither_set_ink_spread(v, 13);
410 d->channel_count = 0;
411 }
412
413 void
414 stpi_dither_reverse_row_ends(stpi_dither_t *d)
415 {
416 int i;
417 for (i = 0; i < CHANNEL_COUNT(d); i++)
418 {
419 int tmp = CHANNEL(d, i).row_ends[0];
420 CHANNEL(d, i).row_ends[0] =
421 CHANNEL(d, i).row_ends[1];
422 CHANNEL(d, i).row_ends[1] = tmp;
423 }
424 }
425
426 int
427 stp_dither_get_first_position(stp_vars_t *v, int color, int subchannel)
428 {
429 stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither");
430 int channel = stpi_dither_translate_channel(v, color, subchannel);
431 if (channel < 0)
432 return -1;
433 return CHANNEL(d, channel).row_ends[0];
434 }
435
436 int
437 stp_dither_get_last_position(stp_vars_t *v, int color, int subchannel)
438 {
439 stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither");
440 int channel = stpi_dither_translate_channel(v, color, subchannel);
441 if (channel < 0)
442 return -1;
443 return CHANNEL(d, channel).row_ends[1];
444 }
445
446 int *
447 stpi_dither_get_errline(stpi_dither_t *d, int row, int color)
448 {
449 stpi_dither_channel_t *dc;
450 if (row < 0 || color < 0 || color >= CHANNEL_COUNT(d))
451 return NULL;
452 dc = &(CHANNEL(d, color));
453 if (!dc->errs)
454 dc->errs = stp_zalloc(d->error_rows * sizeof(int *));
455 if (!dc->errs[row % dc->error_rows])
456 {
457 int size = 2 * MAX_SPREAD + (16 * ((d->dst_width + 7) / 8));
458 dc->errs[row % dc->error_rows] = stp_zalloc(size * sizeof(int));
459 }
460 return dc->errs[row % dc->error_rows] + MAX_SPREAD;
461 }
462
463 void
464 stp_dither_internal(stp_vars_t *v, int row, const unsigned short *input,
465 int duplicate_line, int zero_mask,
466 const unsigned char *mask)
467 {
468 int i;
469 stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither");
470 stpi_dither_finalize(v);
471 stp_dither_matrix_set_row(&(d->dither_matrix), row);
472 for (i = 0; i < CHANNEL_COUNT(d); i++)
473 {
474 CHANNEL(d, i).ptr = CHANNEL(d, i).ptr;
475 if (CHANNEL(d, i).ptr)
476 memset(CHANNEL(d, i).ptr, 0,
477 (d->dst_width + 7) / 8 * CHANNEL(d, i).signif_bits);
478 CHANNEL(d, i).row_ends[0] = -1;
479 CHANNEL(d, i).row_ends[1] = -1;
480
481 stp_dither_matrix_set_row(&(CHANNEL(d, i).dithermat), row);
482 stp_dither_matrix_set_row(&(CHANNEL(d, i).pick), row);
483 }
484 d->ptr_offset = 0;
485 (d->ditherfunc)(v, row, input, duplicate_line, zero_mask, mask);
486 }
487
488 void
489 stp_dither(stp_vars_t *v, int row, int duplicate_line, int zero_mask,
490 const unsigned char *mask)
491 {
492 const unsigned short *input = stp_channel_get_output(v);
493 stp_dither_internal(v, row, input, duplicate_line, zero_mask, mask);
494 }