~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to plug-ins/common/redeye.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GIMP - The GNU Image Manipulation Program
 
2
 * Red Eye Plug-in for the GIMP.
 
3
 *
 
4
 * Copyright (C) 2004  Robert Merkel <robert.merkel@benambra.org>
 
5
 * Copyright (C) 2006  Andreas Røsdal <andrearo@stud.ntnu.no>
 
6
 *
 
7
 * All Rights Reserved.
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU General Public License as published by
 
11
 * the Free Software Foundation; either version 2 of the License, or
 
12
 * (at your option) any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with this program; if not, write to the Free Software
 
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
22
 *
 
23
 * The GNU General Public License is also available from
 
24
 * http://www.fsf.org/copyleft/gpl.html
 
25
 *
 
26
 * This plugin is used for removing the red-eye effect
 
27
 * that occurs in flash photos.
 
28
 *
 
29
 * Based on a GIMP 1.2 Perl plugin by Geoff Kuenning
 
30
 *
 
31
 * Version History:
 
32
 * 0.1 - initial preliminary release, single file, only documentation
 
33
 *       in the header comments, 2004-05-26
 
34
 * 0.2 - Bugfix of red levels, improved documentation.
 
35
 * 0.3 - Add internationalization support, preview widget,
 
36
 *       improve red eye algorithm threshold and documentation.
 
37
 *
 
38
 */
 
39
 
 
40
#include "config.h"
 
41
 
 
42
#include <string.h>
 
43
 
 
44
#include <libgimp/gimp.h>
 
45
#include <libgimp/gimpui.h>
 
46
 
 
47
#include "libgimp/stdplugins-intl.h"
 
48
 
 
49
 
 
50
/* Declare local functions.
 
51
 */
 
52
static void     query                 (void);
 
53
static void     run                   (const gchar      *name,
 
54
                                       gint              nparams,
 
55
                                       const GimpParam  *param,
 
56
                                       gint             *nreturn_vals,
 
57
                                       GimpParam       **return_vals);
 
58
 
 
59
static void     remove_redeye         (GimpDrawable     *drawable);
 
60
static void     remove_redeye_preview (GimpDrawable     *drawable,
 
61
                                       GimpPreview      *preview);
 
62
static void      redeye_inner_loop    (const guchar     *src,
 
63
                                       guchar           *dest,
 
64
                                       gint              width,
 
65
                                       gint              height,
 
66
                                       gint              bpp,
 
67
                                       gboolean          has_alpha,
 
68
                                       int               rowstride);
 
69
 
 
70
 
 
71
#define RED_FACTOR    0.5133333
 
72
#define GREEN_FACTOR  1
 
73
#define BLUE_FACTOR   0.1933333
 
74
 
 
75
#define SCALE_WIDTH   100
 
76
 
 
77
#define PLUG_IN_PROC    "plug-in-red-eye-removal"
 
78
#define PLUG_IN_BINARY  "redeye"
 
79
 
 
80
const GimpPlugInInfo PLUG_IN_INFO =
 
81
{
 
82
  NULL,  /* init_proc  */
 
83
  NULL,  /* quit_proc  */
 
84
  query, /* query_proc */
 
85
  run,   /* run_proc   */
 
86
};
 
87
 
 
88
gint threshold = 50;
 
89
gboolean preview_toggle = TRUE;
 
90
 
 
91
MAIN ()
 
92
 
 
93
static void
 
94
query (void)
 
95
{
 
96
  static const GimpParamDef args[] =
 
97
  {
 
98
    { GIMP_PDB_INT32,    "run-mode",  "Interactive, non-interactive" },
 
99
    { GIMP_PDB_IMAGE,    "image",     "Input image"                  },
 
100
    { GIMP_PDB_DRAWABLE, "drawable",  "Input drawable"               },
 
101
    { GIMP_PDB_INT32,    "threshold", "Red eye threshold in percent" }
 
102
  };
 
103
 
 
104
  gimp_set_data (PLUG_IN_PROC, &threshold, sizeof (threshold));
 
105
 
 
106
  gimp_install_procedure (PLUG_IN_PROC,
 
107
                          N_("Remove the red eye effect caused by camera "
 
108
                             "flashes"),
 
109
                          "This plug-in removes the red eye effect caused by "
 
110
                          "camera flashes by using a percentage based red "
 
111
                          "color threshold.  Make a selection containing the "
 
112
                          "eyes, and apply the filter while adjusting the "
 
113
                          "threshold to accurately remove the red eyes.",
 
114
                          "Robert Merkel <robert.merkel@benambra.org>, "
 
115
                          "Andreas Røsdal <andrearo@stud.ntnu.no>",
 
116
                          "Copyright 2004-2006 Robert Merkel, Andreas Røsdal",
 
117
                          "2006",
 
118
                          N_("_Red Eye Removal..."),
 
119
                          "RGB*",
 
120
                          GIMP_PLUGIN,
 
121
                          G_N_ELEMENTS (args), 0,
 
122
                          args, NULL);
 
123
 
 
124
  gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Filters/Enhance");
 
125
}
 
