home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / common / deinterlace.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-28  |  8.3 KB  |  306 lines

  1. /* Deinterlace 1.00 - image processing plug-in for the Gimp 1.0 API
  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.  * Any bugs in this code are probably my (Andrew Kieschnick's) fault, as I
  10.  * pretty much rewrote it from scratch.
  11.  *
  12.  * This program is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program; if not, write to the Free Software
  24.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25.  */
  26.  
  27. #include "config.h"
  28.  
  29. #include <stdlib.h>
  30.  
  31. #include <gtk/gtk.h>
  32.  
  33. #include <libgimp/gimp.h>
  34. #include <libgimp/gimpui.h>
  35.  
  36. #include "libgimp/stdplugins-intl.h"
  37.  
  38.  
  39. enum
  40. {
  41.   ODD_FIELDS,
  42.   EVEN_FIELDS
  43. };
  44.  
  45. /* Declare local functions.
  46.  */
  47. static void      query  (void);
  48. static void      run    (gchar     *name,
  49.              gint       nparams,
  50.              GimpParam    *param,
  51.              gint      *nreturn_vals,
  52.              GimpParam   **return_vals);
  53.  
  54. static void      deinterlace        (GimpDrawable  *drawable);
  55.  
  56. static gint      deinterlace_dialog (void);
  57.  
  58. GimpPlugInInfo PLUG_IN_INFO =
  59. {
  60.   NULL,  /* init_proc  */
  61.   NULL,  /* quit_proc  */
  62.   query, /* query_proc */
  63.   run,   /* run_proc   */
  64. };
  65.  
  66. static gint DeinterlaceValue = EVEN_FIELDS;
  67.  
  68. MAIN ()
  69.  
  70. static void
  71. query (void)
  72. {
  73.   static GimpParamDef args[] =
  74.   {
  75.     { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
  76.     { GIMP_PDB_IMAGE, "image", "Input image (unused)" },
  77.     { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
  78.     { GIMP_PDB_INT32, "evenodd", "0 = keep odd, 1 = keep even" }
  79.   };
  80.   static gint nargs = sizeof (args) / sizeof (args[0]);
  81.  
  82.   gimp_install_procedure ("plug_in_deinterlace",
  83.               "Deinterlace",
  84.               "Deinterlace is useful for processing images from "
  85.               "video capture cards. When only the odd or even "
  86.               "fields get captured, deinterlace can be used to "
  87.               "interpolate between the existing fields to correct "
  88.               "this.",
  89.               "Andrew Kieschnick",
  90.               "Andrew Kieschnick",
  91.               "1997",
  92.               N_("<Image>/Filters/Enhance/Deinterlace..."),
  93.               "RGB*, GRAY*",
  94.               GIMP_PLUGIN,
  95.               nargs, 0,
  96.               args, NULL);
  97. }
  98.  
  99. static void
  100. run (gchar   *name,
  101.      gint     nparams,
  102.      GimpParam  *param,
  103.      gint    *nreturn_vals,
  104.      GimpParam **return_vals)
  105. {
  106.   static GimpParam values[1];
  107.   GimpDrawable *drawable;
  108.   GimpRunModeType run_mode;
  109.   GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  110.  
  111.   run_mode = param[0].data.d_int32;
  112.  
  113.   /*  Get the specified drawable  */
  114.   drawable = gimp_drawable_get (param[2].data.d_drawable);
  115.  
  116.   switch (run_mode)
  117.     {
  118.     case GIMP_RUN_INTERACTIVE:
  119.       INIT_I18N_UI();
  120.       gimp_get_data ("plug_in_deinterlace", &DeinterlaceValue);
  121.       if (! deinterlace_dialog ())
  122.     status = GIMP_PDB_EXECUTION_ERROR;
  123.       break;
  124.  
  125.     case GIMP_RUN_NONINTERACTIVE:
  126.       INIT_I18N();
  127.       if (nparams != 4)
  128.     status = GIMP_PDB_CALLING_ERROR;
  129.       if (status == GIMP_PDB_SUCCESS)
  130.     DeinterlaceValue = param[3].data.d_int32;
  131.       break;
  132.  
  133.     case GIMP_RUN_WITH_LAST_VALS:
  134.       INIT_I18N();
  135.       gimp_get_data ("plug_in_deinterlace", &DeinterlaceValue);
  136.       break;
  137.  
  138.     default:
  139.       break;
  140.     }
  141.  
  142.   if (status == GIMP_PDB_SUCCESS)
  143.     {
  144.       /*  Make sure that the drawable is gray or RGB color  */
  145.       if (gimp_drawable_is_rgb (drawable->id) ||
  146.       gimp_drawable_is_gray (drawable->id))
  147.     {
  148.       gimp_progress_init (_("Deinterlace..."));
  149.       gimp_tile_cache_ntiles (2 * (drawable->width /
  150.                        gimp_tile_width () + 1));
  151.       deinterlace (drawable);
  152.  
  153.       if (run_mode != GIMP_RUN_NONINTERACTIVE)
  154.         gimp_displays_flush ();
  155.       if (run_mode == GIMP_RUN_INTERACTIVE)
  156.         gimp_set_data ("plug_in_deinterlace",
  157.                &DeinterlaceValue, sizeof (DeinterlaceValue));
  158.     }
  159.       else
  160.     {
  161.       /* gimp_message ("deinterlace: cannot operate on indexed color images"); */
  162.       status = GIMP_PDB_EXECUTION_ERROR;
  163.     }
  164.     }
  165.   *nreturn_vals = 1;
  166.   *return_vals = values;
  167.  
  168.   values[0].type = GIMP_PDB_STATUS;
  169.   values[0].data.d_status = status;
  170.  
  171.   gimp_drawable_detach (drawable);
  172. }
  173.  
  174. static void
  175. deinterlace (GimpDrawable *drawable)
  176. {
  177.   GimpPixelRgn srcPR, destPR;
  178.   gint width, height;
  179.   gint bytes;
  180.   guchar *dest;
  181.   guchar *upper;
  182.   guchar *lower;
  183.   gint row, col;
  184.   gint x1, y1, x2, y2;
  185.  
  186.   /* Get the input area. This is the bounding box of the selection in
  187.    *  the image (or the entire image if there is no selection). Only
  188.    *  operating on the input area is simply an optimization. It doesn't
  189.    *  need to be done for correct operation. (It simply makes it go
  190.    *  faster, since fewer pixels need to be operated on).
  191.    */
  192.  
  193.   gimp_drawable_mask_bounds (drawable->id, &x1, &y1, &x2, &y2);
  194.  
  195.   /* Get the size of the input image. (This will/must be the same
  196.    *  as the size of the output image.
  197.    */
  198.   width  = drawable->width;
  199.   height = drawable->height;
  200.   bytes  = drawable->bpp;
  201.  
  202.   /*  allocate row buffers  */
  203.   dest  = g_new (guchar, (x2 - x1) * bytes);
  204.   upper = g_new (guchar, (x2 - x1) * bytes);
  205.   lower = g_new (guchar, (x2 - x1) * bytes);
  206.  
  207.   /*  initialize the pixel regions  */
  208.   gimp_pixel_rgn_init (&srcPR, drawable, 0, 0, width, height, FALSE, FALSE);
  209.   gimp_pixel_rgn_init (&destPR, drawable, 0, 0, width, height, TRUE, TRUE);
  210.  
  211.   /*  loop through the rows, performing our magic*/
  212.   for (row = y1; row < y2; row++)
  213.     {
  214.       gimp_pixel_rgn_get_row (&srcPR, dest, x1, row, (x2-x1));
  215.  
  216.       /* Only do interpolation if the row:
  217.      (1) Isn't one we want to keep
  218.      (2) Has both an upper and a lower row
  219.      Otherwise, just duplicate the source row
  220.       */
  221.       if (!((row % 2 == DeinterlaceValue) ||
  222.         (row - 1 < 0) ||
  223.         (row + 1 >= height)))
  224.     {
  225.       gimp_pixel_rgn_get_row (&srcPR, upper, x1, row-1, (x2-x1));
  226.       gimp_pixel_rgn_get_row (&srcPR, lower, x1, row+1, (x2-x1));
  227.       for (col=0; col < ((x2-x1)*bytes); col++)
  228.         dest[col]=(upper[col]+lower[col])>>1;
  229.     }
  230.       gimp_pixel_rgn_set_row (&destPR, dest, x1, row, (x2-x1));
  231.  
  232.       if ((row % 5) == 0)
  233.     gimp_progress_update ((double) row / (double) (y2 - y1));
  234.     }
  235.  
  236.   /*  update the deinterlaced region  */
  237.   gimp_drawable_flush (drawable);
  238.   gimp_drawable_merge_shadow (drawable->id, TRUE);
  239.   gimp_drawable_update (drawable->id, x1, y1, (x2 - x1), (y2 - y1));
  240.  
  241.   g_free (lower);
  242.   g_free (upper);
  243.   g_free (dest);
  244. }
  245.  
  246. gboolean run_flag = FALSE;
  247.  
  248. static void
  249. deinterlace_ok_callback (GtkWidget *widget,
  250.              gpointer   data)
  251. {
  252.   run_flag = TRUE;
  253.  
  254.   gtk_widget_destroy (GTK_WIDGET (data));
  255. }
  256.  
  257. static gint
  258. deinterlace_dialog (void)
  259. {
  260.   GtkWidget *dlg;
  261.   GtkWidget *vbox;
  262.   GtkWidget *frame;
  263.  
  264.   gimp_ui_init ("deinterlace", FALSE);
  265.  
  266.   dlg = gimp_dialog_new (_("Deinterlace"), "deinterlace",
  267.              gimp_standard_help_func, "filters/deinterlace.html",
  268.              GTK_WIN_POS_MOUSE,
  269.              FALSE, TRUE, FALSE,
  270.  
  271.              _("OK"), deinterlace_ok_callback,
  272.              NULL, NULL, NULL, TRUE, FALSE,
  273.              _("Cancel"), gtk_widget_destroy,
  274.              NULL, 1, NULL, FALSE, TRUE,
  275.  
  276.              NULL);
  277.  
  278.   gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
  279.               GTK_SIGNAL_FUNC (gtk_main_quit),
  280.               NULL);
  281.  
  282.   vbox = gtk_vbox_new (FALSE, 0);
  283.   gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
  284.   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), vbox, TRUE, TRUE, 0);
  285.   gtk_widget_show (vbox);
  286.  
  287.   frame =
  288.     gimp_radio_group_new2 (TRUE, _("Mode"),
  289.                gimp_radio_button_update,
  290.                &DeinterlaceValue, (gpointer) DeinterlaceValue,
  291.  
  292.                _("Keep Odd Fields"), (gpointer) ODD_FIELDS, NULL,
  293.                _("Keep Even Fields"), (gpointer) EVEN_FIELDS, NULL,
  294.  
  295.                  NULL);
  296.   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  297.   gtk_widget_show (frame);
  298.  
  299.   gtk_widget_show (dlg);
  300.  
  301.   gtk_main ();
  302.   gdk_flush ();
  303.  
  304.   return run_flag;
  305. }
  306.