This file is indexed.

/usr/include/vigra/nonlineardiffusion.hxx is in libvigraimpex-dev 1.10.0+dfsg-3ubuntu2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
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
499
500
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
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
739
740
741
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
/************************************************************************/
/*                                                                      */
/*               Copyright 1998-2002 by Ullrich Koethe                  */
/*                                                                      */
/*    This file is part of the VIGRA computer vision library.           */
/*    The VIGRA Website is                                              */
/*        http://hci.iwr.uni-heidelberg.de/vigra/                       */
/*    Please direct questions, bug reports, and contributions to        */
/*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
/*        vigra@informatik.uni-hamburg.de                               */
/*                                                                      */
/*    Permission is hereby granted, free of charge, to any person       */
/*    obtaining a copy of this software and associated documentation    */
/*    files (the "Software"), to deal in the Software without           */
/*    restriction, including without limitation the rights to use,      */
/*    copy, modify, merge, publish, distribute, sublicense, and/or      */
/*    sell copies of the Software, and to permit persons to whom the    */
/*    Software is furnished to do so, subject to the following          */
/*    conditions:                                                       */
/*                                                                      */
/*    The above copyright notice and this permission notice shall be    */
/*    included in all copies or substantial portions of the             */
/*    Software.                                                         */
/*                                                                      */
/*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
/*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
/*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
/*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
/*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
/*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
/*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
/*    OTHER DEALINGS IN THE SOFTWARE.                                   */                
/*                                                                      */
/************************************************************************/

#ifndef VIGRA_NONLINEARDIFFUSION_HXX
#define VIGRA_NONLINEARDIFFUSION_HXX

#include <vector>
#include "stdimage.hxx"
#include "stdimagefunctions.hxx"
#include "imageiteratoradapter.hxx"
#include "functortraits.hxx"
#include "multi_shape.hxx"

