~ubuntu-branches/ubuntu/hoary/gimp/hoary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* This plugin by thorsten@arch.usyd.edu.au           */
/* Based on S&P's Gauss and Laplace filters              */

/* updated 1/30/03: <sjburges@gimp.org>
 * fixed an off-by-1 error that was causing an attempt to read a
 * get_pixel_by_row at the -1'th row
 */

/* updated 11/04/97:
   Use 8-pixel neighbourhood to create outline,
   use min-max operation for local gradient,
   don't use rint;
   if gamma-channel: set to white if at least one colour channel is >15 */

/* update 03/10/97
   #ifdef MAX and MIN */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>

#include <libgimp/gimp.h>

#include "libgimp/stdplugins-intl.h"


/* Declare local functions.
 */
static void      query  (void);
static void      run    (const gchar      *name,
			 gint              nparams,
			 const GimpParam  *param,
			 gint             *nreturn_vals,
			 GimpParam       **return_vals);

static void      laplace             (GimpDrawable *drawable);
static void      laplace_prepare_row (GimpPixelRgn *pixel_rgn,
				      guchar       *data,
				      gint          x,
				      gint          y,
				      gint          w);


GimpPlugInInfo PLUG_IN_INFO =
{
  NULL,  /* init_proc  */
  NULL,  /* quit_proc  */
  query, /* query_proc */
  run,   /* run_proc   */
};


MAIN ()

static void
query (void)
{
  static GimpParamDef args[] =
  {
    { GIMP_PDB_INT32,    "run_mode", "Interactive, non-interactive" },
    { GIMP_PDB_IMAGE,    "image",    "Input image (unused)"         },
    { GIMP_PDB_DRAWABLE, "drawable", "Input drawable"               }
  };

  gimp_install_procedure ("plug_in_laplace",
			  "Edge Detection with Laplace Operation",
			  "This plugin creates one-pixel wide edges from the "
			  "image, with the value proportional to the gradient. "
			  "It uses the Laplace operator (a 3x3 kernel with -8 "
			  "in the middle). The image has to be laplacered to "
			  "get useful results, a gauss_iir with 1.5 - 5.0 "
			  "depending on the noise in the image is best.",
			  "Thorsten Schnier",
			  "Thorsten Schnier",
			  "1997",
			  N_("_Laplace"),
			  "RGB*, GRAY*",
			  GIMP_PLUGIN,
			  G_N_ELEMENTS (args), 0,
			  args, NULL);

  gimp_plugin_menu_register ("plug_in_laplace",
                             "<Image>/Filters/Edge-Detect");
}

static void
run (const gchar      *name,
     gint              nparams,
     const GimpParam  *param,
     gint             *nreturn_vals,
     GimpParam       **return_vals)
{
  static GimpParam   values[1];
  GimpDrawable      *drawable;
  GimpRunMode        run_mode;
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;

  run_mode = param[0].data.d_int32;

  INIT_I18N();

  /*  Get the specified drawable  */
  drawable = gimp_drawable_get (param[2].data.d_drawable);

  /*  Make sure that the drawable is gray or RGB color  */
  if (gimp_drawable_is_rgb (drawable->drawable_id) ||
      gimp_drawable_is_gray (drawable->drawable_id))
    {
      gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1));
      laplace (drawable);

      if (run_mode != GIMP_RUN_NONINTERACTIVE)
	gimp_displays_flush ();
    }
  else
    {
      /* gimp_message ("laplace: cannot operate on indexed color images"); */
      status = GIMP_PDB_EXECUTION_ERROR;
    }

  *nreturn_vals = 1;
  *return_vals  = values;

  values[0].type          = GIMP_PDB_STATUS;
  values[0].data.d_status = status;

  gimp_drawable_detach (drawable);
}

