nl-filter.c 40 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12
/**************************************************
 * file: nlfilt/nlfilt.c
 *
 * Copyright (c) 1997 Eric L. Hernes (erich@rrnet.com)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
luz.paz's avatar
luz.paz committed
13
 *    derived from this software without specific prior written permission
Elliot Lee's avatar
Elliot Lee committed
14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

27 28 29 30
/*
 * Algorithm fixes, V2.0 compatibility by David Hodson  hodsond@ozemail.com.au
 */

31 32
#include "config.h"

Elliot Lee's avatar
Elliot Lee committed
33
#include <string.h>
34

35
#include <libgimp/gimp.h>
36 37
#include <libgimp/gimpui.h>

38
#include "libgimp/stdplugins-intl.h"
39

40 41

#define PLUG_IN_PROC   "plug-in-nlfilt"
42
#define PLUG_IN_BINARY "nl-filter"
43
#define PLUG_IN_ROLE   "gimp-nl-filter"
44 45


Maurits Rijk's avatar
Maurits Rijk committed
46
typedef struct
47
{
48 49 50 51
  gdouble  alpha;
  gdouble  radius;
  gint     filter;
} NLFilterValues;
Elliot Lee's avatar
Elliot Lee committed
52

53 54 55 56 57
typedef enum
{
  filter_alpha_trim,
  filter_opt_est,
  filter_edge_enhance
Elliot Lee's avatar
Elliot Lee committed
58 59
} FilterType;

60 61 62 63 64

typedef struct _Nlfilter      Nlfilter;
typedef struct _NlfilterClass NlfilterClass;

struct _Nlfilter
65
{
66
  GimpPlugIn parent_instance;
67
};
68

69 70 71 72 73 74 75 76 77 78
struct _NlfilterClass
{
  GimpPlugInClass parent_class;
};


#define NLFILTER_TYPE  (nlfilter_get_type ())
#define NLFILTER (obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NLFILTER_TYPE, Nlfilter))

GType                   nlfilter_get_type         (void) G_GNUC_CONST;
Elliot Lee's avatar
Elliot Lee committed
79

80 81 82
static GList          * nlfilter_query_procedures (GimpPlugIn           *plug_in);
static GimpProcedure  * nlfilter_create_procedure (GimpPlugIn           *plug_in,
                                                   const gchar          *name);
83

84 85 86 87 88 89
static GimpValueArray * nlfilter_run              (GimpProcedure        *procedure,
                                                   GimpRunMode           run_mode,
                                                   GimpImage            *image,
                                                   GimpDrawable         *drawable,
                                                   const GimpValueArray *args,
                                                   gpointer              run_data);
90

91 92 93 94
static void             nlfilter                  (GimpDrawable     *drawable,
                                                   GimpPreview      *preview);
static void             nlfilter_preview          (GimpDrawable     *drawable,
                                                   GimpPreview      *preview);
95

96
static gboolean         nlfilter_dialog           (GimpDrawable     *drawable);
97

98 99 100
static gint             nlfiltInit                (gdouble           alpha,
                                                   gdouble           radius,
                                                   FilterType        filter);
101

102 103 104 105 106 107 108
static void             nlfiltRow                 (guchar           *srclast,
                                                   guchar           *srcthis,
                                                   guchar           *srcnext,
                                                   guchar           *dst,
                                                   gint              width,
                                                   gint              bpp,
                                                   gint              filtno);
109

110 111 112 113 114 115 116

G_DEFINE_TYPE (Nlfilter, nlfilter, GIMP_TYPE_PLUG_IN)

GIMP_MAIN (NLFILTER_TYPE)


static NLFilterValues nlfvals =
117
{
118 119 120
  0.3,
  1.0 / 3.0,
  0
Elliot Lee's avatar
Elliot Lee committed
121 122 123 124
};


static void
125
nlfilter_class_init (NlfilterClass *klass)
126
{
127 128 129 130
  GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);

  plug_in_class->query_procedures = nlfilter_query_procedures;
  plug_in_class->create_procedure = nlfilter_create_procedure;
Elliot Lee's avatar
Elliot Lee committed
131 132 133
}

static void
134
nlfilter_init (Nlfilter *nlfilter)
135
{
136
}
Elliot Lee's avatar
Elliot Lee committed
137

138 139 140 141 142
static GList *
nlfilter_query_procedures (GimpPlugIn *plug_in)
{
  return g_list_append (NULL, g_strdup (PLUG_IN_PROC));
}
143

144 145 146 147 148
static GimpProcedure *
nlfilter_create_procedure (GimpPlugIn  *plug_in,
                               const gchar *name)
{
  GimpProcedure *procedure = NULL;
Elliot Lee's avatar
Elliot Lee committed
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
  if (! strcmp (name, PLUG_IN_PROC))
    {
      procedure = gimp_image_procedure_new (plug_in, name,
                                            GIMP_PDB_PROC_TYPE_PLUGIN,
                                            nlfilter_run, NULL, NULL);

      gimp_procedure_set_image_types (procedure, "RGB, GRAY");

      gimp_procedure_set_menu_label (procedure, N_("_NL Filter..."));
      gimp_procedure_add_menu_path (procedure,"<Image>/Filters/Enhance");

      gimp_procedure_set_documentation (procedure,
                                        N_("Nonlinear swiss army knife filter"),
                                        "This is the pnmnlfilt, in gimp's "
                                        "clothing. See the pnmnlfilt manpage "
                                        "for details.",
                                        name);
      gimp_procedure_set_attribution (procedure,
                                      "Graeme W. Gill, gimp 0.99 plug-in "
                                      "by Eric L. Hernes",
                                      "Graeme W. Gill, Eric L. Hernes",
                                      "1997");

      GIMP_PROC_ARG_DOUBLE (procedure, "alpha",
                            "Alpha",
                            "The amount of the filter to apply",
                            0, 1, 0.3,
                            G_PARAM_READWRITE);

      GIMP_PROC_ARG_DOUBLE (procedure, "radius",
                            "Radius",
                            "The filter radius",
                            1.0 / 3.0, 1, 1.0 / 3.0,
                            G_PARAM_READWRITE);

      GIMP_PROC_ARG_INT (procedure, "filter",
                         "Filter",
                         "The Filter to Run, "
                         "0 - alpha trimmed mean; "
                         "1 - optimal estimation (alpha controls noise variance); "
                         "2 - edge enhancement",
                         0, 2, 0,
                         G_PARAM_READWRITE);
    }
Elliot Lee's avatar
Elliot Lee committed
194

195 196 197 198 199 200 201 202 203 204 205 206 207
  return procedure;
}

