~ubuntu-branches/ubuntu/maverick/gdk-pixbuf/maverick

« back to all changes in this revision

Viewing changes to demo/pixbuf-demo.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-07-23 11:26:25 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100723112625-owhl395g8ifk2wtn
Tags: 2.21.6-2ubuntu1
* Rebase on Debian
* debian/control, debian/gir1.0-gdkpixbuf-2.0.install, debian/rules:
  - build a gir1.0-gdkpixbuf-2.0 gir binary

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* GdkPixbuf library - Scaling and compositing demo
2
 
 *
3
 
 * Copyright (C) 1999 The Free Software Foundation
4
 
 *
5
 
 * Authors: Federico Mena-Quintero <federico@gimp.org>
6
 
 *
7
 
 * This library is free software; you can redistribute it and/or
8
 
 * modify it under the terms of the GNU Library General Public
9
 
 * License as published by the Free Software Foundation; either
10
 
 * version 2 of the License, or (at your option) any later version.
11
 
 *
12
 
 * This library is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
 * Library General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU Library General Public
18
 
 * License along with this library; if not, write to the
19
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
 
 * Boston, MA 02111-1307, USA.
21
 
 */
22
 
 
23
 
#include <config.h>
24
 
#include <stdlib.h>
25
 
#include <gtk/gtk.h>
26
 
#include <gdk-pixbuf/gdk-pixbuf.h>
27
 
#include <math.h>
28
 
 
29
 
 
30
 
 
31
 
#define FRAME_DELAY 50
32
 
 
33
 
#define BACKGROUND_NAME "background.jpg"
34
 
 
35
 
static const char *image_names[] = {
36
 
        "apple-red.png",
37
 
        "gnome-applets.png",
38
 
        "gnome-calendar.png",
39
 
        "gnome-foot.png",
40
 
        "gnome-gmush.png",
41
 
        "gnome-gimp.png",
42
 
        "gnome-gsame.png",
43
 
        "gnu-keys.png"
44
 
};
45
 
 
46
 
#define N_IMAGES (sizeof (image_names) / sizeof (image_names[0]))
47
 
 
48
 
/* Current frame */
49
 
static GdkPixbuf *frame;
50
 
 
51
 
/* Background image */
52
 
static GdkPixbuf *background;
53
 
int back_width, back_height;
54
 
 
55
 
/* Images */
56
 
static GdkPixbuf *images[N_IMAGES];
57
 
 
58
 
/* Widgets */
59
 
GtkWidget *da;
60
 
 
61
 
 
62
 
 
63
 
/* Loads the images for the demo and returns whether the operation succeeded */
64
 
static gboolean
65
 
load_pixbufs (void)
66
 
{
67
 
        int i;
68
 
 
69
 
        background = gdk_pixbuf_new_from_file (BACKGROUND_NAME);
70
 
        if (!background)
71
 
                return FALSE;
72
 
 
73
 
        back_width = gdk_pixbuf_get_width (background);
74
 
        back_height = gdk_pixbuf_get_height (background);
75
 
 
76
 
        for (i = 0; i < N_IMAGES; i++) {
77
 
                images[i] = gdk_pixbuf_new_from_file (image_names[i]);
78
 
                if (!images[i])
79
 
                        return FALSE;
80
 
        }
81
 
 
82
 
        return TRUE;
83
 
}
84
 
 
85
 
/* Expose callback for the drawing area */
86
 
static gint
87
 
expose_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
88
 
{
89
 
        guchar *pixels;
90
 
        int rowstride;
91
 
 
92
 
        rowstride = gdk_pixbuf_get_rowstride (frame);
93
 
 
94
 
        pixels = gdk_pixbuf_get_pixels (frame) + rowstride * event->area.y + event->area.x * 3;
95
 
                  
96
 
        gdk_draw_rgb_image_dithalign (widget->window,
97
 
                                      widget->style->black_gc,
98
 
                                      event->area.x, event->area.y,
99
 
                                      event->area.width, event->area.height,
100
 
                                      GDK_RGB_DITHER_NORMAL,
101
 
                                      pixels, rowstride,
102
 
                                      event->area.x, event->area.y);
103
 
 
104
 
        return TRUE;
105
 
}
106
 
 
107
 
#define CYCLE_LEN 60
108
 
 
109
 
static int frame_num;
110
 
 
111
 
/* Timeout handler to regenerate the frame */
112
 
static gint
113
 
timeout (gpointer data)
114
 
{
115
 
        double f;
116
 
        int i;
117
 
        double xmid, ymid;
118
 
        double radius;
119
 
 
120
 
        gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
121
 
                              frame, 0, 0);
