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 / laplace.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-24  |  10.0 KB  |  352 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  */
  18.  
  19. /* This plugin by thorsten@arch.usyd.edu.au           */
  20. /* Based on S&P's Gauss and Laplace filters              */
  21.  
  22. /* updated 11/04/97:
  23.    Use 8-pixel neighbourhood to create outline,
  24.    use min-max operation for local gradient,
  25.    don't use rint;
  26.    if gamma-channel: set to white if at least one colour channel is >15 */
  27.  
  28. /* update 03/10/97
  29.    #ifdef MAX and MIN */
  30.  
  31. #include "config.h"
  32.  
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35.  
  36. #include <libgimp/gimp.h>
  37.  
  38. #include "libgimp/stdplugins-intl.h"
  39.  
  40.  
  41. /* Declare local functions.
  42.  */
  43. static void      query  (void);
  44. static void      run    (gchar     *name,
  45.              gint       nparams,
  46.              GimpParam    *param,
  47.              gint      *nreturn_vals,
  48.              GimpParam   **return_vals);
  49.  
  50. static void      laplace             (GimpDrawable  *drawable);
  51. static void      laplace_prepare_row (GimpPixelRgn  *pixel_rgn,
  52.                       guchar     *data,
  53.                       gint        x,
  54.                       gint        y,
  55.                       gint        w);
  56.  
  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.  
  67. MAIN ()
  68.  
  69. static void
  70. query (void)
  71. {
  72.   static GimpParamDef args[] =
  73.   {
  74.     { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
  75.     { GIMP_PDB_IMAGE, "image", "Input image (unused)" },
  76.     { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }
  77.   };
  78.   static gint nargs = sizeof (args) / sizeof (args[0]);
  79.  
  80.   gimp_install_procedure ("plug_in_laplace",
  81.               "Edge Detection with Laplace Operation",
  82.               "This plugin creates one-pixel wide edges from the "
  83.               "image, with the value proportional to the gradient. "
  84.               "It uses the Laplace operator (a 3x3 kernel with -8 "
  85.               "in the middle). The image has to be laplacered to "
  86.               "get useful results, a gauss_iir with 1.5 - 5.0 "
  87.               "depending on the noise in the image is best.",
  88.               "Thorsten Schnier",
  89.               "Thorsten Schnier",
  90.               "1997",
  91.               N_("<Image>/Filters/Edge-Detect/Laplace"),
  92.               "RGB*, GRAY*",
  93.               GIMP_PLUGIN,
  94.               nargs, 0,
  95.               args, NULL);
  96. }
  97.  
  98. static void
  99. run (gchar   *name,
  100.      gint     nparams,
  101.      GimpParam  *param,
  102.      gint    *nreturn_vals,
  103.      GimpParam **return_vals)
  104. {
  105.   static GimpParam values[1];
  106.   GimpDrawable *drawable;
  107.   GimpRunModeType run_mode;
  108.   GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  109.  
  110.   run_mode = param[0].data.d_int32;
  111.  
  112.   INIT_I18N();
  113.  
  114.   /*  Get the specified drawable  */
  115.   drawable = gimp_drawable_get (param[2].data.d_drawable);
  116.  
  117.   /*  Make sure that the drawable is gray or RGB color  */
  118.   if (gimp_drawable_is_rgb (drawable->id) ||
  119.       gimp_drawable_is_gray (drawable->id))
  120.     {
  121.       gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1));
  122.       laplace (drawable);
  123.  
  124.       if (run_mode != GIMP_RUN_NONINTERACTIVE)
  125.     gimp_displays_flush ();
  126.     }
  127.   else
  128.     {
  129.       /* gimp_message ("laplace: cannot operate on indexed color images"); */
  130.       status = GIMP_PDB_EXECUTION_ERROR;
  131.     }
  132.  
  133.   *nreturn_vals = 1;
  134.   *return_vals = values;
  135.  
  136.   values[0].type = GIMP_PDB_STATUS;
  137.   values[0].data.d_status = status;
  138.  
  139.   gimp_drawable_detach (drawable);
  140. }
  141.  
  142. static void
  143. laplace_prepare_row (GimpPixelRgn *pixel_rgn,
  144.              guchar    *data,
  145.              gint       x,
  146.              gint       y,
  147.              gint       w)
  148. {
  149.   gint b;
  150.  
  151.   if (y == 0)
  152.     gimp_pixel_rgn_get_row (pixel_rgn, data, x, (y + 1), w);
  153.   else if (y == pixel_rgn->h)
  154.     gimp_pixel_rgn_get_row (pixel_rgn, data, x, (y - 1), w);
  155.   else
  156.     gimp_pixel_rgn_get_row (pixel_rgn, data, x, y, w);
  157.  
  158.   /*  Fill in edge pixels  */
  159.   for (b = 0; b < pixel_rgn->bpp; b++)
  160.     {
  161.       data[-(int)pixel_rgn->bpp + b] = data[b];
  162.       data[w * pixel_rgn->bpp + b] = data[(w - 1) * pixel_rgn->bpp + b];
  163.     }
  164. }
  165.  
  166. #define SIGN(a) (((a) > 0) ? 1 : -1)
  167. #define RMS(a,b) (sqrt (pow ((a),2) + pow ((b), 2)))
  168. #define BLACK_REGION(val) ((val) > 128)
  169. #define WHITE_REGION(val) ((val) <= 128)
  170.  
  171. static void
  172. minmax  (gint  x1,
  173.      gint  x2,
  174.      gint  x3,
  175.      gint  x4,
  176.      gint  x5,
  177.      gint *min_result,
  178.      gint *max_result)
  179. {
  180.   gint min1, min2, max1, max2;
  181.  
  182.   if (x1 > x2) { max1=x1; min1=x2; } else { max1=x2; min1=x1; }
  183.   if (x3 > x4) { max2=x3; min2=x4; } else { max2=x4; min2=x3; }
  184.   if (min1 < min2)
  185.     *min_result = MIN (min1, x5);
  186.       else  *min_result = MIN (min2, x5);
  187.   if (max1 > max2)
  188.     *max_result = MAX (max1, x5);
  189.       else  *max_result = MAX (max2, x5);
  190. }
  191.  
  192. static void
  193. laplace (GimpDrawable *drawable)
  194. {
  195.   GimpPixelRgn srcPR, destPR;
  196.   gint width, height;
  197.   gint bytes;
  198.   gint current;
  199.   gint gradient;
  200.   gint max_gradient = 0;
  201.   gint alpha;
  202.   gint counter;
  203.   guchar *dest, *d;
  204.   guchar *prev_row, *pr;
  205.   guchar *cur_row, *cr;
  206.   guchar *next_row, *nr;
  207.   guchar *tmp;
  208.   gint row, col;
  209.   gint x1, y1, x2, y2;
  210.   gint minval, maxval;
  211.   float scale = 1.0;
  212.  
  213.   /* Get the input area. This is the bounding box of the selection in
  214.    *  the image (or the entire image if there is no selection). Only
  215.    *  operating on the input area is simply an optimization. It doesn't
  216.    *  need to be done for correct operation. (It simply makes it go
  217.    *  faster, since fewer pixels need to be operated on).
  218.    */
  219.  
  220.   gimp_drawable_mask_bounds (drawable->id, &x1, &y1, &x2, &y2);
  221.   gimp_progress_init ( _("Laplace..."));
  222.  
  223.   /* Get the size of the input image. (This will/must be the same
  224.    *  as the size of the output image.
  225.    */
  226.   width = drawable->width;
  227.   height = drawable->height;
  228.   bytes = drawable->bpp;
  229.   alpha = gimp_drawable_has_alpha (drawable -> id);
  230.   /*  allocate row buffers  */
  231.   prev_row = (guchar *) malloc ((x2 - x1 + 2) * bytes);
  232.   cur_row = (guchar *) malloc ((x2 - x1 + 2) * bytes);
  233.   next_row = (guchar *) malloc ((x2 - x1 + 2) * bytes);
  234.   dest = (guchar *) malloc ((x2 - x1) * bytes);
  235.  
  236.   /*  initialize the pixel regions  */
  237.   gimp_pixel_rgn_init (&srcPR, drawable, 0, 0, width, height, FALSE, FALSE);
  238.   gimp_pixel_rgn_init (&destPR, drawable, 0, 0, width, height, TRUE, TRUE);
  239.  
  240.   pr = prev_row + bytes;
  241.   cr = cur_row + bytes;
  242.   nr = next_row + bytes;
  243.  
  244.   laplace_prepare_row (&srcPR, pr, x1, y1 - 1, (x2 - x1));
  245.   laplace_prepare_row (&srcPR, cr, x1, y1, (x2 - x1));
  246.  
  247.   /*  loop through the rows, applying the laplace convolution  */
  248.   for (row = y1; row < y2; row++)
  249.     {
  250.       /*  prepare the next row  */
  251.       laplace_prepare_row (&srcPR, nr, x1, row + 1, (x2 - x1));
  252.  
  253.       d = dest;
  254.       for (col = 0; col < (x2 - x1) * bytes; col++)
  255.     if (alpha && (((col + 1) % bytes) == 0)) /* the alpha channel */
  256.       *d++ = cr[col];
  257.     else
  258.       {
  259.         minmax (pr[col], cr[col - bytes], cr[col], cr[col + bytes],
  260.             nr[col], &minval, &maxval); /* four-neighbourhood */
  261.         gradient = (0.5 * MIN ((maxval - cr [col]), (cr[col]- minval)));
  262.         max_gradient = MAX(abs(gradient), max_gradient);
  263.         *d++ = ((pr[col - bytes] + pr[col]     + pr[col + bytes] +
  264.              cr[col - bytes] - (8 * cr[col]) + cr[col + bytes] +
  265.              nr[col - bytes] + nr[col]       + nr[col + bytes]) > 0) ?
  266.           gradient : (128 + gradient);
  267.       }
  268.  
  269.       /*  store the dest  */
  270.       gimp_pixel_rgn_set_row (&destPR, dest, x1, row, (x2 - x1));
  271.  
  272.       /*  shuffle the row pointers  */
  273.       tmp = pr;
  274.       pr = cr;
  275.       cr = nr;
  276.       nr = tmp;
  277.  
  278.       if ((row % 5) == 0)
  279.     gimp_progress_update ((double) row / (double) (y2 - y1));
  280.     }
  281.  
  282.  
  283.   /* now clean up: leave only edges, but keep gradient value */
  284.  
  285.  
  286.   gimp_pixel_rgn_init (&srcPR, drawable, 0, 0, width, height, FALSE, TRUE);
  287.  
  288.   pr = prev_row + bytes;
  289.   cr = cur_row + bytes;
  290.   nr = next_row + bytes;
  291.  
  292.   laplace_prepare_row (&srcPR, pr, x1, y1 - 1, (x2 - x1));
  293.   laplace_prepare_row (&srcPR, cr, x1, y1, (x2 - x1));
  294.  
  295.   gimp_progress_init ( _("Cleanup..."));
  296.   scale = (255.0 / (float) max_gradient);
  297.   counter =0;
  298.  
  299.   /*  loop through the rows, applying the laplace convolution  */
  300.   for (row = y1; row < y2; row++)
  301.     {
  302.       /*  prepare the next row  */
  303.       laplace_prepare_row (&srcPR, nr, x1, row + 1, (x2 - x1));
  304.  
  305.       d = dest;
  306.       for (col = 0; col < (x2 - x1) * bytes; col++) {
  307.     current = cr[col];
  308.     current = (WHITE_REGION(current) &&
  309.            (BLACK_REGION (pr[col - bytes]) ||
  310.             BLACK_REGION (pr[col]) ||
  311.             BLACK_REGION (pr[col + bytes]) ||
  312.             BLACK_REGION (cr[col - bytes]) ||
  313.  
  314.             BLACK_REGION (cr[col + bytes]) ||
  315.             BLACK_REGION (nr[col - bytes]) ||
  316.             BLACK_REGION (nr[col]) ||
  317.             BLACK_REGION (nr[col + bytes]))) ?
  318.       (gint) (scale * ((float) ((current >= 128) ?
  319.                     (current-128) : current)))
  320.       : 0;
  321.     if (alpha && (((col + 1) % bytes) == 0)) { /* the alpha channel */
  322.       *d++ = (counter == 0) ? 0 : 255;
  323.       counter = 0; }
  324.     else {
  325.       *d++ = current;
  326.       if (current > 15) counter ++;
  327.     }
  328.       }
  329.       /*  store the dest  */
  330.       gimp_pixel_rgn_set_row (&destPR, dest, x1, row, (x2 - x1));
  331.  
  332.       /*  shuffle the row pointers  */
  333.       tmp = pr;
  334.       pr = cr;
  335.       cr = nr;
  336.       nr = tmp;
  337.  
  338.       if ((row % 5) == 0)
  339.     gimp_progress_update ((double) row / (double) (y2 - y1));
  340.     }
  341.  
  342.   /*  update the laplaced region  */
  343.   gimp_drawable_flush (drawable);
  344.   gimp_drawable_merge_shadow (drawable->id, TRUE);
  345.   gimp_drawable_update (drawable->id, x1, y1, (x2 - x1), (y2 - y1));
  346.  
  347.   free (prev_row);
  348.   free (cur_row);
  349.   free (next_row);
  350.   free (dest);
  351. }
  352.