]> Pileus Git - ~andy/gtk/blob - tests/testrgb.c
(committed this again -- broken pipe and ChangeLog update)
[~andy/gtk] / tests / testrgb.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include <glib.h>
23
24 /*
25  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
26  * file for a list of people on the GTK+ Team.  See the ChangeLog
27  * files for a list of changes.  These files are distributed with
28  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
29  */
30
31
32 /* Note: these #includes differ slightly from the testrgb.c file included
33    in the GdkRgb release. */
34
35 #include <stdlib.h>
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 #include <string.h>
40
41 #include "gtk/gtk.h"
42
43 static void
44 quit_func (GtkWidget *widget, gpointer dummy)
45 {
46   gtk_main_quit ();
47 }
48
49 #define WIDTH 640
50 #define HEIGHT 400
51 #define NUM_ITERS 50
52
53 static void
54 testrgb_rgb_test (GtkWidget *drawing_area)
55 {
56   guchar *buf;
57   gint i, j;
58   gint offset;
59   guchar val;
60   gdouble start_time, total_time;
61   gint x, y;
62   gboolean dither;
63   int dith_max;
64   GTimer *timer;
65   GdkPixbuf *pixbuf;
66   gboolean to_pixmap;
67   
68   buf = g_malloc (WIDTH * HEIGHT * 8);
69
70   val = 0;
71   for (j = 0; j < WIDTH * HEIGHT * 8; j++)
72     {
73       val = (val + ((val + (rand () & 0xff)) >> 1)) >> 1;
74       buf[j] = val;
75     }
76
77   /* Let's warm up the cache, and also wait for the window manager
78      to settle. */
79   for (i = 0; i < NUM_ITERS; i++)
80     {
81       offset = (rand () % (WIDTH * HEIGHT * 3)) & -4;
82       gdk_draw_rgb_image (drawing_area->window,
83                           drawing_area->style->white_gc,
84                           0, 0, WIDTH, HEIGHT,
85                           GDK_RGB_DITHER_NONE,
86                           buf + offset, WIDTH * 3);
87     }
88
89   if (gdk_rgb_ditherable ())
90     dith_max = 2;
91   else
92     dith_max = 1;
93
94   timer = g_timer_new ();
95   for (dither = 0; dither < dith_max; dither++)
96     {
97       start_time = g_timer_elapsed (timer, NULL);
98       for (i = 0; i < NUM_ITERS; i++)
99         {
100           offset = (rand () % (WIDTH * HEIGHT * 3)) & -4;
101           gdk_draw_rgb_image (drawing_area->window,
102                               drawing_area->style->white_gc,
103                               0, 0, WIDTH, HEIGHT,
104                               dither ? GDK_RGB_DITHER_MAX :
105                               GDK_RGB_DITHER_NONE,
106                               buf + offset, WIDTH * 3);
107         }
108       gdk_flush ();
109       total_time = g_timer_elapsed (timer, NULL) - start_time;
110       g_print ("Color test%s time elapsed: %.2fs, %.1f fps, %.2f megapixels/s\n",
111                dither ? " (dithered)" : "",
112                total_time,
113                NUM_ITERS / total_time,
114                NUM_ITERS * (WIDTH * HEIGHT * 1e-6) / total_time);
115     }
116
117   for (dither = 0; dither < dith_max; dither++)
118     {
119       start_time = g_timer_elapsed (timer, NULL);
120       for (i = 0; i < NUM_ITERS; i++)
121         {
122           offset = (rand () % (WIDTH * HEIGHT)) & -4;
123           gdk_draw_gray_image (drawing_area->window,
124                                drawing_area->style->white_gc,
125                                0, 0, WIDTH, HEIGHT,
126                                dither ? GDK_RGB_DITHER_MAX :
127                                GDK_RGB_DITHER_NONE,
128                                buf + offset, WIDTH);
129         }
130       gdk_flush ();
131       total_time = g_timer_elapsed (timer, NULL) - start_time;
132       g_print ("Grayscale test%s time elapsed: %.2fs, %.1f fps, %.2f megapixels/s\n",
133                dither ? " (dithered)" : "",
134                total_time,
135                NUM_ITERS / total_time,
136                NUM_ITERS * (WIDTH * HEIGHT * 1e-6) / total_time);
137     }
138
139   for (to_pixmap = FALSE; to_pixmap <= TRUE; to_pixmap++)
140     {
141       if (to_pixmap)
142         {
143           GdkRectangle rect = { 0, 0, WIDTH, HEIGHT };
144           gdk_window_begin_paint_rect (drawing_area->window, &rect);
145         }
146         
147       start_time = g_timer_elapsed (timer, NULL);
148       for (i = 0; i < NUM_ITERS; i++)
149         {
150           offset = (rand () % (WIDTH * HEIGHT * 4)) & -4;
151           pixbuf = gdk_pixbuf_new_from_data (buf + offset, GDK_COLORSPACE_RGB, TRUE,
152                                              8, WIDTH, HEIGHT, WIDTH * 4,
153                                              NULL, NULL);
154           gdk_pixbuf_render_to_drawable_alpha (pixbuf, drawing_area->window,
155                                                0, 0, 0, 0, WIDTH, HEIGHT,
156                                                GDK_PIXBUF_ALPHA_FULL, /* ignored */
157                                                0x80, /* ignored */
158                                                GDK_RGB_DITHER_NORMAL,
159                                                0, 0);
160           gdk_pixbuf_unref (pixbuf);
161         }
162       gdk_flush ();
163       total_time = g_timer_elapsed (timer, NULL) - start_time;
164
165       if (to_pixmap)
166         gdk_window_end_paint (drawing_area->window);
167       
168       g_print ("Alpha test%s time elapsed: %.2fs, %.1f fps, %.2f megapixels/s\n",
169                to_pixmap ? " (to pixmap)" : "",
170                total_time,
171                NUM_ITERS / total_time,
172                NUM_ITERS * (WIDTH * HEIGHT * 1e-6) / total_time);
173     }
174       
175   g_print ("Please submit these results to http://www.levien.com/gdkrgb/survey.html\n");
176
177 #if 1
178   for (x = 0; x < WIDTH; x++)
179     {
180       int cindex;
181
182       cindex = (x * 8) / WIDTH;
183       buf[x * 3] = cindex & 4 ? 0 : 255;
184       buf[x * 3 + 1] = cindex & 2 ? 0 : 255;
185       buf[x * 3 + 2] = cindex & 1 ? 0 : 255;
186     }
187   for (y = 1; y < (HEIGHT * 19) / 32; y++)
188     {
189       memcpy (buf + y * WIDTH * 3, buf, WIDTH * 3);
190     }
191   for (; y < (HEIGHT * 20) / 32; y++)
192     {
193       for (x = 0; x < WIDTH; x++)
194         {
195           guchar gray;
196
197           gray = (x * 255) / (WIDTH - 1);
198           buf[y * WIDTH * 3 + x * 3] = gray;
199           buf[y * WIDTH * 3 + x * 3 + 1] = 0;
200           buf[y * WIDTH * 3 + x * 3 + 2] = 0;
201         }
202     }
203   for (; y < (HEIGHT * 21) / 32; y++)
204     {
205       for (x = 0; x < WIDTH; x++)
206         {
207           guchar gray;
208
209           gray = (x * 255) / (WIDTH - 1);
210           buf[y * WIDTH * 3 + x * 3] = 0;
211           buf[y * WIDTH * 3 + x * 3 + 1] = gray;
212           buf[y * WIDTH * 3 + x * 3 + 2] = 0;
213         }
214     }
215   for (; y < (HEIGHT * 22) / 32; y++)
216     {
217       for (x = 0; x < WIDTH; x++)
218         {
219           guchar gray;
220
221           gray = (x * 255) / (WIDTH - 1);
222           buf[y * WIDTH * 3 + x * 3] = 0;
223           buf[y * WIDTH * 3 + x * 3 + 1] = 0;
224           buf[y * WIDTH * 3 + x * 3 + 2] = gray;
225         }
226     }
227   for (; y < (HEIGHT * 24) / 32; y++)
228     {
229       for (x = 0; x < WIDTH; x++)
230         {
231           guchar gray;
232
233           gray = 112 + (x * 31) / (WIDTH - 1);
234           buf[y * WIDTH * 3 + x * 3] = gray;
235           buf[y * WIDTH * 3 + x * 3 + 1] = gray;
236           buf[y * WIDTH * 3 + x * 3 + 2] = gray;
237         }
238     }
239   for (; y < (HEIGHT * 26) / 32; y++)
240     {
241       for (x = 0; x < WIDTH; x++)
242         {
243           guchar gray;
244
245           gray = (x * 255) / (WIDTH - 1);
246           buf[y * WIDTH * 3 + x * 3] = gray;
247           buf[y * WIDTH * 3 + x * 3 + 1] = gray;
248           buf[y * WIDTH * 3 + x * 3 + 2] = gray;
249         }
250     }
251
252   for (; y < HEIGHT; y++)
253     {
254       for (x = 0; x < WIDTH; x++)
255         {
256           int cindex;
257           guchar gray;
258
259           cindex = (x * 16) / WIDTH;
260           gray = cindex < 3 ? 0 :
261             cindex < 5 ? 255 :
262             cindex < 7 ? 128 :
263             0;
264           buf[y * WIDTH * 3 + x * 3] = gray;
265           buf[y * WIDTH * 3 + x * 3 + 1] = gray;
266           buf[y * WIDTH * 3 + x * 3 + 2] = gray;
267         }
268     }
269   gdk_draw_rgb_image (drawing_area->window,
270                       drawing_area->style->white_gc,
271                       0, 0, WIDTH, HEIGHT, GDK_RGB_DITHER_MAX,
272                       buf, WIDTH * 3);
273 #endif
274 }
275
276 void
277 new_testrgb_window (void)
278 {
279   GtkWidget *window;
280   GtkWidget *vbox;
281   GtkWidget *button;
282   GtkWidget *drawing_area;
283
284   window = gtk_widget_new (gtk_window_get_type (),
285                            "GtkObject::user_data", NULL,
286                            "GtkWindow::type", GTK_WINDOW_TOPLEVEL,
287                            "GtkWindow::title", "testrgb",
288                            "GtkWindow::allow_shrink", FALSE,
289                            NULL);
290   gtk_signal_connect (GTK_OBJECT (window), "destroy",
291                       (GtkSignalFunc) quit_func, NULL);
292
293   vbox = gtk_vbox_new (FALSE, 0);
294
295   drawing_area = gtk_drawing_area_new ();
296
297   gtk_widget_set_usize (drawing_area, WIDTH, HEIGHT);
298   gtk_box_pack_start (GTK_BOX (vbox), drawing_area, FALSE, FALSE, 0);
299   gtk_widget_show (drawing_area);
300
301   button = gtk_button_new_with_label ("Quit");
302   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
303   gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
304                              (GtkSignalFunc) gtk_widget_destroy,
305                              GTK_OBJECT (window));
306
307   gtk_widget_show (button);
308
309   gtk_container_add (GTK_CONTAINER (window), vbox);
310   gtk_widget_show (vbox);
311
312   gtk_widget_show (window);
313
314   testrgb_rgb_test (drawing_area);
315 }
316
317 int
318 main (int argc, char **argv)
319 {
320   gtk_init (&argc, &argv);
321
322   gdk_rgb_set_verbose (TRUE);
323
324   gdk_rgb_init ();
325
326   gtk_widget_set_default_colormap (gdk_rgb_get_cmap ());
327   new_testrgb_window ();
328
329   gtk_main ();
330
331   return 0;
332 }