latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 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 * project ....: LTI Digital Image/Signal Processing Library 00025 * file .......: ltiAllFunctor.h 00026 * authors ....: Axel Berner 00027 * organization: LTI, RWTH Aachen 00028 * creation ...: 17.06.2002 00029 * revisions ..: $Id: ltiALLFunctor.h,v 1.10 2006/09/08 10:01:43 ltilib Exp $ 00030 */ 00031 00032 #ifndef _LTI_ALLFUNCTOR_H_ 00033 #define _LTI_ALLFUNCTOR_H_ 00034 00035 #include "ltiConfig.h" 00036 #include "ltiRGBPixel.h" 00037 #include "ltiBMPFunctor.h" 00038 #include "ltiJPEGFunctor.h" 00039 #include "ltiPNGFunctor.h" 00040 #include "ltiImage.h" 00041 00042 #include "ltiSplitImageToRGB.h" //for grey jpeg/png images 00043 #include "ltiSplitImageToHSI.h" //for grey jpeg/png images 00044 00045 #include <fstream> 00046 00047 // define one macro if the support for jpeg is enabled 00048 #undef LTI_HAVE_JPEG 00049 #if defined(HAVE_LOCALJPEG) || defined(HAVE_LIBJPEG) 00050 #define LTI_HAVE_JPEG 1 00051 #endif 00052 00053 // define one macro if the support for png is enabled 00054 #undef LTI_HAVE_PNG 00055 #if defined(HAVE_LOCALPNG) || defined(HAVE_LIBPNG) 00056 #define LTI_HAVE_PNG 1 00057 #endif 00058 00059 namespace lti { 00060 /** 00061 * \file ltiALLFunctor.h provides classes to read and write images 00062 * in all formats supported by the LTI-Lib. 00063 * At this time these formats are PNG,JPEG,BMP 00064 */ 00065 00066 /** 00067 * Base class to functors which load and save images in all formats. 00068 * The parameter class for this functor is lti::ioImage::parameters. 00069 * 00070 * See the derived classes for more information. 00071 */ 00072 class ioImage : public ioFunctor { 00073 public: 00074 /** 00075 * Parameter class of the ioImage class 00076 */ 00077 class parameters : public ioFunctor::parameters { 00078 public: 00079 /** 00080 * default constructor 00081 */ 00082 parameters(); 00083 /** 00084 * copy constructor 00085 */ 00086 parameters(const parameters& other) : ioFunctor::parameters() { 00087 copy(other); 00088 }; 00089 00090 /** 00091 * copy member 00092 */ 00093 parameters& copy(const parameters& other); 00094 00095 /** 00096 * returns a pointer to a clone of the parameters. 00097 */ 00098 virtual functor::parameters* clone() const; 00099 00100 /** 00101 * returns name of this class 00102 */ 00103 virtual const char* getTypeName() const; 00104 00105 /** 00106 * write the parameters in the given ioHandler 00107 * @param handler the ioHandler to be used 00108 * @param complete if true (the default) the enclosing begin/end will 00109 * be also written, otherwise only the data block will be written. 00110 * @return true if write was successful 00111 */ 00112 virtual bool write(ioHandler& handler,const bool complete=true) const; 00113 00114 /** 00115 * write the parameters in the given ioHandler 00116 * @param handler the ioHandler to be used 00117 * @param complete if true (the default) the enclosing begin/end will 00118 * be also written, otherwise only the data block will be written. 00119 * @return true if write was successful 00120 */ 00121 virtual bool read(ioHandler& handler,const bool complete=true); 00122 00123 # ifdef _LTI_MSC_6 00124 /** 00125 * this function is required by MSVC only, as a workaround for a 00126 * very awful bug, which exists since MSVC V.4.0, and still by 00127 * V.6.0 with all bugfixes (so called "service packs") remains 00128 * there... This method is public due to another bug, so please 00129 * NEVER EVER call this method directly: use read() instead! 00130 */ 00131 bool readMS(ioHandler& handler,const bool complete=true); 00132 00133 /** 00134 * this function is required by MSVC only, as a workaround for a 00135 * very awful bug, which exists since MSVC V.4.0, and still by 00136 * V.6.0 with all bugfixes (so called "service packs") remains 00137 * there... This method is public due to another bug, so please 00138 * NEVER EVER call this method directly: use write() instead! 00139 */ 00140 bool writeMS(ioHandler& handler,const bool complete=true) const; 00141 # endif 00142 00143 // ------------------------------------------------ 00144 // the parameters 00145 // ------------------------------------------------ 00146 00147 /** 00148 * Parameter object used when loading/saving BMP images 00149 */ 00150 ioBMP::parameters paramBMP; 00151 00152 #ifdef LTI_HAVE_JPEG 00153 /** 00154 * Parameter object used when loading/saving JPEG images. 00155 * 00156 * You need to have enabled at least one of both kinds 00157 * of JPEG support enabled to use this. See the FAQ for 00158 * more information. 00159 */ 00160 ioJPEG::parameters paramJPEG; 00161 #endif 00162 00163 #ifdef LTI_HAVE_PNG 00164 /** 00165 * Parameter object used when loading/saving PNG images 00166 * 00167 * You need to have enabled at least one of both kinds 00168 * of PNG support enabled to use this. See the FAQ for 00169 * more information. 00170 */ 00171 ioPNG::parameters paramPNG; 00172 #endif 00173 }; 00174 00175 /** 00176 * default constructor 00177 */ 00178 ioImage(); 00179 00180 /** 00181 * destructor 00182 */ 00183 ~ioImage() {}; 00184 00185 /** 00186 * returns parameters. 00187 */ 00188 const parameters& getParameters() const; 00189 00190 /** 00191 * returns current parameters. 00192 */ 00193 const ioFunctor::parameters& getCurrParameters() const; 00194 00195 /** 00196 * Set the parameters of this functor. 00197 */ 00198 virtual bool setParameters (const functor::parameters& theParam ); 00199 00200 /** 00201 * Shortcut to set the parameters for BMP files only. 00202 */ 00203 bool setParameters(const ioBMP::parameters& theParam ); 00204 00205 #ifdef LTI_HAVE_JPEG 00206 /** 00207 * Shortcut to set the parameters for JPEG files only. 00208 */ 00209 bool setParameters(const ioJPEG::parameters& theParam ); 00210 #endif 00211 #ifdef LTI_HAVE_PNG 00212 /** 00213 * Shortcut to set the parameters for PNG files only. 00214 */ 00215 bool setParameters(const ioPNG::parameters& theParam ); 00216 #endif 00217 /** 00218 * returns the name of this type 00219 */ 00220 virtual const char* getTypeName() const; 00221 00222 /** 00223 * Image File Type 00224 */ 00225 enum eImageFileType { 00226 UNKNOWN = -1, /*! Unknown image file format */ 00227 BMP = 0, /*! MS Bitmap Format */ 00228 JPEG, /*! JPEG Standard */ 00229 PNG /*! Portable Network Graphics */ 00230 }; 00231 00232 bool setType(const eImageFileType& theType); 00233 00234 protected: 00235 /** 00236 * stores the actual extension type 00237 */ 00238 eImageFileType type; 00239 00240 /** 00241 * analyses the extension of filename and 00242 * returns the index of the filetype 00243 */ 00244 eImageFileType getExtensionType(const std::string& filename) const; 00245 00246 /** 00247 * set the filename in the corresponding(type) parameter 00248 */ 00249 bool setFilenameInParam(const std::string& filename, 00250 const eImageFileType& type); 00251 }; 00252 00253 /** 00254 * Functor to read an image file. 00255 * 00256 * It is NOT thread save, this means, the SAME instance can not be 00257 * used from different threads or processes at the same time. If 00258 * this occurs an unpredictible behaviour must be expected!. If you 00259 * need to read or write many images at the same time, use in each 00260 * thread an instance of this functor, or protect your code with 00261 * semaphores. 00262 * 00263 * This class is used to load lti::image or lti::channel objects 00264 * from files of one of the formats supported by the LTI-Lib. 00265 * 00266 * The load methods will use the extension in the filename to decide 00267 * the format in which the image should be read. 00268 * 00269 * Three formats are supported: PNG, BMP and JPEG. 00270 * 00271 * Example: 00272 * 00273 * \code 00274 * lti::loadImage loader; // functor to load images 00275 * lti::image img; // the image loaded will be left here. 00276 * 00277 * loader.load("myimage.png",img); // load PNG image 00278 * loader.load("myimage.bmp",img); // load BMP image 00279 * loader.load("myimage.jpg",img); // load JPG image 00280 * \endcode 00281 * 00282 * The valid extensions are case insensitive matches for .jpg, .jpeg, .png 00283 * and .bmp. 00284 */ 00285 class loadImage : public ioImage { 00286 public: 00287 /** 00288 * default constructor 00289 */ 00290 loadImage(); 00291 00292 /** 00293 * destructor 00294 */ 00295 ~loadImage() {}; 00296 00297 /** 00298 * returns the name of this type ("loadImage") 00299 */ 00300 virtual const char* getTypeName() const; 00301 00302 /** 00303 * load Image 00304 * @param theImage the file specified in the parameters will be loaded 00305 * in this image. Note that independently of the soft 00306 * of image in the file, this will always be converted to 00307 * a color lti::image. 00308 * @return a reference to the loaded image. 00309 */ 00310 bool apply(image& theImage); 00311 00312 /** 00313 * load channel 00314 * 00315 * Use this method if you know that the file contains a gray 00316 * valued image. If you try to load a 24-bit image with this 00317 * method, some quantization algorithms will be used to reduce the 00318 * number of colors to 256. 00319 * 00320 * @param theChannel the image on the file will be loaded here 00321 * @param colors theChannel contains just indexes to the pixel values 00322 * in this vector. 00323 * @return true if successful, false otherwise. 00324 */ 00325 bool apply(channel8& theChannel,lti::palette& colors); 00326 00327 /** 00328 * load channel 00329 * 00330 * Use this method if you know that the file contains a gray 00331 * valued image. If you try to load a 24-bit image with this 00332 * method, some quantization algorithms will be used to reduce the 00333 * number of colors to 256. 00334 * 00335 * @param theChannel the image on the file will be loaded here 00336 * @return true if successful, false otherwise. 00337 */ 00338 bool apply(channel& theChannel); 00339 00340 /** 00341 * shortcut for load an image 00342 * @param filename name of the file to be readed 00343 * @param theImage variable where the image will to be stored 00344 */ 00345 bool load(const std::string& filename,image& theImage); 00346 00347 /** 00348 * shortcut for load an image. 00349 * 00350 * Use this method if you know that the file contains a gray valued image. 00351 * If you try to load a 24-bit image with this method, some quantization 00352 * algorithms will be used to reduce the number of colors to 256. 00353 * 00354 * @param filename name of the file to be readed 00355 * @param theChannel variable where the image will be stored 00356 * @param colors the palette used will be stored here 00357 */ 00358 bool load(const std::string& filename, 00359 channel8& theChannel, 00360 lti::palette& colors); 00361 00362 /** 00363 * shortcut for load an image. 00364 * 00365 * Use this method if you know that the file contains a gray valued image. 00366 * If you try to load a 24-bit image with this method, some quantization 00367 * algorithms will be used to reduce the number of colors to 256. 00368 * 00369 * @param filename name of the file to be readed 00370 * @param theChannel variable where the image will be stored 00371 */ 00372 bool load(const std::string& filename, 00373 channel& theChannel); 00374 00375 00376 /** 00377 * check the data of the image header 00378 * @param filename name of the image file to be tested 00379 * @param imageSize returns the size of the image: imageSize.x is the 00380 * number of columns and imageSize.y the number of 00381 * rows. 00382 * @return true if file is ok 00383 */ 00384 bool checkHeader(const std::string& filename, 00385 point& imageSize); 00386 00387 /** 00388 * check the data of the image header 00389 * @param filename name of the image file to be tested 00390 * @param imageSize returns the size of the image: imageSize.x is the 00391 * number of columns and imageSize.y the number of 00392 * rows. 00393 * @param trueColor true if the image contains 3x256 bits per pixel, 00394 * false, otherwise. If false, you can use the 00395 * methods to read channel8+palette 00396 * @return true if file is ok 00397 */ 00398 bool checkHeader(const std::string& filename, 00399 point& imageSize, 00400 bool& trueColor); 00401 00402 /** 00403 * returns a pointer to a clone of the functor. 00404 */ 00405 virtual functor* clone() const; 00406 00407 protected: 00408 /** 00409 * functor to load BMP images 00410 */ 00411 lti::loadBMP loaderBMP; 00412 00413 #ifdef LTI_HAVE_JPEG 00414 /** 00415 * functor to load JPEG images 00416 */ 00417 lti::loadJPEG loaderJPEG; 00418 #endif 00419 00420 #ifdef LTI_HAVE_PNG 00421 /** 00422 * functor to load PNG images 00423 */ 00424 lti::loadPNG loaderPNG; 00425 #endif 00426 00427 /** functor to split images to RGB */ 00428 lti::splitImageToRGB rgbSplitter; 00429 00430 /** functor to split images to HSI */ 00431 lti::splitImageToHSI hsiSplitter; 00432 00433 }; 00434 00435 /** 00436 * This class is used to save lti::image or lti::channel objects 00437 * in files of one of the formats supported by the LTI-Lib. 00438 * 00439 * The save methods will use the extension in the filename to decide 00440 * the format that should be used to save the image. 00441 * 00442 * Three formats are supported: PNG, BMP and JPEG. 00443 * 00444 * Example: 00445 * 00446 * \code 00447 * lti::saveImage saver; // functor to save images 00448 * lti::image img; // the image to be saved 00449 * 00450 * // ... create an image somehow ... 00451 * 00452 * saver.save("myimage.png",img); // save as PNG 00453 * saver.save("myimage.bmp",img); // save as BMP 00454 * saver.save("myimage.jpg",img); // save as JPEG 00455 * \endcode 00456 * 00457 * The valid extensions are case insensitive matches for .jpg, .jpeg, .png 00458 * and .bmp. 00459 */ 00460 class saveImage : public ioImage { 00461 public: 00462 /** 00463 * default constructor 00464 */ 00465 saveImage(); 00466 00467 /** 00468 * destructor 00469 */ 00470 ~saveImage() {}; 00471 00472 /** 00473 * returns the name of this type 00474 */ 00475 virtual const char* getTypeName() const; 00476 00477 /** 00478 * save image. 00479 * @param theImage the image to be stored. 00480 */ 00481 bool apply(const image& theImage); 00482 00483 /** 00484 * save float channel as a gray-valued image. 00485 * The name of the file is taken from the parameters. 00486 * @param theChannel channel to be stored. 00487 * This channel must contain values between 00488 * 0.0f and 1.0f. 00489 * @return true if successful, false otherwise 00490 */ 00491 bool apply(const channel& theChannel); 00492 00493 /** 00494 * save 8-bit channel as a gray-valued bitmap or as an indexed image. 00495 * The name of the file is taken from the parameters. 00496 * @param theChannel the channel to be saved 00497 * @param colors if a color palette is given, <code>theChannel</code> 00498 * will be considered to contain indices to this palette. 00499 * If nothing (or an empty palette) is given, 00500 * <code>theChannel</code> will be considered as a gray 00501 * valued channel. 00502 * @return true if successful, false otherwise 00503 */ 00504 bool apply(const channel8& theChannel, 00505 const lti::palette& colors=emptyPalette); 00506 00507 /** 00508 * This will save an image as a 24 bit RGB bitmap image. 00509 * 00510 * @param filename the name for the image file. Its extention 00511 * will determine the format to be used. The valid 00512 * extensions are case insensitive matches for .jpg, .jpeg, 00513 * .png * and .bmp. 00514 * @param theImage the image to be written. 00515 * @return true if successful, false otherwise 00516 */ 00517 bool save(const std::string& filename, 00518 const image& theImage); 00519 00520 /** 00521 * This will save a channel8 as an 8 bit RGB bitmap image 00522 * 00523 * @param filename the name for the image file. Its extention 00524 * will determine the format to be used. The valid 00525 * extensions are case insensitive matches for .jpg, .jpeg, 00526 * .png * and .bmp. 00527 * @param theChannel the channel to be save 00528 * @param colors the palette to be used 00529 * (see apply(const channel& theChannel) for details) 00530 */ 00531 bool save(const std::string& filename, 00532 const channel8& theChannel, 00533 const lti::palette& colors=emptyPalette); 00534 00535 /** 00536 * This will save a channel as an 8 bit RGB bitmap image. 00537 * 00538 * The values of the channel must be between 0.0f and 1.0f! 00539 * 00540 * @param filename the name for the image file. Its extention 00541 * will determine the format to be used. The valid 00542 * extensions are case insensitive matches for .jpg, .jpeg, 00543 * .png * and .bmp. 00544 * @param theChannel the channel to be save 00545 */ 00546 bool save(const std::string& filename, 00547 const channel& theChannel); 00548 00549 /** 00550 * returns a pointer to a clone of the functor. 00551 */ 00552 virtual functor* clone() const; 00553 00554 protected: 00555 /** 00556 * functor to save BMP images 00557 */ 00558 lti::saveBMP saverBMP; 00559 00560 #ifdef LTI_HAVE_JPEG 00561 /** 00562 * functor to save JPEG images 00563 */ 00564 lti::saveJPEG saverJPEG; 00565 #endif 00566 00567 #ifdef LTI_HAVE_PNG 00568 /** 00569 * functor to save PNG images 00570 */ 00571 lti::savePNG saverPNG; 00572 #endif 00573 00574 }; 00575 } //namespace lti 00576 00577 #endif