File: vigra_support.hpp

package info (click to toggle)
gamera 1:3.4.2+git20160808.1725654-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 22,312 kB
  • ctags: 24,991
  • sloc: xml: 122,324; ansic: 52,869; cpp: 50,664; python: 35,034; makefile: 118; sh: 101
file content (972 lines) | stat: -rw-r--r-- 30,329 bytes parent folder | download | duplicates (4)
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
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
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
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
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
/*
 *
 * Copyright (C) 2001-2005 Ichiro Fujinaga, Michael Droettboom, Karl MacMillan
 *               2009      Jonathan Koch 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/*
  Much of this code is a modified version of Vigra code and should be
  covered by the following copyright information:
*/

/************************************************************************/
/*                                                                      */
/*               Copyright 1998-2001 by Ullrich Koethe                  */
/*       Cognitive Systems Group, University of Hamburg, Germany        */
/*                                                                      */
/*    This file is part of the VIGRA computer vision library.           */
/*    ( Version 1.1.4, Nov 23 2001 )                                    */
/*    ( Version 1.1.4, Nov 23 2001 )                                    */
/*    You may use, modify, and distribute this software according       */
/*    to the terms stated in the LICENSE file included in               */
/*    the VIGRA distribution.                                           */
/*                                                                      */
/*    The VIGRA Website is                                              */
/*        http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/      */
/*    Please direct questions, bug reports, and contributions to        */
/*        koethe@informatik.uni-hamburg.de                              */
/*                                                                      */
/*  THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY EXPRESS OR          */
/*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED      */
/*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
/*                                                                      */
/************************************************************************/


#ifndef kwm03072002_vigra_support
#define kwm03072002_vigra_support

#include "vigra/rgbvalue.hxx"
#include "vigra/accessor.hxx"
#include "vigra/interpolating_accessor.hxx"
#include "image_types.hpp"
#include "static_image.hpp"

using namespace vigra;

/*
  This is the necessary support to use the VIGRA library. Included are
  convenience functions to make the calling of the vigra library algorithms
  easiers and accessors for a variety of Gamera image types. The accessor
  also invert 0 and 1 from the Gamera standards for OneBitPixel for
  interoperability with vigra.
*/

namespace Gamera {

  template<class T>
  class Accessor {
  public:
    typedef T value_type;
    typedef T VALUETYPE;

    template <class ITERATOR>
    VALUETYPE operator()(ITERATOR const & i) const {
      return m_accessor(i);
    }
    
    template <class ITERATOR, class DIFFERENCE>
    VALUETYPE operator()(ITERATOR & i, DIFFERENCE diff) const
    {
      ITERATOR tmp = i + diff;
      return m_accessor(tmp);
    }

    template <class V, class ITERATOR>
    void set(V const & value, ITERATOR & i) const 
    {
      VALUETYPE tmp = vigra::detail::RequiresExplicitCast<VALUETYPE>::cast(value);
      m_accessor.set(tmp, i);
    }

    template <class V, class ITERATOR, class DIFFERENCE>
    void set(V const & value, ITERATOR & i, DIFFERENCE diff) const 
    { 
        VALUETYPE tmp = vigra::detail::RequiresExplicitCast<VALUETYPE>::cast(value); 
	ITERATOR tmpi = i + diff;
	m_accessor.set(tmp, tmpi);
    }

    ImageAccessor<T> m_accessor;
  };
  
  template <class SEQUENCE>
  class SequenceAccessor : public Gamera::Accessor<SEQUENCE> {
  public:
    typedef typename SEQUENCE::value_type component_type;
    typedef typename SEQUENCE::iterator iterator;

    template <class ITERATOR>
    iterator begin(ITERATOR & i) const { 
        return (*i).begin(); 
    }
    
    template <class ITERATOR>
    iterator end(ITERATOR & i)  const {
         return (*i).end(); 
    }

    template <class ITERATOR, class DIFFERENCE>
    iterator begin(ITERATOR & i, DIFFERENCE diff)  const { 
        return i[diff].begin(); 
    }
    