static GimpValueArray *
nlfilter_run (GimpProcedure        *procedure,
              GimpRunMode           run_mode,
              GimpImage            *image,
              GimpDrawable         *drawable,
              const GimpValueArray *args,
              gpointer              run_data)
{
  INIT_I18N ();
  gegl_init (NULL, NULL);
208

209
  switch (run_mode)
210
    {
Sven Neumann's avatar
Sven Neumann committed
211
    case GIMP_RUN_INTERACTIVE:
212
      gimp_get_data (PLUG_IN_PROC, &nlfvals);
213

214
      if (! nlfilter_dialog (drawable))
215
        {
216 217 218
          return gimp_procedure_new_return_values (procedure,
                                                   GIMP_PDB_CANCEL,
                                                   NULL);
219
        }
220
      break;
221

222 223 224 225
    case GIMP_RUN_NONINTERACTIVE:
      nlfvals.alpha  = GIMP_VALUES_GET_DOUBLE (args, 0);
      nlfvals.radius = GIMP_VALUES_GET_DOUBLE (args, 1);
      nlfvals.filter = GIMP_VALUES_GET_INT    (args, 2);
226
      break;
Elliot Lee's avatar
Elliot Lee committed
227

228
    case GIMP_RUN_WITH_LAST_VALS :
229
      gimp_get_data (PLUG_IN_PROC, &nlfvals);
230
      break;
231
    };
232

233
  nlfilter (drawable, NULL);
234

235 236
  if (run_mode != GIMP_RUN_NONINTERACTIVE)
    gimp_displays_flush ();
237

238 239
  if (run_mode == GIMP_RUN_INTERACTIVE)
    gimp_set_data (PLUG_IN_PROC, &nlfvals, sizeof (NLFilterValues));
240

241
  return gimp_procedure_new_return_values (procedure, GIMP_PDB_SUCCESS, NULL);
Elliot Lee's avatar
Elliot Lee committed
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
}

/* pnmnlfilt.c - 4 in 1 (2 non-linear) filter
**             - smooth an anyimage
**             - do alpha trimmed mean filtering on an anyimage
**             - do optimal estimation smoothing on an anyimage
**             - do edge enhancement on an anyimage
**
** Version 1.0
**
** The implementation of an alpha-trimmed mean filter
** is based on the description in IEEE CG&A May 1990
** Page 23 by Mark E. Lee and Richard A. Redner.
**
** The paper recommends using a hexagon sampling region around each
** pixel being processed, allowing an effective sub pixel radius to be
** specified. The hexagon values are sythesised by area sampling the
** rectangular pixels with a hexagon grid. The seven hexagon values
** obtained from the 3x3 pixel grid are used to compute the alpha
** trimmed mean. Note that an alpha value of 0.0 gives a conventional
** mean filter (where the radius controls the contribution of
** surrounding pixels), while a value of 0.5 gives a median filter.
** Although there are only seven values to trim from before finding
** the mean, the algorithm has been extended from that described in
** CG&A by using interpolation, to allow a continuous selection of
** alpha value between and including 0.0 to 0.5  The useful values
** for radius are between 0.3333333 (where the filter will have no
** effect because only one pixel is sampled), to 1.0, where all
** pixels in the 3x3 grid are sampled.
**
** The optimal estimation filter is taken from an article "Converting Dithered
** Images Back to Gray Scale" by Allen Stenger, Dr Dobb's Journal, November
** 1992, and this article references "Digital Image Enhancement andNoise Filtering by
** Use of Local Statistics", Jong-Sen Lee, IEEE Transactions on Pattern Analysis and
** Machine Intelligence, March 1980.
**
** Also borrow the  technique used in pgmenhance(1) to allow edge
** enhancement if the alpha value is negative.
**
** Author:
**         Graeme W. Gill, 30th Jan 1993
**         graeme@labtam.oz.au
**
285 286 287 288
** Permission is hereby granted, to use, copy, modify, distribute,
** and sell this software and its associated documentation files
** (the "Software") for any purpose without fee, provided
** that:
289
**
290 291 292 293
**     1) The above copyright notices and this permission notice
**        accompany all source code copies of the Software and
**        related documentation.
** and
294
**
295 296 297 298
**     2) If executable code based on the Software only is distributed,
**        then the accompanying documentation must acknowledge that
**        "this software is based in part on the work of Graeme W. Gill".
** and
299
**
300 301 302
**     3) It is accepted that Graeme W. Gill (the "Author") accepts
**        NO LIABILITY for damages of any kind.  The Software is
**        provided without fee by the Author "AS-IS" and without
303
**        warranty of any kind, express, implied or otherwise,
304 305 306
**        including without limitation, any warranty of merchantability
**        or fitness for a particular purpose.
** and
307
**
308 309
**     4) These conditions apply to any software derived from or based
**        on the Software, not just to the unmodified library.
310
**
Elliot Lee's avatar
Elliot Lee committed
311 312 313 314 315 316 317
*/

/* ************************************************** */
/* Hexagon intersecting square area functions */
/* Compute the area of the intersection of a triangle */
/* and a rectangle */

318
static gdouble triang_area(gdouble, gdouble, gdouble, gdouble, gdouble,
319
                           gdouble, gdouble, gdouble, gint);
320
static gdouble rectang_area(gdouble, gdouble, gdouble, gdouble,
321
                            gdouble, gdouble, gdouble, gdouble);
322
static gdouble hex_area(gdouble, gdouble, gdouble, gdouble, gdouble);
Elliot Lee's avatar
Elliot Lee committed
323

324 325 326 327 328 329 330 331 332
static gint atfilt0 (gint *p);
static gint atfilt1 (gint *p);
static gint atfilt2 (gint *p);
static gint atfilt3 (gint *p);
static gint atfilt4 (gint *p);
static gint atfilt5 (gint *p);

gint (*atfuncs[6])(gint *) =
{
Elliot Lee's avatar
Elliot Lee committed
333 334 335 336 337 338 339 340
  atfilt0,
  atfilt1,
  atfilt2,
  atfilt3,
  atfilt4,
  atfilt5
};

