checkerboard.c 10.8 KB
Newer Older
1 2 3 4 5
/* This file is an image processing operation for GEGL
 *
 * GEGL 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
6
 * version 3 of the License, or (at your option) any later version.
7 8 9 10 11 12 13
 *
 * GEGL 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
14
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
15 16 17
 *
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
 */
18 19 20

#include "config.h"
#include <glib/gi18n-lib.h>
21
#include <stdlib.h>
22

23 24
#ifdef GEGL_PROPERTIES

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
property_int (x, _("Width"), 16)
    description (_("Horizontal width of cells pixels"))
    value_range (1, G_MAXINT)
    ui_range    (1, 256)
    ui_gamma    (1.5)
    ui_meta     ("unit", "pixel-distance")
    ui_meta     ("axis", "x")

property_int (y, _("Height"), 16)
    description (_("Vertical width of cells pixels"))
    value_range (1, G_MAXINT)
    ui_range    (1, 256)
    ui_gamma    (1.5)
    ui_meta     ("unit", "pixel-distance")
    ui_meta     ("axis", "y")

property_int (x_offset, _("Offset X"), 0.0)
    description (_("Horizontal offset (from origin) for start of grid"))
43 44 45
    ui_range    (-128, 128)
    ui_meta     ("unit", "pixel-coordinate")
    ui_meta     ("axis", "x")
46 47 48

property_int (y_offset, _("Offset Y"), 0)
    description (_("Vertical offset (from origin) for start of grid"))
49
    ui_range    (-128, 128)
50 51 52
    ui_meta     ("unit", "pixel-coordinate")
    ui_meta     ("axis", "y")

53 54
property_color  (color1, _("Color 1"), "black")
    description (_("The first cell color"))
55 56
    ui_meta     ("role", "color-primary")

57 58
property_color  (color2, _("Color 2"), "white")
    description (_("The second cell color"))
59 60 61 62
    ui_meta     ("role", "color-secondary")

property_format (format, _("Babl Format"), NULL)
    description ( _("The babl format of the output"))
63

64 65
#else

66
#define GEGL_OP_POINT_RENDER
67
#define GEGL_OP_NAME     checkerboard
68
#define GEGL_OP_C_SOURCE checkerboard.c
69

70
#include "gegl-op.h"
71
#include <gegl-buffer-cl-iterator.h>
72
#include <../../gegl/gegl-debug.h>
73

74
static void
75
prepare (GeglOperation *operation)
76
{
77
  GeglProperties *o = GEGL_PROPERTIES (operation);
78 79 80 81 82

  if (o->format)
    gegl_operation_set_format (operation, "output", o->format);
  else
    gegl_operation_set_format (operation, "output", babl_format ("RGBA float"));
83 84
}

85
static GeglRectangle
86
get_bounding_box (GeglOperation *operation)
87
{
88
  return gegl_rectangle_infinite_plane ();
89 90
}

91 92 93 94 95
#define TILE_INDEX(coordinate,stride) \
  (((coordinate) >= 0)?\
      (coordinate) / (stride):\
      ((((coordinate) + 1) /(stride)) - 1))

96

97
#include "opencl/checkerboard.cl.h"
98 99
static GeglClRunData *cl_data = NULL;