126
 
 
127
 
 
128
/*
 
129
 * Create dialog for red eye removal
 
130
 */
 
131
static gboolean
 
132
dialog (gint32        image_id,
 
133
        GimpDrawable *drawable,
 
134
        gint         *current_threshold)
 
135
{
 
136
  GtkWidget *dialog;
 
137
  GtkWidget *preview;
 
138
  GtkWidget *table;
 
139
  GtkWidget *main_vbox;
 
140
  GtkObject *adj;
 
141
  gboolean   run = FALSE;
 
142
 
 
143
  gimp_ui_init (PLUG_IN_BINARY, TRUE);
 
144
 
 
145
  dialog = gimp_dialog_new (_("Red Eye Removal"), PLUG_IN_BINARY,
 
146
                            NULL, 0,
 
147
                            gimp_standard_help_func, PLUG_IN_PROC,
 
148
                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
149
                            GTK_STOCK_OK,     GTK_RESPONSE_OK,
 
150
                            NULL);
 
151
 
 
152
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
 
153
                                           GTK_RESPONSE_OK,
 
154
                                           GTK_RESPONSE_CANCEL,
 
155
                                           -1);
 
156
 
 
157
  gimp_window_set_transient (GTK_WINDOW (dialog));
 
158
 
 
159
  main_vbox = gtk_vbox_new (FALSE, 12);
 
160
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
 
161
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), main_vbox);
 
162
  gtk_widget_show (main_vbox);
 
163
 
 
164
  preview = gimp_zoom_preview_new (drawable);
 
165
  gtk_box_pack_start (GTK_BOX (main_vbox), preview, TRUE, TRUE, 0);
 
166
  gtk_widget_show (preview);
 
167
  table = gtk_table_new (1, 3, FALSE);
 
168
  gtk_table_set_col_spacings (GTK_TABLE (table), 6);
 
169
  gtk_box_pack_start (GTK_BOX (main_vbox), table, FALSE, FALSE, 0);
 
170
  gtk_widget_show (table);
 
171
 
 
172
  adj = gimp_scale_entry_new (GTK_TABLE (table), 1, 0,
 
173
                             _("_Threshold:"),
 
174
                             SCALE_WIDTH, 0,
 
175
                             threshold,
 
176
                             0, 100, 1, 5, 0,
 
177
                             TRUE,
 
178
                             0, 100,
 
179
                             _("Threshold for the red eye color to remove."),
 
180
                             NULL);
 
181
 
 
182
  if (gimp_selection_is_empty (gimp_drawable_get_image (drawable->drawable_id)))
 
183
    {
 
184
      GtkWidget *hints = gimp_hint_box_new (_("Manually selecting the eyes may "
 
185
                                              "improve the results."));
 
186
 
 
187
      gtk_box_pack_end (GTK_BOX (main_vbox), hints, FALSE, FALSE, 0);
 
188
      gtk_widget_show (hints);
 
189
    }
 
190
 
 
191
  g_signal_connect_swapped (preview, "invalidated",
 
192
                            G_CALLBACK (remove_redeye_preview),
 
193
                            drawable);
 
194
 
 
195
  g_signal_connect (adj, "value-changed",
 
196
                    G_CALLBACK (gimp_int_adjustment_update),
 
197
                    &threshold);
 
198
  g_signal_connect_swapped (adj, "value-changed",
 
199
                            G_CALLBACK (gimp_preview_invalidate),
 
200
                            preview);
 
201
 
 
202
  gtk_widget_show (dialog);
 