    template <class ITERATOR, class DIFFERENCE>
    iterator end(ITERATOR & i, DIFFERENCE diff)  const { 
        return i[diff].end(); 
    }

    template <class ITERATOR>
    int size(ITERATOR & i) const { return (*i).size(); }

    template <class ITERATOR, class DIFFERENCE>
    int size(ITERATOR & i, DIFFERENCE diff) const { return i[diff].size(); }
  };

  template <class VECTOR>
  class VectorAccessor : public Gamera::SequenceAccessor<VECTOR> {
  public:
    typedef typename VECTOR::value_type component_type;
    
    template <class ITERATOR>
    component_type getComponent(ITERATOR & i, int idx) const { 
        return (*i)[idx]; 
    }

    template <class V, class ITERATOR>
    void setComponent(V const & value, ITERATOR & i, int idx) const { 
        (*i)[idx] = detail::RequiresExplicitCast<component_type>::cast(value); 
    }

    template <class ITERATOR, class DIFFERENCE>
    component_type getComponent(ITERATOR & i, DIFFERENCE diff, int idx) const { 
        return i[diff][idx]; 
    }
    
    template <class V, class ITERATOR, class DIFFERENCE>
    void 
    setComponent(V const & value, ITERATOR & i, DIFFERENCE diff, int idx) const  { 
        i[diff][idx] = detail::RequiresExplicitCast<component_type>::cast(value); 
    }
  };

  template <class RGBVALUE>
  class RGBAccessor : public Gamera::VectorAccessor<RGBVALUE> {
  public:
    typedef typename RGBVALUE::value_type component_type;

    template <class RGBIterator>
    component_type red(RGBIterator & rgb) const {
      return (*rgb).red();
    }

    template <class V, class RGBIterator>
    void setRed(V value, RGBIterator & rgb) const {
      (*rgb).setRed(value);
    }
    
    template <class RGBIterator, class DIFFERENCE>
    component_type red(RGBIterator & rgb, DIFFERENCE diff) const {
      return rgb[diff].red();
    }

    template <class V, class RGBIterator, class DIFFERENCE>
    void setRed(V value, RGBIterator & rgb, DIFFERENCE diff) const {
      rgb[diff].setRed(value);
    }
       
    template <class RGBIterator>
    component_type green(RGBIterator & rgb) const {
      return (*rgb).green();
    }

    template <class V, class RGBIterator>
    void setGreen(V value, RGBIterator & rgb) const {
      (*rgb).setGreen(value);
    }

    template <class RGBIterator, class DIFFERENCE>
    component_type green(RGBIterator & rgb, DIFFERENCE d) const {
      return rgb[d].green();
    }

    template <class V, class RGBIterator, class DIFFERENCE>
    void setGreen(V value, RGBIterator & rgb, DIFFERENCE d) const {
      rgb[d].setGreen(value);
    }

    template <class RGBIterator>
    component_type blue(RGBIterator & rgb) const {
      return (*rgb).blue();
    }
    
    template <class V, class RGBIterator>
    void setBlue(V value, RGBIterator & rgb) const {
      (*rgb).setBlue(value);
    }

    template <class RGBIterator, class DIFFERENCE>
    component_type blue(RGBIterator & rgb, DIFFERENCE d) const {
      return rgb[d].blue();
    }
    
    template <class V, class RGBIterator, class DIFFERENCE>
    void setBlue(V value, RGBIterator & rgb, DIFFERENCE d) const {
      rgb[d].setBlue(value);
    }

//     template <class V1, class RGBIterator>
//     void set(RGBValue<V1> value, RGBIterator& rgb) const {
//       typedef typename RGBVALUE::value_type V;
//       (*rgb).setRGB(NumericTraits<V>::fromPromote(value.red()), 
// 		    NumericTraits<V>::fromPromote(value.green()), 
// 		    NumericTraits<V>::fromPromote(value.blue()));
//     }
  };

  class ComplexRealAccessor
  {
  public:
    
    /// The accessor's value type.
    typedef double value_type;
    
