| 1 | /* |
| 2 | * |
| 3 | * Print plug-in driver utility functions for the GIMP. |
| 4 | * |
| 5 | * Copyright 2001 Robert Krawitz ([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 | * Revision History: |
| 21 | * |
| 22 | * See ChangeLog |
| 23 | */ |
| 24 | |
| 25 | #ifdef HAVE_CONFIG_H |
| 26 | #include <config.h> |
| 27 | #endif |
| 28 | #include <gutenprint/gutenprint.h> |
| 29 | #include "gutenprint-internal.h" |
| 30 | #include <math.h> |
| 31 | #include <string.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <stdio.h> |
| 34 | #include "dither-impl.h" |
| 35 | #include <sys/param.h> |
| 36 | |
| 37 | #ifdef __GNUC__ |
| 38 | #define inline __inline__ |
| 39 | #endif |
| 40 | |
| 41 | |
| 42 | static unsigned |
| 43 | gcd(unsigned a, unsigned b) |
| 44 | { |
| 45 | unsigned tmp; |
| 46 | if (b > a) |
| 47 | { |
| 48 | tmp = a; |
| 49 | a = b; |
| 50 | b = tmp; |
| 51 | } |
| 52 | while (1) |
| 53 | { |
| 54 | tmp = a % b; |
| 55 | if (tmp == 0) |
| 56 | return b; |
| 57 | a = b; |
| 58 | b = tmp; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | static inline int |
| 63 | calc_ordered_point(unsigned x, unsigned y, int steps, int multiplier, |
| 64 | int size, const unsigned *map) |
| 65 | { |
| 66 | int i, j; |
| 67 | unsigned retval = 0; |
| 68 | int divisor = 1; |
| 69 | int div1; |
| 70 | for (i = 0; i < steps; i++) |
| 71 | { |
| 72 | int xa = (x / divisor) % size; |
| 73 | int ya = (y / divisor) % size; |
| 74 | unsigned base; |
| 75 | base = map[ya + (xa * size)]; |
| 76 | div1 = 1; |
| 77 | for (j = i; j < steps - 1; j++) |
| 78 | div1 *= size * size; |
| 79 | retval += base * div1; |
| 80 | divisor *= size; |
| 81 | } |
| 82 | return retval * multiplier; |
| 83 | } |
| 84 | |
| 85 | static int |
| 86 | is_po2(size_t i) |
| 87 | { |
| 88 | if (i == 0) |
| 89 | return 0; |
| 90 | return (((i & (i - 1)) == 0) ? 1 : 0); |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | stp_dither_matrix_iterated_init(stp_dither_matrix_impl_t *mat, size_t size, |
| 95 | size_t expt, const unsigned *array) |
| 96 | { |
| 97 | int i; |
| 98 | int x, y; |
| 99 | mat->base = size; |
| 100 | mat->exp = expt; |
| 101 | mat->x_size = 1; |
| 102 | for (i = 0; i < expt; i++) |
| 103 | mat->x_size *= mat->base; |
| 104 | mat->y_size = mat->x_size; |
| 105 | mat->total_size = mat->x_size * mat->y_size; |
| 106 | mat->matrix = stp_malloc(sizeof(unsigned) * mat->x_size * mat->y_size); |
| 107 | for (x = 0; x < mat->x_size; x++) |
| 108 | for (y = 0; y < mat->y_size; y++) |
| 109 | { |
| 110 | mat->matrix[x + y * mat->x_size] = |
| 111 | calc_ordered_point(x, y, mat->exp, 1, mat->base, array); |
| 112 | mat->matrix[x + y * mat->x_size] = |
| 113 | (double) mat->matrix[x + y * mat->x_size] * 65536.0 / |
| 114 | (double) (mat->x_size * mat->y_size); |
| 115 | } |
| 116 | mat->last_x = mat->last_x_mod = 0; |
| 117 | mat->last_y = mat->last_y_mod = 0; |
| 118 | mat->index = 0; |
| 119 | mat->i_own = 1; |
| 120 | if (is_po2(mat->x_size)) |
| 121 | mat->fast_mask = mat->x_size - 1; |
| 122 | else |
| 123 | mat->fast_mask = 0; |
| 124 | } |
| 125 | |
| 126 | #define MATRIX_POINT(m, x, y, x_size, y_size) \ |
| 127 | ((m)[(((x) + (x_size)) % (x_size)) + ((x_size) * (((y) + (y_size)) % (y_size)))]) |
| 128 | |
| 129 | void |
| 130 | stp_dither_matrix_shear(stp_dither_matrix_impl_t *mat, int x_shear, int y_shear) |
| 131 | { |
| 132 | int i; |
| 133 | int j; |
| 134 | int *tmp = stp_malloc(mat->x_size * mat->y_size * sizeof(int)); |
| 135 | for (i = 0; i < mat->x_size; i++) |
| 136 | for (j = 0; j < mat->y_size; j++) |
| 137 | MATRIX_POINT(tmp, i, j, mat->x_size, mat->y_size) = |
| 138 | MATRIX_POINT(mat->matrix, i, j * (x_shear + 1), mat->x_size, |
| 139 | mat->y_size); |
| 140 | for (i = 0; i < mat->x_size; i++) |
| 141 | for (j = 0; j < mat->y_size; j++) |
| 142 | MATRIX_POINT(mat->matrix, i, j, mat->x_size, mat->y_size) = |
| 143 | MATRIX_POINT(tmp, i * (y_shear + 1), j, mat->x_size, mat->y_size); |
| 144 | stp_free(tmp); |
| 145 | } |
| 146 | |
| 147 | int |
| 148 | stp_dither_matrix_validate_array(const stp_array_t *array) |
| 149 | { |
| 150 | double low, high; |
| 151 | const stp_sequence_t *seq = stp_array_get_sequence(array); |
| 152 | stp_sequence_get_bounds(seq, &low, &high); |
| 153 | if (low < 0 || high > 65535) |
| 154 | return 0; |
| 155 | return 1; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | void |
| 160 | stp_dither_matrix_init_from_dither_array(stp_dither_matrix_impl_t *mat, |
| 161 | const stp_array_t *array, |
| 162 | int transpose) |
| 163 | { |
| 164 | int x, y; |
| 165 | size_t count; |
| 166 | const unsigned short *vec; |
| 167 | int x_size, y_size; |
| 168 | const stp_sequence_t *seq = stp_array_get_sequence(array); |
| 169 | stp_array_get_size(array, &x_size, &y_size); |
| 170 | |
| 171 | vec = stp_sequence_get_ushort_data(seq, &count); |
| 172 | mat->base = x_size; |
| 173 | mat->exp = 1; |
| 174 | if (transpose) |
| 175 | { |
| 176 | mat->x_size = y_size; |
| 177 | mat->y_size = x_size; |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | mat->x_size = x_size; |
| 182 | mat->y_size = y_size; |
| 183 | } |
| 184 | mat->total_size = mat->x_size * mat->y_size; |
| 185 | mat->matrix = stp_malloc(sizeof(unsigned) * mat->x_size * mat->y_size); |
| 186 | for (x = 0; x < x_size; x++) |
| 187 | for (y = 0; y < y_size; y++) |
| 188 | { |
| 189 | if (transpose) |
| 190 | mat->matrix[y + x * y_size] = vec[x + y * x_size]; |
| 191 | else |
| 192 | mat->matrix[x + y * x_size] = vec[x + y * x_size]; |
| 193 | } |
| 194 | mat->last_x = mat->last_x_mod = 0; |
| 195 | mat->last_y = mat->last_y_mod = 0; |
| 196 | mat->index = 0; |
| 197 | mat->i_own = 1; |
| 198 | if (is_po2(mat->x_size)) |
| 199 | mat->fast_mask = mat->x_size - 1; |
| 200 | else |
| 201 | mat->fast_mask = 0; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | void |
| 206 | stp_dither_matrix_init(stp_dither_matrix_impl_t *mat, int x_size, int y_size, |
| 207 | const unsigned int *array, int transpose, int prescaled) |
| 208 | { |
| 209 | int x, y; |
| 210 | mat->base = x_size; |
| 211 | mat->exp = 1; |
| 212 | mat->x_size = x_size; |
| 213 | mat->y_size = y_size; |
| 214 | mat->total_size = mat->x_size * mat->y_size; |
| 215 | mat->matrix = stp_malloc(sizeof(unsigned) * mat->x_size * mat->y_size); |
| 216 | for (x = 0; x < mat->x_size; x++) |
| 217 | for (y = 0; y < mat->y_size; y++) |
| 218 | { |
| 219 | if (transpose) |
| 220 | mat->matrix[x + y * mat->x_size] = array[y + x * mat->y_size]; |
| 221 | else |
| 222 | mat->matrix[x + y * mat->x_size] = array[x + y * mat->x_size]; |
| 223 | if (!prescaled) |
| 224 | mat->matrix[x + y * mat->x_size] = |
| 225 | (double) mat->matrix[x + y * mat->x_size] * 65536.0 / |
| 226 | (double) (mat->x_size * mat->y_size); |
| 227 | } |
| 228 | mat->last_x = mat->last_x_mod = 0; |
| 229 | mat->last_y = mat->last_y_mod = 0; |
| 230 | mat->index = 0; |
| 231 | mat->i_own = 1; |
| 232 | if (is_po2(mat->x_size)) |
| 233 | mat->fast_mask = mat->x_size - 1; |
| 234 | else |
| 235 | mat->fast_mask = 0; |
| 236 | } |
| 237 | |
| 238 | void |
| 239 | stp_dither_matrix_init_short(stp_dither_matrix_impl_t *mat, int x_size, int y_size, |
| 240 | const unsigned short *array, int transpose, |
| 241 | int prescaled) |
| 242 | { |
| 243 | int x, y; |
| 244 | mat->base = x_size; |
| 245 | mat->exp = 1; |
| 246 | mat->x_size = x_size; |
| 247 | mat->y_size = y_size; |
| 248 | mat->total_size = mat->x_size * mat->y_size; |
| 249 | mat->matrix = stp_malloc(sizeof(unsigned) * mat->x_size * mat->y_size); |
| 250 | for (x = 0; x < mat->x_size; x++) |
| 251 | for (y = 0; y < mat->y_size; y++) |
| 252 | { |
| 253 | if (transpose) |
| 254 | mat->matrix[x + y * mat->x_size] = array[y + x * mat->y_size]; |
| 255 | else |
| 256 | mat->matrix[x + y * mat->x_size] = array[x + y * mat->x_size]; |
| 257 | if (!prescaled) |
| 258 | mat->matrix[x + y * mat->x_size] = |
| 259 | (double) mat->matrix[x + y * mat->x_size] * 65536.0 / |
| 260 | (double) (mat->x_size * mat->y_size); |
| 261 | } |
| 262 | mat->last_x = mat->last_x_mod = 0; |
| 263 | mat->last_y = mat->last_y_mod = 0; |
| 264 | mat->index = 0; |
| 265 | mat->i_own = 1; |
| 266 | if (is_po2(mat->x_size)) |
| 267 | mat->fast_mask = mat->x_size - 1; |
| 268 | else |
| 269 | mat->fast_mask = 0; |
| 270 | } |
| 271 | |
| 272 | void |
| 273 | stp_dither_matrix_destroy(stp_dither_matrix_impl_t *mat) |
| 274 | { |
| 275 | if (mat->i_own && mat->matrix) |
| 276 | stp_free(mat->matrix); |
| 277 | mat->matrix = NULL; |
| 278 | mat->base = 0; |
| 279 | mat->exp = 0; |
| 280 | mat->x_size = 0; |
| 281 | mat->y_size = 0; |
| 282 | mat->total_size = 0; |
| 283 | mat->i_own = 0; |
| 284 | } |
| 285 | |
| 286 | void |
| 287 | stp_dither_matrix_clone(const stp_dither_matrix_impl_t *src, stp_dither_matrix_impl_t *dest, |
| 288 | int x_offset, int y_offset) |
| 289 | { |
| 290 | dest->base = src->base; |
| 291 | dest->exp = src->exp; |
| 292 | dest->x_size = src->x_size; |
| 293 | dest->y_size = src->y_size; |
| 294 | dest->total_size = src->total_size; |
| 295 | dest->matrix = src->matrix; |
| 296 | dest->x_offset = x_offset; |
| 297 | dest->y_offset = y_offset; |
| 298 | dest->last_x = 0; |
| 299 | dest->last_x_mod = dest->x_offset % dest->x_size; |
| 300 | dest->last_y = 0; |
| 301 | dest->last_y_mod = dest->x_size * (dest->y_offset % dest->y_size); |
| 302 | dest->index = dest->last_x_mod + dest->last_y_mod; |
| 303 | dest->fast_mask = src->fast_mask; |
| 304 | dest->i_own = 0; |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | stp_dither_matrix_copy(const stp_dither_matrix_impl_t *src, stp_dither_matrix_impl_t *dest) |
| 309 | { |
| 310 | int x; |
| 311 | dest->base = src->base; |
| 312 | dest->exp = src->exp; |
| 313 | dest->x_size = src->x_size; |
| 314 | dest->y_size = src->y_size; |
| 315 | dest->total_size = src->total_size; |
| 316 | dest->matrix = stp_malloc(sizeof(unsigned) * dest->x_size * dest->y_size); |
| 317 | for (x = 0; x < dest->x_size * dest->y_size; x++) |
| 318 | dest->matrix[x] = src->matrix[x]; |
| 319 | dest->x_offset = 0; |
| 320 | dest->y_offset = 0; |
| 321 | dest->last_x = 0; |
| 322 | dest->last_x_mod = 0; |
| 323 | dest->last_y = 0; |
| 324 | dest->last_y_mod = 0; |
| 325 | dest->index = 0; |
| 326 | dest->fast_mask = src->fast_mask; |
| 327 | dest->i_own = 1; |
| 328 | } |
| 329 | |
| 330 | void |
| 331 | stp_dither_matrix_scale_exponentially(stp_dither_matrix_impl_t *mat, double exponent) |
| 332 | { |
| 333 | int i; |
| 334 | int mat_size = mat->x_size * mat->y_size; |
| 335 | for (i = 0; i < mat_size; i++) |
| 336 | { |
| 337 | double dd = mat->matrix[i] / 65535.0; |
| 338 | dd = pow(dd, exponent); |
| 339 | mat->matrix[i] = 65535 * dd; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | void |
| 344 | stp_dither_matrix_set_row(stp_dither_matrix_impl_t *mat, int y) |
| 345 | { |
| 346 | mat->last_y = y; |
| 347 | mat->last_y_mod = mat->x_size * ((y + mat->y_offset) % mat->y_size); |
| 348 | mat->index = mat->last_x_mod + mat->last_y_mod; |
| 349 | } |
| 350 | |
| 351 | static void |
| 352 | preinit_matrix(stp_vars_t *v) |
| 353 | { |
| 354 | stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither"); |
| 355 | int i; |
| 356 | for (i = 0; i < CHANNEL_COUNT(d); i++) |
| 357 | stp_dither_matrix_destroy(&(CHANNEL(d, i).dithermat)); |
| 358 | stp_dither_matrix_destroy(&(d->dither_matrix)); |
| 359 | } |
| 360 | |
| 361 | static void |
| 362 | postinit_matrix(stp_vars_t *v, int x_shear, int y_shear) |
| 363 | { |
| 364 | stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither"); |
| 365 | unsigned rc = 1 + (unsigned) ceil(sqrt(CHANNEL_COUNT(d))); |
| 366 | int i, j; |
| 367 | int color = 0; |
| 368 | unsigned x_n = d->dither_matrix.x_size / rc; |
| 369 | unsigned y_n = d->dither_matrix.y_size / rc; |
| 370 | if (x_shear || y_shear) |
| 371 | stp_dither_matrix_shear(&(d->dither_matrix), x_shear, y_shear); |
| 372 | for (i = 0; i < rc; i++) |
| 373 | for (j = 0; j < rc; j++) |
| 374 | if (color < CHANNEL_COUNT(d)) |
| 375 | { |
| 376 | stp_dither_matrix_clone(&(d->dither_matrix), |
| 377 | &(CHANNEL(d, color).dithermat), |
| 378 | x_n * i, y_n * j); |
| 379 | color++; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void |
| 384 | stp_dither_set_iterated_matrix(stp_vars_t *v, size_t edge, size_t iterations, |
| 385 | const unsigned *data, int prescaled, |
| 386 | int x_shear, int y_shear) |
| 387 | { |
| 388 | stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither"); |
| 389 | (void)prescaled; |
| 390 | |
| 391 | preinit_matrix(v); |
| 392 | stp_dither_matrix_iterated_init(&(d->dither_matrix), edge, iterations, data); |
| 393 | postinit_matrix(v, x_shear, y_shear); |
| 394 | } |
| 395 | |
| 396 | void |
| 397 | stp_dither_set_matrix(stp_vars_t *v, const stp_dither_matrix_generic_t *matrix, |
| 398 | int transposed, int x_shear, int y_shear) |
| 399 | { |
| 400 | stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither"); |
| 401 | int x = transposed ? matrix->y : matrix->x; |
| 402 | int y = transposed ? matrix->x : matrix->y; |
| 403 | preinit_matrix(v); |
| 404 | if (matrix->bytes == 2) |
| 405 | stp_dither_matrix_init_short(&(d->dither_matrix), x, y, |
| 406 | (const unsigned short *) matrix->data, |
| 407 | transposed, matrix->prescaled); |
| 408 | else if (matrix->bytes == 4) |
| 409 | stp_dither_matrix_init(&(d->dither_matrix), x, y, |
| 410 | (const unsigned *)matrix->data, |
| 411 | transposed, matrix->prescaled); |
| 412 | postinit_matrix(v, x_shear, y_shear); |
| 413 | } |
| 414 | |
| 415 | void |
| 416 | stp_dither_set_matrix_from_dither_array(stp_vars_t *v, |
| 417 | const stp_array_t *array, |
| 418 | int transpose) |
| 419 | { |
| 420 | stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither"); |
| 421 | preinit_matrix(v); |
| 422 | stp_dither_matrix_init_from_dither_array(&(d->dither_matrix), array, transpose); |
| 423 | postinit_matrix(v, 0, 0); |
| 424 | } |
| 425 | |
| 426 | void |
| 427 | stp_dither_set_transition(stp_vars_t *v, double exponent) |
| 428 | { |
| 429 | stpi_dither_t *d = (stpi_dither_t *) stp_get_component_data(v, "Dither"); |
| 430 | unsigned rc = 1 + (unsigned) ceil(sqrt(CHANNEL_COUNT(d))); |
| 431 | int i, j; |
| 432 | int color = 0; |
| 433 | unsigned x_n = d->dither_matrix.x_size / rc; |
| 434 | unsigned y_n = d->dither_matrix.y_size / rc; |
| 435 | for (i = 0; i < CHANNEL_COUNT(d); i++) |
| 436 | stp_dither_matrix_destroy(&(CHANNEL(d, i).pick)); |
| 437 | if (exponent < .999 || exponent > 1.001) |
| 438 | for (i = 0; i < rc; i++) |
| 439 | for (j = 0; j < rc; j++) |
| 440 | if (color < CHANNEL_COUNT(d)) |
| 441 | { |
| 442 | stp_dither_matrix_clone(&(d->dither_matrix), |
| 443 | &(CHANNEL(d, color).pick), |
| 444 | x_n * i, y_n * j); |
| 445 | color++; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | static stp_list_t *dither_matrix_cache = NULL; |
| 450 | |
| 451 | typedef struct |
| 452 | { |
| 453 | int x; |
| 454 | int y; |
| 455 | const char *filename; |
| 456 | const stp_array_t *dither_array; |
| 457 | } stp_xml_dither_cache_t; |
| 458 | |
| 459 | static stp_xml_dither_cache_t * |
| 460 | stp_xml_dither_cache_get(int x, int y) |
| 461 | { |
| 462 | stp_list_item_t *ln; |
| 463 | |
| 464 | stp_deprintf(STP_DBG_XML, |
| 465 | "stp_xml_dither_cache_get: lookup %dx%d... ", x, y); |
| 466 | if (!dither_matrix_cache) |
| 467 | { |
| 468 | stp_deprintf(STP_DBG_XML, "cache does not exist\n"); |
| 469 | return NULL; |
| 470 | } |
| 471 | |
| 472 | ln = stp_list_get_start(dither_matrix_cache); |
| 473 | |
| 474 | while (ln) |
| 475 | { |
| 476 | if (((stp_xml_dither_cache_t *) stp_list_item_get_data(ln))->x == x && |
| 477 | ((stp_xml_dither_cache_t *) stp_list_item_get_data(ln))->y == y) |
| 478 | { |
| 479 | |
| 480 | stp_deprintf(STP_DBG_XML, "found\n"); |
| 481 | |
| 482 | return ((stp_xml_dither_cache_t *) stp_list_item_get_data(ln)); |
| 483 | } |
| 484 | ln = stp_list_item_next(ln); |
| 485 | } |
| 486 | stp_deprintf(STP_DBG_XML, "missing\n"); |
| 487 | |
| 488 | return NULL; |
| 489 | } |
| 490 | |
| 491 | static void |
| 492 | stp_xml_dither_cache_set(int x, int y, const char *filename) |
| 493 | { |
| 494 | stp_xml_dither_cache_t *cacheval; |
| 495 | |
| 496 | STPI_ASSERT(x && y && filename, NULL); |
| 497 | |
| 498 | stp_xml_init(); |
| 499 | |
| 500 | if (dither_matrix_cache == NULL) |
| 501 | dither_matrix_cache = stp_list_create(); |
| 502 | |
| 503 | if (stp_xml_dither_cache_get(x, y)) |
| 504 | /* Already cached for this x and y aspect */ |
| 505 | return; |
| 506 | |
| 507 | cacheval = stp_malloc(sizeof(stp_xml_dither_cache_t)); |
| 508 | cacheval->x = x; |
| 509 | cacheval->y = y; |
| 510 | cacheval->filename = stp_strdup(filename); |
| 511 | cacheval->dither_array = NULL; |
| 512 | |
| 513 | stp_list_item_create(dither_matrix_cache, NULL, (void *) cacheval); |
| 514 | |
| 515 | stp_deprintf(STP_DBG_XML, "stp_xml_dither_cache_set: added %dx%d\n", x, y); |
| 516 | |
| 517 | stp_xml_exit(); |
| 518 | |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Parse the <dither-matrix> node. |
| 524 | */ |
| 525 | static int |
| 526 | stp_xml_process_dither_matrix(stp_mxml_node_t *dm, /* The dither matrix node */ |
| 527 | const char *file) /* Source file */ |
| 528 | |
| 529 | { |
| 530 | const char *value; |
| 531 | int x = -1; |
| 532 | int y = -1; |
| 533 | |
| 534 | value = stp_mxmlElementGetAttr(dm, "x-aspect"); |
| 535 | x = stp_xmlstrtol(value); |
| 536 | |
| 537 | value = stp_mxmlElementGetAttr(dm, "y-aspect"); |
| 538 | y = stp_xmlstrtol(value); |
| 539 | |
| 540 | stp_deprintf(STP_DBG_XML, |
| 541 | "stp_xml_process_dither_matrix: x=%d, y=%d\n", x, y); |
| 542 | |
| 543 | stp_xml_dither_cache_set(x, y, file); |
| 544 | return 1; |
| 545 | } |
| 546 | |
| 547 | static stp_array_t * |
| 548 | stpi_dither_array_create_from_xmltree(stp_mxml_node_t *dm, int x, int y) /* Dither matrix node */ |
| 549 | { |
| 550 | const char *stmp; |
| 551 | stp_mxml_node_t *child; |
| 552 | int x_aspect, y_aspect; /* Dither matrix size */ |
| 553 | |
| 554 | /* Get x-size */ |
| 555 | stmp = stp_mxmlElementGetAttr(dm, "x-aspect"); |
| 556 | if (stmp) |
| 557 | { |
| 558 | x_aspect = (int) stp_xmlstrtoul(stmp); |
| 559 | } |
| 560 | else |
| 561 | { |
| 562 | stp_erprintf("stpi_dither_array_create_from_xmltree: \"x-aspect\" missing\n"); |
| 563 | goto error; |
| 564 | } |
| 565 | /* Get y-size */ |
| 566 | stmp = stp_mxmlElementGetAttr(dm, "y-aspect"); |
| 567 | if (stmp) |
| 568 | { |
| 569 | y_aspect = (int) stp_xmlstrtoul(stmp); |
| 570 | } |
| 571 | else |
| 572 | { |
| 573 | stp_erprintf("stpi_dither_array_create_from_xmltree: \"y-aspect\" missing\n"); |
| 574 | goto error; |
| 575 | } |
| 576 | |
| 577 | if (x * y_aspect != y * x_aspect) |
| 578 | { |
| 579 | stp_erprintf("stpi_dither_array_create_from_xmltree: requested aspect of (%d, %d), found (%d, %d)\n", |
| 580 | x, y, x_aspect, y_aspect); |
| 581 | goto error; |
| 582 | } |
| 583 | |
| 584 | /* Now read in the array */ |
| 585 | child = stp_xml_get_node(dm, "array", NULL); |
| 586 | if (child) |
| 587 | return stp_array_create_from_xmltree(child); |
| 588 | else |
| 589 | stp_erprintf("stpi_dither_array_create_from_xmltree: cannot find root\n"); |
| 590 | error: |
| 591 | return NULL; |
| 592 | } |
| 593 | |
| 594 | static stp_array_t * |
| 595 | xml_doc_get_dither_array(stp_mxml_node_t *doc, int x, int y) |
| 596 | { |
| 597 | stp_mxml_node_t *cur; |
| 598 | stp_mxml_node_t *xmlseq; |
| 599 | |
| 600 | if (doc == NULL ) |
| 601 | { |
| 602 | stp_erprintf("xml_doc_get_dither_array: XML file not parsed successfully.\n"); |
| 603 | return NULL; |
| 604 | } |
| 605 | |
| 606 | cur = doc->child; |
| 607 | |
| 608 | if (cur == NULL) |
| 609 | { |
| 610 | stp_erprintf("xml_doc_get_dither_array: empty document\n"); |
| 611 | return NULL; |
| 612 | } |
| 613 | |
| 614 | xmlseq = stp_xml_get_node(cur, "gutenprint", "dither-matrix", NULL); |
| 615 | if (xmlseq == NULL ) |
| 616 | { |
| 617 | stp_erprintf("xml-doc-get-dither-array: XML file is not a dither matrix.\n"); |
| 618 | return NULL; |
| 619 | } |
| 620 | |
| 621 | return stpi_dither_array_create_from_xmltree(xmlseq, x, y); |
| 622 | } |
| 623 | |
| 624 | static stp_array_t * |
| 625 | stpi_dither_array_create_from_file(const char* file, int x, int y) |
| 626 | { |
| 627 | stp_mxml_node_t *doc; |
| 628 | stp_array_t *ret = NULL; |
| 629 | |
| 630 | stp_xml_init(); |
| 631 | |
| 632 | stp_deprintf(STP_DBG_XML, |
| 633 | "stpi_dither_array_create_from_file: reading `%s'...\n", file); |
| 634 | |
| 635 | doc = stp_mxmlLoadFromFile(NULL, file, STP_MXML_NO_CALLBACK); |
| 636 | |
| 637 | if (doc) |
| 638 | { |
| 639 | ret = xml_doc_get_dither_array(doc, x, y); |
| 640 | stp_mxmlDelete(doc); |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | stp_erprintf("stp_curve_create_from_file: unable to read from %s\n", file); |
| 645 | } |
| 646 | |
| 647 | stp_xml_exit(); |
| 648 | |
| 649 | return ret; |
| 650 | } |
| 651 | |
| 652 | static stp_array_t * |
| 653 | stp_xml_get_dither_array(int x, int y) |
| 654 | { |
| 655 | stp_xml_dither_cache_t *cachedval; |
| 656 | stp_array_t *ret; |
| 657 | |
| 658 | cachedval = stp_xml_dither_cache_get(x, y); |
| 659 | |
| 660 | if (cachedval && cachedval->dither_array) |
| 661 | return stp_array_create_copy(cachedval->dither_array); |
| 662 | |
| 663 | if (!cachedval) |
| 664 | { |
| 665 | char buf[MAXPATHLEN+1]; |
| 666 | (void) snprintf(buf, MAXPATHLEN, "dither/matrix-%dx%d.xml", x, y); |
| 667 | stp_xml_parse_file_named(buf); |
| 668 | cachedval = stp_xml_dither_cache_get(x, y); |
| 669 | if (cachedval == NULL || cachedval->filename == NULL) |
| 670 | { |
| 671 | return NULL; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | ret = stpi_dither_array_create_from_file(cachedval->filename, x, y); |
| 676 | |
| 677 | cachedval->dither_array = ret; |
| 678 | return stp_array_create_copy(ret); |
| 679 | } |
| 680 | |
| 681 | void |
| 682 | stpi_init_dither(void) |
| 683 | { |
| 684 | stp_register_xml_parser("dither-matrix", stp_xml_process_dither_matrix); |
| 685 | } |
| 686 | |
| 687 | stp_array_t * |
| 688 | stp_find_standard_dither_array(int x_aspect, int y_aspect) |
| 689 | { |
| 690 | stp_array_t *answer; |
| 691 | int divisor = gcd(x_aspect, y_aspect); |
| 692 | |
| 693 | x_aspect /= divisor; |
| 694 | y_aspect /= divisor; |
| 695 | |
| 696 | if (x_aspect == 3) /* We don't have x3 matrices */ |
| 697 | x_aspect += 1; /* so cheat */ |
| 698 | if (y_aspect == 3) |
| 699 | y_aspect += 1; |
| 700 | |
| 701 | divisor = gcd(x_aspect, y_aspect); |
| 702 | x_aspect /= divisor; |
| 703 | y_aspect /= divisor; |
| 704 | |
| 705 | answer = stp_xml_get_dither_array(x_aspect, y_aspect); |
| 706 | if (answer) |
| 707 | return answer; |
| 708 | answer = stp_xml_get_dither_array(y_aspect, x_aspect); |
| 709 | if (answer) |
| 710 | return answer; |
| 711 | return NULL; |
| 712 | } |