[gimp] app: Add basic GimpTileBackendTileManager unit test



commit cb242a484e296d095c7728088334813dab6ef2ac
Author: Martin Nordholts <martinn src gnome org>
Date:   Mon Sep 19 06:33:49 2011 +0200

    app: Add basic GimpTileBackendTileManager unit test
    
    Add a super basic GimpTileBackendTileManager unit test and implement
    GimpTileBackendTileManager enough to make it pass. Still a lot of work
    left to do, this was just to get things up and running.

 app/gegl/gimptilebackendtilemanager.c       |  122 +++++++++------------------
 app/gegl/gimptilebackendtilemanager.h       |    2 +-
 app/tests/.gitignore                        |    1 +
 app/tests/Makefile.am                       |    1 +
 app/tests/test-gimptilebackendtilemanager.c |  103 ++++++++++++++++++++++
 5 files changed, 147 insertions(+), 82 deletions(-)
---
diff --git a/app/gegl/gimptilebackendtilemanager.c b/app/gegl/gimptilebackendtilemanager.c
index 56d9518..46dab38 100644
--- a/app/gegl/gimptilebackendtilemanager.c
+++ b/app/gegl/gimptilebackendtilemanager.c
@@ -24,19 +24,23 @@
 
 #include <gegl.h>
 #include <gegl-buffer.h>
+#include <gegl-tile.h>
 
 #include "libgimpmath/gimpmath.h"
 
 #include "gimp-gegl-types.h"
 
 #include "base/tile.h"
+#include "base/tile-manager.h"
 
 #include "gimptilebackendtilemanager.h"
+#include "gimp-gegl-utils.h"
 
 
 struct _GimpTileBackendTileManagerPrivate 
 {
   GHashTable      *entries;
+  TileManager     *tile_manager;
 };
 
 
