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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-04-04 14:51:23 UTC
  • Revision ID: james.westby@ubuntu.com-20050404145123-9py049eeelfymur8
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**************************************************
 
2
 * file: nlfilt/nlfilt.c
 
3
 *
 
4
 * Copyright (c) 1997 Eric L. Hernes (erich@rrnet.com)
 
5
 * All rights reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions
 
9
 * are met:
 
10
 * 1. Redistributions of source code must retain the above copyright
 
11
 *    notice, this list of conditions and the following disclaimer.
 
12
 * 2. The name of the author may not be used to endorse or promote products
 
13
 *    derived from this software withough specific prior written permission
 
14
 *
 
15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 
16
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
17
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 
18
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 
19
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 
20
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
21
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
22
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
23
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 
24
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
25
 *
 
26
 * $Id: nlfilt.c,v 1.63 2004/11/14 02:45:11 yosh Exp $
 
27
 */
 
28
 
 
29
/*
 
30
 * Algorithm fixes, V2.0 compatibility by David Hodson  hodsond@ozemail.com.au
 
31
 */
 
32
 
 
33
#include "config.h"
 
34
 
 
35
#include <string.h>
 
36
#include <stdio.h>
 
37
 
 
38
#include <gtk/gtk.h>
 
39
 
 
40
#include <libgimp/gimp.h>
 
41
#include <libgimp/gimpui.h>
 
42
 
 
43
#include "libgimp/stdplugins-intl.h"
 
44
 
 
45
typedef struct
 
46
{
 
47
  gdouble  alpha;
 
48
  gdouble  radius;
 
49
  gint     filter;
 
50
  gboolean preview;
 
51
} NLFilterValues;
 
52
 
 
53
typedef enum
 
54
{
 
55
  filter_alpha_trim,
 
56
  filter_opt_est,
 
57
  filter_edge_enhance
 
58
} FilterType;
 
59
 
 
60
static NLFilterValues nlfvals =
 
61
{
 
62
  0.3,
 
63
  0.3,
 
64
  0,
 
65
  TRUE
 
66
};
 
67
 
 
68
/* function protos */
 
69
 
 
70
static void query (void);
 
71
static void run   (const gchar      *name,
 
72
                   gint              nparam,
 
73
                   const GimpParam  *param,
 
74
                   gint             *nretvals,
 
75
                   GimpParam       **retvals);
 
76
 
 
77
static void nlfilter            (GimpDrawable *drawable,
 
78
                                 GimpPreview  *preview);
 
79
static gboolean nlfilter_dialog (GimpDrawable *drawable);
 
80
 
 
81
static inline gint nlfiltInit   (gdouble       alpha,
 
82
                                 gdouble       radius,
 
83
                                 FilterType    filter);
 
84
 
 
85
static inline void nlfiltRow    (guchar       *srclast,
 
86
                                 guchar       *srcthis,
 
87
                                 guchar       *srcnext,
 
88
                                 guchar       *dst,
 
89
                                 gint          width,
 
90
                                 gint          bpp,
 
91
                                 gint          filtno);
 
92
 
 
93
GimpPlugInInfo PLUG_IN_INFO =
 
94
{
 
95
  NULL,  /* init_proc  */
 
96
  NULL,  /* quit_proc  */
 
97
  query, /* query_proc */
 
98
  run,   /* run_proc   */
 
99
};
 
100
 
 
101
MAIN ()
 
102
 
 
103
static void
 
104
query (void)
 
105
{
 
106
  static GimpParamDef args[] =
 
107
  {
 
108
    { GIMP_PDB_INT32,    "run_mode", "Interactive, non-interactive" },
 
109
    { GIMP_PDB_IMAGE,    "img",      "The Image to Filter" },
 
110
    { GIMP_PDB_DRAWABLE, "drw",      "The Drawable" },
 
111
    { GIMP_PDB_FLOAT,    "alpha",    "The amount of the filter to apply" },
 
112
    { GIMP_PDB_FLOAT,    "radius",   "The filter radius" },
 
113
    { GIMP_PDB_INT32,    "filter",   "The Filter to Run, "
 
114
                                     "0 - alpha trimmed mean; "
 
115
                                     "1 - optimal estimation (alpha controls noise variance); "
 
116
                                     "2 - edge enhancement" }
 
117
  };
 
118
 
 
119
  gimp_install_procedure ("plug_in_nlfilt",
 
120
                          "Nonlinear swiss army knife filter",
 
121
                          "This is the pnmnlfilt, in gimp's clothing.  "
 
122
                          "See the pnmnlfilt manpage for details.",
 
123
                          "Graeme W. Gill, gimp 0.99 plugin by Eric L. Hernes",
 
124
                          "Graeme W. Gill, Eric L. Hernes",
 
125
                          "1997",
 
126
                          N_("_NL Filter..."),
 
127
                          "RGB,GRAY",
 
128
                          GIMP_PLUGIN,
 
129
                          G_N_ELEMENTS (args), 0,
 
130
                          args, NULL);
 
131
 
 
132
  gimp_plugin_menu_register ("plug_in_nlfilt", "<Image>/Filters/Enhance");
 
133
}
 
134
 
 
135
static void
 
136
run (const gchar      *name,
 
137
     gint              nparams,
 
138
     const GimpParam  *param,
 
139
     gint             *nreturn_vals,
 
140
     GimpParam       **return_vals)
 
141
{
 
142
  static GimpParam   values[1];
 
143
  GimpDrawable      *drawable;
 
144
  GimpRunMode        run_mode;
 
145
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
 
146
 
 
147
  run_mode = param[0].data.d_int32;
 
148
 
 
149
  INIT_I18N ();
 
150
 
 
151
  drawable = gimp_drawable_get (param[2].data.d_drawable);
 
152
 
 
153
  *nreturn_vals = 1;
 
154
  *return_vals  = values;
 
155
 
 
156
  values[0].type          = GIMP_PDB_STATUS;
 
157
  values[0].data.d_status = status;
 
158
 
 
159
  switch (run_mode)
 
160
    {
 
161
    case GIMP_RUN_INTERACTIVE:
 
162
      gimp_get_data ("plug_in_nlfilt", &nlfvals);
 
163
 
 
164
      if (! nlfilter_dialog (drawable))
 
165
        return;
 
166
      break;
 
167
 
 
168
    case GIMP_RUN_NONINTERACTIVE:
 
169
      if (nparams != 6)
 
170
        {
 
171
          status = GIMP_PDB_CALLING_ERROR;
 
172
        }
 
173
      else
 
174
        {
 
175
          nlfvals.alpha  = param[3].data.d_float;
 
176
          nlfvals.radius = param[4].data.d_float;
 
177
          nlfvals.filter = param[5].data.d_int32;
 
178
        }
 
179
 
 
180
      break;
 
181
 
 
182
    case GIMP_RUN_WITH_LAST_VALS:
 
183
      gimp_get_data ("plug_in_nlfilt", &nlfvals);
 
184
      break;
 
185
 
 
186
    default:
 
187
      break;
 
188
  }
 
189
 
 
190
  if (status == GIMP_PDB_SUCCESS)
 
191
    {
 
192
      nlfilter (drawable, NULL);
 
193
 
 
194
      /* Store data */
 
195
      if (run_mode == GIMP_RUN_INTERACTIVE)
 
196
        gimp_set_data ("plug_in_nlfilt", &nlfvals, sizeof (NLFilterValues));
 
197
    }
 
198
 
 
199
  values[0].data.d_status = status;
 
200
 
 
201
  gimp_drawable_detach (drawable);
 
202
}
 