203
 
 
204
  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
 
205
 
 
206
  gtk_widget_destroy (dialog);
 
207
 
 
208
  return run;
 
209
}
 
210
 
 
211
static void
 
212
run (const gchar      *name,
 
213
     gint             nparams,
 
214
     const GimpParam  *param,
 
215
     gint             *nreturn_vals,
 
216
     GimpParam        **return_vals)
 
217
{
 
218
  static GimpParam   values[1];
 
219
  GimpDrawable      *drawable;
 
220
  GimpRunMode        run_mode;
 
221
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
 
222
  gint32             threshold;
 
223
  gint32             image_ID;
 
224
 
 
225
  run_mode = param[0].data.d_int32;
 
226
 
 
227
  INIT_I18N ();
 
228
 
 
229
  /*  Get the specified drawable  */
 
230
  drawable = gimp_drawable_get (param[2].data.d_drawable);
 
231
  image_ID = param[1].data.d_image;
 
232
 
 
233
  switch (run_mode)
 
234
    {
 
235
      case GIMP_RUN_NONINTERACTIVE:
 
236
        if (nparams != 4)
 
237
          status = GIMP_PDB_CALLING_ERROR;
 
238
        else
 
239
          threshold = param[3].data.d_int32;
 
240
        break;
 
241
 
 
242
      case GIMP_RUN_INTERACTIVE:
 
243
        gimp_get_data (PLUG_IN_PROC, &threshold);
 
244
 
 
245
        if (strncmp (name, PLUG_IN_PROC, strlen (PLUG_IN_PROC)) == 0)
 
246
          {
 
247
            if (! dialog (image_ID, drawable, &threshold))
 
248
              status = GIMP_PDB_CANCEL;
 
249
          }
 
250
        else
 
251
          {
 
252
            threshold = 50;
 
253
          }
 
254
        break;
 
255
 
 
256
      case GIMP_RUN_WITH_LAST_VALS:
 
257
        gimp_get_data (PLUG_IN_PROC, &threshold);
 
258
        break;
 
259
 
 
260
      default:
 
261
        break;
 
262
    }
 
263
 
 
264
  /*  Make sure that the drawable is RGB color.
 
265
   *  Greyscale images don't have the red eye effect. */
 
266
  if (status == GIMP_PDB_SUCCESS &&
 
267
      gimp_drawable_is_rgb (drawable->drawable_id))
 
268
    {
 
269
      remove_redeye (drawable);
 
270
 
 
271
      if (run_mode != GIMP_RUN_NONINTERACTIVE)
 
272
        gimp_displays_flush ();
 
273
 
 
274
      /* if we ran in auto mode don't reset the threshold */
 
275
      if (run_mode == GIMP_RUN_INTERACTIVE
 
276
          && strncmp (name, PLUG_IN_PROC, strlen (PLUG_IN_PROC)) == 0)
 
277
      {
 
278
        gimp_set_data (PLUG_IN_PROC, &threshold, sizeof (threshold));
 
279
      }
 
280
    }
 
281
  else
 
282
    {
 
283
      status = GIMP_PDB_EXECUTION_ERROR;
 
284
    }
 
285
 
 
286
  *nreturn_vals = 1;
 
287
  *return_vals = values;
 
288
 
 
289
  values[0].type = GIMP_PDB_STATUS;
 
290
  values[0].data.d_status = status;
 
291
 
 
292
  gimp_drawable_detach (drawable);
 
293
}
 
294
 
 
295
 
 
296
/*
 
297
 * Red Eye Removal Alorithm, based on using a threshold to detect
 
298
 * red pixels. Having a user-made selection around the eyes will
 
299
 * prevent incorrect pixels from being selected.
 
300
 */
 
301
static void
 
302
remove_redeye (GimpDrawable *drawable)
 
