[gegl/gsoc2011-opencl: 12/12] OpenCL Unit-Tests: GeglBuffer



commit 26c07e45def1c7db8a0404af4f34f08871277b53
Author: Victor Oliveira <victormatheus gmail com>
Date:   Tue Jul 26 10:22:06 2011 -0300

    OpenCL Unit-Tests: GeglBuffer

 tests/opencl/Makefile.am                           |    7 +-
 tests/opencl/test-gegl-buffer-blank.c              |   55 +++++
 .../test-gegl-buffer-cl-brightness-contrast.c      |  233 ++++++++++++++++++++
 tests/opencl/test-gegl-buffer-cl-read-write.c      |  176 +++++++++++++++
 tests/opencl/test-gegl-buffer-cl-read.c            |  129 +++++++++++
 tests/opencl/test-gegl-buffer-cl-write.c           |   93 ++++++++
 6 files changed, 692 insertions(+), 1 deletions(-)
---
diff --git a/tests/opencl/Makefile.am b/tests/opencl/Makefile.am
index 48432a6..63fc089 100644
--- a/tests/opencl/Makefile.am
+++ b/tests/opencl/Makefile.am
@@ -13,7 +13,12 @@ noinst_PROGRAMS = \
 	test-gegl-tile-lock-mode-write-then-read \
 	test-gegl-tile-lock-mode-cl-write-then-read \
 	test-gegl-tile-lock-mode-write-then-cl-read \
-	test-gegl-tile-lock-mode-cl-write-then-cl-read
+	test-gegl-tile-lock-mode-cl-write-then-cl-read \
+	test-gegl-buffer-blank \
+	test-gegl-buffer-cl-write \
+	test-gegl-buffer-cl-read \
+	test-gegl-buffer-cl-read-write \
+	test-gegl-buffer-cl-brightness-contrast
 
 TESTS = $(noinst_PROGRAMS)
 