namespace vigra {

template <class SrcIterator, class SrcAccessor,
          class CoeffIterator, class DestIterator>
void internalNonlinearDiffusionDiagonalSolver(
    SrcIterator sbegin, SrcIterator send, SrcAccessor sa,
    CoeffIterator diag, CoeffIterator upper, CoeffIterator lower,
    DestIterator dbegin)
{
    int w = send - sbegin - 1;
    
    int i;
    
    for(i=0; i<w; ++i)
    {
        lower[i] = lower[i] / diag[i];
        
        diag[i+1] = diag[i+1] - lower[i] * upper[i];
    }
    
    dbegin[0] = sa(sbegin);
    
    for(i=1; i<=w; ++i)
    {
        dbegin[i] = sa(sbegin, i) - lower[i-1] * dbegin[i-1];
    }
    
    dbegin[w] = dbegin[w] / diag[w];
    
    for(i=w-1; i>=0; --i)
    {
        dbegin[i] = (dbegin[i] - upper[i] * dbegin[i+1]) / diag[i];
    }
}


template <class SrcIterator, class SrcAccessor,
          class WeightIterator, class WeightAccessor,
          class DestIterator, class DestAccessor>
void internalNonlinearDiffusionAOSStep(
                   SrcIterator sul, SrcIterator slr, SrcAccessor as,
                   WeightIterator wul, WeightAccessor aw,
                   DestIterator dul, DestAccessor ad, double timestep)
{
    // use traits to determine SumType as to prevent possible overflow
    typedef typename
        NumericTraits<typename WeightAccessor::value_type>::RealPromote
        WeightType;
        
    // calculate width and height of the image
    int w = slr.x - sul.x;
    int h = slr.y - sul.y;
    int d = (w < h) ? h : w;

    std::vector<WeightType> lower(d),
                            diag(d),
                            upper(d),
                            res(d);

    int x,y;
    
    WeightType one = NumericTraits<WeightType>::one();
    
     // create y iterators
    SrcIterator ys = sul;
    WeightIterator yw = wul;
    DestIterator yd = dul;
    
    // x-direction
    for(y=0; y<h; ++y, ++ys.y, ++yd.y, ++yw.y)
    {
        typename SrcIterator::row_iterator xs = ys.rowIterator();
        typename WeightIterator::row_iterator xw = yw.rowIterator();
        typename DestIterator::row_iterator xd = yd.rowIterator();

        // fill 3-diag matrix
        
        diag[0] = one + timestep * (aw(xw) + aw(xw, 1));
        for(x=1; x<w-1; ++x)
        {
            diag[x] = one + timestep * (2.0 * aw(xw, x) + aw(xw, x+1) + aw(xw, x-1));
        }
        diag[w-1] = one + timestep * (aw(xw, w-1) + aw(xw, w-2));

        for(x=0; x<w-1; ++x)
        {
            lower[x] = -timestep * (aw(xw, x) + aw(xw, x+1));
            upper[x] = lower[x];
        }
        
        internalNonlinearDiffusionDiagonalSolver(xs, xs+w, as,
                            diag.begin(), upper.begin(), lower.begin(), res.begin());
                            
        for(x=0; x<w; ++x, ++xd)
        {
            ad.set(res[x], xd);
        }
    }
        
    // y-direction
    ys = sul;
    yw = wul;
    yd = dul;
    
    for(x=0; x<w; ++x, ++ys.x, ++yd.x, ++yw.x)
    {
        typename SrcIterator::column_iterator xs = ys.columnIterator();
        typename WeightIterator::column_iterator xw = yw.columnIterator();
        typename DestIterator::column_iterator xd = yd.columnIterator();

        // fill 3-diag matrix
        
        diag[0] = one + timestep * (aw(xw) + aw(xw, 1));
        for(y=1; y<h-1; ++y)
        {
            diag[y] = one + timestep * (2.0 * aw(xw, y) + aw(xw, y+1) + aw(xw, y-1));
        }
        diag[h-1] = one + timestep * (aw(xw, h-1) + aw(xw, h-2));

        for(y=0; y<h-1; ++y)
        {
            lower[y] = -timestep * (aw(xw, y) + aw(xw, y+1));
            upper[y] = lower[y];
        }
        
        internalNonlinearDiffusionDiagonalSolver(xs, xs+h, as,
                            diag.begin(), upper.begin(), lower.begin(), res.begin());
                            
        for(y=0; y<h; ++y, ++xd)
        {
            ad.set(0.5 * (ad(xd) + res[y]), xd);
        }
    }
}

/** \addtogroup NonLinearDiffusion Non-linear Diffusion and Total Variation
    
    Perform edge-preserving smoothing.
*/
//@{

/********************************************************/
/*                                                      */
/*                  nonlinearDiffusion                  */
/*                                                      */
/********************************************************/

/** \brief Perform edge-preserving smoothing at the given scale.

    The algorithm solves the non-linear diffusion equation
    
    \f[
        \frac{\partial}{\partial t} u =
        \frac{\partial}{\partial x}
          \left( g(|\nabla u|)
                 \frac{\partial}{\partial x} u
          \right)
    \f]
    
    where <em> t</em> is the time, <b> x</b> is the location vector,
    <em> u(</em><b> x</b><em> , t)</em> is the smoothed image at time <em> t</em>, and
    <em> g(.)</em> is the location dependent diffusivity. At time zero, the image
    <em> u(</em><b> x</b><em> , 0)</em> is simply the original image. The time is
    proportional to the square of the scale parameter: \f$t = s^2\f$.
    The diffusion equation is solved iteratively according
    to the Additive Operator Splitting Scheme (AOS) from
    
    J. Weickert: <em>"Recursive Separable Schemes for Nonlinear Diffusion
    Filters"</em>,
    in: B. ter Haar Romeny, L. Florack, J. Koenderingk, M. Viergever (eds.):
        1st Intl. Conf. on Scale-Space Theory in Computer Vision 1997,
        Springer LNCS 1252

    <TT>DiffusivityFunctor</TT> implements the gradient-dependent local diffusivity.
    It is passed
    as an argument to \ref gradientBasedTransform(). The return value must be
    between 0 and 1 and determines the weight a pixel gets when
    its neighbors are smoothed. Weickert recommends the use of the diffusivity
    implemented by class \ref DiffusivityFunctor. It's also possible to use
    other functors, for example one that always returns 1, in which case
    we obtain the solution to the linear diffusion equation, i.e.
    Gaussian convolution.
    
    The source value type must be a
    linear space with internal addition, scalar multiplication, and
    NumericTraits defined. The value_type of the DiffusivityFunctor must be the
    scalar field over wich the source value type's linear space is defined.
    
    In addition to <TT>nonlinearDiffusion()</TT>, there is an algorithm
    <TT>nonlinearDiffusionExplicit()</TT> which implements the Explicit Scheme
    described in the above article. Both algorithms have the same interface,
    but the explicit scheme gives slightly more accurate approximations of
    the diffusion process at the cost of much slower processing.
    
    <b> Declarations:</b>
    
    pass 2D array views:
    \code
    namespace vigra {
        template <class T1, class S1,
                  class T2, class S2,
                  class DiffusivityFunc>
        void
        nonlinearDiffusion(MultiArrayView<2, T1, S1> const & src,
                           MultiArrayView<2, T2, S2> dest,
                           DiffusivityFunc const & weight, double scale);
                                   
        template <class T1, class S1,
                  class T2, class S2,
                  class DiffusivityFunc>
        void
        nonlinearDiffusionExplicit(MultiArrayView<2, T1, S1> const & src,
                                   MultiArrayView<2, T2, S2> dest,
                                   DiffusivityFunc const & weight, double scale);
    }
    \endcode
    
    \deprecatedAPI{nonlinearDiffusion}
    pass \ref ImageIterators and \ref DataAccessors :
    \code
    namespace vigra {
        template <class SrcIterator, class SrcAccessor,
                  class DestIterator, class DestAccessor,
                  class DiffusivityFunctor>
        void nonlinearDiffusion(SrcIterator sul, SrcIterator slr, SrcAccessor as,
                                DestIterator dul, DestAccessor ad,
                                DiffusivityFunctor const & weight, double scale);
    }
    \endcode
    use argument objects in conjunction with \ref ArgumentObjectFactories :
    \code
    namespace vigra {
        template <class SrcIterator, class SrcAccessor,
                  class DestIterator, class DestAccessor,
                  class DiffusivityFunctor>
        void nonlinearDiffusion(
                  triple<SrcIterator, SrcIterator, SrcAccessor> src,
                  pair<DestIterator, DestAccessor> dest,
                  DiffusivityFunctor const & weight, double scale);
    }
    \endcode
    \deprecatedEnd
    
    <b> Usage:</b>
    
    <b>\#include</b> \<vigra/nonlineardiffusion.hxx\><br/>
    Namespace: vigra
    
    \code
    MultiArray<2, float> src(w,h), dest(w,h);
    float edge_threshold, scale;
    ...
    
    nonlinearDiffusion(src, dest,
                       DiffusivityFunctor<float>(edge_threshold), scale);
    \endcode

    \deprecatedUsage{nonlinearDiffusion}
    \code
    FImage src(w,h), dest(w,h);
    float edge_threshold, scale;
    ...
    
    nonlinearDiffusion(srcImageRange(src), destImage(dest),
                       DiffusivityFunctor<float>(edge_threshold), scale);
    \endcode
    <b> Required Interface:</b>
    <ul>
    <li> <TT>SrcIterator</TT> and <TT>DestIterator</TT> are models of ImageIterator
    <li> <TT>SrcAccessor</TT> and <TT>DestAccessor</TT> are models of StandardAccessor
    <li> <TT>SrcAccessor::value_type</TT> is a linear space
    <li> <TT>DiffusivityFunctor</TT> conforms to the requirements of
          \ref gradientBasedTransform(). Its range is between 0 and 1.
    <li> <TT>DiffusivityFunctor::value_type</TT> is an algebraic field
    </ul>
    \deprecatedEnd
    
    <b> Precondition:</b>
    
    <TT>scale > 0</TT>
    
    \see vigra::DiffusivityFunctor
*/
doxygen_overloaded_function(template <...> void nonlinearDiffusion)

template <class SrcIterator, class SrcAccessor,
          class DestIterator, class DestAccessor,
          class DiffusivityFunc>
void nonlinearDiffusion(SrcIterator sul, SrcIterator slr, SrcAccessor as,
                   DestIterator dul, DestAccessor ad,
                   DiffusivityFunc const & weight, double scale)
{
    vigra_precondition(scale > 0.0, "nonlinearDiffusion(): scale must be > 0");
    
    double total_time = scale*scale/2.0;
    const double time_step = 5.0;
    int number_of_steps = (int)(total_time / time_step);
    double rest_time = total_time - time_step * number_of_steps;
    
    Size2D size(slr.x - sul.x, slr.y - sul.y);

    typedef typename
        NumericTraits<typename SrcAccessor::value_type>::RealPromote
        TmpType;
    typedef typename DiffusivityFunc::value_type WeightType;
    
    BasicImage<TmpType> smooth1(size);
    BasicImage<TmpType> smooth2(size);
    
    BasicImage<WeightType> weights(size);
    
    typename BasicImage<TmpType>::Iterator s1 = smooth1.upperLeft(),
                                  s2 = smooth2.upperLeft();
    typename BasicImage<TmpType>::Accessor a = smooth1.accessor();
    
    typename BasicImage<WeightType>::Iterator wi = weights.upperLeft();
    typename BasicImage<WeightType>::Accessor wa = weights.accessor();

    gradientBasedTransform(sul, slr, as, wi, wa, weight);

    internalNonlinearDiffusionAOSStep(sul, slr, as, wi, wa, s1, a, rest_time);

    for(int i = 0; i < number_of_steps; ++i)
    {
        gradientBasedTransform(s1, s1+size, a, wi, wa, weight);
                      
        internalNonlinearDiffusionAOSStep(s1, s1+size, a, wi, wa, s2, a, time_step);
    
        std::swap(s1, s2);
    }
    
    copyImage(s1, s1+size, a, dul, ad);
}

template <class SrcIterator, class SrcAccessor,
          class DestIterator, class DestAccessor,
          class DiffusivityFunc>
inline void
nonlinearDiffusion(triple<SrcIterator, SrcIterator, SrcAccessor> src,
                   pair<DestIterator, DestAccessor> dest,
                   DiffusivityFunc const & weight, double scale)
{
    nonlinearDiffusion(src.first, src.second, src.third,
                       dest.first, dest.second,
                       weight, scale);
}

template <class T1, class S1,
          class T2, class S2,
          class DiffusivityFunc>
inline void
nonlinearDiffusion(MultiArrayView<2, T1, S1> const & src,
                   MultiArrayView<2, T2, S2> dest,
                   DiffusivityFunc const & weight, double scale)
{
    vigra_precondition(src.shape() == dest.shape(),
        "nonlinearDiffusion(): shape mismatch between input and output.");
    nonlinearDiffusion(srcImageRange(src),
                       destImage(dest),
                       weight, scale);
}

/********************************************************/

template <class SrcIterator, class SrcAccessor,
          class WeightIterator, class WeightAccessor,
          class DestIterator, class DestAccessor>
void internalNonlinearDiffusionExplicitStep(
                   SrcIterator sul, SrcIterator slr, SrcAccessor as,
                   WeightIterator wul, WeightAccessor aw,
                   DestIterator dul, DestAccessor ad,
                   double time_step)
{
    // use traits to determine SumType as to prevent possible overflow
    typedef typename
        NumericTraits<typename SrcAccessor::value_type>::RealPromote
        SumType;
    
    typedef typename
        NumericTraits<typename WeightAccessor::value_type>::RealPromote
        WeightType;
        
    // calculate width and height of the image
    int w = slr.x - sul.x;
    int h = slr.y - sul.y;

    int x,y;
    
    const Diff2D left(-1, 0);
    const Diff2D right(1, 0);
    const Diff2D top(0, -1);
    const Diff2D bottom(0, 1);
    
    WeightType gt, gb, gl, gr, g0;
    WeightType one = NumericTraits<WeightType>::one();
    SumType sum;
    
    time_step /= 2.0;
    
    // create y iterators
    SrcIterator ys = sul;
    WeightIterator yw = wul;
    DestIterator yd = dul;
        
    SrcIterator xs = ys;
    WeightIterator xw = yw;
    DestIterator xd = yd;
    
    gt = (aw(xw) + aw(xw, bottom)) * time_step;
    gb = (aw(xw) + aw(xw, bottom)) * time_step;
    gl = (aw(xw) + aw(xw, right)) * time_step;
    gr = (aw(xw) + aw(xw, right)) * time_step;
    g0 = one - gt - gb - gr - gl;

    sum = g0 * as(xs);
    sum += gt * as(xs, bottom);
    sum += gb * as(xs, bottom);
    sum += gl * as(xs, right);
    sum += gr * as(xs, right);

    ad.set(sum, xd);

    for(x=2, ++xs.x, ++xd.x, ++xw.x; x<w; ++x, ++xs.x, ++xd.x, ++xw.x)
    {
        gt = (aw(xw) + aw(xw, bottom)) * time_step;
        gb = (aw(xw) + aw(xw, bottom)) * time_step;
        gl = (aw(xw) + aw(xw, left)) * time_step;
        gr = (aw(xw) + aw(xw, right)) * time_step;
        g0 = one - gt - gb - gr - gl;

        sum = g0 * as(xs);
        sum += gt * as(xs, bottom);
        sum += gb * as(xs, bottom);
        sum += gl * as(xs, left);
        sum += gr * as(xs, right);

        ad.set(sum, xd);
    }

    gt = (aw(xw) + aw(xw, bottom)) * time_step;
    gb = (aw(xw) + aw(xw, bottom)) * time_step;
    gl = (aw(xw) + aw(xw, left)) * time_step;
    gr = (aw(xw) + aw(xw, left)) * time_step;
    g0 = one - gt - gb - gr - gl;

    sum = g0 * as(xs);
    sum += gt * as(xs, bottom);
    sum += gb * as(xs, bottom);
    sum += gl * as(xs, left);
    sum += gr * as(xs, left);

    ad.set(sum, xd);
    
    for(y=2, ++ys.y, ++yd.y, ++yw.y; y<h; ++y, ++ys.y, ++yd.y, ++yw.y)
    {
        xs = ys;
        xd = yd;
        xw = yw;
        
        gt = (aw(xw) + aw(xw, top)) * time_step;
        gb = (aw(xw) + aw(xw, bottom)) * time_step;
        gl = (aw(xw) + aw(xw, right)) * time_step;
        gr = (aw(xw) + aw(xw, right)) * time_step;
        g0 = one - gt - gb - gr - gl;

        sum = g0 * as(xs);
        sum += gt * as(xs, top);
        sum += gb * as(xs, bottom);
        sum += gl * as(xs, right);
        sum += gr * as(xs, right);

        ad.set(sum, xd);
        
        for(x=2, ++xs.x, ++xd.x, ++xw.x; x<w; ++x, ++xs.x, ++xd.x, ++xw.x)
        {
            gt = (aw(xw) + aw(xw, top)) * time_step;
            gb = (aw(xw) + aw(xw, bottom)) * time_step;
            gl = (aw(xw) + aw(xw, left)) * time_step;
            gr = (aw(xw) + aw(xw, right)) * time_step;
            g0 = one - gt - gb - gr - gl;
            
            sum = g0 * as(xs);
            sum += gt * as(xs, top);
            sum += gb * as(xs, bottom);
            sum += gl * as(xs, left);
            sum += gr * as(xs, right);
            
            ad.set(sum, xd);
        }
        
        gt = (aw(xw) + aw(xw, top)) * time_step;
        gb = (aw(xw) + aw(xw, bottom)) * time_step;
        gl = (aw(xw) + aw(xw, left)) * time_step;
        gr = (aw(xw) + aw(xw, left)) * time_step;
        g0 = one - gt - gb - gr - gl;

        sum = g0 * as(xs);
        sum += gt * as(xs, top);
        sum += gb * as(xs, bottom);
        sum += gl * as(xs, left);
        sum += gr * as(xs, left);

        ad.set(sum, xd);
    }
    
    xs = ys;
    xd = yd;
    xw = yw;

    gt = (aw(xw) + aw(xw, top)) * time_step;
    gb = (aw(xw) + aw(xw, top)) * time_step;
    gl = (aw(xw) + aw(xw, right)) * time_step;
    gr = (aw(xw) + aw(xw, right)) * time_step;
    g0 = one - gt - gb - gr - gl;

    sum = g0 * as(xs);
    sum += gt * as(xs, top);
    sum += gb * as(xs, top);
    sum += gl * as(xs, right);
    sum += gr * as(xs, right);

    ad.set(sum, xd);

    for(x=2, ++xs.x, ++xd.x, ++xw.x; x<w; ++x, ++xs.x, ++xd.x, ++xw.x)
    {
        gt = (aw(xw) + aw(xw, top)) * time_step;
        gb = (aw(xw) + aw(xw, top)) * time_step;
        gl = (aw(xw) + aw(xw, left)) * time_step;
        gr = (aw(xw) + aw(xw, right)) * time_step;
        g0 = one - gt - gb - gr - gl;

        sum = g0 * as(xs);
        sum += gt * as(xs, top);
        sum += gb * as(xs, top);
        sum += gl * as(xs, left);
        sum += gr * as(xs, right);

        ad.set(sum, xd);
    }

    gt = (aw(xw) + aw(xw, top)) * time_step;
    gb = (aw(xw) + aw(xw, top)) * time_step;
    gl = (aw(xw) + aw(xw, left)) * time_step;
    gr = (aw(xw) + aw(xw, left)) * time_step;
    g0 = one - gt - gb - gr - gl;

    sum = g0 * as(xs);
    sum += gt * as(xs, top);
    sum += gb * as(xs, top);
    sum += gl * as(xs, left);
    sum += gr * as(xs, left);

    ad.set(sum, xd);
}

/** \brief Perform edge-preserving smoothing at the given scale using an explicit scheme.

    See \ref nonlinearDiffusion().
*/
doxygen_overloaded_function(template <...> void nonlinearDiffusionExplicit)

template <class SrcIterator, class SrcAccessor,
          class DestIterator, class DestAccessor,
          class DiffusivityFunc>
void nonlinearDiffusionExplicit(SrcIterator sul, SrcIterator slr, SrcAccessor as,
                   DestIterator dul, DestAccessor ad,
                   DiffusivityFunc const & weight, double scale)
{
    vigra_precondition(scale > 0.0, "nonlinearDiffusionExplicit(): scale must be > 0");
    
    double total_time = scale*scale/2.0;
    const double time_step = 0.25;
    int number_of_steps = total_time / time_step;
    double rest_time = total_time - time_step * number_of_steps;
    
    Size2D size(slr.x - sul.x, slr.y - sul.y);

    typedef typename
        NumericTraits<typename SrcAccessor::value_type>::RealPromote
        TmpType;
    typedef typename DiffusivityFunc::value_type WeightType;
    
    BasicImage<TmpType> smooth1(size);
    BasicImage<TmpType> smooth2(size);
    
    BasicImage<WeightType> weights(size);
    
    typename BasicImage<TmpType>::Iterator s1 = smooth1.upperLeft(),
                                  s2 = smooth2.upperLeft();
    typename BasicImage<TmpType>::Accessor a = smooth1.accessor();
    
    typename BasicImage<WeightType>::Iterator wi = weights.upperLeft();
    typename BasicImage<WeightType>::Accessor wa = weights.accessor();

    gradientBasedTransform(sul, slr, as, wi, wa, weight);

    internalNonlinearDiffusionExplicitStep(sul, slr, as, wi, wa, s1, a, rest_time);

    for(int i = 0; i < number_of_steps; ++i)
    {
        gradientBasedTransform(s1, s1+size, a, wi, wa, weight);
                      
        internalNonlinearDiffusionExplicitStep(s1, s1+size, a, wi, wa, s2, a, time_step);
    
        swap(s1, s2);
    }
    
    copyImage(s1, s1+size, a, dul, ad);
}

template <class SrcIterator, class SrcAccessor,
          class DestIterator, class DestAccessor,
          class DiffusivityFunc>
inline void
nonlinearDiffusionExplicit(triple<SrcIterator, SrcIterator, SrcAccessor> src,
                           pair<DestIterator, DestAccessor> dest,
                           DiffusivityFunc const & weight, double scale)
{
    nonlinearDiffusionExplicit(src.first, src.second, src.third,
                               dest.first, dest.second,
                               weight, scale);
}

template <class T1, class S1,
          class T2, class S2,
          class DiffusivityFunc>
inline void
nonlinearDiffusionExplicit(MultiArrayView<2, T1, S1> const & src,
                           MultiArrayView<2, T2, S2> dest,
                           DiffusivityFunc const & weight, double scale)
{
    vigra_precondition(src.shape() == dest.shape(),
        "nonlinearDiffusionExplicit(): shape mismatch between input and output.");
    nonlinearDiffusionExplicit(srcImageRange(src),
                               destImage(dest),
                               weight, scale);
}

/********************************************************/
/*                                                      */
/*                   DiffusivityFunctor                 */
/*                                                      */
/********************************************************/

/** \brief Diffusivity functor for non-linear diffusion.

    This functor implements the diffusivity recommended by Weickert:
    
    \f[
        g(|\nabla u|) = 1 -
           \exp{\left(\frac{-3.315}{(|\nabla u| / thresh)^4}\right)}
    \f]
    
    
    where <TT>thresh</TT> is a threshold that determines whether a specific gradient
    magnitude is interpreted as a significant edge (above the threshold)
    or as noise. It is meant to be used with \ref nonlinearDiffusion().
*/
template <class Value>
class DiffusivityFunctor
{
  public:
         /** the functors first argument type (must be an algebraic field with <TT>exp()</TT> defined).
             However, the functor also works with RGBValue<first_argument_type> (this hack is
             necessary since Microsoft C++ doesn't support partial specialization).
         */
    typedef Value first_argument_type;
    
