| 1 | /* |
| 2 | * |
| 3 | * Performance-critical functions that should be inlined, based on |
| 4 | * measurements. |
| 5 | * |
| 6 | * Copyright 1997-2003 Michael Sweet ([email protected]) and |
| 7 | * Robert Krawitz ([email protected]) |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the Free |
| 11 | * Software Foundation; either version 2 of the License, or (at your option) |
| 12 | * any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 17 | * for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | * Revision History: |
| 23 | * |
| 24 | * See ChangeLog |
| 25 | */ |
| 26 | |
| 27 | #ifndef GUTENPRINT_INTERNAL_DITHER_INLINED_FUNCTIONS_H |
| 28 | #define GUTENPRINT_INTERNAL_DITHER_INLINED_FUNCTIONS_H |
| 29 | |
| 30 | /* |
| 31 | * Inlining has yielded significant (measured) speedup, even with the |
| 32 | * more complicated dither function. --rlk 20011219 |
| 33 | */ |
| 34 | |
| 35 | static inline unsigned |
| 36 | ditherpoint(const stpi_dither_t *d, stp_dither_matrix_impl_t *mat, int x) |
| 37 | { |
| 38 | (void)d; |
| 39 | if (mat->fast_mask) |
| 40 | return mat->matrix[(mat->last_y_mod + |
| 41 | ((x + mat->x_offset) & mat->fast_mask))]; |
| 42 | /* |
| 43 | * This rather bizarre code is an attempt to avoid having to compute a lot |
| 44 | * of modulus and multiplication operations, which are typically slow. |
| 45 | */ |
| 46 | |
| 47 | if (x == mat->last_x + 1) |
| 48 | { |
| 49 | mat->last_x_mod++; |
| 50 | mat->index++; |
| 51 | if (mat->last_x_mod >= mat->x_size) |
| 52 | { |
| 53 | mat->last_x_mod -= mat->x_size; |
| 54 | mat->index -= mat->x_size; |
| 55 | } |
| 56 | } |
| 57 | else if (x == mat->last_x - 1) |
| 58 | { |
| 59 | mat->last_x_mod--; |
| 60 | mat->index--; |
| 61 | if (mat->last_x_mod < 0) |
| 62 | { |
| 63 | mat->last_x_mod += mat->x_size; |
| 64 | mat->index += mat->x_size; |
| 65 | } |
| 66 | } |
| 67 | else if (x != mat->last_x) |
| 68 | { |
| 69 | mat->last_x_mod = (x + mat->x_offset) % mat->x_size; |
| 70 | mat->index = mat->last_x_mod + mat->last_y_mod; |
| 71 | } |
| 72 | mat->last_x = x; |
| 73 | return mat->matrix[mat->index]; |
| 74 | } |
| 75 | |
| 76 | static inline void |
| 77 | set_row_ends(stpi_dither_channel_t *dc, int x) |
| 78 | { |
| 79 | if (dc->row_ends[0] == -1) |
| 80 | dc->row_ends[0] = x; |
| 81 | dc->row_ends[1] = x; |
| 82 | } |
| 83 | |
| 84 | #endif /* GUTENPRINT_INTERNAL_DITHER_INLINED_FUNCTIONS_H */ |