203
 
 
204
/* pnmnlfilt.c - 4 in 1 (2 non-linear) filter
 
205
**             - smooth an anyimage
 
206
**             - do alpha trimmed mean filtering on an anyimage
 
207
**             - do optimal estimation smoothing on an anyimage
 
208
**             - do edge enhancement on an anyimage
 
209
**
 
210
** Version 1.0
 
211
**
 
212
** The implementation of an alpha-trimmed mean filter
 
213
** is based on the description in IEEE CG&A May 1990
 
214
** Page 23 by Mark E. Lee and Richard A. Redner.
 
215
**
 
216
** The paper recommends using a hexagon sampling region around each
 
217
** pixel being processed, allowing an effective sub pixel radius to be
 
218
** specified. The hexagon values are sythesised by area sampling the
 
219
** rectangular pixels with a hexagon grid. The seven hexagon values
 
220
** obtained from the 3x3 pixel grid are used to compute the alpha
 
221
** trimmed mean. Note that an alpha value of 0.0 gives a conventional
 
222
** mean filter (where the radius controls the contribution of
 
223
** surrounding pixels), while a value of 0.5 gives a median filter.
 
224
** Although there are only seven values to trim from before finding
 
225
** the mean, the algorithm has been extended from that described in
 
226
** CG&A by using interpolation, to allow a continuous selection of
 
227
** alpha value between and including 0.0 to 0.5  The useful values
 
228
** for radius are between 0.3333333 (where the filter will have no
 
229
** effect because only one pixel is sampled), to 1.0, where all
 
230
** pixels in the 3x3 grid are sampled.
 
231
**
 
232
** The optimal estimation filter is taken from an article "Converting Dithered
 
233
** Images Back to Gray Scale" by Allen Stenger, Dr Dobb's Journal, November
 
234
** 1992, and this article references "Digital Image Enhancement andNoise Filtering by
 
235
** Use of Local Statistics", Jong-Sen Lee, IEEE Transactions on Pattern Analysis and
 
236
** Machine Intelligence, March 1980.
 
237
**
 
238
** Also borrow the  technique used in pgmenhance(1) to allow edge
 
239
** enhancement if the alpha value is negative.
 
240
**
 
241
** Author:
 
242
**         Graeme W. Gill, 30th Jan 1993
 
243
**         graeme@labtam.oz.au
 
244
**
 
245
** Permission is hereby granted, to use, copy, modify, distribute,
 
246
** and sell this software and its associated documentation files
 
247
** (the "Software") for any purpose without fee, provided
 
248
** that:
 
249
**
 
250
**     1) The above copyright notices and this permission notice
 
251
**        accompany all source code copies of the Software and
 
252
**        related documentation.
 
253
** and
 
254
**
 
255
**     2) If executable code based on the Software only is distributed,
 
256
**        then the accompanying documentation must acknowledge that
 
257
**        "this software is based in part on the work of Graeme W. Gill".
 
258
** and
 
259
**
 
260
**     3) It is accepted that Graeme W. Gill (the "Author") accepts
 
261
**        NO LIABILITY for damages of any kind.  The Software is
 
262
**        provided without fee by the Author "AS-IS" and without
 
263
**        warranty of any kind, express, implied or otherwise,
 
264
**        including without limitation, any warranty of merchantability
 
265
**        or fitness for a particular purpose.
 
266
** and
 
267
**
 
268
**     4) These conditions apply to any software derived from or based
 
269
**        on the Software, not just to the unmodified library.
 
270
**
 
271
*/
 
272
 
 
273
/* ************************************************** */
 
274
/* Hexagon intersecting square area functions */
 
275
/* Compute the area of the intersection of a triangle */
 
276
/* and a rectangle */
 
277
 
 
278
static gdouble triang_area(gdouble, gdouble, gdouble, gdouble, gdouble,
 
279
                           gdouble, gdouble, gdouble, gint);
 
280
static gdouble rectang_area(gdouble, gdouble, gdouble, gdouble,
 
281
                            gdouble, gdouble, gdouble, gdouble);
 
282
static gdouble hex_area(gdouble, gdouble, gdouble, gdouble, gdouble);
 
283
 
 
284
static gint atfilt0 (gint *p);
 
285
static gint atfilt1 (gint *p);
 
286
static gint atfilt2 (gint *p);
 
287
static gint atfilt3 (gint *p);
 
288
static gint atfilt4 (gint *p);
 
289
static gint atfilt5 (gint *p);
 
290
 
 
291
gint (*atfuncs[6])(gint *) =
 
292
{
 
293
  atfilt0,
 
294
  atfilt1,
 
295
  atfilt2,
 
296
  atfilt3,
 
297
  atfilt4,
 
298
  atfilt5
 
299
};
 
300
 
 
301
gint noisevariance;      /* global so that pixel processing code can get at it quickly */
 
302
 
 
303
#define MXIVAL 255    /* maximum input value */
 
304
#define NOIVAL (MXIVAL + 1)             /* number of possible input values */
 
305
 
 
306
#define SCALEB 8                                /* scale bits */
 
307
#define SCALE (1 << SCALEB)     /* scale factor */
 
308
#define MXSVAL (MXIVAL * SCALE) /* maximum scaled values */
 
309
 
 
310
#define CSCALEB 2                               /* coarse scale bits */
 
311
#define CSCALE (1 << CSCALEB)   /* coarse scale factor */
 
312
#define MXCSVAL (MXIVAL * CSCALE)       /* maximum coarse scaled values */
 
313
#define NOCSVAL (MXCSVAL + 1)   /* number of coarse scaled values */
 
314
#define SCTOCSC(x) ((x) >> (SCALEB - CSCALEB))  /* convert from scaled to coarse scaled */
 
315
#define CSCTOSC(x) ((x) << (SCALEB - CSCALEB))  /* convert from course scaled to scaled */
 
