tile-glass.c 6.55 KB
Newer Older
Denis Knoepfle's avatar
Denis Knoepfle committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* This file is an image processing operation for GEGL
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contains code originaly from GIMP tile-glass.c, copyright
 * Karl-Johan Andersson and Tim Copperfield.
 *
 * Glass Tile (sequential version) ported to GEGL:
 * Copyright 2014 Denis Knoepfle
 */

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

26 27
#ifdef GEGL_PROPERTIES

28 29 30 31 32 33 34 35 36 37 38
property_int (tile_width, _("Tile Width"), 25)
   value_range (5, 500)
   ui_range    (5, 50)
   ui_meta     ("unit",   "pixel-distance")
   ui_meta     ("axis",   "x")

property_int (tile_height, _("Tile Height"), 25)
   value_range (5, 500)
   ui_range    (5, 50)
   ui_meta     ("unit",   "pixel-distance")
   ui_meta     ("axis",   "y")
Denis Knoepfle's avatar
Denis Knoepfle committed
39 40 41

#else

42 43
#define GEGL_OP_AREA_FILTER
#define GEGL_OP_C_FILE "tile-glass.c"
Denis Knoepfle's avatar
Denis Knoepfle committed
44

45
#include "gegl-op.h"
Denis Knoepfle's avatar
Denis Knoepfle committed
46 47 48 49 50 51 52 53 54
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

static void
prepare (GeglOperation *operation)
{
  const Babl *input_format = gegl_operation_get_source_format (operation, "input");
  const Babl *format;
55
  GeglProperties *o = GEGL_PROPERTIES (operation);
Denis Knoepfle's avatar
Denis Knoepfle committed
56 57 58 59 60 61 62 63

  GeglOperationAreaFilter *area = GEGL_OPERATION_AREA_FILTER (operation);

  if (input_format == NULL || babl_format_has_alpha (input_format))
    format = babl_format ("R'G'B'A float");
  else
    format = babl_format ("R'G'B' float");

64 65
  area->left = area->right = o->tile_width - 1;
  area->top = area->bottom = o->tile_height - 1;
Denis Knoepfle's avatar
Denis Knoepfle committed
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 106 107 108 109 110 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 139 140 141 142 143 144 145 146 147 148 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

  gegl_operation_set_format (operation, "input", format);
  gegl_operation_set_format (operation, "output", format);
}

static GeglRectangle
get_bounding_box (GeglOperation *operation)
{
  GeglRectangle *region;

  region = gegl_operation_source_get_bounding_box (operation, "input");

  if (region != NULL)
    return *region;
  else
    return *GEGL_RECTANGLE (0, 0, 0, 0);
}

