checkerboard.c 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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
 * version 2 of the License, or (at your option) any later version.
 *
 * 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
 * License along with GEGL; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
 */
20
#if GEGL_CHANT_PROPERTIES
21

Øyvind Kolås's avatar
Øyvind Kolås committed
22 23 24 25 26 27
gegl_chant_int   (x,        -G_MAXINT, G_MAXINT, 16, "")
gegl_chant_int   (y,        -G_MAXINT, G_MAXINT, 16, "")
gegl_chant_int   (x_offset, -G_MAXINT, G_MAXINT,  0, "")
gegl_chant_int   (y_offset, -G_MAXINT, G_MAXINT,  0, "")
gegl_chant_color (color1,    "black",                "")
gegl_chant_color (color2,    "white",                "")
28 29 30

#else

31 32
#define GEGL_CHANT_SOURCE
#define GEGL_CHANT_NAME           checkerboard
Øyvind Kolås's avatar
Øyvind Kolås committed
33
#define GEGL_CHANT_DESCRIPTION    "Checkerboard renderer. Colors defaults to black and white. "
34

35
#define GEGL_CHANT_SELF           "checkerboard.c"
Øyvind Kolås's avatar
Øyvind Kolås committed
36
#define GEGL_CHANT_CATEGORIES     "sources:render"
37 38
#include "gegl-chant.h"

39 40 41 42 43
#define REMAINDER(dividend, divisor)                     \
    ((dividend) < 0 ?                                    \
       (divisor) - 1 - ((-(dividend) - 1) % (divisor)) : \
       (dividend) % (divisor))

44
static gboolean
45
process (GeglOperation *operation)
46 47 48
{
  GeglRect  *need;
  GeglOperationSource  *op_source = GEGL_OPERATION_SOURCE(operation);
49
  GeglChantOperation *self = GEGL_CHANT_OPERATION (operation);
50

51
  need = gegl_operation_get_requested_region (operation);
52 53 54
  {
    GeglRect *result = gegl_operation_result_rect (operation);
    gfloat *buf;
Øyvind Kolås's avatar
Øyvind Kolås committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68
    gfloat color1[4];
    gfloat color2[4];

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

    gegl_color_get_rgba (self->color2,
                         &color2[0],
                         &color2[1],
                         &color2[2],
                         &color2[3]);
69 70

    op_source->output = g_object_new (GEGL_TYPE_BUFFER,
71 72
                        "format",
                        babl_format ("RGBA float"),
73 74 75 76 77 78 79 80 81 82 83 84 85 86
                        "x",      result->x,
                        "y",      result->y,
                        "width",  result->w,
                        "height", result->h,
                        NULL);
    buf = g_malloc (gegl_buffer_pixels (op_source->output) * gegl_buffer_px_size (op_source->output));
      {
        gfloat *dst=buf;
        gint y;
        for (y=0; y < result->h; y++)
          {
            gint x;
            for (x=0; x < result->w; x++)
              {
87
                gfloat *rgba_color;
88 89
                gint nx,ny;

90 91
                nx = ((x + result->x + self->x_offset + 100000 * self->x)/self->x) ;
                ny = ((y + result->y + self->y_offset + 100000 * self->y)/self->y) ;
92

93
                rgba_color = (nx+ny) % 2 == 0 ? color1 : color2;
94 95 96

                memcpy(dst, rgba_color, 4*sizeof(gfloat));
                dst += 4;
97 98 99 100 101 102 103 104 105
              }
          }
      }
    gegl_buffer_set (op_source->output, buf);
    g_free (buf);
  }
  return  TRUE;
}

106
static GeglRect 
107
get_defined_region (GeglOperation *operation)
108
{
109 110
  GeglRect result = {-10000000,-10000000, 20000000, 20000000};
  return result;
111 112 113
}

#endif