motion-blur-zoom.c 7.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
16
 * Copyright (C) 2013 Téo Mazars  <teo.mazars@ensimag.fr>
17 18
 *
 * This operation is inspired by and uses parts of blur-motion.c
19
 * from GIMP 2.8.4:
20
 *
21 22 23 24 25 26 27 28 29 30
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 * Copyright (C) 1996 Torsten Martinsen       <torsten@danbbs.dk>
 * Copyright (C) 1996 Federico Mena Quintero  <federico@nuclecu.unam.mx>
 * Copyright (C) 1996 Heinz W. Werntges       <quartic@polloux.fciencias.unam.mx>
 * Copyright (C) 1997 Daniel Skarda           <0rfelyus@atrey.karlin.mff.cuni.cz>
 * Copyright (C) 2007 Joerg Gittinger         <sw@gittingerbox.de>
 *
 * This operation is also inspired by GEGL's blur-motion-linear.c :
 *
 * Copyright (C) 2006 Øyvind Kolås  <pippin@gimp.org>
31 32 33 34 35 36 37
 */

#include "config.h"

#include <glib/gi18n-lib.h>
#include <math.h>

38 39
#ifdef GEGL_PROPERTIES

40 41 42
property_double (center_x, _("Center X"), 0.5)
    ui_range    (0.0, 1.0)
    ui_meta     ("unit", "relative-coordinate")
43
    ui_meta     ("axis", "x")
44

45 46 47
property_double (center_y, _("Center Y"), 0.5)
    ui_range    (0.0, 1.0)
    ui_meta     ("unit", "relative-coordinate")
48
    ui_meta     ("axis", "y")
49

50
property_double (factor, _("Blurring factor"), 0.1)
51 52 53
    value_range (-10, 1.0)
    ui_range    (-0.5, 1.0)
    ui_gamma    (2.0)
54 55 56

#else

57 58
#define GEGL_OP_AREA_FILTER
#define GEGL_OP_C_FILE "motion-blur-zoom.c"
59

60
#include "gegl-op.h"
61

62
#define SQR(c) ((c) * (c))
63 64 65 66 67 68 69

#define MAX_NUM_IT 200
#define NOMINAL_NUM_IT 100

static void
prepare (GeglOperation *operation)
{
70
  GeglOperationAreaFilter *op_area = GEGL_OPERATION_AREA_FILTER (operation);
71
  GeglProperties          *o       = GEGL_PROPERTIES (operation);
72
  GeglRectangle           *whole_region;
73

74
  whole_region = gegl_operation_source_get_bounding_box (operation, "input");
75 76 77

  if (whole_region != NULL)
    {
78 79 80 81 82 83 84
     gdouble center_x, center_y;

     center_x = gegl_coordinate_relative_to_pixel (
                  o->center_x, whole_region->width);
     center_y = gegl_coordinate_relative_to_pixel (
                  o->center_y, whole_region->height);

85
      op_area->left = op_area->right
86 87
        = MAX (fabs (whole_region->x - center_x),
               fabs (whole_region->width + whole_region->x - center_x)) * fabs (o->factor) +1;
88 89

      op_area->top = op_area->bottom
90 91
        = MAX (fabs (whole_region->y - center_y),
               fabs (whole_region->height + whole_region->y - center_y)) * fabs (o->factor) +1;
92 93 94 95 96 97 98 99 100 101 102 103 104
    }
  else
    {
      op_area->left   =
      op_area->right  =
      op_area->top    =
      op_area->bottom = 0;
    }

  gegl_operation_set_format (operation, "input",  babl_format ("RaGaBaA float"));
  gegl_operation_set_format (operation, "output", babl_format ("RaGaBaA float"));
}

105 106
static inline gfloat *
get_pixel_color (gfloat              *in_buf,
107
                 const GeglRectangle *rect,
108 109
                 gint                 x,
                 gint                 y)
110 111 112
{
  gint ix = x - rect->x;
  gint iy = y - rect->y;
113

114 115 116 117 118 119 120 121 122 123 124 125 126 127
  ix = CLAMP (ix, 0, rect->width  - 1);
  iy = CLAMP (iy, 0, rect->height - 1);

  return &in_buf[(iy * rect->width + ix) * 4];
}