         /** the functors second argument type (same as first).
             However, the functor also works with RGBValue<second_argument_type> (this hack is
             necessary since Microsoft C++ doesn't support partial specialization).
         */
    typedef Value second_argument_type;
    
         /** the functors result type
         */
    typedef typename NumericTraits<Value>::RealPromote result_type;
    
         /** \deprecated use first_argument_type, second_argument_type, result_type
         */
    typedef Value value_type;
    
         /** init functor with given edge threshold
         */
    DiffusivityFunctor(Value const & thresh)
    : weight_(thresh*thresh),
      one_(NumericTraits<result_type>::one()),
      zero_(NumericTraits<result_type>::zero())
    {}
    
         /** calculate diffusivity from scalar arguments
         */
    result_type operator()(first_argument_type const & gx, second_argument_type const & gy) const
    {
        Value mag = (gx*gx + gy*gy) / weight_;
                     
        return (mag == zero_) ? one_ : one_ - VIGRA_CSTD::exp(-3.315 / mag / mag);
    }
    
         /** calculate diffusivity from RGB arguments
         */
    result_type operator()(RGBValue<Value> const & gx, RGBValue<Value> const & gy) const
    {
        result_type mag = (gx.red()*gx.red() +
                     gx.green()*gx.green() +
                     gx.blue()*gx.blue() +
                     gy.red()*gy.red() +
                     gy.green()*gy.green() +
                     gy.blue()*gy.blue()) / weight_;

        return (mag == zero_) ? one_ : one_ - VIGRA_CSTD::exp(-3.315 / mag / mag);
    }
    
    result_type weight_;
    result_type one_;
    result_type zero_;
};

template <class ValueType>
class FunctorTraits<DiffusivityFunctor<ValueType> >
: public FunctorTraitsBase<DiffusivityFunctor<ValueType> >
{
  public:
    typedef VigraTrueType isBinaryFunctor;
};


//@}

} // namespace vigra

#endif /* VIGRA_NONLINEARDIFFUSION_HXX */