341
static gint noisevariance;
Elliot Lee's avatar
Elliot Lee committed
342

343 344
#define MXIVAL 255                /* maximum input value */
#define NOIVAL (MXIVAL + 1)       /* number of possible input values */
Elliot Lee's avatar
Elliot Lee committed
345

346 347
#define SCALEB 8                  /* scale bits */
#define SCALE (1 << SCALEB)       /* scale factor */
Elliot Lee's avatar
Elliot Lee committed
348

349 350 351 352
#define CSCALEB 2                 /* coarse scale bits */
#define CSCALE (1 << CSCALEB)     /* coarse scale factor */
#define MXCSVAL (MXIVAL * CSCALE) /* maximum coarse scaled values */
#define NOCSVAL (MXCSVAL + 1)     /* number of coarse scaled values */
Elliot Lee's avatar
Elliot Lee committed
353 354 355 356
#define SCTOCSC(x) ((x) >> (SCALEB - CSCALEB))  /* convert from scaled to coarse scaled */
#define CSCTOSC(x) ((x) << (SCALEB - CSCALEB))  /* convert from course scaled to scaled */

/* round and scale floating point to scaled integer */
Marc Lehmann's avatar
Marc Lehmann committed
357
#define SROUND(x) ((gint)(((x) * (gdouble)SCALE) + 0.5))
Elliot Lee's avatar
Elliot Lee committed
358 359 360 361
/* round and un-scale scaled integer value */
#define RUNSCALE(x) (((x) + (1 << (SCALEB-1))) >> SCALEB)       /* rounded un-scale */
#define UNSCALE(x) ((x) >> SCALEB)

362
/* Note: modified by David Hodson, nlfiltRow now accesses
Maurits Rijk's avatar
Maurits Rijk committed
363
 * srclast, srcthis, and srcnext from [-bpp] to [width*bpp-1].
364 365
 * Beware if you use this code anywhere else!
 */
366
static void
367 368 369
nlfiltRow (guchar *srclast, guchar *srcthis, guchar *srcnext, guchar *dst,
           gint width, gint bpp, gint filtno)
{
370 371 372 373 374 375 376 377 378 379
  gint    pf[9];
  guchar *ip0, *ip1, *ip2, *or, *orend;

  orend = dst + width * bpp;
  ip0 = srclast;
  ip1 = srcthis;
  ip2 = srcnext;

  for (or = dst; or < orend; ip0++, ip1++, ip2++, or++)
    {
Elliot Lee's avatar
Elliot Lee committed
380
      pf[0] = *ip1;
Maurits Rijk's avatar
Maurits Rijk committed
381 382
      pf[1] = *(ip1 - bpp);
      pf[2] = *(ip2 - bpp);
Elliot Lee's avatar
Elliot Lee committed
383
      pf[3] = *(ip2);
Maurits Rijk's avatar
Maurits Rijk committed
384 385 386
      pf[4] = *(ip2 + bpp);
      pf[5] = *(ip1 + bpp);
      pf[6] = *(ip0 + bpp);
Elliot Lee's avatar
Elliot Lee committed
387
      pf[7] = *(ip0);
Maurits Rijk's avatar
Maurits Rijk committed
388
      pf[8] = *(ip0 - bpp);
Elliot Lee's avatar
Elliot Lee committed
389
      *or=(atfuncs[filtno])(pf);
390
    }
Elliot Lee's avatar
Elliot Lee committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
}

/* We restrict radius to the values: 0.333333 <= radius <= 1.0 */
/* so that no fewer and no more than a 3x3 grid of pixels around */
/* the pixel in question needs to be read. Given this, we only */
/* need 3 or 4 weightings per hexagon, as follows: */
/*                  _ _                         */
/* Virtical hex:   |_|_|  1 2                   */
/*                 |X|_|  0 3                   */
/*                                       _      */
/*              _                      _|_|   1 */
/* Middle hex: |_| 1  Horizontal hex: |X|_| 0 2 */
/*             |X| 0                    |_|   3 */
/*             |_| 2                            */

/* all filters */
gint V0[NOIVAL],V1[NOIVAL],V2[NOIVAL],V3[NOIVAL];   /* vertical hex */
gint M0[NOIVAL],M1[NOIVAL],M2[NOIVAL];              /* middle hex */
gint H0[NOIVAL],H1[NOIVAL],H2[NOIVAL],H3[NOIVAL];   /* horizontal hex */

/* alpha trimmed and edge enhancement only */
gint ALFRAC[NOIVAL * 8];               /* fractional alpha divider table */

/* optimal estimation only */
gint AVEDIV[7 * NOCSVAL];              /* divide by 7 to give average value */
gint SQUARE[2 * NOCSVAL];              /* scaled square lookup table */

