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 #undef GDK_DISABLE_DEPRECATED
42 
43 #include "gtk/gtk.h"
44 
45 static void
quit_func(GtkWidget * widget,gpointer dummy)46 quit_func (GtkWidget *widget, gpointer dummy)
47 {
48   gtk_main_quit ();
49 }
50 
51 #define WIDTH 640
52 #define HEIGHT 400
53 #define NUM_ITERS 50
54 
55 static void
testrgb_rgb_test(GtkWidget * drawing_area)56 testrgb_rgb_test (GtkWidget *drawing_area)
57 {
58   guchar *buf;
59   gint i, j;
60   gint offset;
61   guchar val;
62   gdouble start_time, total_time;
63   gint x, y;
64   gboolean dither;
65   int dith_max;
66   GTimer *timer;
67   GdkPixbuf *pixbuf;
68   gboolean to_pixmap;
69 
70   buf = g_malloc (WIDTH * HEIGHT * 8);
71 
72   val = 0;
73   for (j = 0; j < WIDTH * HEIGHT * 8; j++)
74     {
75       val = (val + ((val + (rand () & 0xff)) >> 1)) >> 1;
76       buf[j] = val;
77     }
78 
79   /* Let's warm up the cache, and also wait for the window manager
80      to settle. */
81   for (i = 0; i < NUM_ITERS; i++)
82     {
83       offset = (rand () % (WIDTH * HEIGHT * 3)) & -4;
84       gdk_draw_rgb_image (drawing_area->window,
85 			  drawing_area->style->white_gc,
86 			  0, 0, WIDTH, HEIGHT,
87 			  GDK_RGB_DITHER_NONE,
88 			  buf + offset, WIDTH * 3);
89     }
90 
91   if (gdk_rgb_ditherable ())
92     dith_max = 2;
93   else
94     dith_max = 1;
95 
96   timer = g_timer_new ();
97   for (dither = 0; dither < dith_max; dither++)
98     {
99       start_time = g_timer_elapsed (timer, NULL);
100       for (i = 0; i < NUM_ITERS; i++)
101 	{
102 	  offset = (rand () % (WIDTH * HEIGHT * 3)) & -4;
103 	  gdk_draw_rgb_image (drawing_area->window,
104 			      drawing_area->style->white_gc,
105 			      0, 0, WIDTH, HEIGHT,
106 			      dither ? GDK_RGB_DITHER_MAX :
107 			      GDK_RGB_DITHER_NONE,
108 			      buf + offset, WIDTH * 3);
109 	}
110       gdk_flush ();
111       total_time = g_timer_elapsed (timer, NULL) - start_time;
112       g_print ("Color test%s time elapsed: %.2fs, %.1f fps, %.2f megapixels/s\n",
113 	       dither ? " (dithered)" : "",
114 	       total_time,
115 	       NUM_ITERS / total_time,
116 	       NUM_ITERS * (WIDTH * HEIGHT * 1e-6) / total_time);
117     }
118 
119   for (dither = 0; dither < dith_max; dither++)
120     {
121       start_time = g_timer_elapsed (timer, NULL);
122       for (i = 0; i < NUM_ITERS; i++)
123 	{
124 	  offset = (rand () % (WIDTH * HEIGHT)) & -4;
125 	  gdk_draw_gray_image (drawing_area->window,
126 			       drawing_area->style->white_gc,
127 			       0, 0, WIDTH, HEIGHT,
128 			       dither ? GDK_RGB_DITHER_MAX :
129 			       GDK_RGB_DITHER_NONE,
130 			       buf + offset, WIDTH);
131 	}
132       gdk_flush ();
133       total_time = g_timer_elapsed (timer, NULL) - start_time;
134       g_print ("Grayscale test%s time elapsed: %.2fs, %.1f fps, %.2f megapixels/s\n",
135 	       dither ? " (dithered)" : "",
136 	       total_time,
137 	       NUM_ITERS / total_time,
138 	       NUM_ITERS * (WIDTH * HEIGHT * 1e-6) / total_time);
139     }
140 
141   for (to_pixmap = FALSE; to_pixmap <= TRUE; to_pixmap++)
142     {
143       if (to_pixmap)
144 	{
145 	  GdkRectangle rect = { 0, 0, WIDTH, HEIGHT };
146 	  gdk_window_begin_paint_rect (drawing_area->window, &rect);
147 	}
148 
149       start_time = g_timer_elapsed (timer, NULL);
150       for (i = 0; i < NUM_ITERS; i++)
151 	{
152           cairo_t *cr;
153 
154 	  offset = (rand () % (WIDTH * HEIGHT * 4)) & -4;
155 	  pixbuf = gdk_pixbuf_new_from_data (buf + offset, GDK_COLORSPACE_RGB, TRUE,
156 					     8, WIDTH, HEIGHT, WIDTH * 4,
157 					     NULL, NULL);
158           cr = gdk_cairo_create (drawing_area->window);
159           gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
160           cairo_rectangle (cr, 0, 0, WIDTH, HEIGHT);
161           cairo_fill (cr);
162           cairo_destroy (cr);
163 	  g_object_unref (pixbuf);
164 	}
165       gdk_flush ();
166       total_time = g_timer_elapsed (timer, NULL) - start_time;
167 
168       if (to_pixmap)
169 	gdk_window_end_paint (drawing_area->window);
170 
171       g_print ("Alpha test%s time elapsed: %.2fs, %.1f fps, %.2f megapixels/s\n",
172 	       to_pixmap ? " (to pixmap)" : "",
173 	       total_time,
174 	       NUM_ITERS / total_time,
175 	       NUM_ITERS * (WIDTH * HEIGHT * 1e-6) / total_time);
176     }
177 
178   g_print ("Please submit these results to http://www.levien.com/gdkrgb/survey.html\n");
179 
180 #if 1
181   for (x = 0; x < WIDTH; x++)
182     {
183       int cindex;
184 
185       cindex = (x * 8) / WIDTH;
186       buf[x * 3] = cindex & 4 ? 0 : 255;
187       buf[x * 3 + 1] = cindex & 2 ? 0 : 255;
188       buf[x * 3 + 2] = cindex & 1 ? 0 : 255;
189     }
190   for (y = 1; y < (HEIGHT * 19) / 32; y++)
191     {
192       memcpy (buf + y * WIDTH * 3, buf, WIDTH * 3);
193     }
194   for (; y < (HEIGHT * 20) / 32; y++)
195     {
196       for (x = 0; x < WIDTH; x++)
197 	{
198 	  guchar gray;
199 
200 	  gray = (x * 255) / (WIDTH - 1);
201 	  buf[y * WIDTH * 3 + x * 3] = gray;
202 	  buf[y * WIDTH * 3 + x * 3 + 1] = 0;
203 	  buf[y * WIDTH * 3 + x * 3 + 2] = 0;
204 	}
205     }
206   for (; y < (HEIGHT * 21) / 32; y++)
207     {
208       for (x = 0; x < WIDTH; x++)
209 	{
210 	  guchar gray;
211 
212 	  gray = (x * 255) / (WIDTH - 1);
213 	  buf[y * WIDTH * 3 + x * 3] = 0;
214 	  buf[y * WIDTH * 3 + x * 3 + 1] = gray;
215 	  buf[y * WIDTH * 3 + x * 3 + 2] = 0;
216 	}
217     }
218   for (; y < (HEIGHT * 22) / 32; y++)
219     {
220       for (x = 0; x < WIDTH; x++)
221 	{
222 	  guchar gray;
223 
224 	  gray = (x * 255) / (WIDTH - 1);
225 	  buf[y * WIDTH * 3 + x * 3] = 0;
226 	  buf[y * WIDTH * 3 + x * 3 + 1] = 0;
227 	  buf[y * WIDTH * 3 + x * 3 + 2] = gray;
228 	}
229     }
230   for (; y < (HEIGHT * 24) / 32; y++)
231     {
232       for (x = 0; x < WIDTH; x++)
233 	{
234 	  guchar gray;
235 
236 	  gray = 112 + (x * 31) / (WIDTH - 1);
237 	  buf[y * WIDTH * 3 + x * 3] = gray;
238 	  buf[y * WIDTH * 3 + x * 3 + 1] = gray;
239 	  buf[y * WIDTH * 3 + x * 3 + 2] = gray;
240 	}
241     }
242   for (; y < (HEIGHT * 26) / 32; y++)
243     {
244       for (x = 0; x < WIDTH; x++)
245 	{
246 	  guchar gray;
247 
248 	  gray = (x * 255) / (WIDTH - 1);
249 	  buf[y * WIDTH * 3 + x * 3] = gray;
250 	  buf[y * WIDTH * 3 + x * 3 + 1] = gray;
251 	  buf[y * WIDTH * 3 + x * 3 + 2] = gray;
252 	}
253     }
254 
255   for (; y < HEIGHT; y++)
256     {
257       for (x = 0; x < WIDTH; x++)
258 	{
259 	  int cindex;
260 	  guchar gray;
261 
262 	  cindex = (x * 16) / WIDTH;
263 	  gray = cindex < 3 ? 0 :
264 	    cindex < 5 ? 255 :
265 	    cindex < 7 ? 128 :
266 	    0;
267 	  buf[y * WIDTH * 3 + x * 3] = gray;
268 	  buf[y * WIDTH * 3 + x * 3 + 1] = gray;
269 	  buf[y * WIDTH * 3 + x * 3 + 2] = gray;
270 	}
271     }
272   gdk_draw_rgb_image (drawing_area->window,
273 		      drawing_area->style->white_gc,
274 		      0, 0, WIDTH, HEIGHT, GDK_RGB_DITHER_MAX,
275 		      buf, WIDTH * 3);
276 #endif
277 }
278 
279 void
new_testrgb_window(void)280 new_testrgb_window (void)
281 {
282   GtkWidget *window;
283   GtkWidget *vbox;
284   GtkWidget *button;
285   GtkWidget *drawing_area;
286 
287   window = g_object_new (gtk_window_get_type (),
288 			   "GtkObject::user_data", NULL,
289 			   "GtkWindow::type", GTK_WINDOW_TOPLEVEL,
290 			   "GtkWindow::title", "testrgb",
291 			   "GtkWindow::allow_shrink", FALSE,
292 			   NULL);
293   g_signal_connect (window, "destroy",
294 		    G_CALLBACK (quit_func), NULL);
295 
296   vbox = gtk_vbox_new (FALSE, 0);
297 
298   drawing_area = gtk_drawing_area_new ();
299 
300   gtk_widget_set_size_request (drawing_area, WIDTH, HEIGHT);
301   gtk_box_pack_start (GTK_BOX (vbox), drawing_area, FALSE, FALSE, 0);
302   gtk_widget_show (drawing_area);
303 
304   button = gtk_button_new_with_label ("Quit");
305   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
306   g_signal_connect_swapped (button, "clicked",
307 			    G_CALLBACK (gtk_widget_destroy), window);
308 
309   gtk_widget_show (button);
310 
311   gtk_container_add (GTK_CONTAINER (window), vbox);
312   gtk_widget_show (vbox);
313 
314   gtk_widget_show (window);
315 
316   testrgb_rgb_test (drawing_area);
317 }
318 
319 int
main(int argc,char ** argv)320 main (int argc, char **argv)
321 {
322   gtk_init (&argc, &argv);
323 
324   gdk_rgb_set_verbose (TRUE);
325 
326   gtk_widget_set_default_colormap (gdk_rgb_get_colormap ());
327   new_testrgb_window ();
328 
329   gtk_main ();
330 
331   return 0;
332 }
333