    /// Read real part at iterator position.
    template <class Iterator>
    value_type operator()(Iterator const & i) const {
      return (*i).real();
    }
    
    /// Read real part at offset from iterator position.
    template <class Iterator, class Difference>
    value_type operator()(Iterator const & i, Difference d) const {
      return (*(i+d)).real();
    }
    
    /// Write real part at iterator position.
    template <class Iterator>
    void set(value_type const & v, Iterator const & i) const {
      (*i) = v;
    }
    
    /// Write real part at offset from iterator position.
    template <class Iterator, class Difference>
    void set(value_type const & v, Iterator const & i, Difference d) const {
      (*(i+d)) = v;
    }
  };

  class RGBRealAccessor {
  public:

        /// The accessor's value type.
    typedef double value_type;

        /// Read real part at iterator position.
    template <class Iterator>
    value_type operator()(Iterator const & i) const {
      return (*i).luminance();
    }
    
        /// Read real part at offset from iterator position.
    template <class Iterator, class Difference>
    value_type operator()(Iterator const & i, Difference d) const {
      return (*(i+d)).luminance();
    }
    
        /// Write real part at iterator position.
    template <class Iterator>
    void set(value_type const & v, Iterator const & i) const {
      (*i).setRed(v);
      (*i).setGreen(v);
      (*i).setBlue(v);
    }

        /// Write real part at offset from iterator position.
    template <class Iterator, class Difference>
    void set(value_type const & v, Iterator const & i, Difference d) const {
      (*(i+d)).setRed(v);
      (*(i+d)).setGreen(v);
      (*(i+d)).setBlue(v);
    }
  };
  
  /*
    The CCAccessor provides filtering of pixels based on an image label. This serves the
    same purpose as the CCProxy in connected_component_iterators.hpp.
  */

  class CCAccessor {
  public:
    typedef OneBitPixel value_type;
    typedef OneBitPixel VALUETYPE;

    CCAccessor(OneBitPixel label) : m_label(label) { }
    
    template <class ITERATOR>
    VALUETYPE operator()(ITERATOR const & i) const {
      if (m_label == m_accessor(i))
      	return 0;
      else
      	return 1;
    }
    
    template <class ITERATOR, class DIFFERENCE>
    VALUETYPE operator()(ITERATOR & i, DIFFERENCE diff) const
    {
      ITERATOR tmp = i + diff;
      if (m_label == m_accessor(tmp))
        return 0; 
      else
      	return 1;
    }

    template <class V, class ITERATOR>
    void set(V const & value, ITERATOR & i) const 
    {
      VALUETYPE tmp = vigra::detail::RequiresExplicitCast<VALUETYPE>::cast(value);
      if (m_accessor(i) == m_label) {
	      if (tmp) {
	        m_accessor.set(0, i);
	      } else {
	        m_accessor.set(m_label, i);
	      }
      }
    }

    template <class V, class ITERATOR, class DIFFERENCE>
    void set(V const & value, ITERATOR & i, DIFFERENCE diff) const 
    { 
      VALUETYPE tmp = vigra::detail::RequiresExplicitCast<VALUETYPE>::cast(value); 
	    ITERATOR tmpi = i + diff;
	    if (m_accessor(tmpi) == m_label) {
	      if (tmp) {
	        m_accessor.set(0, tmpi);
	      } else {
	        m_accessor.set(m_label, tmpi);
	      }
      }
    }
    OneBitPixel m_label;
    ImageAccessor<value_type> m_accessor;
  };

  class RawCCAccessor {
  public:
    typedef OneBitPixel value_type;
    typedef OneBitPixel VALUETYPE;

    RawCCAccessor(OneBitPixel label) : m_label(label) { }
    
    template <class ITERATOR>
    VALUETYPE operator()(ITERATOR const & i) const {
      if (m_label == m_accessor(i))
	      return 1;
      else
	      return 0;
    }
    
    template <class ITERATOR, class DIFFERENCE>
    VALUETYPE operator()(ITERATOR & i, DIFFERENCE diff) const
    {
      ITERATOR tmp = i + diff;
      if (m_label == m_accessor(tmp))
        return 1; 
      else
	      return 0;
    }