/* Table initialisation function - return alpha range */
419
static gint
420 421
nlfiltInit (gdouble alpha, gdouble radius, FilterType filter)
{
Elliot Lee's avatar
Elliot Lee committed
422 423 424 425 426 427
   gint alpharange;                 /* alpha range value 0 - 3 */
   gdouble meanscale;               /* scale for finding mean */
   gdouble mmeanscale;              /* scale for finding mean - midle hex */
   gdouble alphafraction;   /* fraction of next largest/smallest
                             *  to subtract from sum
                             */
428 429 430 431
   switch (filter)
     {
       case filter_alpha_trim:
         {
Elliot Lee's avatar
Elliot Lee committed
432
          gdouble noinmean;
433 434
          /* alpha only makes sense in range 0.0 - 0.5 */
          alpha /= 2.0;
Elliot Lee's avatar
Elliot Lee committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
              /* number of elements (out of a possible 7) used in the mean */
          noinmean = ((0.5 - alpha) * 12.0) + 1.0;
          mmeanscale = meanscale = 1.0/noinmean;
          if (alpha == 0.0) {                    /* mean filter */
             alpharange = 0;
             alphafraction = 0.0;            /* not used */
          } else if (alpha < (1.0/6.0)) {    /* mean of 5 to 7 middle values */
             alpharange = 1;
             alphafraction = (7.0 - noinmean)/2.0;
          } else if (alpha < (1.0/3.0)) {    /* mean of 3 to 5 middle values */
             alpharange = 2;
             alphafraction = (5.0 - noinmean)/2.0;
          } else {                           /* mean of 1 to 3 middle values */
                                             /* alpha==0.5  => median filter */
             alpharange = 3;
             alphafraction = (3.0 - noinmean)/2.0;
          }
       }
       break;
       case filter_opt_est: {
          gint i;
          gdouble noinmean = 7.0;

              /* edge enhancement function */
          alpharange = 5;

              /* compute scaled hex values */
          mmeanscale=meanscale=1.0;

              /* Set up 1:1 division lookup - not used */
          alphafraction=1.0/noinmean;

              /* estimate of noise variance */
          noisevariance = alpha * (gdouble)255;
          noisevariance = noisevariance * noisevariance / 8.0;

              /* set yp optimal estimation specific stuff */

          for (i=0;i<(7*NOCSVAL);i++) { /* divide scaled value by 7 lookup */
             AVEDIV[i] = CSCTOSC(i)/7;       /* scaled divide by 7 */
          }
              /* compute square and rescale by
               * (val >> (2 * SCALEB + 2)) table
               */
          for (i=0;i<(2*NOCSVAL);i++) {
             gint val;
                 /* NOCSVAL offset to cope with -ve input values */
             val = CSCTOSC(i - NOCSVAL);
             SQUARE[i] = (val * val) >> (2 * SCALEB + 2);
          }
       }
       break;
       case filter_edge_enhance: {
          if (alpha == 1.0) alpha = 0.99;
          alpharange = 4;
              /* mean of 7 and scaled by -alpha/(1-alpha) */
          meanscale = 1.0 * (-alpha/((1.0 - alpha) * 7.0));

              /* middle pixel has 1/(1-alpha) as well */
          mmeanscale = 1.0 * (1.0/(1.0 - alpha) + meanscale);
          alphafraction = 0.0;    /* not used */
       }
       break;
       default:
499 500
         g_printerr ("unknown filter %d\n", filter);
         return -1;
Elliot Lee's avatar
Elliot Lee committed
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
   }
       /*
        * Setup pixel weighting tables -
        * note we pre-compute mean division here too.
        */
   {
      gint i;
      gdouble hexhoff,hexvoff;
      gdouble tabscale,mtabscale;
      gdouble v0,v1,v2,v3,m0,m1,m2,h0,h1,h2,h3;

          /* horizontal offset of virtical hex centers */
      hexhoff = radius/2;

          /* vertical offset of virtical hex centers */
      hexvoff = 3.0 * radius/sqrt(12.0);

          /*
           * scale tables to normalise by hexagon
           * area, and number of hexes used in mean
           */
      tabscale = meanscale / (radius * hexvoff);
      mtabscale = mmeanscale / (radius * hexvoff);
      v0 = hex_area(0.0,0.0,hexhoff,hexvoff,radius) * tabscale;
      v1 = hex_area(0.0,1.0,hexhoff,hexvoff,radius) * tabscale;
      v2 = hex_area(1.0,1.0,hexhoff,hexvoff,radius) * tabscale;
      v3 = hex_area(1.0,0.0,hexhoff,hexvoff,radius) * tabscale;
      m0 = hex_area(0.0,0.0,0.0,0.0,radius) * mtabscale;
      m1 = hex_area(0.0,1.0,0.0,0.0,radius) * mtabscale;
      m2 = hex_area(0.0,-1.0,0.0,0.0,radius) * mtabscale;
      h0 = hex_area(0.0,0.0,radius,0.0,radius) * tabscale;
      h1 = hex_area(1.0,1.0,radius,0.0,radius) * tabscale;
      h2 = hex_area(1.0,0.0,radius,0.0,radius) * tabscale;
      h3 = hex_area(1.0,-1.0,radius,0.0,radius) * tabscale;

      for (i=0; i <= MXIVAL; i++) {
         gdouble fi;
         fi = (gdouble)i;
Marc Lehmann's avatar
Marc Lehmann committed
539 540 541 542 543 544 545 546 547 548 549
         V0[i] = SROUND(fi * v0);
         V1[i] = SROUND(fi * v1);
         V2[i] = SROUND(fi * v2);
         V3[i] = SROUND(fi * v3);
         M0[i] = SROUND(fi * m0);
         M1[i] = SROUND(fi * m1);
         M2[i] = SROUND(fi * m2);
         H0[i] = SROUND(fi * h0);
         H1[i] = SROUND(fi * h1);
         H2[i] = SROUND(fi * h2);
         H3[i] = SROUND(fi * h3);
Elliot Lee's avatar
Elliot Lee committed
550 551 552
      }
          /* set up alpha fraction lookup table used on big/small */
      for (i=0; i < (NOIVAL * 8); i++) {
Marc Lehmann's avatar
Marc Lehmann committed
553
         ALFRAC[i] = SROUND((gdouble)i * alphafraction);
Elliot Lee's avatar
Elliot Lee committed
554 555 556 557 558 559 560
      }
   }
   return alpharange;
}

/* Core pixel processing function - hand it 3x3 pixels and return result. */
/* Mean filter */
561 562 563
static gint
atfilt0(gint32 *p)
{
Elliot Lee's avatar
Elliot Lee committed
564 565 566 567 568 569 570 571 572 573 574 575 576
   gint retv;
       /* map to scaled hexagon values */
   retv = M0[p[0]] + M1[p[3]] + M2[p[7]];
   retv += H0[p[0]] + H1[p[2]] + H2[p[1]] + H3[p[8]];
   retv += V0[p[0]] + V1[p[3]] + V2[p[2]] + V3[p[1]];
   retv += V0[p[0]] + V1[p[3]] + V2[p[4]] + V3[p[5]];
   retv += H0[p[0]] + H1[p[4]] + H2[p[5]] + H3[p[6]];
   retv += V0[p[0]] + V1[p[7]] + V2[p[6]] + V3[p[5]];
   retv += V0[p[0]] + V1[p[7]] + V2[p[8]] + V3[p[1]];
   return UNSCALE(retv);
}