316
 
 
317
/* round and scale floating point to scaled integer */
 
318
#define SROUND(x) ((gint)(((x) * (gdouble)SCALE) + 0.5))
 
319
/* round and un-scale scaled integer value */
 
320
#define RUNSCALE(x) (((x) + (1 << (SCALEB-1))) >> SCALEB)       /* rounded un-scale */
 
321
#define UNSCALE(x) ((x) >> SCALEB)
 
322
 
 
323
/* Note: modified by David Hodson, nlfiltRow now accesses
 
324
 * srclast, srcthis, and srcnext from [-bpp] to [width*bpp-1].
 
325
 * Beware if you use this code anywhere else!
 
326
 */
 
327
static inline void
 
328
nlfiltRow (guchar *srclast, guchar *srcthis, guchar *srcnext, guchar *dst,
 
329
           gint width, gint bpp, gint filtno)
 
330
{
 
331
  gint    pf[9];
 
332
  guchar *ip0, *ip1, *ip2, *or, *orend;
 
333
 
 
334
  or = dst;
 
335
  orend = dst + width * bpp;
 
336
  ip0 = srclast;
 
337
  ip1 = srcthis;
 
338
  ip2 = srcnext;
 
339
 
 
340
  for (or = dst; or < orend; ip0++, ip1++, ip2++, or++)
 
341
    {
 
342
      pf[0] = *ip1;
 
343
      pf[1] = *(ip1 - bpp);
 
344
      pf[2] = *(ip2 - bpp);
 
345
      pf[3] = *(ip2);
 
346
      pf[4] = *(ip2 + bpp);
 
347
      pf[5] = *(ip1 + bpp);
 
348
      pf[6] = *(ip0 + bpp);
 
349
      pf[7] = *(ip0);
 
350
      pf[8] = *(ip0 - bpp);
 
351
      *or=(atfuncs[filtno])(pf);
 
352
    }
 
353
}
 
354
 
 
355
/* We restrict radius to the values: 0.333333 <= radius <= 1.0 */
 
356
/* so that no fewer and no more than a 3x3 grid of pixels around */
 
357
/* the pixel in question needs to be read. Given this, we only */
 
358
/* need 3 or 4 weightings per hexagon, as follows: */
 
359
/*                  _ _                         */
 
360
/* Virtical hex:   |_|_|  1 2                   */
 
361
/*                 |X|_|  0 3                   */
 
362
/*                                       _      */
 
363
/*              _                      _|_|   1 */
 
364
/* Middle hex: |_| 1  Horizontal hex: |X|_| 0 2 */
 
365
/*             |X| 0                    |_|   3 */
 
366
/*             |_| 2                            */
 
367
 
 
368
/* all filters */
 
369
gint V0[NOIVAL],V1[NOIVAL],V2[NOIVAL],V3[NOIVAL];   /* vertical hex */
 
370
gint M0[NOIVAL],M1[NOIVAL],M2[NOIVAL];              /* middle hex */
 
371
gint H0[NOIVAL],H1[NOIVAL],H2[NOIVAL],H3[NOIVAL];   /* horizontal hex */
 
372
 
 
373
/* alpha trimmed and edge enhancement only */
 
374
gint ALFRAC[NOIVAL * 8];               /* fractional alpha divider table */
 
375
 
 
376
/* optimal estimation only */
 
377
gint AVEDIV[7 * NOCSVAL];              /* divide by 7 to give average value */
 
378
gint SQUARE[2 * NOCSVAL];              /* scaled square lookup table */
 
379
 
 
380
/* Table initialisation function - return alpha range */
 
381
static inline gint
 
382
nlfiltInit (gdouble alpha, gdouble radius, FilterType filter)
 
