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