/* Mean of 5 - 7 middle values */
577 578 579
static gint
atfilt1 (gint32 *p)
{
Elliot Lee's avatar
Elliot Lee committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
   gint h0,h1,h2,h3,h4,h5,h6;       /* hexagon values    2 3   */
                                    /*                  1 0 4  */
                                    /*                   6 5   */
   gint big,small;
       /* map to scaled hexagon values */
   h0 = M0[p[0]] + M1[p[3]] + M2[p[7]];
   h1 = H0[p[0]] + H1[p[2]] + H2[p[1]] + H3[p[8]];
   h2 = V0[p[0]] + V1[p[3]] + V2[p[2]] + V3[p[1]];
   h3 = V0[p[0]] + V1[p[3]] + V2[p[4]] + V3[p[5]];
   h4 = H0[p[0]] + H1[p[4]] + H2[p[5]] + H3[p[6]];
   h5 = V0[p[0]] + V1[p[7]] + V2[p[6]] + V3[p[5]];
   h6 = V0[p[0]] + V1[p[7]] + V2[p[8]] + V3[p[1]];
       /* sum values and also discover the largest and smallest */
   big = small = h0;
#define CHECK(xx) \
        h0 += xx; \
        if (xx > big) \
                big = xx; \
        else if (xx < small) \
                small = xx;
        CHECK(h1)
        CHECK(h2)
        CHECK(h3)
        CHECK(h4)
        CHECK(h5)
        CHECK(h6)
#undef CHECK
       /* Compute mean of middle 5-7 values */
   return UNSCALE(h0 -ALFRAC[(big + small)>>SCALEB]);
}

/* Mean of 3 - 5 middle values */
612 613 614
static gint
atfilt2 (gint32 *p)
{
Elliot Lee's avatar
Elliot Lee committed
615 616 617 618 619 620 621 622 623 624 625 626 627 628
   gint h0,h1,h2,h3,h4,h5,h6;       /* hexagon values    2 3   */
                                    /*                  1 0 4  */
                                    /*                   6 5   */
   gint big0,big1,small0,small1;
       /* map to scaled hexagon values */
   h0 = M0[p[0]] + M1[p[3]] + M2[p[7]];
   h1 = H0[p[0]] + H1[p[2]] + H2[p[1]] + H3[p[8]];
   h2 = V0[p[0]] + V1[p[3]] + V2[p[2]] + V3[p[1]];
   h3 = V0[p[0]] + V1[p[3]] + V2[p[4]] + V3[p[5]];
   h4 = H0[p[0]] + H1[p[4]] + H2[p[5]] + H3[p[6]];
   h5 = V0[p[0]] + V1[p[7]] + V2[p[6]] + V3[p[5]];
   h6 = V0[p[0]] + V1[p[7]] + V2[p[8]] + V3[p[1]];
       /* sum values and also discover the 2 largest and 2 smallest */
   big0 = small0 = h0;
629
   small1 = G_MAXINT;
Elliot Lee's avatar
Elliot Lee committed
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
   big1 = 0;
#define CHECK(xx) \
        h0 += xx; \
        if (xx > big1) \
                { \
                if (xx > big0) \
                        { \
                        big1 = big0; \
                        big0 = xx; \
                        } \
                else \
                        big1 = xx; \
                } \
        if (xx < small1) \
                { \
                if (xx < small0) \
                        { \
                        small1 = small0; \
                        small0 = xx; \
                        } \
                else \
                        small1 = xx; \
                }
        CHECK(h1)
        CHECK(h2)
        CHECK(h3)
        CHECK(h4)
        CHECK(h5)
        CHECK(h6)
#undef CHECK
       /* Compute mean of middle 3-5 values */
  return UNSCALE(h0 -big0 -small0 -ALFRAC[(big1 + small1)>>SCALEB]);
}

/*
 * Mean of 1 - 3 middle values.
 * If only 1 value, then this is a median filter.
 */
668 669 670
static gint32
atfilt3(gint32 *p)
{
Elliot Lee's avatar
Elliot Lee committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684
   gint h0,h1,h2,h3,h4,h5,h6;       /* hexagon values    2 3   */
                                   /*                  1 0 4  */
                                   /*                   6 5   */
   gint big0,big1,big2,small0,small1,small2;
       /* map to scaled hexagon values */
   h0 = M0[p[0]] + M1[p[3]] + M2[p[7]];
   h1 = H0[p[0]] + H1[p[2]] + H2[p[1]] + H3[p[8]];
   h2 = V0[p[0]] + V1[p[3]] + V2[p[2]] + V3[p[1]];
   h3 = V0[p[0]] + V1[p[3]] + V2[p[4]] + V3[p[5]];
   h4 = H0[p[0]] + H1[p[4]] + H2[p[5]] + H3[p[6]];
   h5 = V0[p[0]] + V1[p[7]] + V2[p[6]] + V3[p[5]];
   h6 = V0[p[0]] + V1[p[7]] + V2[p[8]] + V3[p[1]];
       /* sum values and also discover the 3 largest and 3 smallest */
   big0 = small0 = h0;
685
   small1 = small2 = G_MAXINT;
Elliot Lee's avatar
Elliot Lee committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
   big1 = big2 = 0;
#define CHECK(xx) \
        h0 += xx; \
        if (xx > big2) \
                { \
                if (xx > big1) \
                        { \
                        if (xx > big0) \
                                { \
                                big2 = big1; \
                                big1 = big0; \
                                big0 = xx; \
                                } \
                        else \
                                { \
                                big2 = big1; \
                                big1 = xx; \
                                } \
                        } \
                else \
                        big2 = xx; \
                } \
        if (xx < small2) \
                { \
                if (xx < small1) \
                        { \
                        if (xx < small0) \
                                { \
                                small2 = small1; \
                                small1 = small0; \
                                small0 = xx; \
                                } \
                        else \
                                { \
                                small2 = small1; \
                                small1 = xx; \
                                } \
                        } \
                else \
                        small2 = xx; \
                }
        CHECK(h1)
        CHECK(h2)
        CHECK(h3)
        CHECK(h4)
        CHECK(h5)
        CHECK(h6)
#undef CHECK
       /* Compute mean of middle 1-3 values */
   return  UNSCALE(h0-big0-big1-small0-small1-ALFRAC[(big2+small2)>>SCALEB]);
}

