~ubuntu-branches/ubuntu/breezy/gimp/breezy

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-10-04 19:04:46 UTC
  • Revision ID: james.westby@ubuntu.com-20051004190446-ukh32kwk56s4sjhu
Tags: upstream-2.2.8
ImportĀ upstreamĀ versionĀ 2.2.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Deinterlace 1.00 - image processing plug-in for the Gimp
 
2
 *
 
3
 * Copyright (C) 1997 Andrew Kieschnick (andrewk@mail.utexas.edu)
 
4
 *
 
5
 * Original deinterlace for the Gimp 0.54 API by Federico Mena Quintero
 
6
 *
 
7
 * Copyright (C) 1996 Federico Mena Quintero
 
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
 
 
24
#include "config.h"
 
25
 
 
26
#include <gtk/gtk.h>
 
27
 
 
28
#include <libgimp/gimp.h>
 
29
#include <libgimp/gimpui.h>
 
30
 
 
31
#include "libgimp/stdplugins-intl.h"
 
32
 
 
33
 
 
34
enum
 
35
{
 
36
  ODD_FIELDS,
 
37
  EVEN_FIELDS
 
38
};
 
39
 
 
40
typedef struct
 
41
{
 
42
  gint     evenness;
 
43
  gboolean preview;
 
44
} DeinterlaceValues;
 
45
 
 
46
/* Declare local functions.
 
47
 */
 
48
static void      query  (void);
 
49
static void      run    (const gchar      *name,
 
50
                         gint              nparams,
 
51
                         const GimpParam  *param,
 
52
                         gint             *nreturn_vals,
 
53
                         GimpParam       **return_vals);
 
54
 
 
55
 
 
56
static void      deinterlace        (GimpDrawable *drawable,
 
57
                                     GimpPreview  *preview);
 
58
 
 
59
static gboolean  deinterlace_dialog (GimpDrawable *drawable);
 
60
 
 
61
 
 
62
GimpPlugInInfo PLUG_IN_INFO =
 
63
{
 
64
  NULL,  /* init_proc  */
 
65
  NULL,  /* quit_proc  */
 
66
  query, /* query_proc */
 
67
  run,   /* run_proc   */
 
68
};
 
69
 
 
70
static DeinterlaceValues devals =
 
71
{
 
72
  EVEN_FIELDS,  /* evenness */
 
73
  TRUE          /* preview */
 
74
};
 
75
 
 
76
MAIN ()
 
77
 
 
78
static void
 
79
query (void)
 
80
{
 
81
  static GimpParamDef args[] =
 
82
  {
 
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"  }
 
87
  };
 
88
 
 
89
  gimp_install_procedure ("plug_in_deinterlace",
 
90
                          "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 "
 
95
                          "this.",
 
96
                          "Andrew Kieschnick",
 
97
                          "Andrew Kieschnick",
 
98
                          "1997",
 
99
                          N_("_Deinterlace..."),
 
100
                          "RGB*, GRAY*",
 
101
                          GIMP_PLUGIN,
 
102
                          G_N_ELEMENTS (args), 0,
 
103
                          args, NULL);
 
104
 
 
105
  gimp_plugin_menu_register ("plug_in_deinterlace", "<Image>/Filters/Enhance");
 
106
}
 
107
 
 
108
static void
 
109
run (const gchar      *name,
 
110
     gint              nparams,
 
111
     const GimpParam  *param,
 
112
     gint             *nreturn_vals,
 
113
     GimpParam       **return_vals)
 