100
static gboolean
101 102 103 104 105 106
checkerboard_cl_process (GeglOperation       *operation,
                         cl_mem               out_tex,
                         size_t               global_worksize,
                         const GeglRectangle *roi,
                         gint                 level)
{
107
  GeglProperties *o         = GEGL_PROPERTIES (operation);
108
  const Babl   *out_format  = gegl_operation_get_format (operation, "output");
109 110
  const size_t  gbl_size[2] = {roi->width, roi->height};
  const size_t  gbl_offs[2] = {roi->x, roi->y};
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
  cl_int        cl_err      = 0;
  float         color1[4];
  float         color2[4];

  if (!cl_data)
  {
    const char *kernel_name[] = {"kernel_checkerboard", NULL};
    cl_data = gegl_cl_compile_and_build (checkerboard_cl_source, kernel_name);

    if (!cl_data)
      return TRUE;
  }

  gegl_color_get_pixel (o->color1, out_format, color1);
  gegl_color_get_pixel (o->color2, out_format, color2);

  cl_err = gegl_cl_set_kernel_args (cl_data->kernel[0],
                                    sizeof(cl_mem), &out_tex,
                                    sizeof(color1), &color1,
                                    sizeof(color2), &color2,
                                    sizeof(cl_int), &o->x,
                                    sizeof(cl_int), &o->y,
                                    sizeof(cl_int), &o->x_offset,
                                    sizeof(cl_int), &o->y_offset,
                                    NULL);
  CL_CHECK;

  cl_err = gegl_clEnqueueNDRangeKernel (gegl_cl_get_command_queue (),
139 140
                                        cl_data->kernel[0], 2,
                                        gbl_offs, gbl_size, NULL,
141 142 143 144 145 146 147 148
                                        0, NULL, NULL);
  CL_CHECK;

  return FALSE;
error:
  return TRUE;
}

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

static gboolean
checkerboard_process_simple (GeglOperation       *operation,
                             void                *out_buf,
                             glong                n_pixels,
                             const GeglRectangle *roi,
                             gint                 level)
{
  gint        factor = 1 << level;
  GeglProperties *o = GEGL_PROPERTIES (operation);
  const Babl *out_format = gegl_operation_get_format (operation, "output");

  gint        pixel_size = babl_format_get_bytes_per_pixel (out_format);
  guchar     *out_pixel = out_buf;
  void       *color1 = alloca(pixel_size);
  void       *color2 = alloca(pixel_size);

  gint        x = roi->x; /* initial x                   */
  gint        y = roi->y; /*           and y coordinates */

  gegl_color_get_pixel (o->color1, out_format, color1);
  gegl_color_get_pixel (o->color2, out_format, color2);

  while (n_pixels--)
    {
      gint nx,ny;

      if ((x - o->x_offset) < 0)
        {
          nx = div (x - o->x_offset + 1, o->x / factor).quot;
        }
      else
        {
          nx = div (x - o->x_offset, o->x / factor).quot;
        }

      if ((y - o->y_offset) < 0)
        {
          ny = div (y - o->y_offset + 1, o->y / factor).quot;
        }
      else
        {
          ny = div (y - o->y_offset, o->y / factor).quot;
        }

      /* shift negative cell indices */
      nx -= (x - o->x_offset) < 0 ? 1 : 0;
      ny -= (y - o->y_offset) < 0 ? 1 : 0;

      if ( (nx+ny) % 2 == 0)
        memcpy (out_pixel, color1, pixel_size);
      else
        memcpy (out_pixel, color2, pixel_size);

      out_pixel += pixel_size;

      /* update x and y coordinates */
      x++;
      if (x>=roi->x + roi->width)
        {
          x=roi->x;
          y++;
        }
    }

  return  TRUE;
}

217 218 219 220 221 222
static gboolean
checkerboard_process (GeglOperation       *operation,
                      void                *out_buf,
                      glong                n_pixels,
                      const GeglRectangle *roi,
                      gint                 level)