/* Edge enhancement */
739 740 741
static gint
atfilt4 (gint *p)
{
Elliot Lee's avatar
Elliot Lee committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
   gint hav;
       /* map to scaled hexagon values and compute enhance value */
   hav = M0[p[0]] + M1[p[3]] + M2[p[7]];
   hav += H0[p[0]] + H1[p[2]] + H2[p[1]] + H3[p[8]];
   hav += V0[p[0]] + V1[p[3]] + V2[p[2]] + V3[p[1]];
   hav += V0[p[0]] + V1[p[3]] + V2[p[4]] + V3[p[5]];
   hav += H0[p[0]] + H1[p[4]] + H2[p[5]] + H3[p[6]];
   hav += V0[p[0]] + V1[p[7]] + V2[p[6]] + V3[p[5]];
   hav += V0[p[0]] + V1[p[7]] + V2[p[8]] + V3[p[1]];
   if (hav < 0)
      hav = 0;
   hav = UNSCALE(hav);
   if (hav > (gdouble)255)
      hav = (gdouble)255;
   return hav;
}

/* Optimal estimation - do smoothing in inverse proportion */
/* to the local variance. */
/* notice we use the globals noisevariance */
gint
atfilt5(gint *p) {
   gint mean,variance,temp;
   gint h0,h1,h2,h3,h4,h5,h6;       /* hexagon values    2 3   */
                                   /*                  1 0 4  */
                                   /*                   6 5   */
       /* map to scaled hexagon values */
   h0 = M0[p[0]] + M1[p[3]] + M2[p[7]];
   h1 = H0[p[0]] + H1[p[2]] + H2[p[1]] + H3[p[8]];
   h2 = V0[p[0]] + V1[p[3]] + V2[p[2]] + V3[p[1]];
   h3 = V0[p[0]] + V1[p[3]] + V2[p[4]] + V3[p[5]];
   h4 = H0[p[0]] + H1[p[4]] + H2[p[5]] + H3[p[6]];
   h5 = V0[p[0]] + V1[p[7]] + V2[p[6]] + V3[p[5]];
   h6 = V0[p[0]] + V1[p[7]] + V2[p[8]] + V3[p[1]];
   mean = h0 + h1 + h2 + h3 + h4 + h5 + h6;
       /* compute scaled mean by dividing by 7 */
   mean = AVEDIV[SCTOCSC(mean)];

       /* compute scaled variance */
   temp = (h1 - mean); variance = SQUARE[NOCSVAL + SCTOCSC(temp)];

       /* and rescale to keep */
   temp = (h2 - mean); variance += SQUARE[NOCSVAL + SCTOCSC(temp)];

 /* within 32 bit limits */
   temp = (h3 - mean); variance += SQUARE[NOCSVAL + SCTOCSC(temp)];
   temp = (h4 - mean); variance += SQUARE[NOCSVAL + SCTOCSC(temp)];
   temp = (h5 - mean); variance += SQUARE[NOCSVAL + SCTOCSC(temp)];
   temp = (h6 - mean); variance += SQUARE[NOCSVAL + SCTOCSC(temp)];
       /* (temp = h0 - mean) */
   temp = (h0 - mean); variance += SQUARE[NOCSVAL + SCTOCSC(temp)];
   if (variance != 0)      /* avoid possible divide by 0 */
          /* optimal estimate */
      temp = mean + (variance * temp) / (variance + noisevariance);
   else temp = h0;
   if (temp < 0)
      temp = 0;
   temp = RUNSCALE(temp);
   if (temp > (gdouble)255) temp = (gdouble)255;
   return temp;
}


/* Triangle orientation is per geometric axes (not graphical axies) */

#define NW 0    /* North west triangle /| */
#define NE 1    /* North east triangle |\ */
#define SW 2    /* South west triangle \| */
#define SE 3    /* South east triangle |/ */
#define STH 2
#define EST 1

#define SWAPI(a,b) (t = a, a = -b, b = -t)

/* compute the area of overlap of a hexagon diameter d, */
/* centered at hx,hy, with a unit square of center sx,sy. */
818
static gdouble
819 820
hex_area (gdouble sx, gdouble sy, gdouble hx, gdouble hy, gdouble d)
{
Elliot Lee's avatar
Elliot Lee committed
821 822 823
   gdouble hx0,hx1,hx2,hy0,hy1,hy2,hy3;
   gdouble sx0,sx1,sy0,sy1;

luzpaz's avatar
luzpaz committed
824
       /* compute square coordinates */
Elliot Lee's avatar
Elliot Lee committed
825 826 827 828
   sx0 = sx - 0.5;
   sy0 = sy - 0.5;
   sx1 = sx + 0.5;
   sy1 = sy + 0.5;
luzpaz's avatar
luzpaz committed
829
       /* compute hexagon coordinates */
Elliot Lee's avatar
Elliot Lee committed
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
   hx0 = hx - d/2.0;
   hx1 = hx;
   hx2 = hx + d/2.0;
   hy0 = hy - 0.5773502692 * d;    /* d / sqrt(3) */
   hy1 = hy - 0.2886751346 * d;    /* d / sqrt(12) */
   hy2 = hy + 0.2886751346 * d;    /* d / sqrt(12) */
   hy3 = hy + 0.5773502692 * d;    /* d / sqrt(3) */

   return
      triang_area(sx0,sy0,sx1,sy1,hx0,hy2,hx1,hy3,NW) +
      triang_area(sx0,sy0,sx1,sy1,hx1,hy2,hx2,hy3,NE) +
      rectang_area(sx0,sy0,sx1,sy1,hx0,hy1,hx2,hy2) +
      triang_area(sx0,sy0,sx1,sy1,hx0,hy0,hx1,hy1,SW) +
      triang_area(sx0,sy0,sx1,sy1,hx1,hy0,hx2,hy1,SE);
}

