home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 February / CMCD0205.ISO / Linux / gimp-2.2.0.tar.gz / gimp-2.2.0.tar / gimp-2.2.0 / libgimpwidgets / test-preview-area.c < prev   
C/C++ Source or Header  |  2004-09-08  |  7KB  |  240 lines

  1. /* LIBGIMP - The GIMP Library
  2.  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
  3.  *
  4.  * Copyright (C) 2004  Sven Neumann <sven@gimp.org>
  5.  * test-preview-area.c
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the
  19.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20.  * Boston, MA 02111-1307, USA.
  21.  */
  22.  
  23. /* This code is based on testrgb.c from GTK+ - The GIMP Toolkit
  24.  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  25.  */
  26.  
  27. #include <config.h>
  28.  
  29. #include <stdlib.h>
  30.  
  31. #include <gtk/gtk.h>
  32.  
  33. #include "gimpwidgetstypes.h"
  34.  
  35. #include "gimppreviewarea.h"
  36.  
  37.  
  38. #define WIDTH      256
  39. #define HEIGHT     256
  40. #define NUM_ITERS  100
  41.  
  42.  
  43. static void
  44. test_run (GtkWidget *area,
  45.           gboolean   visible)
  46. {
  47.   guchar       *buf;
  48.   gint          i, j;
  49.   gint          offset, offset2, offset3;
  50.   gint          num_iters = NUM_ITERS;
  51.   guchar        val;
  52.   gdouble       start_time, total_time;
  53.   GTimer       *timer;
  54.   GEnumClass   *enum_class;
  55.   GEnumValue   *enum_value;
  56.  
  57.   if (! visible)
  58.     num_iters *= 4;
  59.  
  60.   gtk_widget_realize (area);
  61.  
  62.   g_print ("\nPerformance tests for GimpPreviewArea (%s, %d iterations):\n\n",
  63.            visible ? "visible" : "hidden", num_iters);
  64.  
  65.   buf = g_malloc (WIDTH * HEIGHT * 8);
  66.  
  67.   val = 0;
  68.   for (j = 0; j < WIDTH * HEIGHT * 8; j++)
  69.     {
  70.       val = (val + ((val + (rand () & 0xff)) >> 1)) >> 1;
  71.       buf[j] = val;
  72.     }
  73.  
  74.   gimp_preview_area_set_cmap (GIMP_PREVIEW_AREA (area), buf, 256);
  75.  
  76.   /* Let's warm up the cache, and also wait for the window manager
  77.      to settle. */
  78.   for (i = 0; i < NUM_ITERS; i++)
  79.     {
  80.       offset = (rand () % (WIDTH * HEIGHT * 4)) & -4;
  81.       gimp_preview_area_draw (GIMP_PREVIEW_AREA (area),
  82.                               0, 0, WIDTH, HEIGHT,
  83.                               GIMP_RGB_IMAGE,
  84.                               buf + offset,
  85.                               WIDTH * 4);
  86.     }
  87.  
  88.   gdk_window_process_all_updates ();
  89.   gdk_flush ();
  90.  
  91.   timer = g_timer_new ();
  92.  
  93.   enum_class = g_type_class_ref (GIMP_TYPE_IMAGE_TYPE);
  94.  
  95.   for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
  96.     {
  97.       /* gimp_preview_area_draw */
  98.       start_time = g_timer_elapsed (timer, NULL);
  99.  
  100.       for (i = 0; i < num_iters; i++)
  101.         {
  102.           offset = (rand () % (WIDTH * HEIGHT * 4)) & -4;
  103.           gimp_preview_area_draw (GIMP_PREVIEW_AREA (area),
  104.                                   0, 0, WIDTH, HEIGHT,
  105.                                   enum_value->value,
  106.                                   buf + offset,
  107.                                   WIDTH * 4);
  108.  
  109.           gdk_window_process_updates (area->window, FALSE);
  110.         }
  111.  
  112.       gdk_flush ();
  113.       total_time = g_timer_elapsed (timer, NULL) - start_time;
  114.  
  115.       g_print ("%-16s "
  116.                "draw  :  %5.2fs, %8.1f fps, %8.2f megapixels/s\n",
  117.                enum_value->value_name,
  118.                total_time,
  119.                num_iters / total_time,
  120.                num_iters * (WIDTH * HEIGHT * 1e-6) / total_time);
  121.  
  122.       /* gimp_preview_area_blend */
  123.       start_time = g_timer_elapsed (timer, NULL);
  124.  
  125.       for (i = 0; i < num_iters; i++)
  126.         {
  127.           offset  = (rand () % (WIDTH * HEIGHT * 4)) & -4;
  128.           offset2 = (rand () % (WIDTH * HEIGHT * 4)) & -4;
  129.           gimp_preview_area_blend (GIMP_PREVIEW_AREA (area),
  130.                                    0, 0, WIDTH, HEIGHT,
  131.                                    enum_value->value,
  132.                                    buf + offset,
  133.                                    WIDTH * 4,
  134.                                    buf + offset2,
  135.                                    WIDTH * 4,
  136.                                    rand () & 0xFF);
  137.  
  138.           gdk_window_process_updates (area->window, FALSE);
  139.         }
  140.  
  141.       gdk_flush ();
  142.       total_time = g_timer_elapsed (timer, NULL) - start_time;
  143.  
  144.       g_print ("%-16s "
  145.                "blend :  %5.2fs, %8.1f fps, %8.2f megapixels/s\n",
  146.                enum_value->value_name,
  147.                total_time,
  148.                num_iters / total_time,
  149.                num_iters * (WIDTH * HEIGHT * 1e-6) / total_time);
  150.  
  151.       /* gimp_preview_area_mask */
  152.       start_time = g_timer_elapsed (timer, NULL);
  153.  
  154.       for (i = 0; i < num_iters; i++)
  155.         {
  156.           offset  = (rand () % (WIDTH * HEIGHT * 4)) & -4;
  157.           offset2 = (rand () % (WIDTH * HEIGHT * 4)) & -4;
  158.           offset3 = (rand () % (WIDTH * HEIGHT * 4)) & -4;
  159.           gimp_preview_area_mask (GIMP_PREVIEW_AREA (area),
  160.                                   0, 0, WIDTH, HEIGHT,
  161.                                   enum_value->value,
  162.                                   buf + offset,
  163.                                   WIDTH * 4,
  164.                                   buf + offset2,
  165.                                   WIDTH * 4,
  166.                                   buf + offset3,
  167.                                   WIDTH);
  168.  
  169.           gdk_window_process_updates (area->window, FALSE);
  170.         }
  171.  
  172.       gdk_flush ();
  173.       total_time = g_timer_elapsed (timer, NULL) - start_time;
  174.  
  175.       g_print ("%-16s "
  176.                "mask  :  %5.2fs, %8.1f fps, %8.2f megapixels/s\n",
  177.                enum_value->value_name,
  178.                total_time,
  179.                num_iters / total_time,
  180.                num_iters * (WIDTH * HEIGHT * 1e-6) / total_time);
  181.       g_print ("\n");
  182.     }
  183.  
  184.   start_time = g_timer_elapsed (timer, NULL);
  185.  
  186.   for (i = 0; i < num_iters; i++)
  187.     {
  188.       guchar  r = rand () % 0xFF;
  189.       guchar  g = rand () % 0xFF;
  190.       guchar  b = rand () % 0xFF;
  191.  
  192.       gimp_preview_area_fill (GIMP_PREVIEW_AREA (area),
  193.                               0, 0, WIDTH, HEIGHT,
  194.                               r, g, b);
  195.  
  196.       gdk_window_process_updates (area->window, FALSE);
  197.     }
  198.  
  199.   gdk_flush ();
  200.   total_time = g_timer_elapsed (timer, NULL) - start_time;
  201.   g_print ("%-16s "
  202.            "fill  :  %5.2fs, %8.1f fps, %8.2f megapixels/s\n",
  203.            "Color fill",
  204.            total_time,
  205.            num_iters / total_time,
  206.            num_iters * (WIDTH * HEIGHT * 1e-6) / total_time);
  207.   g_print ("\n");
  208.  
  209.   g_free (buf);
  210. }
  211.  
  212. static void
  213. test_preview_area (void)
  214. {
  215.   GtkWidget *window;
  216.   GtkWidget *area;
  217.  
  218.   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  219.  
  220.   area = gimp_preview_area_new ();
  221.   gtk_container_add (GTK_CONTAINER (window), area);
  222.   gtk_widget_show (area);
  223.  
  224.   test_run (area, FALSE);
  225.  
  226.   gtk_widget_show (window);
  227.  
  228.   test_run (area, TRUE);
  229. }
  230.  
  231. int
  232. main (int argc, char **argv)
  233. {
  234.   gtk_init (&argc, &argv);
  235.  
  236.   test_preview_area ();
  237.  
  238.   return EXIT_SUCCESS;
  239. }
  240.