    template <class V, class ITERATOR>
    void set(V const & value, ITERATOR & i) const 
    {
      VALUETYPE tmp = vigra::detail::RequiresExplicitCast<VALUETYPE>::cast(value);
      if (m_accessor(i) == m_label) {
	      if (tmp) {
	        m_accessor.set(m_label, i);
	      } else {
	        m_accessor.set(0, i);
      	}
      }
    }

    template <class V, class ITERATOR, class DIFFERENCE>
    void set(V const & value, ITERATOR & i, DIFFERENCE diff) const 
    { 
      VALUETYPE tmp = vigra::detail::RequiresExplicitCast<VALUETYPE>::cast(value); 
	    ITERATOR tmpi = i + diff;
	    if (m_accessor(tmpi) == m_label) {
	      if (tmp) {
	        m_accessor.set(m_label, tmpi);
	      } else {
	        m_accessor.set(0, tmpi);
	      }
      }
    }
    OneBitPixel m_label;
    ImageAccessor<value_type> m_accessor;
  };

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

  /*
    The MLCCAccessor provides filtering of pixels based on an image label. This serves the
    same purpose as the MLCCProxy in connected_component_iterators.hpp.
  */

  class MLCCAccessor {
  public:
    typedef OneBitPixel value_type;
    typedef OneBitPixel VALUETYPE;

    MLCCAccessor(const std::map<value_type, Rect*>* labels){
      m_labels=labels;
    }
    
    inline bool has_label(value_type value) const {
      return m_labels->find(value)==m_labels->end();
    }
    
    template <class ITERATOR>
    VALUETYPE operator()(ITERATOR const & i) const {
      if (has_label(m_accessor(i)))
      	return 0;
      else
      	return 1;
    }
    
    template <class ITERATOR, class DIFFERENCE>
    VALUETYPE operator()(ITERATOR & i, DIFFERENCE diff) const
    {
      ITERATOR tmp = i + diff;
      if (has_label(m_accessor(tmp)))
        return 0; 
      else
      	return 1;
    }

    template <class V, class ITERATOR>
    void set(V const & value, ITERATOR & i) const 
    {
      VALUETYPE tmp = vigra::detail::RequiresExplicitCast<VALUETYPE>::cast(value);
      value_type val=m_accessor(i);
      if (has_label(val)) {
	      if (tmp) {
	        m_accessor.set(0, i);
	      } else {
	        m_accessor.set(val, i);
	      }
      }
    }

    template <class V, class ITERATOR, class DIFFERENCE>
    void set(V const & value, ITERATOR & i, DIFFERENCE diff) const 
    { 
      VALUETYPE tmp = vigra::detail::RequiresExplicitCast<VALUETYPE>::cast(value); 
	    ITERATOR tmpi = i + diff;
	    value_type val=m_accessor(tmpi);
	    if (has_label(val)) {
	      if (tmp) {
	        m_accessor.set(0, tmpi);
	      } else {
	        m_accessor.set(val, tmpi);
	      }
      }
    }
    const std::map<value_type, Rect*>* m_labels;
    ImageAccessor<value_type> m_accessor;
  };

  class RawMLCCAccessor {
  public:
    typedef OneBitPixel value_type;
    typedef OneBitPixel VALUETYPE;

    RawMLCCAccessor(const std::map<value_type, Rect*>* labels){ 
      m_labels=labels;
    }
    
   inline bool has_label(value_type value) const {
      return m_labels->find(value)==m_labels->end();
    }
    
    template <class ITERATOR>
    VALUETYPE operator()(ITERATOR const & i) const {
      if (has_label(m_accessor(i)))
	      return 1;
      else
	      return 0;
    }
    
    template <class ITERATOR, class DIFFERENCE>
    VALUETYPE operator()(ITERATOR & i, DIFFERENCE diff) const
    {
      ITERATOR tmp = i + diff;
      if (has_label(m_accessor(tmp)))
        return 1; 
      else
	      return 0;
    }