383
{
 
384
   gint alpharange;                 /* alpha range value 0 - 3 */
 
385
   gdouble meanscale;               /* scale for finding mean */
 
386
   gdouble mmeanscale;              /* scale for finding mean - midle hex */
 
387
   gdouble alphafraction;   /* fraction of next largest/smallest
 
388
                             *  to subtract from sum
 
389
                             */
 
390
   switch (filter)
 
391
     {
 
392
       case filter_alpha_trim:
 
393
         {
 
394
          gdouble noinmean;
 
395
          /* alpha only makes sense in range 0.0 - 0.5 */
 
396
          alpha /= 2.0;
 
397
              /* number of elements (out of a possible 7) used in the mean */
 
398
          noinmean = ((0.5 - alpha) * 12.0) + 1.0;
 
399
          mmeanscale = meanscale = 1.0/noinmean;
 
400
          if (alpha == 0.0) {                    /* mean filter */
 
401
             alpharange = 0;
 
402
             alphafraction = 0.0;            /* not used */
 
403
          } else if (alpha < (1.0/6.0)) {    /* mean of 5 to 7 middle values */
 
404
             alpharange = 1;
 
405
             alphafraction = (7.0 - noinmean)/2.0;
 
406
          } else if (alpha < (1.0/3.0)) {    /* mean of 3 to 5 middle values */
 
407
             alpharange = 2;
 
408
             alphafraction = (5.0 - noinmean)/2.0;
 
409
          } else {                           /* mean of 1 to 3 middle values */
 
410
                                             /* alpha==0.5  => median filter */
 
411
             alpharange = 3;
 
412
             alphafraction = (3.0 - noinmean)/2.0;
 
413
          }
 
414
       }
 
415
       break;
 
416
       case filter_opt_est: {
 
417
          gint i;
 
418
          gdouble noinmean = 7.0;
 
419
 
 
420
              /* edge enhancement function */
 
421
          alpharange = 5;
 
422
 
 
423
              /* compute scaled hex values */
 
424
          mmeanscale=meanscale=1.0;
 
425
 
 
426
              /* Set up 1:1 division lookup - not used */
 
427
          alphafraction=1.0/noinmean;
 
428
 
 
429
              /* estimate of noise variance */
 
430
          noisevariance = alpha * (gdouble)255;
 
431
          noisevariance = noisevariance * noisevariance / 8.0;
 
432
 
 
433
              /* set yp optimal estimation specific stuff */
 
434
 
 
435
          for (i=0;i<(7*NOCSVAL);i++) { /* divide scaled value by 7 lookup */
 
436
             AVEDIV[i] = CSCTOSC(i)/7;       /* scaled divide by 7 */
 
437
          }
 
438
              /* compute square and rescale by
 
439
               * (val >> (2 * SCALEB + 2)) table
 
440
               */
 
441
          for (i=0;i<(2*NOCSVAL);i++) {
 
442
             gint val;
 
443
                 /* NOCSVAL offset to cope with -ve input values */
 
444
             val = CSCTOSC(i - NOCSVAL);
 
445
             SQUARE[i] = (val * val) >> (2 * SCALEB + 2);
 
446
          }
 
447
       }
 
448
       break;
 
449
       case filter_edge_enhance: {
 
450
          if (alpha == 1.0) alpha = 0.99;
 
451
          alpharange = 4;
 
452
              /* mean of 7 and scaled by -alpha/(1-alpha) */
 
453
          meanscale = 1.0 * (-alpha/((1.0 - alpha) * 7.0));
 
454
 
 
455
              /* middle pixel has 1/(1-alpha) as well */
 
456
          mmeanscale = 1.0 * (1.0/(1.0 - alpha) + meanscale);
 
457
          alphafraction = 0.0;    /* not used */
 
458
       }
 
459
       break;
 
460
       default:
 
461
          fprintf(stderr, "unknown filter %d\n", filter);
 
462
          return -1;
 
463
   }
 
464
       /*
 
465
        * Setup pixel weighting tables -
 
466
        * note we pre-compute mean division here too.
 
467
        */
 
468
   {
 
469
      gint i;
 
470
      gdouble hexhoff,hexvoff;
 
471
      gdouble tabscale,mtabscale;
 
472
      gdouble v0,v1,v2,v3,m0,m1,m2,h0,h1,h2,h3;
 
473
 
 
474
          /* horizontal offset of virtical hex centers */
 
475
      hexhoff = radius/2;
 
476
 
 
477
          /* vertical offset of virtical hex centers */
 
478
      hexvoff = 3.0 * radius/sqrt(12.0);
 
479
 
 
480
          /*
 
481
           * scale tables to normalise by hexagon
 
482
           * area, and number of hexes used in mean
 
483
           */
 
484
      tabscale = meanscale / (radius * hexvoff);
 
485
      mtabscale = mmeanscale / (radius * hexvoff);
 
486
      v0 = hex_area(0.0,0.0,hexhoff,hexvoff,radius) * tabscale;
 
487
      v1 = hex_area(0.0,1.0,hexhoff,hexvoff,radius) * tabscale;
 
488
      v2 = hex_area(1.0,1.0,hexhoff,hexvoff,radius) * tabscale;
 
489
      v3 = hex_area(1.0,0.0,hexhoff,hexvoff,radius) * tabscale;
 
490
      m0 = hex_area(0.0,0.0,0.0,0.0,radius) * mtabscale;
 
491
      m1 = hex_area(0.0,1.0,0.0,0.0,radius) * mtabscale;
 
492
      m2 = hex_area(0.0,-1.0,0.0,0.0,radius) * mtabscale;
 
493
      h0 = hex_area(0.0,0.0,radius,0.0,radius) * tabscale;
 
494
      h1 = hex_area(1.0,1.0,radius,0.0,radius) * tabscale;
 
495
      h2 = hex_area(1.0,0.0,radius,0.0,radius) * tabscale;
 
496
      h3 = hex_area(1.0,-1.0,radius,0.0,radius) * tabscale;
 
497
 
 
498
      for (i=0; i <= MXIVAL; i++) {
 
499
         gdouble fi;
 
500
         fi = (gdouble)i;
 
501
         V0[i] = SROUND(fi * v0);
 
502
         V1[i] = SROUND(fi * v1);
 
503
         V2[i] = SROUND(fi * v2);
 
504
         V3[i] = SROUND(fi * v3);
 
505
         M0[i] = SROUND(fi * m0);
 
506
         M1[i] = SROUND(fi * m1);
 
507
         M2[i] = SROUND(fi * m2);
 
508
         H0[i] = SROUND(fi * h0);
 
509
         H1[i] = SROUND(fi * h1);
 
510
         H2[i] = SROUND(fi * h2);
 
511
         H3[i] = SROUND(fi * h3);
 
512
      }
 
513
          /* set up alpha fraction lookup table used on big/small */
 
514
      for (i=0; i < (NOIVAL * 8); i++) {
 
515
         ALFRAC[i] = SROUND((gdouble)i * alphafraction);
 
516
      }
 
517
   }
 
518
   return alpharange;
 
519
}
 
520
 
 
521
/* Core pixel processing function - hand it 3x3 pixels and return result. */
 
522
/* Mean filter */
 
523
static gint
 
524
atfilt0(gint32 *p)
 
525
{
 
526
   gint retv;
 
527
       /* map to scaled hexagon values */
 
528
   retv = M0[p[0]] + M1[p[3]] + M2[p[7]];
 
529
   retv += H0[p[0]] + H1[p[2]] + H2[p[1]] + H3[p[8]];
 
530
   retv += V0[p[0]] + V1[p[3]] + V2[p[2]] + V3[p[1]];
 
531
   retv += V0[p[0]] + V1[p[3]] + V2[p[4]] + V3[p[5]];
 
532
   retv += H0[p[0]] + H1[p[4]] + H2[p[5]] + H3[p[6]];
 
533
   retv += V0[p[0]] + V1[p[7]] + V2[p[6]] + V3[p[5]];
 
534
   retv += V0[p[0]] + V1[p[7]] + V2[p[8]] + V3[p[1]];
 
535
   return UNSCALE(retv);
 
536
}
 
537
 
 
538
/* Mean of 5 - 7 middle values */
 
539
static gint
 
540
atfilt1 (gint32 *p)
 
541
{
 
542
   gint h0,h1,h2,h3,h4,h5,h6;       /* hexagon values    2 3   */
 
543
                                    /*                  1 0 4  */
 
544
                                    /*                   6 5   */
 
545
   gint big,small;
 
546
       /* map to scaled hexagon values */
 
547
   h0 = M0[p[0]] + M1[p[3]] + M2[p[7]];
 
548
   h1 = H0[p[0]] + H1[p[2]] + H2[p[1]] + H3[p[8]];
 
549
   h2 = V0[p[0]] + V1[p[3]] + V2[p[2]] + V3[p[1]];
 
550
   h3 = V0[p[0]] + V1[p[3]] + V2[p[4]] + V3[p[5]];
 
551
   h4 = H0[p[0]] + H1[p[4]] + H2[p[5]] + H3[p[6]];
 
552
   h5 = V0[p[0]] + V1[p[7]] + V2[p[6]] + V3[p[5]];
 
553
   h6 = V0[p[0]] + V1[p[7]] + V2[p[8]] + V3[p[1]];
 
554
       /* sum values and also discover the largest and smallest */
 
555
   big = small = h0;
 
556
#define CHECK(xx) \
 
557
        h0 += xx; \
 
558
        if (xx > big) \
 
559
                big = xx; \
 
560
        else if (xx < small) \
 
561
                small = xx;
 
562
        CHECK(h1)
 
563
        CHECK(h2)
 
564
        CHECK(h3)
 
565
        CHECK(h4)
 
566
        CHECK(h5)
 
567
        CHECK(h6)
 
568
#undef CHECK
 
569
       /* Compute mean of middle 5-7 values */
 
570
   return UNSCALE(h0 -ALFRAC[(big + small)>>SCALEB]);
 
571
}
 
