Debian Codegraph @

src/main/curve.c

raw symbols SCIP
1 /*
2 *
3 * Print plug-in driver utility functions for the GIMP.
4 *
5 * Copyright 1997-2000 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
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <gutenprint/gutenprint.h>
26 #include "gutenprint-internal.h"
27 #include <gutenprint/gutenprint-intl-internal.h>
28 #include <math.h>
29 #ifdef sun
30 #include <ieeefp.h>
31 #endif
32 #include <string.h>
33 #include <stdlib.h>
34 #include <limits.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <strings.h>
39
40 #ifdef __GNUC__
41 #define inline __inline__
42 #endif
43
44 #undef inline
45 #define inline
46
47 /*
48 * We could do more sanity checks here if we want.
49 */
50 #define CHECK_CURVE(curve) \
51 do \
52 { \
53 STPI_ASSERT((curve) != NULL, NULL); \
54 STPI_ASSERT((curve)->seq != NULL, NULL); \
55 } while (0)
56
57 static const int curve_point_limit = 1048576;
58
59 struct stp_curve
60 {
61 stp_curve_type_t curve_type;
62 stp_curve_wrap_mode_t wrap_mode;
63 int piecewise;
64 int recompute_interval; /* Do we need to recompute the deltas? */
65 double gamma; /* 0.0 means that the curve is not a gamma */
66 stp_sequence_t *seq; /* Sequence (contains the curve data) */
67 double *interval; /* We allocate an extra slot for the
68 wrap-around value. */
69
70 };
71
72 static const char *const stpi_curve_type_names[] =
73 {
74 "linear",
75 "spline",
76 };
77
78 static const int stpi_curve_type_count =
79 (sizeof(stpi_curve_type_names) / sizeof(const char *));
80
81 static const char *const stpi_wrap_mode_names[] =
82 {
83 "nowrap",
84 "wrap"
85 };
86
87 /*
88 * Get the total number of points in the base sequence class
89 */
90 static size_t
91 get_real_point_count(const stp_curve_t *curve)
92 {
93 if (curve->piecewise)
94 return stp_sequence_get_size(curve->seq) / 2;
95 else
96 return stp_sequence_get_size(curve->seq);
97 }
98
99 /*
100 * Get the number of points used by the curve (that are visible to the
101 * user). This is the real point count, but is decreased by 1 if the
102 * curve wraps around.
103 */
104 static size_t
105 get_point_count(const stp_curve_t *curve)
106 {
107 size_t count = get_real_point_count(curve);
108 if (curve->wrap_mode == STP_CURVE_WRAP_AROUND)
109 count -= 1;
110
111 return count;
112 }
113
114 static void
115 invalidate_auxiliary_data(stp_curve_t *curve)
116 {
117 STP_SAFE_FREE(curve->interval);
118 }
119
120 static void
121 clear_curve_data(stp_curve_t *curve)
122 {
123 if (curve->seq)
124 stp_sequence_set_size(curve->seq, 0);
125 curve->recompute_interval = 0;
126 invalidate_auxiliary_data(curve);
127 }
128
129 static void
130 compute_linear_deltas(stp_curve_t *curve)
131 {
132 int i;
133 size_t delta_count;
134 size_t seq_point_count;
135 const double *data;
136
137 stp_sequence_get_data(curve->seq, &seq_point_count, &data);
138 if (data == NULL)
139 return;
140
141 delta_count = get_real_point_count(curve);
142
143 if (delta_count <= 1) /* No intervals can be computed */
144 return;
145 delta_count--; /* One less than the real point count. Note size_t
146 is unsigned. */
147
148 curve->interval = stp_malloc(sizeof(double) * delta_count);
149 for (i = 0; i < delta_count; i++)
150 {
151 if (curve->piecewise)
152 curve->interval[i] = data[(2 * (i + 1)) + 1] - data[(2 * i) + 1];
153 else
154 curve->interval[i] = data[i + 1] - data[i];
155 }
156 }
157
158 static void
159 compute_spline_deltas_piecewise(stp_curve_t *curve)
160 {
161 int i;
162 int k;
163 double *u;
164 double *y2;
165 const double *data = NULL;
166 const stp_curve_point_t *dp;
167 size_t point_count;
168 size_t real_point_count;
169 double sig;
170 double p;
171
172 point_count = get_point_count(curve);
173
174 stp_sequence_get_data(curve->seq, &real_point_count, &data);
175 dp = (const stp_curve_point_t *)data;
176 real_point_count = real_point_count / 2;
177
178 u = stp_malloc(sizeof(double) * real_point_count);
179 y2 = stp_malloc(sizeof(double) * real_point_count);
180
181 if (curve->wrap_mode == STP_CURVE_WRAP_AROUND)
182 {
183 int reps = 3;
184 int count = reps * real_point_count;
185 double *y2a = stp_malloc(sizeof(double) * count);
186 double *ua = stp_malloc(sizeof(double) * count);
187 y2a[0] = 0.0;
188 ua[0] = 0.0;
189 for (i = 1; i < count - 1; i++)
190 {
191 int im1 = (i - 1) % point_count;
192 int ia = i % point_count;
193 int ip1 = (i + 1) % point_count;
194
195 sig = (dp[ia].x - dp[im1].x) / (dp[ip1].x - dp[im1].x);
196 p = sig * y2a[im1] + 2.0;
197 y2a[i] = (sig - 1.0) / p;
198
199 ua[i] = ((dp[ip1].y - dp[ia].y) / (dp[ip1].x - dp[ia].x)) -
200 ((dp[ia].y - dp[im1].y) / (dp[ia].x - dp[im1].x));
201 ua[i] =
202 (((6.0 * ua[ia]) / (dp[ip1].x - dp[im1].x)) - (sig * ua[im1])) / p;
203 }
204 y2a[count - 1] = 0.0;
205 for (k = count - 2 ; k >= 0; k--)
206 y2a[k] = y2a[k] * y2a[k + 1] + ua[k];
207 memcpy(u, ua + ((reps / 2) * point_count),
208 sizeof(double) * real_point_count);
209 memcpy(y2, y2a + ((reps / 2) * point_count),
210 sizeof(double) * real_point_count);
211 stp_free(y2a);
212 stp_free(ua);
213 }
214 else
215 {
216 int count = real_point_count - 1;
217
218 y2[0] = 0;
219 u[0] = 2 * (dp[1].y - dp[0].y);
220 for (i = 1; i < count; i++)
221 {
222 int im1 = (i - 1);
223 int ip1 = (i + 1);
224
225 sig = (dp[i].x - dp[im1].x) / (dp[ip1].x - dp[im1].x);
226 p = sig * y2[im1] + 2.0;
227 y2[i] = (sig - 1.0) / p;
228
229 u[i] = ((dp[ip1].y - dp[i].y) / (dp[ip1].x - dp[i].x)) -
230 ((dp[i].y - dp[im1].y) / (dp[i].x - dp[im1].x));
231 u[i] =
232 (((6.0 * u[i]) / (dp[ip1].x - dp[im1].x)) - (sig * u[im1])) / p;
233 stp_deprintf(STP_DBG_CURVE,
234 "%d sig %f p %f y2 %f u %f x %f %f %f y %f %f %f\n",
235 i, sig, p, y2[i], u[i],
236 dp[im1].x, dp[i].x, dp[ip1].x,
237 dp[im1].y, dp[i].y, dp[ip1].y);
238 }
239 y2[count] = 0.0;
240 u[count] = 0.0;
241 for (k = real_point_count - 2; k >= 0; k--)
242 y2[k] = y2[k] * y2[k + 1] + u[k];
243 }
244
245 curve->interval = y2;
246 stp_free(u);
247 }
248
249 static void
250 compute_spline_deltas_dense(stp_curve_t *curve)
251 {
252 int i;
253 int k;
254 double *u;
255 double *y2;
256 const double *y;
257 size_t point_count;
258 size_t real_point_count;
259 double sig;
260 double p;
261
262 point_count = get_point_count(curve);
263
264 stp_sequence_get_data(curve->seq, &real_point_count, &y);
265 u = stp_malloc(sizeof(double) * real_point_count);
266 y2 = stp_malloc(sizeof(double) * real_point_count);
267
268 if (curve->wrap_mode == STP_CURVE_WRAP_AROUND)
269 {
270 int reps = 3;
271 int count = reps * real_point_count;
272 double *y2a = stp_malloc(sizeof(double) * count);
273 double *ua = stp_malloc(sizeof(double) * count);
274 y2a[0] = 0.0;
275 ua[0] = 0.0;
276 for (i = 1; i < count - 1; i++)
277 {
278 int im1 = (i - 1);
279 int ip1 = (i + 1);
280 int im1a = im1 % point_count;
281 int ia = i % point_count;
282 int ip1a = ip1 % point_count;
283
284 sig = (i - im1) / (ip1 - im1);
285 p = sig * y2a[im1] + 2.0;
286 y2a[i] = (sig - 1.0) / p;
287
288 ua[i] = y[ip1a] - 2 * y[ia] + y[im1a];
289 ua[i] = 3.0 * ua[i] - sig * ua[im1] / p;
290 }
291 y2a[count - 1] = 0.0;
292 for (k = count - 2 ; k >= 0; k--)
293 y2a[k] = y2a[k] * y2a[k + 1] + ua[k];
294 memcpy(u, ua + ((reps / 2) * point_count),
295 sizeof(double) * real_point_count);
296 memcpy(y2, y2a + ((reps / 2) * point_count),
297 sizeof(double) * real_point_count);
298 stp_free(y2a);
299 stp_free(ua);
300 }
301 else
302 {
303 int count = real_point_count - 1;
304
305 y2[0] = 0;
306 u[0] = 2 * (y[1] - y[0]);
307 for (i = 1; i < count; i++)
308 {
309 int im1 = (i - 1);
310 int ip1 = (i + 1);
311
312 sig = (i - im1) / (ip1 - im1);
313 p = sig * y2[im1] + 2.0;
314 y2[i] = (sig - 1.0) / p;
315
316 u[i] = y[ip1] - 2 * y[i] + y[im1];
317 u[i] = 3.0 * u[i] - sig * u[im1] / p;
318 }
319
320 u[count] = 2 * (y[count] - y[count - 1]);
321 y2[count] = 0.0;
322
323 u[count] = 0.0;
324 for (k = real_point_count - 2; k >= 0; k--)
325 y2[k] = y2[k] * y2[k + 1] + u[k];
326 }
327
328 curve->interval = y2;
329 stp_free(u);
330 }
331
332 static void
333 compute_spline_deltas(stp_curve_t *curve)
334 {
335 if (curve->piecewise)
336 compute_spline_deltas_piecewise(curve);
337 else
338 compute_spline_deltas_dense(curve);
339 }
340
341 /*
342 * Recompute the delta values for interpolation.
343 * When we actually do support spline curves, this routine will
344 * compute the second derivatives for that purpose, too.
345 */
346 static void
347 compute_intervals(stp_curve_t *curve)
348 {
349 if (curve->interval)
350 {
351 stp_free(curve->interval);
352 curve->interval = NULL;
353 }
354 if (stp_sequence_get_size(curve->seq) > 0)
355 {
356 switch (curve->curve_type)
357 {
358 case STP_CURVE_TYPE_SPLINE:
359 compute_spline_deltas(curve);
360 break;
361 case STP_CURVE_TYPE_LINEAR:
362 compute_linear_deltas(curve);
363 break;
364 }
365 }
366 curve->recompute_interval = 0;
367 }
368
369 static int
370 stpi_curve_set_points(stp_curve_t *curve, size_t points)
371 {
372 if (points < 2)
373 return 0;
374 if (points > curve_point_limit ||
375 (curve->wrap_mode == STP_CURVE_WRAP_AROUND &&
376 points > curve_point_limit - 1))
377 return 0;
378 clear_curve_data(curve);
379 if (curve->wrap_mode == STP_CURVE_WRAP_AROUND)
380 points++;
381 if (curve->piecewise)
382 points *= 2;
383 if ((stp_sequence_set_size(curve->seq, points)) == 0)
384 return 0;
385 return 1;
386 }
387
388 static int
389 stpi_curve_set_data(stp_curve_t *curve, size_t points, const double *data)
390 {
391 if (points < 2)
392 return 0;
393 if (points > curve_point_limit ||
394 (curve->wrap_mode == STP_CURVE_WRAP_AROUND &&
395 points > curve_point_limit - 1))
396 return 0;
397 clear_curve_data(curve);
398 if (curve->wrap_mode == STP_CURVE_WRAP_AROUND)
399 points++;
400 if (curve->piecewise)
401 points *= 2;
402 if ((stp_sequence_set_data(curve->seq, points, data)) == 0)
403 return 0;
404 return 1;
405 }
406
407 /*
408 * Create a default curve
409 */
410 static void
411 stpi_curve_ctor(stp_curve_t *curve, stp_curve_wrap_mode_t wrap_mode)
412 {
413 curve->seq = stp_sequence_create();
414 stp_sequence_set_bounds(curve->seq, 0.0, 1.0);
415 curve->curve_type = STP_CURVE_TYPE_LINEAR;
416 curve->wrap_mode = wrap_mode;
417 curve->piecewise = 0;
418 stpi_curve_set_points(curve, 2);
419 curve->recompute_interval = 1;
420 if (wrap_mode == STP_CURVE_WRAP_NONE)
421 curve->gamma = 1.0;
422 stp_sequence_set_point(curve->seq, 0, 0);
423 stp_sequence_set_point(curve->seq, 1, 1);
424 }
425
426 stp_curve_t *
427 stp_curve_create(stp_curve_wrap_mode_t wrap_mode)
428 {
429 stp_curve_t *ret;
430 if (wrap_mode != STP_CURVE_WRAP_NONE && wrap_mode != STP_CURVE_WRAP_AROUND)
431 return NULL;
432 ret = stp_zalloc(sizeof(stp_curve_t));
433 stpi_curve_ctor(ret, wrap_mode);
434 return ret;
435 }
436
437 static void
438 curve_dtor(stp_curve_t *curve)
439 {
440 CHECK_CURVE(curve);
441 clear_curve_data(curve);
442 if (curve->seq)
443 stp_sequence_destroy(curve->seq);
444 memset(curve, 0, sizeof(stp_curve_t));
445 curve->curve_type = -1;
446 }
447
448 void
449 stp_curve_destroy(stp_curve_t *curve)
450 {
451 curve_dtor(curve);
452 stp_free(curve);
453 }
454
455 void
456 stp_curve_copy(stp_curve_t *dest, const stp_curve_t *source)
457 {
458 CHECK_CURVE(dest);
459 CHECK_CURVE(source);
460 curve_dtor(dest);
461 dest->curve_type = source->curve_type;
462 dest->wrap_mode = source->wrap_mode;
463 dest->gamma = source->gamma;
464 dest->seq = stp_sequence_create_copy(source->seq);
465 dest->piecewise = source->piecewise;
466 dest->recompute_interval = 1;
467 }
468
469 stp_curve_t *
470 stp_curve_create_copy(const stp_curve_t *curve)
471 {
472 stp_curve_t *ret;
473 CHECK_CURVE(curve);
474 ret = stp_curve_create(curve->wrap_mode);
475 stp_curve_copy(ret, curve);
476 return ret;
477 }
478
479 void
480 stp_curve_reverse(stp_curve_t *dest, const stp_curve_t *source)
481 {
482 CHECK_CURVE(dest);
483 CHECK_CURVE(source);
484 curve_dtor(dest);
485 dest->curve_type = source->curve_type;
486 dest->wrap_mode = source->wrap_mode;
487 dest->gamma = source->gamma;
488 if (source->piecewise)
489 {
490 const double *source_data;
491 size_t size;
492 double *new_data;
493 int i;
494 stp_sequence_get_data(source->seq, &size, &source_data);
495 new_data = stp_malloc(sizeof(double) * size);
496 for (i = 0; i < size; i += 2)
497 {
498 int j = size - i - 2;
499 new_data[i] = 1.0 - source_data[j];
500 new_data[i + 1] = source_data[j + 1];
501 }
502 dest->seq = stp_sequence_create();
503 stp_sequence_set_data(dest->seq, size, new_data);
504 stp_free(new_data);
505 }
506 else
507 dest->seq = stp_sequence_create_reverse(source->seq);
508 dest->piecewise = source->piecewise;
509 dest->recompute_interval = 1;
510 }
511
512 stp_curve_t *
513 stp_curve_create_reverse(const stp_curve_t *curve)
514 {
515 stp_curve_t *ret;
516 CHECK_CURVE(curve);
517 ret = stp_curve_create(curve->wrap_mode);
518 stp_curve_reverse(ret, curve);
519 return ret;
520 }
521
522 int
523 stp_curve_set_bounds(stp_curve_t *curve, double low, double high)
524 {
525 CHECK_CURVE(curve);
526 return stp_sequence_set_bounds(curve->seq, low, high);
527 }
528
529 void
530 stp_curve_get_bounds(const stp_curve_t *curve, double *low, double *high)
531 {
532 CHECK_CURVE(curve);
533 stp_sequence_get_bounds(curve->seq, low, high);
534 }
535
536 /*
537 * Find the minimum and maximum points on the curve. This does not
538 * attempt to find the minimum and maximum interpolations; with cubic
539 * splines these could exceed the boundaries. That's OK; the interpolation
540 * code will clip them to the bounds.
541 */
542 void
543 stp_curve_get_range(const stp_curve_t *curve, double *low, double *high)
544 {
545 CHECK_CURVE(curve);
546 stp_sequence_get_range(curve->seq, low, high);
547 }
548
549 size_t
550 stp_curve_count_points(const stp_curve_t *curve)
551 {
552 CHECK_CURVE(curve);
553 return get_point_count(curve);
554 }
555
556 stp_curve_wrap_mode_t
557 stp_curve_get_wrap(const stp_curve_t *curve)
558 {
559 CHECK_CURVE(curve);
560 return curve->wrap_mode;
561 }
562
563 int
564 stp_curve_is_piecewise(const stp_curve_t *curve)
565 {
566 CHECK_CURVE(curve);
567 return curve->piecewise;
568 }
569
570 int
571 stp_curve_set_interpolation_type(stp_curve_t *curve, stp_curve_type_t itype)
572 {
573 CHECK_CURVE(curve);
574 if (itype >= stpi_curve_type_count)
575 return 0;
576 curve->curve_type = itype;
577 return 1;
578 }
579
580 stp_curve_type_t
581 stp_curve_get_interpolation_type(const stp_curve_t *curve)
582 {
583 CHECK_CURVE(curve);
584 return curve->curve_type;
585 }
586
587 int
588 stp_curve_set_gamma(stp_curve_t *curve, double fgamma)
589 {
590 CHECK_CURVE(curve);
591 if (curve->wrap_mode || ! isfinite(fgamma) || fgamma == 0.0)
592 return 0;
593 clear_curve_data(curve);
594 curve->gamma = fgamma;
595 stp_curve_resample(curve, 2);
596 return 1;
597 }
598
599 double
600 stp_curve_get_gamma(const stp_curve_t *curve)
601 {
602 CHECK_CURVE(curve);
603 return curve->gamma;
604 }
605
606 int
607 stp_curve_set_data(stp_curve_t *curve, size_t count, const double *data)
608 {
609 size_t i;
610 size_t real_count = count;
611 double low, high;
612 CHECK_CURVE(curve);
613 if (count < 2)
614 return 0;
615 if (curve->wrap_mode == STP_CURVE_WRAP_AROUND)
616 real_count++;
617 if (real_count > curve_point_limit)
618 return 0;
619
620 /* Validate the data before we commit to it. */
621 stp_sequence_get_bounds(curve->seq, &low, &high);
622 for (i = 0; i < count; i++)
623 if (! isfinite(data[i]) || data[i] < low || data[i] > high)
624 {
625 stp_deprintf(STP_DBG_CURVE_ERRORS,
626 "stp_curve_set_data: datum out of bounds: "
627 "%g (require %g <= x <= %g), n = %ld\n",
628 data[i], low, high, (long)i);
629 return 0;
630 }
631 /* Allocate sequence; also accounts for WRAP_MODE */
632 stpi_curve_set_points(curve, count);
633 curve->gamma = 0.0;
634 stp_sequence_set_subrange(curve->seq, 0, count, data);
635
636 if (curve->wrap_mode == STP_CURVE_WRAP_AROUND)
637 stp_sequence_set_point(curve->seq, count, data[0]);
638 curve->recompute_interval = 1;
639 curve->piecewise = 0;
640
641 return 1;
642 }
643
644 int
645 stp_curve_set_data_points(stp_curve_t *curve, size_t count,
646 const stp_curve_point_t *data)
647 {
648 size_t i;
649 size_t real_count = count;
650 double low, high;
651 double last_x = -1;
652 CHECK_CURVE(curve);
653 if (count < 2)
654 {
655 stp_deprintf(STP_DBG_CURVE_ERRORS,
656 "stp_curve_set_data_points: too few points %ld\n", (long)count);
657 return 0;
658 }
659 if (curve->wrap_mode == STP_CURVE_WRAP_AROUND)
660 real_count++;
661 if (real_count > curve_point_limit)
662 {
663 stp_deprintf(STP_DBG_CURVE_ERRORS,
664 "stp_curve_set_data_points: too many points %ld\n",
665 (long)real_count);
666 return 0;
667 }
668
669 /* Validate the data before we commit to it. */
670 stp_sequence_get_bounds(curve->seq, &low, &high);
671 for (i = 0; i < count; i++)
672 {
673 if (! isfinite(data[i].y) || data[i].y < low || data[i].y > high)
674 {
675 stp_deprintf(STP_DBG_CURVE_ERRORS,
676 "stp_curve_set_data_points: datum out of bounds: "
677 "%g (require %g <= x <= %g), n = %ld\n",
678 data[i].y, low, high, (long)i);
679 return 0;
680 }
681 if (i == 0 && data[i].x != 0.0)
682 {
683 stp_deprintf(STP_DBG_CURVE_ERRORS,
684 "stp_curve_set_data_points: first point must have x=0\n");
685 return 0;
686 }
687 if (curve->wrap_mode == STP_CURVE_WRAP_NONE && i == count - 1 &&
688 data[i].x != 1.0)
689 {
690 stp_deprintf(STP_DBG_CURVE_ERRORS,
691 "stp_curve_set_data_points: last point must have x=1\n");
692 return 0;
693 }
694 if (curve->wrap_mode == STP_CURVE_WRAP_AROUND &&
695 data[i].x >= 1.0 - .000001)
696 {
697 stp_deprintf(STP_DBG_CURVE_ERRORS,
698 "stp_curve_set_data_points: horizontal value must "
699 "not exceed .99999\n");
700 return 0;
701 }
702 if (data[i].x < 0 || data[i].x > 1)
703 {
704 stp_deprintf(STP_DBG_CURVE_ERRORS,
705 "stp_curve_set_data_points: horizontal position out of bounds: "
706 "%g, n = %ld\n",
707 data[i].x, (long)i);
708 return 0;
709 }
710 if (data[i].x - .000001 < last_x)
711 {
712 stp_deprintf(STP_DBG_CURVE_ERRORS,
713 "stp_curve_set_data_points: horizontal position must "
714 "exceed previous position by .000001: %g, %g, n = %ld\n",
715 data[i].x, last_x, (long)i);
716 return 0;
717 }
718 last_x = data[i].x;
719 }
720 /* Allocate sequence; also accounts for WRAP_MODE */
721 curve->piecewise = 1;
722 stpi_curve_set_points(curve, count);
723 curve->gamma = 0.0;
724 stp_sequence_set_subrange(curve->seq, 0, count * 2, (const double *) data);
725
726 if (curve->wrap_mode == STP_CURVE_WRAP_AROUND)
727 {
728 stp_sequence_set_point(curve->seq, count * 2, data[0].x);
729 stp_sequence_set_point(curve->seq, count * 2 + 1, data[0].y);
730 }
731 curve->recompute_interval = 1;
732
733 return 1;
734 }
735
736
737 /*
738 * Note that we return a pointer to the raw data here.
739 * A lot of operations change the data vector, that's why we don't
740 * guarantee it across non-const calls.
741 */
742 const double *
743 stp_curve_get_data(const stp_curve_t *curve, size_t *count)
744 {
745 const double *ret;
746 CHECK_CURVE(curve);
747 if (curve->piecewise)
748 return NULL;
749 stp_sequence_get_data(curve->seq, count, &ret);
750 *count = get_point_count(curve);
751 return ret;
752 }
753
754 const stp_curve_point_t *
755 stp_curve_get_data_points(const stp_curve_t *curve, size_t *count)
756 {
757 const double *ret;
758 CHECK_CURVE(curve);
759 if (!curve->piecewise)
760 return NULL;
761 stp_sequence_get_data(curve->seq, count, &ret);
762 *count = get_point_count(curve);
763 return (const stp_curve_point_t *) ret;
764 }
765
766 static const double *
767 stpi_curve_get_data_internal(const stp_curve_t *curve, size_t *count)
768 {
769 const double *ret;
770 CHECK_CURVE(curve);
771 stp_sequence_get_data(curve->seq, count, &ret);
772 *count = get_point_count(curve);
773 if (curve->piecewise)
774 *count *= 2;
775 return ret;
776 }
777
778
779 /* "Overloaded" functions */
780
781 #define DEFINE_DATA_SETTER(t, name) \
782 int \
783 stp_curve_set_##name##_data(stp_curve_t *curve, size_t count, const t *data) \
784 { \
785 double *tmp_data; \
786 size_t i; \
787 int status; \
788 size_t real_count = count; \
789 \
790 CHECK_CURVE(curve); \
791 if (count < 2) \
792 return 0; \
793 if (curve->wrap_mode == STP_CURVE_WRAP_AROUND) \
794 real_count++; \
795 if (real_count > curve_point_limit) \
796 return 0; \
797 tmp_data = stp_malloc(count * sizeof(double)); \
798 for (i = 0; i < count; i++) \
799 tmp_data[i] = (double) data[i]; \
800 status = stp_curve_set_data(curve, count, tmp_data); \
801 stp_free(tmp_data); \
802 return status; \
803 }
804
805 DEFINE_DATA_SETTER(float, float)
806 DEFINE_DATA_SETTER(long, long)
807 DEFINE_DATA_SETTER(unsigned long, ulong)
808 DEFINE_DATA_SETTER(int, int)
809 DEFINE_DATA_SETTER(unsigned int, uint)
810 DEFINE_DATA_SETTER(short, short)
811 DEFINE_DATA_SETTER(unsigned short, ushort)
812
813
814 #define DEFINE_DATA_ACCESSOR(t, name) \
815 const t * \
816 stp_curve_get_##name##_data(const stp_curve_t *curve, size_t *count) \
817 { \
818 if (curve->piecewise) \
819 return 0; \
820 return stp_sequence_get_##name##_data(curve->seq, count); \
821 }
822
823 DEFINE_DATA_ACCESSOR(float, float)
824 DEFINE_DATA_ACCESSOR(long, long)
825 DEFINE_DATA_ACCESSOR(unsigned long, ulong)
826 DEFINE_DATA_ACCESSOR(int, int)
827 DEFINE_DATA_ACCESSOR(unsigned int, uint)
828 DEFINE_DATA_ACCESSOR(short, short)
829 DEFINE_DATA_ACCESSOR(unsigned short, ushort)
830
831
832 stp_curve_t *
833 stp_curve_get_subrange(const stp_curve_t *curve, size_t start, size_t count)
834 {
835 stp_curve_t *retval;
836 size_t ncount;
837 double blo, bhi;
838 const double *data;
839 if (start + count > stp_curve_count_points(curve) || count < 2)
840 return NULL;
841 if (curve->piecewise)
842 return NULL;
843 retval = stp_curve_create(STP_CURVE_WRAP_NONE);
844 stp_curve_get_bounds(curve, &blo, &bhi);
845 stp_curve_set_bounds(retval, blo, bhi);
846 data = stp_curve_get_data(curve, &ncount);
847 if (! stp_curve_set_data(retval, count, data + start))
848 {
849 stp_curve_destroy(retval);
850 return NULL;
851 }
852 return retval;
853 }
854
855 int
856 stp_curve_set_subrange(stp_curve_t *curve, const stp_curve_t *range,
857 size_t start)
858 {
859 double blo, bhi;
860 double rlo, rhi;
861 const double *data;
862 size_t count;
863 CHECK_CURVE(curve);
864 if (start + stp_curve_count_points(range) > stp_curve_count_points(curve))
865 return 0;
866 if (curve->piecewise)
867 return 0;
868 stp_sequence_get_bounds(curve->seq, &blo, &bhi);
869 stp_sequence_get_range(curve->seq, &rlo, &rhi);
870 if (rlo < blo || rhi > bhi)
871 return 0;
872 stp_sequence_get_data(range->seq, &count, &data);
873 curve->recompute_interval = 1;
874 curve->gamma = 0.0;
875 invalidate_auxiliary_data(curve);
876 stp_sequence_set_subrange(curve->seq, start, stp_curve_count_points(range),
877 data);
878 return 1;
879 }
880
881
882 int
883 stp_curve_set_point(stp_curve_t *curve, size_t where, double data)
884 {
885 CHECK_CURVE(curve);
886 if (where >= get_point_count(curve))
887 return 0;
888 curve->gamma = 0.0;
889
890 if (curve->piecewise)
891 return 0;
892 if ((stp_sequence_set_point(curve->seq, where, data)) == 0)
893 return 0;
894 if (where == 0 && curve->wrap_mode == STP_CURVE_WRAP_AROUND)
895 if ((stp_sequence_set_point(curve->seq,
896 get_point_count(curve), data)) == 0)
897 return 0;
898 invalidate_auxiliary_data(curve);
899 return 1;
900 }
901
902 int
903 stp_curve_get_point(const stp_curve_t *curve, size_t where, double *data)
904 {
905 CHECK_CURVE(curve);
906 if (where >= get_point_count(curve))
907 return 0;
908 if (curve->piecewise)
909 return 0;
910 return stp_sequence_get_point(curve->seq, where, data);
911 }
912
913 const stp_sequence_t *
914 stp_curve_get_sequence(const stp_curve_t *curve)
915 {
916 CHECK_CURVE(curve);
917 if (curve->piecewise)
918 return NULL;
919 return curve->seq;
920 }
921
922 int
923 stp_curve_rescale(stp_curve_t *curve, double scale,
924 stp_curve_compose_t mode, stp_curve_bounds_t bounds_mode)
925 {
926 int i;
927 double nblo;
928 double nbhi;
929 size_t count;
930
931 CHECK_CURVE(curve);
932
933 stp_sequence_get_bounds(curve->seq, &nblo, &nbhi);
934 if (bounds_mode == STP_CURVE_BOUNDS_RESCALE)
935 {
936 switch (mode)
937 {
938 case STP_CURVE_COMPOSE_ADD:
939 nblo += scale;
940 nbhi += scale;
941 break;
942 case STP_CURVE_COMPOSE_MULTIPLY:
943 if (scale < 0)
944 {
945 double tmp = nblo * scale;
946 nblo = nbhi * scale;
947 nbhi = tmp;
948 }
949 else
950 {
951 nblo *= scale;
952 nbhi *= scale;
953 }
954 break;
955 case STP_CURVE_COMPOSE_EXPONENTIATE:
956 if (scale == 0.0)
957 return 0;
958 if (nblo < 0)
959 return 0;
960 nblo = pow(nblo, scale);
961 nbhi = pow(nbhi, scale);
962 break;
963 default:
964 return 0;
965 }
966 }
967
968 if (! isfinite(nbhi) || ! isfinite(nblo))
969 return 0;
970
971 count = get_point_count(curve);
972 if (count)
973 {
974 double *tmp;
975 size_t scount;
976 int stride = 1;
977 int offset = 0;
978 const double *data;
979 if (curve->piecewise)
980 {
981 stride = 2;
982 offset = 1;
983 }
984 stp_sequence_get_data(curve->seq, &scount, &data);
985 tmp = stp_malloc(sizeof(double) * scount);
986 memcpy(tmp, data, scount * sizeof(double));
987 for (i = offset; i < scount; i += stride)
988 {
989 switch (mode)
990 {
991 case STP_CURVE_COMPOSE_ADD:
992 tmp[i] = tmp[i] + scale;
993 break;
994 case STP_CURVE_COMPOSE_MULTIPLY:
995 tmp[i] = tmp[i] * scale;
996 break;
997 case STP_CURVE_COMPOSE_EXPONENTIATE:
998 tmp[i] = pow(tmp[i], scale);
999 break;
1000 }
1001 if (tmp[i] > nbhi || tmp[i] < nblo)
1002 {
1003 if (bounds_mode == STP_CURVE_BOUNDS_ERROR)
1004 {
1005 stp_free(tmp);
1006 return(0);
1007 }
1008 else if (tmp[i] > nbhi)
1009 tmp[i] = nbhi;
1010 else
1011 tmp[i] = nblo;
1012 }
1013 }
1014 stp_sequence_set_bounds(curve->seq, nblo, nbhi);
1015 curve->gamma = 0.0;
1016 stpi_curve_set_points(curve, count);
1017 stp_sequence_set_subrange(curve->seq, 0, scount, tmp);
1018 stp_free(tmp);
1019 curve->recompute_interval = 1;
1020 invalidate_auxiliary_data(curve);
1021 }
1022 return 1;
1023 }
1024
1025 static int
1026 stpi_curve_check_parameters(stp_curve_t *curve, size_t points)
1027 {
1028 double blo, bhi;
1029 (void)points;
1030
1031 if (curve->gamma && curve->wrap_mode)
1032 {
1033 stp_deprintf(STP_DBG_CURVE_ERRORS,
1034 "curve sets both gamma and wrap_mode\n");
1035 return 0;
1036 }
1037 stp_sequence_get_bounds(curve->seq, &blo, &bhi);
1038 if (blo > bhi)
1039 {
1040 stp_deprintf(STP_DBG_CURVE_ERRORS,
1041 "curve low bound is greater than high bound\n");
1042 return 0;
1043 }
1044 return 1;
1045 }
1046
1047 static inline double
1048 interpolate_gamma_internal(const stp_curve_t *curve, double where)
1049 {
1050 double fgamma = curve->gamma;
1051 double blo, bhi;
1052 size_t real_point_count;
1053
1054 real_point_count = get_real_point_count(curve);
1055
1056 if (real_point_count)
1057 where /= (real_point_count - 1);
1058 if (fgamma < 0)
1059 {
1060 where = 1.0 - where;
1061 fgamma = -fgamma;
1062 }
1063 stp_sequence_get_bounds(curve->seq, &blo, &bhi);
1064 stp_deprintf(STP_DBG_CURVE,
1065 "interpolate_gamma %f %f %f %f %f\n", where, fgamma,
1066 blo, bhi, pow(where, fgamma));
1067 return blo + (bhi - blo) * pow(where, fgamma);
1068 }
1069
1070 static inline double
1071 do_interpolate_spline(double low, double high, double frac,
1072 double interval_low, double interval_high,
1073 double x_interval)
1074 {
1075 double a = 1.0 - frac;
1076 double b = frac;
1077 double retval =
1078 ((a * a * a - a) * interval_low) + ((b * b * b - b) * interval_high);
1079 retval = retval * x_interval * x_interval / 6;
1080 retval += (a * low) + (b * high);
1081 return retval;
1082 }
1083
1084 static inline double
1085 interpolate_point_internal(const stp_curve_t *curve, double where)
1086 {
1087 int integer = where;
1088 double frac = where - (double) integer;
1089 double bhi, blo;
1090
1091 if (frac == 0.0)
1092 {
1093 double val;
1094 if ((stp_sequence_get_point(curve->seq, integer, &val)) == 0)
1095 return HUGE_VAL; /* Infinity */
1096 return val;
1097 }
1098 if (curve->recompute_interval)
1099 compute_intervals((stpi_cast_safe(curve)));
1100 if (curve->curve_type == STP_CURVE_TYPE_LINEAR)
1101 {
1102 double val;
1103 if ((stp_sequence_get_point(curve->seq, integer, &val)) == 0)
1104 return HUGE_VAL; /* Infinity */
1105 return val + frac * curve->interval[integer];
1106 }
1107 else
1108 {
1109 size_t point_count;
1110 double ival, ip1val;
1111 double retval;
1112 int i = integer;
1113 int ip1 = integer + 1;
1114
1115 point_count = get_point_count(curve);
1116
1117 if (ip1 >= point_count)
1118 ip1 -= point_count;
1119
1120 if ((stp_sequence_get_point(curve->seq, i, &ival)) == 0 ||
1121 (stp_sequence_get_point(curve->seq, ip1, &ip1val)) == 0)
1122 return HUGE_VAL; /* Infinity */
1123
1124 retval = do_interpolate_spline(ival, ip1val, frac, curve->interval[i],
1125 curve->interval[ip1], 1.0);
1126
1127 stp_sequence_get_bounds(curve->seq, &blo, &bhi);
1128 if (retval > bhi)
1129 retval = bhi;
1130 if (retval < blo)
1131 retval = blo;
1132 return retval;
1133 }
1134 }
1135
1136 int
1137 stp_curve_interpolate_value(const stp_curve_t *curve, double where,
1138 double *result)
1139 {
1140 size_t limit;
1141
1142 CHECK_CURVE(curve);
1143 if (curve->piecewise)
1144 return 0;
1145
1146 limit = get_real_point_count(curve);
1147
1148 if (where < 0 || where > limit)
1149 return 0;
1150 if (curve->gamma) /* this means a pure gamma curve */
1151 *result = interpolate_gamma_internal(curve, where);
1152 else
1153 *result = interpolate_point_internal(curve, where);
1154 return 1;
1155 }
1156
1157 int
1158 stp_curve_resample(stp_curve_t *curve, size_t points)
1159 {
1160 size_t limit = points;
1161 size_t old;
1162 size_t i;
1163 double *new_vec;
1164
1165 CHECK_CURVE(curve);
1166
1167 if (points == get_point_count(curve) && curve->seq && !(curve->piecewise))
1168 return 1;
1169
1170 if (points < 2)
1171 return 1;
1172
1173 if (curve->wrap_mode == STP_CURVE_WRAP_AROUND)
1174 limit++;
1175 if (limit > curve_point_limit)
1176 return 0;
1177 old = get_real_point_count(curve);
1178 if (old)
1179 old--;
1180 if (!old)
1181 old = 1;
1182
1183 new_vec = stp_malloc(sizeof(double) * limit);
1184
1185 /*
1186 * Be very careful how we calculate the location along the scale!
1187 * If we're not careful how we do it, we might get a small roundoff
1188 * error
1189 */
1190 if (curve->piecewise)
1191 {
1192 double blo, bhi;
1193 int curpos = 0;
1194 stp_sequence_get_bounds(curve->seq, &blo, &bhi);
1195 if (curve->recompute_interval)
1196 compute_intervals(curve);
1197 for (i = 0; i < old; i++)
1198 {
1199 double low;
1200 double high;
1201 double low_y;
1202 double high_y;
1203 double x_delta;
1204 if (!stp_sequence_get_point(curve->seq, i * 2, &low))
1205 {
1206 stp_free(new_vec);
1207 return 0;
1208 }
1209 if (i == old - 1)
1210 high = 1.0;
1211 else if (!stp_sequence_get_point(curve->seq, ((i + 1) * 2), &high))
1212 {
1213 stp_free(new_vec);
1214 return 0;
1215 }
1216 if (!stp_sequence_get_point(curve->seq, (i * 2) + 1, &low_y))
1217 {
1218 stp_free(new_vec);
1219 return 0;
1220 }
1221 if (!stp_sequence_get_point(curve->seq, ((i + 1) * 2) + 1, &high_y))
1222 {
1223 stp_free(new_vec);
1224 return 0;
1225 }
1226 stp_deprintf(STP_DBG_CURVE,
1227 "Filling slots at %ld %d: %f %f %f %f %ld\n",
1228 (long)i,curpos, high, low, high_y, low_y, (long)limit);
1229 x_delta = high - low;
1230 high *= (limit - 1);
1231 low *= (limit - 1);
1232 while (curpos <= high)
1233 {
1234 double frac = (curpos - low) / (high - low);
1235 if (curve->curve_type == STP_CURVE_TYPE_LINEAR)
1236 new_vec[curpos] = low_y + frac * (high_y - low_y);
1237 else
1238 new_vec[curpos] =
1239 do_interpolate_spline(low_y, high_y, frac,
1240 curve->interval[i],
1241 curve->interval[i + 1],
1242 x_delta);
1243 if (new_vec[curpos] < blo)
1244 new_vec[curpos] = blo;
1245 if (new_vec[curpos] > bhi)
1246 new_vec[curpos] = bhi;
1247 stp_deprintf(STP_DBG_CURVE,
1248 " Filling slot %d %f %f\n",
1249 curpos, frac, new_vec[curpos]);
1250 curpos++;
1251 }
1252 }
1253 curve->piecewise = 0;
1254 }
1255 else if (curve->gamma)
1256 {
1257 double fgamma = curve->gamma;
1258 double blo, bhi;
1259 int negative_gamma = 0;
1260 stp_sequence_get_bounds(curve->seq, &blo, &bhi);
1261 if (fgamma > 0)
1262 {
1263 fgamma = -fgamma;
1264 negative_gamma = 1;
1265 }
1266 for (i = 0; i < limit; i++)
1267 {
1268 double where = ((double) i * (double) old / (double) (limit - 1));
1269 if (negative_gamma)
1270 where = 1.0 - where;
1271 new_vec[i] = blo + ((bhi - blo) * pow(where, fgamma));
1272 }
1273 }
1274 else
1275 {
1276 double blo, bhi;
1277 const double *seq_data;
1278 size_t seq_count;
1279 size_t point_count = get_point_count(curve);
1280 stp_sequence_get_data(curve->seq, &seq_count, &seq_data);
1281 stp_sequence_get_bounds(curve->seq, &blo, &bhi);
1282 if (curve->recompute_interval)
1283 compute_intervals((stpi_cast_safe(curve)));
1284 for (i = 0; i < limit; i++)
1285 {
1286 double where = ((double) i * (double) old / (double) (limit - 1));
1287 int iwhere = (int) where;
1288 double frac = where - (double) iwhere;
1289 if (frac == 0.0)
1290 new_vec[i] = seq_data[iwhere];
1291 else if (curve->curve_type == STP_CURVE_TYPE_LINEAR)
1292 new_vec[i] = seq_data[iwhere] + (frac * curve->interval[iwhere]);
1293 else
1294 {
1295 int iwhere1 = iwhere + 1;
1296 while (iwhere1 > point_count)
1297 iwhere1 -= point_count;
1298 new_vec[i] =
1299 do_interpolate_spline(seq_data[iwhere], seq_data[iwhere1],
1300 frac, curve->interval[iwhere],
1301 curve->interval[iwhere1], 1.0);
1302 if (new_vec[i] > bhi)
1303 new_vec[i] = bhi;
1304 else if (new_vec[i] < blo)
1305 new_vec[i] = blo;
1306 }
1307 }
1308 }
1309 stpi_curve_set_data(curve, points, new_vec);
1310 curve->recompute_interval = 1;
1311 stp_free(new_vec);
1312 return 1;
1313 }
1314
1315 static unsigned
1316 gcd(unsigned a, unsigned b)
1317 {
1318 unsigned tmp;
1319 if (b > a)
1320 {
1321 tmp = a;
1322 a = b;
1323 b = tmp;
1324 }
1325 while (1)
1326 {
1327 tmp = a % b;
1328 if (tmp == 0)
1329 return b;
1330 a = b;
1331 b = tmp;
1332 }
1333 }
1334
1335 static unsigned
1336 lcm(unsigned a, unsigned b)
1337 {
1338 if (a == b)
1339 return a;
1340 else if (a * b == 0)
1341 return a > b ? a : b;
1342 else
1343 {
1344 double rval = (double) a / gcd(a, b) * b;
1345 if (rval > curve_point_limit)
1346 return curve_point_limit;
1347 else
1348 return rval;
1349 }
1350 }
1351
1352 static int
1353 create_gamma_curve(stp_curve_t **retval, double lo, double hi, double fgamma,
1354 int points)
1355 {
1356 *retval = stp_curve_create(STP_CURVE_WRAP_NONE);
1357 if (stp_curve_set_bounds(*retval, lo, hi) &&
1358 stp_curve_set_gamma(*retval, fgamma) &&
1359 stp_curve_resample(*retval, points))
1360 return 1;
1361 stp_curve_destroy(*retval);
1362 *retval = 0;
1363 return 0;
1364 }
1365
1366 static int
1367 interpolate_points(stp_curve_t *a, stp_curve_t *b,
1368 stp_curve_compose_t mode,
1369 int points, double *tmp_data)
1370 {
1371 double pa, pb;
1372 int i;
1373 size_t points_a = stp_curve_count_points(a);
1374 size_t points_b = stp_curve_count_points(b);
1375 for (i = 0; i < points; i++)
1376 {
1377 if (!stp_curve_interpolate_value
1378 (a, (double) i * (points_a - 1) / (points - 1), &pa))
1379 {
1380 stp_deprintf(STP_DBG_CURVE_ERRORS,
1381 "interpolate_points: interpolate curve a value failed\n");
1382 return 0;
1383 }
1384 if (!stp_curve_interpolate_value
1385 (b, (double) i * (points_b - 1) / (points - 1), &pb))
1386 {
1387 stp_deprintf(STP_DBG_CURVE_ERRORS,
1388 "interpolate_points: interpolate curve b value failed\n");
1389 return 0;
1390 }
1391 if (mode == STP_CURVE_COMPOSE_ADD)
1392 pa += pb;
1393 else
1394 pa *= pb;
1395 if (! isfinite(pa))
1396 {
1397 stp_deprintf(STP_DBG_CURVE_ERRORS,
1398 "interpolate_points: interpolated point %lu is invalid\n",
1399 (unsigned long) i);
1400 return 0;
1401 }
1402 tmp_data[i] = pa;
1403 }
1404 return 1;
1405 }
1406
1407 int
1408 stp_curve_compose(stp_curve_t **retval,
1409 stp_curve_t *a, stp_curve_t *b,
1410 stp_curve_compose_t mode, int points)
1411 {
1412 stp_curve_t *ret;
1413 double *tmp_data;
1414 double gamma_a = stp_curve_get_gamma(a);
1415 double gamma_b = stp_curve_get_gamma(b);
1416 unsigned points_a = stp_curve_count_points(a);
1417 unsigned points_b = stp_curve_count_points(b);
1418 double alo, ahi, blo, bhi;
1419
1420 if (a->piecewise && b->piecewise)
1421 return 0;
1422 if (a->piecewise)
1423 {
1424 stp_curve_t *a_save = a;
1425 a = stp_curve_create_copy(a_save);
1426 stp_curve_resample(a, stp_curve_count_points(b));
1427 }
1428 if (b->piecewise)
1429 {
1430 stp_curve_t *b_save = b;
1431 b = stp_curve_create_copy(b_save);
1432 stp_curve_resample(b, stp_curve_count_points(a));
1433 }
1434
1435 if (mode != STP_CURVE_COMPOSE_ADD && mode != STP_CURVE_COMPOSE_MULTIPLY)
1436 return 0;
1437 if (stp_curve_get_wrap(a) != stp_curve_get_wrap(b))
1438 return 0;
1439 stp_curve_get_bounds(a, &alo, &ahi);
1440 stp_curve_get_bounds(b, &blo, &bhi);
1441 if (mode == STP_CURVE_COMPOSE_MULTIPLY && (alo < 0 || blo < 0))
1442 return 0;
1443
1444 if (stp_curve_get_wrap(a) == STP_CURVE_WRAP_AROUND)
1445 {
1446 points_a++;
1447 points_b++;
1448 }
1449 if (points == -1)
1450 {
1451 points = lcm(points_a, points_b);
1452 if (stp_curve_get_wrap(a) == STP_CURVE_WRAP_AROUND)
1453 points--;
1454 }
1455 if (points < 2 || points > curve_point_limit ||
1456 ((stp_curve_get_wrap(a) == STP_CURVE_WRAP_AROUND) &&
1457 points > curve_point_limit - 1))
1458 return 0;
1459
1460 if (gamma_a && gamma_b && gamma_a * gamma_b > 0 &&
1461 mode == STP_CURVE_COMPOSE_MULTIPLY)
1462 return create_gamma_curve(retval, alo * blo, ahi * bhi, gamma_a + gamma_b,
1463 points);
1464 tmp_data = stp_malloc(sizeof(double) * points);
1465 if (!interpolate_points(a, b, mode, points, tmp_data))
1466 {
1467 stp_free(tmp_data);
1468 return 0;
1469 }
1470 ret = stp_curve_create(stp_curve_get_wrap(a));
1471 if (mode == STP_CURVE_COMPOSE_ADD)
1472 {
1473 stp_curve_rescale(ret, (ahi - alo) + (bhi - blo),
1474 STP_CURVE_COMPOSE_MULTIPLY, STP_CURVE_BOUNDS_RESCALE);
1475 stp_curve_rescale(ret, alo + blo,
1476 STP_CURVE_COMPOSE_ADD, STP_CURVE_BOUNDS_RESCALE);
1477 }
1478 else
1479 {
1480 stp_curve_rescale(ret, (ahi - alo) * (bhi - blo),
1481 STP_CURVE_COMPOSE_MULTIPLY, STP_CURVE_BOUNDS_RESCALE);
1482 stp_curve_rescale(ret, alo * blo,
1483 STP_CURVE_COMPOSE_ADD, STP_CURVE_BOUNDS_RESCALE);
1484 }
1485 if (! stp_curve_set_data(ret, points, tmp_data))
1486 goto bad1;
1487 *retval = ret;
1488 stp_free(tmp_data);
1489 return 1;
1490 bad1:
1491 stp_curve_destroy(ret);
1492 stp_free(tmp_data);
1493 return 0;
1494 }
1495
1496
1497 stp_curve_t *
1498 stp_curve_create_from_xmltree(stp_mxml_node_t *curve) /* The curve node */
1499 {
1500 const char *stmp; /* Temporary string */
1501 stp_mxml_node_t *child; /* Child sequence node */
1502 stp_curve_t *ret = NULL; /* Curve to return */
1503 stp_curve_type_t curve_type; /* Type of curve */
1504 stp_curve_wrap_mode_t wrap_mode; /* Curve wrap mode */
1505 double fgamma; /* Gamma value */
1506 stp_sequence_t *seq = NULL; /* Sequence data */
1507 double low, high; /* Sequence bounds */
1508 int piecewise = 0;
1509
1510 stp_xml_init();
1511 /* FIXME Need protection against unlimited recursion */
1512 if ((stmp = stp_mxmlElementGetAttr(curve, "src")) != NULL)
1513 return stp_curve_create_from_file(stmp);
1514 /* Get curve type */
1515 stmp = stp_mxmlElementGetAttr(curve, "type");
1516 if (stmp)
1517 {
1518 if (!strcmp(stmp, "linear"))
1519 curve_type = STP_CURVE_TYPE_LINEAR;
1520 else if (!strcmp(stmp, "spline"))
1521 curve_type = STP_CURVE_TYPE_SPLINE;
1522 else
1523 {
1524 stp_deprintf(STP_DBG_CURVE_ERRORS,
1525 "stp_curve_create_from_xmltree: %s: \"type\" invalid\n", stmp);
1526 goto error;
1527 }
1528 }
1529 else
1530 {
1531 stp_deprintf(STP_DBG_CURVE_ERRORS,
1532 "stp_curve_create_from_xmltree: \"type\" missing\n");
1533 goto error;
1534 }
1535 /* Get curve wrap mode */
1536 stmp = stp_mxmlElementGetAttr(curve, "wrap");
1537 if (stmp)
1538 {
1539 if (!strcmp(stmp, "nowrap"))
1540 wrap_mode = STP_CURVE_WRAP_NONE;
1541 else if (!strcmp(stmp, "wrap"))
1542 {
1543 wrap_mode = STP_CURVE_WRAP_AROUND;
1544 }
1545 else
1546 {
1547 stp_deprintf(STP_DBG_CURVE_ERRORS,
1548 "stp_curve_create_from_xmltree: %s: \"wrap\" invalid\n", stmp);
1549 goto error;
1550 }
1551 }
1552 else
1553 {
1554 stp_deprintf(STP_DBG_CURVE_ERRORS,
1555 "stp_curve_create_from_xmltree: \"wrap\" missing\n");
1556 goto error;
1557 }
1558 /* Get curve gamma */
1559 stmp = stp_mxmlElementGetAttr(curve, "gamma");
1560 if (stmp)
1561 {
1562 fgamma = stp_xmlstrtod(stmp);
1563 }
1564 else
1565 {
1566 stp_deprintf(STP_DBG_CURVE_ERRORS,
1567 "stp_curve_create_from_xmltree: \"gamma\" missing\n");
1568 goto error;
1569 }
1570 /* If gamma is set, wrap_mode must be STP_CURVE_WRAP_NONE */
1571 if (fgamma && wrap_mode != STP_CURVE_WRAP_NONE)
1572 {
1573 stp_deprintf(STP_DBG_CURVE_ERRORS, "stp_curve_create_from_xmltree: "
1574 "gamma set and \"wrap\" is not STP_CURVE_WRAP_NONE\n");
1575 goto error;
1576 }
1577 stmp = stp_mxmlElementGetAttr(curve, "piecewise");
1578 if (stmp && strcmp(stmp, "true") == 0)
1579 piecewise = 1;
1580
1581 /* Set up the curve */
1582 ret = stp_curve_create(wrap_mode);
1583 stp_curve_set_interpolation_type(ret, curve_type);
1584
1585 child = stp_xml_get_node(curve, "sequence", NULL);
1586 if (child)
1587 seq = stp_sequence_create_from_xmltree(child);
1588
1589 if (seq == NULL)
1590 {
1591 stp_deprintf(STP_DBG_CURVE_ERRORS,
1592 "stp_curve_create_from_xmltree: sequence read failed\n");
1593 goto error;
1594 }
1595
1596 /* Set curve bounds */
1597 stp_sequence_get_bounds(seq, &low, &high);
1598 stp_curve_set_bounds(ret, low, high);
1599
1600 if (fgamma)
1601 stp_curve_set_gamma(ret, fgamma);
1602 else /* Not a gamma curve, so set points */
1603 {
1604 size_t seq_count;
1605 const double* data;
1606
1607 stp_sequence_get_data(seq, &seq_count, &data);
1608 if (piecewise)
1609 {
1610 if ((seq_count % 2) != 0)
1611 {
1612 stp_deprintf(STP_DBG_CURVE_ERRORS,
1613 "stp_curve_create_from_xmltree: invalid data count %ld\n",
1614 (long)seq_count);
1615 goto error;
1616 }
1617 if (stp_curve_set_data_points(ret, seq_count / 2,
1618 (const stp_curve_point_t *) data) == 0)
1619 {
1620 stp_deprintf(STP_DBG_CURVE_ERRORS,
1621 "stp_curve_create_from_xmltree: failed to set curve data points\n");
1622 goto error;
1623 }
1624 }
1625 else
1626 {
1627 if (stp_curve_set_data(ret, seq_count, data) == 0)
1628 {
1629 stp_deprintf(STP_DBG_CURVE_ERRORS,
1630 "stp_curve_create_from_xmltree: failed to set curve data\n");
1631 goto error;
1632 }
1633 }
1634 }
1635
1636 if (seq)
1637 {
1638 stp_sequence_destroy(seq);
1639 seq = NULL;
1640 }
1641
1642 /* Validate curve */
1643 if (stpi_curve_check_parameters(ret, stp_curve_count_points(ret)) == 0)
1644 {
1645 stp_deprintf(STP_DBG_CURVE_ERRORS,
1646 "stp_curve_create_from_xmltree: parameter check failed\n");
1647 goto error;
1648 }
1649
1650 stp_xml_exit();
1651
1652 return ret;
1653
1654 error:
1655 stp_deprintf(STP_DBG_CURVE_ERRORS,
1656 "stp_curve_create_from_xmltree: error during curve read\n");
1657 if (seq)
1658 {
1659 stp_sequence_destroy(seq);
1660 seq = NULL;
1661 }
1662 if (ret)
1663 stp_curve_destroy(ret);
1664 stp_xml_exit();
1665 return NULL;
1666 }
1667
1668
1669 stp_mxml_node_t *
1670 stp_xmltree_create_from_curve(const stp_curve_t *curve) /* The curve */
1671 {
1672 stp_curve_wrap_mode_t wrapmode;
1673 stp_curve_type_t interptype;
1674 double gammaval, low, high;
1675 stp_sequence_t *seq;
1676
1677 char *cgamma;
1678
1679 stp_mxml_node_t *curvenode = NULL;
1680 stp_mxml_node_t *child = NULL;
1681
1682 stp_xml_init();
1683
1684 /* Get curve details */
1685 wrapmode = stp_curve_get_wrap(curve);
1686 interptype = stp_curve_get_interpolation_type(curve);
1687 gammaval = stp_curve_get_gamma(curve);
1688
1689 if (gammaval && wrapmode != STP_CURVE_WRAP_NONE)
1690 {
1691 stp_deprintf(STP_DBG_CURVE_ERRORS, "stp_xmltree_create_from_curve: "
1692 "curve sets gamma and wrap_mode is not STP_CURVE_WRAP_NONE\n");
1693 goto error;
1694 }
1695
1696 /* Construct the allocated strings required */
1697 stp_asprintf(&cgamma, "%g", gammaval);
1698
1699 curvenode = stp_mxmlNewElement(NULL, "curve");
1700 stp_mxmlElementSetAttr(curvenode, "wrap", stpi_wrap_mode_names[wrapmode]);
1701 stp_mxmlElementSetAttr(curvenode, "type", stpi_curve_type_names[interptype]);
1702 stp_mxmlElementSetAttr(curvenode, "gamma", cgamma);
1703 if (curve->piecewise)
1704 stp_mxmlElementSetAttr(curvenode, "piecewise", "true");
1705 else
1706 stp_mxmlElementSetAttr(curvenode, "piecewise", "false");
1707
1708 stp_free(cgamma);
1709
1710 seq = stp_sequence_create();
1711 stp_curve_get_bounds(curve, &low, &high);
1712 stp_sequence_set_bounds(seq, low, high);
1713 if (gammaval != 0) /* A gamma curve does not require sequence data */
1714 {
1715 stp_sequence_set_size(seq, 0);
1716 }
1717 else
1718 {
1719 const double *data;
1720 size_t count;
1721 data = stpi_curve_get_data_internal(curve, &count);
1722 stp_sequence_set_data(seq, count, data);
1723 }
1724
1725 child = stp_xmltree_create_from_sequence(seq);
1726
1727 if (seq)
1728 {
1729 stp_sequence_destroy(seq);
1730 seq = NULL;
1731 }
1732
1733 if (child == NULL)
1734 {
1735 stp_deprintf(STP_DBG_CURVE_ERRORS,
1736 "stp_xmltree_create_from_curve: sequence node is NULL\n");
1737 goto error;
1738 }
1739 stp_mxmlAdd(curvenode, STP_MXML_ADD_AFTER, NULL, child);
1740
1741 stp_xml_exit();
1742
1743 return curvenode;
1744
1745 error:
1746 stp_deprintf(STP_DBG_CURVE_ERRORS,
1747 "stp_xmltree_create_from_curve: error during xmltree creation\n");
1748 if (curvenode)
1749 stp_mxmlDelete(curvenode);
1750 if (child)
1751 stp_mxmlDelete(child);
1752 stp_xml_exit();
1753
1754 return NULL;
1755 }
1756
1757 static stp_mxml_node_t *
1758 xmldoc_create_from_curve(const stp_curve_t *curve)
1759 {
1760 stp_mxml_node_t *xmldoc;
1761 stp_mxml_node_t *rootnode;
1762 stp_mxml_node_t *curvenode;
1763
1764 /* Get curve details */
1765 curvenode = stp_xmltree_create_from_curve(curve);
1766 if (curvenode == NULL)
1767 {
1768 stp_deprintf(STP_DBG_CURVE_ERRORS,
1769 "xmldoc_create_from_curve: error creating curve node\n");
1770 return NULL;
1771 }
1772 /* Create the XML tree */
1773 xmldoc = stp_xmldoc_create_generic();
1774 if (xmldoc == NULL)
1775 {
1776 stp_deprintf(STP_DBG_CURVE_ERRORS,
1777 "xmldoc_create_from_curve: error creating XML document\n");
1778 return NULL;
1779 }
1780 rootnode = xmldoc->child;
1781 if (rootnode == NULL)
1782 {
1783 stp_mxmlDelete(xmldoc);
1784 stp_deprintf(STP_DBG_CURVE_ERRORS,
1785 "xmldoc_create_from_curve: error getting XML document root node\n");
1786 return NULL;
1787 }
1788
1789 stp_mxmlAdd(rootnode, STP_MXML_ADD_AFTER, NULL, curvenode);
1790
1791 return xmldoc;
1792 }
1793
1794 static int
1795 curve_whitespace_callback(stp_mxml_node_t *node, int where)
1796 {
1797 if (node->type != STP_MXML_ELEMENT)
1798 return 0;
1799 if (strcasecmp(node->value.element.name, "gutenprint") == 0)
1800 {
1801 switch (where)
1802 {
1803 case STP_MXML_WS_AFTER_OPEN:
1804 case STP_MXML_WS_BEFORE_CLOSE:
1805 case STP_MXML_WS_AFTER_CLOSE:
1806 return '\n';
1807 case STP_MXML_WS_BEFORE_OPEN:
1808 default:
1809 return 0;
1810 }
1811 }
1812 else if (strcasecmp(node->value.element.name, "curve") == 0)
1813 {
1814 switch (where)
1815 {
1816 case STP_MXML_WS_AFTER_OPEN:
1817 return '\n';
1818 case STP_MXML_WS_BEFORE_CLOSE:
1819 case STP_MXML_WS_AFTER_CLOSE:
1820 case STP_MXML_WS_BEFORE_OPEN:
1821 default:
1822 return 0;
1823 }
1824 }
1825 else if (strcasecmp(node->value.element.name, "sequence") == 0)
1826 {
1827 const char *count;
1828 switch (where)
1829 {
1830 case STP_MXML_WS_BEFORE_CLOSE:
1831 count = stp_mxmlElementGetAttr(node, "count");
1832 if (strcmp(count, "0") == 0)
1833 return 0;
1834 else
1835 return '\n';
1836 case STP_MXML_WS_AFTER_OPEN:
1837 case STP_MXML_WS_AFTER_CLOSE:
1838 return '\n';
1839 case STP_MXML_WS_BEFORE_OPEN:
1840 default:
1841 return 0;
1842 }
1843 }
1844 else
1845 return 0;
1846 }
1847
1848
1849 int
1850 stp_curve_write(FILE *file, const stp_curve_t *curve) /* The curve */
1851 {
1852 stp_mxml_node_t *xmldoc = NULL;
1853
1854 stp_xml_init();
1855
1856 xmldoc = xmldoc_create_from_curve(curve);
1857 if (xmldoc == NULL)
1858 {
1859 stp_xml_exit();
1860 return 1;
1861 }
1862
1863 stp_mxmlSaveFile(xmldoc, file, curve_whitespace_callback);
1864
1865 if (xmldoc)
1866 stp_mxmlDelete(xmldoc);
1867
1868 stp_xml_exit();
1869
1870 return 0;
1871 }
1872
1873 char *
1874 stp_curve_write_string(const stp_curve_t *curve) /* The curve */
1875 {
1876 stp_mxml_node_t *xmldoc = NULL;
1877 char *retval;
1878
1879 stp_xml_init();
1880
1881 xmldoc = xmldoc_create_from_curve(curve);
1882 if (xmldoc == NULL)
1883 {
1884 stp_xml_exit();
1885 return NULL;
1886 }
1887
1888 retval = stp_mxmlSaveAllocString(xmldoc, curve_whitespace_callback);
1889
1890 if (xmldoc)
1891 stp_mxmlDelete(xmldoc);
1892
1893 stp_xml_exit();
1894
1895 return retval;
1896 }
1897
1898 static stp_curve_t *
1899 xml_doc_get_curve(stp_mxml_node_t *doc)
1900 {
1901 stp_mxml_node_t *cur;
1902 stp_mxml_node_t *xmlcurve;
1903 stp_curve_t *curve = NULL;
1904
1905 if (doc == NULL )
1906 {
1907 stp_deprintf(STP_DBG_CURVE_ERRORS,
1908 "xml_doc_get_curve: XML file not parsed successfully.\n");
1909 return NULL;
1910 }
1911
1912 cur = doc->child;
1913
1914 if (cur == NULL)
1915 {
1916 stp_deprintf(STP_DBG_CURVE_ERRORS,
1917 "xml_doc_get_curve: empty document\n");
1918 return NULL;
1919 }
1920
1921 xmlcurve = stp_xml_get_node(cur, "gutenprint", "curve", NULL);
1922
1923 if (xmlcurve)
1924 curve = stp_curve_create_from_xmltree(xmlcurve);
1925
1926 return curve;
1927 }
1928
1929 stp_curve_t *
1930 stp_curve_create_from_file(const char* file)
1931 {
1932 stp_curve_t *curve = NULL;
1933 stp_mxml_node_t *doc;
1934 FILE *fp = NULL;
1935 if (file[0] != '/' && strncmp(file, "./", 2) && strncmp(file, "../", 3))
1936 {
1937 char *fn = stp_path_find_file(NULL, file);
1938 if (fn)
1939 {
1940 fp = fopen(file, "r");
1941 free(fn);
1942 }
1943 }
1944 else if (file)
1945 {
1946 fp = fopen(file, "r");
1947 }
1948 if (!fp)
1949 {
1950 stp_deprintf(STP_DBG_CURVE_ERRORS,
1951 "stp_curve_create_from_file: unable to open %s: %s\n",
1952 file, strerror(errno));
1953 return NULL;
1954 }
1955 stp_deprintf(STP_DBG_XML, "stp_curve_create_from_file: reading `%s'...\n",
1956 file);
1957
1958 stp_xml_init();
1959
1960 doc = stp_mxmlLoadFile(NULL, fp, STP_MXML_NO_CALLBACK);
1961
1962 curve = xml_doc_get_curve(doc);
1963
1964 if (doc)
1965 stp_mxmlDelete(doc);
1966
1967 stp_xml_exit();
1968 (void) fclose(fp);
1969 return curve;
1970
1971 }
1972
1973 stp_curve_t *
1974 stp_curve_create_from_stream(FILE* fp)
1975 {
1976 stp_curve_t *curve = NULL;
1977 stp_mxml_node_t *doc;
1978 stp_deprintf(STP_DBG_XML, "stp_curve_create_from_fp: reading...\n");
1979
1980 stp_xml_init();
1981
1982 doc = stp_mxmlLoadFile(NULL, fp, STP_MXML_NO_CALLBACK);
1983
1984 curve = xml_doc_get_curve(doc);
1985
1986 if (doc)
1987 stp_mxmlDelete(doc);
1988
1989 stp_xml_exit();
1990 return curve;
1991
1992 }
1993
1994 stp_curve_t *
1995 stp_curve_create_from_string(const char* string)
1996 {
1997 stp_curve_t *curve = NULL;
1998 stp_mxml_node_t *doc;
1999 stp_deprintf(STP_DBG_XML,
2000 "stp_curve_create_from_string: reading '%s'...\n", string);
2001 stp_xml_init();
2002
2003 doc = stp_mxmlLoadString(NULL, string, STP_MXML_NO_CALLBACK);
2004
2005 curve = xml_doc_get_curve(doc);
2006
2007 if (doc)
2008 stp_mxmlDelete(doc);
2009
2010 stp_xml_exit();
2011 return curve;
2012 }