Blame operations/common/checkerboard.c

Packit bc1512
/* This file is an image processing operation for GEGL
Packit bc1512
 *
Packit bc1512
 * GEGL is free software; you can redistribute it and/or
Packit bc1512
 * modify it under the terms of the GNU Lesser General Public
Packit bc1512
 * License as published by the Free Software Foundation; either
Packit bc1512
 * version 3 of the License, or (at your option) any later version.
Packit bc1512
 *
Packit bc1512
 * GEGL is distributed in the hope that it will be useful,
Packit bc1512
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit bc1512
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit bc1512
 * Lesser General Public License for more details.
Packit bc1512
 *
Packit bc1512
 * You should have received a copy of the GNU Lesser General Public
Packit bc1512
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
Packit bc1512
 *
Packit bc1512
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
Packit bc1512
 */
Packit bc1512
Packit bc1512
#include "config.h"
Packit bc1512
#include <glib/gi18n-lib.h>
Packit bc1512
#include <stdlib.h>
Packit bc1512
Packit bc1512
#ifdef GEGL_CHANT_PROPERTIES
Packit bc1512
Packit bc1512
gegl_chant_int_ui   (x,        _("Width"),  1, G_MAXINT, 16, 1, 256, 1.5,
Packit bc1512
                  _("Horizontal width of cells pixels"))
Packit bc1512
gegl_chant_int_ui   (y,        _("Height"), 1, G_MAXINT, 16, 1, 256, 1.5,
Packit bc1512
                  _("Vertical width of cells in pixels"))
Packit bc1512
gegl_chant_int_ui   (x_offset, _("X offset"), -G_MAXINT, G_MAXINT, 0, -10, 10, 1.0,
Packit bc1512
                  _("Horizontal offset (from origin) for start of grid"))
Packit bc1512
gegl_chant_int_ui   (y_offset, _("Y offset"), -G_MAXINT, G_MAXINT,  0, -10, 10, 1.0,
Packit bc1512
                  _("Vertical offset (from origin) for start of grid"))
Packit bc1512
gegl_chant_color (color1,   _("Color"), "black",
Packit bc1512
                  _("One of the cell colors (defaults to 'black')"))
Packit bc1512
gegl_chant_color (color2,   _("Other color"), "white",
Packit bc1512
                  _("The other cell color (defaults to 'white')"))
Packit bc1512
Packit bc1512
#else
Packit bc1512
Packit bc1512
#define GEGL_CHANT_TYPE_POINT_RENDER
Packit bc1512
#define GEGL_CHANT_C_FILE       "checkerboard.c"
Packit bc1512
Packit bc1512
#include "gegl-chant.h"
Packit bc1512
Packit bc1512
static void
Packit bc1512
prepare (GeglOperation *operation)
Packit bc1512
{
Packit bc1512
  gegl_operation_set_format (operation, "output", babl_format ("RGBA float"));
Packit bc1512
}
Packit bc1512
Packit bc1512
static GeglRectangle
Packit bc1512
get_bounding_box (GeglOperation *operation)
Packit bc1512
{
Packit bc1512
  return gegl_rectangle_infinite_plane ();
Packit bc1512
}
Packit bc1512
Packit bc1512
static gboolean
Packit bc1512
process (GeglOperation       *operation,
Packit bc1512
         void                *out_buf,
Packit bc1512
         glong                n_pixels,
Packit bc1512
         const GeglRectangle *roi,
Packit bc1512
         gint                 level)
Packit bc1512
{
Packit bc1512
  GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
Packit bc1512
  gfloat     *out_pixel = out_buf;
Packit bc1512
  gfloat      color1[4];
Packit bc1512
  gfloat      color2[4];
Packit bc1512
  gint        x = roi->x; /* initial x                   */
Packit bc1512
  gint        y = roi->y; /*           and y coordinates */
Packit bc1512
Packit bc1512
  gegl_color_get_pixel (o->color1, babl_format ("RGBA float"), color1);
Packit bc1512
  gegl_color_get_pixel (o->color2, babl_format ("RGBA float"), color2);
Packit bc1512
Packit bc1512
  while (n_pixels--)
Packit bc1512
    {
Packit bc1512
      gint nx,ny;
Packit bc1512
Packit bc1512
      if ((x - o->x_offset) < 0)
Packit bc1512
        {
Packit bc1512
          nx = div (x - o->x_offset + 1, o->x).quot;
Packit bc1512
        }
Packit bc1512
      else
Packit bc1512
        {
Packit bc1512
          nx = div (x - o->x_offset, o->x).quot;
Packit bc1512
        }
Packit bc1512
Packit bc1512
      if ((y - o->y_offset) < 0)
Packit bc1512
        {
Packit bc1512
          ny = div (y - o->y_offset + 1, o->y).quot;
Packit bc1512
        }
Packit bc1512
      else
Packit bc1512
        {
Packit bc1512
          ny = div (y - o->y_offset, o->y).quot;
Packit bc1512
        }
Packit bc1512
Packit bc1512
      /* shift negative cell indices */
Packit bc1512
      nx -= (x - o->x_offset) < 0 ? 1 : 0;
Packit bc1512
      ny -= (y - o->y_offset) < 0 ? 1 : 0;
Packit bc1512
Packit bc1512
      if ( (nx+ny) % 2 == 0)
Packit bc1512
        {
Packit bc1512
          out_pixel[0]=color1[0];
Packit bc1512
          out_pixel[1]=color1[1];
Packit bc1512
          out_pixel[2]=color1[2];
Packit bc1512
          out_pixel[3]=color1[3];
Packit bc1512
        }
Packit bc1512
      else
Packit bc1512
        {
Packit bc1512
          out_pixel[0]=color2[0];
Packit bc1512
          out_pixel[1]=color2[1];
Packit bc1512
          out_pixel[2]=color2[2];
Packit bc1512
          out_pixel[3]=color2[3];
Packit bc1512
        }
Packit bc1512
Packit bc1512
      out_pixel += 4;
Packit bc1512
Packit bc1512
      /* update x and y coordinates */
Packit bc1512
      x++;
Packit bc1512
      if (x>=roi->x + roi->width)
Packit bc1512
        {
Packit bc1512
          x=roi->x;
Packit bc1512
          y++;
Packit bc1512
        }
Packit bc1512
    }
Packit bc1512
Packit bc1512
  return  TRUE;
Packit bc1512
}
Packit bc1512
Packit bc1512
Packit bc1512
static void
Packit bc1512
gegl_chant_class_init (GeglChantClass *klass)
Packit bc1512
{
Packit bc1512
  GeglOperationClass            *operation_class;
Packit bc1512
  GeglOperationPointRenderClass *point_render_class;
Packit bc1512
Packit bc1512
  operation_class = GEGL_OPERATION_CLASS (klass);
Packit bc1512
  point_render_class = GEGL_OPERATION_POINT_RENDER_CLASS (klass);
Packit bc1512
Packit bc1512
  point_render_class->process = process;
Packit bc1512
  operation_class->get_bounding_box = get_bounding_box;
Packit bc1512
  operation_class->prepare = prepare;
Packit bc1512
Packit bc1512
  gegl_operation_class_set_keys (operation_class,
Packit bc1512
    "name",  "gegl:checkerboard",
Packit bc1512
    "categories", "render",
Packit bc1512
    "description", _("Checkerboard renderer"),
Packit bc1512
    NULL);
Packit bc1512
}
Packit bc1512
Packit bc1512
#endif