    template <class V, class ITERATOR>
    void set(V const & value, ITERATOR & i) const 
    {
      VALUETYPE tmp = vigra::detail::RequiresExplicitCast<VALUETYPE>::cast(value);
      value_type val=m_accessor(i);
      if (has_label(val)) {
	      if (tmp) {
	        m_accessor.set(val, i);
	      } else {
	        m_accessor.set(0, i);
      	}
      }
    }

    template <class V, class ITERATOR, class DIFFERENCE>
    void set(V const & value, ITERATOR & i, DIFFERENCE diff) const 
    { 
      VALUETYPE tmp = vigra::detail::RequiresExplicitCast<VALUETYPE>::cast(value); 
	    ITERATOR tmpi = i + diff;
	    value_type val=m_accessor(tmpi);
	    if (has_label(val)) {
	      if (tmp) {
	        m_accessor.set(val, tmpi);
	      } else {
	        m_accessor.set(0, tmpi);
	      }
      }
    }
    const std::map<value_type, Rect*>* m_labels;
    ImageAccessor<value_type> m_accessor;
  };


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

  /*
    The OneBitAccessor hides the fact that OneBitValues can be something other
    than 0 or 1 in Gamera images.
  */
  class OneBitAccessor {
  public:
    typedef OneBitPixel value_type;
    typedef OneBitPixel VALUETYPE;
    
    template <class ITERATOR>
    VALUETYPE operator()(ITERATOR const & i) const {
      if (m_accessor(i))
	return 0;
      else
	return 1;
    }
    
    template <class ITERATOR, class DIFFERENCE>
    VALUETYPE operator()(ITERATOR & i, DIFFERENCE diff) const
    { 
      if (m_accessor(i + diff))
	return 0;
      else
	return 1;
    }
    
    template <class V, class ITERATOR>
    void set(V const & value, ITERATOR & i) const 
    { 
      if (value)
	m_accessor.set(0, i);
      else
	m_accessor.set(1, i);
    }
    
    template <class V, class ITERATOR, class DIFFERENCE>
    void set(V const & value, ITERATOR & i, DIFFERENCE diff) const 
    { 
      if (value)
	m_accessor.set(0, i + diff);
      else
	m_accessor.set(1, i + diff);
    }

    ImageAccessor<value_type> m_accessor;
  };

  // A OneBitAccessor that doesn't do any pixel inversion.  Should
  // only be used when converting a OneBit image to another OneBit image.

  class RawOneBitAccessor {
  public:
    typedef OneBitPixel value_type;
    typedef OneBitPixel VALUETYPE;
    
    template <class ITERATOR>
    VALUETYPE operator()(ITERATOR const & i) const {
      return m_accessor(i);
    }
    
    template <class ITERATOR, class DIFFERENCE>
    VALUETYPE operator()(ITERATOR & i, DIFFERENCE diff) const
    { 
      return m_accessor(i + diff);
    }
    
    template <class V, class ITERATOR>
    void set(V const & value, ITERATOR & i) const 
    { 
      if (value)
	m_accessor.set(1, i);
      else
	m_accessor.set(0, i);
    }
    
    template <class V, class ITERATOR, class DIFFERENCE>
    void set(V const & value, ITERATOR & i, DIFFERENCE diff) const 
    { 
      if (value)
	m_accessor.set(1, i + diff);
      else
	m_accessor.set(0, i + diff);
    }

    ImageAccessor<value_type> m_accessor;
  };

  /*
    These classes are used to make the selection of the appropriate accessor for a
    given type easier. They are, in essence, compile time factories that use
    specialization to choose the correct types. Also, the make_accessor static function
    is used to give the same function signature regardless of the arguments to the
    accessor.
  */

  template<class T>
  struct choose_accessor {
    typedef Accessor<typename T::value_type> accessor;
    static accessor make_accessor(const T& mat) {
      return accessor();
    }
    typedef Accessor<typename T::value_type> raw_accessor;
    static raw_accessor make_raw_accessor(const T& mat) {
      return raw_accessor();
    }
    typedef accessor real_accessor;
    static real_accessor make_real_accessor(const T& mat) {
      return real_accessor();
    }
    typedef BilinearInterpolatingAccessor<raw_accessor, typename T::value_type> interp_accessor;
    static interp_accessor make_interp_accessor(const T& mat) {
      return interp_accessor(make_raw_accessor(mat));
    }
  };