diff --git a/tests/opencl/test-gegl-buffer-blank.c b/tests/opencl/test-gegl-buffer-blank.c
new file mode 100644
index 0000000..b34b741
--- /dev/null
+++ b/tests/opencl/test-gegl-buffer-blank.c
@@ -0,0 +1,55 @@
+/* This file is part of GEGL.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2011 Victor M. de Araujo Oliveira <victormatheus gmail com>
+ */
+
+#include <string.h>
+#include <babl/babl.h>
+
+#include "gegl.h"
+#include "gegl-types.h"
+#include "gegl-utils.h"
+#include "gegl-cl-init.h"
+#include "gegl-cl-texture.h"
+
+#define SUCCESS 0
+#define FAILURE (-1)
+
+gint
+main (gint    argc,
+      gchar **argv)
+{
+  gint retval = SUCCESS;
+
+  GeglBuffer    *buffer;
+  GeglBufferIterator *i;
+  GeglRectangle  rect = {0, 0, 20, 20};
+
+  gegl_init (&argc, &argv);
+  gegl_cl_init (NULL);
+
+  buffer = gegl_buffer_new (&rect, babl_format ("RGBA float"));
+
+  i = gegl_buffer_iterator_new (buffer, &rect, NULL, GEGL_BUFFER_CL_WRITE);
+  while (gegl_buffer_iterator_next (i))
+    gegl_cl_texture_clear(i->cl_data[0], NULL);
+
+  gegl_buffer_destroy (buffer);
+
+  gegl_exit ();
+
+  return retval;
+}
diff --git a/tests/opencl/test-gegl-buffer-cl-brightness-contrast.c b/tests/opencl/test-gegl-buffer-cl-brightness-contrast.c
new file mode 100644
index 0000000..66c6a9f
--- /dev/null
+++ b/tests/opencl/test-gegl-buffer-cl-brightness-contrast.c
@@ -0,0 +1,233 @@
+/* This file is part of GEGL.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2011 Victor M. de Araujo Oliveira <victormatheus gmail com>
+ */
+
+#include <string.h>
+#include <assert.h>
+#include <babl/babl.h>
+#include <sys/time.h>
+
+#include "gegl.h"
+#include "gegl-types.h"
+#include "gegl-utils.h"
+#include "gegl-cl-init.h"
+#include "gegl-cl-texture.h"
+
+#define SUCCESS 0
+#define FAILURE (-1)
+
+gint
+main (gint    argc,
+      gchar **argv)
+{
+
+  gint USE_OPENCL = (argc>1)? atoi(argv[1]) : 1;
+
+  gint retval = SUCCESS;
+
+  GeglBuffer *buffer_read, *buffer_write;
+  /*GeglBuffer *buffer_truth;*/
+
+  gfloat *buf_write;
+  /*gfloat *buf_truth;*/
+
+  gfloat brightness = 2.0f;
+  gfloat contrast   = 0.5f;
+
+  int errcode;
+  cl_program program;
+  cl_kernel kernel;
+
+  gegl_init (&argc, &argv);
+  gegl_cl_init (NULL);
+
+  /* Load Image */
+  {
+    GeglNode *gegl, *sink;
+
+    /*
+    gegl = gegl_graph (sink = gegl_node ("gegl:buffer-sink", "buffer", &buffer_truth,
+                                                             "format",  babl_format ("RGBA float"), NULL,
+                              gegl_node ("gegl:brightness-contrast", "brightness", brightness,
+                                                                     "contrast", contrast, NULL,
+                              gegl_node ("gegl:load", "path", "GEGL.png", NULL))));
+    gegl_node_process (sink);
+    g_object_unref (gegl);
+
+    buf_truth = g_new (gfloat, gegl_buffer_get_pixel_count (buffer_truth) * 4);
+    gegl_buffer_get (buffer_truth, 1.0, NULL, NULL, buf_truth, GEGL_AUTO_ROWSTRIDE);
+    */
+
+    /* read and write buffers */
+    gegl = gegl_graph (sink = gegl_node ("gegl:buffer-sink", "buffer", &buffer_read,
+                                                             "format",  babl_format ("RGBA float"), NULL,
+                              gegl_node ("gegl:load", "path", "GEGL_big.png", NULL)));
+    gegl_node_process (sink);
+    g_object_unref (gegl);
+
+    buffer_write = gegl_buffer_new (gegl_buffer_get_extent (buffer_read), babl_format ("RGBA float"));
+    buf_write = g_new (gfloat, gegl_buffer_get_pixel_count (buffer_write) * 4);
+  }
+
+  /* define and compile OpenCL Kernel */
+  {
+    char buffer[10000];
+    const char* kernel_source[] =
+    {
+    "sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE |              \n",
+    "                    CLK_ADDRESS_NONE            |              \n",
+    "                    CLK_FILTER_NEAREST;                        \n",
+    "__kernel void kernel_bc(__read_only  image2d_t in,             \n",
+    "                        __write_only image2d_t out,            \n",
+    "                         float brightness,                     \n",
+    "                         float contrast)                       \n",
+    "{                                                              \n",
+    "  int2 gid = (int2)(get_global_id(0), get_global_id(1));       \n",
+    "  float4 in_v  = read_imagef(in, sampler, gid);                \n",
+    "  float4 out_v;                                                \n",
+    "  out_v.xyz = (in_v.xyz - 0.5f) * contrast + brightness + 0.5f;\n",
+    "  out_v.w   =  in_v.w;                                         \n",
+    "  write_imagef(out, gid, out_v);                               \n",
+    "}                                                              \n",
+    NULL,
+    };
+
+    CL_SAFE_CALL( program = gegl_clCreateProgramWithSource(gegl_cl_get_context(),
+                                                           gegl_cl_count_lines(kernel_source),
+                                                           (const char **)&kernel_source,
+                                                           NULL, &errcode) );
+
+    errcode = gegl_clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
+    if (errcode != CL_SUCCESS)
+      {
+        CL_SAFE_CALL( errcode = gegl_clGetProgramBuildInfo(program,
+                                                           gegl_cl_get_device(),
+                                                           CL_PROGRAM_BUILD_LOG,
+                                                           sizeof(buffer), buffer, NULL) );
+        g_warning("OpenCL Build Error in Line %u in file %s\nError:%s\n%s",
+                  __LINE__, __FILE__, gegl_cl_errstring(errcode), buffer);
+      }
+
+    CL_SAFE_CALL( kernel = gegl_clCreateKernel(program, "kernel_bc", &errcode) );
+  }
+
+  /* process */
+  {
+    GeglBufferIterator *i;
+    gint index;
+    struct timeval start;
+    struct timeval end;
+    long mtime, seconds, useconds;
+
+    gettimeofday(&start, NULL);
+
+    if (USE_OPENCL)
+      {
+        i = gegl_buffer_iterator_new (buffer_write, NULL, NULL, GEGL_BUFFER_CL_WRITE);
+        index = gegl_buffer_iterator_add (i, buffer_read, NULL, NULL, GEGL_BUFFER_CL_READ);
+        while (gegl_buffer_iterator_next (i))
+          {
+            GeglClTexture *in_tex  = i->cl_data[index];
+            GeglClTexture *out_tex = i->cl_data[0];
+            size_t global_worksize[2] = {i->roi[0].width, i->roi[0].height};
+
+            CL_SAFE_CALL( errcode = gegl_clSetKernelArg(kernel, 0, sizeof(cl_mem),   (void*)&in_tex->data) );
+            CL_SAFE_CALL( errcode = gegl_clSetKernelArg(kernel, 1, sizeof(cl_mem),   (void*)&out_tex->data) );
+            CL_SAFE_CALL( errcode = gegl_clSetKernelArg(kernel, 2, sizeof(cl_float), (void*)&brightness) );
+            CL_SAFE_CALL( errcode = gegl_clSetKernelArg(kernel, 3, sizeof(cl_float), (void*)&contrast) );
+
+            CL_SAFE_CALL( errcode = gegl_clEnqueueNDRangeKernel(gegl_cl_get_command_queue(), kernel, 2,
+                                                          NULL, global_worksize, NULL,
+                                                          0, NULL, NULL) );
+            CL_SAFE_CALL( errcode = gegl_clFinish(gegl_cl_get_command_queue()) );
+          }
+      }
+    else
+      {
+        i = gegl_buffer_iterator_new (buffer_write, NULL, NULL, GEGL_BUFFER_WRITE);
+        index = gegl_buffer_iterator_add (i, buffer_read, NULL, NULL, GEGL_BUFFER_READ);
+        while (gegl_buffer_iterator_next (i))
+          {
+            gfloat *in  = (gfloat*)i->data[index];
+            gfloat *out = (gfloat*)i->data[0];
+            int j;
+            int k=0;
+            for (j=0; j<i->length; j++)
+              {
+                out[k+0] = (in[k+0] - 0.5f) * contrast + brightness + 0.5f;
+                out[k+1] = (in[k+1] - 0.5f) * contrast + brightness + 0.5f;
+                out[k+2] = (in[k+2] - 0.5f) * contrast + brightness + 0.5f;
+                out[k+3] = in[k+3];
+                k+=4;
+              }
+          }
+      }
+
+      gettimeofday(&end, NULL);
+
+      seconds  = end.tv_sec  - start.tv_sec;
+      useconds = end.tv_usec - start.tv_usec;
+      mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
+
+      g_warning("Use OpenCL: %d\tElapsed time: %ld milliseconds\n", USE_OPENCL, mtime);
+
+    gegl_buffer_get (buffer_write, 1.0, NULL, NULL, buf_write, GEGL_AUTO_ROWSTRIDE);
+  }
+
+  goto ok;
+
+  /* verification */
+  /*
+  {
+    gint i;
+
+    assert(gegl_buffer_get_pixel_count (buffer_truth) == gegl_buffer_get_pixel_count (buffer_write));
+
+    for (i=0; i<gegl_buffer_get_pixel_count (buffer_write)*4; i++)
+      if (!GEGL_FLOAT_EQUAL (buf_truth[i], buf_write[i]))
+        {
+          g_printerr ("The test failed. "
+            "Aborting.\n");
+
+          retval = FAILURE;
+          goto abort;
+        }
+
+    goto ok;
+  }
+  */
+abort:
+    /* Save Image */
+    {
+      GeglNode *gegl, *sink;
+      gegl = gegl_graph (sink = gegl_node ("gegl:png-save", "path", "error.png", NULL,
+                                gegl_node ("gegl:buffer-source", "buffer", buffer_write, NULL)));
+      gegl_node_process (sink);
+      g_object_unref (gegl);
+    }
+
+ok:
+  /*g_free (buf_truth);*/
+  g_free (buf_write);
+  gegl_buffer_destroy (buffer_read);
+  gegl_buffer_destroy (buffer_write);
+  /*gegl_buffer_destroy (buffer_truth);*/
+
+  gegl_exit ();
+
+  return retval;
+}
diff --git a/tests/opencl/test-gegl-buffer-cl-read-write.c b/tests/opencl/test-gegl-buffer-cl-read-write.c
new file mode 100644
index 0000000..979d4a2
--- /dev/null
+++ b/tests/opencl/test-gegl-buffer-cl-read-write.c
@@ -0,0 +1,176 @@
+/* This file is part of GEGL.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2011 Victor M. de Araujo Oliveira <victormatheus gmail com>
+ */
+
+#include <string.h>
+#include <assert.h>
+#include <babl/babl.h>
+
+#include "gegl.h"
+#include "gegl-types.h"
+#include "gegl-utils.h"
+#include "gegl-cl-init.h"
+#include "gegl-cl-texture.h"
+
+#define SUCCESS 0
+#define FAILURE (-1)
+
+gint
+main (gint    argc,
+      gchar **argv)
+{
+  gint retval = SUCCESS;
+
+  GeglBuffer *buffer_read, *buffer_write;
+  gfloat *buf_write;
+
+  int errcode;
+  cl_program program;
+  cl_kernel kernel;
+
+  gegl_init (&argc, &argv);
+  gegl_cl_init (NULL);
+
+  /* Load Image */
+  {
+    GeglNode *gegl, *sink;
+    gegl = gegl_graph (sink = gegl_node ("gegl:buffer-sink", "buffer", &buffer_read,
+                                                             "format",  babl_format ("RGBA float"), NULL,
+                              gegl_node ("gegl:load", "path", "GEGL.png", NULL)));
+
+    gegl_node_process (sink);
+
+    buffer_write = gegl_buffer_new (gegl_buffer_get_extent (buffer_read), babl_format ("RGBA float"));
+
+    buf_write = g_new (gfloat, gegl_buffer_get_pixel_count (buffer_write) * 4);
+    g_object_unref (gegl);
+  }
+
+  /* define and compile OpenCL Kernel */
+  {
+    char buffer[10000];
+    const char* kernel_source[] =
+    {
+    "sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE |              \n",
+    "                    CLK_ADDRESS_NONE            |              \n",
+    "                    CLK_FILTER_NEAREST;                        \n",
+    "__kernel void kernel_m (__read_only  image2d_t in,             \n",
+    "                        __write_only image2d_t out)            \n",
+    "{                                                              \n",
+    "  int2 gid = (int2)(get_global_id(0), get_global_id(1));       \n",
+    "  float4 in_v  = read_imagef(in, sampler, gid);                \n",
+    "  write_imagef(out, gid, in_v*0.5f);                           \n",
+    "}                                                              \n",
+    NULL,
+    };
+
+    CL_SAFE_CALL( program = gegl_clCreateProgramWithSource(gegl_cl_get_context(),
+                                                           gegl_cl_count_lines(kernel_source),
+                                                           (const char **)&kernel_source,
+                                                           NULL, &errcode) );
+
+    errcode = gegl_clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
+    if (errcode != CL_SUCCESS)
+      {
+        CL_SAFE_CALL( errcode = gegl_clGetProgramBuildInfo(program,
+                                                           gegl_cl_get_device(),
+                                                           CL_PROGRAM_BUILD_LOG,
+                                                           sizeof(buffer), buffer, NULL) );
+        g_warning("OpenCL Build Error in Line %u in file %s\nError:%s\n%s",
+                  __LINE__, __FILE__, gegl_cl_errstring(errcode), buffer);
+      }
+
+    CL_SAFE_CALL( kernel = gegl_clCreateKernel(program, "kernel_m", &errcode) );
+  }
+
+  /* process */
+  {
+    GeglBufferIterator *i;
+    gint index;
+
+    i = gegl_buffer_iterator_new (buffer_write, NULL, NULL, GEGL_BUFFER_CL_WRITE);
+    index = gegl_buffer_iterator_add (i, buffer_read, NULL, NULL, GEGL_BUFFER_CL_READ);
+    while (gegl_buffer_iterator_next (i))
+      {
+        GeglClTexture *in_tex  = i->cl_data[index];
+        GeglClTexture *out_tex = i->cl_data[0];
+        size_t global_worksize[2] = {i->roi[0].width, i->roi[0].height};
+
+        CL_SAFE_CALL( errcode = gegl_clSetKernelArg(kernel, 0, sizeof(cl_mem),   (void*)&in_tex->data) );
+        CL_SAFE_CALL( errcode = gegl_clSetKernelArg(kernel, 1, sizeof(cl_mem),   (void*)&out_tex->data) );
+        CL_SAFE_CALL( errcode = gegl_clEnqueueNDRangeKernel(gegl_cl_get_command_queue(), kernel, 2,
+                                                      NULL, global_worksize, NULL,
+                                                      0, NULL, NULL) );
+        CL_SAFE_CALL( errcode = gegl_clFinish(gegl_cl_get_command_queue()) );
+      }
+
+    gegl_buffer_get (buffer_write, 1.0, NULL, NULL, buf_write, GEGL_AUTO_ROWSTRIDE);
+  }
+
+  /* verification */
+  {
+    GeglNode *gegl, *sink;
+    GeglBuffer *buffer_truth;
+    gfloat *buf_truth;
+    gint i;
+
+    gegl = gegl_graph (sink = gegl_node ("gegl:buffer-sink", "buffer", &buffer_truth,
+                                                             "format",  babl_format ("RGBA float"), NULL,
+                              gegl_node ("gegl:load", "path", "GEGL.png", NULL)));
+
+    gegl_node_process (sink);
+
+    buf_truth = g_new (gfloat, gegl_buffer_get_pixel_count (buffer_truth)  * 4);
+    gegl_buffer_get (buffer_truth, 1.0, NULL, NULL, buf_truth, GEGL_AUTO_ROWSTRIDE);
+
+    assert(gegl_buffer_get_pixel_count (buffer_truth) == gegl_buffer_get_pixel_count (buffer_write));
+
+    for (i=0; i<gegl_buffer_get_pixel_count (buffer_write)*4; i++)
+      if (!GEGL_FLOAT_EQUAL (buf_truth[i]*0.5f, buf_write[i]))
+        {
+          g_printerr ("The test failed. "
+            "Aborting.\n");
+
+          retval = FAILURE;
+          goto abort;
+        }
+    goto ok;
+
+abort:
+    /* Save Image */
+    {
+      GeglNode *gegl, *sink;
+      gegl = gegl_graph (sink = gegl_node ("gegl:png-save", "path", "error.png", NULL,
+                                gegl_node ("gegl:buffer-source", "buffer", buffer_write, NULL)));
+      gegl_node_process (sink);
+      g_object_unref (gegl);
+    }
+
+ok:
+    g_free (buf_truth);
+    gegl_buffer_destroy (buffer_truth);
+    g_object_unref (gegl);
+  }
+
+  g_free (buf_write);
+  gegl_buffer_destroy (buffer_read);
+  gegl_buffer_destroy (buffer_write);
+
+  gegl_exit ();
+
+  return retval;
+}
diff --git a/tests/opencl/test-gegl-buffer-cl-read.c b/tests/opencl/test-gegl-buffer-cl-read.c
new file mode 100644
index 0000000..a41fe59
--- /dev/null
+++ b/tests/opencl/test-gegl-buffer-cl-read.c
@@ -0,0 +1,129 @@
+/* This file is part of GEGL.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2011 Victor M. de Araujo Oliveira <victormatheus gmail com>
+ */
+
+#include <string.h>
+#include <assert.h>
+#include <babl/babl.h>
+
+#include "gegl.h"
+#include "gegl-types.h"
+#include "gegl-utils.h"
+#include "gegl-cl-init.h"
+#include "gegl-cl-texture.h"
+
+#define SUCCESS 0
+#define FAILURE (-1)
+
+gint
+main (gint    argc,
+      gchar **argv)
+{
+  gint retval = SUCCESS;
+
+  GeglBuffer *buffer_read, *buffer_write;
+  gfloat *buf_write;
+
+  gegl_init (&argc, &argv);
+  gegl_cl_init (NULL);
+
+  /* Load Image */
+  {
+    GeglNode *gegl, *sink;
+    gegl = gegl_graph (sink = gegl_node ("gegl:buffer-sink", "buffer", &buffer_read,
+                                                             "format",  babl_format ("RGBA float"), NULL,
+                              gegl_node ("gegl:load", "path", "GEGL.png", NULL)));
+
+    gegl_node_process (sink);
+
+    buffer_write = gegl_buffer_new (gegl_buffer_get_extent (buffer_read), babl_format ("RGBA float"));
+
+    buf_write = g_new (gfloat, gegl_buffer_get_pixel_count (buffer_write) * 4);
+    g_object_unref (gegl);
+  }
+
+  /* process */
+
+  {
+    GeglBufferIterator *i;
+    gint index;
+
+    i = gegl_buffer_iterator_new (buffer_write, NULL, NULL, GEGL_BUFFER_WRITE);
+    index = gegl_buffer_iterator_add (i, buffer_read, NULL, NULL, GEGL_BUFFER_CL_READ);
+    while (gegl_buffer_iterator_next (i))
+      {
+        GeglClTexture *in_tex  = i->cl_data[index];
+        gpointer *out = i->data[0];
+        gegl_cl_texture_get (in_tex, out);
+      }
+
+    gegl_buffer_get (buffer_write, 1.0, NULL, NULL, buf_write, GEGL_AUTO_ROWSTRIDE);
+  }
+
+  /* Verification */
+  {
+    GeglNode *gegl, *sink;
+    GeglBuffer *buffer_truth;
+    gfloat *buf_truth;
+    gint i;
+
+    gegl = gegl_graph (sink = gegl_node ("gegl:buffer-sink", "buffer", &buffer_truth,
+                                                             "format",  babl_format ("RGBA float"), NULL,
+                              gegl_node ("gegl:load", "path", "GEGL.png", NULL)));
+
+    gegl_node_process (sink);
+
+    buf_truth = g_new (gfloat, gegl_buffer_get_pixel_count (buffer_truth)  * 4);
+    gegl_buffer_get (buffer_truth, 1.0, NULL, NULL, buf_truth, GEGL_AUTO_ROWSTRIDE);
+
+    assert(gegl_buffer_get_pixel_count (buffer_truth) == gegl_buffer_get_pixel_count (buffer_write));
+
+    for (i=0; i<gegl_buffer_get_pixel_count (buffer_write)*4; i++)
+      if (!GEGL_FLOAT_EQUAL (buf_truth[i], buf_write[i]))
+        {
+          g_printerr ("The test failed. "
+            "Aborting.\n");
+
+          retval = FAILURE;
+          goto abort;
+        }
+    goto ok;
+
+abort:
+    /* Save Image */
+    {
+      GeglNode *gegl, *sink;
+      gegl = gegl_graph (sink = gegl_node ("gegl:png-save", "path", "error.png", NULL,
+                                gegl_node ("gegl:buffer-source", "buffer", buffer_write, NULL)));
+      gegl_node_process (sink);
+      g_object_unref (gegl);
+    }
+
+ok:
+    g_free (buf_truth);
+    gegl_buffer_destroy (buffer_truth);
+    g_object_unref (gegl);
+  }
+
+  g_free (buf_write);
+  gegl_buffer_destroy (buffer_read);
+  gegl_buffer_destroy (buffer_write);
+
+  gegl_exit ();
+
+  return retval;
+}
diff --git a/tests/opencl/test-gegl-buffer-cl-write.c b/tests/opencl/test-gegl-buffer-cl-write.c
new file mode 100644
index 0000000..2ee8b73
--- /dev/null
+++ b/tests/opencl/test-gegl-buffer-cl-write.c
@@ -0,0 +1,93 @@
+/* This file is part of GEGL.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2011 Victor M. de Araujo Oliveira <victormatheus gmail com>
+ */
+
+#include <string.h>
+#include <babl/babl.h>
+
+#include "gegl.h"
+#include "gegl-types.h"
+#include "gegl-utils.h"
+#include "gegl-cl-init.h"
+#include "gegl-cl-texture.h"
+
+#define SUCCESS 0
+#define FAILURE (-1)
+
+gint
+main (gint    argc,
+      gchar **argv)
+{
+  gint retval = SUCCESS;
+
+  gfloat color[4] = {0.3f, 0.8f, 0.1f, 1.0f};
+
+  GeglBuffer    *buffer;
+  GeglBufferIterator *i;
+  GeglRectangle  rect = {0, 0, 200, 200};
+  gfloat *buf = g_new (gfloat, rect.width*rect.height*4);
+  gint j;
+
+  gegl_init (&argc, &argv);
+  gegl_cl_init (NULL);
+
+  buffer = gegl_buffer_new (&rect, babl_format ("RGBA float"));
+
+  i = gegl_buffer_iterator_new (buffer, &rect, NULL, GEGL_BUFFER_CL_WRITE);
+  while (gegl_buffer_iterator_next (i))
+    {
+    /*gfloat *data = (gfloat*)(i->data[0]);
+      int k=0;
+      for (j=0; j<i->length; j++)
+        {
+          data[k+0] = color[0];
+          data[k+1] = color[1];
+          data[k+2] = color[2];
+          data[k+3] = color[3];
+          k+=4;
+        }*/
+      gegl_cl_texture_fill(i->cl_data[0], NULL, color);
+    }
+
+  gegl_buffer_get (buffer, 1.0, NULL, NULL, buf, GEGL_AUTO_ROWSTRIDE);
+
+  for (j=0 ; j<rect.width*rect.height; j++)
+    {
+      gboolean ok = 1;
+      ok &= GEGL_FLOAT_EQUAL (buf[j*4+0], color[0]);
+      ok &= GEGL_FLOAT_EQUAL (buf[j*4+1], color[1]);
+      ok &= GEGL_FLOAT_EQUAL (buf[j*4+2], color[2]);
+      ok &= GEGL_FLOAT_EQUAL (buf[j*4+3], color[3]);
+      if (!ok)
+        {
+          g_printerr ("The test failed. "
+                      "Aborting.\n");
+
+          retval = FAILURE;
+          goto abort;
+        }
+    }
+
+abort:
+
+  g_free (buf);
+  gegl_buffer_destroy (buffer);
+
+  gegl_exit ();
+
+  return retval;
+}



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