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

details vigra/impex.hxx VIGRA

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