| 1 | /* |
| 2 | * |
| 3 | * Sequence data type. This type is designed to be derived from by |
| 4 | * the curve and dither matrix types. |
| 5 | * |
| 6 | * Copyright 2002-2003 Robert Krawitz ([email protected]) |
| 7 | * Copyright 2003 Roger Leigh ([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 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | #include <gutenprint/gutenprint.h> |
| 27 | #include <gutenprint/sequence.h> |
| 28 | #include "gutenprint-internal.h" |
| 29 | #include <gutenprint/gutenprint-intl-internal.h> |
| 30 | #include <math.h> |
| 31 | #ifdef sun |
| 32 | #include <ieeefp.h> |
| 33 | #endif |
| 34 | #include <string.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <limits.h> |
| 37 | #include <errno.h> |
| 38 | #include <ctype.h> |
| 39 | |
| 40 | struct stp_sequence |
| 41 | { |
| 42 | int recompute_range; /* Do we need to recompute the min and max? */ |
| 43 | double blo; /* Lower bound */ |
| 44 | double bhi; /* Upper bound */ |
| 45 | double rlo; /* Lower range limit */ |
| 46 | double rhi; /* Upper range limit */ |
| 47 | size_t size; /* Number of points */ |
| 48 | double *data; /* Array of doubles */ |
| 49 | float *float_data; /* Data converted to other form */ |
| 50 | long *long_data; |
| 51 | unsigned long *ulong_data; |
| 52 | int *int_data; |
| 53 | unsigned *uint_data; |
| 54 | short *short_data; |
| 55 | unsigned short *ushort_data; |
| 56 | }; |
| 57 | |
| 58 | /* |
| 59 | * We could do more sanity checks here if we want. |
| 60 | */ |
| 61 | |
| 62 | #define CHECK_SEQUENCE(sequence) STPI_ASSERT(sequence, NULL) |
| 63 | |
| 64 | static void |
| 65 | sequence_ctor(stp_sequence_t *sequence) |
| 66 | { |
| 67 | sequence->rlo = sequence->blo = 0.0; |
| 68 | sequence->rhi = sequence->bhi = 1.0; |
| 69 | sequence->recompute_range = 1; |
| 70 | sequence->size = 0; |
| 71 | sequence->data = NULL; |
| 72 | } |
| 73 | |
| 74 | stp_sequence_t * |
| 75 | stp_sequence_create(void) |
| 76 | { |
| 77 | stp_sequence_t *ret; |
| 78 | ret = stp_zalloc(sizeof(stp_sequence_t)); |
| 79 | sequence_ctor(ret); |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | invalidate_auxilliary_data(stp_sequence_t *sequence) |
| 85 | { |
| 86 | STP_SAFE_FREE(sequence->float_data); |
| 87 | STP_SAFE_FREE(sequence->long_data); |
| 88 | STP_SAFE_FREE(sequence->ulong_data); |
| 89 | STP_SAFE_FREE(sequence->int_data); |
| 90 | STP_SAFE_FREE(sequence->uint_data); |
| 91 | STP_SAFE_FREE(sequence->short_data); |
| 92 | STP_SAFE_FREE(sequence->ushort_data); |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | sequence_dtor(stp_sequence_t *sequence) |
| 97 | { |
| 98 | invalidate_auxilliary_data(sequence); |
| 99 | if (sequence->data) |
| 100 | stp_free(sequence->data); |
| 101 | memset(sequence, 0, sizeof(stp_sequence_t)); |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | stp_sequence_destroy(stp_sequence_t *sequence) |
| 106 | { |
| 107 | CHECK_SEQUENCE(sequence); |
| 108 | sequence_dtor(sequence); |
| 109 | stp_free(sequence); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | stp_sequence_copy(stp_sequence_t *dest, const stp_sequence_t *source) |
| 114 | { |
| 115 | CHECK_SEQUENCE(dest); |
| 116 | CHECK_SEQUENCE(source); |
| 117 | |
| 118 | dest->recompute_range = source->recompute_range; |
| 119 | dest->blo = source->blo; |
| 120 | dest->bhi = source->bhi; |
| 121 | dest->rlo = source->rlo; |
| 122 | dest->rhi = source->rhi; |
| 123 | dest->size = source->size; |
| 124 | dest->data = stp_zalloc(sizeof(double) * source->size); |
| 125 | memcpy(dest->data, source->data, (sizeof(double) * source->size)); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | stp_sequence_reverse(stp_sequence_t *dest, const stp_sequence_t *source) |
| 130 | { |
| 131 | unsigned int i; |
| 132 | CHECK_SEQUENCE(dest); |
| 133 | CHECK_SEQUENCE(source); |
| 134 | |
| 135 | dest->recompute_range = source->recompute_range; |
| 136 | dest->blo = source->blo; |
| 137 | dest->bhi = source->bhi; |
| 138 | dest->rlo = source->rlo; |
| 139 | dest->rhi = source->rhi; |
| 140 | dest->size = source->size; |
| 141 | dest->data = stp_zalloc(sizeof(double) * source->size); |
| 142 | for (i = 0; i < source->size; i++) |
| 143 | dest->data[i] = source->data[source->size - i - 1]; |
| 144 | } |
| 145 | |
| 146 | stp_sequence_t * |
| 147 | stp_sequence_create_copy(const stp_sequence_t *sequence) |
| 148 | { |
| 149 | stp_sequence_t *ret; |
| 150 | CHECK_SEQUENCE(sequence); |
| 151 | ret = stp_sequence_create(); |
| 152 | stp_sequence_copy(ret, sequence); |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | stp_sequence_t * |
| 157 | stp_sequence_create_reverse(const stp_sequence_t *sequence) |
| 158 | { |
| 159 | stp_sequence_t *ret; |
| 160 | CHECK_SEQUENCE(sequence); |
| 161 | ret = stp_sequence_create(); |
| 162 | stp_sequence_reverse(ret, sequence); |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | int |
| 167 | stp_sequence_set_bounds(stp_sequence_t *sequence, double low, double high) |
| 168 | { |
| 169 | CHECK_SEQUENCE(sequence); |
| 170 | if (low > high) |
| 171 | return 0; |
| 172 | sequence->rlo = sequence->blo = low; |
| 173 | sequence->rhi = sequence->bhi = high; |
| 174 | sequence->recompute_range = 1; |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | stp_sequence_get_bounds(const stp_sequence_t *sequence, |
| 180 | double *low, double *high) |
| 181 | { |
| 182 | CHECK_SEQUENCE(sequence); |
| 183 | *low = sequence->blo; |
| 184 | *high = sequence->bhi; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | /* |
| 189 | * Find the minimum and maximum points on the curve. |
| 190 | */ |
| 191 | static void |
| 192 | scan_sequence_range(stp_sequence_t *sequence) |
| 193 | { |
| 194 | unsigned int i; |
| 195 | sequence->rlo = sequence->bhi; |
| 196 | sequence->rhi = sequence->blo; |
| 197 | if (sequence->size) |
| 198 | for (i = 0; i < sequence->size; i++) |
| 199 | { |
| 200 | if (sequence->data[i] < sequence->rlo) |
| 201 | sequence->rlo = sequence->data[i]; |
| 202 | if (sequence->data[i] > sequence->rhi) |
| 203 | sequence->rhi = sequence->data[i]; |
| 204 | } |
| 205 | sequence->recompute_range = 0; /* Don't recompute unless the data changes */ |
| 206 | } |
| 207 | |
| 208 | void |
| 209 | stp_sequence_get_range(const stp_sequence_t *sequence, |
| 210 | double *low, double *high) |
| 211 | { |
| 212 | if (sequence->recompute_range) /* Don't recompute the range if we don't |
| 213 | need to. */ |
| 214 | scan_sequence_range((stp_sequence_t *) stpi_cast_safe(sequence)); |
| 215 | *low = sequence->rlo; |
| 216 | *high = sequence->rhi; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | int |
| 221 | stp_sequence_set_size(stp_sequence_t *sequence, size_t size) |
| 222 | { |
| 223 | CHECK_SEQUENCE(sequence); |
| 224 | if (sequence->data) /* Free old data */ |
| 225 | { |
| 226 | stp_free(sequence->data); |
| 227 | sequence->data = NULL; |
| 228 | } |
| 229 | sequence->size = size; |
| 230 | sequence->recompute_range = 1; /* Always recompute on change */ |
| 231 | if (size == 0) |
| 232 | return 1; |
| 233 | invalidate_auxilliary_data(sequence); |
| 234 | sequence->data = stp_zalloc(sizeof(double) * size); |
| 235 | return 1; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | size_t |
| 240 | stp_sequence_get_size(const stp_sequence_t *sequence) |
| 241 | { |
| 242 | CHECK_SEQUENCE(sequence); |
| 243 | return sequence->size; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | |
| 248 | int |
| 249 | stp_sequence_set_data(stp_sequence_t *sequence, |
| 250 | size_t size, const double *data) |
| 251 | { |
| 252 | CHECK_SEQUENCE(sequence); |
| 253 | sequence->size = size; |
| 254 | if (sequence->data) |
| 255 | stp_free(sequence->data); |
| 256 | sequence->data = stp_malloc(sizeof(double) * size); |
| 257 | memcpy(sequence->data, data, (sizeof(double) * size)); |
| 258 | invalidate_auxilliary_data(sequence); |
| 259 | sequence->recompute_range = 1; |
| 260 | return 1; |
| 261 | } |
| 262 | |
| 263 | int |
| 264 | stp_sequence_set_subrange(stp_sequence_t *sequence, size_t where, |
| 265 | size_t size, const double *data) |
| 266 | { |
| 267 | CHECK_SEQUENCE(sequence); |
| 268 | if (where + size > sequence->size) /* Exceeds data size */ |
| 269 | return 0; |
| 270 | memcpy(sequence->data+where, data, (sizeof(double) * size)); |
| 271 | invalidate_auxilliary_data(sequence); |
| 272 | sequence->recompute_range = 1; |
| 273 | return 1; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | void |
| 278 | stp_sequence_get_data(const stp_sequence_t *sequence, size_t *size, |
| 279 | const double **data) |
| 280 | { |
| 281 | CHECK_SEQUENCE(sequence); |
| 282 | *size = sequence->size; |
| 283 | *data = sequence->data; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | int |
| 288 | stp_sequence_set_point(stp_sequence_t *sequence, size_t where, |
| 289 | double data) |
| 290 | { |
| 291 | CHECK_SEQUENCE(sequence); |
| 292 | |
| 293 | if (where >= sequence->size || ! isfinite(data) || |
| 294 | data < sequence->blo || data > sequence->bhi) |
| 295 | return 0; |
| 296 | |
| 297 | if (sequence->recompute_range == 0 && (data < sequence->rlo || |
| 298 | data > sequence->rhi || |
| 299 | sequence->data[where] == sequence->rhi || |
| 300 | sequence->data[where] == sequence->rlo)) |
| 301 | sequence->recompute_range = 1; |
| 302 | |
| 303 | sequence->data[where] = data; |
| 304 | invalidate_auxilliary_data(sequence); |
| 305 | return 1; |
| 306 | } |
| 307 | |
| 308 | int |
| 309 | stp_sequence_get_point(const stp_sequence_t *sequence, size_t where, |
| 310 | double *data) |
| 311 | { |
| 312 | CHECK_SEQUENCE(sequence); |
| 313 | |
| 314 | if (where >= sequence->size) |
| 315 | return 0; |
| 316 | *data = sequence->data[where]; |
| 317 | return 1; |
| 318 | } |
| 319 | |
| 320 | stp_sequence_t * |
| 321 | stp_sequence_create_from_xmltree(stp_mxml_node_t *da) |
| 322 | { |
| 323 | const char *stmp; |
| 324 | stp_sequence_t *ret = NULL; |
| 325 | size_t point_count; |
| 326 | double low, high; |
| 327 | unsigned int i; |
| 328 | |
| 329 | ret = stp_sequence_create(); |
| 330 | |
| 331 | /* Get curve point count */ |
| 332 | stmp = stp_mxmlElementGetAttr(da, "count"); |
| 333 | if (stmp) |
| 334 | { |
| 335 | point_count = (size_t) stp_xmlstrtoul(stmp); |
| 336 | if ((stp_xmlstrtol(stmp)) < 0) |
| 337 | { |
| 338 | stp_erprintf("stp_sequence_create_from_xmltree: \"count\" is less than zero\n"); |
| 339 | goto error; |
| 340 | } |
| 341 | } |
| 342 | else |
| 343 | { |
| 344 | stp_erprintf("stp_sequence_create_from_xmltree: \"count\" missing\n"); |
| 345 | goto error; |
| 346 | } |
| 347 | /* Get lower bound */ |
| 348 | stmp = stp_mxmlElementGetAttr(da, "lower-bound"); |
| 349 | if (stmp) |
| 350 | { |
| 351 | low = stp_xmlstrtod(stmp); |
| 352 | } |
| 353 | else |
| 354 | { |
| 355 | stp_erprintf("stp_sequence_create_from_xmltree: \"lower-bound\" missing\n"); |
| 356 | goto error; |
| 357 | } |
| 358 | /* Get upper bound */ |
| 359 | stmp = stp_mxmlElementGetAttr(da, "upper-bound"); |
| 360 | if (stmp) |
| 361 | { |
| 362 | high = stp_xmlstrtod(stmp); |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | stp_erprintf("stp_sequence_create_from_xmltree: \"upper-bound\" missing\n"); |
| 367 | goto error; |
| 368 | } |
| 369 | |
| 370 | stp_deprintf(STP_DBG_XML, |
| 371 | "stp_sequence_create_from_xmltree: stp_sequence_set_size: %ld\n", |
| 372 | (long)point_count); |
| 373 | stp_sequence_set_size(ret, point_count); |
| 374 | stp_sequence_set_bounds(ret, low, high); |
| 375 | |
| 376 | /* Now read in the data points */ |
| 377 | if (point_count) |
| 378 | { |
| 379 | stp_mxml_node_t *child = da->child; |
| 380 | i = 0; |
| 381 | while (child && i < point_count) |
| 382 | { |
| 383 | if (child->type == STP_MXML_TEXT) |
| 384 | { |
| 385 | char *endptr; |
| 386 | /* |
| 387 | * Explicitly documented that callers to strtod should clear |
| 388 | * errno before calling it if they want to check the return |
| 389 | * status. |
| 390 | */ |
| 391 | errno = 0; |
| 392 | double tmpval = strtod(child->value.text.string, &endptr); |
| 393 | if (endptr == child->value.text.string) |
| 394 | { |
| 395 | stp_erprintf |
| 396 | ("stp_sequence_create_from_xmltree: bad data %s\n", |
| 397 | child->value.text.string); |
| 398 | goto error; |
| 399 | } |
| 400 | if (! isfinite(tmpval) |
| 401 | || ( tmpval == 0 && errno == ERANGE ) |
| 402 | || tmpval < low |
| 403 | || tmpval > high) |
| 404 | { |
| 405 | stp_erprintf("stp_sequence_create_from_xmltree: " |
| 406 | "read aborted: datum out of bounds: " |
| 407 | "%g %d %s (require %g <= x <= %g), n = %d\n", |
| 408 | tmpval, errno, child->value.text.string, |
| 409 | low, high, i); |
| 410 | goto error; |
| 411 | } |
| 412 | /* Datum was valid, so now add to the sequence */ |
| 413 | stp_sequence_set_point(ret, i, tmpval); |
| 414 | i++; |
| 415 | } |
| 416 | child = child->next; |
| 417 | } |
| 418 | if (i < point_count) |
| 419 | { |
| 420 | stp_erprintf("stp_sequence_create_from_xmltree: " |
| 421 | "read aborted: too little data " |
| 422 | "(n=%d, needed %ld)\n", i, (long)point_count); |
| 423 | goto error; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | return ret; |
| 428 | |
| 429 | error: |
| 430 | stp_erprintf("stp_sequence_create_from_xmltree: error during sequence read\n"); |
| 431 | if (ret) |
| 432 | stp_sequence_destroy(ret); |
| 433 | return NULL; |
| 434 | } |
| 435 | |
| 436 | stp_mxml_node_t * |
| 437 | stp_xmltree_create_from_sequence(const stp_sequence_t *seq) /* The sequence */ |
| 438 | { |
| 439 | size_t pointcount; |
| 440 | double low; |
| 441 | double high; |
| 442 | |
| 443 | char *count; |
| 444 | char *lower_bound; |
| 445 | char *upper_bound; |
| 446 | |
| 447 | stp_mxml_node_t *seqnode; |
| 448 | |
| 449 | unsigned int i; /* loop counter */ |
| 450 | |
| 451 | pointcount = stp_sequence_get_size(seq); |
| 452 | stp_sequence_get_bounds(seq, &low, &high); |
| 453 | |
| 454 | /* should count be of greater precision? */ |
| 455 | stp_asprintf(&count, "%lu", (unsigned long) pointcount); |
| 456 | stp_asprintf(&lower_bound, "%g", low); |
| 457 | stp_asprintf(&upper_bound, "%g", high); |
| 458 | |
| 459 | seqnode = stp_mxmlNewElement(NULL, "sequence"); |
| 460 | (void) stp_mxmlElementSetAttr(seqnode, "count", count); |
| 461 | (void) stp_mxmlElementSetAttr(seqnode, "lower-bound", lower_bound); |
| 462 | (void) stp_mxmlElementSetAttr(seqnode, "upper-bound", upper_bound); |
| 463 | |
| 464 | stp_free(count); |
| 465 | stp_free(lower_bound); |
| 466 | stp_free(upper_bound); |
| 467 | |
| 468 | /* Write the curve points into the node content */ |
| 469 | if (pointcount) /* Is there any data to write? */ |
| 470 | { |
| 471 | for (i = 0; i < pointcount; i++) |
| 472 | { |
| 473 | double dval; |
| 474 | char *sval; |
| 475 | |
| 476 | if ((stp_sequence_get_point(seq, i, &dval)) != 1) |
| 477 | goto error; |
| 478 | |
| 479 | stp_asprintf(&sval, "%g", dval); |
| 480 | stp_mxmlNewText(seqnode, 1, sval); |
| 481 | stp_free(sval); |
| 482 | } |
| 483 | } |
| 484 | return seqnode; |
| 485 | |
| 486 | error: |
| 487 | if (seqnode) |
| 488 | stp_mxmlDelete(seqnode); |
| 489 | return NULL; |
| 490 | } |
| 491 | |
| 492 | |
| 493 | #define isfinite_null(x) (1) |
| 494 | |
| 495 | /* "Overloaded" functions */ |
| 496 | |
| 497 | #define DEFINE_DATA_SETTER(t, name, checkfinite) \ |
| 498 | int \ |
| 499 | stp_sequence_set_##name##_data(stp_sequence_t *sequence, \ |
| 500 | size_t count, const t *data) \ |
| 501 | { \ |
| 502 | unsigned int i; \ |
| 503 | CHECK_SEQUENCE(sequence); \ |
| 504 | if (count < 2) \ |
| 505 | return 0; \ |
| 506 | \ |
| 507 | /* Validate the data before we commit to it. */ \ |
| 508 | for (i = 0; i < count; i++) \ |
| 509 | if ((! checkfinite(data[i])) || \ |
| 510 | data[i] < sequence->blo || \ |
| 511 | data[i] > sequence->bhi) \ |
| 512 | return 0; \ |
| 513 | stp_sequence_set_size(sequence, count); \ |
| 514 | for (i = 0; i < count; i++) \ |
| 515 | stp_sequence_set_point(sequence, i, (double) data[i]); \ |
| 516 | return 1; \ |
| 517 | } |
| 518 | |
| 519 | DEFINE_DATA_SETTER(float, float, isfinite) |
| 520 | DEFINE_DATA_SETTER(long, long, isfinite_null) |
| 521 | DEFINE_DATA_SETTER(unsigned long, ulong, isfinite_null) |
| 522 | DEFINE_DATA_SETTER(int, int, isfinite_null) |
| 523 | DEFINE_DATA_SETTER(unsigned int, uint, isfinite_null) |
| 524 | DEFINE_DATA_SETTER(short, short, isfinite_null) |
| 525 | DEFINE_DATA_SETTER(unsigned short, ushort, isfinite_null) |
| 526 | |
| 527 | #define DEFINE_DATA_ACCESSOR(t, lb, ub, name) \ |
| 528 | const t * \ |
| 529 | stp_sequence_get_##name##_data(const stp_sequence_t *sequence, size_t *count) \ |
| 530 | { \ |
| 531 | unsigned int i; \ |
| 532 | CHECK_SEQUENCE(sequence); \ |
| 533 | if (sequence->blo < (double) lb || sequence->bhi > (double) ub) \ |
| 534 | return NULL; \ |
| 535 | if (!sequence->name##_data) \ |
| 536 | { \ |
| 537 | stp_sequence_t *seq = (stp_sequence_t *) stpi_cast_safe(sequence); \ |
| 538 | seq->name##_data = stp_zalloc(sizeof(t) * sequence->size); \ |
| 539 | for (i = 0; i < sequence->size; i++) \ |
| 540 | seq->name##_data[i] = (t) sequence->data[i]; \ |
| 541 | } \ |
| 542 | *count = sequence->size; \ |
| 543 | return sequence->name##_data; \ |
| 544 | } |
| 545 | |
| 546 | #ifndef HUGE_VALF /* ISO constant, from <math.h> */ |
| 547 | #define HUGE_VALF 3.402823466E+38F |
| 548 | #endif |
| 549 | |
| 550 | DEFINE_DATA_ACCESSOR(float, -HUGE_VALF, HUGE_VALF, float) |
| 551 | DEFINE_DATA_ACCESSOR(long, LONG_MIN, LONG_MAX, long) |
| 552 | DEFINE_DATA_ACCESSOR(unsigned long, 0, ULONG_MAX, ulong) |
| 553 | DEFINE_DATA_ACCESSOR(int, INT_MIN, INT_MAX, int) |
| 554 | DEFINE_DATA_ACCESSOR(unsigned int, 0, UINT_MAX, uint) |
| 555 | DEFINE_DATA_ACCESSOR(short, SHRT_MIN, SHRT_MAX, short) |
| 556 | DEFINE_DATA_ACCESSOR(unsigned short, 0, USHRT_MAX, ushort) |