572
 
 
573
/* Mean of 3 - 5 middle values */
 
574
static gint
 
575
atfilt2 (gint32 *p)
 
576
{
 
577
   gint h0,h1,h2,h3,h4,h5,h6;       /* hexagon values    2 3   */
 
578
                                    /*                  1 0 4  */
 
579
                                    /*                   6 5   */
 
580
   gint big0,big1,small0,small1;
 
581
       /* map to scaled hexagon values */
 
582
   h0 = M0[p[0]] + M1[p[3]] + M2[p[7]];
 
583
   h1 = H0[p[0]] + H1[p[2]] + H2[p[1]] + H3[p[8]];
 
584
   h2 = V0[p[0]] + V1[p[3]] + V2[p[2]] + V3[p[1]];
 
585
   h3 = V0[p[0]] + V1[p[3]] + V2[p[4]] + V3[p[5]];
 
586
   h4 = H0[p[0]] + H1[p[4]] + H2[p[5]] + H3[p[6]];
 
587
   h5 = V0[p[0]] + V1[p[7]] + V2[p[6]] + V3[p[5]];
 
588
   h6 = V0[p[0]] + V1[p[7]] + V2[p[8]] + V3[p[1]];
 
589
       /* sum values and also discover the 2 largest and 2 smallest */
 
590
   big0 = small0 = h0;
 
591
   small1 = G_MAXINT;
 
592
   big1 = 0;
 
593
#define CHECK(xx) \
 
594
        h0 += xx; \
 
595
        if (xx > big1) \
 
596
                { \
 
597
                if (xx > big0) \
 
598
                        { \
 
599
                        big1 = big0; \
 
600
                        big0 = xx; \
 
601
                        } \
 
602
                else \
 
603
                        big1 = xx; \
 
604
                } \
 
605
        if (xx < small1) \
 
606
                { \
 
607
                if (xx < small0) \
 
608
                        { \
 
609
                        small1 = small0; \
 
610
                        small0 = xx; \
 
611
                        } \
 
612
                else \
 
613
                        small1 = xx; \
 
614
                }
 
615
        CHECK(h1)
 
616
        CHECK(h2)
 
617
        CHECK(h3)
 
618
        CHECK(h4)
 
619
        CHECK(h5)
 
620
        CHECK(h6)
 
621
#undef CHECK
 
622
       /* Compute mean of middle 3-5 values */
 
623
  return UNSCALE(h0 -big0 -small0 -ALFRAC[(big1 + small1)>>SCALEB]);
 
624
}
 
625
 
 
626
/*
 
627
 * Mean of 1 - 3 middle values.
 
628
 * If only 1 value, then this is a median filter.
 
629
 */
 
630
static gint32
 
631
atfilt3(gint32 *p)
 
632
{
 
633
   gint h0,h1,h2,h3,h4,h5,h6;       /* hexagon values    2 3   */
 
634
                                   /*                  1 0 4  */
 
635
                                   /*                   6 5   */
 
636
   gint big0,big1,big2,small0,small1,small2;
 
637
       /* map to scaled hexagon values */
 
638
   h0 = M0[p[0]] + M1[p[3]] + M2[p[7]];
 
639
   h1 = H0[p[0]] + H1[p[2]] + H2[p[1]] + H3[p[8]];
 
640
   h2 = V0[p[0]] + V1[p[3]] + V2[p[2]] + V3[p[1]];
 
641
   h3 = V0[p[0]] + V1[p[3]] + V2[p[4]] + V3[p[5]];
 
642
   h4 = H0[p[0]] + H1[p[4]] + H2[p[5]] + H3[p[6]];
 
643
   h5 = V0[p[0]] + V1[p[7]] + V2[p[6]] + V3[p[5]];
 
644
   h6 = V0[p[0]] + V1[p[7]] + V2[p[8]] + V3[p[1]];
 
645
       /* sum values and also discover the 3 largest and 3 smallest */
 
646
   big0 = small0 = h0;
 
647
   small1 = small2 = G_MAXINT;
 
648
   big1 = big2 = 0;
 
649
#define CHECK(xx) \
 
650
        h0 += xx; \
 
651
        if (xx > big2) \
 
652
                { \
 
653
                if (xx > big1) \
 
654
                        { \
 
655
                        if (xx > big0) \
 
656
                                { \
 
657
                                big2 = big1; \
 
658
                                big1 = big0; \
 
659
                                big0 = xx; \
 
660
                                } \
 
661
                        else \
 
662
                                { \
 
663
                                big2 = big1; \
 
664
                                big1 = xx; \
 
665
                                } \
 
666
                        } \
 
667
                else \
 
668
                        big2 = xx; \
 
669
                } \
 
670
        if (xx < small2) \
 
671
                { \
 
672
                if (xx < small1) \
 
673
                        { \
 
674
                        if (xx < small0) \
 
675
                                { \
 
676
                                small2 = small1; \
 
677
                                small1 = small0; \
 
678
                                small0 = xx; \
 
679
                                } \
 
680
                        else \
 
681
                                { \
 
682
                                small2 = small1; \
 
683
                                small1 = xx; \
 
684
                                } \
 
685
                        } \
 
686
                else \
 
687
                        small2 = xx; \
 
688
                }
 
689
        CHECK(h1)
 
690
        CHECK(h2)
 
691
        CHECK(h3)
 
692
        CHECK(h4)
 
693
        CHECK(h5)
 
694
        CHECK(h6)
 
695
#undef CHECK
 
696
       /* Compute mean of middle 1-3 values */
 
697
   return  UNSCALE(h0-big0-big1-small0-small1-ALFRAC[(big2+small2)>>SCALEB]);
 
698
}
 
699
 
 
700
/* Edge enhancement */
 
701
static gint
 
702
atfilt4 (gint *p)
 
703
{
 
704
   gint hav;
 
705
       /* map to scaled hexagon values and compute enhance value */
 
706
   hav = M0[p[0]] + M1[p[3]] + M2[p[7]];
 
707
   hav += H0[p[0]] + H1[p[2]] + H2[p[1]] + H3[p[8]];
 
708
   hav += V0[p[0]] + V1[p[3]] + V2[p[2]] + V3[p[1]];
 
709
   hav += V0[p[0]] + V1[p[3]] + V2[p[4]] + V3[p[5]];
 
710
   hav += H0[p[0]] + H1[p[4]] + H2[p[5]] + H3[p[6]];
 
711
   hav += V0[p[0]] + V1[p[7]] + V2[p[6]] + V3[p[5]];
 
712
   hav += V0[p[0]] + V1[p[7]] + V2[p[8]] + V3[p[1]];
 
713
   if (hav < 0)
 
714
      hav = 0;
 
715
   hav = UNSCALE(hav);
 
716
   if (hav > (gdouble)255)
 
717
      hav = (gdouble)255;
 
718
   return hav;
 
719
}
 