846
static gdouble
847 848 849
triang_area (gdouble rx0, gdouble ry0, gdouble rx1, gdouble ry1, gdouble tx0,
             gdouble ty0, gdouble tx1, gdouble ty1, gint tt)
{
Elliot Lee's avatar
Elliot Lee committed
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
   gdouble a,b,c,d;
   gdouble lx0,ly0,lx1,ly1;
       /* Convert everything to a NW triangle */
   if (tt & STH) {
      gdouble t;
      SWAPI(ry0,ry1);
      SWAPI(ty0,ty1);
   } if (tt & EST) {
      gdouble t;
      SWAPI(rx0,rx1);
      SWAPI(tx0,tx1);
   }
       /* Compute overlapping box */
   if (tx0 > rx0)
      rx0 = tx0;
   if (ty0 > ry0)
      ry0 = ty0;
   if (tx1 < rx1)
      rx1 = tx1;
   if (ty1 < ry1)
      ry1 = ty1;
   if (rx1 <= rx0 || ry1 <= ry0)
      return 0.0;
       /* Need to compute diagonal line intersection with the box */
       /* First compute co-efficients to formulas x = a + by and y = c + dx */
   b = (tx1 - tx0)/(ty1 - ty0);
   a = tx0 - b * ty0;
   d = (ty1 - ty0)/(tx1 - tx0);
   c = ty0 - d * tx0;

       /* compute top or right intersection */
   tt = 0;
   ly1 = ry1;
   lx1 = a + b * ly1;
   if (lx1 <= rx0)
      return (rx1 - rx0) * (ry1 - ry0);
   else if (lx1 > rx1) {     /* could be right hand side */
      lx1 = rx1;
      ly1 = c + d * lx1;
      if (ly1 <= ry0)
         return (rx1 - rx0) * (ry1 - ry0);
      tt = 1; /* right hand side intersection */
   }
       /* compute left or bottom intersection */
   lx0 = rx0;
   ly0 = c + d * lx0;
   if (ly0 >= ry1)
      return (rx1 - rx0) * (ry1 - ry0);
   else if (ly0 < ry0) {    /* could be right hand side */
      ly0 = ry0;
      lx0 = a + b * ly0;
      if (lx0 >= rx1)
         return (rx1 - rx0) * (ry1 - ry0);
      tt |= 2;        /* bottom intersection */
   }

   if (tt == 0) {    /* top and left intersection */
                       /* rectangle minus triangle */
      return ((rx1 - rx0) * (ry1 - ry0))
         - (0.5 * (lx1 - rx0) * (ry1 - ly0));
   }
   else if (tt == 1) {       /* right and left intersection */
      return ((rx1 - rx0) * (ly0 - ry0))
         + (0.5 * (rx1 - rx0) * (ly1 - ly0));
   } else if (tt == 2) {      /* top and bottom intersection */
      return ((rx1 - lx1) * (ry1 - ry0))
         + (0.5 * (lx1 - lx0) * (ry1 - ry0));
   } else { /* tt == 3 */      /* right and bottom intersection */
          /* triangle */
      return (0.5 * (rx1 - lx0) * (ly1 - ry0));
   }
}

/* Compute rectangle area */
924
static gdouble
925 926 927 928
rectang_area (gdouble rx0, gdouble ry0, gdouble rx1, gdouble ry1, gdouble tx0,
              gdouble ty0, gdouble tx1, gdouble ty1)
{
  /* Compute overlapping box */
Elliot Lee's avatar
Elliot Lee committed
929 930 931 932 933 934 935 936 937 938 939 940
   if (tx0 > rx0)
      rx0 = tx0;
   if (ty0 > ry0)
      ry0 = ty0;
   if (tx1 < rx1)
      rx1 = tx1;
   if (ty1 < ry1)
      ry1 = ty1;
   if (rx1 <= rx0 || ry1 <= ry0)
      return 0.0;
   return (rx1 - rx0) * (ry1 - ry0);
}
941 942

