| 1 | /* |
| 2 | * |
| 3 | * Very fast 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 | static inline void |
| 36 | print_color_very_fast(const stpi_dither_t *d, stpi_dither_channel_t *dc, |
| 37 | int val, int x, int y, unsigned char bit, |
| 38 | unsigned bits, int length) |
| 39 | { |
| 40 | int j; |
| 41 | (void)y; |
| 42 | |
| 43 | if (bits && val >= ditherpoint(d, &(dc->dithermat), x)) |
| 44 | { |
| 45 | unsigned char *tptr = dc->ptr + d->ptr_offset; |
| 46 | |
| 47 | /* |
| 48 | * Lay down all of the bits in the pixel. |
| 49 | */ |
| 50 | set_row_ends(dc, x); |
| 51 | for (j = 1; j <= bits; j += j, tptr += length) |
| 52 | { |
| 53 | if (j & bits) |
| 54 | tptr[0] |= bit; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | stpi_dither_very_fast(stp_vars_t *v, |
| 61 | int row, |
| 62 | const unsigned short *raw, |
| 63 | int duplicate_line, |
| 64 | int zero_mask, |
| 65 | const unsigned char *mask) |
| 66 | { |
| 67 | stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither"); |
| 68 | int x, |
| 69 | length; |
| 70 | unsigned char *bit_patterns; |
| 71 | unsigned char bit; |
| 72 | int i; |
| 73 | int one_bit_only = 1; |
| 74 | |
| 75 | int xerror, xstep, xmod; |
| 76 | (void)duplicate_line; |
| 77 | |
| 78 | if ((zero_mask & ((1 << CHANNEL_COUNT(d)) - 1)) == |
| 79 | ((1 << CHANNEL_COUNT(d)) - 1)) |
| 80 | return; |
| 81 | |
| 82 | length = (d->dst_width + 7) / 8; |
| 83 | |
| 84 | bit = 128; |
| 85 | xstep = CHANNEL_COUNT(d) * (d->src_width / d->dst_width); |
| 86 | xmod = d->src_width % d->dst_width; |
| 87 | xerror = 0; |
| 88 | |
| 89 | bit_patterns = stp_zalloc(sizeof(unsigned char) * CHANNEL_COUNT(d)); |
| 90 | for (i = 0; i < CHANNEL_COUNT(d); i++) |
| 91 | { |
| 92 | stpi_dither_channel_t *dc = &(CHANNEL(d, i)); |
| 93 | if (dc->nlevels > 0) |
| 94 | bit_patterns[i] = dc->ranges[dc->nlevels - 1].upper->bits; |
| 95 | if (bit_patterns[i] != 1) |
| 96 | one_bit_only = 0; |
| 97 | } |
| 98 | if (one_bit_only) |
| 99 | { |
| 100 | for (x = 0; x < d->dst_width; x ++) |
| 101 | { |
| 102 | if (!mask || (*(mask + d->ptr_offset) & bit)) |
| 103 | { |
| 104 | for (i = 0; i < CHANNEL_COUNT(d); i++) |
| 105 | { |
| 106 | if (raw[i] && |
| 107 | raw[i] >= ditherpoint(d, &(CHANNEL(d, i).dithermat), x)) |
| 108 | { |
| 109 | set_row_ends(&(CHANNEL(d, i)), x); |
| 110 | CHANNEL(d, i).ptr[d->ptr_offset] |= bit; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | ADVANCE_UNIDIRECTIONAL(d, bit, raw, CHANNEL_COUNT(d), |
| 115 | xerror, xstep, xmod); |
| 116 | } |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | for (x = 0; x < d->dst_width; x ++) |
| 121 | { |
| 122 | if (!mask || (*(mask + d->ptr_offset) & bit)) |
| 123 | { |
| 124 | for (i = 0; i < CHANNEL_COUNT(d); i++) |
| 125 | { |
| 126 | if (CHANNEL(d, i).ptr && raw[i]) |
| 127 | print_color_very_fast(d, &(CHANNEL(d, i)), raw[i], x, row, |
| 128 | bit, bit_patterns[i], length); |
| 129 | } |
| 130 | } |
| 131 | ADVANCE_UNIDIRECTIONAL(d, bit, raw, CHANNEL_COUNT(d), |
| 132 | xerror, xstep, xmod); |
| 133 | } |
| 134 | } |
| 135 | stp_free(bit_patterns); |
| 136 | } |