720
 
 
721
/* Optimal estimation - do smoothing in inverse proportion */
 
722
/* to the local variance. */
 
723
/* notice we use the globals noisevariance */
 
724
gint
 
725
atfilt5(gint *p) {
 
726
   gint mean,variance,temp;
 
727
   gint h0,h1,h2,h3,h4,h5,h6;       /* hexagon values    2 3   */
 
728
                                   /*                  1 0 4  */
 
729
                                   /*                   6 5   */
 
730
       /* map to scaled hexagon values */
 
731
   h0 = M0[p[0]] + M1[p[3]] + M2[p[7]];
 
732
   h1 = H0[p[0]] + H1[p[2]] + H2[p[1]] + H3[p[8]];
 
733
   h2 = V0[p[0]] + V1[p[3]] + V2[p[2]] + V3[p[1]];
 
734
   h3 = V0[p[0]] + V1[p[3]] + V2[p[4]] + V3[p[5]];
 
735
   h4 = H0[p[0]] + H1[p[4]] + H2[p[5]] + H3[p[6]];
 
736
   h5 = V0[p[0]] + V1[p[7]] + V2[p[6]] + V3[p[5]];
 
737
   h6 = V0[p[0]] + V1[p[7]] + V2[p[8]] + V3[p[1]];
 
738
   mean = h0 + h1 + h2 + h3 + h4 + h5 + h6;
 
739
       /* compute scaled mean by dividing by 7 */
 
740
   mean = AVEDIV[SCTOCSC(mean)];
 
741
 
 
742
       /* compute scaled variance */
 
743
   temp = (h1 - mean); variance = SQUARE[NOCSVAL + SCTOCSC(temp)];
 
744
 
 
745
       /* and rescale to keep */
 
746
   temp = (h2 - mean); variance += SQUARE[NOCSVAL + SCTOCSC(temp)];
 
747
 
 
748
 /* within 32 bit limits */
 
749
   temp = (h3 - mean); variance += SQUARE[NOCSVAL + SCTOCSC(temp)];
 
750
   temp = (h4 - mean); variance += SQUARE[NOCSVAL + SCTOCSC(temp)];
 
751
   temp = (h5 - mean); variance += SQUARE[NOCSVAL + SCTOCSC(temp)];
 
752
   temp = (h6 - mean); variance += SQUARE[NOCSVAL + SCTOCSC(temp)];
 
753
       /* (temp = h0 - mean) */
 
754
   temp = (h0 - mean); variance += SQUARE[NOCSVAL + SCTOCSC(temp)];
 
755
   if (variance != 0)      /* avoid possible divide by 0 */
 
756
          /* optimal estimate */
 
757
      temp = mean + (variance * temp) / (variance + noisevariance);
 
758
   else temp = h0;
 
759
   if (temp < 0)
 
760
      temp = 0;
 
761
   temp = RUNSCALE(temp);
 
762
   if (temp > (gdouble)255) temp = (gdouble)255;
 
763
   return temp;
 
764
}
 
765
 
 
766
 
 
767
/* Triangle orientation is per geometric axes (not graphical axies) */
 
768
 
 
769
#define NW 0    /* North west triangle /| */
 
770
#define NE 1    /* North east triangle |\ */
 
771
#define SW 2    /* South west triangle \| */
 
772
#define SE 3    /* South east triangle |/ */
 
773
#define STH 2
 
774
#define EST 1
 
775
 
 
776
#define SWAPI(a,b) (t = a, a = -b, b = -t)
 
777
 
 
778
/* compute the area of overlap of a hexagon diameter d, */
 
779
/* centered at hx,hy, with a unit square of center sx,sy. */
 
780
static gdouble
 
781
hex_area (gdouble sx, gdouble sy, gdouble hx, gdouble hy, gdouble d)
 
782
{
 
783
   gdouble hx0,hx1,hx2,hy0,hy1,hy2,hy3;
 
784
   gdouble sx0,sx1,sy0,sy1;
 
785
 
 
786
       /* compute square co-ordinates */
 
787
   sx0 = sx - 0.5;
 
788
   sy0 = sy - 0.5;
 
789
   sx1 = sx + 0.5;
 
790
   sy1 = sy + 0.5;
 
791
       /* compute hexagon co-ordinates */
 
792
   hx0 = hx - d/2.0;
 
793
   hx1 = hx;
 
794
   hx2 = hx + d/2.0;
 
795
   hy0 = hy - 0.5773502692 * d;    /* d / sqrt(3) */
 
796
   hy1 = hy - 0.2886751346 * d;    /* d / sqrt(12) */
 
797
   hy2 = hy + 0.2886751346 * d;    /* d / sqrt(12) */
 
798
   hy3 = hy + 0.5773502692 * d;    /* d / sqrt(3) */
 
799
 
 
800
   return
 
801
      triang_area(sx0,sy0,sx1,sy1,hx0,hy2,hx1,hy3,NW) +
 
802
      triang_area(sx0,sy0,sx1,sy1,hx1,hy2,hx2,hy3,NE) +
 
803
      rectang_area(sx0,sy0,sx1,sy1,hx0,hy1,hx2,hy2) +
 
804
      triang_area(sx0,sy0,sx1,sy1,hx0,hy0,hx1,hy1,SW) +
 
805
      triang_area(sx0,sy0,sx1,sy1,hx1,hy0,hx2,hy1,SE);
 
806
}
 
807
 
 
808
static gdouble
 
809
triang_area (gdouble rx0, gdouble ry0, gdouble rx1, gdouble ry1, gdouble tx0,
 
810
             gdouble ty0, gdouble tx1, gdouble ty1, gint tt)
 