static void
943
nlfilter (GimpDrawable *drawable,
944 945
          GimpPreview  *preview)
{
946 947 948 949 950 951 952 953
  GeglBuffer *src_buffer;
  GeglBuffer *dest_buffer;
  const Babl *format;
  guchar     *srcbuf, *dstbuf;
  guchar     *lastrow, *thisrow, *nextrow, *temprow;
  gint        x1, y1, y2;
  gint        width, height, bpp;
  gint        filtno, y, rowsize, exrowsize, p_update;
954 955 956 957 958 959 960 961 962

  if (preview)
    {
      gimp_preview_get_position (preview, &x1, &y1);
      gimp_preview_get_size (preview, &width, &height);
      y2 = y1 + height;
    }
  else
    {
963
      if (! gimp_drawable_mask_intersect (drawable,
964
                                          &x1, &y1, &width, &height))
965
        return;
966 967

      y2 = y1 + height;
968
    }
969

970
  if (gimp_drawable_has_alpha (drawable))
971 972 973 974
    format = babl_format ("R'G'B'A u8");
  else
    format = babl_format ("R'G'B' u8");

975
  src_buffer = gimp_drawable_get_buffer (drawable);
976 977 978 979

  if (preview)
    dest_buffer = gegl_buffer_new (gegl_buffer_get_extent (src_buffer), format);
  else
980
    dest_buffer = gimp_drawable_get_shadow_buffer (drawable);
981 982

  bpp = babl_format_get_bytes_per_pixel (format);
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

  rowsize = width * bpp;
  exrowsize = (width + 2) * bpp;
  p_update = width / 20 + 1;

  /* source buffer gives one pixel margin all around destination buffer */
  srcbuf = g_new0 (guchar, exrowsize * 3);
  dstbuf = g_new0 (guchar, rowsize);

  /* pointers to second pixel in each source row */
  lastrow = srcbuf + bpp;
  thisrow = lastrow + exrowsize;
  nextrow = thisrow + exrowsize;

  filtno = nlfiltInit (nlfvals.alpha, nlfvals.radius, nlfvals.filter);

  if (!preview)
1000
    gimp_progress_init (_("NL Filter"));
1001 1002

  /* first row */
1003 1004 1005 1006
  gegl_buffer_get (src_buffer, GEGL_RECTANGLE (x1, y1, width, 1), 1.0,
                   format, thisrow,
                   GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
  /* copy thisrow[0] to thisrow[-1], thisrow[width-1] to thisrow[width] */
  memcpy (thisrow - bpp, thisrow, bpp);
  memcpy (thisrow + rowsize, thisrow + rowsize - bpp, bpp);
  /* copy whole thisrow to lastrow */
  memcpy (lastrow - bpp, thisrow - bpp, exrowsize);

  for (y = y1; y < y2 - 1; y++)
    {
      if (((y % p_update) == 0) && !preview)
        gimp_progress_update ((gdouble) y / (gdouble) height);

1018 1019 1020 1021
      gegl_buffer_get (src_buffer, GEGL_RECTANGLE (x1, y + 1, width, 1), 1.0,
                       format, nextrow,
                       GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);

1022 1023 1024
      memcpy (nextrow - bpp, nextrow, bpp);
      memcpy (nextrow + rowsize, nextrow + rowsize - bpp, bpp);
      nlfiltRow (lastrow, thisrow, nextrow, dstbuf, width, bpp, filtno);
1025 1026 1027 1028 1029

      gegl_buffer_set (dest_buffer, GEGL_RECTANGLE (x1, y, width, 1), 0,
                       format, dstbuf,
                       GEGL_AUTO_ROWSTRIDE);

1030 1031 1032 1033 1034 1035 1036 1037
      /* rotate row buffers */
      temprow = lastrow; lastrow = thisrow;
      thisrow = nextrow; nextrow = temprow;
    }

  /* last row */
  memcpy (nextrow - bpp, thisrow - bpp, exrowsize);
  nlfiltRow (lastrow, thisrow, nextrow, dstbuf, width, bpp, filtno);
1038 1039 1040 1041

  gegl_buffer_set (dest_buffer, GEGL_RECTANGLE (x1, y2 - 1, width, 1), 0,
                   format, dstbuf,
                   GEGL_AUTO_ROWSTRIDE);
1042 1043 1044 1045 1046 1047

  g_free (srcbuf);
  g_free (dstbuf);

  if (preview)
    {
1048 1049 1050 1051 1052 1053 1054 1055 1056
      guchar *buf = g_new (guchar, width * height * bpp);

      gegl_buffer_get (dest_buffer, GEGL_RECTANGLE (x1, y1, width, height), 1.0,
                       format, buf,
                       GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);

      gimp_preview_draw_buffer (GIMP_PREVIEW (preview), buf, width * bpp);

      g_free (buf);
1057
    }
1058 1059 1060 1061 1062

  g_object_unref (src_buffer);
  g_object_unref (dest_buffer);

  if (! preview)
1063
    {
1064
      gimp_progress_update (1.0);
1065

1066 1067
      gimp_drawable_merge_shadow (drawable, TRUE);
      gimp_drawable_update (drawable, x1, y1, width, height);
1068 1069 1070 1071
      gimp_displays_flush ();
    }
}

1072
static void
1073 1074
nlfilter_preview (GimpDrawable *drawable,
                  GimpPreview  *preview)
1075
{
1076
  nlfilter (drawable, preview);
1077 1078
}

1079
static gboolean
1080
nlfilter_dialog (GimpDrawable *drawable)
1081
{
1082 1083 1084 1085 1086 1087 1088
  GtkWidget     *dialog;
  GtkWidget     *main_vbox;
  GtkWidget     *preview;
  GtkWidget     *frame;
  GtkWidget     *alpha_trim;
  GtkWidget     *opt_est;
  GtkWidget     *edge_enhance;
1089
  GtkWidget     *grid;
1090 1091
  GtkAdjustment *adj;
  gboolean       run;
1092

1093
  gimp_ui_init (PLUG_IN_BINARY);
1094

1095
  dialog = gimp_dialog_new (_("NL Filter"), PLUG_IN_ROLE,
1096 1097
                            NULL, 0,
                            gimp_standard_help_func, PLUG_IN_PROC,
1098

1099 1100
                            _("_Cancel"), GTK_RESPONSE_CANCEL,
                            _("_OK"),     GTK_RESPONSE_OK,
1101

1102
                            NULL);
1103

1104
  gimp_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
1105 1106 1107
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);
1108

1109
  gimp_window_set_transient (GTK_WINDOW (dialog));
1110

1111
  main_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
1112
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
1113 1114
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
                      main_vbox, TRUE, TRUE, 0);
1115 1116
  gtk_widget_show (main_vbox);

1117
  preview = gimp_drawable_preview_new_from_drawable (drawable);
1118
  gtk_box_pack_start (GTK_BOX (main_vbox), preview, TRUE, TRUE, 0);
1119
  gtk_widget_show (preview);
1120

1121
  g_signal_connect_swapped (preview, "invalidated",
1122
                            G_CALLBACK (nlfilter_preview),
1123
                            drawable);
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150

  frame = gimp_int_radio_group_new (TRUE, _("Filter"),
                                    G_CALLBACK (gimp_radio_button_update),
                                    &nlfvals.filter, nlfvals.filter,

                                    _("_Alpha trimmed mean"),
                                    filter_alpha_trim, &alpha_trim,
                                    _("Op_timal estimation"),
                                    filter_opt_est, &opt_est,
                                    _("_Edge enhancement"),
                                    filter_edge_enhance, &edge_enhance,

                                    NULL);

  gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  g_signal_connect_swapped (alpha_trim, "toggled",
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);
  g_signal_connect_swapped (opt_est, "toggled",
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);
  g_signal_connect_swapped (edge_enhance, "toggled",
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);

1151 1152 1153 1154 1155 1156
  grid = gtk_grid_new ();
  gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
  gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
  gtk_box_pack_start (GTK_BOX (main_vbox), grid, FALSE, FALSE, 0);
  gtk_widget_show (grid);

1157 1158 1159 1160 1161
  adj = gimp_scale_entry_new (GTK_GRID (grid), 0, 0,
                              _("A_lpha:"), 0, 0,
                              nlfvals.alpha, 0.0, 1.0, 0.05, 0.1, 2,
                              TRUE, 0, 0,
                              NULL, NULL);
1162
  g_signal_connect (adj, "value-changed",
1163 1164
                    G_CALLBACK (gimp_double_adjustment_update),
                    &nlfvals.alpha);
1165
  g_signal_connect_swapped (adj, "value-changed",
1166 1167 1168
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);

1169 1170 1171 1172 1173
  adj = gimp_scale_entry_new (GTK_GRID (grid), 0, 1,
                              _("_Radius:"), 0, 0,
                              nlfvals.radius, 1.0 / 3.0, 1.0, 0.05, 0.1, 2,
                              TRUE, 0, 0,
                              NULL, NULL);
1174
  g_signal_connect (adj, "value-changed",
1175 1176
                    G_CALLBACK (gimp_double_adjustment_update),
                    &nlfvals.radius);
1177
  g_signal_connect_swapped (adj, "value-changed",
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);

  gtk_widget_show (dialog);

  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);

  gtk_widget_destroy (dialog);

  return run;
}