Shows the set of points
[cascardo/movie.git] / gdk.c
1 /*
2  *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19
20 #include <gtk/gtk.h>
21 #include <gdk-pixbuf/gdk-pixbuf.h>
22 #include "point.h"
23
24 struct ctx
25 {
26   GdkPixbuf *picture;
27   GArray *points;
28   int i;
29 };
30
31 #define FILENAME "/home/cascardo/fotos/debconf.jpg"
32 #define WIDTH 800
33 #define HEIGHT 600
34
35 gboolean
36 queue (gpointer data)
37 {
38   gtk_widget_queue_draw (GTK_WIDGET (data));
39   return TRUE;
40 }
41
42 gboolean
43 expose (GtkWidget *widget, GdkEventExpose *event, gpointer data)
44 {
45   GdkPixbuf *screen;
46   struct ctx *ctx;
47   Point point;
48   ctx = (struct ctx *) data;
49   ctx->i = (ctx->i >= ctx->points->len) ? 0 : ctx->i + 1;
50   point = g_array_index (ctx->points, Point, ctx->i);
51   screen = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
52                            event->area.width, event->area.height);
53   gdk_pixbuf_scale (ctx->picture, screen, 0, 0,
54                     event->area.width, event->area.height,
55                     -point.x + event->area.width/2, -point.y + event->area.height/2,
56                     1.0, 1.0, GDK_INTERP_HYPER);
57   gdk_draw_pixbuf (widget->window, NULL, screen, 0, 0, 0, 0, -1, -1,
58                    GDK_RGB_DITHER_NONE, 0, 0);
59   gdk_pixbuf_unref (screen);
60   return FALSE;
61 }
62
63 int
64 main (int argc, char **argv)
65 {
66   char *filename;
67   GdkColorspace colorspace;
68   gboolean has_alpha;
69   int bits_per_sample;
70   int width, height;
71   GtkWidget *window;
72   GtkWidget *draw;
73   struct ctx ctx;
74   gtk_init (&argc, &argv);
75   if (argc < 2)
76     filename = FILENAME;
77   else
78     filename = argv[1];
79   ctx.points = ReadPoints ("pro-gnu");
80   ctx.picture = gdk_pixbuf_new_from_file (filename, NULL);
81   ctx.i = ctx.points->len;
82   colorspace = gdk_pixbuf_get_colorspace (ctx.picture);
83   has_alpha = gdk_pixbuf_get_has_alpha (ctx.picture);
84   bits_per_sample = gdk_pixbuf_get_bits_per_sample (ctx.picture);
85   width = WIDTH;
86   height = HEIGHT;
87   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
88   g_signal_connect (G_OBJECT (window), "destroy",
89                     G_CALLBACK (gtk_main_quit), NULL);
90   draw = gtk_drawing_area_new ();
91   gtk_widget_set_size_request (draw, width, height);
92   gtk_container_add (GTK_CONTAINER (window), draw);
93   gtk_widget_show_all (window);
94   g_signal_connect (G_OBJECT (draw), "expose_event",
95                     G_CALLBACK (expose), &ctx);
96   g_timeout_add (10, queue, draw);
97   gtk_main ();
98   gdk_pixbuf_unref (ctx.picture);
99   return 0;
100 }