static gboolean
process (GeglOperation       *operation,
         GeglBuffer          *input,
         GeglBuffer          *output,
         const GeglRectangle *roi,
         gint                 level)
{
128
  GeglOperationAreaFilter *op_area = GEGL_OPERATION_AREA_FILTER (operation);
129
  GeglProperties          *o       = GEGL_PROPERTIES (operation);
130 131 132
  gfloat                  *in_buf, *out_buf, *out_pixel;
  gint                     x, y;
  GeglRectangle            src_rect;
133

134 135 136 137 138 139 140 141 142
  GeglRectangle           *whole_region;
  gdouble                  center_x, center_y;

  whole_region = gegl_operation_source_get_bounding_box (operation, "input");
  center_x = gegl_coordinate_relative_to_pixel (
                o->center_x, whole_region->width);
  center_y = gegl_coordinate_relative_to_pixel (
                o->center_y, whole_region->width);

143 144 145 146 147 148 149 150 151 152
  src_rect = *roi;
  src_rect.x -= op_area->left;
  src_rect.y -= op_area->top;
  src_rect.width += op_area->left + op_area->right;
  src_rect.height += op_area->top + op_area->bottom;

  in_buf  = g_new  (gfloat, src_rect.width * src_rect.height * 4);
  out_buf = g_new0 (gfloat, roi->width * roi->height * 4);
  out_pixel = out_buf;

153 154
  gegl_buffer_get (input, &src_rect, 1.0, babl_format ("RaGaBaA float"),
                   in_buf, GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
155 156 157 158 159 160 161 162 163 164 165

  for (y = roi->y; y < roi->height + roi->y; ++y)
    {
      for (x = roi->x; x < roi->width + roi->x; ++x)
        {
          gint c, i;
          gfloat dxx, dyy, ix, iy, inv_xy_len;
          gfloat sum[] = {0, 0, 0, 0};

          gfloat x_start = x;
          gfloat y_start = y;
166 167
          gfloat x_end   = x + (center_x - (gfloat) x) * o->factor;
          gfloat y_end   = y + (center_y - (gfloat) y) * o->factor;
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

          gint dist = ceil (sqrt (SQR (x_end - x_start) + SQR (y_end - y_start)) +1);

          /* ensure quality when near the center or with small factor */
          gint xy_len = MAX (dist, 3);

          /* performance concern */
          if (xy_len > NOMINAL_NUM_IT)
            xy_len = MIN (NOMINAL_NUM_IT + (gint) sqrt (xy_len - NOMINAL_NUM_IT), MAX_NUM_IT);

          inv_xy_len = 1.0 / (gfloat) xy_len;

          dxx = (x_end - x_start) * inv_xy_len;
          dyy = (y_end - y_start) * inv_xy_len;

          ix  = x_start;
          iy  = y_start;

          for (i = 0; i < xy_len; i++)
            {
              gfloat dx = ix - floor (ix);
              gfloat dy = iy - floor (iy);

              /* do bilinear interpolation to get a nice smooth result */
              gfloat *pix0, *pix1, *pix2, *pix3;
              gfloat mixy0[4];
              gfloat mixy1[4];

              pix0 = get_pixel_color (in_buf, &src_rect, ix, iy);
              pix1 = get_pixel_color (in_buf, &src_rect, ix+1, iy);
              pix2 = get_pixel_color (in_buf, &src_rect, ix, iy+1);
              pix3 = get_pixel_color (in_buf, &src_rect, ix+1, iy+1);

              for (c = 0; c < 4; ++c)
                {
                  mixy0[c] = dy * (pix2[c] - pix0[c]) + pix0[c];
                  mixy1[c] = dy * (pix3[c] - pix1[c]) + pix1[c];

                  sum[c] +=  dx * (mixy1[c] - mixy0[c]) + mixy0[c];
                }

              ix += dxx;
              iy += dyy;
            }

          for (c = 0; c < 4; ++c)
            *out_pixel++ = sum[c] * inv_xy_len;
        }
    }

218
  gegl_buffer_set (output, roi, 0, babl_format ("RaGaBaA float"),
219
                   out_buf, GEGL_AUTO_ROWSTRIDE);
220 221 222 223 224 225 226 227

  g_free (in_buf);
  g_free (out_buf);

  return  TRUE;
}

static void
228
gegl_op_class_init (GeglOpClass *klass)
229 230 231 232 233 234 235 236
{
  GeglOperationClass       *operation_class;
  GeglOperationFilterClass *filter_class;

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

  operation_class->prepare = prepare;
237
  filter_class->process    = process;
238 239

  gegl_operation_class_set_keys (operation_class,
240
      "name",               "gegl:motion-blur-zoom",
241
      "title",              _("Zooming Motion Blur"),
242 243
      "categories",         "blur",
      "position-dependent", "true",
244
      "license",            "GPL3+",
245 246
      "description", _("Zoom motion blur"),
      NULL);
247 248 249
}

#endif