~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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/****************************************************************************
 * This is a plugin for the GIMP v 0.99.8 or later.  Documentation is
 * available at http://www.rru.com/~meo/gimp/ .
 *
 * Copyright (C) 1997-98 Miles O'Neal  <meo@rru.com>  http://www.rru.com/~meo/
 * Blur code Copyright (C) 1995 Spencer Kimball and Peter Mattis
 * GUI based on GTK code from:
 *    alienmap (Copyright (C) 1996, 1997 Daniel Cotting)
 *    plasma   (Copyright (C) 1996 Stephen Norris),
 *    oilify   (Copyright (C) 1996 Torsten Martinsen),
 *    ripple   (Copyright (C) 1997 Brian Degenhardt) and
 *    whirl    (Copyright (C) 1997 Federico Mena Quintero).
 *
 * 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.
 *
 ****************************************************************************/

/****************************************************************************
 * Blur:
 *
 * blur version 2.1 (10 June 2004 WES)
 * history
 *     2.1 - 10 June 2004 WES
 *         removed dialog along with randomization and repeat options
 *     2.0 -  1 May 1998 MEO
 *         based on randomize 1.7
 *
 * Please send any patches or suggestions to the author: meo@rru.com .
 *
 * Blur applies a 3x3 blurring convolution kernel to the specified drawable.
 *
 * For each pixel in the selection or image,
 * whether to change the pixel is decided by picking a
 * random number, weighted by the user's "randomization" percentage.
 * If the random number is in range, the pixel is modified.  For
 * blurring, an average is determined from the current and adjacent
 * pixels. *(Except for the random factor, the blur code came
 * straight from the original S&P blur plug-in.)*
 *
 * This works only with RGB and grayscale images.
 *
 ****************************************************************************/

#include "config.h"

#include <string.h>

#include <libgimp/gimp.h>

#include "libgimp/stdplugins-intl.h"


/*********************************
 *
 *  PLUGIN-SPECIFIC CONSTANTS
 *
 ********************************/

/*
 *  progress meter update frequency
 */
#define PROG_UPDATE_TIME ((row % 10) == 0)

#define PLUG_IN_NAME "plug_in_blur"
#define HELP_ID      "plug-in-blur"

/*********************************
 *
 *  LOCAL FUNCTIONS
 *
 ********************************/

static void query (void);
static void run   (const gchar      *name,
                   gint              nparams,
                   const GimpParam  *param,
                   gint             *nreturn_vals,
                   GimpParam       **return_vals);

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

static void         blur             (GimpDrawable *drawable);

static inline void  blur_prepare_row (GimpPixelRgn *pixel_rgn,
                                      guchar       *data,
                                      gint          x,
                                      gint          y,
                                      gint          w);

/************************************ Guts ***********************************/

MAIN ()

/*********************************
 *
 *  query() - query_proc
 *
 *      called by the GIMP to learn about this plug-in
 *
 ********************************/

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"               },
  };

  const gchar *blurb =
    "Apply a 3x3 blurring convolution kernel to the specified drawable.";
  const gchar *help =
    "This plug-in blurs the specified drawable, using a 3x3 blur. "
    "Indexed images are not supported.";
  const gchar *author = "Miles O'Neal  <meo@rru.com>";
  const gchar *copyrights =
    "Miles O'Neal, Spencer Kimball, Peter Mattis, Torsten Martinsen, "
    "Brian Degenhardt, Federico Mena Quintero, Stephen Norris, Daniel Cotting";
  const gchar *copyright_date = "1995-1998";

  gimp_install_procedure ("plug_in_blur",
                          blurb,
                          help,
                          author,
                          copyrights,
                          copyright_date,
                          N_("_Blur"),
                          "RGB*, GRAY*",
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (args), 0,
                          args, NULL);

  gimp_plugin_menu_register ("plug_in_blur", "<Image>/Filters/Blur");
}


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

  INIT_I18N ();

  values[0].type = GIMP_PDB_STATUS;
  values[0].data.d_status = status;
  *nreturn_vals = 1;
  *return_vals = values;

  if (strcmp (name, "plug_in_blur") != 0 || nparams < 3)
    {
      values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
      return;
    }

  run_mode = param[0].data.d_int32;
  drawable = gimp_drawable_get (param[2].data.d_drawable);

  /*
   *  Make sure the drawable type is appropriate.
   */
  if (gimp_drawable_is_rgb (drawable->drawable_id) ||
      gimp_drawable_is_gray (drawable->drawable_id))
    {
      gimp_progress_init (_("Blurring..."));
      gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1));

      blur (drawable);

      if (run_mode != GIMP_RUN_NONINTERACTIVE)
        {
          gimp_displays_flush ();
        }
    }
  else
    {
      status = GIMP_PDB_EXECUTION_ERROR;
    }

  values[0].data.d_status = status;
  gimp_drawable_detach (drawable);
}


