]> Pileus Git - ~andy/gtk/blob - gtk/testrgb.c
c629598beee5c960d89b46a6b670b913fe340cb6
[~andy/gtk] / gtk / 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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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-1999.  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.h"
42
43 static void
44 quit_func (GtkWidget *widget, gpointer dummy)
45 {
46   gtk_main_quit ();
47 }
48
49 #define WIDTH 320
50 #define HEIGHT 200
51 #define NUM_ITERS 100
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
66   buf = g_malloc (WIDTH * HEIGHT * 6);
67
68   val = 0;
69   for (j = 0; j < WIDTH * HEIGHT * 6; j++)
70     {
71       val = (val + ((val + (rand () & 0xff)) >> 1)) >> 1;
72       buf[j] = val;
73     }
74
75   /* Let's warm up the cache, and also wait for the window manager
76      to settle. */
77   for (i = 0; i < NUM_ITERS; i++)
78     {
79       offset = (rand () % (WIDTH * HEIGHT * 3)) & -4;
80       gdk_draw_rgb_image (drawing_area->window,
81                           drawing_area->style->white_gc,
82                           0, 0, WIDTH, HEIGHT,
83                           GDK_RGB_DITHER_NONE,
84                           buf + offset, WIDTH * 3);
85     }
86
87   if (gdk_rgb_ditherable ())
88     dith_max = 2;
89   else
90     dith_max = 1;
91
92   timer = g_timer_new ();
93   for (dither = 0; dither < dith_max; dither++)
94     {
95       start_time = g_timer_elapsed (timer, NULL);
96       for (i = 0; i < NUM_ITERS; i++)
97         {
98           offset = (rand () % (WIDTH * HEIGHT * 3)) & -4;
99           gdk_draw_rgb_image (drawing_area->window,
100                               drawing_area->style->white_gc,
101                               0, 0, WIDTH, HEIGHT,
102                               dither ? GDK_RGB_DITHER_MAX :
103                               GDK_RGB_DITHER_NONE,
104                               buf + offset, WIDTH * 3);
105         }
106       total_time = g_timer_elapsed (timer, NULL) - start_time;
107       g_print ("Color test%s time elapsed: %.2fs, %.1f fps, %.2f megapixels/s\n",
108                dither ? " (dithered)" : "",
109                total_time,
110                NUM_ITERS / total_time,
111                NUM_ITERS * (WIDTH * HEIGHT * 1e-6) / total_time);
112     }
113
114   for (dither = 0; dither < dith_max; dither++)
115     {
116       start_time = g_timer_elapsed (timer, NULL);
117       for (i = 0; i < NUM_ITERS; i++)
118         {
119           offset = (rand () % (WIDTH * HEIGHT)) & -4;
120           gdk_draw_gray_image (drawing_area->window,
121                                drawing_area->style->white_gc,
122                                0, 0, WIDTH, HEIGHT,
123                                dither ? GDK_RGB_DITHER_MAX :
124                                GDK_RGB_DITHER_NONE,
125                                buf + offset, WIDTH);
126         }
127       total_time = g_timer_elapsed (timer, NULL) - start_time;
128       g_print ("Grayscale test%s time elapsed: %.2fs, %.1f fps, %.2f megapixels/s\n",
129                dither ? " (dithered)" : "",
130                total_time,
131                NUM_ITERS / total_time,
132                NUM_ITERS * (WIDTH * HEIGHT * 1e-6) / total_time);
133     }
134
135   g_print ("Please submit these results to http://www.levien.com/gdkrgb/survey.html\n");
136
137 #if 1
138   for (x = 0; x < WIDTH; x++)
139     {
140       int cindex;
141
142       cindex = (x * 8) / WIDTH;
143       buf[x * 3] = cindex & 4 ? 0 : 255;
144       buf[x * 3 + 1] = cindex & 2 ? 0 : 255;
145       buf[x * 3 + 2] = cindex & 1 ? 0 : 255;
146     }
147   for (y = 1; y < (HEIGHT * 19) / 32; y++)
148     {
149       memcpy (buf + y * WIDTH * 3, buf, WIDTH * 3);
150     }
151   for (; y < (HEIGHT * 20) / 32; y++)
152     {
153       for (x = 0; x < WIDTH; x++)
154         {
155           guchar gray;
156
157           gray = (x * 255) / (WIDTH - 1);
158           buf[y * WIDTH * 3 + x * 3] = gray;
159           buf[y * WIDTH * 3 + x * 3 + 1] = 0;
160           buf[y * WIDTH * 3 + x * 3 + 2] = 0;
161         }
162     }
163   for (; y < (HEIGHT * 21) / 32; y++)
164     {
165       for (x = 0; x < WIDTH; x++)
166         {
167           guchar gray;
168
169           gray = (x * 255) / (WIDTH - 1);
170           buf[y * WIDTH * 3 + x * 3] = 0;
171           buf[y * WIDTH * 3 + x * 3 + 1] = gray;
172           buf[y * WIDTH * 3 + x * 3 + 2] = 0;
173         }
174     }
175   for (; y < (HEIGHT * 22) / 32; y++)
176     {
177       for (x = 0; x < WIDTH; x++)
178         {
179           guchar gray;
180
181           gray = (x * 255) / (WIDTH - 1);
182           buf[y * WIDTH * 3 + x * 3] = 0;
183           buf[y * WIDTH * 3 + x * 3 + 1] = 0;
184           buf[y * WIDTH * 3 + x * 3 + 2] = gray;
185         }
186     }
187   for (; y < (HEIGHT * 24) / 32; y++)
188     {
189       for (x = 0; x < WIDTH; x++)
190         {
191           guchar gray;
192
193           gray = 112 + (x * 31) / (WIDTH - 1);
194           buf[y * WIDTH * 3 + x * 3] = gray;
195           buf[y * WIDTH * 3 + x * 3 + 1] = gray;
196           buf[y * WIDTH * 3 + x * 3 + 2] = gray;
197         }
198     }
199   for (; y < (HEIGHT * 26) / 32; y++)
200     {
201       for (x = 0; x < WIDTH; x++)
202         {
203           guchar gray;
204
205           gray = (x * 255) / (WIDTH - 1);
206           buf[y * WIDTH * 3 + x * 3] = gray;
207           buf[y * WIDTH * 3 + x * 3 + 1] = gray;
208           buf[y * WIDTH * 3 + x * 3 + 2] = gray;
209         }
210     }
211
212   for (; y < HEIGHT; y++)
213     {
214       for (x = 0; x < WIDTH; x++)
215         {
216           int cindex;
217           guchar gray;
218
219           cindex = (x * 16) / WIDTH;
220           gray = cindex < 3 ? 0 :
221             cindex < 5 ? 255 :
222             cindex < 7 ? 128 :
223             0;
224           buf[y * WIDTH * 3 + x * 3] = gray;
225           buf[y * WIDTH * 3 + x * 3 + 1] = gray;
226           buf[y * WIDTH * 3 + x * 3 + 2] = gray;
227         }
228     }
229   gdk_draw_rgb_image (drawing_area->window,
230                       drawing_area->style->white_gc,
231                       0, 0, WIDTH, HEIGHT, GDK_RGB_DITHER_MAX,
232                       buf, WIDTH * 3);
233 #endif
234 }
235
236 void
237 new_testrgb_window (void)
238 {
239   GtkWidget *window;
240   GtkWidget *vbox;
241   GtkWidget *button;
242   GtkWidget *drawing_area;
243
244   window = gtk_widget_new (gtk_window_get_type (),
245                            "GtkObject::user_data", NULL,
246                            "GtkWindow::type", GTK_WINDOW_TOPLEVEL,
247                            "GtkWindow::title", "testrgb",
248                            "GtkWindow::allow_shrink", FALSE,
249                            NULL);
250   gtk_signal_connect (GTK_OBJECT (window), "destroy",
251                       (GtkSignalFunc) quit_func, NULL);
252
253   vbox = gtk_vbox_new (FALSE, 0);
254
255   drawing_area = gtk_drawing_area_new ();
256
257   gtk_widget_set_usize (drawing_area, WIDTH, HEIGHT);
258   gtk_box_pack_start (GTK_BOX (vbox), drawing_area, FALSE, FALSE, 0);
259   gtk_widget_show (drawing_area);
260
261   button = gtk_button_new_with_label ("Quit");
262   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
263   gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
264                              (GtkSignalFunc) gtk_widget_destroy,
265                              GTK_OBJECT (window));
266
267   gtk_widget_show (button);
268
269   gtk_container_add (GTK_CONTAINER (window), vbox);
270   gtk_widget_show (vbox);
271
272   gtk_widget_show (window);
273
274   testrgb_rgb_test (drawing_area);
275 }
276
277 int
278 main (int argc, char **argv)
279 {
280   gtk_init (&argc, &argv);
281
282   gdk_rgb_set_verbose (TRUE);
283
284   gdk_rgb_init ();
285
286   gtk_widget_set_default_colormap (gdk_rgb_get_cmap ());
287   gtk_widget_set_default_visual (gdk_rgb_get_visual ());
288   new_testrgb_window ();
289
290   gtk_main ();
291
292   return 0;
293 }