red-eye-removal.c 4.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 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/>.
 *
 * This plugin is used for removing the red-eye effect
 * that occurs in flash photos.
 *
 * Based on a GIMP 1.2 Perl plugin by Geoff Kuenning
 *
21 22
 * Copyright (C) 2004 Robert Merkel <robert.merkel@benambra.org>
 * Copyright (C) 2006 Andreas Røsdal <andrearo@stud.ntnu.no>
23 24 25 26 27 28
 * Copyright (C) 2011 Robert Sasu <sasu.robert@gmail.com>
 */

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

29
#ifdef GEGL_PROPERTIES
30

31 32 33
property_double (threshold, _("Threshold"), 0.4)
    description (_("Red eye threshold"))
    value_range (0, 0.8)
34 35 36

#else

37 38
#define GEGL_OP_POINT_FILTER
#define GEGL_OP_C_FILE "red-eye-removal.c"
39

40
#include "gegl-op.h"
41 42 43 44 45 46 47
#include <stdio.h>
#include <math.h>

#define RED_FACTOR    0.5133333
#define GREEN_FACTOR  1
#define BLUE_FACTOR   0.1933333

48 49
static void
prepare (GeglOperation *operation)
50
{
51
  gegl_operation_set_format (operation, "input",
52
                             babl_format ("R'G'B'A float"));
53
  gegl_operation_set_format (operation, "output",
54
                             babl_format ("R'G'B'A float"));
55 56 57
}

static void
58
red_eye_reduction (gfloat *buf,
59 60
                   gfloat  threshold)
{
61 62 63
  gfloat adjusted_red       = buf[0] * RED_FACTOR;
  gfloat adjusted_green     = buf[1] * GREEN_FACTOR;
  gfloat adjusted_blue      = buf[2] * BLUE_FACTOR;
64
  gfloat adjusted_threshold = (threshold - 0.4) * 2;
65
  gfloat tmp;
66 67

  if (adjusted_red >= adjusted_green - adjusted_threshold &&
68
      adjusted_red >= adjusted_blue  - adjusted_threshold)
69
    {
70 71
      tmp = (gdouble) (adjusted_green + adjusted_blue) / (2.0 * RED_FACTOR);
      buf[0] = CLAMP (tmp, 0.0, 1.0);
72
    }
73
  /* Otherwise, leave the red channel alone */
74 75 76 77
}

static gboolean
process (GeglOperation       *operation,
78 79 80 81
         void                *in_buf,
         void                *out_buf,
         glong                n_pixels,
         const GeglRectangle *roi,
82
         gint                 level)
83
{
84
  GeglProperties *o = GEGL_PROPERTIES (operation);
85

86 87
  gfloat *dest = out_buf;
  glong   i;
88

89 90 91 92 93
  /*
   * Initialize the dest buffer to the input buffer
   * (we only want to change the red component)
   */
  memcpy (out_buf, in_buf, sizeof (gfloat) * 4 * n_pixels);
94

95 96 97 98 99
  for (i = n_pixels; i > 0; i--)
    {
      red_eye_reduction (dest, o->threshold);
      dest += 4;
    }
100

101
  return TRUE;
102 103
}

104 105 106
#include "opencl/gegl-cl.h"
#include "opencl/red-eye-removal.cl.h"

107
static GeglClRunData *cl_data = NULL;
108 109 110 111 112 113 114 115 116

static gboolean
cl_process (GeglOperation       *operation,
            cl_mem              in,
            cl_mem              out,
            size_t              global_worksize,
            const GeglRectangle *roi,
            gint                level)
{
117
  GeglProperties *o           = GEGL_PROPERTIES (operation);
118 119
  cl_float   threshold    = o->threshold;

120 121 122 123 124 125 126 127 128

  if (!cl_data)
    {
      const char *kernel_name[] = {"cl_red_eye_removal", NULL};
      cl_data = gegl_cl_compile_and_build(red_eye_removal_cl_source, kernel_name);
    }

  if (!cl_data)
    return TRUE;
129 130 131 132

  {
  cl_int cl_err = 0;

133 134 135 136 137 138
  gegl_cl_set_kernel_args (cl_data->kernel[0],
                           sizeof(cl_mem),   &in,
                           sizeof(cl_mem),   &out,
                           sizeof(cl_float), &threshold,
                           NULL);
  CL_CHECK;
139

140 141 142 143
  cl_err = gegl_clEnqueueNDRangeKernel (gegl_cl_get_command_queue (),
                                        cl_data->kernel[0], 1,
                                        NULL, &global_worksize, NULL,
                                        0, NULL, NULL);
144 145 146 147 148 149 150 151 152
  CL_CHECK;
  }

  return  FALSE;

error:
  return TRUE;
}

153
static void
154
gegl_op_class_init (GeglOpClass *klass)
155
{
156 157
  GeglOperationClass            *operation_class;
  GeglOperationPointFilterClass *point_filter_class;
158

159 160
  operation_class    = GEGL_OPERATION_CLASS (klass);
  point_filter_class = GEGL_OPERATION_POINT_FILTER_CLASS (klass);
161

162
  operation_class->prepare    = prepare;
163
  operation_class->opencl_support = TRUE;
164
  point_filter_class->process = process;
165
  point_filter_class->cl_process  = cl_process;
166

167
  gegl_operation_class_set_keys (operation_class,
168
    "name",        "gegl:red-eye-removal",
169
    "title",       _("Red Eye Removal"),
170
    "categories",  "enhance",
171
    "license",     "GPL3+",
172
    "description", _("Remove the red eye effect caused by camera flashes"),
173
    NULL);
174 175 176
}

#endif