static void
laplace_prepare_row (GimpPixelRgn *pixel_rgn,
		     guchar       *data,
		     gint          x,
		     gint          y,
		     gint          w)
{
  gint b;

  if (y < 0)
      gimp_pixel_rgn_get_row (pixel_rgn, data, x, (y + 1), w);
  else if (y == pixel_rgn->h)
      gimp_pixel_rgn_get_row (pixel_rgn, data, x, (y - 1), w);
  else
      gimp_pixel_rgn_get_row (pixel_rgn, data, x, y, w);

  /*  Fill in edge pixels  */
  for (b = 0; b < pixel_rgn->bpp; b++)
    {
      data[-(int)pixel_rgn->bpp + b] = data[b];
      data[w * pixel_rgn->bpp + b] = data[(w - 1) * pixel_rgn->bpp + b];
    }
}

#define SIGN(a) (((a) > 0) ? 1 : -1)
#define RMS(a,b) (sqrt (pow ((a),2) + pow ((b), 2)))
#define BLACK_REGION(val) ((val) > 128)
#define WHITE_REGION(val) ((val) <= 128)

static void
minmax  (gint  x1,
	 gint  x2,
	 gint  x3,
	 gint  x4,
	 gint  x5,
	 gint *min_result,
	 gint *max_result)
{
  gint min1, min2, max1, max2;

  if (x1 > x2) { max1=x1; min1=x2; } else { max1=x2; min1=x1; }
  if (x3 > x4) { max2=x3; min2=x4; } else { max2=x4; min2=x3; }
  if (min1 < min2)
    *min_result = MIN (min1, x5);
      else  *min_result = MIN (min2, x5);
  if (max1 > max2)
    *max_result = MAX (max1, x5);
      else  *max_result = MAX (max2, x5);
}