@@ -76,13 +80,6 @@ static RamEntry * lookup_entry      (GimpTileBackendTileManager *self,
 static guint      hash_func         (gconstpointer               key);
 static gboolean   equal_func        (gconstpointer               a,
                                      gconstpointer               b);
-static void       ram_entry_read    (GimpTileBackendTileManager *ram,
-                                     gint                        x,
-                                     gint                        y,
-                                     gint                        z,
-                                     gfloat                      w,
-                                     gfloat                      h,
-                                     guchar                     *dest);
 static void       ram_entry_write   (GimpTileBackendTileManager *ram,
                                      RamEntry                   *entry,
                                      guchar                     *source);
@@ -179,12 +176,29 @@ gimp_tile_backend_tile_manager_command (GeglTileSource  *tile_store,
     case GEGL_TILE_GET:
       {
         GeglTile *tile;
-        gint      tile_size = gegl_tile_backend_get_tile_size (backend);
+        gint      tile_size;
+        Tile     *gimp_tile;
+        gint      tile_stride;
+        gint      gimp_tile_stride;
+        int       row;
 
-        tile = gegl_tile_new (tile_size);
+        gimp_tile = tile_manager_get_at (backend_tm->priv->tile_manager,
+                                         x, y, TRUE, FALSE);
+
+        g_return_val_if_fail (gimp_tile != NULL, NULL);
 
-        ram_entry_read (backend_tm, x, y, z, TILE_WIDTH, TILE_HEIGHT,
-                        gegl_tile_get_data (tile));
+        tile_size        = gegl_tile_backend_get_tile_size (backend);
+        tile_stride      = TILE_WIDTH * tile_bpp (gimp_tile);
+        gimp_tile_stride = tile_ewidth (gimp_tile) * tile_bpp (gimp_tile);
+
+        /* XXX: Point to Tile data directly instead of using memcpy */
+        tile = gegl_tile_new (tile_size);
+        for (row = 0; row < tile_eheight (gimp_tile); row++)
+          {
+            memcpy (gegl_tile_get_data (tile) + row * tile_stride,
+                    tile_data_pointer (gimp_tile, 0, row),
+                    gimp_tile_stride);
+          }
 
         return tile;
       }
@@ -324,66 +338,6 @@ equal_func (gconstpointer a,
   return FALSE;
 }
 
-static gfloat
-mandel_calc (gfloat x,
-             gfloat y)
-{
-  gfloat fCReal = x;
-  gfloat fCImg  = y;
-  gfloat fZReal = fCReal;
-  gfloat fZImg  = fCImg;
-#define MAX_ITER 100
-
-  gint n;
-
-  for (n=0; n< MAX_ITER; n++)
-    {
-      gfloat fZRealSquared = fZReal * fZReal;
-      gfloat fZImgSquared = fZImg * fZImg;
-
-      if (fZRealSquared + fZImgSquared > 4)
-        return 1.0*n/(MAX_ITER);
-
-/*              -- z = z^2 + c*/
-      fZImg = 2 * fZReal * fZImg + fCImg;
-      fZReal = fZRealSquared - fZImgSquared + fCReal;
-    }
-  return 1.0;
-}
-
-static void
-ram_entry_read (GimpTileBackendTileManager *ram,
-                gint                        x,
-                gint                        y,
-                gint                        z,
-                gfloat                      w,
-                gfloat                      h,
-                guchar                     *dest)
-{
-  GeglTileBackend *backend = GEGL_TILE_BACKEND (ram);
-  Babl            *format  = gegl_tile_backend_get_format (backend);
-  gint             u, v;
-  gint             i = 0;
-
-  g_printerr ("%s\n", babl_get_name (format));
-  g_printerr ("READ %i %i %i\n", x, y, z);
-
-  for (v = 0; v < h; v++)
-    for (u = 0; u < w; u++)
-      {
-        float a   = ((u + x * w) * powf (2,z) - 2 * w);
-        float b   = ((v + y * h) * powf (2,z) - 1 * h);
-        float val =
-          mandel_calc (a / w,
-                       b / h);
-        dest[i*4+0] = val * 255;
-        dest[i*4+1] = val * 255;
-        dest[i*4+2] = val * 255;
-        dest[i*4+3] = 255;
-        i++;
-      }
-}
-
 static void
 ram_entry_write (GimpTileBackendTileManager *ram,
                  RamEntry                   *entry,
@@ -418,19 +372,25 @@ ram_entry_destroy (RamEntry           *entry,
 }
 
 GeglTileBackend *
-gimp_tile_backend_tile_manager_new (void)
+gimp_tile_backend_tile_manager_new (TileManager *tm)
 {
-  GimpTileBackendTileManager *ret =
-    g_object_new (GIMP_TYPE_TILE_BACKEND_TILE_MANAGER,
-                  "tile-width",  TILE_WIDTH,
-                  "tile-height", TILE_HEIGHT,
-                  "format", babl_format ("RGBA u8"),
-                  NULL);
-  GeglRectangle rect = { 100,100, 200, 200 };
+  GeglTileBackend *ret;
+  gint             width  = tile_manager_width (tm);
+  gint             height = tile_manager_height (tm);
+  gint             bpp    = tile_manager_bpp (tm);
+  GeglRectangle    rect   = { 0, 0, width, height };
+
+  ret = g_object_new (GIMP_TYPE_TILE_BACKEND_TILE_MANAGER,
+                      "tile-width",  TILE_WIDTH,
+                      "tile-height", TILE_HEIGHT,
+                      "format",      gimp_bpp_to_babl_format (bpp, FALSE),
+                      NULL);
+
+  GIMP_TILE_BACKEND_TILE_MANAGER (ret)->priv->tile_manager = tile_manager_ref (tm);
 
-  gegl_tile_backend_set_extent (GEGL_TILE_BACKEND (ret), &rect);
+  gegl_tile_backend_set_extent (ret, &rect);
 
-  return GEGL_TILE_BACKEND (ret);
+  return ret;
 }
 
 void
diff --git a/app/gegl/gimptilebackendtilemanager.h b/app/gegl/gimptilebackendtilemanager.h
index ba54636..85fb7e4 100644
--- a/app/gegl/gimptilebackendtilemanager.h
+++ b/app/gegl/gimptilebackendtilemanager.h
@@ -49,7 +49,7 @@ struct _GimpTileBackendTileManagerClass
 
 GType             gimp_tile_backend_tile_manager_get_type (void) G_GNUC_CONST;
 
-GeglTileBackend * gimp_tile_backend_tile_manager_new      (void);
+GeglTileBackend * gimp_tile_backend_tile_manager_new      (TileManager *tm);
 
 void              gimp_tile_backend_tile_manager_stats    (void);
 
diff --git a/app/tests/.gitignore b/app/tests/.gitignore
index b315e4a..d62fad0 100644
--- a/app/tests/.gitignore
+++ b/app/tests/.gitignore
@@ -6,6 +6,7 @@ Makefile.in
 libgimpapptestutils.a
 test-core*
 test-gimpidtable*
+test-gimptilebackendtilemanager*
 test-layer-grouping*
 test-save-and-export*
 test-session-2-6-compatibility*
diff --git a/app/tests/Makefile.am b/app/tests/Makefile.am
index 3a3194a..b9e1937 100644
--- a/app/tests/Makefile.am
+++ b/app/tests/Makefile.am
@@ -21,6 +21,7 @@ endif
 TESTS = \
 	test-core					\
 	test-gimpidtable				\
+	test-gimptilebackendtilemanager			\
 	test-save-and-export				\
 	test-session-2-6-compatibility			\
 	test-session-2-8-compatibility-multi-window	\
diff --git a/app/tests/test-gimptilebackendtilemanager.c b/app/tests/test-gimptilebackendtilemanager.c
new file mode 100644
index 0000000..7206759
--- /dev/null
+++ b/app/tests/test-gimptilebackendtilemanager.c
@@ -0,0 +1,103 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * test-gimptilebackendtilemanager.c
+ * Copyright (C) 2011 Martin Nordholts <martinn src gnome org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gegl.h>
+#include <gtk/gtk.h>
+#include <string.h>
+
+#include "widgets/widgets-types.h"
+
+#include "base/tile-manager.h"
+#include "base/pixel-region.h"
+#include "base/tile-cache.h"
+
+#include "gegl/gimptilebackendtilemanager.h"
+
+#include "paint-funcs/paint-funcs.h"
+
+#include "tests.h"
+#include "gimp-app-test-utils.h"
+
+
+#define ADD_TEST(function) \
+  g_test_add ("/gimptilebackendtilemanager/" #function, \
+              GimpTestFixture, \
+              NULL, \
+              NULL, \
+              function, \
+              NULL);
+
+
+typedef struct
+{
+  gint avoid_sizeof_zero;
+} GimpTestFixture;
+
+
+/**
+ * basic_usage:
+ * @fixture:
+ * @data:
+ *
+ * Test basic usage.
+ **/
+static void
+basic_usage (GimpTestFixture *fixture,
+             gconstpointer    data)
+{
+  GeglRectangle rect                = { 0, 0, 10, 10 };
+  GeglRectangle pixel_rect          = { 5, 5, 1, 1 };
+  guchar        opaque_magenta8[4]  = { 0xff, 0, 0xff, 0xff };
+  guint16       opaque_magenta16[4] = { 0xffff, 0, 0xffff, 0xffff };
+
+  PixelRegion      pr;
+  TileManager     *tm;
+  GeglTileBackend *backend;
+  GeglBuffer      *buffer;
+  guint16          actual_data[4];
+
+  /* Write some pixels to the tile manager */
+  tm = tile_manager_new (rect.width, rect.height, 4);
+  pixel_region_init (&pr, tm, rect.x, rect.y, rect.width, rect.height, TRUE);
+  color_region (&pr, opaque_magenta8);
+
+  /* Make sure we can read them through the GeglBuffer using the
+   * TileManager backend. Use u16 to complicate code paths, decreasing
+   * risk of the test accidentally passing
+   */
+  backend = gimp_tile_backend_tile_manager_new (tm);
+  buffer  = gegl_buffer_new_for_backend (NULL, backend);
+  gegl_buffer_get (buffer, 1.0 /*scale*/, &pixel_rect, babl_format ("RGBA u16"), actual_data, GEGL_AUTO_ROWSTRIDE);
+  g_assert_cmpint (0, ==, memcmp (opaque_magenta16, actual_data, sizeof (actual_data)));
+}
+
+int
+main (int    argc,
+      char **argv)
+{
+  g_type_init ();
+  tile_cache_init (G_MAXUINT32);
+  gegl_init (&argc, &argv);
+  g_test_init (&argc, &argv, NULL);
+
+  ADD_TEST (basic_usage);
+
+  return g_test_run ();
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]