| 1 | /* |
| 2 | * |
| 3 | * EvenTone dither implementation for Gimp-Print |
| 4 | * |
| 5 | * Copyright 2002-2003 Mark Tomlinson ([email protected]) |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the Free |
| 9 | * Software Foundation; either version 2 of the License, or (at your option) |
| 10 | * any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | * for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | * This code uses the Eventone dither algorithm. This is described |
| 21 | * at the website http://www.artofcode.com/eventone/ |
| 22 | * This algorithm is covered by US Patents 5,055,942 and 5,917,614 |
| 23 | * and was invented by Raph Levien <[email protected]> |
| 24 | * It was made available to be used free of charge in GPL-licensed |
| 25 | */ |
| 26 | |
| 27 | #ifdef HAVE_CONFIG_H |
| 28 | #include <config.h> |
| 29 | #endif |
| 30 | #include <gutenprint/gutenprint.h> |
| 31 | #include "gutenprint-internal.h" |
| 32 | #include <gutenprint/gutenprint-intl-internal.h> |
| 33 | #include <string.h> |
| 34 | #include <math.h> |
| 35 | #include <limits.h> |
| 36 | #include "dither-impl.h" |
| 37 | #include "dither-inlined-functions.h" |
| 38 | |
| 39 | typedef struct |
| 40 | { |
| 41 | int dx; |
| 42 | int dy; |
| 43 | int r_sq; |
| 44 | } distance_t; |
| 45 | |
| 46 | typedef struct |
| 47 | { |
| 48 | int d2x; |
| 49 | int d2y; |
| 50 | distance_t d_sq; |
| 51 | int aspect; |
| 52 | int unitone_aspect; |
| 53 | int physical_aspect; |
| 54 | int diff_factor; |
| 55 | stpi_dither_channel_t *dummy_channel; |
| 56 | double transition; /* Exponential scaling for transition region */ |
| 57 | stp_dither_matrix_impl_t transition_matrix; |
| 58 | } eventone_t; |
| 59 | |
| 60 | typedef struct shade_segment |
| 61 | { |
| 62 | distance_t dis; |
| 63 | distance_t *et_dis; |
| 64 | stpi_ink_defn_t lower; |
| 65 | stpi_ink_defn_t upper; |
| 66 | int share_this_channel; |
| 67 | } shade_distance_t; |
| 68 | |
| 69 | |
| 70 | #define EVEN_C1 256 |
| 71 | #define EVEN_C2 (EVEN_C1 * sqrt(3.0) / 2.0) |
| 72 | #define UNITONE_C1 16384 |
| 73 | #define UNITONE_C2 (UNITONE_C1 * sqrt(3.0) / 2.0) |
| 74 | |
| 75 | static void |
| 76 | free_eventone_data(stpi_dither_t *d) |
| 77 | { |
| 78 | int i; |
| 79 | eventone_t *et = (eventone_t *) (d->aux_data); |
| 80 | for (i = 0; i < CHANNEL_COUNT(d); i++) |
| 81 | { |
| 82 | if (CHANNEL(d, i).aux_data) |
| 83 | { |
| 84 | shade_distance_t *shade = (shade_distance_t *) CHANNEL(d,i).aux_data; |
| 85 | STP_SAFE_FREE(shade->et_dis); |
| 86 | STP_SAFE_FREE(CHANNEL(d, i).aux_data); |
| 87 | } |
| 88 | } |
| 89 | if (et->dummy_channel) |
| 90 | { |
| 91 | stpi_dither_channel_t *dc = et->dummy_channel; |
| 92 | shade_distance_t *shade = (shade_distance_t *) dc->aux_data; |
| 93 | STP_SAFE_FREE(shade->et_dis); |
| 94 | STP_SAFE_FREE(dc->aux_data); |
| 95 | stpi_dither_channel_destroy(dc); |
| 96 | STP_SAFE_FREE(et->dummy_channel); |
| 97 | } |
| 98 | if (d->stpi_dither_type & D_UNITONE) |
| 99 | stp_dither_matrix_destroy(&(et->transition_matrix)); |
| 100 | STP_SAFE_FREE(et); |
| 101 | } |
| 102 | |
| 103 | static void |
| 104 | et_setup(stpi_dither_t *d) |
| 105 | { |
| 106 | int size = 2 * MAX_SPREAD + ((d->dst_width + 7) & ~7); |
| 107 | static const int diff_factors[] = {1, 10, 16, 23, 32}; |
| 108 | eventone_t *et = stp_zalloc(sizeof(eventone_t)); |
| 109 | int xa, ya; |
| 110 | int i; |
| 111 | for (i = 0; i < CHANNEL_COUNT(d); i++) |
| 112 | { |
| 113 | CHANNEL(d, i).error_rows = 1; |
| 114 | CHANNEL(d, i).errs = stp_zalloc(1 * sizeof(int *)); |
| 115 | CHANNEL(d, i).errs[0] = stp_zalloc(size * sizeof(int)); |
| 116 | } |
| 117 | if (d->stpi_dither_type & D_UNITONE) |
| 118 | { |
| 119 | stpi_dither_channel_t *dc = stp_zalloc(sizeof(stpi_dither_channel_t)); |
| 120 | stp_dither_matrix_clone(&(d->dither_matrix), &(dc->dithermat), 0, 0); |
| 121 | et->transition = 0.7; |
| 122 | stp_dither_matrix_destroy(&(et->transition_matrix)); |
| 123 | stp_dither_matrix_copy(&(d->dither_matrix), &(et->transition_matrix)); |
| 124 | stp_dither_matrix_scale_exponentially(&(et->transition_matrix), et->transition); |
| 125 | stp_dither_matrix_clone(&(et->transition_matrix), &(dc->pick), 0, 0); |
| 126 | dc->error_rows = 1; |
| 127 | dc->errs = stp_zalloc(1 * sizeof(int *)); |
| 128 | dc->errs[0] = stp_zalloc(size * sizeof(int)); |
| 129 | et->dummy_channel = dc; |
| 130 | } |
| 131 | |
| 132 | xa = d->x_aspect / d->y_aspect; |
| 133 | if (xa == 0) |
| 134 | xa = 1; |
| 135 | et->d_sq.dx = xa * xa; |
| 136 | et->d2x = 2 * et->d_sq.dx; |
| 137 | |
| 138 | ya = d->y_aspect / d->x_aspect; |
| 139 | if (ya == 0) |
| 140 | ya = 1; |
| 141 | et->d_sq.dy = ya * ya; |
| 142 | et->d2y = 2 * et->d_sq.dy; |
| 143 | |
| 144 | et->aspect = EVEN_C2 / (xa * ya); |
| 145 | et->unitone_aspect = UNITONE_C2 / (xa * ya); |
| 146 | et->d_sq.r_sq = 0; |
| 147 | |
| 148 | for (i = 0; i < CHANNEL_COUNT(d); i++) |
| 149 | { |
| 150 | int x; |
| 151 | shade_distance_t *shade = stp_zalloc(sizeof(shade_distance_t)); |
| 152 | shade->dis = et->d_sq; |
| 153 | shade->et_dis = stp_malloc(sizeof(distance_t) * d->dst_width); |
| 154 | if (CHANNEL(d, i).darkness > .1) |
| 155 | shade->share_this_channel = 1; |
| 156 | else |
| 157 | shade->share_this_channel = 0; |
| 158 | for (x = 0; x < d->dst_width; x++) |
| 159 | shade->et_dis[x] = et->d_sq; |
| 160 | CHANNEL(d, i).aux_data = shade; |
| 161 | } |
| 162 | if (et->dummy_channel) |
| 163 | { |
| 164 | int x; |
| 165 | shade_distance_t *shade = stp_zalloc(sizeof(shade_distance_t)); |
| 166 | shade->dis = et->d_sq; |
| 167 | shade->et_dis = stp_malloc(sizeof(distance_t) * d->dst_width); |
| 168 | for (x = 0; x < d->dst_width; x++) |
| 169 | shade->et_dis[x] = et->d_sq; |
| 170 | et->dummy_channel->aux_data = shade; |
| 171 | } |
| 172 | |
| 173 | et->physical_aspect = d->y_aspect / d->x_aspect; |
| 174 | if (et->physical_aspect >= 4) |
| 175 | et->physical_aspect = 4; |
| 176 | else if (et->physical_aspect >= 2) |
| 177 | et->physical_aspect = 2; |
| 178 | else et->physical_aspect = 1; |
| 179 | |
| 180 | et->diff_factor = diff_factors[et->physical_aspect]; |
| 181 | |
| 182 | d->aux_data = et; |
| 183 | d->aux_freefunc = free_eventone_data; |
| 184 | } |
| 185 | |
| 186 | static int |
| 187 | et_initializer(stpi_dither_t *d, int duplicate_line, int zero_mask) |
| 188 | { |
| 189 | int i; |
| 190 | eventone_t *et; |
| 191 | if (!d->aux_data) |
| 192 | et_setup(d); |
| 193 | |
| 194 | et = (eventone_t *) (d->aux_data); |
| 195 | if (!duplicate_line) |
| 196 | { |
| 197 | if ((zero_mask & ((1 << CHANNEL_COUNT(d)) - 1)) != |
| 198 | ((1 << CHANNEL_COUNT(d)) - 1)) |
| 199 | d->last_line_was_empty = 0; |
| 200 | else |
| 201 | d->last_line_was_empty++; |
| 202 | } |
| 203 | else if (d->last_line_was_empty) |
| 204 | d->last_line_was_empty++; |
| 205 | |
| 206 | if (d->last_line_was_empty >= 5) |
| 207 | return 0; |
| 208 | else if (d->last_line_was_empty == 4) |
| 209 | { |
| 210 | if (et->dummy_channel) |
| 211 | memset(et->dummy_channel->errs[0], 0, d->dst_width * sizeof(int)); |
| 212 | for (i = 0; i < CHANNEL_COUNT(d); i++) |
| 213 | memset(CHANNEL(d, i).errs[0], 0, d->dst_width * sizeof(int)); |
| 214 | return 0; |
| 215 | } |
| 216 | for (i = 0; i < CHANNEL_COUNT(d); i++) |
| 217 | CHANNEL(d, i).v = 0; |
| 218 | if (et->dummy_channel) |
| 219 | et->dummy_channel->v = 0; |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | static inline void |
| 224 | advance_eventone_pre(shade_distance_t *sp, eventone_t *et, int x) |
| 225 | { |
| 226 | distance_t *etd = &sp->et_dis[x]; |
| 227 | int t = sp->dis.r_sq + sp->dis.dx; |
| 228 | if (t <= etd->r_sq) |
| 229 | { /* Do eventone calculations */ |
| 230 | sp->dis.r_sq = t; /* Nearest pixel same as last one */ |
| 231 | sp->dis.dx += et->d2x; |
| 232 | } |
| 233 | else |
| 234 | sp->dis = *etd; /* Nearest pixel is from a previous line */ |
| 235 | } |
| 236 | |
| 237 | static inline void |
| 238 | eventone_update(stpi_dither_channel_t *dc, eventone_t *et, |
| 239 | int x, int direction) |
| 240 | { |
| 241 | shade_distance_t *sp = (shade_distance_t *) dc->aux_data; |
| 242 | distance_t *etd = &sp->et_dis[x]; |
| 243 | int t = etd->r_sq + etd->dy; /* r^2 from dot above */ |
| 244 | int u = sp->dis.r_sq + sp->dis.dy; /* r^2 from dot on this line */ |
| 245 | (void)direction; |
| 246 | |
| 247 | if (u < t) |
| 248 | { /* If dot from this line is close */ |
| 249 | t = u; /* Use it instead */ |
| 250 | etd->dx = sp->dis.dx; |
| 251 | etd->dy = sp->dis.dy; |
| 252 | } |
| 253 | etd->dy += et->d2y; |
| 254 | |
| 255 | if (t > 65535) |
| 256 | t = 65535; /* Do some hard limiting */ |
| 257 | etd->r_sq = t; |
| 258 | } |
| 259 | |
| 260 | static inline void |
| 261 | diffuse_error(stpi_dither_channel_t *dc, eventone_t *et, int x, int direction) |
| 262 | { |
| 263 | /* |
| 264 | * Tests to date show that the second diffusion pattern works better |
| 265 | * than the first in most cases. The previous code is being left here |
| 266 | * in case it is later determined that the original code works better. |
| 267 | * -- rlk 20031101 |
| 268 | */ |
| 269 | #if 0 |
| 270 | /* int fraction = (dc->v + (et->diff_factor>>1)) / et->diff_factor; */ |
| 271 | int frac_2 = dc->v + dc->v; |
| 272 | int frac_3 = frac_2 + dc->v; |
| 273 | dc->errs[0][x + MAX_SPREAD] = frac_3; |
| 274 | dc->errs[0][x + MAX_SPREAD - direction] += frac_2; |
| 275 | dc->v -= (frac_2 + frac_3) / 16; |
| 276 | #else |
| 277 | dc->errs[0][x + MAX_SPREAD] = dc->v * 3; |
| 278 | dc->errs[0][x + MAX_SPREAD - direction] += dc->v * 5; |
| 279 | dc->errs[0][x + MAX_SPREAD - (direction * 2)] += dc->v * 1; |
| 280 | dc->v -= dc->v * 9 / 16; |
| 281 | #endif |
| 282 | (void)et; |
| 283 | } |
| 284 | |
| 285 | static inline int |
| 286 | eventone_adjust(stpi_dither_channel_t *dc, eventone_t *et, int dither_point, |
| 287 | unsigned int desired) |
| 288 | { |
| 289 | if (dither_point <= 0) |
| 290 | return 0; |
| 291 | else if (dither_point >= 65535) |
| 292 | return 65535; |
| 293 | if (desired == 0) |
| 294 | dither_point = 0; |
| 295 | else |
| 296 | { |
| 297 | shade_distance_t *shade = (shade_distance_t *) dc->aux_data; |
| 298 | dither_point += shade->dis.r_sq * et->aspect - (EVEN_C1 * 65535) / desired; |
| 299 | if (dither_point > 65535) |
| 300 | dither_point = 65535; |
| 301 | else if (dither_point < 0) |
| 302 | dither_point = 0; |
| 303 | } |
| 304 | return dither_point; |
| 305 | } |
| 306 | |
| 307 | static inline int |
| 308 | unitone_adjust(stpi_dither_channel_t *dc, eventone_t *et, |
| 309 | int dither_point, unsigned int desired) |
| 310 | { |
| 311 | if (dither_point <= 0) |
| 312 | return INT_MIN; |
| 313 | else if (dither_point >= 65535) |
| 314 | return dither_point; |
| 315 | if (desired == 0) |
| 316 | dither_point = INT_MIN; |
| 317 | else |
| 318 | { |
| 319 | shade_distance_t *shade = (shade_distance_t *) dc->aux_data; |
| 320 | dither_point += shade->dis.r_sq * et->unitone_aspect - |
| 321 | (UNITONE_C1 * 65535u) / desired; |
| 322 | } |
| 323 | return dither_point; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | static inline void |
| 328 | find_segment(stpi_dither_channel_t *dc, unsigned inkval, |
| 329 | stpi_ink_defn_t *lower, stpi_ink_defn_t *upper) |
| 330 | { |
| 331 | lower->range = 0; |
| 332 | lower->bits = 0; |
| 333 | |
| 334 | if (dc->nlevels == 1) |
| 335 | { |
| 336 | upper->bits = dc->ink_list[1].bits; |
| 337 | upper->range = dc->ink_list[1].value; |
| 338 | } |
| 339 | else |
| 340 | { |
| 341 | int i; |
| 342 | stpi_ink_defn_t *ip; |
| 343 | |
| 344 | for (i=0, ip = dc->ink_list; i < dc->nlevels - 1; i++, ip++) |
| 345 | { |
| 346 | if (ip->value > inkval) |
| 347 | break; |
| 348 | lower->bits = ip->bits; |
| 349 | lower->range = ip->value; |
| 350 | } |
| 351 | |
| 352 | upper->bits = ip->bits; |
| 353 | upper->range = ip->value; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | static inline int |
| 358 | find_segment_and_ditherpoint(stpi_dither_channel_t *dc, unsigned inkval, |
| 359 | stpi_ink_defn_t *lower, stpi_ink_defn_t *upper) |
| 360 | { |
| 361 | find_segment(dc, inkval, lower, upper); |
| 362 | if (inkval <= lower->range) |
| 363 | return 0; |
| 364 | else if (inkval >= upper->range) |
| 365 | return 65535; |
| 366 | else |
| 367 | return (65535u * (inkval - lower->range)) / (upper->range - lower->range); |
| 368 | } |
| 369 | |
| 370 | static inline void |
| 371 | print_ink(stpi_dither_t *d, unsigned char *tptr, const stpi_ink_defn_t *ink, |
| 372 | unsigned char bit, int length) |
| 373 | { |
| 374 | int j; |
| 375 | |
| 376 | if (tptr != 0) |
| 377 | { |
| 378 | tptr += d->ptr_offset; |
| 379 | switch(ink->bits) |
| 380 | { |
| 381 | case 1: |
| 382 | tptr[0] |= bit; |
| 383 | return; |
| 384 | case 2: |
| 385 | tptr[length] |= bit; |
| 386 | return; |
| 387 | case 3: |
| 388 | tptr[0] |= bit; |
| 389 | tptr[length] |= bit; |
| 390 | return; |
| 391 | default: |
| 392 | for (j=1; j <= ink->bits; j+=j, tptr += length) |
| 393 | { |
| 394 | if (j & ink->bits) |
| 395 | *tptr |= bit; |
| 396 | } |
| 397 | return; |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | void |
| 403 | stpi_dither_et(stp_vars_t *v, |
| 404 | int row, |
| 405 | const unsigned short *raw, |
| 406 | int duplicate_line, |
| 407 | int zero_mask, |
| 408 | const unsigned char *mask) |
| 409 | { |
| 410 | stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither"); |
| 411 | eventone_t *et; |
| 412 | |
| 413 | int x; |
| 414 | int length; |
| 415 | unsigned char bit; |
| 416 | int i; |
| 417 | |
| 418 | int terminate; |
| 419 | int direction; |
| 420 | int xerror, xstep, xmod; |
| 421 | int channel_count = CHANNEL_COUNT(d); |
| 422 | |
| 423 | if (!et_initializer(d, duplicate_line, zero_mask)) |
| 424 | return; |
| 425 | |
| 426 | et = (eventone_t *) d->aux_data; |
| 427 | if (d->stpi_dither_type & D_UNITONE) |
| 428 | stp_dither_matrix_set_row(&(et->transition_matrix), row); |
| 429 | |
| 430 | length = (d->dst_width + 7) / 8; |
| 431 | |
| 432 | if (row & 1) |
| 433 | { |
| 434 | direction = 1; |
| 435 | x = 0; |
| 436 | terminate = d->dst_width; |
| 437 | d->ptr_offset = 0; |
| 438 | } |
| 439 | else |
| 440 | { |
| 441 | direction = -1; |
| 442 | x = d->dst_width - 1; |
| 443 | terminate = -1; |
| 444 | d->ptr_offset = length - 1; |
| 445 | raw += channel_count * (d->src_width - 1); |
| 446 | } |
| 447 | bit = 1 << (7 - (x & 7)); |
| 448 | xstep = channel_count * (d->src_width / d->dst_width); |
| 449 | xmod = d->src_width % d->dst_width; |
| 450 | xerror = (xmod * x) % d->dst_width; |
| 451 | |
| 452 | for (; x != terminate; x += direction) |
| 453 | { |
| 454 | |
| 455 | int point_error = 0; |
| 456 | int comparison = 32768; |
| 457 | |
| 458 | if (d->stpi_dither_type & D_ORDERED_BASE) |
| 459 | comparison += (ditherpoint(d, &(d->dither_matrix), x) / 16) - 2048; |
| 460 | |
| 461 | for (i=0; i < channel_count; i++) |
| 462 | { |
| 463 | if (CHANNEL(d, i).ptr) |
| 464 | { |
| 465 | int inkspot; |
| 466 | int range_point; |
| 467 | stpi_dither_channel_t *dc = &CHANNEL(d, i); |
| 468 | shade_distance_t *sp = (shade_distance_t *) dc->aux_data; |
| 469 | stpi_ink_defn_t *inkp; |
| 470 | stpi_ink_defn_t lower, upper; |
| 471 | |
| 472 | advance_eventone_pre(sp, et, x); |
| 473 | |
| 474 | /* |
| 475 | * Find which are the two candidate dot sizes. |
| 476 | * Rather than use the absolute value of the point to compute |
| 477 | * the error, we will use the relative value of the point within |
| 478 | * the range to find the two candidate dot sizes. |
| 479 | */ |
| 480 | range_point = |
| 481 | find_segment_and_ditherpoint(dc, raw[i], &lower, &upper); |
| 482 | |
| 483 | /* Incorporate error data from previous line */ |
| 484 | dc->v += 2 * range_point + (dc->errs[0][x + MAX_SPREAD] + 8) / 16; |
| 485 | inkspot = dc->v - range_point; |
| 486 | |
| 487 | point_error += eventone_adjust(dc, et, inkspot, range_point); |
| 488 | |
| 489 | /* Determine whether to print the larger or smaller dot */ |
| 490 | inkp = &lower; |
| 491 | if (point_error >= comparison) |
| 492 | { |
| 493 | point_error -= 65535; |
| 494 | inkp = &upper; |
| 495 | dc->v -= 131070; |
| 496 | sp->dis = et->d_sq; |
| 497 | } |
| 498 | |
| 499 | /* Adjust the error to reflect the dot choice */ |
| 500 | if (inkp->bits) |
| 501 | { |
| 502 | if (!mask || (*(mask + d->ptr_offset) & bit)) |
| 503 | { |
| 504 | set_row_ends(dc, x); |
| 505 | |
| 506 | /* Do the printing */ |
| 507 | print_ink(d, dc->ptr, inkp, bit, length); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /* Spread the error around to the adjacent dots */ |
| 512 | eventone_update(dc, et, x, direction); |
| 513 | diffuse_error(dc, et, x, direction); |
| 514 | } |
| 515 | } |
| 516 | if (direction == 1) |
| 517 | ADVANCE_UNIDIRECTIONAL(d, bit, raw, channel_count, xerror, xstep, xmod); |
| 518 | else |
| 519 | ADVANCE_REVERSE(d, bit, raw, channel_count, xerror, xstep, xmod); |
| 520 | } |
| 521 | if (direction == -1) |
| 522 | stpi_dither_reverse_row_ends(d); |
| 523 | } |
| 524 | |
| 525 | void |
| 526 | stpi_dither_ut(stp_vars_t *v, |
| 527 | int row, |
| 528 | const unsigned short *raw, |
| 529 | int duplicate_line, |
| 530 | int zero_mask, |
| 531 | const unsigned char *mask) |
| 532 | { |
| 533 | stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither"); |
| 534 | eventone_t *et; |
| 535 | |
| 536 | int x; |
| 537 | int length; |
| 538 | unsigned char bit; |
| 539 | int i; |
| 540 | |
| 541 | int terminate; |
| 542 | int direction; |
| 543 | int xerror, xstep, xmod; |
| 544 | int channel_count = CHANNEL_COUNT(d); |
| 545 | stpi_dither_channel_t *ddc; |
| 546 | |
| 547 | if (channel_count == 1) |
| 548 | { |
| 549 | stpi_dither_et(v, row, raw, duplicate_line, zero_mask, mask); |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | if (!et_initializer(d, duplicate_line, zero_mask)) |
| 554 | return; |
| 555 | |
| 556 | et = (eventone_t *) d->aux_data; |
| 557 | ddc = et->dummy_channel; |
| 558 | |
| 559 | length = (d->dst_width + 7) / 8; |
| 560 | |
| 561 | if (row & 1) |
| 562 | { |
| 563 | direction = 1; |
| 564 | x = 0; |
| 565 | terminate = d->dst_width; |
| 566 | d->ptr_offset = 0; |
| 567 | } |
| 568 | else |
| 569 | { |
| 570 | direction = -1; |
| 571 | x = d->dst_width - 1; |
| 572 | terminate = -1; |
| 573 | d->ptr_offset = length - 1; |
| 574 | raw += channel_count * (d->src_width - 1); |
| 575 | } |
| 576 | bit = 1 << (7 - (x & 7)); |
| 577 | xstep = channel_count * (d->src_width / d->dst_width); |
| 578 | xmod = d->src_width % d->dst_width; |
| 579 | xerror = (xmod * x) % d->dst_width; |
| 580 | |
| 581 | for (; x != terminate; x += direction) |
| 582 | { |
| 583 | |
| 584 | shade_distance_t *ssp = (shade_distance_t *) ddc->aux_data; |
| 585 | int point_error = 0; |
| 586 | int total_error = 0; |
| 587 | int channels_to_print = 0; |
| 588 | int print_all_channels = 0; |
| 589 | int maximum_value = 0; |
| 590 | int comparison = 32768; |
| 591 | stpi_dither_channel_t *best_channel = NULL; |
| 592 | stpi_dither_channel_t *second_best_channel = NULL; |
| 593 | int best_channel_value = INT_MIN; |
| 594 | int second_best_channel_value = INT_MIN; |
| 595 | int random_value = ditherpoint(d, &(d->dither_matrix), x); |
| 596 | |
| 597 | if (d->stpi_dither_type & D_ORDERED_BASE) |
| 598 | comparison += (random_value / 16) - 2048; |
| 599 | |
| 600 | |
| 601 | ddc->b = 0; |
| 602 | advance_eventone_pre(ssp, et, x); |
| 603 | |
| 604 | for (i=0; i < channel_count; i++) |
| 605 | { |
| 606 | stpi_dither_channel_t *dc = &CHANNEL(d, i); |
| 607 | if (dc->ptr) |
| 608 | { |
| 609 | shade_distance_t *sp = (shade_distance_t *) dc->aux_data; |
| 610 | |
| 611 | advance_eventone_pre(sp, et, x); |
| 612 | |
| 613 | /* |
| 614 | * Find which are the two candidate dot sizes. |
| 615 | * Rather than use the absolute value of the point to compute |
| 616 | * the error, we will use the relative value of the point within |
| 617 | * the range to find the two candidate dot sizes. |
| 618 | */ |
| 619 | dc->b = find_segment_and_ditherpoint(dc, raw[i], |
| 620 | &(sp->lower), &(sp->upper)); |
| 621 | if (sp->share_this_channel) |
| 622 | { |
| 623 | if (dc->b > maximum_value) |
| 624 | maximum_value = dc->b; |
| 625 | ddc->b += dc->b; |
| 626 | } |
| 627 | /* Incorporate error data from previous line */ |
| 628 | dc->v += 2 * dc->b + (dc->errs[0][x + MAX_SPREAD] + 8) / 16; |
| 629 | dc->o = unitone_adjust(dc, et, dc->v - dc->b, dc->b); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | #if 0 |
| 634 | if ((2 * (ddc->b - maximum_value)) < (3 * maximum_value)) |
| 635 | print_all_channels = 1; |
| 636 | #endif |
| 637 | |
| 638 | if (ddc->b > 131070) |
| 639 | print_all_channels = 1; |
| 640 | else if (ddc->b > 65535) |
| 641 | { |
| 642 | ddc->b -= 65535; |
| 643 | channels_to_print = 1; |
| 644 | } |
| 645 | |
| 646 | if (ddc->b > 65535) |
| 647 | ddc->b = 65535; |
| 648 | |
| 649 | ddc->v += 2 * ddc->b + (ddc->errs[0][x + MAX_SPREAD] + 8) / 16; |
| 650 | total_error += eventone_adjust(ddc, et, ddc->v - ddc->b, ddc->b); |
| 651 | if (total_error >= comparison) |
| 652 | channels_to_print += 1; |
| 653 | |
| 654 | if (!print_all_channels) |
| 655 | { |
| 656 | for (i=0; i < channel_count; i++) |
| 657 | { |
| 658 | stpi_dither_channel_t *dc = &CHANNEL(d, i); |
| 659 | shade_distance_t *sp = (shade_distance_t *) dc->aux_data; |
| 660 | |
| 661 | if (dc->ptr) |
| 662 | { |
| 663 | |
| 664 | if (sp->share_this_channel) |
| 665 | { |
| 666 | if (dc->o > best_channel_value) |
| 667 | { |
| 668 | second_best_channel = best_channel; |
| 669 | best_channel = dc; |
| 670 | second_best_channel_value = best_channel_value; |
| 671 | if (dc->o >= 32768) |
| 672 | best_channel_value = INT_MAX; |
| 673 | else |
| 674 | best_channel_value = dc->o; |
| 675 | } |
| 676 | else if (dc->o > second_best_channel_value) |
| 677 | { |
| 678 | second_best_channel = dc; |
| 679 | if (dc->o >= 32768) |
| 680 | second_best_channel_value = INT_MAX; |
| 681 | else |
| 682 | second_best_channel_value = dc->o; |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | for (i=0; i < channel_count; i++) |
| 690 | { |
| 691 | stpi_dither_channel_t *dc = &CHANNEL(d, i); |
| 692 | if (dc->ptr) |
| 693 | { |
| 694 | /* Determine whether to print the larger or smaller dot */ |
| 695 | shade_distance_t *sp = (shade_distance_t *) dc->aux_data; |
| 696 | stpi_ink_defn_t *inkp = &(sp->lower); |
| 697 | |
| 698 | if (dc->o < 0) |
| 699 | dc->o = 0; |
| 700 | else if (dc->o > 65535) |
| 701 | dc->o = 65535; |
| 702 | if (print_all_channels || !sp->share_this_channel) |
| 703 | { |
| 704 | point_error += dc->o; |
| 705 | if (point_error >= comparison) |
| 706 | { |
| 707 | point_error -= 65535; |
| 708 | inkp = &(sp->upper); |
| 709 | dc->v -= 131070; |
| 710 | sp->dis = et->d_sq; |
| 711 | } |
| 712 | } |
| 713 | else if ((channels_to_print >= 1 && best_channel == dc) || |
| 714 | (channels_to_print >= 2 && second_best_channel == dc)) |
| 715 | { |
| 716 | inkp = &(sp->upper); |
| 717 | dc->v -= 131070; |
| 718 | sp->dis = et->d_sq; |
| 719 | } |
| 720 | if (inkp->bits) |
| 721 | { |
| 722 | if (!mask || (*(mask + d->ptr_offset) & bit)) |
| 723 | { |
| 724 | set_row_ends(dc, x); |
| 725 | |
| 726 | /* Do the printing */ |
| 727 | print_ink(d, dc->ptr, inkp, bit, length); |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | if (total_error >= comparison) |
| 733 | { |
| 734 | ddc->v -= 131070; |
| 735 | total_error -= 65535; |
| 736 | ssp->dis = et->d_sq; |
| 737 | } |
| 738 | |
| 739 | eventone_update(ddc, et, x, direction); |
| 740 | diffuse_error(ddc, et, x, direction); |
| 741 | for (i=0; i < channel_count; i++) |
| 742 | { |
| 743 | stpi_dither_channel_t *dc = &CHANNEL(d, i); |
| 744 | if (dc->ptr) |
| 745 | { |
| 746 | /* Spread the error around to the adjacent dots */ |
| 747 | eventone_update(dc, et, x, direction); |
| 748 | diffuse_error(dc, et, x, direction); |
| 749 | } |
| 750 | } |
| 751 | if (direction == 1) |
| 752 | ADVANCE_UNIDIRECTIONAL(d, bit, raw, channel_count, xerror, xstep, xmod); |
| 753 | else |
| 754 | ADVANCE_REVERSE(d, bit, raw, channel_count, xerror, xstep, xmod); |
| 755 | } |
| 756 | if (direction == -1) |
| 757 | stpi_dither_reverse_row_ends(d); |
| 758 | } |