last update 20 Sep 2009 |
00001 /* 00002 * Copyright (C) 1998-2006 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * 00006 * This file is part of the Computer Vision and Robotics Library (CVR-Lib) 00007 * 00008 * The CVR-Lib is free software; you can redistribute it and/or 00009 * modify it under the terms of the BSD License. 00010 * 00011 * All rights reserved. 00012 * 00013 * Redistribution and use in source and binary forms, with or without 00014 * modification, are permitted provided that the following conditions are met: 00015 * 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 00019 * 2. Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * 00023 * 3. Neither the name of the authors nor the names of its contributors may be 00024 * used to endorse or promote products derived from this software without 00025 * specific prior written permission. 00026 * 00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00028 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00029 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00030 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00031 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00032 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00033 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00034 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00035 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00036 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00037 * POSSIBILITY OF SUCH DAMAGE. 00038 */ 00039 00040 00041 /** 00042 * \file cvrIOPNG.h 00043 * Contains the cvr::ioPNG class to deal with .png files. 00044 * \author Stefan Syberichs 00045 * \author Pablo Alvarado 00046 * \date 19.04.1999 00047 * 00048 * revisions ..: $Id: cvrIOPNG.h,v 1.7 2007/10/07 04:21:25 alvarado Exp $ 00049 */ 00050 00051 #ifndef _CVR_IO_PNG_H_ 00052 #define _CVR_IO_PNG_H_ 00053 00054 #include "cvrConfig.h" 00055 00056 #if defined HAVE_LIBPNG || defined HAVE_LOCALPNG 00057 00058 #include "cvrIOImageInterface.h" 00059 #include <fstream> 00060 00061 #ifdef HAVE_LIBPNG 00062 #include <cstdio> 00063 #else 00064 #include "ltipngdecoder.h" 00065 #include "ltipngencoder.h" 00066 #endif 00067 00068 namespace cvr { 00069 /** 00070 * Class to read and write files with images and channels in PNG format. 00071 * 00072 * It is NOT thread save, this means, the SAME instance can not be used 00073 * from different threads or processes at the same time. If this occurs 00074 * an unpredictible behaviour must be expected!. 00075 * If this is required, just use a different instance of the functor 00076 * for each thread, or use mutexes to avoid parallel accesses. 00077 * 00078 * There are two implementations of this functor. 00079 * - The first one uses the C++ PNG interface from Miano (at 00080 * Colosseum Builders C++ Image Library), for which several files 00081 * are required. These are published under another licence and 00082 * therefore are not included in the default CVR-Lib . You can use 00083 * them in non-commercial applications and get them from 00084 * ltilib.sourceforge.net. See file ltilib/src/io/png/00readme.txt 00085 * for more information. 00086 * If the file cvrPNGFunctor.cpp and the directory png/ are found 00087 * in ltilib/src/io, and HAVE_LIBPNG is not defined in basis/cvrConfig.h 00088 * this will be the implementation used. 00089 * 00090 * - The second implementation uses the standard PNG Library, which is 00091 * usually installed in all Linux distributions, but not for Windows. 00092 * The implementation is included in the standard CVR-Lib , (file 00093 * is called cvrPNGLibFunctor.cpp). 00094 * The autoconf script (./configure) will automatically detect if you 00095 * have the libpng installed, and activate this implementation if 00096 * possible. 00097 * 00098 * We recomend to use the libpng, it is more robust against not 100% 00099 * correct PNG-files, and it is faster. Mianos' implementation is mainly 00100 * for windows users that do not want to install the pnglib. 00101 * 00102 * \section inpng Reading PNG files 00103 * 00104 * To read PNG files, you can use following code 00105 * 00106 * Example: 00107 * 00108 * \code 00109 * 00110 * cvr::image anImg; // an image 00111 * cvr::ioPNG loader; // the load functor 00112 * loader.load("~/tmp/theFile.png",img); // load the image. 00113 * \endcode 00114 * 00115 * \section outpng Saving PNG files 00116 * 00117 * To save a Protable Network graphic (PNG) file just initialize the 00118 * parameters and call the save member. 00119 * 00120 * Example: 00121 * 00122 * \code 00123 * 00124 * cvr::image anImg; // an image 00125 * ... [ initialize you image data here ] 00126 * cvr::ioPNG saveImg; // the save functor 00127 * saveImg.save("~/tmp/theFile.png",img); // save the image. 00128 * \endcode 00129 * 00130 * @exception BadPngStream is thrown when the Stream is corrupted. 00131 * 00132 * @ingroup gIOImage 00133 */ 00134 class ioPNG : public ioImageInterface { 00135 public: 00136 /** 00137 * Parameter class of the ioBMP class 00138 */ 00139 class parameters : public ioImageInterface::parameters { 00140 public: 00141 00142 /** 00143 * Default constructor 00144 */ 00145 parameters(); 00146 00147 /** 00148 * Copy constructor 00149 */ 00150 parameters(const parameters& other); 00151 00152 /** 00153 * Copy member 00154 */ 00155 parameters& copy(const parameters& other); 00156 00157 /** 00158 * Copy operator 00159 */ 00160 parameters& operator=(const parameters& other); 00161 00162 /** 00163 * Returns the name of this class. 00164 */ 00165 virtual const std::string& name() const; 00166 00167 /** 00168 * Returns a pointer to a clone of the parameters. 00169 */ 00170 virtual parameters* clone() const; 00171 00172 /** 00173 * Returns a pointer to a new instance of the parameters 00174 */ 00175 virtual parameters* newInstance() const; 00176 00177 /** 00178 * Write the parameters in the given ioHandler 00179 * @param handler The ioHandler to be used 00180 * @param complete If true (the default) the enclosing begin/end will 00181 * be also written, otherwise only the data block will be written. 00182 * @return true if write was successful 00183 */ 00184 virtual bool write(ioHandler& handler,const bool complete=true) const; 00185 00186 /** 00187 * Write the parameters in the given ioHandler 00188 * @param handler The ioHandler to be used 00189 * @param complete If true (the default) the enclosing begin/end will 00190 * be also written, otherwise only the data block will be written. 00191 * @return true if write was successful 00192 */ 00193 virtual bool read(ioHandler& handler,const bool complete=true); 00194 00195 00196 // ------------------------------------------------ 00197 // the parameters 00198 // ------------------------------------------------ 00199 00200 /** 00201 * Bits per pixel 00202 * 00203 * Default value: 24 00204 */ 00205 int bitsPerPixel; 00206 00207 /** 00208 * True if alpha channel is to be considered. If this is true, 00209 * bitsPerPixel \b must be 32! 00210 * 00211 * Default value: false 00212 */ 00213 bool useAlphaChannel; 00214 }; 00215 00216 /** 00217 * Default constructor 00218 */ 00219 ioPNG(); 00220 00221 /** 00222 * Constructor with parameters 00223 */ 00224 ioPNG(const parameters& par); 00225 00226 /** 00227 * Copy constructor 00228 */ 00229 ioPNG(const ioPNG& other); 00230 00231 /** 00232 * Destructor 00233 */ 00234 virtual ~ioPNG(); 00235 00236 /** 00237 * Returns current parameters. 00238 */ 00239 const parameters& getParameters() const; 00240 00241 /** 00242 * Returns the name of this class 00243 */ 00244 virtual const std::string& name() const; 00245 00246 /** 00247 * Returns a pointer to a clone of this functor. 00248 */ 00249 virtual ioPNG* clone() const; 00250 00251 /** 00252 * Returns a pointer to a new instance of this functor. 00253 */ 00254 virtual ioPNG* newInstance() const; 00255 00256 /** 00257 * Load true-color image 00258 */ 00259 virtual bool load(const std::string& filename,image& theImage); 00260 00261 /** 00262 * Load indexed image 00263 */ 00264 virtual bool load(const std::string& filename, 00265 matrix<ubyte>& theImage, 00266 palette& colors); 00267 00268 /** 00269 * Load the contents as a gray channel. 00270 * 00271 * Default implementation just discards the palette of 00272 * load(const std::string&,matrix<ubyte>&,palette&) 00273 */ 00274 virtual bool load(const std::string& filename, 00275 matrix<ubyte>& chnl); 00276 00277 00278 /** 00279 * Load a floating point channel. Not many file formats support floating 00280 * point channels, so the default implementation just casts a channel8 into 00281 * the float. 00282 */ 00283 virtual bool load(const std::string& filename, 00284 matrix<float>& chnl); 00285 00286 /** 00287 * Load the contents as a integer mask 00288 * 00289 * Default implementation returns the casting of the channel8 related 00290 * method. 00291 */ 00292 virtual bool load(const std::string& filename, 00293 matrix<int32>& chnl, 00294 palette& pal); 00295 00296 /** 00297 * Load the contents as a integer mask 00298 * 00299 * Default implementation discards the palette of the other matrix<int32> 00300 * method. 00301 */ 00302 virtual bool load(const std::string& filename, 00303 matrix<int32>& chnl); 00304 00305 00306 /** 00307 * Check the data of the PNG header 00308 * @param filename name of the PNG file to be tested 00309 * @param info header information. 00310 * @return true if file is ok 00311 */ 00312 virtual bool checkHeader(const std::string& filename, 00313 headerInformation& info); 00314 00315 00316 00317 /** 00318 * This will save an image as a 24 bit RGB bitmap image 00319 */ 00320 bool save(const std::string& filename, 00321 const image& theImage); 00322 00323 /** 00324 * Save an "indexed" image contained in the given channel8 (indices) and 00325 * the given color palette 00326 */ 00327 bool save(const std::string& filename, 00328 const matrix<ubyte>& theChannel, 00329 const cvr::palette& colors); 00330 00331 /** 00332 * Save an gray scale channel 00333 */ 00334 bool save(const std::string& filename, 00335 const matrix<ubyte>& theChannel); 00336 00337 00338 /** 00339 * Save a floating point channel. 00340 * 00341 * Not many file formats support floating point channels, so the default 00342 * implementation just casts the channel to a channel8, which is saved. 00343 * You normally lose precision. 00344 */ 00345 virtual bool save(const std::string& filename, 00346 const matrix<float>& chnl); 00347 00348 /** 00349 * Save the contents as a integer mask. 00350 * 00351 * Default implementation returns the casting of the channel8 related 00352 * method. 00353 */ 00354 virtual bool save(const std::string& filename, 00355 const matrix<int32>& chnl, 00356 const palette& pal); 00357 00358 /** 00359 * Save the contents as a integer mask. 00360 * 00361 * Default implementation discards the palette of the other matrix<int32> 00362 * method. 00363 */ 00364 virtual bool save(const std::string& filename, 00365 const matrix<int32>& chnl); 00366 00367 00368 00369 00370 private: 00371 #ifdef HAVE_LIBPNG 00372 /** 00373 * method for loading a PNG image from an already opened 00374 * file. 00375 */ 00376 bool load(FILE* file,image& theImage); 00377 00378 #endif 00379 00380 }; 00381 00382 } //namespace cvr 00383 00384 #endif 00385 #endif 00386