static void
laplace (GimpDrawable *drawable)
{
  GimpPixelRgn srcPR, destPR;
  gint         width, height;
  gint         bytes;
  gint         current;
  gint         gradient;
  gint         max_gradient = 0;
  gint         alpha;
  gint         counter;
  guchar      *dest, *d;
  guchar      *prev_row, *pr;
  guchar      *cur_row, *cr;
  guchar      *next_row, *nr;
  guchar      *tmp;
  gint         row, col;
  gint         x1, y1, x2, y2;
  gint         minval, maxval;
  gfloat       scale = 1.0;

  /* Get the input area. This is the bounding box of the selection in
   *  the image (or the entire image if there is no selection). Only
   *  operating on the input area is simply an optimization. It doesn't
   *  need to be done for correct operation. (It simply makes it go
   *  faster, since fewer pixels need to be operated on).
   */

  gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
  gimp_progress_init (_("Laplace..."));

  /* Get the size of the input image. (This will/must be the same
   *  as the size of the output image.
   */
  width  = drawable->width;
  height = drawable->height;
  bytes  = drawable->bpp;
  alpha  = gimp_drawable_has_alpha (drawable->drawable_id);

  /*  allocate row buffers  */
  prev_row = g_new (guchar, (x2 - x1 + 2) * bytes);
  cur_row  = g_new (guchar, (x2 - x1 + 2) * bytes);
  next_row = g_new (guchar, (x2 - x1 + 2) * bytes);
  dest     = g_new (guchar, (x2 - x1) * bytes);

  /*  initialize the pixel regions  */
  gimp_pixel_rgn_init (&srcPR, drawable, 0, 0, width, height, FALSE, FALSE);
  gimp_pixel_rgn_init (&destPR, drawable, 0, 0, width, height, TRUE, TRUE);

  pr = prev_row + bytes;
  cr = cur_row + bytes;
  nr = next_row + bytes;

  laplace_prepare_row (&srcPR, pr, x1, y1 - 1, (x2 - x1));
  laplace_prepare_row (&srcPR, cr, x1, y1, (x2 - x1));

  /*  loop through the rows, applying the laplace convolution  */
  for (row = y1; row < y2; row++)
    {
      /*  prepare the next row  */
      laplace_prepare_row (&srcPR, nr, x1, row + 1, (x2 - x1));

      d = dest;
      for (col = 0; col < (x2 - x1) * bytes; col++)
	if (alpha && (((col + 1) % bytes) == 0)) /* the alpha channel */
          {
            *d++ = cr[col];
          }
	else
	  {
	    minmax (pr[col], cr[col - bytes], cr[col], cr[col + bytes],
		    nr[col], &minval, &maxval); /* four-neighbourhood */
	    gradient = (0.5 * MAX ((maxval - cr [col]), (cr[col]- minval)));
	    max_gradient = MAX (abs (gradient), max_gradient);
	    *d++ = ((pr[col - bytes] + pr[col]       + pr[col + bytes] +
		     cr[col - bytes] - (8 * cr[col]) + cr[col + bytes] +
		     nr[col - bytes] + nr[col]       + nr[col + bytes]) > 0) ?
	      gradient : (128 + gradient);
	  }

      /*  store the dest  */
      gimp_pixel_rgn_set_row (&destPR, dest, x1, row, (x2 - x1));

      /*  shuffle the row pointers  */
      tmp = pr;
      pr = cr;
      cr = nr;
      nr = tmp;

      if ((row % 5) == 0)
	gimp_progress_update ((gdouble) row / (gdouble) (y2 - y1));
    }


  /* now clean up: leave only edges, but keep gradient value */


  gimp_pixel_rgn_init (&srcPR, drawable, 0, 0, width, height, FALSE, TRUE);

  pr = prev_row + bytes;
  cr = cur_row + bytes;
  nr = next_row + bytes;

  laplace_prepare_row (&srcPR, pr, x1, y1 - 1, (x2 - x1));
  laplace_prepare_row (&srcPR, cr, x1, y1, (x2 - x1));

  gimp_progress_init (_("Cleanup..."));
  scale = (255.0 / (gfloat) max_gradient);
  counter =0;

  /*  loop through the rows, applying the laplace convolution  */
  for (row = y1; row < y2; row++)
    {
      /*  prepare the next row  */
      laplace_prepare_row (&srcPR, nr, x1, row + 1, (x2 - x1));

      d = dest;
      for (col = 0; col < (x2 - x1) * bytes; col++)
        {
          current = cr[col];
          current = (WHITE_REGION(current) &&
                     (BLACK_REGION (pr[col - bytes]) ||
                      BLACK_REGION (pr[col])         ||
                      BLACK_REGION (pr[col + bytes]) ||
                      BLACK_REGION (cr[col - bytes]) ||
                      BLACK_REGION (cr[col + bytes]) ||
                      BLACK_REGION (nr[col - bytes]) ||
                      BLACK_REGION (nr[col])         ||
                      BLACK_REGION (nr[col + bytes]))) ?
            (gint) (scale * ((float) ((current >= 128) ?
                                      (current-128) : current))) : 0;

          if (alpha && (((col + 1) % bytes) == 0)) /* the alpha channel */
            {
              *d++ = (counter == 0) ? 0 : 255;
              counter = 0;
            }
          else
            {
              *d++ = current;
              if (current > 15)
                counter ++;
            }
        }

      /*  store the dest  */
      gimp_pixel_rgn_set_row (&destPR, dest, x1, row, (x2 - x1));

      /*  shuffle the row pointers  */
      tmp = pr;
      pr = cr;
      cr = nr;
      nr = tmp;

      if ((row % 5) == 0)
	gimp_progress_update ((gdouble) row / (gdouble) (y2 - y1));
    }

  /*  update the laplaced region  */
  gimp_drawable_flush (drawable);
  gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
  gimp_drawable_update (drawable->drawable_id, x1, y1, (x2 - x1), (y2 - y1));

  g_free (prev_row);
  g_free (cur_row);
  g_free (next_row);
  g_free (dest);
}