static void
tile_glass (GeglBuffer          *src,
            GeglBuffer          *dst,
            const GeglRectangle *dst_rect, /* region of interest */
            const Babl          *format,
            gint                 tileWidth,
            gint                 tileHeight)
{
  gint components;
  gfloat *src_row_buf;
  gfloat *dst_row_buf;
  const GeglRectangle *extent;
  GeglRectangle src_bufrect, dst_bufrect;

  gint row, col, i;
  gint x1, y1, y2;
  gint dst_xoffs, src_x0, xright_abyss, src_rowwidth;

  gint xpixel1, xpixel2;
  gint ypixel2;
  gint xhalf, xoffs, xmiddle, xplus;
  gint yhalf, yoffs, ymiddle, yplus;

  extent = gegl_buffer_get_extent (dst);

  x1 = dst_rect->x;
  y1 = dst_rect->y;
  y2 = y1 + dst_rect->height;

  xhalf = tileWidth  / 2;
  yhalf = tileHeight / 2;

  xplus = tileWidth  % 2;
  yplus = tileHeight % 2;

  dst_xoffs = x1 % tileWidth + xplus;
  src_x0 = x1 - dst_xoffs;
  xright_abyss = 2 * ((x1 + dst_rect->width) % tileWidth);
  if (xright_abyss > tileWidth - 2)
    xright_abyss = tileWidth - 2;
  src_rowwidth = dst_xoffs + dst_rect->width + xright_abyss;

  yoffs = y1 % tileHeight;
  ymiddle = y1 - yoffs;
  if (yoffs >= yhalf)
    {
      ymiddle += tileHeight;
      yoffs -= tileHeight;
    }

  components = babl_format_get_n_components (format);

  src_row_buf = g_new (gfloat, src_rowwidth * components);
  dst_row_buf = g_new (gfloat, dst_rect->width * components);
  gegl_rectangle_set (&src_bufrect, src_x0, 0, src_rowwidth, 1);
  gegl_rectangle_set (&dst_bufrect, x1, 0, dst_rect->width, 1);

  /* loop through rows */
  for (row = y1; row < y2; ++row)
    {
      /* no need to clamp as abyss policy does that */
      ypixel2 = ymiddle + yoffs * 2;

      src_bufrect.y = ypixel2;
      gegl_buffer_get (src, &src_bufrect, 1.0, format, src_row_buf, GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_CLAMP);

      yoffs++;

      /* if current offset = half, do a displacement next time around */
      if (yoffs == yhalf)
        {
          ymiddle += tileHeight;
          yoffs = - (yhalf + yplus);
        }

      xoffs = x1 % tileWidth;
      xmiddle = x1 - xoffs;
      if (xoffs >= xhalf)
        {
          xmiddle += tileWidth;
          xoffs -= tileWidth;
        }

      /* loop through columns */
      for (col = 0; col < dst_rect->width; ++col)
        {
          xpixel1 = (xmiddle + xoffs - x1) * components;
          if (xmiddle + xoffs * 2 + dst_xoffs < extent->width)
            {
              xpixel2 = (xmiddle + xoffs * 2 - x1 + dst_xoffs) * components;
            }
          else
            {
              xpixel2 = (xmiddle + xoffs - x1 + dst_xoffs) * components;
            }

          for (i = 0; i < components; ++i)
            {
              dst_row_buf[xpixel1 + i] = src_row_buf[xpixel2 + i];
            }

          xoffs++;

          if (xoffs == xhalf)
            {
              xmiddle += tileWidth;
              xoffs = - (xhalf + xplus);
            }
        }

      /* write result row to dest */
      dst_bufrect.y = row;
      gegl_buffer_set (dst, &dst_bufrect, 0, format, dst_row_buf, GEGL_AUTO_ROWSTRIDE);
    }

  g_free (src_row_buf);
  g_free (dst_row_buf);
}

static gboolean
process (GeglOperation       *operation,
         GeglBuffer          *input,
         GeglBuffer          *output,
         const GeglRectangle *result,
         gint                 level)
{
210
  GeglProperties *o = GEGL_PROPERTIES (operation);
Denis Knoepfle's avatar
Denis Knoepfle committed
211 212
  const Babl *format = gegl_operation_get_format (operation, "input");

213
  tile_glass (input, output, result, format, o->tile_width, o->tile_height);
Denis Knoepfle's avatar
Denis Knoepfle committed
214 215 216 217 218

  return TRUE;
}

static void
219
gegl_op_class_init (GeglOpClass *klass)
Denis Knoepfle's avatar
Denis Knoepfle committed
220 221 222 223 224 225 226 227 228 229 230 231
{
  GeglOperationClass       *operation_class;
  GeglOperationFilterClass *filter_class;

  operation_class = GEGL_OPERATION_CLASS (klass);
  filter_class    = GEGL_OPERATION_FILTER_CLASS (klass);

  operation_class->prepare          = prepare;
  operation_class->get_bounding_box = get_bounding_box;
  filter_class->process             = process;

  gegl_operation_class_set_keys (operation_class,
232 233 234 235
    "categories",         "artistic",
    "license",            "GPL3+",
    "name",               "gegl:tile-glass",
    "position-dependent", "true",
236
    "description", _("Simulate distortion caused by rectangular glass tiles"),
Denis Knoepfle's avatar
Denis Knoepfle committed
237 238 239 240
    NULL);
}

#endif