| 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_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 <string.h> |
| 33 | #include "dither-impl.h" |
| 34 | #include "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_COLOR(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 inline int |
| 53 | update_dither(stpi_dither_t *d, int channel, int width, |
| 54 | int direction, int *error0, int *error1) |
| 55 | { |
| 56 | int r = CHANNEL(d, channel).v; |
| 57 | int o = CHANNEL(d, 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->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->spread) + |
| 79 | ((tmp & d->spread_mask) > (tmpo & d->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_table[offset]; |
| 99 | dist1 = tmp / d->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 inline int |
| 123 | print_color(const stpi_dither_t *d, 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->b; |
| 128 | int density = dc->o; |
| 129 | int adjusted = dc->v; |
| 130 | unsigned randomizer = dc->randomizer; |
| 131 | stp_dither_matrix_impl_t *pick_matrix = &(dc->pick); |
| 132 | stp_dither_matrix_impl_t *dither_matrix = &(dc->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->nlevels - 1; |
| 141 | int dither_value = adjusted; |
| 142 | stpi_ink_defn_t *lower; |
| 143 | stpi_ink_defn_t *upper; |
| 144 | |
| 145 | (void)y; |
| 146 | |
| 147 | if (base <= 0 || density <= 0 || |
| 148 | (adjusted <= 0 && !(stpi_dither_type & D_ADAPTIVE_BASE))) |
| 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_t *dd = &(dc->ranges[i]); |
| 164 | |
| 165 | if (density <= dd->lower->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_BASE) |
| 174 | { |
| 175 | stpi_dither_type -= D_ADAPTIVE_BASE; |
| 176 | |
| 177 | if (i < levels || base <= d->adaptive_limit) |
| 178 | { |
| 179 | stpi_dither_type = D_ORDERED; |
| 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->lower; |
| 196 | upper = dd->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_BASE) |
| 204 | { |
| 205 | rangepoint = density - dd->lower->range; |
| 206 | if (dd->range_span < 65535) |
| 207 | rangepoint = rangepoint * 65535 / dd->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->lower->range; |
| 221 | if (dd->range_span < 65535) |
| 222 | rangepoint = rangepoint * 65535 / dd->range_span; |
| 223 | if (dd->value_span == 0) |
| 224 | virtual_value = upper->value; |
| 225 | else /* if (dd->range_span == 0) */ |
| 226 | virtual_value = (upper->value + lower->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_cutoff) |
| 235 | randomizer = 0; |
| 236 | else if (base > d->d_cutoff / 2) |
| 237 | randomizer = randomizer * 2 * (d->d_cutoff - base) / d->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 = ditherpoint(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_t *subc; |
| 287 | |
| 288 | if (dd->is_same_ink) |
| 289 | subc = upper; |
| 290 | else |
| 291 | { |
| 292 | if (rangepoint >= ditherpoint(d, pick_matrix, x)) |
| 293 | subc = upper; |
| 294 | else |
| 295 | subc = lower; |
| 296 | } |
| 297 | v = subc->value; |
| 298 | if (!mask || (*(mask + d->ptr_offset) & bit)) |
| 299 | { |
| 300 | if (dc->ptr) |
| 301 | { |
| 302 | tptr = dc->ptr + d->ptr_offset; |
| 303 | |
| 304 | /* |
| 305 | * Lay down all of the bits in the pixel. |
| 306 | */ |
| 307 | if (dontprint < v) |
| 308 | { |
| 309 | bits = subc->bits; |
| 310 | set_row_ends(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_BASE) |
| 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_initializer(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_COUNT(d); i++) |
| 348 | CHANNEL(d, i).error_rows = 2; |
| 349 | if (!duplicate_line) |
| 350 | { |
| 351 | if ((zero_mask & ((1 << CHANNEL_COUNT(d)) - 1)) != |
| 352 | ((1 << CHANNEL_COUNT(d)) - 1)) |
| 353 | d->last_line_was_empty = 0; |
| 354 | else |
| 355 | d->last_line_was_empty++; |
| 356 | } |
| 357 | else if (d->last_line_was_empty) |
| 358 | d->last_line_was_empty++; |
| 359 | if (d->last_line_was_empty >= 5) |
| 360 | return 0; |
| 361 | else if (d->last_line_was_empty == 4) |
| 362 | { |
| 363 | for (i = 0; i < CHANNEL_COUNT(d); i++) |
| 364 | for (j = 0; j < d->error_rows; j++) |
| 365 | memset(stpi_dither_get_errline(d, row + j, i), 0, |
| 366 | d->dst_width * sizeof(int)); |
| 367 | return 0; |
| 368 | } |
| 369 | d->ptr_offset = (direction == 1) ? 0 : length - 1; |
| 370 | |
| 371 | *error = stp_malloc(CHANNEL_COUNT(d) * sizeof(int **)); |
| 372 | *ndither = stp_malloc(CHANNEL_COUNT(d) * sizeof(int)); |
| 373 | for (i = 0; i < CHANNEL_COUNT(d); i++) |
| 374 | { |
| 375 | (*error)[i] = stp_malloc(d->error_rows * sizeof(int *)); |
| 376 | for (j = 0; j < d->error_rows; j++) |
| 377 | { |
| 378 | (*error)[i][j] = stpi_dither_get_errline(d, row + j, i); |
| 379 | if (j == d->error_rows - 1) |
| 380 | memset((*error)[i][j], 0, d->dst_width * sizeof(int)); |
| 381 | if (direction == -1) |
| 382 | (*error)[i][j] += d->dst_width - 1; |
| 383 | } |
| 384 | (*ndither)[i] = (*error)[i][0][0]; |
| 385 | } |
| 386 | return 1; |
| 387 | } |
| 388 | |
| 389 | static void |
| 390 | shared_ed_deinitializer(stpi_dither_t *d, |
| 391 | int ***error, |
| 392 | int *ndither) |
| 393 | { |
| 394 | int i; |
| 395 | for (i = 0; i < CHANNEL_COUNT(d); i++) |
| 396 | { |
| 397 | STP_SAFE_FREE(error[i]); |
| 398 | } |
| 399 | STP_SAFE_FREE(error); |
| 400 | STP_SAFE_FREE(ndither); |
| 401 | } |
| 402 | |
| 403 | void |
| 404 | stpi_dither_ed(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_t *d = (stpi_dither_t *) stp_get_component_data(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_width + 7) / 8; |
| 424 | if (d->stpi_dither_type & D_ADAPTIVE_BASE) |
| 425 | for (i = 0; i < CHANNEL_COUNT(d); i++) |
| 426 | if (CHANNEL(d, i).nlevels > 1) |
| 427 | { |
| 428 | stpi_dither_ordered(v, row, raw, duplicate_line, zero_mask, mask); |
| 429 | return; |
| 430 | } |
| 431 | if (!shared_ed_initializer(d, row, duplicate_line, zero_mask, length, |
| 432 | direction, &error, &ndither)) |
| 433 | return; |
| 434 | |
| 435 | x = (direction == 1) ? 0 : d->dst_width - 1; |
| 436 | bit = 1 << (7 - (x & 7)); |
| 437 | xstep = CHANNEL_COUNT(d) * (d->src_width / d->dst_width); |
| 438 | xmod = d->src_width % d->dst_width; |
| 439 | xerror = (xmod * x) % d->dst_width; |
| 440 | terminate = (direction == 1) ? d->dst_width : -1; |
| 441 | |
| 442 | if (direction == -1) |
| 443 | raw += (CHANNEL_COUNT(d) * (d->src_width - 1)); |
| 444 | |
| 445 | for (; x != terminate; x += direction) |
| 446 | { |
| 447 | for (i = 0; i < CHANNEL_COUNT(d); i++) |
| 448 | { |
| 449 | if (CHANNEL(d, i).ptr) |
| 450 | { |
| 451 | CHANNEL(d, i).v = raw[i]; |
| 452 | CHANNEL(d, i).o = CHANNEL(d, i).v; |
| 453 | CHANNEL(d, i).b = CHANNEL(d, i).v; |
| 454 | CHANNEL(d, i).v = UPDATE_COLOR(CHANNEL(d, i).v, ndither[i]); |
| 455 | CHANNEL(d, i).v = print_color(d, &(CHANNEL(d, i)), x, row, bit, |
| 456 | length, 0, d->stpi_dither_type, |
| 457 | mask); |
| 458 | ndither[i] = update_dither(d, i, d->src_width, |
| 459 | direction, error[i][0], error[i][1]); |
| 460 | } |
| 461 | } |
| 462 | ADVANCE_BIDIRECTIONAL(d, bit, raw, direction, CHANNEL_COUNT(d), xerror, |
| 463 | xstep, xmod, error, d->error_rows); |
| 464 | } |
| 465 | shared_ed_deinitializer(d, error, ndither); |
| 466 | if (direction == -1) |
| 467 | stpi_dither_reverse_row_ends(d); |
| 468 | } |