122
 
 
123
 
        f = (double) (frame_num % CYCLE_LEN) / CYCLE_LEN;
124
 
 
125
 
        xmid = back_width / 2.0;
126
 
        ymid = back_height / 2.0;
127
 
 
128
 
        radius = MIN (xmid, ymid) / 2.0;
129
 
 
130
 
        for (i = 0; i < N_IMAGES; i++) {
131
 
                double ang;
132
 
                int xpos, ypos;
133
 
                int iw, ih;
134
 
                double r;
135
 
                GdkRectangle r1, r2, dest;
136
 
                double k;
137
 
 
138
 
                ang = 2.0 * M_PI * (double) i / N_IMAGES - f * 2.0 * M_PI;
139
 
 
140
 
                iw = gdk_pixbuf_get_width (images[i]);
141
 
                ih = gdk_pixbuf_get_height (images[i]);
142
 
 
143
 
                r = radius + (radius / 3.0) * sin (f * 2.0 * M_PI);
144
 
 
145
 
                xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5);
146
 
                ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5);
147
 
 
148
 
                k = (i & 1) ? sin (f * 2.0 * M_PI) : cos (f * 2.0 * M_PI);
149
 
                k = 2.0 * k * k;
150
 
                k = MAX (0.25, k);
151
 
 
152
 
                r1.x = xpos;
153
 
                r1.y = ypos;
154
 
                r1.width = iw * k;
155
 
                r1.height = ih * k;
156
 
 
157
 
                r2.x = 0;
158
 
                r2.y = 0;
159
 
                r2.width = back_width;
160
 
                r2.height = back_height;
161
 
 
162
 
                if (gdk_rectangle_intersect (&r1, &r2, &dest))
163
 
                        gdk_pixbuf_composite (images[i],
164
 
                                              frame,
165
 
                                              dest.x, dest.y,
166
 
                                              dest.width, dest.height,
167
 
                                              xpos, ypos,
168
 
                                              k, k,
169
 
                                              GDK_INTERP_BILINEAR,
170
 
                                              ((i & 1)
171
 
                                               ? MAX (127, fabs (255 * sin (f * 2.0 * M_PI)))
172
 
                                               : MAX (127, fabs (255 * cos (f * 2.0 * M_PI)))));
173
 
        }
174
 
 
175
 
        gtk_widget_draw (da, NULL);
176
 
 
177
 
        frame_num++;
178
 
        return TRUE;
179
 
}
180
 
 
181
 
static guint timeout_id;
182
 
 
183
 
/* Destroy handler for the window */
184
 
static void
185
 
destroy_cb (GtkObject *object, gpointer data)
186
 
{
187
 
        gtk_timeout_remove (timeout_id);
188
 
        timeout_id = 0;
189
 
 
190
 
        gtk_main_quit ();
191
 
}
192
 
 
193
 
int
194
 
main (int argc, char **argv)
195
 
{
196
 
        GtkWidget *window;
197
 
 
198
 
        gtk_init (&argc, &argv);
199
 
        gdk_rgb_init ();
200
 
 
201
 
        if (!load_pixbufs ()) {
202
 
                g_message ("main(): Could not load all the pixbufs!");
203
 
                exit (EXIT_FAILURE);
204
 
        }
205
 
 
206
 
        frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
207
 
 
208
 
        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
209
 
        gtk_widget_set_usize (window, back_width, back_height);
210
 
        gtk_window_set_policy (GTK_WINDOW (window), FALSE, FALSE, FALSE);
211
 
 
212
 
        gtk_signal_connect (GTK_OBJECT (window), "destroy",
213
 
                            GTK_SIGNAL_FUNC (destroy_cb), NULL);
214
 
 
215
 
        gtk_widget_push_visual (gdk_rgb_get_visual ());
216
 
        gtk_widget_push_colormap (gdk_rgb_get_cmap ());
217
 
 
218
 
        da = gtk_drawing_area_new ();
219
 
 
220
 
        gtk_widget_pop_visual ();
221
 
        gtk_widget_pop_colormap ();
222
 
 
223
 
        gtk_signal_connect (GTK_OBJECT (da), "expose_event",
224
 
                            GTK_SIGNAL_FUNC (expose_cb), NULL);
225
 
 
226
 
        gtk_container_add (GTK_CONTAINER (window), da);
227
 
 
228
 
        timeout_id = gtk_timeout_add (FRAME_DELAY, timeout, NULL);
229
 
 
230
 
        gtk_widget_show_all (window);
231
 
        gtk_main ();
232
 
 
233
 
        return 0;
234
 
}