testrgb.c 5.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* Note: these #includes differ slightly from the testrgb.c file included
   in the GdkRgb release. */

/* For gettimeofday */
#include <sys/time.h>
25
#include <stdlib.h>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
#include <unistd.h>

#include "gtk.h"

static void
quit_func (GtkWidget *widget, gpointer dummy)
{
  gtk_main_quit ();
}

#define WIDTH 320
#define HEIGHT 200

gdouble
get_time (void)
{
  struct timeval tv;
  struct timezone tz;

  gettimeofday (&tv, &tz);

  return tv.tv_sec + 1e-6 * tv.tv_usec;
}

#define NUM_ITERS 100

static void
testrgb_rgb_test (GtkWidget *drawing_area)
{
  guchar buf[WIDTH * HEIGHT * 6];
  gint i, j;
  gint offset;
  guchar val;
  gdouble start_time, total_time;
  gint x, y;
  gboolean dither;
  int dith_max;

  val = 0;
  for (j = 0; j < WIDTH * HEIGHT * 6; j++)
    {
      val = (val + ((val + (rand () & 0xff)) >> 1)) >> 1;
      buf[j] = val;
    }

  /* Let's warm up the cache, and also wait for the window manager
     to settle. */
  for (i = 0; i < NUM_ITERS; i++)
    {
      offset = (rand () % (WIDTH * HEIGHT * 3)) & -4;
      gdk_draw_rgb_image (drawing_area->window,
			  drawing_area->style->white_gc,
			  0, 0, WIDTH, HEIGHT,
			  GDK_RGB_DITHER_NONE,
			  buf + offset, WIDTH * 3);
    }

  if (gdk_rgb_ditherable ())
    dith_max = 2;
  else
    dith_max = 1;

  for (dither = 0; dither < dith_max; dither++)
    {
      start_time = get_time ();
      for (i = 0; i < NUM_ITERS; i++)
	{
	  offset = (rand () % (WIDTH * HEIGHT * 3)) & -4;
	  gdk_draw_rgb_image (drawing_area->window,
			      drawing_area->style->white_gc,
			      0, 0, WIDTH, HEIGHT,
			      dither ? GDK_RGB_DITHER_MAX :
			      GDK_RGB_DITHER_NONE,
			      buf + offset, WIDTH * 3);
	}
      total_time = get_time () - start_time;
      g_print ("Color test%s time elapsed: %.2fs, %.1f fps, %.2f megapixels/s\n",
	       dither ? " (dithered)" : "",
	       total_time,
	       NUM_ITERS / total_time,
	       NUM_ITERS * (WIDTH * HEIGHT * 1e-6) / total_time);
    }

  for (dither = 0; dither < dith_max; dither++)
    {
      start_time = get_time ();
      for (i = 0; i < NUM_ITERS; i++)
	{
	  offset = (rand () % (WIDTH * HEIGHT)) & -4;
	  gdk_draw_gray_image (drawing_area->window,
			       drawing_area->style->white_gc,
			       0, 0, WIDTH, HEIGHT,
			       dither ? GDK_RGB_DITHER_MAX :
			       GDK_RGB_DITHER_NONE,
			       buf + offset, WIDTH);
	}
      total_time = get_time () - start_time;
      g_print ("Grayscale test%s time elapsed: %.2fs, %.1f fps, %.2f megapixels/s\n",
	       dither ? " (dithered)" : "",
	       total_time,
	       NUM_ITERS / total_time,
	       NUM_ITERS * (WIDTH * HEIGHT * 1e-6) / total_time);
    }

#if 1
  for (x = 0; x < WIDTH; x++)
    {
      int cindex;

      cindex = (x * 8) / WIDTH;
      buf[x * 3] = cindex & 4 ? 0 : 255;
      buf[x * 3 + 1] = cindex & 2 ? 0 : 255;
      buf[x * 3 + 2] = cindex & 1 ? 0 : 255;
    }
  for (y = 1; y < (HEIGHT * 3) / 4; y++)
    {
      memcpy (buf + y * WIDTH * 3, buf, WIDTH * 3);
    }
  for (; y < (HEIGHT * 13) / 16; y++)
    {
      for (x = 0; x < WIDTH; x++)
	{
	  guchar gray;

	  gray = (x * 255) / (WIDTH - 1);
	  buf[y * WIDTH * 3 + x * 3] = gray;
	  buf[y * WIDTH * 3 + x * 3 + 1] = gray;
	  buf[y * WIDTH * 3 + x * 3 + 2] = gray;
	}
    }

  for (; y < HEIGHT; y++)
    {
      for (x = 0; x < WIDTH; x++)
	{
	  int cindex;
	  guchar gray;

	  cindex = (x * 16) / WIDTH;
	  gray = cindex < 3 ? 0 :
	    cindex < 5 ? 255 :
	    cindex < 7 ? 128 :
	    0;
	  buf[y * WIDTH * 3 + x * 3] = gray;
	  buf[y * WIDTH * 3 + x * 3 + 1] = gray;
	  buf[y * WIDTH * 3 + x * 3 + 2] = gray;
	}
    }
  gdk_draw_rgb_image (drawing_area->window,
		      drawing_area->style->white_gc,
		      0, 0, WIDTH, HEIGHT, GDK_RGB_DITHER_MAX,
		      buf, WIDTH * 3);
#endif
}

void
new_testrgb_window (void)
{
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *button;
  GtkWidget *drawing_area;

  window = gtk_widget_new (gtk_window_get_type (),
			   "GtkObject::user_data", NULL,
			   "GtkWindow::type", GTK_WINDOW_TOPLEVEL,
			   "GtkWindow::title", "testrgb",
			   "GtkWindow::allow_shrink", FALSE,
			   NULL);
  gtk_signal_connect (GTK_OBJECT (window), "destroy",
		      (GtkSignalFunc) quit_func, NULL);

  vbox = gtk_vbox_new (FALSE, 0);

  drawing_area = gtk_drawing_area_new ();

  gtk_widget_set_usize (drawing_area, WIDTH, HEIGHT);
  gtk_box_pack_start (GTK_BOX (vbox), drawing_area, FALSE, FALSE, 0);
  gtk_widget_show (drawing_area);

  button = gtk_button_new_with_label ("Quit");
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
  gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
			     (GtkSignalFunc) gtk_widget_destroy,
			     GTK_OBJECT (window));

  gtk_widget_show (button);

  gtk_container_add (GTK_CONTAINER (window), vbox);
  gtk_widget_show (vbox);

  gtk_widget_show (window);

  testrgb_rgb_test (drawing_area);
}

int
main (int argc, char **argv)
{
  gtk_init (&argc, &argv);

  gdk_rgb_set_verbose (TRUE);

  gdk_rgb_init ();

  gtk_widget_set_default_colormap (gdk_rgb_get_cmap ());
  gtk_widget_set_default_visual (gdk_rgb_get_visual ());
  new_testrgb_window ();

  gtk_main ();

  return 0;
}