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