1
/* GIMP - The GNU Image Manipulation Program
2
* Red Eye Plug-in for the GIMP.
4
* Copyright (C) 2004 Robert Merkel <robert.merkel@benambra.org>
5
* Copyright (C) 2006 Andreas Røsdal <andrearo@stud.ntnu.no>
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.
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.
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.
23
* The GNU General Public License is also available from
24
* http://www.fsf.org/copyleft/gpl.html
26
* This plugin is used for removing the red-eye effect
27
* that occurs in flash photos.
29
* Based on a GIMP 1.2 Perl plugin by Geoff Kuenning
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.
44
#include <libgimp/gimp.h>
45
#include <libgimp/gimpui.h>
47
#include "libgimp/stdplugins-intl.h"
50
/* Declare local functions.
52
static void query (void);
53
static void run (const gchar *name,
55
const GimpParam *param,
57
GimpParam **return_vals);
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,
71
#define RED_FACTOR 0.5133333
72
#define GREEN_FACTOR 1
73
#define BLUE_FACTOR 0.1933333
75
#define SCALE_WIDTH 100
77
#define PLUG_IN_PROC "plug-in-red-eye-removal"
78
#define PLUG_IN_BINARY "redeye"
80
const GimpPlugInInfo PLUG_IN_INFO =
84
query, /* query_proc */
89
gboolean preview_toggle = TRUE;
96
static const GimpParamDef args[] =
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" }
104
gimp_set_data (PLUG_IN_PROC, &threshold, sizeof (threshold));
106
gimp_install_procedure (PLUG_IN_PROC,
107
N_("Remove the red eye effect caused by camera "
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",
118
N_("_Red Eye Removal..."),
121
G_N_ELEMENTS (args), 0,
124
gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Filters/Enhance");
129
* Create dialog for red eye removal
132
dialog (gint32 image_id,
133
GimpDrawable *drawable,
134
gint *current_threshold)
139
GtkWidget *main_vbox;
141
gboolean run = FALSE;
143
gimp_ui_init (PLUG_IN_BINARY, TRUE);
145
dialog = gimp_dialog_new (_("Red Eye Removal"), PLUG_IN_BINARY,
147
gimp_standard_help_func, PLUG_IN_PROC,
148
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
149
GTK_STOCK_OK, GTK_RESPONSE_OK,
152
gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
157
gimp_window_set_transient (GTK_WINDOW (dialog));
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);
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);
172
adj = gimp_scale_entry_new (GTK_TABLE (table), 1, 0,
179
_("Threshold for the red eye color to remove."),
182
if (gimp_selection_is_empty (gimp_drawable_get_image (drawable->drawable_id)))
184
GtkWidget *hints = gimp_hint_box_new (_("Manually selecting the eyes may "
185
"improve the results."));
187
gtk_box_pack_end (GTK_BOX (main_vbox), hints, FALSE, FALSE, 0);
188
gtk_widget_show (hints);
191
g_signal_connect_swapped (preview, "invalidated",
192
G_CALLBACK (remove_redeye_preview),
195
g_signal_connect (adj, "value-changed",
196
G_CALLBACK (gimp_int_adjustment_update),
198
g_signal_connect_swapped (adj, "value-changed",
199
G_CALLBACK (gimp_preview_invalidate),
202
gtk_widget_show (dialog);
204
run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
206
gtk_widget_destroy (dialog);
212
run (const gchar *name,
214
const GimpParam *param,
216
GimpParam **return_vals)
218
static GimpParam values[1];
219
GimpDrawable *drawable;
220
GimpRunMode run_mode;
221
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
225
run_mode = param[0].data.d_int32;
229
/* Get the specified drawable */
230
drawable = gimp_drawable_get (param[2].data.d_drawable);
231
image_ID = param[1].data.d_image;
235
case GIMP_RUN_NONINTERACTIVE:
237
status = GIMP_PDB_CALLING_ERROR;
239
threshold = param[3].data.d_int32;
242
case GIMP_RUN_INTERACTIVE:
243
gimp_get_data (PLUG_IN_PROC, &threshold);
245
if (strncmp (name, PLUG_IN_PROC, strlen (PLUG_IN_PROC)) == 0)
247
if (! dialog (image_ID, drawable, &threshold))
248
status = GIMP_PDB_CANCEL;
256
case GIMP_RUN_WITH_LAST_VALS:
257
gimp_get_data (PLUG_IN_PROC, &threshold);
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))
269
remove_redeye (drawable);
271
if (run_mode != GIMP_RUN_NONINTERACTIVE)
272
gimp_displays_flush ();
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)
278
gimp_set_data (PLUG_IN_PROC, &threshold, sizeof (threshold));
283
status = GIMP_PDB_EXECUTION_ERROR;
287
*return_vals = values;
289
values[0].type = GIMP_PDB_STATUS;
290
values[0].data.d_status = status;
292
gimp_drawable_detach (drawable);
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.
302
remove_redeye (GimpDrawable *drawable)
304
GimpPixelRgn src_rgn;
305
GimpPixelRgn dest_rgn;
306
gint progress, max_progress;
313
if (! gimp_drawable_mask_intersect (drawable->drawable_id,
314
&x, &y, &width, &height))
317
gimp_progress_init (_("Removing red eye"));
319
has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
322
max_progress = width * height;
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);
329
for (pr = gimp_pixel_rgns_register (2, &src_rgn, &dest_rgn), i = 0;
331
pr = gimp_pixel_rgns_process (pr), i++)
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);
336
progress += src_rgn.w * src_rgn.h;
339
gimp_progress_update ((double) progress / (double) max_progress);
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);
348
remove_redeye_preview (GimpDrawable *drawable,
349
GimpPreview *preview)
358
src = gimp_zoom_preview_get_source (GIMP_ZOOM_PREVIEW (preview),
359
&width, &height, &bpp);
360
dest = g_new (guchar, height * width * bpp);
362
has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
363
rowstride = bpp * width;
365
redeye_inner_loop (src, dest, width, height, bpp, has_alpha, rowstride);
367
gimp_preview_draw_buffer (preview, dest, rowstride);
373
redeye_inner_loop (const guchar *src,
382
const gint green = 1;
384
const gint alpha = 3;
387
for (y = 0; y < height; y++)
389
const guchar *s = src;
392
for (x = 0; x < width; x++)
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;
399
if (adjusted_red >= adjusted_green - adjusted_threshold &&
400
adjusted_red >= adjusted_blue - adjusted_threshold)
402
d[red] = ((gdouble) (adjusted_green + adjusted_blue)
403
/ (2.0 * RED_FACTOR));