/*********************************
 *
 *  blur_prepare_row()
 *
 *  Get a row of pixels.  If the requested row
 *  is off the edge, clone the edge row.
 *
 ********************************/

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

  y = CLAMP (y, 1, pixel_rgn->h - 1);
  gimp_pixel_rgn_get_row (pixel_rgn, data, x, y, w);

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

/*********************************
 *
 *  blur()
 *
 *  Actually mess with the image.
 *
 ********************************/

static void
blur (GimpDrawable *drawable)
{
  GimpPixelRgn  srcPR, destPR, destPR2, *sp, *dp, *tp;
  gint          width, height;
  gint          bytes;
  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          ind;
  gboolean      has_alpha;

  gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
  /*
   *  Get the size of the input image. (This will/must be the same
   *  as the size of the output image.  Also get alpha info.
   */
  width = drawable->width;
  height = drawable->height;
  bytes = drawable->bpp;
  has_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);
  gimp_pixel_rgn_init (&destPR2, drawable, 0, 0, width, height, TRUE, TRUE);
  sp = &srcPR;
  dp = &destPR;
  tp = NULL;

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

  /*
   *  prepare the first row and previous row
   */
  blur_prepare_row (sp, pr, x1, y1 - 1, (x2 - x1));
  blur_prepare_row (sp, cr, x1, y1, (x2 - x1));
  /*
   *  loop through the rows, applying the selected convolution
   */
  for (row = y1; row < y2; row++)
    {
      /*  prepare the next row  */
      blur_prepare_row (sp, nr, x1, row + 1, (x2 - x1));

      d = dest;
      ind = 0;
      for (col = 0; col < (x2 - x1) * bytes; col++)
	{
	  ind++;
	  if (ind == bytes || !has_alpha)
	    {
	      /*
	       *  If no alpha channel,
	       *   or if there is one and this is it...
	       */
	      *d++ = ((gint) pr[col - bytes] + (gint) pr[col] +
		      (gint) pr[col + bytes] +
		      (gint) cr[col - bytes] + (gint) cr[col] +
		      (gint) cr[col + bytes] +
		      (gint) nr[col - bytes] + (gint) nr[col] +
		      (gint) nr[col + bytes]) / 9;
	      ind = 0;
	    }
	  else
	    {
	      /*
	       *  otherwise we have an alpha channel,
	       *   but this is a color channel
	       */
	      *d++ = ((gint)
		      (((gdouble) (pr[col - bytes] * pr[col - ind])
			+ (gdouble) (pr[col] * pr[col + bytes - ind])
			+ (gdouble) (pr[col + bytes] * pr[col + 2*bytes - ind])
			+ (gdouble) (cr[col - bytes] * cr[col - ind])
			+ (gdouble) (cr[col] * cr[col + bytes - ind])
			+ (gdouble) (cr[col + bytes] * cr[col + 2*bytes - ind])
			+ (gdouble) (nr[col - bytes] * nr[col - ind])
			+ (gdouble) (nr[col] * nr[col + bytes - ind])
			+ (gdouble) (nr[col + bytes] * nr[col + 2*bytes - ind]))
		       / ((gdouble) pr[col - ind]
			  + (gdouble) pr[col + bytes - ind]
			  + (gdouble) pr[col + 2*bytes - ind]
			  + (gdouble) cr[col - ind]
			  + (gdouble) cr[col + bytes - ind]
			  + (gdouble) cr[col + 2*bytes - ind]
			  + (gdouble) nr[col - ind]
			  + (gdouble) nr[col + bytes - ind]
			  + (gdouble) nr[col + 2*bytes - ind])));
	    }
	}

      /*
       *  Save the modified row, shuffle the row pointers, and every
       *  so often, update the progress meter.
       */
      gimp_pixel_rgn_set_row (dp, dest, x1, row, (x2 - x1));

      tmp = pr;
      pr = cr;
      cr = nr;
      nr = tmp;

      if (PROG_UPDATE_TIME)
	gimp_progress_update ((gdouble) row / (gdouble) (y2 - y1));
    }

  gimp_progress_update (1.0);

  /*
   *  update the blurred 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));
  /*
   *  clean up after ourselves.
   */
  g_free (prev_row);
  g_free (cur_row);
  g_free (next_row);
  g_free (dest);
}