303
{
 
304
  GimpPixelRgn  src_rgn;
 
305
  GimpPixelRgn  dest_rgn;
 
306
  gint          progress, max_progress;
 
307
  gboolean      has_alpha;
 
308
  gint          x, y;
 
309
  gint          width, height;
 
310
  gint          i;
 
311
  gpointer      pr;
 
312
 
 
313
  if (! gimp_drawable_mask_intersect (drawable->drawable_id,
 
314
                                      &x, &y, &width, &height))
 
315
    return;
 
316
 
 
317
  gimp_progress_init (_("Removing red eye"));
 
318
 
 
319
  has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
 
320
 
 
321
  progress = 0;
 
322
  max_progress = width * height;
 
323
 
 
324
  gimp_pixel_rgn_init (&src_rgn, drawable,
 
325
                       x, y, width, height, FALSE, FALSE);
 
326
  gimp_pixel_rgn_init (&dest_rgn, drawable,
 
327
                       x, y, width, height, TRUE, TRUE);
 
328
 
 
329
  for (pr = gimp_pixel_rgns_register (2, &src_rgn, &dest_rgn), i = 0;
 
330
       pr != NULL;
 
331
       pr = gimp_pixel_rgns_process (pr), i++)
 
332
    {
 
333
      redeye_inner_loop (src_rgn.data, dest_rgn.data, src_rgn.w, src_rgn.h,
 
334
                         src_rgn.bpp, has_alpha, src_rgn.rowstride);
 
335
 
 
336
      progress += src_rgn.w * src_rgn.h;
 
337
 
 
338
      if (i % 16 == 0)
 
339
        gimp_progress_update ((double) progress / (double) max_progress);
 
340
    }
 
341
 
 
342
  gimp_drawable_flush (drawable);
 
343
  gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
 
344
  gimp_drawable_update (drawable->drawable_id, x, y, width, height);
 
345
}
 
346
 
 
347
static void
 
348
remove_redeye_preview (GimpDrawable *drawable,
 
349
                       GimpPreview  *preview)
 
350
{
 
351
  guchar   *src;
 
352
  guchar   *dest;
 
353
  gboolean  has_alpha;
 
354
  gint      width, height;
 
355
  gint      bpp;
 
356
  gint      rowstride;
 
357
 
 
358
  src  = gimp_zoom_preview_get_source (GIMP_ZOOM_PREVIEW (preview),
 
359
                                       &width, &height, &bpp);
 
360
  dest = g_new (guchar, height * width * bpp);
 
361
 
 
362
  has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
 
363
  rowstride = bpp * width;
 
364
 
 
365
  redeye_inner_loop (src, dest, width, height, bpp, has_alpha, rowstride);
 
366
 
 
367
  gimp_preview_draw_buffer (preview, dest, rowstride);
 
368
  g_free (src);
 
369
  g_free (dest);
 
370
}
 
371
 
 
372
static void
 
373
redeye_inner_loop (const guchar *src,
 
374
                   guchar       *dest,
 
375
                   gint          width,
 
376
                   gint          height,
 
377
                   gint          bpp,
 
378
                   gboolean      has_alpha,
 
379
                   int           rowstride)
 
380
{
 
381
  const gint    red   = 0;
 
382
  const gint    green = 1;
 
383
  const gint    blue  = 2;
 
384
  const gint    alpha = 3;
 
385
  gint          x, y;
 
386
 
 
387
  for (y = 0; y < height; y++)
 
388
    {
 
389
      const guchar *s = src;
 
390
      guchar       *d = dest;
 
391
 
 
392
      for (x = 0; x < width; x++)
 
393
        {
 
394
          gint adjusted_red       = s[red] * RED_FACTOR;
 
395
          gint adjusted_green     = s[green] * GREEN_FACTOR;
 
396
          gint adjusted_blue      = s[blue] * BLUE_FACTOR;
 
397
          gint adjusted_threshold = (threshold - 50) * 2;
 
398
 
 
399
          if (adjusted_red >= adjusted_green - adjusted_threshold &&
 
400
              adjusted_red >= adjusted_blue - adjusted_threshold)
 
401
            {
 
402
              d[red] = ((gdouble) (adjusted_green + adjusted_blue)
 
403
                        / (2.0  * RED_FACTOR));
 
404
            }
 
405
          else
 
406
            {
 
407
              d[red] = s[red];
 
408
            }
 
409
 
 
410
          d[green] = s[green];
 
411
          d[blue] = s[blue];
 
412
 
 
413
          if (has_alpha)
 
414
            d[alpha] = s[alpha];
 
415
 
 
416
          s += bpp;
 
417
          d += bpp;
 
418
        }
 
419
 
 
420
      src += rowstride;
 
421
      dest += rowstride;
 
422
    }
 
423
}