LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiPNGFunctor.h

00001 /*
00002  * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*----------------------------------------------------------------
00025  * project ....: LTI Digital Image/Signal Processing Library
00026  * file .......: ltiPNGFunctor.h
00027  * authors ....: Stefan Syberichs, Pablo Alvarado
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 19.04.99
00030  * revisions ..: $Id: ltiPNGFunctor.h,v 1.9 2006/09/05 10:37:08 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_PNGFUNCTOR_H_
00034 #define _LTI_PNGFUNCTOR_H_
00035 
00036 #include "ltiConfig.h"
00037 
00038 #if defined HAVE_LIBPNG || defined HAVE_LOCALPNG
00039 
00040 #include "ltiIOFunctor.h"
00041 #include "ltiImage.h"
00042 #include <fstream>
00043 
00044 #ifdef HAVE_LIBPNG
00045 #include <cstdio>
00046 #else
00047 #include "ltipngdecoder.h"
00048 #include "ltipngencoder.h"
00049 #endif
00050 
00051 namespace lti {
00052   /**
00053    * Base class to functors which load and save images in PNG format.
00054    *
00055    * There are two implementations of this functor.
00056    * - The first one uses the C++ PNG interface from Miano (at
00057    *   Colosseum Builders C++ Image Library), for which several files
00058    *   are required.  These are published under another licence and
00059    *   therefore are not included in the default LTI-Lib.  You can use
00060    *   them in non-commercial applications and get them from
00061    *   ltilib.sourceforge.net.  See file ltilib/src/io/png/00readme.txt
00062    *   for more information.
00063    *   If the file ltiPNGFunctor.cpp and the directory png/ are found
00064    *   in ltilib/src/io, and HAVE_LIBPNG is not defined in basis/ltiConfig.h
00065    *   this will be the implementation used.
00066    *
00067    * - The second implementation uses the standard PNG Library, which is
00068    *   usually installed in all Linux distributions, but not for Windows.
00069    *   The implementation is included in the standard LTI-Lib, (file
00070    *   is called ltiPNGLibFunctor.cpp).
00071    *   The autoconf script (./configure) will automatically detect if you
00072    *   have the libpng installed, and activate this implementation if
00073    *   possible.
00074    *
00075    * We recomend to use the libpng, it is more robust against not 100%
00076    * correct PNG-files, and it is faster.  Mianos' implementation is mainly
00077    * for windows users that do not want to install the pnglib.
00078    *
00079    */
00080   class ioPNG : public ioFunctor {
00081   public:
00082     /**
00083      * Parameter class of the ioBMP class
00084      */
00085     class parameters : public ioFunctor::parameters {
00086     public:
00087 
00088       /**
00089        * default constructor
00090        */
00091       parameters();
00092 
00093       /**
00094        * copy constructor
00095        */
00096       parameters(const parameters& other) : ioFunctor::parameters() {
00097         copy(other);
00098       };
00099 
00100       /**
00101        * copy member
00102        */
00103       parameters& copy(const parameters& other);
00104 
00105       /**
00106        * returns a pointer to a clone of the parameters.
00107        */
00108       virtual functor::parameters* clone() const;
00109 
00110       /**
00111        * returns name of this class
00112        */
00113       virtual const char* getTypeName() const;
00114 
00115       /**
00116        * write the parameters in the given ioHandler
00117        * @param handler the ioHandler to be used
00118        * @param complete if true (the default) the enclosing begin/end will
00119        *        be also written, otherwise only the data block will be written.
00120        * @return true if write was successful
00121        */
00122       virtual bool write(ioHandler& handler,const bool complete=true) const;
00123 
00124       /**
00125        * write the parameters in the given ioHandler
00126        * @param handler the ioHandler to be used
00127        * @param complete if true (the default) the enclosing begin/end will
00128        *        be also written, otherwise only the data block will be written.
00129        * @return true if write was successful
00130        */
00131       virtual bool read(ioHandler& handler,const bool complete=true);
00132 
00133 #     ifdef _LTI_MSC_6
00134       /**
00135        * this function is required by MSVC only, as a workaround for a
00136        * very awful bug, which exists since MSVC V.4.0, and still by
00137        * V.6.0 with all bugfixes (so called "service packs") remains
00138        * there...  This method is public due to another bug, so please
00139        * NEVER EVER call this method directly: use read() instead!
00140        */
00141       bool readMS(ioHandler& handler,const bool complete=true);
00142 
00143       /**
00144        * this function is required by MSVC only, as a workaround for a
00145        * very awful bug, which exists since MSVC V.4.0, and still by
00146        * V.6.0 with all bugfixes (so called "service packs") remains
00147        * there...  This method is public due to another bug, so please
00148        * NEVER EVER call this method directly: use write() instead!
00149        */
00150       bool writeMS(ioHandler& handler,const bool complete=true) const;
00151 #     endif
00152 
00153       // ------------------------------------------------
00154       // the parameters
00155       // ------------------------------------------------
00156 
00157       /**
00158        * bits per pixel
00159        *
00160        * Default value: 24
00161        */
00162       int bitsPerPixel;
00163 
00164       /**
00165        * quantization of colors.
00166        * if 0 no quantization will be made.
00167        * if >0 then a k-mean analysis of the colors will be made and
00168        * the used color number will be reduce to <code>quantColors</code>.
00169        * <b> Not implemented yet</b>
00170        *
00171        * Default value: 0
00172        */
00173       int quantColors;
00174 
00175       /**
00176        * True if Alpha channel is to be considered. If this is true,
00177        * bitsPerPixel <b>must</b> be 32!
00178        *
00179        * Default value: false
00180        */
00181       bool useAlphaChannel;
00182 
00183     };
00184 
00185     /**
00186      * default constructor
00187      */
00188     ioPNG();
00189 
00190     /**
00191      * destructor
00192      */
00193     ~ioPNG();
00194 
00195     /**
00196      * returns current parameters.
00197      */
00198     const parameters& getParameters() const;
00199 
00200     // returns a pointer to a clone of the functor.
00201     //  virtual functor* clone() const { return (new ioBMP(*this));};
00202 
00203     /**
00204      * returns the name of this type
00205      */
00206     virtual const char* getTypeName() const;
00207   };
00208 
00209   /**
00210    * Functor to read a Portable Network Graphic (PNG) file.
00211    * It is NOT thread save, this means, the SAME instance can not be used
00212    * from different threads or processes at the same time.  If this occurs
00213    * an unpredictible behaviour must be expected!.
00214    * If this is required, just use a different instance of the functor
00215    * for each thread!
00216    *
00217    * Example:
00218    *
00219    * \code
00220    *
00221    * lti::image anImg;                      // an image
00222    * ...
00223    * lti::loadPNG myImg;                    // the load functor
00224    * lti::loadPNG::parameters param;        // functor parameters
00225    * param.filename = "~/tmp/theFile.png";  // name of the PNG file
00226    * myImg.setParameters(param);            // set the functor parameters
00227    * myImg.apply(anImg);                    // load the file
00228    *
00229    * \endcode
00230    *
00231    * You can also use the more confortable shortcut:
00232    *
00233    * \code
00234    * lti::image anImg;                      // an image
00235    * ...
00236    * lti::loadPNG loadImg;                  // the load functor
00237    * loadImg.load("~/tmp/theFile.png",img); // load the image.
00238    * \endcode
00239    *
00240    * @exception BadPngStream is thrown when the Stream is corrupted.
00241    * @see savePNG
00242    * @see image
00243    * @see loadPNG::parameters
00244    *
00245    * For more information on implementation issues see ioPNG
00246    */
00247   class loadPNG : public ioPNG {
00248   public:
00249     /**
00250      * default constructor
00251      */
00252     loadPNG();
00253 
00254     /**
00255      * destructor
00256      */
00257     ~loadPNG();
00258 
00259     /**
00260      * returns the name of this type
00261      */
00262     virtual const char* getTypeName() const;
00263 
00264     /**
00265      * load Image.
00266      * @exception BadPngStream is thrown when the Stream is corrupted.
00267      */
00268     bool apply(image& theImage);
00269 
00270     /**
00271      * load Image.
00272      * @exception BadPngStream is thrown when the Stream is corrupted.
00273      */
00274     bool apply(channel8& theChannel,lti::palette& colors);
00275 
00276     /**
00277      * shortcut for load PNG
00278      */
00279     bool load(const std::string& filename,image& theImage);
00280 
00281     /**
00282      * method for loading a PNG image from an already opened
00283      * stream.
00284      */
00285     bool load(std::ifstream& file,image& theImage);
00286 
00287     /**
00288      * shortcut for load PNG
00289      */
00290     bool load(const std::string& filename,
00291               channel8& theImage,
00292               lti::palette& colors);
00293 
00294     /**
00295      * returns a pointer to a clone of the functor.
00296      */
00297     virtual functor* clone() const;
00298 
00299     /**
00300      * check the data of the PNG header
00301      * @param filename name of the PNG file to be tested
00302      * @param imageSize returns the size of the PNG: imageSize.x is the
00303      *                  number of columns and imageSize.y the numbeer of
00304      *                  rows.
00305      * @param bitsPerPixel number of bits per pixel
00306      * @param imageType For color types 0 (gray) and 4 (gray-alpha), the
00307      *        mappings apply to the gray sample values (but not to the alpha
00308      *        sample). For color types 2 (RGB), 3 (indexed RGB), and 6 (RGBA),
00309      *        the mappings apply independently to each of the red, green, and
00310      *        blue sample values (but not the alpha sample). In the case of
00311      *        color type 3 (indexed RGB), the mapping refers to the RGB samples
00312      *        and not to the index values.
00313      * @return true if file is ok
00314      */
00315     bool checkHeader(const std::string& filename,
00316                            point& imageSize,
00317                            int&   bitsPerPixel,
00318                            int&   imageType);
00319 
00320   private:
00321 #ifdef HAVE_LIBPNG
00322     /**
00323      * method for loading a PNG image from an already opened
00324      * file.
00325      */
00326     bool load(FILE* file,image& theImage);
00327 
00328 #endif
00329   };
00330 
00331   /**
00332    * Functor to save a Protable Network graphic (PNG) file.
00333    * To save a PNG image just initialize the parameters of the
00334    * "savePNG" object and call the apply member.
00335    *
00336    * Example:
00337    *
00338    * \code
00339    *
00340    * lti::image anImg;                      // an image
00341    * ...
00342    * lti::savePNG saveImg;                  // the save functor
00343    * lti::savePNG::parameters param;        // functor parameters
00344    * param.filename = "~/tmp/theFile.png";  // name of the PNG file
00345    * saveImg.setParameters(param);          // set the functor parameters
00346    * saveImg.apply(anImg);                  // save the file
00347    *
00348    * \endcode
00349    *
00350    * You can also use the more confortable shortcut:
00351    *
00352    * \code
00353    * lti::image anImg;                      // an image
00354    * ...
00355    * lti::savePNG saveImg;                  // the save functor
00356    * saveImg.save("~/tmp/theFile.png",img); // save the image.
00357    * \endcode
00358    *
00359    * @see loadPNG
00360    * @see image
00361    * @see savePNG::parameters
00362    */
00363   class savePNG : public ioPNG {
00364   public:
00365     /**
00366      * default constructor
00367      */
00368     savePNG();
00369 
00370     /**
00371      * destructor
00372      */
00373     ~savePNG();
00374 
00375     /**
00376      * returns the name of this type
00377      */
00378     virtual const char* getTypeName() const;
00379 
00380     /**
00381      * save image as PNG
00382      */
00383     bool apply(const image& theImage);
00384 
00385     /**
00386      * save float channel as grey image
00387      */
00388     bool apply(const channel& theChannel);
00389 
00390     /**
00391      * save 8-bit channel as 24-bbit RGB by assigning each
00392      * R, G and B the same grey-value
00393      */
00394     bool apply(const channel8& theChannel,
00395                const lti::palette& colors = emptyPalette);
00396 
00397     /**
00398      * this will save an image as a 24 bit RGB bitmap image
00399      */
00400     bool save(const std::string& filename,
00401               const image& theImage);
00402 
00403     /**
00404      * save an "indexed" image contained in the given channel8 (indices) and
00405      * the given color palette
00406      */
00407     bool save(const std::string& filename,
00408               const channel8& theChannel,
00409               const lti::palette& colors = emptyPalette);
00410 
00411     /**
00412      * save a channel as a grey image (0 = Black, 1 = White)
00413      * the given color palette
00414      */
00415     bool save(const std::string& filename,
00416               const channel& theChannel);
00417 
00418     /**
00419      * returns a pointer to a clone of the functor.
00420      */
00421     virtual functor* clone() const;
00422   };
00423 }  //namespace lti
00424 
00425 #endif
00426 #endif

Generated on Sat Apr 10 15:25:58 2010 for LTI-Lib by Doxygen 1.6.1