checkerboard.c 10.9 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 <https://www.gnu.org/licenses/>.
15 16 17
 *
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
 */
18

19

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

24 25
#ifdef GEGL_PROPERTIES

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
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"))
44 45 46
    ui_range    (-128, 128)
    ui_meta     ("unit", "pixel-coordinate")
    ui_meta     ("axis", "x")
47 48 49

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

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

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

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

65 66
#else

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

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

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

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

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

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

97

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

101
static gboolean
102 103 104 105 106 107
checkerboard_cl_process (GeglOperation       *operation,
                         cl_mem               out_tex,
                         size_t               global_worksize,
                         const GeglRectangle *roi,
                         gint                 level)
{
108
  GeglProperties *o         = GEGL_PROPERTIES (operation);
109
  const Babl   *out_format  = gegl_operation_get_format (operation, "output");
110 111
  const size_t  gbl_size[2] = {roi->width, roi->height};
  const size_t  gbl_offs[2] = {roi->x, roi->y};
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
  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 (),
140 141
                                        cl_data->kernel[0], 2,
                                        gbl_offs, gbl_size, NULL,
142 143 144 145 146 147 148 149
                                        0, NULL, NULL);
  CL_CHECK;

  return FALSE;
error:
  return TRUE;
}

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

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;
}

218 219 220 221 222 223
static gboolean
checkerboard_process (GeglOperation       *operation,
                      void                *out_buf,
                      glong                n_pixels,
                      const GeglRectangle *roi,
                      gint                 level)
224
{
225
  GeglProperties *o = GEGL_PROPERTIES (operation);
226 227 228 229 230
  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);
231 232 233 234 235
  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;
236

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

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

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

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

251 252 253 254 255
      /* 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;
256
      else
257
        cur_color = color2;
258

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

          count = stripe_end - x;
267

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

          if (cur_color == color1)
            cur_color = color2;
          else
            cur_color = color1;
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 321
  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;
        }

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

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

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

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

340
  source_class->process = operation_source_process;
341
  operation_class->get_bounding_box = get_bounding_box;
342
  operation_class->prepare = prepare;
343
  operation_class->opencl_support = TRUE;
344

345
  gegl_operation_class_set_keys (operation_class,
346 347
    "name",               "gegl:checkerboard",
    "categories",         "render",
348
    "title",              _("Checkerboard"),
349
    "reference-hash",     "b2f5f85a0ec1de87639c1b0cfcd17fbc",
350
    "position-dependent", "true",
351
    "description",        _("Render a checkerboard pattern"),
352
    NULL);
353 354
}

355
#endif