811
{
 
812
   gdouble a,b,c,d;
 
813
   gdouble lx0,ly0,lx1,ly1;
 
814
       /* Convert everything to a NW triangle */
 
815
   if (tt & STH) {
 
816
      gdouble t;
 
817
      SWAPI(ry0,ry1);
 
818
      SWAPI(ty0,ty1);
 
819
   } if (tt & EST) {
 
820
      gdouble t;
 
821
      SWAPI(rx0,rx1);
 
822
      SWAPI(tx0,tx1);
 
823
   }
 
824
       /* Compute overlapping box */
 
825
   if (tx0 > rx0)
 
826
      rx0 = tx0;
 
827
   if (ty0 > ry0)
 
828
      ry0 = ty0;
 
829
   if (tx1 < rx1)
 
830
      rx1 = tx1;
 
831
   if (ty1 < ry1)
 
832
      ry1 = ty1;
 
833
   if (rx1 <= rx0 || ry1 <= ry0)
 
834
      return 0.0;
 
835
       /* Need to compute diagonal line intersection with the box */
 
836
       /* First compute co-efficients to formulas x = a + by and y = c + dx */
 
837
   b = (tx1 - tx0)/(ty1 - ty0);
 
838
   a = tx0 - b * ty0;
 
839
   d = (ty1 - ty0)/(tx1 - tx0);
 
840
   c = ty0 - d * tx0;
 
841
 
 
842
       /* compute top or right intersection */
 
843
   tt = 0;
 
844
   ly1 = ry1;
 
845
   lx1 = a + b * ly1;
 
846
   if (lx1 <= rx0)
 
847
      return (rx1 - rx0) * (ry1 - ry0);
 
848
   else if (lx1 > rx1) {     /* could be right hand side */
 
849
      lx1 = rx1;
 
850
      ly1 = c + d * lx1;
 
851
      if (ly1 <= ry0)
 
852
         return (rx1 - rx0) * (ry1 - ry0);
 
853
      tt = 1; /* right hand side intersection */
 
854
   }
 
855
       /* compute left or bottom intersection */
 
856
   lx0 = rx0;
 
857
   ly0 = c + d * lx0;
 
858
   if (ly0 >= ry1)
 
859
      return (rx1 - rx0) * (ry1 - ry0);
 
860
   else if (ly0 < ry0) {    /* could be right hand side */
 
861
      ly0 = ry0;
 
862
      lx0 = a + b * ly0;
 
863
      if (lx0 >= rx1)
 
864
         return (rx1 - rx0) * (ry1 - ry0);
 
865
      tt |= 2;        /* bottom intersection */
 
866
   }
 
867
 
 
868
   if (tt == 0) {    /* top and left intersection */
 
869
                       /* rectangle minus triangle */
 
870
      return ((rx1 - rx0) * (ry1 - ry0))
 
871
         - (0.5 * (lx1 - rx0) * (ry1 - ly0));
 
872
   }
 
873
   else if (tt == 1) {       /* right and left intersection */
 
874
      return ((rx1 - rx0) * (ly0 - ry0))
 
875
         + (0.5 * (rx1 - rx0) * (ly1 - ly0));
 
876
   } else if (tt == 2) {      /* top and bottom intersection */
 
877
      return ((rx1 - lx1) * (ry1 - ry0))
 
878
         + (0.5 * (lx1 - lx0) * (ry1 - ry0));
 
879
   } else { /* tt == 3 */      /* right and bottom intersection */
 
880
          /* triangle */
 
881
      return (0.5 * (rx1 - lx0) * (ly1 - ry0));
 
882
   }
 
883
}
 
884
 
 
885
/* Compute rectangle area */
 
886
static gdouble
 
887
rectang_area (gdouble rx0, gdouble ry0, gdouble rx1, gdouble ry1, gdouble tx0,
 
888
              gdouble ty0, gdouble tx1, gdouble ty1)
 
889
{
 
890
  /* Compute overlapping box */
 
891
   if (tx0 > rx0)
 
892
      rx0 = tx0;
 
893
   if (ty0 > ry0)
 
894
      ry0 = ty0;
 
895
   if (tx1 < rx1)
 
896
      rx1 = tx1;
 
897
   if (ty1 < ry1)
 
898
      ry1 = ty1;
 
899
   if (rx1 <= rx0 || ry1 <= ry0)
 
900
      return 0.0;
 
901
   return (rx1 - rx0) * (ry1 - ry0);
 
902
}
 
903
 
 
904
static void
 
905
nlfilter (GimpDrawable *drawable,
 
906
          GimpPreview  *preview)
 
907
{
 
908
  GimpPixelRgn  srcPr, dstPr;
 
909
  guchar       *srcbuf, *dstbuf;
 
910
  guchar       *lastrow, *thisrow, *nextrow, *temprow;
 
911
  gint          x1, x2, y1, y2;
 
912
  guint         width, height, bpp;
 
913
  gint          filtno, y, rowsize, exrowsize, p_update;
 
914
 
 
915
  if (preview)
 
916
    {
 
917
      gimp_preview_get_position (preview, &x1, &y1);
 
918
      gimp_preview_get_size (preview, &width, &height);
 
919
      x2 = x1 + width;
 
920
      y2 = y1 + height;
 
921
    }
 
922
  else
 
923
    {
 
924
      gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
 
925
      width = x2 - x1;
 
926
      height = y2 - y1;
 
927
    }
 
928
  bpp = drawable->bpp;
 
929
 
 
930
  rowsize = width * bpp;
 
931
  exrowsize = (width + 2) * bpp;
 
932
  p_update = width / 20 + 1;
 
933
 
 
934
  gimp_tile_cache_ntiles (2 * (width / gimp_tile_width () + 1));
 
935
 
 
936
  gimp_pixel_rgn_init (&srcPr, drawable,
 
937
                       x1, y1, width, height, FALSE, FALSE);
 
938
  gimp_pixel_rgn_init (&dstPr, drawable,
 
939
                       x1, y1, width, height,
 
940
                       preview == NULL, TRUE);
 
941
 
 
942
  /* source buffer gives one pixel margin all around destination buffer */
 
943
  srcbuf = g_new0 (guchar, exrowsize * 3);
 
944
  dstbuf = g_new0 (guchar, rowsize);
 
945
 
 
946
  /* pointers to second pixel in each source row */
 
947
  lastrow = srcbuf + bpp;
 
948
  thisrow = lastrow + exrowsize;
 
949
  nextrow = thisrow + exrowsize;
 
950
 
 
951
  filtno = nlfiltInit (nlfvals.alpha, nlfvals.radius, nlfvals.filter);
 
952
 
 
953
  if (!preview)
 
954
    gimp_progress_init (_("NL Filter..."));
 
955
 
 
956
  /* first row */
 
957
  gimp_pixel_rgn_get_row (&srcPr, thisrow, x1, y1, width);
 
958
  /* copy thisrow[0] to thisrow[-1], thisrow[width-1] to thisrow[width] */
 
959
  memcpy (thisrow - bpp, thisrow, bpp);
 
960
  memcpy (thisrow + rowsize, thisrow + rowsize - bpp, bpp);
 
961
  /* copy whole thisrow to lastrow */
 
962
  memcpy (lastrow - bpp, thisrow - bpp, exrowsize);
 
963
 
 
964
  for (y = y1; y < y2 - 1; y++)
 
965
    {
 
966
      if (((y % p_update) == 0) && !preview)
 
967
        gimp_progress_update ((gdouble) y / (gdouble) height);
 
968
 
 
969
      gimp_pixel_rgn_get_row (&srcPr, nextrow, x1, y + 1, width);
 
970
      memcpy (nextrow - bpp, nextrow, bpp);
 
971
      memcpy (nextrow + rowsize, nextrow + rowsize - bpp, bpp);
 
972
      nlfiltRow (lastrow, thisrow, nextrow, dstbuf, width, bpp, filtno);
 
973
      gimp_pixel_rgn_set_row (&dstPr, dstbuf, x1, y, width);
 
974
      /* rotate row buffers */
 
975
      temprow = lastrow; lastrow = thisrow;
 
976
      thisrow = nextrow; nextrow = temprow;
 
977
    }
 
978
 
 
979
  /* last row */
 
980
  memcpy (nextrow - bpp, thisrow - bpp, exrowsize);
 
981
  nlfiltRow (lastrow, thisrow, nextrow, dstbuf, width, bpp, filtno);
 
982
  gimp_pixel_rgn_set_row (&dstPr, dstbuf, x1, y2 - 1, width);
 
983
 
 
984
  g_free (srcbuf);
 
985
  g_free (dstbuf);
 
986
 
 
987
  if (preview)
 
988
    {
 
989
      gimp_drawable_preview_draw_region (GIMP_DRAWABLE_PREVIEW (preview),
 
990
                                         &dstPr);
 
991
    }
 
992
  else
 
993
    {
 
994
      gimp_drawable_flush (drawable);
 
995
      gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
 
996
      gimp_drawable_update (drawable->drawable_id, x1, y1, width, height);
 
997
      gimp_displays_flush ();
 
998
    }
 
999
}
 
