| 1 | /* |
| 2 | * |
| 3 | * Array 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-internal.h" |
| 28 | #include <gutenprint/gutenprint-intl-internal.h> |
| 29 | #include <math.h> |
| 30 | #include <string.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <limits.h> |
| 33 | #include <unistd.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <strings.h> |
| 37 | |
| 38 | |
| 39 | struct stp_array |
| 40 | { |
| 41 | stp_sequence_t *data; /* First member, to allow typecasting to sequence. */ |
| 42 | int x_size; |
| 43 | int y_size; |
| 44 | }; |
| 45 | |
| 46 | /* |
| 47 | * We could do more sanity checks here if we want. |
| 48 | */ |
| 49 | #define CHECK_ARRAY(array) STPI_ASSERT(array != NULL, NULL) |
| 50 | |
| 51 | static void array_ctor(stp_array_t *array) |
| 52 | { |
| 53 | array->data = stp_sequence_create(); |
| 54 | stp_sequence_set_size(array->data, array->x_size * array->y_size); |
| 55 | } |
| 56 | |
| 57 | stp_array_t * |
| 58 | stp_array_create(int x_size, int y_size) |
| 59 | { |
| 60 | stp_array_t *ret; |
| 61 | ret = stp_zalloc(sizeof(stp_array_t)); |
| 62 | ret->x_size = x_size; |
| 63 | ret->y_size = y_size; |
| 64 | ret->data = NULL; |
| 65 | array_ctor(ret); |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | static void |
| 71 | array_dtor(stp_array_t *array) |
| 72 | { |
| 73 | if (array->data) |
| 74 | stp_sequence_destroy(array->data); |
| 75 | memset(array, 0, sizeof(stp_array_t)); |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | stp_array_destroy(stp_array_t *array) |
| 80 | { |
| 81 | CHECK_ARRAY(array); |
| 82 | array_dtor(array); |
| 83 | stp_free(array); |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | stp_array_copy(stp_array_t *dest, const stp_array_t *source) |
| 88 | { |
| 89 | CHECK_ARRAY(dest); |
| 90 | CHECK_ARRAY(source); |
| 91 | |
| 92 | dest->x_size = source->x_size; |
| 93 | dest->y_size = source->y_size; |
| 94 | if (dest->data) |
| 95 | stp_sequence_destroy(dest->data); |
| 96 | dest->data = stp_sequence_create_copy(source->data); |
| 97 | } |
| 98 | |
| 99 | stp_array_t * |
| 100 | stp_array_create_copy(const stp_array_t *array) |
| 101 | { |
| 102 | stp_array_t *ret; |
| 103 | CHECK_ARRAY(array); |
| 104 | ret = stp_array_create(0, 0); /* gets freed next */ |
| 105 | stp_array_copy(ret, array); |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | void |
| 111 | stp_array_set_size(stp_array_t *array, int x_size, int y_size) |
| 112 | { |
| 113 | CHECK_ARRAY(array); |
| 114 | if (array->data) /* Free old data */ |
| 115 | stp_sequence_destroy(array->data); |
| 116 | array->x_size = x_size; |
| 117 | array->y_size = y_size; |
| 118 | array->data = stp_sequence_create(); |
| 119 | stp_sequence_set_size(array->data, array->x_size * array->y_size); |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | stp_array_get_size(const stp_array_t *array, int *x_size, int *y_size) |
| 124 | { |
| 125 | CHECK_ARRAY(array); |
| 126 | *x_size = array->x_size; |
| 127 | *y_size = array->y_size; |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | stp_array_set_data(stp_array_t *array, const double *data) |
| 133 | { |
| 134 | CHECK_ARRAY(array); |
| 135 | stp_sequence_set_data(array->data, array->x_size * array->y_size, |
| 136 | data); |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | stp_array_get_data(const stp_array_t *array, size_t *size, const double **data) |
| 141 | { |
| 142 | CHECK_ARRAY(array); |
| 143 | stp_sequence_get_data(array->data, size, data); |
| 144 | } |
| 145 | |
| 146 | int |
| 147 | stp_array_set_point(stp_array_t *array, int x, int y, double data) |
| 148 | { |
| 149 | CHECK_ARRAY(array); |
| 150 | |
| 151 | if (((array->x_size * x) + y) >= (array->x_size * array->y_size)) |
| 152 | return 0; |
| 153 | |
| 154 | return stp_sequence_set_point(array->data, (array->x_size * x) + y, data);} |
| 155 | |
| 156 | int |
| 157 | stp_array_get_point(const stp_array_t *array, int x, int y, double *data) |
| 158 | { |
| 159 | CHECK_ARRAY(array); |
| 160 | |
| 161 | if (((array->x_size * x) + y) >= array->x_size * array->y_size) |
| 162 | return 0; |
| 163 | return stp_sequence_get_point(array->data, |
| 164 | (array->x_size * x) + y, data); |
| 165 | } |
| 166 | |
| 167 | const stp_sequence_t * |
| 168 | stp_array_get_sequence(const stp_array_t *array) |
| 169 | { |
| 170 | CHECK_ARRAY(array); |
| 171 | |
| 172 | return array->data; |
| 173 | } |
| 174 | |
| 175 | static stp_array_t * |
| 176 | xml_doc_get_array(stp_mxml_node_t *doc) |
| 177 | { |
| 178 | stp_mxml_node_t *cur; |
| 179 | stp_mxml_node_t *xmlarray; |
| 180 | stp_array_t *array = NULL; |
| 181 | |
| 182 | if (doc == NULL ) |
| 183 | { |
| 184 | stp_deprintf(STP_DBG_ARRAY_ERRORS, |
| 185 | "xml_doc_get_array: XML file not parsed successfully.\n"); |
| 186 | return NULL; |
| 187 | } |
| 188 | |
| 189 | cur = doc->child; |
| 190 | |
| 191 | if (cur == NULL) |
| 192 | { |
| 193 | stp_deprintf(STP_DBG_ARRAY_ERRORS, |
| 194 | "xml_doc_get_array: empty document\n"); |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | xmlarray = stp_xml_get_node(cur, "gutenprint", "array", NULL); |
| 199 | |
| 200 | if (xmlarray) |
| 201 | array = stp_array_create_from_xmltree(xmlarray); |
| 202 | |
| 203 | return array; |
| 204 | } |
| 205 | |
| 206 | stp_array_t * |
| 207 | stp_array_create_from_file(const char* file) |
| 208 | { |
| 209 | stp_array_t *array = NULL; |
| 210 | stp_mxml_node_t *doc; |
| 211 | FILE *fp = NULL; |
| 212 | if (file[0] != '/' && strncmp(file, "./", 2) && strncmp(file, "../", 3)) |
| 213 | { |
| 214 | char *fn = stp_path_find_file(NULL, file); |
| 215 | if (fn) |
| 216 | { |
| 217 | fp = fopen(file, "r"); |
| 218 | free(fn); |
| 219 | } |
| 220 | } |
| 221 | else if (file) |
| 222 | { |
| 223 | fp = fopen(file, "r"); |
| 224 | } |
| 225 | if (!fp) |
| 226 | { |
| 227 | stp_deprintf(STP_DBG_ARRAY_ERRORS, |
| 228 | "stp_array_create_from_file: unable to open %s: %s\n", |
| 229 | file, strerror(errno)); |
| 230 | return NULL; |
| 231 | } |
| 232 | stp_deprintf(STP_DBG_XML, "stp_array_create_from_file: reading `%s'...\n", |
| 233 | file); |
| 234 | |
| 235 | stp_xml_init(); |
| 236 | |
| 237 | doc = stp_mxmlLoadFile(NULL, fp, STP_MXML_NO_CALLBACK); |
| 238 | |
| 239 | array = xml_doc_get_array(doc); |
| 240 | |
| 241 | if (doc) |
| 242 | stp_mxmlDelete(doc); |
| 243 | |
| 244 | stp_xml_exit(); |
| 245 | (void) fclose(fp); |
| 246 | return array; |
| 247 | |
| 248 | } |
| 249 | |
| 250 | stp_array_t * |
| 251 | stp_array_create_from_xmltree(stp_mxml_node_t *array) /* The array node */ |
| 252 | { |
| 253 | const char *stmp; /* Temporary string */ |
| 254 | stp_mxml_node_t *child; /* Child sequence node */ |
| 255 | unsigned int x_size, y_size; |
| 256 | size_t count; |
| 257 | stp_sequence_t *seq = NULL; |
| 258 | stp_array_t *ret = NULL; |
| 259 | |
| 260 | /* FIXME Need protection against unlimited recursion */ |
| 261 | if ((stmp = stp_mxmlElementGetAttr(array, "src")) != NULL) |
| 262 | return stp_array_create_from_file(stmp); |
| 263 | stmp = stp_mxmlElementGetAttr(array, "x-size"); |
| 264 | if (stmp) |
| 265 | { |
| 266 | x_size = (int) strtoul(stmp, NULL, 0); |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | stp_erprintf("stp_array_create_from_xmltree: \"x-size\" missing\n"); |
| 271 | goto error; |
| 272 | } |
| 273 | /* Get y-size */ |
| 274 | stmp = stp_mxmlElementGetAttr(array, "y-size"); |
| 275 | if (stmp) |
| 276 | { |
| 277 | y_size = (int) strtoul(stmp, NULL, 0); |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | stp_erprintf("stp_array_create_from_xmltree: \"y-size\" missing\n"); |
| 282 | goto error; |
| 283 | } |
| 284 | |
| 285 | /* Get the sequence data */ |
| 286 | |
| 287 | child = stp_xml_get_node(array, "sequence", NULL); |
| 288 | if (child) |
| 289 | seq = stp_sequence_create_from_xmltree(child); |
| 290 | |
| 291 | if (seq == NULL) |
| 292 | goto error; |
| 293 | |
| 294 | ret = stp_array_create(x_size, y_size); |
| 295 | if (ret->data) |
| 296 | stp_sequence_destroy(ret->data); |
| 297 | ret->data = seq; |
| 298 | |
| 299 | count = stp_sequence_get_size(seq); |
| 300 | if (count != (x_size * y_size)) |
| 301 | { |
| 302 | stp_erprintf("stp_array_create_from_xmltree: size mismatch between array and sequence\n"); |
| 303 | goto error; |
| 304 | } |
| 305 | |
| 306 | return ret; |
| 307 | |
| 308 | error: |
| 309 | stp_erprintf("stp_array_create_from_xmltree: error during array read\n"); |
| 310 | if (ret) |
| 311 | stp_array_destroy(ret); |
| 312 | return NULL; |
| 313 | } |
| 314 | |
| 315 | stp_mxml_node_t * |
| 316 | stp_xmltree_create_from_array(const stp_array_t *array) /* The array */ |
| 317 | { |
| 318 | int x_size, y_size; |
| 319 | char *xs, *ys; |
| 320 | |
| 321 | stp_mxml_node_t *arraynode = NULL; |
| 322 | stp_mxml_node_t *child = NULL; |
| 323 | |
| 324 | stp_xml_init(); |
| 325 | |
| 326 | /* Get array details */ |
| 327 | stp_array_get_size(array, &x_size, &y_size); |
| 328 | |
| 329 | /* Construct the allocated strings required */ |
| 330 | stp_asprintf(&xs, "%d", x_size); |
| 331 | stp_asprintf(&ys, "%d", y_size); |
| 332 | |
| 333 | arraynode = stp_mxmlNewElement(NULL, "array"); |
| 334 | stp_mxmlElementSetAttr(arraynode, "x-size", xs); |
| 335 | stp_mxmlElementSetAttr(arraynode, "y-size", ys); |
| 336 | stp_free(xs); |
| 337 | stp_free(ys); |
| 338 | |
| 339 | child = stp_xmltree_create_from_sequence(stp_array_get_sequence(array)); |
| 340 | |
| 341 | if (child) |
| 342 | stp_mxmlAdd(arraynode, STP_MXML_ADD_AFTER, NULL, child); |
| 343 | else |
| 344 | { |
| 345 | stp_mxmlDelete(arraynode); |
| 346 | arraynode = NULL; |
| 347 | } |
| 348 | |
| 349 | stp_xml_exit(); |
| 350 | |
| 351 | return arraynode; |
| 352 | } |
| 353 | |
| 354 | static stp_mxml_node_t * |
| 355 | xmldoc_create_from_array(const stp_array_t *array) |
| 356 | { |
| 357 | stp_mxml_node_t *xmldoc; |
| 358 | stp_mxml_node_t *rootnode; |
| 359 | stp_mxml_node_t *arraynode; |
| 360 | |
| 361 | /* Get array details */ |
| 362 | arraynode = stp_xmltree_create_from_array(array); |
| 363 | if (arraynode == NULL) |
| 364 | { |
| 365 | stp_deprintf(STP_DBG_ARRAY_ERRORS, |
| 366 | "xmldoc_create_from_array: error creating array node\n"); |
| 367 | return NULL; |
| 368 | } |
| 369 | /* Create the XML tree */ |
| 370 | xmldoc = stp_xmldoc_create_generic(); |
| 371 | if (xmldoc == NULL) |
| 372 | { |
| 373 | stp_deprintf(STP_DBG_ARRAY_ERRORS, |
| 374 | "xmldoc_create_from_array: error creating XML document\n"); |
| 375 | return NULL; |
| 376 | } |
| 377 | rootnode = xmldoc->child; |
| 378 | if (rootnode == NULL) |
| 379 | { |
| 380 | stp_mxmlDelete(xmldoc); |
| 381 | stp_deprintf(STP_DBG_ARRAY_ERRORS, |
| 382 | "xmldoc_create_from_array: error getting XML document root node\n"); |
| 383 | return NULL; |
| 384 | } |
| 385 | |
| 386 | stp_mxmlAdd(rootnode, STP_MXML_ADD_AFTER, NULL, arraynode); |
| 387 | |
| 388 | return xmldoc; |
| 389 | } |
| 390 | |
| 391 | static int |
| 392 | array_whitespace_callback(stp_mxml_node_t *node, int where) |
| 393 | { |
| 394 | if (node->type != STP_MXML_ELEMENT) |
| 395 | return 0; |
| 396 | if (strcasecmp(node->value.element.name, "gutenprint") == 0) |
| 397 | { |
| 398 | switch (where) |
| 399 | { |
| 400 | case STP_MXML_WS_AFTER_OPEN: |
| 401 | case STP_MXML_WS_BEFORE_CLOSE: |
| 402 | case STP_MXML_WS_AFTER_CLOSE: |
| 403 | return '\n'; |
| 404 | case STP_MXML_WS_BEFORE_OPEN: |
| 405 | default: |
| 406 | return 0; |
| 407 | } |
| 408 | } |
| 409 | else if (strcasecmp(node->value.element.name, "array") == 0) |
| 410 | { |
| 411 | switch (where) |
| 412 | { |
| 413 | case STP_MXML_WS_AFTER_OPEN: |
| 414 | return '\n'; |
| 415 | case STP_MXML_WS_BEFORE_CLOSE: |
| 416 | case STP_MXML_WS_AFTER_CLOSE: |
| 417 | case STP_MXML_WS_BEFORE_OPEN: |
| 418 | default: |
| 419 | return 0; |
| 420 | } |
| 421 | } |
| 422 | else if (strcasecmp(node->value.element.name, "sequence") == 0) |
| 423 | { |
| 424 | const char *count; |
| 425 | switch (where) |
| 426 | { |
| 427 | case STP_MXML_WS_BEFORE_CLOSE: |
| 428 | count = stp_mxmlElementGetAttr(node, "count"); |
| 429 | if (strcmp(count, "0") == 0) |
| 430 | return 0; |
| 431 | else |
| 432 | return '\n'; |
| 433 | case STP_MXML_WS_AFTER_OPEN: |
| 434 | case STP_MXML_WS_AFTER_CLOSE: |
| 435 | return '\n'; |
| 436 | case STP_MXML_WS_BEFORE_OPEN: |
| 437 | default: |
| 438 | return 0; |
| 439 | } |
| 440 | } |
| 441 | else |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | |
| 446 | int |
| 447 | stp_array_write(FILE *file, const stp_array_t *array) /* The array */ |
| 448 | { |
| 449 | stp_mxml_node_t *xmldoc = NULL; |
| 450 | |
| 451 | stp_xml_init(); |
| 452 | |
| 453 | xmldoc = xmldoc_create_from_array(array); |
| 454 | if (xmldoc == NULL) |
| 455 | { |
| 456 | stp_xml_exit(); |
| 457 | return 1; |
| 458 | } |
| 459 | |
| 460 | stp_mxmlSaveFile(xmldoc, file, array_whitespace_callback); |
| 461 | |
| 462 | if (xmldoc) |
| 463 | stp_mxmlDelete(xmldoc); |
| 464 | |
| 465 | stp_xml_exit(); |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | char * |
| 471 | stp_array_write_string(const stp_array_t *array) /* The array */ |
| 472 | { |
| 473 | stp_mxml_node_t *xmldoc = NULL; |
| 474 | char *retval; |
| 475 | |
| 476 | stp_xml_init(); |
| 477 | |
| 478 | xmldoc = xmldoc_create_from_array(array); |
| 479 | if (xmldoc == NULL) |
| 480 | { |
| 481 | stp_xml_exit(); |
| 482 | return NULL; |
| 483 | } |
| 484 | |
| 485 | retval = stp_mxmlSaveAllocString(xmldoc, array_whitespace_callback); |
| 486 | |
| 487 | if (xmldoc) |
| 488 | stp_mxmlDelete(xmldoc); |
| 489 | |
| 490 | stp_xml_exit(); |
| 491 | |
| 492 | return retval; |
| 493 | } |