223
{
224
  GeglProperties *o = GEGL_PROPERTIES (operation);
225 226 227 228 229
  const Babl *out_format = gegl_operation_get_format (operation, "output");
  gint        pixel_size = babl_format_get_bytes_per_pixel (out_format);
  guchar     *out_pixel = out_buf;
  void       *color1 = alloca(pixel_size);
  void       *color2 = alloca(pixel_size);
230 231 232 233 234
  gint        y;
  const gint  x_min = roi->x - o->x_offset;
  const gint  y_min = roi->y - o->y_offset;
  const gint  x_max = roi->x + roi->width - o->x_offset;
  const gint  y_max = roi->y + roi->height - o->y_offset;
235

236 237
  const gint  square_width  = o->x;
  const gint  square_height = o->y;
238

239 240 241
  if (level)
    return checkerboard_process_simple (operation, out_buf, n_pixels, roi, level);

242 243
  gegl_color_get_pixel (o->color1, out_format, color1);
  gegl_color_get_pixel (o->color2, out_format, color2);
244

245 246
  for (y = y_min; y < y_max; y++)
    {
247
      gint  x = x_min;
248
      void *cur_color;
249

250 251 252 253 254
      /* Figure out which box we're in */
      gint tilex = TILE_INDEX (x, square_width);
      gint tiley = TILE_INDEX (y, square_height);
      if ((tilex + tiley) % 2 == 0)
        cur_color = color1;
255
      else
256
        cur_color = color2;
257

258
      while (x < x_max)
259
        {
260
          /* Figure out how long this stripe is */
261
          gint count;
262 263
          gint stripe_end = (TILE_INDEX (x, square_width) + 1) * square_width;
               stripe_end = stripe_end > x_max ? x_max : stripe_end;
264 265

          count = stripe_end - x;
266

267 268 269
          gegl_memset_pattern (out_pixel, cur_color, pixel_size, count);
          out_pixel += count * pixel_size;
          x = stripe_end;
270 271 272 273 274

          if (cur_color == color1)
            cur_color = color2;
          else
            cur_color = color1;
275 276 277
        }
    }

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
  return TRUE;
}


static gboolean
operation_source_process (GeglOperation       *operation,
                          GeglBuffer          *output,
                          const GeglRectangle *result,
                          gint                 level)
{
  const Babl *out_format = gegl_operation_get_format (operation, "output");

  if ((result->width > 0) && (result->height > 0))
    {
      GeglBufferIterator *iter;
      if (gegl_operation_use_opencl (operation) &&
          babl_format_get_n_components (out_format) == 4 &&
          babl_format_get_type (out_format, 0) == babl_type ("float"))
        {
          GeglBufferClIterator *cl_iter;
          gboolean err;

          GEGL_NOTE (GEGL_DEBUG_OPENCL, "GEGL_OPERATION_POINT_RENDER: %s", GEGL_OPERATION_GET_CLASS (operation)->name);

          cl_iter = gegl_buffer_cl_iterator_new (output, result, out_format, GEGL_CL_BUFFER_WRITE);

          while (gegl_buffer_cl_iterator_next (cl_iter, &err) && !err)
            {
              err = checkerboard_cl_process (operation, cl_iter->tex[0], cl_iter->size[0], &cl_iter->roi[0], level);

              if (err)
                {
                  gegl_buffer_cl_iterator_stop (cl_iter);
                  break;
                }
            }

          if (err)
            GEGL_NOTE (GEGL_DEBUG_OPENCL, "Error: %s", GEGL_OPERATION_GET_CLASS (operation)->name);
          else
            return TRUE;
        }

321 322
      iter = gegl_buffer_iterator_new (output, result, level, out_format,
                                       GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE);
323 324

      while (gegl_buffer_iterator_next (iter))
325
        checkerboard_process (operation, iter->data[0], iter->length, &iter->roi[0], level);
326 327
    }
  return TRUE;
328 329
}

330
static void
331
gegl_op_class_init (GeglOpClass *klass)
332
{
333
  GeglOperationClass            *operation_class;
334
  GeglOperationSourceClass      *source_class;
335 336

  operation_class = GEGL_OPERATION_CLASS (klass);
337
  source_class = GEGL_OPERATION_SOURCE_CLASS (klass);
338

339
  source_class->process = operation_source_process;
340
  operation_class->get_bounding_box = get_bounding_box;
341 342
  operation_class->prepare = prepare;

343
  gegl_operation_class_set_keys (operation_class,
344 345
    "name",               "gegl:checkerboard",
    "categories",         "render",
346
    "title",              _("Checkerboard"),
347
    "position-dependent", "true",
348
    "description",        _("Create a checkerboard pattern"),
349
    NULL);
350 351
}

352
#endif