  template<>
  struct choose_accessor<RGBImageView> {
    typedef Gamera::RGBAccessor<RGBPixel> accessor;
    static accessor make_accessor(const RGBImageView& mat) {
      return accessor();
    }
    typedef Gamera::RGBAccessor<RGBPixel> raw_accessor;
    static raw_accessor make_raw_accessor(const RGBImageView& mat) {
      return raw_accessor();
    }
    typedef RGBRealAccessor real_accessor;
    static real_accessor make_real_accessor(const RGBImageView& mat) {
      return real_accessor();
    }
    typedef BilinearInterpolatingAccessor<raw_accessor, RGBPixel> interp_accessor;
    static interp_accessor make_interp_accessor(const RGBImageView& mat) {
      return interp_accessor(make_raw_accessor(mat));
    }
  };

  template<>
  struct choose_accessor<ComplexImageView> {
    typedef Accessor<ComplexPixel> accessor;
    static accessor make_accessor(const ComplexImageView& mat) {
      return accessor();
    }
    typedef Accessor<ComplexImageView::value_type> raw_accessor;
    static raw_accessor make_raw_accessor(const ComplexImageView& mat) {
      return raw_accessor();
    }
    typedef ComplexRealAccessor real_accessor;
    static real_accessor make_real_accessor(const ComplexImageView& mat) {
      return real_accessor();
    }
    typedef BilinearInterpolatingAccessor<raw_accessor, ComplexImageView::value_type> interp_accessor;
    static interp_accessor make_interp_accessor(const ComplexImageView& mat) {
      return interp_accessor(make_raw_accessor(mat));
    }
  };

  template<>
  struct choose_accessor<OneBitImageView> {
    typedef OneBitAccessor accessor;
    static accessor make_accessor(const OneBitImageView& mat) {
      return accessor();
    }
    typedef RawOneBitAccessor raw_accessor;
    static raw_accessor make_raw_accessor(const OneBitImageView& mat) {
      return raw_accessor();
    }
    typedef accessor real_accessor;
    static real_accessor make_real_accessor(const OneBitImageView& mat) {
      return real_accessor();
    }
    typedef BilinearInterpolatingAccessor<raw_accessor, OneBitPixel> interp_accessor;
    static interp_accessor make_interp_accessor(const OneBitImageView& mat) {
      return interp_accessor(make_raw_accessor(mat));
    }
  };

  template<>
  struct choose_accessor<OneBitRleImageView> {
    typedef OneBitAccessor accessor;
    static accessor make_accessor(const OneBitRleImageView& mat) {
      return accessor();
    }
    typedef RawOneBitAccessor raw_accessor;
    static raw_accessor make_raw_accessor(const OneBitRleImageView& mat) {
      return raw_accessor();
    }
    typedef accessor real_accessor;
    static real_accessor make_real_accessor(const OneBitRleImageView& mat) {
      return real_accessor();
    }
    typedef BilinearInterpolatingAccessor<raw_accessor, OneBitPixel> interp_accessor;
    static interp_accessor make_interp_accessor(const OneBitRleImageView& mat) {
      return interp_accessor(make_raw_accessor(mat));
    }
  };

  template<>
  struct choose_accessor<StaticImage<OneBitPixel> > {
    typedef OneBitAccessor accessor;
    static accessor make_accessor(const StaticImage<OneBitPixel>& mat) {
      return accessor();
    }
    typedef RawOneBitAccessor raw_accessor;
    static raw_accessor make_raw_accessor(const StaticImage<OneBitPixel>& mat) {
      return raw_accessor();
    }
    typedef accessor real_accessor;
    static real_accessor make_real_accessor(const StaticImage<OneBitPixel>& mat) {
      return real_accessor();
    }
    typedef BilinearInterpolatingAccessor<raw_accessor, OneBitPixel> interp_accessor;
    static interp_accessor make_interp_accessor(const StaticImage<OneBitPixel>& mat) {
      return interp_accessor(make_raw_accessor(mat));
    }
  };

