checkerboard.c 3.92 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
#ifdef GEGL_CHANT_PROPERTIES
19

20
gegl_chant_int   (x,        "Width",  1, G_MAXINT, 16,
21
                  "Horizontal width of cells pixels.")
22
gegl_chant_int   (y,        "Height", 1, G_MAXINT, 16,
23 24 25 26 27 28 29 30 31
                  "Vertical width of cells in pixels.")
gegl_chant_int   (x_offset, "X offset", -G_MAXINT, G_MAXINT, 0,
                  "Horizontal offset (from origin) for start of grid.")
gegl_chant_int   (y_offset, "Y offset", -G_MAXINT, G_MAXINT,  0,
                  "Vertical offset (from origin) for start of grid.")
gegl_chant_color (color1,   "Color", "black",
                  "One of the cell colors (defaults to 'black')")
gegl_chant_color (color2,   "Other color", "white",
                  "The other cell color (defaults to 'white')")
32 33 34

#else

35 36
#define GEGL_CHANT_TYPE_SOURCE
#define GEGL_CHANT_C_FILE       "checkerboard.c"
37

38
#include "gegl-chant.h"
39

40
static void
41
prepare (GeglOperation *operation)
42 43 44 45
{
  gegl_operation_set_format (operation, "output", babl_format ("RGBA float"));
}

46
static GeglRectangle
47
get_bounding_box (GeglOperation *operation)
48 49 50 51 52
{
  GeglRectangle result = {-10000000, -10000000, 20000000, 20000000};
  return result;
}

53
static gboolean
54 55
process (GeglOperation       *operation,
         GeglBuffer          *output,
56
         const GeglRectangle *result)
57
{
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
  gfloat     *buf;
  gfloat      color1[4];
  gfloat      color2[4];
  gint        pxsize;
  gint        n_pixels;

  gegl_color_get_rgba (o->color1,
                       &color1[0],
                       &color1[1],
                       &color1[2],
                       &color1[3]);

  gegl_color_get_rgba (o->color2,
                       &color2[0],
                       &color2[1],
                       &color2[2],
                       &color2[3]);

  g_object_get (output, "px-size", &pxsize,
                        "pixels", &n_pixels,
                        NULL);
  buf = g_malloc (n_pixels * pxsize);
    {
      gfloat *dst=buf;
      gint y;
      for (y=0; y < result->height; y++)
        {
          gint x;
          for (x=0; x < result->width ; x++)
            {
              gfloat *rgba_color;
              gint nx,ny;

              nx = ((x + result->x + o->x_offset + 100000 * o->x)/o->x) ;
              ny = ((y + result->y + o->y_offset + 100000 * o->y)/o->y) ;

              rgba_color = (nx+ny) % 2 == 0 ? color1 : color2;

              memcpy (dst, rgba_color, 4*sizeof(gfloat));
              dst += 4;
            }
        }
    }

  gegl_buffer_set (output, NULL, NULL, buf, GEGL_AUTO_ROWSTRIDE);
  g_free (buf);

106 107 108 109
  return  TRUE;
}


110
static void
111
gegl_chant_class_init (GeglChantClass *klass)
112
{
113 114 115 116 117 118 119
  GeglOperationClass       *operation_class;
  GeglOperationSourceClass *source_class;

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

  source_class->process = process;
120
  operation_class->get_bounding_box = get_bounding_box;
121 122 123 124 125 126 127
  operation_class->prepare = prepare;

  operation_class->name        = "checkerboard";
  operation_class->categories  = "render";
  operation_class->description = "Checkerboard renderer.";

  operation_class->no_cache = TRUE;
128
  operation_class->get_cached_region = NULL;
129 130
}

131
#endif