114
{
 
115
  static GimpParam   values[1];
 
116
  GimpDrawable      *drawable;
 
117
  GimpRunMode        run_mode;
 
118
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
 
119
 
 
120
  run_mode = param[0].data.d_int32;
 
121
 
 
122
  INIT_I18N ();
 
123
 
 
124
  /*  Get the specified drawable  */
 
125
  drawable = gimp_drawable_get (param[2].data.d_drawable);
 
126
 
 
127
  switch (run_mode)
 
128
    {
 
129
    case GIMP_RUN_INTERACTIVE:
 
130
      gimp_get_data ("plug_in_deinterlace", &devals);
 
131
      if (! deinterlace_dialog (drawable))
 
132
        status = GIMP_PDB_EXECUTION_ERROR;
 
133
      break;
 
134
 
 
135
    case GIMP_RUN_NONINTERACTIVE:
 
136
      if (nparams != 4)
 
137
        status = GIMP_PDB_CALLING_ERROR;
 
138
      if (status == GIMP_PDB_SUCCESS)
 
139
        devals.evenness = param[3].data.d_int32;
 
140
      break;
 
141
 
 
142
    case GIMP_RUN_WITH_LAST_VALS:
 
143
      gimp_get_data ("plug_in_deinterlace", &devals);
 
144
      break;
 
145
 
 
146
    default:
 
147
      break;
 
148
    }
 
149
 
 
150
  if (status == GIMP_PDB_SUCCESS)
 
151
    {
 
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))
 
155
        {
 
156
          gimp_progress_init (_("Deinterlace..."));
 
157
          gimp_tile_cache_ntiles (2 * (drawable->width /
 
158
                                       gimp_tile_width () + 1));
 
159
          deinterlace (drawable, NULL);
 
160
 
 
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));
 
166
        }
 
167
      else
 
168
        {
 
169
          /* gimp_message ("deinterlace: cannot operate on indexed color images"); */
 
170
          status = GIMP_PDB_EXECUTION_ERROR;
 
171
        }
 
172
    }
 
173
  *nreturn_vals = 1;
 
174
  *return_vals = values;
 
175
 
 
176
  values[0].type = GIMP_PDB_STATUS;
 
177
  values[0].data.d_status = status;
 
178
 
 
179
  gimp_drawable_detach (drawable);
 
180
}
 
181
 
 
182
static void
 
183
deinterlace (GimpDrawable *drawable,
 
184
             GimpPreview  *preview)
 
185
{
 
186
  GimpPixelRgn  srcPR, destPR;
 
187
  gboolean      has_alpha;
 
188
  guchar       *dest;
 
189
  guchar       *dest_buffer = NULL;
 
190
  guchar       *upper;
 
191
  guchar       *lower;
 
192
  gint          row, col;
 
193
  gint          x, y;
 
194
  gint          width, height;
 
195
  gint          bytes;
 
196
 
 
197
  bytes = drawable->bpp;
 
198
 
 
199
  if (preview)
 
200
    {
 
201
      gimp_preview_get_position (preview, &x, &y);
 
202
      gimp_preview_get_size (preview, &width, &height);
 
203
 
 
204
      dest_buffer = g_new (guchar, width * height * bytes);
 
205
      dest = dest_buffer;
 
206
    }
 
207
  else
 
208
    {
 
209
      gint x2, y2;
 
210
 
 
211
      gimp_drawable_mask_bounds (drawable->drawable_id, &x, &y, &x2, &y2);
 
212
 
 
213
      width  = x2 - x;
 
214
      height = y2 - y;
 
215
 
 
216
      dest = g_new (guchar, width * bytes);
 
217
 
 
218
      gimp_pixel_rgn_init (&destPR, drawable, x, y, width, height, TRUE, TRUE);
 
219
    }
 
220
 
 
221
  gimp_pixel_rgn_init (&srcPR, drawable,
 
222
                       x, MAX (y - 1, 0),
 
223
                       width, MIN (height + 1, drawable->height),
 
224
                       FALSE, FALSE);
 
225
 
 
226
  has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
 
227
 
 
228
  /*  allocate row buffers  */
 
229
  upper = g_new (guchar, width * bytes);
 
230
  lower = g_new (guchar, width * bytes);
 
231
 
 
232
  /*  loop through the rows, performing our magic  */
 
233
  for (row = y; row < y + height; row++)
 
234
    {
 
235
      gimp_pixel_rgn_get_row (&srcPR, dest, x, row, width);
 
236
 
 
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
 
241
       */
 
242
      if ((row % 2 != devals.evenness) &&
 
243
          (row - 1 >= 0) && (row + 1 < drawable->height))
 
244
        {
 
245
          gimp_pixel_rgn_get_row (&srcPR, upper, x, row - 1, width);
 
246
          gimp_pixel_rgn_get_row (&srcPR, lower, x, row + 1, width);
 
247
 
 
248
          if (has_alpha)
 
249
            {
 
250
              const guchar *upix = upper;
 
251
              const guchar *lpix = lower;
 
252
              guchar       *dpix = dest;
 
253
 
 
254
              for (col = 0; col < width; col++)
 
255
                {
 
256
                  guint ualpha = upix[bytes - 1];
 
257
                  guint lalpha = lpix[bytes - 1];
 
258
                  guint alpha  = ualpha + lalpha;
 
259
 
 
260
                  if ((dpix[bytes - 1] = (alpha >> 1)))
 
261
                    {
 
262
                      gint b;
 
263
 
 
264
                      for (b = 0; b < bytes - 1; b++)
 
265
                        dpix[b] = (upix[b] * ualpha + lpix[b] * lalpha) / alpha;
 
266
                    }
 
267
 
 
268
                  upix += bytes;
 
269
                  lpix += bytes;
 
270
                  dpix += bytes;
 
271
                }
 
272
            }
 
273
          else
 
274
            {
 
275
              for (col = 0; col < width * bytes; col++)
 
276
                dest[col] = ((guint) upper[col] + (guint) lower[col]) / 2;
 
277
            }
 
278
        }
 
279
 
 
280
      if (preview)
 
281
        {
 
282
          dest += width * bytes;
 
283
        }
 
284
      else
 
285
        {
 
286
          gimp_pixel_rgn_set_row (&destPR, dest, x, row, width);
 
287
 
 
288
          if ((row % 5) == 0)
 
289
            gimp_progress_update ((double) row / (double) (height));
 
290
        }
 
291
    }
 
292
 
 
293
  if (preview)
 
294
    {
 
295
      gimp_preview_draw_buffer (preview, dest_buffer, width * bytes);
 
296
      dest = dest_buffer;
 
297
    }
 
298
  else
 
299
    {
 
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);
 
304
    }
 