  template<>
  struct choose_accessor<Cc> {
    typedef CCAccessor accessor;
    static accessor make_accessor(const Cc& mat) {
      return accessor(mat.label());
    }
    typedef RawCCAccessor raw_accessor;
    static raw_accessor make_raw_accessor(const Cc& mat) {
      return raw_accessor(mat.label());
    }
    typedef accessor real_accessor;
    static real_accessor make_real_accessor(const Cc& mat) {
      return real_accessor(mat.label());
    }
    typedef BilinearInterpolatingAccessor<raw_accessor, OneBitPixel> interp_accessor;
    static interp_accessor make_interp_accessor(const Cc& mat) {
      return interp_accessor(make_raw_accessor(mat));
    }
  };

  template<>
  struct choose_accessor<MlCc> {
    typedef MLCCAccessor accessor;
    static accessor make_accessor(const MlCc& mat) {
      return accessor(mat.get_labels_pointer());
    }
    typedef RawMLCCAccessor raw_accessor;
    static raw_accessor make_raw_accessor(const MlCc& mat) {
      return raw_accessor(mat.get_labels_pointer());
    }
    typedef accessor real_accessor;
    static real_accessor make_real_accessor(const MlCc& mat) {
      return real_accessor(mat.get_labels_pointer());
    }
    typedef BilinearInterpolatingAccessor<raw_accessor, OneBitPixel> interp_accessor;
    static interp_accessor make_interp_accessor(MlCc& mat) {
      return interp_accessor(make_raw_accessor(mat));
    }
  };

  template<>
  struct choose_accessor<RleCc> {
    typedef CCAccessor accessor;
    static accessor make_accessor(const RleCc& mat) {
      return accessor(mat.label());
    }
    typedef RawCCAccessor raw_accessor;
    static raw_accessor make_raw_accessor(const RleCc& mat) {
      return raw_accessor(mat.label());
    }
    typedef accessor real_accessor;
    static real_accessor make_real_accessor(const RleCc& mat) {
      return real_accessor(mat.label());
    }
    typedef BilinearInterpolatingAccessor<raw_accessor, OneBitPixel> interp_accessor;
    static interp_accessor make_interp_accessor(const RleCc& mat) {
      return interp_accessor(make_raw_accessor(mat));
    }
  };

  /*
    These three functions are for convenience. They create the arguments for Vigra
    algorithms including the appropriate iterators and the corrent accessor for the type.
  */

  template<class Mat>
  inline triple<typename Mat::ConstIterator, typename Mat::ConstIterator,
		typename choose_accessor<Mat>::accessor>
  src_image_range(const Mat& img) {
    return triple<typename Mat::ConstIterator, typename Mat::ConstIterator,
      typename choose_accessor<Mat>::accessor> (img.upperLeft(), img.lowerRight(),
						choose_accessor<Mat>::make_accessor(img));
  }

  template<class Mat>
  inline std::pair<typename Mat::ConstIterator,
	      typename choose_accessor<Mat>::accessor>
  src_image(const Mat& img) {
    return std::pair<typename Mat::ConstIterator,
      typename choose_accessor<Mat>::accessor> (img.upperLeft(),
						choose_accessor<Mat>::make_accessor(img));
  }

  template<class Mat>
  inline triple<typename Mat::Iterator, typename Mat::Iterator,
		typename choose_accessor<Mat>::accessor>
  dest_image_range(Mat& img) {
    return triple<typename Mat::Iterator, typename Mat::Iterator,
      typename choose_accessor<Mat>::accessor> (img.upperLeft(), img.lowerRight(),
				       choose_accessor<Mat>::make_accessor(img));
  }

