This file is indexed.

/usr/include/vigra/impexalpha.hxx is in libvigraimpex-dev 1.9.0+dfsg-10+b2.

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
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 2012 Christoph Spiel                         */
/*                                                                      */
/*    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_IMPEXALPHA_HXX
#define VIGRA_IMPEXALPHA_HXX

#include <vector>

#include "imageinfo.hxx"
#include "impex.hxx"
#include "impexbase.hxx"

namespace vigra
{
/** \addtogroup VigraImpex
 * @{
*/
    namespace detail
    {
        template <class ValueType,
                  class ImageIterator, class ImageAccessor,
                  class AlphaIterator, class AlphaAccessor>
        void
        read_image_band_and_alpha(Decoder* decoder,
                                  ImageIterator image_iterator, ImageAccessor image_accessor,
                                  AlphaIterator alpha_iterator, AlphaAccessor alpha_accessor)
        {
            typedef typename ImageIterator::row_iterator ImageRowIterator;
            typedef typename AlphaIterator::row_iterator AlphaRowIterator;

            vigra_precondition(decoder->getNumExtraBands() == 1,
                               "vigra::detail::read_image_band_and_alpha: expecting exactly one alpha band");
            vigra_precondition(decoder->getNumBands() - decoder->getNumExtraBands() == 1,
                               "vigra::detail::read_image_band_and_alpha: expecting exactly one image band");

            const unsigned width(decoder->getWidth());
            const unsigned height(decoder->getHeight());
            const unsigned offset(decoder->getOffset());

            for (unsigned y = 0U; y != height; ++y)
            {
                decoder->nextScanline();

                const ValueType* scanline0 = static_cast<const ValueType*>(decoder->currentScanlineOfBand(0));
                const ValueType* scanline1 = static_cast<const ValueType*>(decoder->currentScanlineOfBand(1));

                ImageRowIterator is(image_iterator.rowIterator());
                const ImageRowIterator is_end(is + width);
                AlphaRowIterator as(alpha_iterator.rowIterator());

                while (is != is_end)
                {
                    image_accessor.set(*scanline0, is);
                    scanline0 += offset;
                    ++is;

                    alpha_accessor.set(*scanline1, as);
                    scanline1 += offset;
                    ++as;
                }

                ++image_iterator.y;
                ++alpha_iterator.y;
            }
        }


        template <class ValueType,
                  class ImageIterator, class ImageAccessor,
                  class AlphaIterator, class AlphaAccessor>
        void
        read_image_bands_and_alpha(Decoder* decoder,
                                   ImageIterator image_iterator, ImageAccessor image_accessor,
                                   AlphaIterator alpha_iterator, AlphaAccessor alpha_accessor)
        {
            typedef typename ImageIterator::row_iterator ImageRowIterator;
            typedef typename AlphaIterator::row_iterator AlphaRowIterator;

            vigra_precondition(decoder->getNumExtraBands() == 1,
                               "vigra::detail::read_image_bands_and_alpha: expecting exactly one alpha band");
            vigra_precondition(decoder->getNumBands() - decoder->getNumExtraBands() == image_accessor.size(image_iterator),
                               "vigra::detail::read_image_bands_and_alpha: number of channels and image accessor do not match");

            const unsigned width(decoder->getWidth());
            const unsigned height(decoder->getHeight());
            const unsigned offset(decoder->getOffset());
            const unsigned accessor_size(image_accessor.size(image_iterator));

            // OPTIMIZATION: Specialization for the most common case
            // of an RGBA-image, i.e. three color channels plus one
            // alpha channel.
            if (accessor_size == 3U)
            {
                const ValueType* scanline_0;
                const ValueType* scanline_1;
                const ValueType* scanline_2;
                const ValueType* scanline_3; // alpha

                for (unsigned y = 0U; y != height; ++y)
                {
                    decoder->nextScanline();

                    scanline_0 = static_cast<const ValueType*>(decoder->currentScanlineOfBand(0));
                    scanline_1 = static_cast<const ValueType*>(decoder->currentScanlineOfBand(1));
                    scanline_2 = static_cast<const ValueType*>(decoder->currentScanlineOfBand(2));
                    scanline_3 = static_cast<const ValueType*>(decoder->currentScanlineOfBand(3));

                    ImageRowIterator is(image_iterator.rowIterator());
                    const ImageRowIterator is_end(is + width);
                    AlphaRowIterator as(alpha_iterator.rowIterator());

                    while (is != is_end)
                    {
                        image_accessor.setComponent(*scanline_0, is, 0);
                        image_accessor.setComponent(*scanline_1, is, 1);
                        image_accessor.setComponent(*scanline_2, is, 2);
                        alpha_accessor.set(*scanline_3, as);
                        scanline_0 += offset;
                        scanline_1 += offset;
                        scanline_2 += offset;
                        scanline_3 += offset;

                        ++is;
                        ++as;
                    }

                    ++image_iterator.y;
                    ++alpha_iterator.y;
                }
            }
            else
            {
                std::vector<const ValueType*> scanlines(accessor_size + 1U);

                for (unsigned y = 0U; y != height; ++y)
                {
                    decoder->nextScanline();

                    for (unsigned i = 0U; i != accessor_size + 1U; ++i)
                    {
                        scanlines[i] = static_cast<const ValueType*>(decoder->currentScanlineOfBand(i));
                    }

                    ImageRowIterator is(image_iterator.rowIterator());
                    const ImageRowIterator is_end(is + width);
                    AlphaRowIterator as(alpha_iterator.rowIterator());

                    while (is != is_end)
                    {
                        for (unsigned i = 0U; i != accessor_size; ++i)
                        {
                            image_accessor.setComponent(*scanlines[i], is, static_cast<int>(i));
                            scanlines[i] += offset;
                        }
                        ++is;

                        alpha_accessor.set(*scanlines[accessor_size], as);
                        scanlines[accessor_size] += offset;
                        ++as;
                    }

                    ++image_iterator.y;
                    ++alpha_iterator.y;
                }
            }
        }


        template <class ImageIterator, class ImageAccessor,
                  class AlphaIterator, class AlphaAccessor>
        void
        importImageAlpha(const ImageImportInfo& import_info,
                         ImageIterator image_iterator, ImageAccessor image_accessor,
                         AlphaIterator alpha_iterator, AlphaAccessor alpha_accessor,
                         /* isScalar? */ VigraTrueType)
        {
            VIGRA_UNIQUE_PTR<Decoder> decoder(vigra::decoder(import_info));

            switch (pixel_t_of_string(decoder->getPixelType()))
            {
            case UNSIGNED_INT_8:
                read_image_band_and_alpha<UInt8>(decoder.get(),
                                                 image_iterator, image_accessor,
                                                 alpha_iterator, alpha_accessor);
                break;
            case UNSIGNED_INT_16:
                read_image_band_and_alpha<UInt16>(decoder.get(),
                                                  image_iterator, image_accessor,
                                                  alpha_iterator, alpha_accessor);
                break;
            case UNSIGNED_INT_32:
                read_image_band_and_alpha<UInt32>(decoder.get(),
                                                  image_iterator, image_accessor,
                                                  alpha_iterator, alpha_accessor);
                break;
            case SIGNED_INT_16:
                read_image_band_and_alpha<Int16>(decoder.get(),
                                                 image_iterator, image_accessor,
                                                 alpha_iterator, alpha_accessor);
                break;
            case SIGNED_INT_32:
                read_image_band_and_alpha<Int32>(decoder.get(),
                                                 image_iterator, image_accessor,
                                                 alpha_iterator, alpha_accessor);
                break;
            case IEEE_FLOAT_32:
                read_image_band_and_alpha<float>(decoder.get(),
                                                 image_iterator, image_accessor,
                                                 alpha_iterator, alpha_accessor);
                break;
            case IEEE_FLOAT_64:
                read_image_band_and_alpha<double>(decoder.get(),
                                                  image_iterator, image_accessor,
                                                  alpha_iterator, alpha_accessor);
                break;
            default:
                vigra_fail("vigra::detail::importImageAlpha<scalar>: not reached");
            }

            decoder->close();
        }


        template <class ImageIterator, class ImageAccessor,
                  class AlphaIterator, class AlphaAccessor>
        void
        importImageAlpha(const ImageImportInfo& import_info,
                         ImageIterator image_iterator, ImageAccessor image_accessor,
                         AlphaIterator alpha_iterator, AlphaAccessor alpha_accessor,
                         /* isScalar? */ VigraFalseType)
        {
            VIGRA_UNIQUE_PTR<Decoder> decoder(vigra::decoder(import_info));

            switch (pixel_t_of_string(decoder->getPixelType()))
            {
            case UNSIGNED_INT_8:
                read_image_bands_and_alpha<UInt8>(decoder.get(),
                                                  image_iterator, image_accessor,
                                                  alpha_iterator, alpha_accessor);
                break;
            case UNSIGNED_INT_16:
                read_image_bands_and_alpha<UInt16>(decoder.get(),
                                                   image_iterator, image_accessor,
                                                   alpha_iterator, alpha_accessor);
                break;
            case UNSIGNED_INT_32:
                read_image_bands_and_alpha<UInt32>(decoder.get(),
                                                   image_iterator, image_accessor,
                                                   alpha_iterator, alpha_accessor);
                break;
            case SIGNED_INT_16:
                read_image_bands_and_alpha<Int16>(decoder.get(),
                                                  image_iterator, image_accessor,
                                                  alpha_iterator, alpha_accessor);
                break;
            case SIGNED_INT_32:
                read_image_bands_and_alpha<Int32>(decoder.get(),
                                                  image_iterator, image_accessor,
                                                  alpha_iterator, alpha_accessor);
                break;
            case IEEE_FLOAT_32:
                read_image_bands_and_alpha<float>(decoder.get(),
                                                  image_iterator, image_accessor,
                                                  alpha_iterator, alpha_accessor);
                break;
            case IEEE_FLOAT_64:
                read_image_bands_and_alpha<double>(decoder.get(),
                                                   image_iterator, image_accessor,
                                                   alpha_iterator, alpha_accessor);
                break;
            default:
                vigra_fail("vigra::detail::importImageAlpha<non-scalar>: not reached");
            }

            decoder->close();
        }
    } // end namespace detail


    /*!
     * \brief Read the image specified by the given \ref
     * vigra::ImageImportInfo object including its alpha channel.
     *
     * <B>Declarations</B>
     *
     * Pass arguments explicitly:
     * \code
     *     namespace vigra {
     *         template <class ImageIterator, class ImageAccessor,
     *                   class AlphaIterator, class AlphaAccessor>
     *         void
     *         importImageAlpha(const ImageImportInfo& importInfo,
     *                          ImageIterator imageIterator, ImageAccessor imageAccessor,
     *                          AlphaIterator alphaIterator, AlphaAccessor alphaAccessor)
     *     }
     * \endcode
     *
     * Use argument objects in conjunction with \ref ArgumentObjectFactories :
     * \code
     *     namespace vigra {
     *         template <class ImageIterator, class ImageAccessor,
     *                   class AlphaIterator, class AlphaAccessor>
     *         void
     *         importImageAlpha(const ImageImportInfo& importInfo,
     *                          const pair<ImageIterator, ImageAccessor>& image,
     *                          const pair<AlphaIterator, AlphaAccessor>& alpha)
     *     }
     * \endcode
     *
     * <B>Usage</B>
     *
     * <B>\#include \<vigra/impexalpha.hxx\></B>
     *
     * Namespace: vigra
     * \code
     *     typedef UInt8 value_t;
     *     ImageImportInfo info("zorro.tif");
     *
     *     if (info.isGrayscale())
     *     {
     *         BasicImage<value_t> alpha(info.size());
     *         BasicImage<value_t> image(info.size());
     *
     *         importImageAlpha(info,
     *                          image.upperLeft(), image.accessor(),
     *                          alpha.upperLeft(), alpha.accessor());
     *         ...
     *     }
     *     else
     *     {
     *         BasicImage<value_t> alpha(info.size());
     *         BasicImage<vigra::RGBValue<value_t> > image(info.size());
     *
     *         importImageAlpha(info,
     *                          image.upperLeft(), image.accessor(),
     *                          alpha.upperLeft(), alpha.accessor());
     *         ...
     *     }
     * \endcode
     *
     * <B>Preconditions</B>
     *
     * - The same preconditions hold as for importImage(), however the
     *   only image formats that support alpha channels are
     *   + TIFF and
     *   + PNG.
     *   In particular, JPEG does <B>not</B> support alpha channels.
     * - The alpha channel always is scalar-valued, i.e. comprises a
     *   single band.
     */
    doxygen_overloaded_function(template <...> inline void importImageAlpha)


    template <class ImageIterator, class ImageAccessor,
              class AlphaIterator, class AlphaAccessor>
    inline void
    importImageAlpha(const ImageImportInfo& import_info,
                     ImageIterator image_iterator, ImageAccessor image_accessor,
                     AlphaIterator alpha_iterator, AlphaAccessor alpha_accessor)
    {
        typedef typename ImageAccessor::value_type ImageValueType;
        typedef typename vigra::NumericTraits<ImageValueType>::isScalar is_scalar;

        detail::importImageAlpha(import_info,
                                 image_iterator, image_accessor,
                                 alpha_iterator, alpha_accessor,
                                 is_scalar());
    }


    template <class ImageIterator, class ImageAccessor,
              class AlphaIterator, class AlphaAccessor>
    inline void
    importImageAlpha(const ImageImportInfo& import_info,
                     const vigra::pair<ImageIterator, ImageAccessor>& image,
                     const vigra::pair<AlphaIterator, AlphaAccessor>& alpha)
    {
        importImageAlpha(import_info,
                         image.first, image.second,
                         alpha.first, alpha.second);
    }


    namespace detail
    {
        template<class ValueType,
                 class ImageIterator, class ImageAccessor, class ImageScaler,
                 class AlphaIterator, class AlphaAccessor, class AlphaScaler>
        void
        write_image_band_and_alpha(Encoder* encoder,
                                   ImageIterator image_upper_left, ImageIterator image_lower_right, ImageAccessor image_accessor,
                                   const ImageScaler& image_scaler,
                                   AlphaIterator alpha_upper_left, AlphaAccessor alpha_accessor,
                                   const AlphaScaler& alpha_scaler)
        {
            typedef typename ImageIterator::row_iterator ImageRowIterator;
            typedef typename ImageAccessor::value_type ImageValueType;

            typedef typename AlphaIterator::row_iterator AlphaRowIterator;
            typedef typename AlphaAccessor::value_type AlphaValueType;

            typedef detail::RequiresExplicitCast<ValueType> explicit_cast;

            vigra_precondition(image_lower_right.x >= image_upper_left.x,
                               "vigra::detail::write_image_band_and_alpha: negative width");
            vigra_precondition(image_lower_right.y >= image_upper_left.y,
                               "vigra::detail::write_image_band_and_alpha: negative height");

            const unsigned width(static_cast<unsigned>(image_lower_right.x - image_upper_left.x));
            const unsigned height(static_cast<unsigned>(image_lower_right.y - image_upper_left.y));

            encoder->setWidth(width);
            encoder->setHeight(height);
            encoder->setNumBands(1 + 1);
            encoder->finalizeSettings();

            const unsigned offset(encoder->getOffset()); // correct offset only _after_ finalizeSettings()

            // IMPLEMENTATION NOTE: We avoid calling the default constructor
            // to allow classes ImageIterator and AlphaIterator that do not
            // define one.
            ImageIterator image_iterator(image_upper_left);
            AlphaIterator alpha_iterator(alpha_upper_left);

            for (unsigned y = 0U; y != height; ++y)
            {
                ValueType* scanline0 = static_cast<ValueType*>(encoder->currentScanlineOfBand(0));
                ValueType* scanline1 = static_cast<ValueType*>(encoder->currentScanlineOfBand(1));

                ImageRowIterator is(image_iterator.rowIterator());
                const ImageRowIterator is_end(is + width);
                AlphaRowIterator as(alpha_iterator.rowIterator());

                while (is != is_end)
                {
                    *scanline0 = explicit_cast::cast(image_scaler(image_accessor(is)));
                    scanline0 += offset;
                    ++is;

                    *scanline1 = explicit_cast::cast(alpha_scaler(alpha_accessor(as)));
                    scanline1 += offset;
                    ++as;
                }

                encoder->nextScanline();

                ++image_iterator.y;
                ++alpha_iterator.y;
            }
        }


        template<class ValueType,
                 class ImageIterator, class ImageAccessor, class ImageScaler,
                 class AlphaIterator, class AlphaAccessor, class AlphaScaler>
        void
        write_image_bands_and_alpha(Encoder* encoder,
                                    ImageIterator image_upper_left, ImageIterator image_lower_right, ImageAccessor image_accessor,
                                    const ImageScaler& image_scaler,
                                    AlphaIterator alpha_upper_left, AlphaAccessor alpha_accessor,
                                    const AlphaScaler& alpha_scaler)
        {
            typedef typename ImageIterator::row_iterator ImageRowIterator;
            typedef typename AlphaIterator::row_iterator AlphaRowIterator;
            typedef detail::RequiresExplicitCast<ValueType> explicit_cast;

            vigra_precondition(image_lower_right.x >= image_upper_left.x,
                               "vigra::detail::write_image_bands_and_alpha: negative width");
            vigra_precondition(image_lower_right.y >= image_upper_left.y,
                               "vigra::detail::write_image_bands_and_alpha: negative height");

            const unsigned width(static_cast<unsigned>(image_lower_right.x - image_upper_left.x));
            const unsigned height(static_cast<unsigned>(image_lower_right.y - image_upper_left.y));
            const unsigned accessor_size(image_accessor.size(image_upper_left));

            encoder->setWidth(width);
            encoder->setHeight(height);
            encoder->setNumBands(accessor_size + 1U);
            encoder->finalizeSettings();

            const unsigned offset(encoder->getOffset()); // correct offset only _after_ finalizeSettings()

            // IMPLEMENTATION NOTE: We avoid calling the default constructor
            // to allow classes ImageIterator and AlphaIterator that do not
            // define one.
            ImageIterator image_iterator(image_upper_left);
            AlphaIterator alpha_iterator(alpha_upper_left);

            // OPTIMIZATION: Specialization for the most common case
            // of an RGBA-image, i.e. three color channels plus one
            // alpha channel.
            if (accessor_size == 3U)
            {
                ValueType* scanline_0;
                ValueType* scanline_1;
                ValueType* scanline_2;
                ValueType* scanline_3; // alpha

                for (unsigned y = 0U; y != height; ++y)
                {
                    scanline_0 = static_cast<ValueType*>(encoder->currentScanlineOfBand(0));
                    scanline_1 = static_cast<ValueType*>(encoder->currentScanlineOfBand(1));
                    scanline_2 = static_cast<ValueType*>(encoder->currentScanlineOfBand(2));
                    scanline_3 = static_cast<ValueType*>(encoder->currentScanlineOfBand(3));

                    ImageRowIterator is(image_iterator.rowIterator());
                    const ImageRowIterator is_end(is + width);
                    AlphaRowIterator as(alpha_iterator.rowIterator());

                    while (is != is_end)
                    {
                        *scanline_0 = explicit_cast::cast(image_scaler(image_accessor.getComponent(is, 0)));
                        *scanline_1 = explicit_cast::cast(image_scaler(image_accessor.getComponent(is, 1)));
                        *scanline_2 = explicit_cast::cast(image_scaler(image_accessor.getComponent(is, 2)));
                        *scanline_3 = explicit_cast::cast(alpha_scaler(alpha_accessor(as)));
                        scanline_0 += offset;
                        scanline_1 += offset;
                        scanline_2 += offset;
                        scanline_3 += offset;

                        ++is;
                        ++as;
                    }

                    encoder->nextScanline();

                    ++image_iterator.y;
                    ++alpha_iterator.y;
                }
            }
            else
            {
                std::vector<ValueType*> scanlines(accessor_size + 1U);

                for (unsigned y = 0U; y != height; ++y)
                {
                    for (unsigned i = 0U; i != accessor_size + 1U; ++i)
                    {
                        scanlines[i] = static_cast<ValueType*>(encoder->currentScanlineOfBand(i));
                    }

                    ImageRowIterator is(image_iterator.rowIterator());
                    const ImageRowIterator is_end(is + width);
                    AlphaRowIterator as(alpha_iterator.rowIterator());

                    while (is != is_end)
                    {
                        for (unsigned i = 0U; i != accessor_size; ++i)
                        {
                            *scanlines[i] = explicit_cast::cast(image_scaler(image_accessor.getComponent(is, static_cast<int>(i))));
                            scanlines[i] += offset;
                        }
                        ++is;

                        *scanlines[accessor_size] = explicit_cast::cast(alpha_scaler(alpha_accessor(as)));
                        scanlines[accessor_size] += offset;
                        ++as;
                    }

                    encoder->nextScanline();

                    ++image_iterator.y;
                    ++alpha_iterator.y;
                }
            }
        }


        template <class ImageIterator, class ImageAccessor,
                  class AlphaIterator, class AlphaAccessor>
        void
        exportImageAlpha(ImageIterator image_upper_left, ImageIterator image_lower_right, ImageAccessor image_accessor,
                         AlphaIterator alpha_upper_left, AlphaAccessor alpha_accessor,
                         const ImageExportInfo& export_info,
                         /* isScalar? */ VigraTrueType)
        {
            typedef typename ImageAccessor::value_type ImageValueType;

            VIGRA_UNIQUE_PTR<Encoder> encoder(vigra::encoder(export_info));

            std::string pixel_type(export_info.getPixelType());
            const bool downcast(negotiatePixelType(encoder->getFileType(), TypeAsString<ImageValueType>::result(), pixel_type));
            const pixel_t type(pixel_t_of_string(pixel_type));

            encoder->setPixelType(pixel_type);

            const range_t image_source_range(find_source_value_range(export_info,
                                                                     image_upper_left, image_lower_right, image_accessor));
            const range_t alpha_source_range(find_source_value_range(export_info,
                                                                     alpha_upper_left,
                                                                     alpha_upper_left + (image_lower_right - image_upper_left),
                                                                     alpha_accessor));
            const range_t destination_range(find_destination_value_range(export_info, type));

            if ((downcast || export_info.hasForcedRangeMapping()) &&
                (image_source_range.first != destination_range.first || image_source_range.second != destination_range.second ||
                 alpha_source_range.first != destination_range.first || alpha_source_range.second != destination_range.second))
            {
                const linear_transform image_rescaler(image_source_range, destination_range);
                const linear_transform alpha_rescaler(alpha_source_range, destination_range);

                switch (type)
                {
                case UNSIGNED_INT_8:
                    write_image_band_and_alpha<UInt8>(encoder.get(),
                                                      image_upper_left, image_lower_right, image_accessor, image_rescaler,
                                                      alpha_upper_left, alpha_accessor, alpha_rescaler);
                    break;
                case UNSIGNED_INT_16:
                    write_image_band_and_alpha<UInt16>(encoder.get(),
                                                       image_upper_left, image_lower_right, image_accessor, image_rescaler,
                                                       alpha_upper_left, alpha_accessor, alpha_rescaler);
                    break;
                case UNSIGNED_INT_32:
                    write_image_band_and_alpha<UInt32>(encoder.get(),
                                                       image_upper_left, image_lower_right, image_accessor, image_rescaler,
                                                       alpha_upper_left, alpha_accessor, alpha_rescaler);
                    break;
                case SIGNED_INT_16:
                    write_image_band_and_alpha<Int16>(encoder.get(),
                                                      image_upper_left, image_lower_right, image_accessor, image_rescaler,
                                                      alpha_upper_left, alpha_accessor, alpha_rescaler);
                    break;
                case SIGNED_INT_32:
                    write_image_band_and_alpha<Int32>(encoder.get(),
                                                      image_upper_left, image_lower_right, image_accessor, image_rescaler,
                                                      alpha_upper_left, alpha_accessor, alpha_rescaler);
                    break;
                case IEEE_FLOAT_32:
                    write_image_band_and_alpha<float>(encoder.get(),
                                                      image_upper_left, image_lower_right, image_accessor, image_rescaler,
                                                      alpha_upper_left, alpha_accessor, alpha_rescaler);
                    break;
                case IEEE_FLOAT_64:
                    write_image_band_and_alpha<double>(encoder.get(),
                                                       image_upper_left, image_lower_right, image_accessor, image_rescaler,
                                                       alpha_upper_left, alpha_accessor, alpha_rescaler);
                    break;
                default:
                    vigra_fail("vigra::detail::exportImageAlpha<scalar>: not reached");
                }
            }
            else
            {
                switch (type)
                {
                case UNSIGNED_INT_8:
                    write_image_band_and_alpha<UInt8>(encoder.get(),
                                                      image_upper_left, image_lower_right, image_accessor, identity(),
                                                      alpha_upper_left, alpha_accessor, identity());
                    break;
                case UNSIGNED_INT_16:
                    write_image_band_and_alpha<UInt16>(encoder.get(),
                                                       image_upper_left, image_lower_right, image_accessor, identity(),
                                                       alpha_upper_left, alpha_accessor, identity());
                    break;
                case UNSIGNED_INT_32:
                    write_image_band_and_alpha<UInt32>(encoder.get(),
                                                       image_upper_left, image_lower_right, image_accessor, identity(),
                                                       alpha_upper_left, alpha_accessor, identity());
                    break;
                case SIGNED_INT_16:
                    write_image_band_and_alpha<Int16>(encoder.get(),
                                                      image_upper_left, image_lower_right, image_accessor, identity(),
                                                      alpha_upper_left, alpha_accessor, identity());
                    break;
                case SIGNED_INT_32:
                    write_image_band_and_alpha<Int32>(encoder.get(),
                                                      image_upper_left, image_lower_right, image_accessor, identity(),
                                                      alpha_upper_left, alpha_accessor, identity());
                    break;
                case IEEE_FLOAT_32:
                    write_image_band_and_alpha<float>(encoder.get(),
                                                      image_upper_left, image_lower_right, image_accessor, identity(),
                                                      alpha_upper_left, alpha_accessor, identity());
                    break;
                case IEEE_FLOAT_64:
                    write_image_band_and_alpha<double>(encoder.get(),
                                                       image_upper_left, image_lower_right, image_accessor, identity(),
                                                       alpha_upper_left, alpha_accessor, identity());
                    break;
                default:
                    vigra_fail("vigra::detail::exportImageAlpha<scalar>: not reached");
                }
            }

            encoder->close();
        }


        template <class ImageIterator, class ImageAccessor,
                  class AlphaIterator, class AlphaAccessor>
        void
        exportImageAlpha(ImageIterator image_upper_left, ImageIterator image_lower_right, ImageAccessor image_accessor,
                         AlphaIterator alpha_upper_left, AlphaAccessor alpha_accessor,
                         const ImageExportInfo& export_info,
                         /* isScalar? */ VigraFalseType)
        {
            typedef typename ImageAccessor::value_type ImageBaseType;
            typedef typename ImageBaseType::value_type ImageValueType;

            VIGRA_UNIQUE_PTR<Encoder> encoder(vigra::encoder(export_info));

            std::string pixel_type(export_info.getPixelType());
            const bool downcast(negotiatePixelType(encoder->getFileType(), TypeAsString<ImageValueType>::result(), pixel_type));
            const pixel_t type(pixel_t_of_string(pixel_type));

            encoder->setPixelType(pixel_type);

            vigra_precondition(isBandNumberSupported(encoder->getFileType(), image_accessor.size(image_upper_left)),
                               "exportImageAlpha(): file format does not support requested number of bands (color channels)");

            const range_t image_source_range(find_source_value_range(export_info,
                                                                     image_upper_left, image_lower_right, image_accessor));
            const range_t alpha_source_range(find_source_value_range(export_info,
                                                                     alpha_upper_left,
                                                                     alpha_upper_left + (image_lower_right - image_upper_left),
                                                                     alpha_accessor));
            const range_t destination_range(find_destination_value_range(export_info, pixel_t_of_string(pixel_type)));

            if ((downcast || export_info.hasForcedRangeMapping()) &&
                (image_source_range.first != destination_range.first || image_source_range.second != destination_range.second ||
                 alpha_source_range.first != destination_range.first || alpha_source_range.second != destination_range.second))
            {
                const linear_transform image_rescaler(image_source_range, destination_range);
                const linear_transform alpha_rescaler(alpha_source_range, destination_range);

                switch (type)
                {
                case UNSIGNED_INT_8:
                    write_image_bands_and_alpha<UInt8>(encoder.get(),
                                                       image_upper_left, image_lower_right, image_accessor, image_rescaler,
                                                       alpha_upper_left, alpha_accessor, alpha_rescaler);
                    break;
                case UNSIGNED_INT_16:
                    write_image_bands_and_alpha<UInt16>(encoder.get(),
                                                        image_upper_left, image_lower_right, image_accessor, image_rescaler,
                                                        alpha_upper_left, alpha_accessor, alpha_rescaler);
                    break;
                case UNSIGNED_INT_32:
                    write_image_bands_and_alpha<UInt32>(encoder.get(),
                                                        image_upper_left, image_lower_right, image_accessor, image_rescaler,
                                                        alpha_upper_left, alpha_accessor, alpha_rescaler);
                    break;
                case SIGNED_INT_16:
                    write_image_bands_and_alpha<Int16>(encoder.get(),
                                                       image_upper_left, image_lower_right, image_accessor, image_rescaler,
                                                       alpha_upper_left, alpha_accessor, alpha_rescaler);
                    break;
                case SIGNED_INT_32:
                    write_image_bands_and_alpha<Int32>(encoder.get(),
                                                       image_upper_left, image_lower_right, image_accessor, image_rescaler,
                                                       alpha_upper_left, alpha_accessor, alpha_rescaler);
                    break;
                case IEEE_FLOAT_32:
                    write_image_bands_and_alpha<float>(encoder.get(),
                                                       image_upper_left, image_lower_right, image_accessor, image_rescaler,
                                                       alpha_upper_left, alpha_accessor, alpha_rescaler);
                    break;
                case IEEE_FLOAT_64:
                    write_image_bands_and_alpha<double>(encoder.get(),
                                                        image_upper_left, image_lower_right, image_accessor, image_rescaler,
                                                        alpha_upper_left, alpha_accessor, alpha_rescaler);
                    break;
                default:
                    vigra_fail("vigra::detail::exportImageAlpha<non-scalar>: not reached");
                }
            }
            else
            {
                switch (type)
                {
                case UNSIGNED_INT_8:
                    write_image_bands_and_alpha<UInt8>(encoder.get(),
                                                       image_upper_left, image_lower_right, image_accessor, identity(),
                                                       alpha_upper_left, alpha_accessor, identity());
                    break;
                case UNSIGNED_INT_16:
                    write_image_bands_and_alpha<UInt16>(encoder.get(),
                                                        image_upper_left, image_lower_right, image_accessor, identity(),
                                                        alpha_upper_left, alpha_accessor, identity());
                    break;
                case UNSIGNED_INT_32:
                    write_image_bands_and_alpha<UInt32>(encoder.get(),
                                                        image_upper_left, image_lower_right, image_accessor, identity(),
                                                        alpha_upper_left, alpha_accessor, identity());
                    break;
                case SIGNED_INT_16:
                    write_image_bands_and_alpha<Int16>(encoder.get(),
                                                       image_upper_left, image_lower_right, image_accessor, identity(),
                                                       alpha_upper_left, alpha_accessor, identity());
                    break;
                case SIGNED_INT_32:
                    write_image_bands_and_alpha<Int32>(encoder.get(),
                                                       image_upper_left, image_lower_right, image_accessor, identity(),
                                                       alpha_upper_left, alpha_accessor, identity());
                    break;
                case IEEE_FLOAT_32:
                    write_image_bands_and_alpha<float>(encoder.get(),
                                                       image_upper_left, image_lower_right, image_accessor, identity(),
                                                       alpha_upper_left, alpha_accessor, identity());
                    break;
                case IEEE_FLOAT_64:
                    write_image_bands_and_alpha<double>(encoder.get(),
                                                        image_upper_left, image_lower_right, image_accessor, identity(),
                                                        alpha_upper_left, alpha_accessor, identity());
                    break;
                default:
                    vigra_fail("vigra::detail::exportImageAlpha<non-scalar>: not reached");
                }
            }

            encoder->close();
        }
    } // end namespace detail


    /*!
     * \brief Write the image specified by the given \ref
     * vigra::ImageExportInfo object including an alpha channel.
     *
     * <B>Declarations</B>
     *
     * Pass arguments explicitly:
     * \code
     *     namespace vigra {
     *         template <class ImageIterator, class ImageAccessor,
     *                   class AlphaIterator, class AlphaAccessor>
     *         void
     *         exportImageAlpha(ImageIterator imageUpperLeft, ImageIterator imageLowerRight, ImageAccessor imageAccessor,
     *                          AlphaIterator alphaUpperLeft, AlphaAccessor alphaAccessor,
     *                          const ImageExportInfo& exportInfo)
     *     }
     * \endcode
     *
     * Use argument objects in conjunction with \ref ArgumentObjectFactories :
     * \code
     *     namespace vigra {
     *     template <class ImageIterator, class ImageAccessor,
     *               class AlphaIterator, class AlphaAccessor>
     *     void
     *     exportImageAlpha(const triple<ImageIterator, ImageIterator, ImageAccessor>& image,
     *                      const pair<AlphaIterator, AlphaAccessor>& alpha,
     *                      const ImageExportInfo& exportInfo)
     *     }
     * \endcode
     *
     * <B>Usage</B>
     *
     * <B>\#include \<vigra/impexalpha.hxx\></B>
     *
     * Namespace: vigra
     * \code
     *     typedef UInt8 value_t;
     *     ImageExportInfo info("zorro.tif");
     *
     *     if (info.isGrayscale())
     *     {
     *         BasicImage<value_t> alpha;
     *         BasicImage<value_t> image;
     *
     *         ...
     *
     *         exportImageAlpha(image.upperLeft(), image.lowerRight(), image.accessor(),
     *                          alpha.upperLeft(), alpha.accessor(),
     *                          info);
     *     }
     *     else
     *     {
     *         BasicImage<value_t> alpha;
     *         BasicImage<vigra::RGBValue<value_t> > image;
     *
     *         ...
     *
     *         exportImageAlpha(image.upperLeft(), image.lowerRight(), image.accessor(),
     *                          alpha.upperLeft(), alpha.accessor(),
     *                          info);
     *     }
     * \endcode
     *
     * <B>Preconditions</B>
     *
     * - The same preconditions hold as for exportImage(), however the
     *   only image formats that support alpha channels are
     *   + TIFF and
     *   + PNG.
     *   In particular, JPEG does <B>not</B> support alpha channels.
     * - The alpha channel always is scalar-valued, i.e. comprises a
     *   single band.
     */
    doxygen_overloaded_function(template <...> inline void exportImageAlpha)


    template <class ImageIterator, class ImageAccessor,
              class AlphaIterator, class AlphaAccessor>
    inline void
    exportImageAlpha(ImageIterator image_upper_left, ImageIterator image_lower_right, ImageAccessor image_accessor,
                     AlphaIterator alpha_upper_left, AlphaAccessor alpha_accessor,
                     const ImageExportInfo& export_info)
    {
        typedef typename ImageAccessor::value_type ImageValueType;
        typedef typename vigra::NumericTraits<ImageValueType>::isScalar is_scalar;

        try
        {
            detail::exportImageAlpha(image_upper_left, image_lower_right, image_accessor,
                                     alpha_upper_left, alpha_accessor,
                                     export_info,
                                     is_scalar());
        }
        catch (Encoder::TIFFCompressionException&)
        {
            ImageExportInfo info(export_info);

            info.setCompression("");
            detail::exportImageAlpha(image_upper_left, image_lower_right, image_accessor,
                                     alpha_upper_left, alpha_accessor,
                                     info,
                                     is_scalar());
        }
    }


    template <class ImageIterator, class ImageAccessor,
              class AlphaIterator, class AlphaAccessor>
    inline void
    exportImageAlpha(const vigra::triple<ImageIterator, ImageIterator, ImageAccessor>& image,
                     const vigra::pair<AlphaIterator, AlphaAccessor>& alpha,
                     const ImageExportInfo& export_info)
    {
        exportImageAlpha(image.first, image.second, image.third,
                         alpha.first, alpha.second,
                         export_info);
    }

/** @} */
    
} // end namespace vigra

#endif // VIGRA_IMPEXALPHA_HXX