305
 
 
306
  g_free (lower);
 
307
  g_free (upper);
 
308
  g_free (dest);
 
309
}
 
310
 
 
311
static gboolean
 
312
deinterlace_dialog (GimpDrawable *drawable)
 
313
{
 
314
  GtkWidget *dialog;
 
315
  GtkWidget *main_vbox;
 
316
  GtkWidget *preview;
 
317
  GtkWidget *frame;
 
318
  GtkWidget *odd;
 
319
  GtkWidget *even;
 
320
  gboolean   run;
 
321
 
 
322
  gimp_ui_init ("deinterlace", FALSE);
 
323
 
 
324
  dialog = gimp_dialog_new (_("Deinterlace"), "deinterlace",
 
325
                            NULL, 0,
 
326
                            gimp_standard_help_func, "plug-in-deinterlace",
 
327
 
 
328
                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
329
                            GTK_STOCK_OK,     GTK_RESPONSE_OK,
 
330
 
 
331
                            NULL);
 
332
 
 
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);
 
337
 
 
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),
 
343
                            drawable);
 
344
 
 
345
  frame = gimp_int_radio_group_new (FALSE, NULL,
 
346
                                    G_CALLBACK (gimp_radio_button_update),
 
347
                                    &devals.evenness, devals.evenness,
 
348
 
 
349
                                    _("Keep o_dd fields"),  ODD_FIELDS,  &odd,
 
350
                                    _("Keep _even fields"), EVEN_FIELDS, &even,
 
351
 
 
352
                                    NULL);
 
353
 
 
354
  g_signal_connect_swapped (odd, "toggled",
 
355
                            G_CALLBACK (gimp_preview_invalidate),
 
356
                            preview);
 
357
  g_signal_connect_swapped (even, "toggled",
 
358
                            G_CALLBACK (gimp_preview_invalidate),
 
359
                            preview);
 
360
 
 
361
  gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
 
362
  gtk_widget_show (frame);
 
363
 
 
364
  gtk_widget_show (dialog);
 
365
 
 
366
  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
 
367
 
 
368
  gtk_widget_destroy (dialog);
 
369
 
 
370
  return run;
 
371
}