  template<class Mat>
  inline std::pair<typename Mat::Iterator, typename choose_accessor<Mat>::accessor>
  dest_image(Mat& img) {
    return std::pair<typename Mat::Iterator, typename choose_accessor<Mat>::accessor>(img.upperLeft(),
	       choose_accessor<Mat>::make_accessor(img));
  }
}

/*
  Declare numeric traits for the RGB type in Gamera. This must be done in the vigra namespace,
  though it seems kind of nasty to hijack someone else's namespace . . . 
*/

namespace vigra {
  /*
    NumericTraits for Gamera RGB types.
  */
  using namespace Gamera;

  template<>
  struct NumericTraits<RGBPixel>
  {
    typedef RGBPixel Type;
    typedef RGBValue<NumericTraits<RGBPixel::value_type>::Promote> Promote;
    typedef RGBValue<NumericTraits<RGBPixel::value_type>::RealPromote> RealPromote;
    
    typedef NumericTraits<RGBPixel::value_type>::isIntegral isIntegral;
    typedef VigraFalseType isScalar;
    typedef VigraFalseType isOrdered;
    
    static RGBPixel zero() { 
      return RGBPixel(NumericTraits<RGBPixel::value_type>::zero()); 
    }
    static RGBPixel one() { 
      return RGBPixel(NumericTraits<RGBPixel::value_type>::one()); 
    }
    static RGBPixel nonZero() { 
      return RGBPixel(NumericTraits<RGBPixel::value_type>::nonZero()); 
    }
    
    static Promote toPromote(RGBPixel const & v) { 
      return Promote(v); 
    }
    static RealPromote toRealPromote(RGBPixel const & v) { 
      return RealPromote(v); 
    }
    static RGBPixel fromPromote(Promote const & v) { 
      return RGBPixel(NumericTraits<RGBPixel::value_type>::fromPromote(v.red()),
			 NumericTraits<RGBPixel::value_type>::fromPromote(v.green()),
			 NumericTraits<RGBPixel::value_type>::fromPromote(v.blue()));
    }
    static RGBPixel fromRealPromote(RealPromote const & v) {
      return RGBPixel(NumericTraits<RGBPixel::value_type>::fromRealPromote(v.red()),
			 NumericTraits<RGBPixel::value_type>::fromRealPromote(v.green()),
			 NumericTraits<RGBPixel::value_type>::fromRealPromote(v.blue()));
    }
  };
#if 0  
  template <class T2>
  struct PromoteTraits<RGBPixel, RGBValue<T2> >
  {
    typedef RGBValue<typename PromoteTraits<typename RGBPixel::value_type, T2>::Promote> Promote;
  };
#endif
  
  template<>
  struct PromoteTraits<RGBPixel, double>
  {
    typedef RGBValue<NumericTraits<RGBPixel::value_type>::RealPromote> Promote;
  };
 
  template<>
  struct PromoteTraits<double, RGBPixel>
  {
    typedef RGBValue<NumericTraits<RGBPixel::value_type>::RealPromote> Promote;
  };

template<>
struct NumericTraits<ComplexPixel> {
  typedef ComplexPixel Type;
  typedef ComplexPixel Promote;
  typedef ComplexPixel RealPromote;
  typedef VigraFalseType isIntegral;
  typedef VigraFalseType isScalar;
  typedef VigraFalseType isOrdered;
  
  static ComplexPixel zero() {
    return ComplexPixel(0.0, 0.0);
  }
  static ComplexPixel one() {
    return ComplexPixel(1.0, 0.0);
  }
  static ComplexPixel nonZero() { return one(); }
  
  static ComplexPixel epsilon() { return ComplexPixel(LDBL_EPSILON, LDBL_EPSILON); }  
  
  static ComplexPixel max() { return ComplexPixel(DBL_MAX, DBL_MAX); };
  static ComplexPixel min() { return ComplexPixel(-DBL_MAX, -DBL_MAX); };
  
  static const Promote & toPromote(const Type & v) { return v; }
  static const RealPromote & toRealPromote(const Type & v) { return v; }
  static const Type & fromPromote(const Promote & v) { return v; }
  static const Type & fromRealPromote(const RealPromote & v) { return v; }
};
}
#endif