]> Pileus Git - ~andy/gtk/blob - tests/testrgb.c
Revert name change
[~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_draw_pixbuf (drawing_area->window, drawing_area->style->black_gc,
155                            pixbuf,
156                            0, 0, 0, 0, WIDTH, HEIGHT,
157                            GDK_RGB_DITHER_NORMAL,
158                            0, 0);
159           g_object_unref (pixbuf);
160         }
161       gdk_flush ();
162       total_time = g_timer_elapsed (timer, NULL) - start_time;
163
164       if (to_pixmap)
165         gdk_window_end_paint (drawing_area->window);
166       
167       g_print ("Alpha test%s time elapsed: %.2fs, %.1f fps, %.2f megapixels/s\n",
168                to_pixmap ? " (to pixmap)" : "",
169                total_time,
170                NUM_ITERS / total_time,
171                NUM_ITERS * (WIDTH * HEIGHT * 1e-6) / total_time);
172     }
173       
174   g_print ("Please submit these results to http://www.levien.com/gdkrgb/survey.html\n");
175
176 #if 1
177   for (x = 0; x < WIDTH; x++)
178     {
179       int cindex;
180
181       cindex = (x * 8) / WIDTH;
182       buf[x * 3] = cindex & 4 ? 0 : 255;
183       buf[x * 3 + 1] = cindex & 2 ? 0 : 255;
184       buf[x * 3 + 2] = cindex & 1 ? 0 : 255;
185     }
186   for (y = 1; y < (HEIGHT * 19) / 32; y++)
187     {
188       memcpy (buf + y * WIDTH * 3, buf, WIDTH * 3);
189     }
190   for (; y < (HEIGHT * 20) / 32; y++)
191     {
192       for (x = 0; x < WIDTH; x++)
193         {
194           guchar gray;
195
196           gray = (x * 255) / (WIDTH - 1);
197           buf[y * WIDTH * 3 + x * 3] = gray;
198           buf[y * WIDTH * 3 + x * 3 + 1] = 0;
199           buf[y * WIDTH * 3 + x * 3 + 2] = 0;
200         }
201     }
202   for (; y < (HEIGHT * 21) / 32; y++)
203     {
204       for (x = 0; x < WIDTH; x++)
205         {
206           guchar gray;
207
208           gray = (x * 255) / (WIDTH - 1);
209           buf[y * WIDTH * 3 + x * 3] = 0;
210           buf[y * WIDTH * 3 + x * 3 + 1] = gray;
211           buf[y * WIDTH * 3 + x * 3 + 2] = 0;
212         }
213     }
214   for (; y < (HEIGHT * 22) / 32; y++)
215     {
216       for (x = 0; x < WIDTH; x++)
217         {
218           guchar gray;
219
220           gray = (x * 255) / (WIDTH - 1);
221           buf[y * WIDTH * 3 + x * 3] = 0;
222           buf[y * WIDTH * 3 + x * 3 + 1] = 0;
223           buf[y * WIDTH * 3 + x * 3 + 2] = gray;
224         }
225     }
226   for (; y < (HEIGHT * 24) / 32; y++)
227     {
228       for (x = 0; x < WIDTH; x++)
229         {
230           guchar gray;
231
232           gray = 112 + (x * 31) / (WIDTH - 1);
233           buf[y * WIDTH * 3 + x * 3] = gray;
234           buf[y * WIDTH * 3 + x * 3 + 1] = gray;
235           buf[y * WIDTH * 3 + x * 3 + 2] = gray;
236         }
237     }
238   for (; y < (HEIGHT * 26) / 32; y++)
239     {
240       for (x = 0; x < WIDTH; x++)
241         {
242           guchar gray;
243
244           gray = (x * 255) / (WIDTH - 1);
245           buf[y * WIDTH * 3 + x * 3] = gray;
246           buf[y * WIDTH * 3 + x * 3 + 1] = gray;
247           buf[y * WIDTH * 3 + x * 3 + 2] = gray;
248         }
249     }
250
251   for (; y < HEIGHT; y++)
252     {
253       for (x = 0; x < WIDTH; x++)
254         {
255           int cindex;
256           guchar gray;
257
258           cindex = (x * 16) / WIDTH;
259           gray = cindex < 3 ? 0 :
260             cindex < 5 ? 255 :
261             cindex < 7 ? 128 :
262             0;
263           buf[y * WIDTH * 3 + x * 3] = gray;
264           buf[y * WIDTH * 3 + x * 3 + 1] = gray;
265           buf[y * WIDTH * 3 + x * 3 + 2] = gray;
266         }
267     }
268   gdk_draw_rgb_image (drawing_area->window,
269                       drawing_area->style->white_gc,
270                       0, 0, WIDTH, HEIGHT, GDK_RGB_DITHER_MAX,
271                       buf, WIDTH * 3);
272 #endif
273 }
274
275 void
276 new_testrgb_window (void)
277 {
278   GtkWidget *window;
279   GtkWidget *vbox;
280   GtkWidget *button;
281   GtkWidget *drawing_area;
282
283   window = g_object_new (gtk_window_get_type (),
284                            "GtkObject::user_data", NULL,
285                            "GtkWindow::type", GTK_WINDOW_TOPLEVEL,
286                            "GtkWindow::title", "testrgb",
287                            "GtkWindow::allow_shrink", FALSE,
288                            NULL);
289   g_signal_connect (window, "destroy",
290                     G_CALLBACK (quit_func), NULL);
291
292   vbox = gtk_vbox_new (FALSE, 0);
293
294   drawing_area = gtk_drawing_area_new ();
295
296   gtk_widget_set_size_request (drawing_area, WIDTH, HEIGHT);
297   gtk_box_pack_start (GTK_BOX (vbox), drawing_area, FALSE, FALSE, 0);
298   gtk_widget_show (drawing_area);
299
300   button = gtk_button_new_with_label ("Quit");
301   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
302   g_signal_connect_swapped (button, "clicked",
303                             G_CALLBACK (gtk_widget_destroy), window);
304
305   gtk_widget_show (button);
306
307   gtk_container_add (GTK_CONTAINER (window), vbox);
308   gtk_widget_show (vbox);
309
310   gtk_widget_show (window);
311
312   testrgb_rgb_test (drawing_area);
313 }
314
315 int
316 main (int argc, char **argv)
317 {
318   gtk_init (&argc, &argv);
319
320   gdk_rgb_set_verbose (TRUE);
321
322   gtk_widget_set_default_colormap (gdk_rgb_get_colormap ());
323   new_testrgb_window ();
324
325   gtk_main ();
326
327   return 0;
328 }