1
/* Deinterlace 1.00 - image processing plug-in for the Gimp
3
* Copyright (C) 1997 Andrew Kieschnick (andrewk@mail.utexas.edu)
5
* Original deinterlace for the Gimp 0.54 API by Federico Mena Quintero
7
* Copyright (C) 1996 Federico Mena Quintero
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.
28
#include <libgimp/gimp.h>
29
#include <libgimp/gimpui.h>
31
#include "libgimp/stdplugins-intl.h"
46
/* Declare local functions.
48
static void query (void);
49
static void run (const gchar *name,
51
const GimpParam *param,
53
GimpParam **return_vals);
56
static void deinterlace (GimpDrawable *drawable,
57
GimpPreview *preview);
59
static gboolean deinterlace_dialog (GimpDrawable *drawable);
62
GimpPlugInInfo PLUG_IN_INFO =
66
query, /* query_proc */
70
static DeinterlaceValues devals =
72
EVEN_FIELDS, /* evenness */
81
static GimpParamDef args[] =
83
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
84
{ GIMP_PDB_IMAGE, "image", "Input image (unused)" },
85
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
86
{ GIMP_PDB_INT32, "evenodd", "0 = keep odd, 1 = keep even" }
89
gimp_install_procedure ("plug_in_deinterlace",
91
"Deinterlace is useful for processing images from "
92
"video capture cards. When only the odd or even "
93
"fields get captured, deinterlace can be used to "
94
"interpolate between the existing fields to correct "
99
N_("_Deinterlace..."),
102
G_N_ELEMENTS (args), 0,
105
gimp_plugin_menu_register ("plug_in_deinterlace", "<Image>/Filters/Enhance");
109
run (const gchar *name,
111
const GimpParam *param,
113
GimpParam **return_vals)
115
static GimpParam values[1];
116
GimpDrawable *drawable;
117
GimpRunMode run_mode;
118
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
120
run_mode = param[0].data.d_int32;
124
/* Get the specified drawable */
125
drawable = gimp_drawable_get (param[2].data.d_drawable);
129
case GIMP_RUN_INTERACTIVE:
130
gimp_get_data ("plug_in_deinterlace", &devals);
131
if (! deinterlace_dialog (drawable))
132
status = GIMP_PDB_EXECUTION_ERROR;
135
case GIMP_RUN_NONINTERACTIVE:
137
status = GIMP_PDB_CALLING_ERROR;
138
if (status == GIMP_PDB_SUCCESS)
139
devals.evenness = param[3].data.d_int32;
142
case GIMP_RUN_WITH_LAST_VALS:
143
gimp_get_data ("plug_in_deinterlace", &devals);
150
if (status == GIMP_PDB_SUCCESS)
152
/* Make sure that the drawable is gray or RGB color */
153
if (gimp_drawable_is_rgb (drawable->drawable_id) ||
154
gimp_drawable_is_gray (drawable->drawable_id))
156
gimp_progress_init (_("Deinterlace..."));
157
gimp_tile_cache_ntiles (2 * (drawable->width /
158
gimp_tile_width () + 1));
159
deinterlace (drawable, NULL);
161
if (run_mode != GIMP_RUN_NONINTERACTIVE)
162
gimp_displays_flush ();
163
if (run_mode == GIMP_RUN_INTERACTIVE)
164
gimp_set_data ("plug_in_deinterlace",
165
&devals, sizeof (DeinterlaceValues));
169
/* gimp_message ("deinterlace: cannot operate on indexed color images"); */
170
status = GIMP_PDB_EXECUTION_ERROR;
174
*return_vals = values;
176
values[0].type = GIMP_PDB_STATUS;
177
values[0].data.d_status = status;
179
gimp_drawable_detach (drawable);
183
deinterlace (GimpDrawable *drawable,
184
GimpPreview *preview)
186
GimpPixelRgn srcPR, destPR;
189
guchar *dest_buffer = NULL;
197
bytes = drawable->bpp;
201
gimp_preview_get_position (preview, &x, &y);
202
gimp_preview_get_size (preview, &width, &height);
204
dest_buffer = g_new (guchar, width * height * bytes);
211
gimp_drawable_mask_bounds (drawable->drawable_id, &x, &y, &x2, &y2);
216
dest = g_new (guchar, width * bytes);
218
gimp_pixel_rgn_init (&destPR, drawable, x, y, width, height, TRUE, TRUE);
221
gimp_pixel_rgn_init (&srcPR, drawable,
223
width, MIN (height + 1, drawable->height),
226
has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
228
/* allocate row buffers */
229
upper = g_new (guchar, width * bytes);
230
lower = g_new (guchar, width * bytes);
232
/* loop through the rows, performing our magic */
233
for (row = y; row < y + height; row++)
235
gimp_pixel_rgn_get_row (&srcPR, dest, x, row, width);
237
/* Only do interpolation if the row:
238
* (1) Isn't one we want to keep
239
* (2) Has both an upper and a lower row
240
* Otherwise, just duplicate the source row
242
if ((row % 2 != devals.evenness) &&
243
(row - 1 >= 0) && (row + 1 < drawable->height))
245
gimp_pixel_rgn_get_row (&srcPR, upper, x, row - 1, width);
246
gimp_pixel_rgn_get_row (&srcPR, lower, x, row + 1, width);
250
const guchar *upix = upper;
251
const guchar *lpix = lower;
254
for (col = 0; col < width; col++)
256
guint ualpha = upix[bytes - 1];
257
guint lalpha = lpix[bytes - 1];
258
guint alpha = ualpha + lalpha;
260
if ((dpix[bytes - 1] = (alpha >> 1)))
264
for (b = 0; b < bytes - 1; b++)
265
dpix[b] = (upix[b] * ualpha + lpix[b] * lalpha) / alpha;
275
for (col = 0; col < width * bytes; col++)
276
dest[col] = ((guint) upper[col] + (guint) lower[col]) / 2;
282
dest += width * bytes;
286
gimp_pixel_rgn_set_row (&destPR, dest, x, row, width);
289
gimp_progress_update ((double) row / (double) (height));
295
gimp_preview_draw_buffer (preview, dest_buffer, width * bytes);
300
/* update the deinterlaced region */
301
gimp_drawable_flush (drawable);
302
gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
303
gimp_drawable_update (drawable->drawable_id, x, y, width, height);
312
deinterlace_dialog (GimpDrawable *drawable)
315
GtkWidget *main_vbox;
322
gimp_ui_init ("deinterlace", FALSE);
324
dialog = gimp_dialog_new (_("Deinterlace"), "deinterlace",
326
gimp_standard_help_func, "plug-in-deinterlace",
328
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
329
GTK_STOCK_OK, GTK_RESPONSE_OK,
333
main_vbox = gtk_vbox_new (FALSE, 12);
334
gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
335
gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), main_vbox);
336
gtk_widget_show (main_vbox);
338
preview = gimp_drawable_preview_new (drawable, &devals.preview);
339
gtk_box_pack_start_defaults (GTK_BOX (main_vbox), preview);
340
gtk_widget_show (preview);
341
g_signal_connect_swapped (preview, "invalidated",
342
G_CALLBACK (deinterlace),
345
frame = gimp_int_radio_group_new (FALSE, NULL,
346
G_CALLBACK (gimp_radio_button_update),
347
&devals.evenness, devals.evenness,
349
_("Keep o_dd fields"), ODD_FIELDS, &odd,
350
_("Keep _even fields"), EVEN_FIELDS, &even,
354
g_signal_connect_swapped (odd, "toggled",
355
G_CALLBACK (gimp_preview_invalidate),
357
g_signal_connect_swapped (even, "toggled",
358
G_CALLBACK (gimp_preview_invalidate),
361
gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
362
gtk_widget_show (frame);
364
gtk_widget_show (dialog);
366
run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
368
gtk_widget_destroy (dialog);