1000
 
 
1001
static gboolean
 
1002
nlfilter_dialog (GimpDrawable *drawable)
 
1003
{
 
1004
  GtkWidget *dialog;
 
1005
  GtkWidget *main_vbox;
 
1006
  GtkWidget *preview;
 
1007
  GtkWidget *frame;
 
1008
  GtkWidget *alpha_trim;
 
1009
  GtkWidget *opt_est;
 
1010
  GtkWidget *edge_enhance;
 
1011
  GtkWidget *table;
 
1012
  GtkObject *adj;
 
1013
  gboolean   run;
 
1014
 
 
1015
  gimp_ui_init ("nlfilt", TRUE);
 
1016
 
 
1017
  dialog = gimp_dialog_new (_("NL Filter"), "nlfilt",
 
1018
                         NULL, 0,
 
1019
                         gimp_standard_help_func, "plug-in-nlfilt",
 
1020
 
 
1021
                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
1022
                         GTK_STOCK_OK,     GTK_RESPONSE_OK,
 
1023
 
 
1024
                         NULL);
 
1025
 
 
1026
  main_vbox = gtk_vbox_new (FALSE, 12);
 
1027
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
 
1028
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), main_vbox);
 
1029
  gtk_widget_show (main_vbox);
 
1030
 
 
1031
  preview = gimp_drawable_preview_new (drawable, &nlfvals.preview);
 
1032
  gtk_box_pack_start_defaults (GTK_BOX (main_vbox), preview);
 
1033
  gtk_widget_show (preview);
 
1034
  g_signal_connect_swapped (preview, "invalidated",
 
1035
                            G_CALLBACK (nlfilter),
 
1036
                            drawable);
 
1037
 
 
1038
  frame = gimp_int_radio_group_new (TRUE, _("Filter"),
 
1039
                                    G_CALLBACK (gimp_radio_button_update),
 
1040
                                    &nlfvals.filter, nlfvals.filter,
 
1041
 
 
1042
                                    _("_Alpha trimmed mean"),
 
1043
                                    filter_alpha_trim, &alpha_trim,
 
1044
                                    _("Op_timal estimation"),
 
1045
                                    filter_opt_est, &opt_est,
 
1046
                                    _("_Edge enhancement"),
 
1047
                                    filter_edge_enhance, &edge_enhance,
 
1048
 
 
1049
                                    NULL);
 
1050
 
 
1051
  gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
 
1052
  gtk_widget_show (frame);
 
1053
 
 
1054
  g_signal_connect_swapped (alpha_trim, "toggled",
 
1055
                            G_CALLBACK (gimp_preview_invalidate),
 
1056
                            preview);
 
1057
  g_signal_connect_swapped (opt_est, "toggled",
 
1058
                            G_CALLBACK (gimp_preview_invalidate),
 
1059
                            preview);
 
1060
  g_signal_connect_swapped (edge_enhance, "toggled",
 
1061
                            G_CALLBACK (gimp_preview_invalidate),
 
1062
                            preview);
 
1063
 
 
1064
  table = gtk_table_new (2, 3, FALSE);
 
1065
  gtk_table_set_col_spacings (GTK_TABLE (table), 6);
 
1066
  gtk_table_set_row_spacings (GTK_TABLE (table), 6);
 
1067
  gtk_box_pack_start (GTK_BOX (main_vbox), table, FALSE, FALSE, 0);
 
1068
  gtk_widget_show (table);
 
1069
 
 
1070
  adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
 
1071
                              _("A_lpha:"), 0, 0,
 
1072
                              nlfvals.alpha, 0.0, 1.0, 0.05, 0.1, 2,
 
1073
                              TRUE, 0, 0,
 
1074
                              NULL, NULL);
 
1075
  g_signal_connect (adj, "value_changed",
 
1076
                    G_CALLBACK (gimp_double_adjustment_update),
 
1077
                    &nlfvals.alpha);
 
1078
  g_signal_connect_swapped (adj, "value_changed",
 
1079
                            G_CALLBACK (gimp_preview_invalidate),
 
1080
                            preview);
 
1081
 
 
1082
  adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 1,
 
1083
                              _("_Radius:"), 0, 0,
 
1084
                              nlfvals.radius, 1.0 / 3.0, 1.0, 0.05, 0.1, 2,
 
1085
                              TRUE, 0, 0,
 
1086
                              NULL, NULL);
 
1087
  g_signal_connect (adj, "value_changed",
 
1088
                    G_CALLBACK (gimp_double_adjustment_update),
 
1089
                    &nlfvals.radius);
 
1090
  g_signal_connect_swapped (adj, "value_changed",
 
1091
                            G_CALLBACK (gimp_preview_invalidate),
 
1092
                            preview);
 
1093
 
 
1094
  gtk_widget_show (dialog);
 
1095
 
 
1096
  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
 
1097
 
 
1098
  gtk_widget_destroy (dialog);
 
1099
 
 
1100
  return run;
 
1101
}