Debian Codegraph @

src/gutenprintui2/curve.c

raw symbols SCIP
1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1997 David Mosberger
3 * 2004 Roger Leigh
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
17 */
18
19 /*
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
24 */
25
26 /*
27 * Incorporated into Gutenprint by Roger Leigh in 2004. Subsequent
28 * changes are by the Gutenprint team.
29 */
30
31 #include <config.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <math.h>
35
36 #include <gutenprintui2/gutenprintui.h>
37 #include "gutenprintui-internal.h"
38 #include <gutenprint/gutenprint-intl-internal.h>
39
40 #include <gutenprintui2/curve.h>
41 #include <gutenprintui2/typebuiltins.h>
42
43 #define RADIUS 3 /* radius of the control points */
44 #define MIN_DISTANCE 8 /* min distance between control points */
45
46 #define GRAPH_MASK (GDK_EXPOSURE_MASK | \
47 GDK_POINTER_MOTION_MASK | \
48 GDK_POINTER_MOTION_HINT_MASK | \
49 GDK_ENTER_NOTIFY_MASK | \
50 GDK_BUTTON_PRESS_MASK | \
51 GDK_BUTTON_RELEASE_MASK | \
52 GDK_BUTTON1_MOTION_MASK)
53
54 enum {
55 PROP_0,
56 PROP_CURVE_TYPE,
57 PROP_MIN_X,
58 PROP_MAX_X,
59 PROP_MIN_Y,
60 PROP_MAX_Y
61 };
62
63 static GtkDrawingAreaClass *parent_class = NULL;
64 static guint curve_type_changed_signal = 0;
65
66
67 /* forward declarations: */
68 static void stpui_curve_class_init (StpuiCurveClass *class, void *classdata);
69 static void stpui_curve_init (StpuiCurve *curve, gpointer g_class);
70 static void stpui_curve_get_property (GObject *object,
71 guint param_id,
72 GValue *value,
73 GParamSpec *pspec);
74 static void stpui_curve_set_property (GObject *object,
75 guint param_id,
76 const GValue *value,
77 GParamSpec *pspec);
78 static void stpui_curve_finalize (GObject *object);
79 static gint stpui_curve_graph_events (GtkWidget *widget,
80 GdkEvent *event,
81 StpuiCurve *c);
82 static void stpui_curve_size_graph (StpuiCurve *curve);
83
84 GType
85 stpui_curve_get_type (void)
86 {
87 static GType curve_type = 0;
88
89 if (!curve_type)
90 {
91 static const GTypeInfo curve_info =
92 {
93 sizeof (StpuiCurveClass),
94 NULL, /* base_init */
95 NULL, /* base_finalize */
96 (GClassInitFunc) stpui_curve_class_init,
97 NULL, /* class_finalize */
98 NULL, /* class_data */
99 sizeof (StpuiCurve),
100 0, /* n_preallocs */
101 (GInstanceInitFunc) stpui_curve_init,
102 NULL, /* value_table */
103 };
104
105 curve_type = g_type_register_static (GTK_TYPE_DRAWING_AREA,
106 "StpuiCurve",
107 &curve_info, 0);
108 }
109 return curve_type;
110 }
111
112 static void
113 stpui_curve_class_init (StpuiCurveClass *class, void *classdata)
114 {
115 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
116 (void)classdata;
117
118 parent_class = g_type_class_peek_parent (class);
119
120 gobject_class->finalize = stpui_curve_finalize;
121
122 gobject_class->set_property = stpui_curve_set_property;
123 gobject_class->get_property = stpui_curve_get_property;
124
125 g_object_class_install_property (gobject_class,
126 PROP_CURVE_TYPE,
127 g_param_spec_enum ("curve_type",
128 _("Curve type"),
129 _("Is this curve linear, spline interpolated, or free-form"),
130 STPUI_TYPE_CURVE_TYPE,
131 STPUI_CURVE_TYPE_LINEAR,
132 G_PARAM_READABLE |
133 G_PARAM_WRITABLE));
134 g_object_class_install_property (gobject_class,
135 PROP_MIN_X,
136 g_param_spec_float ("min_x",
137 _("Minimum X"),
138 _("Minimum possible value for X"),
139 -G_MAXFLOAT,
140 G_MAXFLOAT,
141 0.0,
142 G_PARAM_READABLE |
143 G_PARAM_WRITABLE));
144 g_object_class_install_property (gobject_class,
145 PROP_MAX_X,
146 g_param_spec_float ("max_x",
147 _("Maximum X"),
148 _("Maximum possible X value"),
149 -G_MAXFLOAT,
150 G_MAXFLOAT,
151 1.0,
152 G_PARAM_READABLE |
153 G_PARAM_WRITABLE));
154 g_object_class_install_property (gobject_class,
155 PROP_MIN_Y,
156 g_param_spec_float ("min_y",
157 _("Minimum Y"),
158 _("Minimum possible value for Y"),
159 -G_MAXFLOAT,
160 G_MAXFLOAT,
161 0.0,
162 G_PARAM_READABLE |
163 G_PARAM_WRITABLE));
164 g_object_class_install_property (gobject_class,
165 PROP_MAX_Y,
166 g_param_spec_float ("max_y",
167 _("Maximum Y"),
168 _("Maximum possible value for Y"),
169 -G_MAXFLOAT,
170 G_MAXFLOAT,
171 1.0,
172 G_PARAM_READABLE |
173 G_PARAM_WRITABLE));
174
175 curve_type_changed_signal =
176 g_signal_new ("curve_type_changed",
177 G_OBJECT_CLASS_TYPE (gobject_class),
178 G_SIGNAL_RUN_FIRST,
179 G_STRUCT_OFFSET (StpuiCurveClass, curve_type_changed),
180 NULL, NULL,
181 g_cclosure_marshal_VOID__VOID,
182 G_TYPE_NONE, 0);
183 }
184
185 static void
186 stpui_curve_init (StpuiCurve *curve, gpointer g_class)
187 {
188 gint old_mask;
189 (void)g_class;
190
191 curve->cursor_type = GDK_TOP_LEFT_ARROW;
192 curve->pixmap = NULL;
193 curve->curve_type = STPUI_CURVE_TYPE_SPLINE;
194 curve->height = 0;
195 curve->grab_point = -1;
196
197 curve->num_points = 0;
198 curve->point = 0;
199
200 curve->num_ctlpoints = 0;
201 curve->ctlpoint = NULL;
202
203 curve->min_x = 0.0;
204 curve->max_x = 1.0;
205 curve->min_y = 0.0;
206 curve->max_y = 1.0;
207
208 old_mask = gtk_widget_get_events (GTK_WIDGET (curve));
209 gtk_widget_set_events (GTK_WIDGET (curve), old_mask | GRAPH_MASK);
210 g_signal_connect (curve, "event",
211 G_CALLBACK (stpui_curve_graph_events), curve);
212 stpui_curve_size_graph (curve);
213 }
214
215 static void
216 stpui_curve_set_property (GObject *object,
217 guint prop_id,
218 const GValue *value,
219 GParamSpec *pspec)
220 {
221 StpuiCurve *curve = STPUI_CURVE (object);
222
223 switch (prop_id)
224 {
225 case PROP_CURVE_TYPE:
226 stpui_curve_set_curve_type (curve, g_value_get_enum (value));
227 break;
228 case PROP_MIN_X:
229 stpui_curve_set_range (curve, g_value_get_float (value), curve->max_x,
230 curve->min_y, curve->max_y);
231 break;
232 case PROP_MAX_X:
233 stpui_curve_set_range (curve, curve->min_x, g_value_get_float (value),
234 curve->min_y, curve->max_y);
235 break;
236 case PROP_MIN_Y:
237 stpui_curve_set_range (curve, curve->min_x, curve->max_x,
238 g_value_get_float (value), curve->max_y);
239 break;
240 case PROP_MAX_Y:
241 stpui_curve_set_range (curve, curve->min_x, curve->max_x,
242 curve->min_y, g_value_get_float (value));
243 break;
244 default:
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246 break;
247 }
248 }
249
250 static void
251 stpui_curve_get_property (GObject *object,
252 guint prop_id,
253 GValue *value,
254 GParamSpec *pspec)
255 {
256 StpuiCurve *curve = STPUI_CURVE (object);
257
258 switch (prop_id)
259 {
260 case PROP_CURVE_TYPE:
261 g_value_set_enum (value, curve->curve_type);
262 break;
263 case PROP_MIN_X:
264 g_value_set_float (value, curve->min_x);
265 break;
266 case PROP_MAX_X:
267 g_value_set_float (value, curve->max_x);
268 break;
269 case PROP_MIN_Y:
270 g_value_set_float (value, curve->min_y);
271 break;
272 case PROP_MAX_Y:
273 g_value_set_float (value, curve->max_y);
274 break;
275 default:
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
277 break;
278 }
279 }
280
281 static int
282 project (gfloat value, gfloat min, gfloat max, int norm)
283 {
284 return (norm - 1) * ((value - min) / (max - min)) + 0.5;
285 }
286
287 static gfloat
288 unproject (gint value, gfloat min, gfloat max, int norm)
289 {
290 return value / (gfloat) (norm - 1) * (max - min) + min;
291 }
292
293 /* Solve the tridiagonal equation system that determines the second
294 derivatives for the interpolation points. (Based on Numerical
295 Recipies 2nd Edition.) */
296 static void
297 spline_solve (int n, gfloat x[], gfloat y[], gfloat y2[])
298 {
299 gfloat p, sig, *u;
300 gint i, k;
301
302 u = g_malloc ((n - 1) * sizeof (u[0]));
303
304 y2[0] = u[0] = 0.0; /* set lower boundary condition to "natural" */
305
306 for (i = 1; i < n - 1; ++i)
307 {
308 sig = (x[i] - x[i - 1]) / (x[i + 1] - x[i - 1]);
309 p = sig * y2[i - 1] + 2.0;
310 y2[i] = (sig - 1.0) / p;
311 u[i] = ((y[i + 1] - y[i])
312 / (x[i + 1] - x[i]) - (y[i] - y[i - 1]) / (x[i] - x[i - 1]));
313 u[i] = (6.0 * u[i] / (x[i + 1] - x[i - 1]) - sig * u[i - 1]) / p;
314 }
315
316 y2[n - 1] = 0.0;
317 for (k = n - 2; k >= 0; --k)
318 y2[k] = y2[k] * y2[k + 1] + u[k];
319
320 g_free (u);
321 }
322
323 static gfloat
324 spline_eval (int n, gfloat x[], gfloat y[], gfloat y2[], gfloat val)
325 {
326 gint k_lo, k_hi, k;
327 gfloat h, b, a;
328
329 /* do a binary search for the right interval: */
330 k_lo = 0; k_hi = n - 1;
331 while (k_hi - k_lo > 1)
332 {
333 k = (k_hi + k_lo) / 2;
334 if (x[k] > val)
335 k_hi = k;
336 else
337 k_lo = k;
338 }
339
340 h = x[k_hi] - x[k_lo];
341 g_assert (h > 0.0);
342
343 a = (x[k_hi] - val) / h;
344 b = (val - x[k_lo]) / h;
345 return a*y[k_lo] + b*y[k_hi] +
346 ((a*a*a - a)*y2[k_lo] + (b*b*b - b)*y2[k_hi]) * (h*h)/6.0;
347 }
348
349 static void
350 stpui_curve_interpolate (StpuiCurve *c, gint width, gint height)
351 {
352 gfloat *vector;
353 int i;
354
355 vector = g_malloc (width * sizeof (vector[0]));
356
357 stpui_curve_get_vector (c, width, vector);
358
359 c->height = height;
360 if (c->num_points != width)
361 {
362 c->num_points = width;
363 if (c->point)
364 g_free (c->point);
365 c->point = g_malloc (c->num_points * sizeof (c->point[0]));
366 }
367
368 for (i = 0; i < width; ++i)
369 {
370 c->point[i].x = RADIUS + i;
371 c->point[i].y = RADIUS + height
372 - project (vector[i], c->min_y, c->max_y, height);
373 }
374
375 g_free (vector);
376 }
377
378 static void
379 stpui_curve_draw (StpuiCurve *c, gint width, gint height)
380 {
381 GtkStateType state;
382 GtkStyle *style;
383 gint i;
384
385 if (!c->pixmap)
386 return;
387
388 if (c->height != height || c->num_points != width)
389 stpui_curve_interpolate (c, width, height);
390
391 state = GTK_STATE_NORMAL;
392 if (!GTK_WIDGET_IS_SENSITIVE (GTK_WIDGET (c)))
393 state = GTK_STATE_INSENSITIVE;
394
395 style = GTK_WIDGET (c)->style;
396
397 /* clear the pixmap: */
398 gtk_paint_flat_box (style, c->pixmap, GTK_STATE_NORMAL, GTK_SHADOW_NONE,
399 NULL, GTK_WIDGET (c), "curve_bg",
400 0, 0, width + RADIUS * 2, height + RADIUS * 2);
401 /* draw the grid lines: (XXX make more meaningful) */
402 for (i = 0; i < 5; i++)
403 {
404 gdk_draw_line (c->pixmap, style->dark_gc[state],
405 RADIUS, i * (height / 4.0) + RADIUS,
406 width + RADIUS, i * (height / 4.0) + RADIUS);
407 gdk_draw_line (c->pixmap, style->dark_gc[state],
408 i * (width / 4.0) + RADIUS, RADIUS,
409 i * (width / 4.0) + RADIUS, height + RADIUS);
410 }
411
412 gdk_draw_points (c->pixmap, style->fg_gc[state], c->point, c->num_points);
413 if (c->curve_type != STPUI_CURVE_TYPE_FREE)
414 for (i = 0; i < c->num_ctlpoints; ++i)
415 {
416 gint x, y;
417
418 if (c->ctlpoint[i][0] < c->min_x)
419 continue;
420
421 x = project (c->ctlpoint[i][0], c->min_x, c->max_x,
422 width);
423 y = height -
424 project (c->ctlpoint[i][1], c->min_y, c->max_y,
425 height);
426
427 /* draw a bullet: */
428 gdk_draw_arc (c->pixmap, style->fg_gc[state], TRUE, x, y,
429 RADIUS * 2, RADIUS*2, 0, 360*64);
430 }
431 gdk_draw_drawable (GTK_WIDGET (c)->window, style->fg_gc[state], c->pixmap,
432 0, 0, 0, 0, width + RADIUS * 2, height + RADIUS * 2);
433 }
434
435 static gint
436 stpui_curve_graph_events (GtkWidget *widget,
437 GdkEvent *event,
438 StpuiCurve *c)
439 {
440 GdkCursorType new_type = c->cursor_type;
441 gint i, src, dst, leftbound, rightbound;
442 GdkEventMotion *mevent;
443 GtkWidget *w;
444 gint tx, ty;
445 gint cx, x, y, width, height;
446 gint closest_point = 0;
447 gfloat rx, ry, min_x;
448 guint distance;
449 gint x1, x2, y1, y2;
450 gint retval = FALSE;
451
452 w = GTK_WIDGET (c);
453 width = w->allocation.width - RADIUS * 2;
454 height = w->allocation.height - RADIUS * 2;
455
456 if ((width < 0) || (height < 0))
457 return FALSE;
458
459 /* get the pointer position */
460 gdk_window_get_pointer (w->window, &tx, &ty, NULL);
461 x = CLAMP ((tx - RADIUS), 0, width-1);
462 y = CLAMP ((ty - RADIUS), 0, height-1);
463
464 min_x = c->min_x;
465
466 distance = ~0U;
467 for (i = 0; i < c->num_ctlpoints; ++i)
468 {
469 cx = project (c->ctlpoint[i][0], min_x, c->max_x, width);
470 if ((guint) abs (x - cx) < distance)
471 {
472 distance = abs (x - cx);
473 closest_point = i;
474 }
475 }
476
477 switch (event->type)
478 {
479 case GDK_CONFIGURE:
480 if (c->pixmap)
481 g_object_unref (c->pixmap);
482 c->pixmap = NULL;
483 /* fall through */
484 case GDK_EXPOSE:
485 if (!c->pixmap)
486 c->pixmap = gdk_pixmap_new (w->window,
487 w->allocation.width,
488 w->allocation.height, -1);
489 stpui_curve_draw (c, width, height);
490 break;
491
492 case GDK_BUTTON_PRESS:
493 gtk_grab_add (widget);
494
495 new_type = GDK_TCROSS;
496
497 switch (c->curve_type)
498 {
499 case STPUI_CURVE_TYPE_LINEAR:
500 case STPUI_CURVE_TYPE_SPLINE:
501 if (distance > MIN_DISTANCE)
502 {
503 /* insert a new control point */
504 if (c->num_ctlpoints > 0)
505 {
506 cx = project (c->ctlpoint[closest_point][0], min_x,
507 c->max_x, width);
508 if (x > cx)
509 ++closest_point;
510 }
511 ++c->num_ctlpoints;
512 c->ctlpoint =
513 g_realloc (c->ctlpoint,
514 c->num_ctlpoints * sizeof (*c->ctlpoint));
515 for (i = c->num_ctlpoints - 1; i > closest_point; --i)
516 memcpy (c->ctlpoint + i, c->ctlpoint + i - 1,
517 sizeof (*c->ctlpoint));
518 }
519 c->grab_point = closest_point;
520 c->ctlpoint[c->grab_point][0] =
521 unproject (x, min_x, c->max_x, width);
522 c->ctlpoint[c->grab_point][1] =
523 unproject (height - y, c->min_y, c->max_y, height);
524
525 stpui_curve_interpolate (c, width, height);
526 break;
527
528 case STPUI_CURVE_TYPE_FREE:
529 c->point[x].x = RADIUS + x;
530 c->point[x].y = RADIUS + y;
531 c->grab_point = x;
532 c->last = y;
533 break;
534 }
535 stpui_curve_draw (c, width, height);
536 retval = TRUE;
537 break;
538
539 case GDK_BUTTON_RELEASE:
540 gtk_grab_remove (widget);
541
542 /* delete inactive points: */
543 if (c->curve_type != STPUI_CURVE_TYPE_FREE)
544 {
545 for (src = dst = 0; src < c->num_ctlpoints; ++src)
546 {
547 if (c->ctlpoint[src][0] >= min_x)
548 {
549 memcpy (c->ctlpoint + dst, c->ctlpoint + src,
550 sizeof (*c->ctlpoint));
551 ++dst;
552 }
553 }
554 if (dst < src)
555 {
556 c->num_ctlpoints -= (src - dst);
557 if (c->num_ctlpoints <= 0)
558 {
559 c->num_ctlpoints = 1;
560 c->ctlpoint[0][0] = min_x;
561 c->ctlpoint[0][1] = c->min_y;
562 stpui_curve_interpolate (c, width, height);
563 stpui_curve_draw (c, width, height);
564 }
565 c->ctlpoint =
566 g_realloc (c->ctlpoint,
567 c->num_ctlpoints * sizeof (*c->ctlpoint));
568 }
569 }
570 new_type = GDK_FLEUR;
571 c->grab_point = -1;
572 retval = TRUE;
573 break;
574
575 case GDK_MOTION_NOTIFY:
576 mevent = (GdkEventMotion *) event;
577
578 switch (c->curve_type)
579 {
580 case STPUI_CURVE_TYPE_LINEAR:
581 case STPUI_CURVE_TYPE_SPLINE:
582 if (c->grab_point == -1)
583 {
584 /* if no point is grabbed... */
585 if (distance <= MIN_DISTANCE)
586 new_type = GDK_FLEUR;
587 else
588 new_type = GDK_TCROSS;
589 }
590 else
591 {
592 /* drag the grabbed point */
593 new_type = GDK_TCROSS;
594
595 leftbound = -MIN_DISTANCE;
596 if (c->grab_point > 0)
597 leftbound = project (c->ctlpoint[c->grab_point - 1][0],
598 min_x, c->max_x, width);
599
600 rightbound = width + RADIUS * 2 + MIN_DISTANCE;
601 if (c->grab_point + 1 < c->num_ctlpoints)
602 rightbound = project (c->ctlpoint[c->grab_point + 1][0],
603 min_x, c->max_x, width);
604
605 if (tx <= leftbound || tx >= rightbound
606 || ty > height + RADIUS * 2 + MIN_DISTANCE
607 || ty < -MIN_DISTANCE)
608 c->ctlpoint[c->grab_point][0] = min_x - 1.0;
609 else
610 {
611 rx = unproject (x, min_x, c->max_x, width);
612 ry = unproject (height - y, c->min_y, c->max_y, height);
613 c->ctlpoint[c->grab_point][0] = rx;
614 c->ctlpoint[c->grab_point][1] = ry;
615 }
616 stpui_curve_interpolate (c, width, height);
617 stpui_curve_draw (c, width, height);
618 }
619 break;
620
621 case STPUI_CURVE_TYPE_FREE:
622 if (c->grab_point != -1)
623 {
624 if (c->grab_point > x)
625 {
626 x1 = x;
627 x2 = c->grab_point;
628 y1 = y;
629 y2 = c->last;
630 }
631 else
632 {
633 x1 = c->grab_point;
634 x2 = x;
635 y1 = c->last;
636 y2 = y;
637 }
638
639 if (x2 != x1)
640 for (i = x1; i <= x2; i++)
641 {
642 c->point[i].x = RADIUS + i;
643 c->point[i].y = RADIUS +
644 (y1 + ((y2 - y1) * (i - x1)) / (x2 - x1));
645 }
646 else
647 {
648 c->point[x].x = RADIUS + x;
649 c->point[x].y = RADIUS + y;
650 }
651 c->grab_point = x;
652 c->last = y;
653 stpui_curve_draw (c, width, height);
654 }
655 if (mevent->state & GDK_BUTTON1_MASK)
656 new_type = GDK_TCROSS;
657 else
658 new_type = GDK_PENCIL;
659 break;
660 }
661 if (new_type != (GdkCursorType) c->cursor_type)
662 {
663 GdkCursor *cursor;
664
665 c->cursor_type = new_type;
666
667 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (w),
668 c->cursor_type);
669 gdk_window_set_cursor (w->window, cursor);
670 gdk_cursor_unref (cursor);
671 }
672 retval = TRUE;
673 break;
674
675 default:
676 break;
677 }
678
679 return retval;
680 }
681
682 void
683 stpui_curve_set_curve_type (StpuiCurve *c, StpuiCurveType new_type)
684 {
685 gfloat rx, dx;
686 gint x, i;
687
688 if (new_type != c->curve_type)
689 {
690 gint width, height;
691
692 width = GTK_WIDGET (c)->allocation.width - RADIUS * 2;
693 height = GTK_WIDGET (c)->allocation.height - RADIUS * 2;
694
695 if (new_type == STPUI_CURVE_TYPE_FREE)
696 {
697 stpui_curve_interpolate (c, width, height);
698 c->curve_type = new_type;
699 }
700 else if (c->curve_type == STPUI_CURVE_TYPE_FREE)
701 {
702 if (c->ctlpoint)
703 g_free (c->ctlpoint);
704 c->num_ctlpoints = 9;
705 c->ctlpoint = g_malloc (c->num_ctlpoints * sizeof (*c->ctlpoint));
706
707 rx = 0.0;
708 dx = (width - 1) / (gfloat) (c->num_ctlpoints - 1);
709
710 for (i = 0; i < c->num_ctlpoints; ++i, rx += dx)
711 {
712 x = (int) (rx + 0.5);
713 c->ctlpoint[i][0] =
714 unproject (x, c->min_x, c->max_x, width);
715 c->ctlpoint[i][1] =
716 unproject (RADIUS + height - c->point[x].y,
717 c->min_y, c->max_y, height);
718 }
719 c->curve_type = new_type;
720 stpui_curve_interpolate (c, width, height);
721 }
722 else
723 {
724 c->curve_type = new_type;
725 stpui_curve_interpolate (c, width, height);
726 }
727 g_signal_emit (c, curve_type_changed_signal, 0);
728 g_object_notify (G_OBJECT (c), "curve_type");
729 stpui_curve_draw (c, width, height);
730 }
731 }
732
733 static void
734 stpui_curve_size_graph (StpuiCurve *curve)
735 {
736 gint width, height;
737 gfloat aspect;
738 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (curve));
739
740 width = (curve->max_x - curve->min_x) + 1;
741 height = (curve->max_y - curve->min_y) + 1;
742 aspect = width / (gfloat) height;
743 if (width > gdk_screen_get_width (screen) / 4)
744 width = gdk_screen_get_width (screen) / 4;
745 if (height > gdk_screen_get_height (screen) / 4)
746 height = gdk_screen_get_height (screen) / 4;
747
748 if (aspect < 1.0)
749 width = height * aspect;
750 else
751 height = width / aspect;
752
753 gtk_widget_set_size_request (GTK_WIDGET (curve),
754 width + RADIUS * 2,
755 height + RADIUS * 2);
756 }
757
758 static void
759 stpui_curve_reset_vector (StpuiCurve *curve)
760 {
761 if (curve->ctlpoint)
762 g_free (curve->ctlpoint);
763
764 curve->num_ctlpoints = 2;
765 curve->ctlpoint = g_malloc (2 * sizeof (curve->ctlpoint[0]));
766 curve->ctlpoint[0][0] = curve->min_x;
767 curve->ctlpoint[0][1] = curve->min_y;
768 curve->ctlpoint[1][0] = curve->max_x;
769 curve->ctlpoint[1][1] = curve->max_y;
770
771 if (curve->pixmap)
772 {
773 gint width, height;
774
775 width = GTK_WIDGET (curve)->allocation.width - RADIUS * 2;
776 height = GTK_WIDGET (curve)->allocation.height - RADIUS * 2;
777
778 if (curve->curve_type == STPUI_CURVE_TYPE_FREE)
779 {
780 curve->curve_type = STPUI_CURVE_TYPE_LINEAR;
781 stpui_curve_interpolate (curve, width, height);
782 curve->curve_type = STPUI_CURVE_TYPE_FREE;
783 }
784 else
785 stpui_curve_interpolate (curve, width, height);
786 stpui_curve_draw (curve, width, height);
787 }
788 }
789
790 void
791 stpui_curve_reset (StpuiCurve *c)
792 {
793 StpuiCurveType old_type;
794
795 old_type = c->curve_type;
796 c->curve_type = STPUI_CURVE_TYPE_SPLINE;
797 stpui_curve_reset_vector (c);
798
799 if (old_type != STPUI_CURVE_TYPE_SPLINE)
800 {
801 g_signal_emit (c, curve_type_changed_signal, 0);
802 g_object_notify (G_OBJECT (c), "curve_type");
803 }
804 }
805
806 void
807 stpui_curve_set_gamma (StpuiCurve *c, gfloat gamma)
808 {
809 gfloat x, one_over_gamma, height;
810 StpuiCurveType old_type;
811 gint i;
812
813 if (c->num_points < 2)
814 return;
815
816 old_type = c->curve_type;
817 c->curve_type = STPUI_CURVE_TYPE_FREE;
818
819 if (gamma <= 0)
820 one_over_gamma = 1.0;
821 else
822 one_over_gamma = 1.0 / gamma;
823 height = c->height;
824 for (i = 0; i < c->num_points; ++i)
825 {
826 x = (gfloat) i / (c->num_points - 1);
827 c->point[i].x = RADIUS + i;
828 c->point[i].y =
829 RADIUS + (height * (1.0 - pow (x, one_over_gamma)) + 0.5);
830 }
831
832 if (old_type != STPUI_CURVE_TYPE_FREE)
833 g_signal_emit (c, curve_type_changed_signal, 0);
834
835 stpui_curve_draw (c, c->num_points, c->height);
836 }
837
838 void
839 stpui_curve_set_range (StpuiCurve *curve,
840 gfloat min_x,
841 gfloat max_x,
842 gfloat min_y,
843 gfloat max_y)
844 {
845 g_object_freeze_notify (G_OBJECT (curve));
846 if (curve->min_x != min_x) {
847 curve->min_x = min_x;
848 g_object_notify (G_OBJECT (curve), "min_x");
849 }
850 if (curve->max_x != max_x) {
851 curve->max_x = max_x;
852 g_object_notify (G_OBJECT (curve), "max_x");
853 }
854 if (curve->min_y != min_y) {
855 curve->min_y = min_y;
856 g_object_notify (G_OBJECT (curve), "min_y");
857 }
858 if (curve->max_y != max_y) {
859 curve->max_y = max_y;
860 g_object_notify (G_OBJECT (curve), "max_y");
861 }
862 g_object_thaw_notify (G_OBJECT (curve));
863
864 stpui_curve_size_graph (curve);
865 stpui_curve_reset_vector (curve);
866 }
867
868 void
869 stpui_curve_set_vector (StpuiCurve *c, int veclen, const gfloat vector[])
870 {
871 StpuiCurveType old_type;
872 gfloat rx, dx, ry;
873 gint i, height;
874 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (c));
875
876 old_type = c->curve_type;
877 c->curve_type = STPUI_CURVE_TYPE_FREE;
878
879 if (c->point)
880 height = GTK_WIDGET (c)->allocation.height - RADIUS * 2;
881 else
882 {
883 height = (c->max_y - c->min_y);
884 if (height > gdk_screen_get_height (screen) / 4)
885 height = gdk_screen_get_height (screen) / 4;
886
887 c->height = height;
888 c->num_points = veclen;
889 c->point = g_malloc (c->num_points * sizeof (c->point[0]));
890 }
891 rx = 0;
892 dx = (veclen - 1.0) / (c->num_points - 1.0);
893
894 for (i = 0; i < c->num_points; ++i, rx += dx)
895 {
896 ry = vector[(int) (rx + 0.5)];
897 if (ry > c->max_y) ry = c->max_y;
898 if (ry < c->min_y) ry = c->min_y;
899 c->point[i].x = RADIUS + i;
900 c->point[i].y =
901 RADIUS + height - project (ry, c->min_y, c->max_y, height);
902 }
903 if (old_type != STPUI_CURVE_TYPE_FREE)
904 {
905 g_signal_emit (c, curve_type_changed_signal, 0);
906 g_object_notify (G_OBJECT (c), "curve_type");
907 }
908
909 stpui_curve_draw (c, c->num_points, height);
910 }
911
912 void
913 stpui_curve_get_vector (StpuiCurve *c, int veclen, gfloat vector[])
914 {
915 gfloat rx, ry, dx, dy, min_x, delta_x, *mem, *xv, *yv, *y2v, prev;
916 gint dst, i, x, next, num_active_ctlpoints = 0, first_active = -1;
917
918 min_x = c->min_x;
919
920 if (c->curve_type != STPUI_CURVE_TYPE_FREE)
921 {
922 /* count active points: */
923 prev = min_x - 1.0;
924 for (i = num_active_ctlpoints = 0; i < c->num_ctlpoints; ++i)
925 if (c->ctlpoint[i][0] > prev)
926 {
927 if (first_active < 0)
928 first_active = i;
929 prev = c->ctlpoint[i][0];
930 ++num_active_ctlpoints;
931 }
932
933 /* handle degenerate case: */
934 if (num_active_ctlpoints < 2)
935 {
936 if (num_active_ctlpoints > 0)
937 ry = c->ctlpoint[first_active][1];
938 else
939 ry = c->min_y;
940 if (ry < c->min_y) ry = c->min_y;
941 if (ry > c->max_y) ry = c->max_y;
942 for (x = 0; x < veclen; ++x)
943 vector[x] = ry;
944 return;
945 }
946 }
947
948 switch (c->curve_type)
949 {
950 case STPUI_CURVE_TYPE_SPLINE:
951 mem = g_malloc (3 * num_active_ctlpoints * sizeof (gfloat));
952 xv = mem;
953 yv = mem + num_active_ctlpoints;
954 y2v = mem + 2*num_active_ctlpoints;
955
956 prev = min_x - 1.0;
957 for (i = dst = 0; i < c->num_ctlpoints; ++i)
958 if (c->ctlpoint[i][0] > prev)
959 {
960 prev = c->ctlpoint[i][0];
961 xv[dst] = c->ctlpoint[i][0];
962 yv[dst] = c->ctlpoint[i][1];
963 ++dst;
964 }
965
966 spline_solve (num_active_ctlpoints, xv, yv, y2v);
967
968 rx = min_x;
969 dx = (c->max_x - min_x) / (veclen - 1);
970 for (x = 0; x < veclen; ++x, rx += dx)
971 {
972 ry = spline_eval (num_active_ctlpoints, xv, yv, y2v, rx);
973 if (ry < c->min_y) ry = c->min_y;
974 if (ry > c->max_y) ry = c->max_y;
975 vector[x] = ry;
976 }
977
978 g_free (mem);
979 break;
980
981 case STPUI_CURVE_TYPE_LINEAR:
982 dx = (c->max_x - min_x) / (veclen - 1);
983 rx = min_x;
984 ry = c->min_y;
985 dy = 0.0;
986 i = first_active;
987 for (x = 0; x < veclen; ++x, rx += dx)
988 {
989 if (rx >= c->ctlpoint[i][0])
990 {
991 if (rx > c->ctlpoint[i][0])
992 ry = c->min_y;
993 dy = 0.0;
994 next = i + 1;
995 while (next < c->num_ctlpoints
996 && c->ctlpoint[next][0] <= c->ctlpoint[i][0])
997 ++next;
998 if (next < c->num_ctlpoints)
999 {
1000 delta_x = c->ctlpoint[next][0] - c->ctlpoint[i][0];
1001 dy = ((c->ctlpoint[next][1] - c->ctlpoint[i][1])
1002 / delta_x);
1003 dy *= dx;
1004 ry = c->ctlpoint[i][1];
1005 i = next;
1006 }
1007 }
1008 vector[x] = ry;
1009 ry += dy;
1010 }
1011 break;
1012
1013 case STPUI_CURVE_TYPE_FREE:
1014 if (c->point)
1015 {
1016 rx = 0.0;
1017 dx = c->num_points / (double) veclen;
1018 for (x = 0; x < veclen; ++x, rx += dx)
1019 vector[x] = unproject (RADIUS + c->height - c->point[(int) rx].y,
1020 c->min_y, c->max_y,
1021 c->height);
1022 }
1023 else
1024 memset (vector, 0, veclen * sizeof (vector[0]));
1025 break;
1026 }
1027 }
1028
1029 GtkWidget*
1030 stpui_curve_new (void)
1031 {
1032 return g_object_new (STPUI_TYPE_CURVE, NULL);
1033 }
1034
1035 static void
1036 stpui_curve_finalize (GObject *object)
1037 {
1038 StpuiCurve *curve;
1039
1040 g_return_if_fail (STPUI_IS_CURVE (object));
1041
1042 curve = STPUI_CURVE (object);
1043 if (curve->pixmap)
1044 g_object_unref (curve->pixmap);
1045 if (curve->point)
1046 g_free (curve->point);
1047 if (curve->ctlpoint)
1048 g_free (curve->ctlpoint);
1049
1050 G_OBJECT_CLASS (parent_class)->finalize (object);
1051 }