[ VIGRA Homepage | Class Index | Function Index | File Index | Main Page ]

| vigra/impex.hxx |
--- | --- | ---

Go to the documentation of this file.

    00001 /************************************************************************/
    00002 /*                                                                      */
    00003 /*               Copyright 2001-2002 by Gunnar Kedenburg                */
    00004 /*       Cognitive Systems Group, University of Hamburg, Germany        */
    00005 /*                                                                      */
    00006 /*    This file is part of the VIGRA computer vision library.           */
    00007 /*    ( Version 1.3.2, Jan 27 2005 )                                    */
    00008 /*    You may use, modify, and distribute this software according       */
    00009 /*    to the terms stated in the LICENSE file included in               */
    00010 /*    the VIGRA distribution.                                           */
    00011 /*                                                                      */
    00012 /*    The VIGRA Website is                                              */
    00013 /*        http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/      */
    00014 /*    Please direct questions, bug reports, and contributions to        */
    00015 /*        koethe@informatik.uni-hamburg.de                              */
    00016 /*                                                                      */
    00017 /*  THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY EXPRESS OR          */
    00018 /*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED      */
    00019 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
    00020 /*                                                                      */
    00021 /************************************************************************/
    00022
    00023 /*!
    00024   \file  impex.hxx
    00025   \brief image import and export functions
    00026
    00027   this file provides the declarations and implementations of importImage()
    00028   and exportImage(). the matching implementation for the given datatype is
    00029   selected by template metacode.
    00030 */
    00031
    00032 #ifndef VIGRA_IMPEX_HXX
    00033 #define VIGRA_IMPEX_HXX
    00034
    00035 #if defined(_MSC_VER)
    00036 #pragma warning (disable: 4267)
    00037 #endif
    00038
    00039 #include "vigra/stdimage.hxx"
    00040 #include "vigra/tinyvector.hxx"
    00041 #include "vigra/imageinfo.hxx"
    00042 #include "vigra/numerictraits.hxx"
    00043 #include "vigra/codec.hxx"
    00044 #include "vigra/accessor.hxx"
    00045 #include "vigra/inspectimage.hxx"
    00046 #include "vigra/transformimage.hxx"
    00047 #include "vigra/copyimage.hxx"
    00048 #include "vigra/multi_array.hxx"
    00049
    00050 // TODO
    00051 // next refactoring: pluggable conversion algorithms
    00052
    00053 namespace vigra
    00054 {
    00055 /** \addtogroup VigraImpex
    00056 **/
    00057 //@{
    00058
    00059     /*!
    00060       \brief used for reading bands after the source data type has been figured out.
    00061
    00062         <b>\#include</b> "<a href="impex_8hxx-source.html">vigra/impex.hxx</a>"<br>
    00063         Namespace: vigra
    00064
    00065         <b> Declaration:</b>
    00066
    00067         \code
    00068         namespace vigra {
    00069             template< class ImageIterator, class Accessor, class SrcValueType >
    00070             void read_bands( Decoder * dec, ImageIterator ys, Accessor a, SrcValueType )
    00071         }
    00072         \endcode
    00073
    00074       \param dec decoder object through which the source data will be accessed
    00075       \param ys  image iterator referencing the upper left pixel of the destination image
    00076       \param a   image accessor for the destination image
    00077     */
    00078     template< class ImageIterator, class Accessor, class SrcValueType >
    00079     void read_bands( Decoder * dec, ImageIterator ys, Accessor a, SrcValueType )
    00080     {
    00081         typedef unsigned int size_type;
    00082         typedef typename ImageIterator::row_iterator DstRowIterator;
    00083         typedef typename Accessor::value_type  AccessorValueType;
    00084         typedef typename AccessorValueType::value_type DstValueType;
    00085
    00086         const size_type width = dec->getWidth();
    00087         const size_type height = dec->getHeight();
    00088         const size_type num_bands = dec->getNumBands();
    00089
    00090         vigra_precondition(num_bands == a.size(ys),
    00091            "importImage(): number of bands (color channels) in file and destination image differ.");
    00092
    00093         SrcValueType const * scanline;
    00094         DstRowIterator xs;
    00095
    00096         // iterate
    00097         for( size_type y = 0; y < height; ++y, ++ys.y ) {
    00098             dec->nextScanline();
    00099             for( size_type b = 0; b < num_bands; ++b ) {
    00100                 xs = ys.rowIterator();
    00101                 scanline = static_cast< SrcValueType const * >
    00102                     (dec->currentScanlineOfBand(b));
    00103                 for( size_type x = 0; x < width; ++x, ++xs ) {
    00104                     a.setComponent( *scanline, xs, b );
    00105                     scanline += dec->getOffset();
    00106                 }
    00107             }
    00108         }
    00109     } // read_bands()
    00110
    00111     /*!
    00112       \brief used for reading bands after the source data type has been figured out.
    00113
    00114         <b>\#include</b> "<a href="impex_8hxx-source.html">vigra/impex.hxx</a>"<br>
    00115         Namespace: vigra
    00116
    00117         <b> Declaration:</b>
    00118
    00119         \code
    00120         namespace vigra {
    00121             template< class ImageIterator, class Accessor, class SrcValueType >
    00122             void read_band( Decoder * dec, ImageIterator ys, Accessor a, SrcValueType )
    00123         }
    00124         \endcode
    00125
    00126       \param dec decoder object through which the source data will be accessed
    00127       \param ys  image iterator referencing the upper left pixel of the destination image
    00128       \param a   image accessor for the destination image
    00129     */
    00130     template< class ImageIterator, class Accessor, class SrcValueType >
    00131     void read_band( Decoder * dec, ImageIterator ys, Accessor a, SrcValueType )
    00132     {
    00133         typedef unsigned int size_type;
    00134         typedef typename ImageIterator::row_iterator DstRowIterator;
    00135         typedef typename Accessor::value_type DstValueType;
    00136         const size_type width = dec->getWidth();
    00137         const size_type height = dec->getHeight();
    00138
    00139         SrcValueType const * scanline;
    00140         DstRowIterator xs;
    00141
    00142         for( size_type y = 0; y < height; ++y, ++ys.y ) {
    00143             dec->nextScanline();
    00144             xs = ys.rowIterator();
    00145             scanline = static_cast< SrcValueType const * >(dec->currentScanlineOfBand(0));
    00146             for( size_type x = 0; x < width; ++x, ++xs )
    00147                 a.set( scanline[x], xs );
    00148         }
    00149     } // read_band()
    00150
    00151     /*!
    00152       \brief used for reading images of vector type, such as integer of float rgb.
    00153
    00154         <b>\#include</b> "<a href="impex_8hxx-source.html">vigra/impex.hxx</a>"<br>
    00155         Namespace: vigra
    00156
    00157         <b> Declaration:</b>
    00158
    00159         \code
    00160         namespace vigra {
    00161             template< class ImageIterator, class Accessor >
    00162             void importVectorImage( const ImageImportInfo & info, ImageIterator iter, Accessor a )
    00163         }
    00164         \endcode
    00165
    00166       \param ImageIterator the image iterator type for the destination image
    00167       \param Accessor      the image accessor type for the destination image
    00168       \param info          user supplied image import information
    00169       \param iter          image iterator referencing the upper left pixel of the destination image
    00170       \param a             image accessor for the destination image
    00171     */
    00172     template< class ImageIterator, class Accessor >
    00173     void importVectorImage( const ImageImportInfo & info, ImageIterator iter, Accessor a )
    00174     {
    00175         std::auto_ptr<Decoder> dec = decoder(info);
    00176         std::string pixeltype = dec->getPixelType();
    00177
    00178         if ( pixeltype == "UINT8" )
    00179             read_bands( dec.get(), iter, a, (unsigned char)0 );
    00180         else if ( pixeltype == "INT16" )
    00181             read_bands( dec.get(), iter, a, short() );
    00182         else if ( pixeltype == "INT32" )
    00183             read_bands( dec.get(), iter, a, int() );
    00184         else if ( pixeltype == "FLOAT" )
    00185             read_bands( dec.get(), iter, a, float() );
    00186         else if ( pixeltype == "DOUBLE" )
    00187             read_bands( dec.get(), iter, a, double() );
    00188         else
    00189             vigra_precondition( false, "invalid pixeltype" );
    00190
    00191         // close the decoder
    00192         dec->close();
    00193     }
    00194
    00195     /*!
    00196       \brief used for reading images of  scalar type, such as integer and float grayscale.
    00197
    00198         <b>\#include</b> "<a href="impex_8hxx-source.html">vigra/impex.hxx</a>"<br>
    00199         Namespace: vigra
    00200
    00201         <b> Declaration:</b>
    00202
    00203         \code
    00204         namespace vigra {
    00205             template < class ImageIterator, class Accessor >
    00206             void importScalarImage( const ImageImportInfo & info, ImageIterator iter, Accessor a )
    00207         }
    00208         \endcode
    00209
    00210       \param ImageIterator the image iterator type for the destination image
    00211       \param Accessor      the image accessor type for the destination image
    00212       \param info          user supplied image import information
    00213       \param iter          image iterator referencing the upper left pixel of the destination image
    00214       \param a             image accessor for the destination image
    00215     */
    00216     template < class ImageIterator, class Accessor >
    00217     void importScalarImage( const ImageImportInfo & info, ImageIterator iter, Accessor a )
    00218     {
    00219         std::auto_ptr<Decoder> dec = decoder(info);
    00220         std::string pixeltype = dec->getPixelType();
    00221
    00222         if ( pixeltype == "UINT8" )
    00223             read_band( dec.get(), iter, a, (unsigned char)0 );
    00224         else if ( pixeltype == "INT16" )
    00225             read_band( dec.get(), iter, a, short() );
    00226         else if ( pixeltype == "INT32" )
    00227             read_band( dec.get(), iter, a, int() );
    00228         else if ( pixeltype == "FLOAT" )
    00229             read_band( dec.get(), iter, a, float() );
    00230         else if ( pixeltype == "DOUBLE" )
    00231             read_band( dec.get(), iter, a, double() );
    00232         else
    00233             vigra_precondition( false, "invalid pixeltype" );
    00234
    00235         // close the decoder
    00236         dec->close();
    00237     }
    00238
    00239     template < class ImageIterator, class Accessor >
    00240     void importImage( const ImageImportInfo & info, ImageIterator iter, Accessor a, VigraFalseType )
    00241     {
    00242         importVectorImage( info, iter, a );
    00243     }
    00244
    00245     template < class ImageIterator, class Accessor >
    00246     void importImage( const ImageImportInfo & info, ImageIterator iter, Accessor a, VigraTrueType )
    00247     {
    00248         importScalarImage( info, iter, a );
    00249     }
    00250
    00251 /********************************************************/
    00252 /*                                                      */
    00253 /*                     importImage                      */
    00254 /*                                                      */
    00255 /********************************************************/
    00256
    00257     /** \brief Read an image, given an \ref vigra::ImageImportInfo object.
    00258
    00259         <b> Declarations:</b>
    00260
    00261         pass arguments explicitly:
    00262         \code
    00263         namespace vigra {
    00264             template <class ImageIterator, class Accessor>
    00265             void
    00266             importImage(ImageImportInfo const & image, ImageIterator iter, Accessor a)
    00267         }
    00268         \endcode
    00269
    00270         use argument objects in conjunction with \ref ArgumentObjectFactories:
    00271         \code
    00272         namespace vigra {
    00273             template <class ImageIterator, class Accessor>
    00274             inline void
    00275             importImage(ImageImportInfo const & image, pair<ImageIterator, Accessor> dest)
    00276         }
    00277         \endcode
    00278
    00279         <b> Usage:</b>
    00280
    00281         <b>\#include</b> "<a href="impex_8hxx-source.html">vigra/impex.hxx</a>"<br>
    00282         Namespace: vigra
    00283
    00284         \code
    00285
    00286         vigra::ImageImportInfo info("myimage.gif");
    00287
    00288         if(info.isGrayscale())
    00289         {
    00290             // create byte image of appropriate size
    00291             vigra::BImage in(info.width(), info.height());
    00292
    00293             vigra::importImage(info, destImage(in)); // read the image
    00294             ...
    00295         }
    00296         else
    00297         {
    00298             // create byte RGB image of appropriate size
    00299             vigra::BRGBImage in(info.width(), info.height());
    00300
    00301             vigra::importImage(info, destImage(in)); // read the image
    00302             ...
    00303         }
    00304
    00305         \endcode
    00306
    00307         <b> Preconditions:</b>
    00308
    00309         <UL>
    00310
    00311         <LI> the image file must be readable
    00312         <LI> the file type must be one of
    00313
    00314                 <DL>
    00315                 <DT>"BMP"<DD> Microsoft Windows bitmap image file.
    00316                 <DT>"GIF"<DD> CompuServe graphics interchange format; 8-bit color.
    00317                 <DT>"JPEG"<DD> Joint Photographic Experts Group JFIF format; compressed 24-bit color. (only available if libjpeg is installed)
    00318                 <DT>"PNG"<DD> Portable Network Graphic. (only available if libpng is installed)
    00319                 <DT>"PBM"<DD> Portable bitmap format (black and white).
    00320                 <DT>"PGM"<DD> Portable graymap format (gray scale).
    00321                 <DT>"PNM"<DD> Portable anymap.
    00322                 <DT>"PPM"<DD> Portable pixmap format (color).
    00323                 <DT>"SUN"<DD> SUN Rasterfile.
    00324                 <DT>"TIFF"<DD> Tagged Image File Format. (only available if libtiff is installed.)
    00325                 <DT>"VIFF"<DD> Khoros Visualization image file.
    00326                 </DL>
    00327         </UL>
    00328     **/
    00329     template < class ImageIterator, class Accessor >
    00330     void importImage( const ImageImportInfo & info, ImageIterator iter, Accessor a )
    00331     {
    00332         typedef typename NumericTraits<typename Accessor::value_type>::isScalar is_scalar;
    00333         importImage( info, iter, a, is_scalar() );
    00334     }
    00335
    00336     template < class ImageIterator, class Accessor >
    00337     void importImage( const ImageImportInfo & info, pair< ImageIterator, Accessor > dest )
    00338     {
    00339         importImage( info, dest.first, dest.second );
    00340     }
    00341
    00342     /*!
    00343       \brief used for writing bands after the source data type has been figured out.
    00344
    00345         <b>\#include</b> "<a href="impex_8hxx-source.html">vigra/impex.hxx</a>"<br>
    00346         Namespace: vigra
    00347
    00348         <b> Declaration:</b>
    00349
    00350         \code
    00351         namespace vigra {
    00352             template< class ImageIterator, class Accessor, class DstValueType >
    00353             void write_bands( Encoder * enc, ImageIterator ul, ImageIterator lr, Accessor a, DstValueType )
    00354         }
    00355         \endcode
    00356
    00357       \param enc encoder object through which the destination data will be accessed
    00358       \param ul  image iterator referencing the upper left pixel of the source image
    00359       \param lr  image iterator referencing the lower right pixel of the source image
    00360       \param a   image accessor for the source image
    00361     */
    00362     template< class ImageIterator, class Accessor, class DstValueType >
    00363     void write_bands( Encoder * enc, ImageIterator ul, ImageIterator lr, Accessor a, DstValueType)
    00364     {
    00365         typedef unsigned int size_type;
    00366         typedef typename ImageIterator::row_iterator SrcRowIterator;
    00367         typedef typename Accessor::value_type  AccessorValueType;
    00368         typedef typename AccessorValueType::value_type SrcValueType;
    00369
    00370         // complete decoder settings
    00371         const size_type width = lr.x - ul.x;
    00372         const size_type height = lr.y - ul.y;
    00373         enc->setWidth(width);
    00374         enc->setHeight(height);
    00375         const size_type num_bands = a.size(ul);
    00376         enc->setNumBands(num_bands);
    00377         enc->finalizeSettings();
    00378
    00379         SrcRowIterator xs;
    00380         DstValueType * scanline;
    00381
    00382         // iterate
    00383         ImageIterator ys(ul);
    00384         for( size_type y = 0; y < height; ++y, ++ys.y ) {
    00385             for( size_type b = 0; b < num_bands; ++b ) {
    00386                 xs = ys.rowIterator();
    00387                 scanline = static_cast< DstValueType * >
    00388                     (enc->currentScanlineOfBand(b));
    00389                 for( size_type x = 0; x < width; ++x, ++xs ) {
    00390                     *scanline = detail::RequiresExplicitCast<DstValueType>::cast(a.getComponent( xs, b ));
    00391                     scanline += enc->getOffset();
    00392                 }
    00393             }
    00394             enc->nextScanline();
    00395         }
    00396     } // write_bands()
    00397
    00398     template< class MArray, class DstValueType >
    00399     void write_bands( Encoder * enc, MArray const & array, DstValueType)
    00400     {
    00401         typedef unsigned int size_type;
    00402
    00403         // complete decoder settings
    00404         const size_type width = array.shape(0);
    00405         const size_type height = array.shape(1);
    00406         enc->setWidth(width);
    00407         enc->setHeight(height);
    00408         const size_type num_bands = array.shape(2);
    00409         enc->setNumBands(num_bands);
    00410         enc->finalizeSettings();
    00411
    00412         DstValueType * scanline;
    00413
    00414         // iterate
    00415         for( size_type y = 0; y < height; ++y ) {
    00416             for( size_type b = 0; b < num_bands; ++b ) {
    00417                 scanline = static_cast< DstValueType * >
    00418                     (enc->currentScanlineOfBand(b));
    00419                 for( size_type x = 0; x < width; ++x) {
    00420                     *scanline = array(x, y, b);
    00421                     scanline += enc->getOffset();
    00422                 }
    00423             }
    00424             enc->nextScanline();
    00425         }
    00426     } // write_bands()
    00427
    00428     /*!
    00429       \brief used for writing bands after the source data type has been figured out.
    00430
    00431         <b>\#include</b> "<a href="impex_8hxx-source.html">vigra/impex.hxx</a>"<br>
    00432         Namespace: vigra
    00433
    00434         <b> Declaration:</b>
    00435
    00436         \code
    00437         namespace vigra {
    00438             template< class ImageIterator, class Accessor, class DstValueType >
    00439             void write_band( Encoder * enc, ImageIterator ul, ImageIterator lr, Accessor a, DstValueType )
    00440         }
    00441         \endcode
    00442
    00443       \param enc encoder object through which the destination data will be accessed
    00444       \param ul  image iterator referencing the upper left pixel of the source image
    00445       \param lr  image iterator referencing the lower right pixel of the source image
    00446       \param a   image accessor for the source image
    00447     */
    00448     template< class ImageIterator, class Accessor, class DstValueType >
    00449     void write_band( Encoder * enc, ImageIterator ul, ImageIterator lr, Accessor a, DstValueType)
    00450     {
    00451         typedef unsigned int size_type;
    00452         typedef typename ImageIterator::row_iterator SrcRowIterator;
    00453         typedef typename Accessor::value_type SrcValueType;
    00454
    00455         // complete decoder settings
    00456         const size_type width = lr.x - ul.x;
    00457         const size_type height = lr.y - ul.y;
    00458         enc->setWidth(width);
    00459         enc->setHeight(height);
    00460         enc->setNumBands(1);
    00461         enc->finalizeSettings();
    00462
    00463         SrcRowIterator xs;
    00464         DstValueType * scanline;
    00465
    00466         // iterate
    00467         ImageIterator ys(ul);
    00468         size_type y;
    00469         for(  y = 0; y < height; ++y, ++ys.y ) {
    00470             xs = ys.rowIterator();
    00471             scanline = static_cast< DstValueType * >(enc->currentScanlineOfBand(0));
    00472             for( size_type x = 0; x < width; ++x, ++xs, ++scanline )
    00473                 *scanline = detail::RequiresExplicitCast<DstValueType>::cast(a(xs));
    00474             enc->nextScanline();
    00475         }
    00476     } // write_band()
    00477
    00478 namespace detail {
    00479
    00480     template < class SrcIterator, class SrcAccessor,
    00481                class DestIterator, class DestAccessor >
    00482     void mapScalarImageToLowerPixelType( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00483                                          DestIterator dul, DestAccessor dget )
    00484     {
    00485         typedef typename SrcAccessor::value_type SrcValue;
    00486         typedef typename DestAccessor::value_type DestValue;
    00487         typedef typename NumericTraits<SrcValue>::RealPromote PromoteValue;
    00488
    00489         FindMinMax<SrcValue> minmax;
    00490         inspectImage( sul, slr, sget, minmax );
    00491         double scale = (double)NumericTraits<DestValue>::max() / (minmax.max - minmax.min) -
    00492                        (double)NumericTraits<DestValue>::min() / (minmax.max - minmax.min);
    00493         double offset = -minmax.min + NumericTraits<DestValue>::min() / scale;
    00494         transformImage( sul, slr, sget, dul, dget,
    00495                         linearIntensityTransform( scale, offset ) );
    00496     }
    00497
    00498     // export scalar images with conversion (if necessary)
    00499     template < class SrcIterator, class SrcAccessor, class T >
    00500     void exportScalarImage(SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00501                            Encoder * enc, bool downcast, T zero)
    00502     {
    00503         if (!downcast) {
    00504             write_band( enc, sul, slr, sget, zero );
    00505         } else {
    00506             // convert to unsigned char in the usual way
    00507             BasicImage<T> image(slr-sul);
    00508             mapScalarImageToLowerPixelType(sul, slr, sget, image.upperLeft(), image.accessor());
    00509             write_band( enc, image.upperLeft(),
    00510                         image.lowerRight(), image.accessor(), zero );
    00511         }
    00512     }
    00513
    00514     template < class SrcIterator, class SrcAccessor,
    00515                class MArray>
    00516     void mapVectorImageToLowerPixelType( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00517                                          MArray & array )
    00518     {
    00519         typedef typename SrcAccessor::value_type SrcValue;
    00520         typedef typename SrcValue::value_type SrcComponent;
    00521         typedef typename MArray::value_type DestValue;
    00522
    00523         FindMinMax<SrcComponent> minmax;
    00524         for(unsigned int i=0; i<sget.size(sul); ++i)
    00525         {
    00526             VectorElementAccessor<SrcAccessor> band(i, sget);
    00527             inspectImage( sul, slr, band, minmax );
    00528         }
    00529         double scale = (double)NumericTraits<DestValue>::max() / (minmax.max - minmax.min) -
    00530                        (double)NumericTraits<DestValue>::min() / (minmax.max - minmax.min);
    00531         double offset = -minmax.min + NumericTraits<DestValue>::min() / scale;
    00532         for(unsigned int i=0; i<sget.size(sul); ++i)
    00533         {
    00534             BasicImageView<DestValue> subImage = makeBasicImageView(array.bindOuter(i));
    00535             VectorElementAccessor<SrcAccessor> band(i, sget);
    00536             transformImage( sul, slr, band, subImage.upperLeft(), subImage.accessor(),
    00537                             linearIntensityTransform( scale, offset ) );
    00538         }
    00539     }
    00540
    00541     // export vector images with conversion (if necessary)
    00542     template < class SrcIterator, class SrcAccessor, class T >
    00543     void exportVectorImage(SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00544                            Encoder * enc, bool downcast, T zero)
    00545     {
    00546         int bands = sget.size(sul);
    00547         vigra_precondition(isBandNumberSupported(enc->getFileType(), bands),
    00548            "exportImage(): file format does not support requested number of bands (color channels)");
    00549         if ( !downcast )
    00550         {
    00551             write_bands( enc, sul, slr, sget, zero );
    00552         }
    00553         else
    00554         {
    00555             // convert to unsigned char in the usual way
    00556             int w = slr.x - sul.x;
    00557             int h = slr.y - sul.y;
    00558
    00559             typedef vigra::MultiArray<3, T> MArray;
    00560             MArray array(typename MArray::difference_type(w, h, bands));
    00561
    00562             mapVectorImageToLowerPixelType(sul, slr, sget, array);
    00563
    00564             write_bands( enc, array, zero );
    00565         }
    00566     }
    00567 } // namespace detail
    00568
    00569
    00570     /*!
    00571       \brief Deprecated.
    00572
    00573         Use \ref exportImage() instead.
    00574
    00575         <b> Declaration:</b>
    00576
    00577         \code
    00578         namespace vigra {
    00579             template < class SrcIterator, class SrcAccessor >
    00580             void exportFloatingVectorImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00581                                             const ImageExportInfo & info )
    00582         }
    00583         \endcode
    00584     */
    00585     template < class SrcIterator, class SrcAccessor >
    00586     void exportFloatingVectorImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00587                                     const ImageExportInfo & info )
    00588     {
    00589         exportImage(sul, slr, sget, info);
    00590     }
    00591
    00592     /*!
    00593       \brief Deprecated.
    00594
    00595         Use \ref exportImage() instead.
    00596
    00597         <b> Declaration:</b>
    00598
    00599         \code
    00600         namespace vigra {
    00601             template < class SrcIterator, class SrcAccessor >
    00602             void exportIntegralVectorImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00603                                             const ImageExportInfo & info )
    00604         }
    00605         \endcode
    00606     */
    00607     template < class SrcIterator, class SrcAccessor >
    00608     void exportIntegralVectorImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00609                                     const ImageExportInfo & info )
    00610     {
    00611         exportImage(sul, slr, sget, info);
    00612     }
    00613
    00614     /*!
    00615       \brief Deprecated.
    00616
    00617         Use \ref exportImage() instead.
    00618
    00619         <b> Declaration:</b>
    00620
    00621         \code
    00622         namespace vigra {
    00623             template < class SrcIterator, class SrcAccessor >
    00624             void exportFloatingScalarImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00625                                             const ImageExportInfo & info )
    00626         }
    00627         \endcode
    00628     */
    00629     template < class SrcIterator, class SrcAccessor >
    00630     void exportFloatingScalarImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00631                                     const ImageExportInfo & info )
    00632     {
    00633         exportImage(sul, slr, sget, info);
    00634     }
    00635
    00636     /*!
    00637       \brief Deprecated.
    00638
    00639         Use \ref exportImage() instead.
    00640
    00641         <b> Declaration:</b>
    00642
    00643         \code
    00644         namespace vigra {
    00645             template < class SrcIterator, class SrcAccessor >
    00646             void exportIntegralScalarImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00647                                             const ImageExportInfo & info )
    00648         }
    00649         \endcode
    00650     */
    00651     template < class SrcIterator, class SrcAccessor >
    00652     void exportIntegralScalarImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00653                                     const ImageExportInfo & info )
    00654     {
    00655         exportImage(sul, slr, sget, info);
    00656     }
    00657
    00658     template < class SrcIterator, class SrcAccessor >
    00659     void exportImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00660                       const ImageExportInfo & info, VigraFalseType /*not scalar */)
    00661     {
    00662         typedef typename SrcAccessor::value_type AccessorValueType;
    00663         typedef typename AccessorValueType::value_type SrcValueType;
    00664         std::string pixeltype = info.getPixelType();
    00665         std::auto_ptr<Encoder> enc = encoder(info);
    00666         bool downcast = negotiatePixelType(enc->getFileType(),
    00667                         TypeAsString<SrcValueType>::result(), pixeltype);
    00668         enc->setPixelType(pixeltype);
    00669         if(pixeltype == "UINT8")
    00670             detail::exportVectorImage( sul, slr, sget, enc.get(), downcast, (unsigned char)0);
    00671         else if(pixeltype == "INT16")
    00672             detail::exportVectorImage( sul, slr, sget, enc.get(), downcast, short());
    00673         else if(pixeltype == "INT32")
    00674             detail::exportVectorImage( sul, slr, sget, enc.get(), downcast, int());
    00675         else if(pixeltype == "FLOAT")
    00676             detail::exportVectorImage( sul, slr, sget, enc.get(), downcast, float());
    00677         else if(pixeltype == "DOUBLE")
    00678             detail::exportVectorImage( sul, slr, sget, enc.get(), downcast, double());
    00679         enc->close();
    00680     }
    00681
    00682     template < class SrcIterator, class SrcAccessor >
    00683     void exportImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00684                       const ImageExportInfo & info, VigraTrueType /*scalar*/ )
    00685     {
    00686         typedef typename SrcAccessor::value_type SrcValueType;
    00687         std::string pixeltype = info.getPixelType();
    00688         std::auto_ptr<Encoder> enc = encoder(info);
    00689         bool downcast = negotiatePixelType(enc->getFileType(),
    00690                            TypeAsString<SrcValueType>::result(), pixeltype);
    00691         enc->setPixelType(pixeltype);
    00692         if(pixeltype == "UINT8")
    00693             detail::exportScalarImage( sul, slr, sget, enc.get(), downcast, (unsigned char)0);
    00694         else if(pixeltype == "INT16")
    00695             detail::exportScalarImage( sul, slr, sget, enc.get(), downcast, short());
    00696         else if(pixeltype == "INT32")
    00697             detail::exportScalarImage( sul, slr, sget, enc.get(), downcast, int());
    00698         else if(pixeltype == "FLOAT")
    00699             detail::exportScalarImage( sul, slr, sget, enc.get(), downcast, float());
    00700         else if(pixeltype == "DOUBLE")
    00701             detail::exportScalarImage( sul, slr, sget, enc.get(), downcast, double());
    00702         enc->close();
    00703     }
    00704
    00705 /********************************************************/
    00706 /*                                                      */
    00707 /*                     exportImage                      */
    00708 /*                                                      */
    00709 /********************************************************/
    00710
    00711 /** \brief Write an image, given an \ref vigra::ImageExportInfo object.
    00712
    00713     If the file format to be exported to supports the pixel type of the
    00714     source image, the pixel type will be kept (e.g. <tt>float</tt>
    00715     can be stored as TIFF without conversion, in contrast to most other
    00716     image export toolkits). Otherwise, the pixel values are transformed
    00717     to the range 0.255 and converted to <tt>unsigned char</tt>. Currently,
    00718     the following file formats are supported. The pixel types given in
    00719     brackets are those that are written without conversion:
    00720
    00721     <DL>
    00722     <DT>"BMP"<DD> Microsoft Windows bitmap image file (pixel types: UINT8 as gray and RGB).
    00723     <DT>"GIF"<DD> CompuServe graphics interchange format; 8-bit color (pixel types: UINT8 as gray and RGB).
    00724     <DT>"JPEG"<DD> Joint Photographic Experts Group JFIF format; compressed 24-bit color
    00725                   (pixel types: UINT8 as gray and RGB). (only available if libjpeg is installed)
    00726     <DT>"PNG"<DD> Portable Network Graphic (pixel types: UINT8 and UINT16 with up to 4 channels).
    00727                   (only available if libpng is installed)
    00728     <DT>"PBM"<DD> Portable bitmap format (black and white).
    00729     <DT>"PGM"<DD> Portable graymap format (pixel types: UINT8, INT16, INT32 as gray scale)).
    00730     <DT>"PNM"<DD> Portable anymap (pixel types: UINT8, INT16, INT32 as gray and RGB).
    00731     <DT>"PPM"<DD> Portable pixmap format (pixel types: UINT8, INT16, INT32 as RGB).
    00732     <DT>"SUN"<DD> SUN Rasterfile (pixel types: UINT8 as gray and RGB).
    00733     <DT>"TIFF"<DD> Tagged Image File Format
    00734                 (pixel types: UINT8, INT16, INT32, FLOAT, DOUBLE with up to 4 channels).
    00735                 (only available if libtiff is installed.)
    00736     <DT>"VIFF"<DD> Khoros Visualization image file
    00737         (pixel types: UINT8, INT16, INT32, FLOAT, DOUBLE with arbitrary many channels).
    00738     </DL>
    00739
    00740     <b> Declarations:</b>
    00741
    00742     pass arguments explicitly:
    00743     \code
    00744     namespace vigra {
    00745         template <class SrcIterator, class SrcAccessor>
    00746         void exportImage(SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00747                          ImageExportInfo const & info)
    00748     }
    00749     \endcode
    00750
    00751
    00752     use argument objects in conjunction with \ref ArgumentObjectFactories:
    00753     \code
    00754     namespace vigra {
    00755         template <class SrcIterator, class SrcAccessor>
    00756         void exportImage(SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00757                          ImageExportInfo const & info)
    00758     }
    00759     \endcode
    00760
    00761     <b> Usage:</b>
    00762
    00763     <b>\#include</b> "<a href="impex_8hxx-source.html">vigra/impex.hxx</a>"<br>
    00764     Namespace: vigra
    00765
    00766     \code
    00767
    00768
    00769     vigra::BRGBImage out(w, h);
    00770     ...
    00771
    00772     // write as JPEG image, using compression quality 80
    00773     vigra::exportImage(srcImageRange(out),
    00774                       vigra::ImageExportInfo("myimage.jpg").setCompression("80"));
    00775
    00776
    00777     // force it to a particular pixel type (the pixel type must be supported by the
    00778     // desired image file format, otherwise an \ref vigra::PreconditionViolation exception will be thrown)
    00779     vigra::exportImage(srcImageRange(out),
    00780                       vigra::ImageExportInfo("myINT16image.tif").setPixelType("INT16"));
    00781     \endcode
    00782
    00783     <b> Preconditions:</b>
    00784
    00785     <UL>
    00786
    00787     <LI> the image file must be writable.
    00788     <LI> the file type must be one of the supported file types.
    00789
    00790
    00791     </UL>
    00792 **/
    00793     template < class SrcIterator, class SrcAccessor >
    00794     inline
    00795     void exportImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
    00796                       const ImageExportInfo & info )
    00797     {
    00798         typedef typename NumericTraits<typename SrcAccessor::value_type>::isScalar is_scalar;
    00799
    00800         try
    00801         {
    00802             exportImage( sul, slr, sget, info, is_scalar() );
    00803         }
    00804         catch(Encoder::TIFFNoLZWException &)
    00805         {
    00806             const_cast<ImageExportInfo &>(info).setCompression("");
    00807             exportImage( sul, slr, sget, info, is_scalar() );
    00808         }
    00809     }
    00810
    00811     template < class SrcIterator, class SrcAccessor >
    00812     inline
    00813     void exportImage( triple<SrcIterator, SrcIterator, SrcAccessor> src,
    00814                       const ImageExportInfo & info )
    00815     {
    00816         exportImage( src.first, src.second, src.third, info );
    00817     }
    00818
    00819 //@}
    00820
    00821 } // namespace vigra
    00822
    00823 #endif /* VIGRA_IMPEX_HXX */

© Ullrich Köthe (koethe@informatik.uni-hamburg.de) Cognitive Systems Group, University of Hamburg, Germany | html generated using doxygen and Python VIGRA 1.3.2 (27 Jan 2005)