Tizen 2.0 Release
[framework/graphics/cairo.git] / util / show-contour.c
1 #define _GNU_SOURCE
2 #include <gtk/gtk.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <gdk/gdkkeysyms.h>
6 #include <math.h>
7
8 typedef struct _point {
9     gdouble x, y;
10 } point_t;
11 typedef struct _box {
12     point_t p1, p2;
13 } box_t;
14
15 typedef struct _contour {
16     struct _contour *next, *prev;
17     int direction;
18     int num_points;
19     int size;
20     point_t points[0];
21 } contour_t;
22
23 typedef struct _TrapView {
24     GtkWidget widget;
25
26     cairo_surface_t *pixmap;
27     int pixmap_width, pixmap_height;
28
29     box_t extents;
30     contour_t *contours;
31
32     double px, py;
33
34     gint mag_x, mag_y;
35     gint mag_size;
36     gdouble mag_zoom;
37     gboolean in_mag_drag;
38     gint mag_drag_x, mag_drag_y;
39 } TrapView;
40
41 typedef struct _TrapViewClass {
42     GtkWidgetClass parent_class;
43 } TrapViewClass;
44
45 G_DEFINE_TYPE (TrapView, trap_view, GTK_TYPE_WIDGET)
46
47 static cairo_surface_t *
48 pixmap_create (TrapView *self, cairo_surface_t *target)
49 {
50     cairo_surface_t *surface =
51         cairo_surface_create_similar (target, CAIRO_CONTENT_COLOR,
52                                       self->widget.allocation.width,
53                                       self->widget.allocation.height);
54     cairo_t *cr = cairo_create (surface);
55     contour_t *contour;
56     gdouble sf_x, sf_y, sf;
57     gdouble mid, dim;
58     gdouble x0,  y0;
59     int n;
60     box_t extents;
61
62     cairo_set_source_rgb (cr, 1, 1, 1);
63     cairo_paint (cr);
64
65     if (self->contours == NULL) {
66         cairo_destroy(cr);
67         return surface;
68     }
69
70     extents = self->extents;
71
72     mid = (extents.p2.x + extents.p1.x) / 2.;
73     dim = (extents.p2.x - extents.p1.x) / 2. * 1.25;
74     sf_x = self->widget.allocation.width / dim / 2;
75
76     mid = (extents.p2.y + extents.p1.y) / 2.;
77     dim = (extents.p2.y - extents.p1.y) / 2. * 1.25;
78     sf_y = self->widget.allocation.height / dim / 2;
79
80     sf = MIN (sf_x, sf_y);
81
82     mid = (extents.p2.x + extents.p1.x) / 2.;
83     dim = sf_x / sf * (extents.p2.x - extents.p1.x) / 2. * 1.25;
84     x0 = mid - dim;
85     mid = (extents.p2.y + extents.p1.y) / 2.;
86     dim = sf_y / sf * (extents.p2.y - extents.p1.y) / 2. * 1.25;
87     y0 = mid - dim;
88
89     for (contour = self->contours; contour; contour = contour->next) {
90         if (contour->num_points == 0)
91             continue;
92
93         cairo_save (cr); {
94             cairo_scale (cr, sf, sf);
95             cairo_translate (cr, -x0, -y0);
96             switch (contour->direction) {
97             case -1:
98                 cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
99                 break;
100             case 0:
101                 cairo_set_source_rgb (cr, 0.0, 1.0, 0.0);
102                 break;
103             case 1:
104                 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
105                 break;
106             }
107             {
108                 const point_t *p = &contour->points[0];
109                 cairo_arc (cr, p->x, p->y, 4/sf, 0, 2 * M_PI);
110                 cairo_save (cr);
111                 cairo_identity_matrix (cr);
112                 cairo_stroke (cr);
113                 cairo_restore (cr);
114             }
115             for (n = 0; n < contour->num_points; n++) {
116                 const point_t *p = &contour->points[n];
117                 cairo_arc (cr, p->x, p->y, 2/sf, 0, 2 * M_PI);
118                 cairo_fill (cr);
119             }
120             for (n = 0; n < contour->num_points; n++) {
121                 const point_t *p = &contour->points[n];
122                 cairo_line_to (cr, p->x, p->y);
123             }
124         } cairo_restore (cr);
125
126         switch (contour->direction) {
127         case -1:
128             cairo_set_source_rgb (cr, 0.3, 0.3, 0.9);
129             break;
130         case 0:
131             cairo_set_source_rgb (cr, 0.3, 0.9, 0.3);
132             break;
133         case 1:
134             cairo_set_source_rgb (cr, 0.9, 0.3, 0.3);
135             break;
136         }
137         cairo_set_line_width (cr, 1.);
138         cairo_stroke (cr);
139     }
140
141     cairo_destroy (cr);
142     return surface;
143 }
144
145 static void
146 trap_view_draw (TrapView *self, cairo_t *cr)
147 {
148     contour_t *contour;
149     gdouble sf_x, sf_y, sf;
150     gdouble mid, dim;
151     gdouble x0,  y0;
152     int n;
153     box_t extents;
154
155     extents = self->extents;
156
157     mid = (extents.p2.x + extents.p1.x) / 2.;
158     dim = (extents.p2.x - extents.p1.x) / 2. * 1.25;
159     sf_x = self->widget.allocation.width / dim / 2;
160
161     mid = (extents.p2.y + extents.p1.y) / 2.;
162     dim = (extents.p2.y - extents.p1.y) / 2. * 1.25;
163     sf_y = self->widget.allocation.height / dim / 2;
164
165     sf = MIN (sf_x, sf_y);
166
167     mid = (extents.p2.x + extents.p1.x) / 2.;
168     dim = sf_x / sf * (extents.p2.x - extents.p1.x) / 2. * 1.25;
169     x0 = mid - dim;
170     mid = (extents.p2.y + extents.p1.y) / 2.;
171     dim = sf_y / sf * (extents.p2.y - extents.p1.y) / 2. * 1.25;
172     y0 = mid - dim;
173
174     if (self->pixmap_width != self->widget.allocation.width ||
175         self->pixmap_height != self->widget.allocation.height)
176     {
177         cairo_surface_destroy (self->pixmap);
178         self->pixmap = pixmap_create (self, cairo_get_target (cr));
179         self->pixmap_width = self->widget.allocation.width;
180         self->pixmap_height = self->widget.allocation.height;
181     }
182
183     cairo_set_source_surface (cr, self->pixmap, 0, 0);
184     cairo_paint (cr);
185
186     if (self->contours == NULL)
187         return;
188
189     /* draw a zoom view of the area around the mouse */
190     if (1) {
191         double zoom = self->mag_zoom;
192         int size = self->mag_size;
193         int mag_x = self->mag_x;
194         int mag_y = self->mag_y;
195
196         if (1) {
197             if (self->px + size < self->widget.allocation.width/2)
198                 mag_x = self->px + size/4;
199             else
200                 mag_x = self->px - size/4 - size;
201             mag_y = self->py - size/2;
202             if (mag_y < 0)
203                 mag_y = 0;
204             if (mag_y + size > self->widget.allocation.height)
205                 mag_y = self->widget.allocation.height - size;
206         }
207
208         cairo_save (cr); {
209             /* bottom right */
210             cairo_rectangle (cr, mag_x, mag_y, size, size);
211             cairo_stroke_preserve (cr);
212             cairo_set_source_rgb (cr, 1, 1, 1);
213             cairo_fill_preserve (cr);
214             cairo_clip (cr);
215
216             /* compute roi in extents */
217             cairo_translate (cr, mag_x + size/2, mag_y + size/2);
218
219             cairo_save (cr); {
220                 for (contour = self->contours; contour; contour = contour->next) {
221                     if (contour->num_points == 0)
222                         continue;
223
224                     cairo_save (cr); {
225                         cairo_scale (cr, zoom, zoom);
226                         cairo_translate (cr, -(self->px / sf + x0), -(self->py /sf + y0));
227                         switch (contour->direction) {
228                         case -1:
229                             cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
230                             break;
231                         case 0:
232                             cairo_set_source_rgb (cr, 0.0, 1.0, 0.0);
233                             break;
234                         case 1:
235                             cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
236                             break;
237                         }
238                         {
239                             const point_t *p = &contour->points[0];
240                             cairo_arc (cr, p->x, p->y, 4/zoom, 0, 2 * M_PI);
241                             cairo_save (cr);
242                             cairo_identity_matrix (cr);
243                             cairo_stroke (cr);
244                             cairo_restore (cr);
245                         }
246                         for (n = 0; n < contour->num_points; n++) {
247                             const point_t *p = &contour->points[n];
248                             cairo_arc (cr, p->x, p->y, 2/zoom, 0, 2 * M_PI);
249                             cairo_fill (cr);
250                         }
251                         for (n = 0; n < contour->num_points; n++) {
252                             const point_t *p = &contour->points[n];
253                             cairo_line_to (cr, p->x, p->y);
254                         }
255                     } cairo_restore (cr);
256
257                     switch (contour->direction) {
258                     case -1:
259                         cairo_set_source_rgb (cr, 0.3, 0.3, 0.9);
260                         break;
261                     case 0:
262                         cairo_set_source_rgb (cr, 0.3, 0.9, 0.3);
263                         break;
264                     case 1:
265                         cairo_set_source_rgb (cr, 0.9, 0.3, 0.3);
266                         break;
267                     }
268                     cairo_stroke (cr);
269                 }
270             } cairo_restore (cr);
271
272             /* grid */
273             cairo_save (cr); {
274                 int i;
275
276                 cairo_translate (cr,
277                                  -zoom*fmod (self->px/sf + x0, 1.),
278                                  -zoom*fmod (self->py/sf + y0, 1.));
279                 zoom /= 2;
280                 for (i = -size/2/zoom; i <= size/2/zoom + 1; i+=2) {
281                     cairo_move_to (cr, zoom*i, -size/2);
282                     cairo_line_to (cr, zoom*i, size/2 + zoom);
283                     cairo_move_to (cr, -size/2, zoom*i);
284                     cairo_line_to (cr, size/2 + zoom, zoom*i);
285                 }
286                 zoom *= 2;
287                 cairo_set_source_rgba (cr, .7, .7, .7, .5);
288                 cairo_set_line_width (cr, 1.);
289                 cairo_stroke (cr);
290
291                 for (i = -size/2/zoom - 1; i <= size/2/zoom + 1; i++) {
292                     cairo_move_to (cr, zoom*i, -size/2);
293                     cairo_line_to (cr, zoom*i, size/2 + zoom);
294                     cairo_move_to (cr, -size/2, zoom*i);
295                     cairo_line_to (cr, size/2 + zoom, zoom*i);
296                 }
297                 cairo_set_source_rgba (cr, .1, .1, .1, .5);
298                 cairo_set_line_width (cr, 2.);
299                 cairo_stroke (cr);
300             } cairo_restore (cr);
301
302         } cairo_restore (cr);
303     }
304 }
305
306
307 static gdouble
308 edge_length (const point_t *p1, const point_t *p2)
309 {
310     return hypot (p2->x - p1->x, p2->y - p1->y);
311 }
312
313 static gdouble
314 contour_compute_total_length (const contour_t *contour)
315 {
316     int n;
317     gdouble len = 0.;
318     for (n = 1; n < contour->num_points; n++)
319         len += edge_length (&contour->points[n-1], &contour->points[n]);
320     return len;
321 }
322
323 static void
324 trap_view_draw_labels (TrapView *self, cairo_t *cr)
325 {
326     contour_t *contour;
327     int y = 12;
328
329     for (contour = self->contours; contour; contour = contour->next) {
330         double total_length = contour_compute_total_length (contour) / 256.;
331         PangoLayout *layout;
332         gint width, height;
333         GString *string;
334         gchar *str;
335
336         if (contour->num_points == 0)
337             continue;
338
339         string = g_string_new (NULL);
340         g_string_append_printf (string,
341                                 "Number of points:\t%d\n"
342                                 "Total length of contour: \t%.2f",
343                                 contour->num_points,
344                                 total_length);
345
346         str = g_string_free (string, FALSE);
347         layout = gtk_widget_create_pango_layout (&self->widget, str);
348         g_free (str);
349
350         pango_layout_get_pixel_size (layout, &width, &height);
351
352         switch (contour->direction) {
353         case -1:
354             cairo_set_source_rgb (cr, 0.9, 0.3, 0.3);
355             break;
356         case 0:
357             cairo_set_source_rgb (cr, 0.3, 0.9, 0.3);
358             break;
359         case 1:
360             cairo_set_source_rgb (cr, 0.3, 0.3, 0.9);
361             break;
362         }
363
364         cairo_move_to (cr, 10, y);
365         pango_cairo_show_layout (cr, layout);
366         g_object_unref (layout);
367
368         y += height + 4;
369     }
370 }
371
372 static gboolean
373 trap_view_expose (GtkWidget *w, GdkEventExpose *ev)
374 {
375     TrapView *self = (TrapView *) w;
376     cairo_t *cr;
377
378     cr = gdk_cairo_create (w->window);
379     gdk_cairo_region (cr, ev->region);
380     cairo_clip (cr);
381
382     trap_view_draw (self, cr);
383     trap_view_draw_labels (self, cr);
384
385     cairo_destroy (cr);
386     return FALSE;
387 }
388
389 static gboolean
390 trap_view_key_press (GtkWidget *w, GdkEventKey *ev)
391 {
392     switch (ev->keyval) {
393     case GDK_Escape:
394     case GDK_Q:
395         gtk_main_quit ();
396         break;
397     }
398
399     return FALSE;
400 }
401
402 static gboolean
403 trap_view_button_press (GtkWidget *w, GdkEventButton *ev)
404 {
405     TrapView *self = (TrapView *) w;
406
407     if (ev->x < self->mag_x ||
408         ev->y < self->mag_y ||
409         ev->x > self->mag_x + self->mag_size ||
410         ev->y > self->mag_y + self->mag_size)
411     {
412     }
413     else
414     {
415         self->in_mag_drag = TRUE;
416         self->mag_drag_x = ev->x;
417         self->mag_drag_y = ev->y;
418     }
419
420     return FALSE;
421 }
422
423 static gboolean
424 trap_view_button_release (GtkWidget *w, GdkEventButton *ev)
425 {
426     TrapView *self = (TrapView *) w;
427
428     self->in_mag_drag = FALSE;
429
430     return FALSE;
431 }
432
433 static void
434 trap_view_update_mouse (TrapView *self, GdkEventMotion *ev)
435 {
436     self->px = ev->x;
437     self->py = ev->y;
438
439     gtk_widget_queue_draw (&self->widget);
440 }
441
442 static void
443 trap_view_update_magnifier (TrapView *self, gint *xy)
444 {
445     self->mag_x = xy[0];
446     self->mag_y = xy[1];
447
448     gtk_widget_queue_draw (&self->widget);
449 }
450
451 static gboolean
452 trap_view_motion (GtkWidget *w, GdkEventMotion *ev)
453 {
454     TrapView *self = (TrapView *) w;
455
456     if (self->in_mag_drag) {
457         int xy[2];
458
459         xy[0] = self->mag_x + ev->x - self->mag_drag_x;
460         xy[1] = self->mag_y + ev->y - self->mag_drag_y;
461
462         trap_view_update_magnifier (self, xy);
463
464         self->mag_drag_x = ev->x;
465         self->mag_drag_y = ev->y;
466     } else if (ev->x < self->mag_x ||
467                ev->y < self->mag_y ||
468                ev->x > self->mag_x + self->mag_size ||
469                ev->y > self->mag_y + self->mag_size)
470     {
471         trap_view_update_mouse (self, ev);
472     }
473
474     return FALSE;
475 }
476
477 static void
478 trap_view_realize (GtkWidget *widget)
479 {
480     GdkWindowAttr attributes;
481
482     GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
483
484     attributes.window_type = GDK_WINDOW_CHILD;
485     attributes.x = widget->allocation.x;
486     attributes.y = widget->allocation.y;
487     attributes.width  = widget->allocation.width;
488     attributes.height = widget->allocation.height;
489     attributes.wclass = GDK_INPUT_OUTPUT;
490     attributes.visual = gtk_widget_get_visual (widget);
491     attributes.colormap = gtk_widget_get_colormap (widget);
492     attributes.event_mask = gtk_widget_get_events (widget) |
493                             GDK_BUTTON_PRESS_MASK |
494                             GDK_BUTTON_RELEASE_MASK |
495                             GDK_KEY_PRESS_MASK |
496                             GDK_KEY_RELEASE_MASK |
497                             GDK_POINTER_MOTION_MASK |
498                             GDK_BUTTON_MOTION_MASK |
499                             GDK_EXPOSURE_MASK;
500
501     widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
502                                      &attributes,
503                                      GDK_WA_X | GDK_WA_Y |
504                                      GDK_WA_VISUAL | GDK_WA_COLORMAP);
505     gdk_window_set_user_data (widget->window, widget);
506
507     widget->style = gtk_style_attach (widget->style, widget->window);
508     gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
509 }
510
511 static void
512 trap_view_size_allocate (GtkWidget *w, GdkRectangle *r)
513 {
514     TrapView *self = (TrapView *) w;
515
516     GTK_WIDGET_CLASS (trap_view_parent_class)->size_allocate (w, r);
517
518     self->mag_x = w->allocation.width - self->mag_size - 10;
519     self->mag_y = w->allocation.height - self->mag_size - 10;
520 }
521
522 static void
523 trap_view_finalize (GObject *obj)
524 {
525     G_OBJECT_CLASS (trap_view_parent_class)->finalize (obj);
526 }
527
528 static void
529 trap_view_class_init (TrapViewClass *klass)
530 {
531     GObjectClass *object_class = (GObjectClass *) klass;
532     GtkWidgetClass *widget_class = (GtkWidgetClass *) klass;
533
534     object_class->finalize = trap_view_finalize;
535
536     widget_class->realize = trap_view_realize;
537     widget_class->size_allocate = trap_view_size_allocate;
538     widget_class->expose_event = trap_view_expose;
539     widget_class->key_press_event = trap_view_key_press;
540     widget_class->button_press_event = trap_view_button_press;
541     widget_class->button_release_event = trap_view_button_release;
542     widget_class->motion_notify_event = trap_view_motion;
543 }
544
545 static void
546 trap_view_init (TrapView *self)
547 {
548     self->mag_zoom = 64;
549     self->mag_size = 200;
550
551     self->extents.p1.x = G_MAXDOUBLE;
552     self->extents.p1.y = G_MAXDOUBLE;
553     self->extents.p2.x = -G_MAXDOUBLE;
554     self->extents.p2.y = -G_MAXDOUBLE;
555
556     GTK_WIDGET_SET_FLAGS (self, GTK_CAN_FOCUS);
557 }
558
559 static contour_t *
560 _contour_add_point (TrapView *tv, contour_t *contour, point_t *p)
561 {
562     if (contour == NULL)
563         return NULL;
564
565     if (p->y < tv->extents.p1.y)
566         tv->extents.p1.y = p->y;
567     if (p->y > tv->extents.p2.y)
568         tv->extents.p2.y = p->y;
569
570     if (p->x < tv->extents.p1.x)
571         tv->extents.p1.x = p->x;
572     if (p->x > tv->extents.p2.x)
573         tv->extents.p2.x = p->x;
574
575     if (contour->num_points == contour->size) {
576         int newsize = 2 * contour->size;
577         void *newcontour;
578
579         newcontour = g_realloc (contour,
580                               sizeof (contour_t) + newsize * sizeof (point_t));
581         if (newcontour == NULL)
582             return contour;
583
584         contour = newcontour;
585         contour->size = newsize;
586
587         if (contour->next != NULL)
588             contour->next->prev = newcontour;
589         if (contour->prev != NULL)
590             contour->prev->next = newcontour;
591         else
592             tv->contours = newcontour;
593     }
594
595     contour->points[contour->num_points++] = *p;
596
597     return contour;
598 }
599
600 static contour_t *
601 contour_new (TrapView *tv, int direction)
602 {
603     contour_t *t;
604
605     t = g_malloc (sizeof (contour_t) + 128 * sizeof (point_t));
606     t->direction = direction;
607     t->prev = NULL;
608     t->next = tv->contours;
609     if (tv->contours)
610         tv->contours->prev = t;
611     tv->contours = t;
612
613     t->size = 128;
614     t->num_points = 0;
615
616     return t;
617 }
618
619 int
620 main (int argc, char **argv)
621 {
622     TrapView *tv;
623     contour_t *contour = NULL;
624     GtkWidget *window;
625     FILE *file;
626     char *line = NULL;
627     size_t len = 0;
628
629     gtk_init (&argc, &argv);
630
631     tv = g_object_new (trap_view_get_type (), NULL);
632
633     file = fopen (argv[1], "r");
634     if (file != NULL) {
635         while (getline (&line, &len, file) != -1) {
636             point_t p;
637             int direction;
638
639             if (sscanf (line, "contour: direction=%d", &direction)) {
640                 if (contour)
641                     g_print ("read %d contour\n", contour->num_points);
642
643                 contour = contour_new (tv, direction);
644             } else if (sscanf (line, "  [%*d] = (%lf, %lf)", &p.x, &p.y) == 2) {
645                 contour = _contour_add_point (tv, contour, &p);
646             }
647         }
648
649         if (contour)
650             g_print ("read %d contour\n", contour->num_points);
651
652         g_print ("extents=(%lg, %lg), (%lg, %lg)\n",
653                  tv->extents.p1.x, tv->extents.p1.y,
654                  tv->extents.p2.x, tv->extents.p2.y);
655         fclose (file);
656     }
657
658     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
659     g_signal_connect (window, "delete-event",
660                       G_CALLBACK (gtk_main_quit), NULL);
661     gtk_widget_set_size_request (window, 800, 800);
662     gtk_container_add (GTK_CONTAINER (window), &tv->widget);
663     gtk_widget_show_all (window);
664
665     gtk_main ();
666     return 0;
667 }