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 /*-------------------------------------------------------------------- 00025 * project ....: LTI-Lib: Image Processing and Computer Vision Library 00026 * file .......: ltiLoadImageList.h 00027 * authors ....: Peter Doerfler 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 22.11.2002 00030 * revisions ..: $Id: ltiLoadImageList.h,v 1.13 2007/01/10 02:26:14 alvarado Exp $ 00031 */ 00032 00033 #ifndef _LTI_LOAD_IMAGE_LIST_H_ 00034 #define _LTI_LOAD_IMAGE_LIST_H_ 00035 00036 00037 #include "ltiImage.h" 00038 #include "ltiALLFunctor.h" 00039 #include <list> 00040 00041 00042 namespace lti { 00043 /** 00044 * This functor loads images given either in a std::list of 00045 * strings, a special file or a whole directory: Each apply call 00046 * loads the next image from the list. 00047 * 00048 * The source of the list of image filenames can be chosen via the 00049 * parameters parameters::mode. All images which are of types PNG, 00050 * BMP or JPEG are loaded. The default is a textfile with a list of 00051 * filenames: parameters::listFileName contains the name of the 00052 * file (must be set) and parameters::mode is parameters::ListFileName. 00053 * \b Note: see parameters::mode for corresponding parameter. 00054 * 00055 * Setting the parameter parameters::skipErrors to true makes the 00056 * functor error tolerant. I.e. if an image in the list can not be 00057 * loaded, false is returned and the image name is in the status 00058 * string. However, the next valid image in the list is 00059 * returned. If the parameter is false, false is returned and no 00060 * image is loaded. Default is true. 00061 */ 00062 class loadImageList : public ioFunctor { 00063 public: 00064 /** 00065 * the parameters for the class loadImageList 00066 */ 00067 class parameters : public ioFunctor::parameters { 00068 public: 00069 00070 /** 00071 * Operation modes 00072 */ 00073 enum eOperationMode { 00074 ListFileName, /*!< read all images given in a textfile, 00075 see listFileName*/ 00076 DirName, /*!< read all image in a directory, see dirName */ 00077 NameList /*!< read all images in the list, see nameList */ 00078 }; 00079 00080 /** 00081 * default constructor 00082 */ 00083 parameters(); 00084 00085 /** 00086 * copy constructor 00087 * @param other the parameters object to be copied 00088 */ 00089 parameters(const parameters& other); 00090 00091 /** 00092 * destructor 00093 */ 00094 ~parameters(); 00095 00096 /** 00097 * returns name of this type 00098 */ 00099 const char* getTypeName() const; 00100 00101 /** 00102 * copy the contents of a parameters object 00103 * @param other the parameters object to be copied 00104 * @return a reference to this parameters object 00105 */ 00106 parameters& copy(const parameters& other); 00107 00108 /** 00109 * copy the contents of a parameters object 00110 * @param other the parameters object to be copied 00111 * @return a reference to this parameters object 00112 */ 00113 parameters& operator=(const parameters& other); 00114 00115 00116 /** 00117 * returns a pointer to a clone of the parameters 00118 */ 00119 virtual functor::parameters* clone() const; 00120 00121 /** 00122 * write the parameters in the given ioHandler 00123 * @param handler the ioHandler to be used 00124 * @param complete if true (the default) the enclosing begin/end will 00125 * be also written, otherwise only the data block will be written. 00126 * @return true if write was successful 00127 */ 00128 virtual bool write(ioHandler& handler,const bool complete=true) const; 00129 00130 /** 00131 * read the parameters from the given ioHandler 00132 * @param handler the ioHandler to be used 00133 * @param complete if true (the default) the enclosing begin/end will 00134 * be also written, otherwise only the data block will be written. 00135 * @return true if write was successful 00136 */ 00137 virtual bool read(ioHandler& handler,const bool complete=true); 00138 00139 # ifdef _LTI_MSC_6 00140 /** 00141 * this function is required by MSVC only, as a workaround for a 00142 * very awful bug, which exists since MSVC V.4.0, and still by 00143 * V.6.0 with all bugfixes (so called "service packs") remains 00144 * there... This method is also public due to another bug, so please 00145 * NEVER EVER call this method directly: use read() instead 00146 */ 00147 bool readMS(ioHandler& handler,const bool complete=true); 00148 00149 /** 00150 * this function is required by MSVC only, as a workaround for a 00151 * very awful bug, which exists since MSVC V.4.0, and still by 00152 * V.6.0 with all bugfixes (so called "service packs") remains 00153 * there... This method is also public due to another bug, so please 00154 * NEVER EVER call this method directly: use write() instead 00155 */ 00156 bool writeMS(ioHandler& handler,const bool complete=true) const; 00157 # endif 00158 00159 // ------------------------------------------------ 00160 // the parameters 00161 // ------------------------------------------------ 00162 00163 /** 00164 * Determines where the functor expects to find the filenames of 00165 * the images to load. There are three possibilities: 00166 * - ListFileName: The string listFileName contains the name of 00167 * a textfile which contains the filenames of images to be 00168 * selected for later loading. See listFileName for the format 00169 * of the textfile 00170 * - DirName: The string dirName must contain the the path of a 00171 * directory which contains image files. All files ending in 00172 * '.jpg', '.jpeg', '.png' or '.bmp' are selected for later 00173 * loading no matter whether upper or lower case. 00174 * - NameList: nameList is a list of strings, where each string 00175 * is a filename of an image. 00176 */ 00177 eOperationMode mode; 00178 00179 /** 00180 * Name of a directory containing images, e.g. "/home/fooman/myImages/" 00181 */ 00182 std::string dirName; 00183 00184 /** 00185 * Filename of a text file containing filenames of images. Each 00186 * row contains one filename only. The comment character is the 00187 * semi-colon ';'. All text behind a semi-colon is regarded as 00188 * comment. Such a file could look like this: 00189 * 00190 * \code 00191 * ; some images to read 00192 * /home/fooman/pics1/image1.png 00193 * ;/home/fooman/pics1/image2.png 00194 * /home/fooman/pics2/2.BMP 00195 * /home/fooman/pics1/image1.jpg; another image 00196 * \endcode 00197 * 00198 * The functor would read the images: 00199 * - /home/fooman/pics1/image1.png 00200 * - /home/fooman/pics2/2.BMP 00201 * - /home/fooman/pics1/image1.jpg 00202 */ 00203 std::string listFileName; 00204 00205 /** 00206 * Each entry in the list contains the full filename of an image. 00207 */ 00208 std::list<std::string> nameList; 00209 00210 /** 00211 * If reading an image fails and \a skipErrors is: 00212 * - \a true: The next available filename is tried. The apply 00213 * method returns false and sets the status string. 00214 * - \a false: The image is empty, the apply method returns 00215 * false and the status string is set. 00216 * 00217 * The default is \a false; 00218 */ 00219 bool skipErrors; 00220 }; 00221 00222 /** 00223 * default constructor 00224 */ 00225 loadImageList(); 00226 00227 /** 00228 * Construct a functor using the given parameters 00229 */ 00230 loadImageList(const parameters& par); 00231 00232 /** 00233 * copy constructor 00234 * @param other the object to be copied 00235 */ 00236 loadImageList(const loadImageList& other); 00237 00238 /** 00239 * Sets one of two \a modes: parameters::ListFileName or 00240 * parameters::DirName and the corresponding \a name of the file 00241 * or directory in the parameters. Default is the filename of a textfile 00242 * containing image filenames. 00243 * 00244 * See also loadImageList(const std::list<std::string>&), 00245 * use(const std::string&, parameters::eOperationMode), 00246 * use(const std::list<std::string>&). 00247 * 00248 * @param name name of a file containing image filenames or of a directory 00249 * @param mode parameters::ListFileName or parameters::DirName. 00250 */ 00251 loadImageList(const std::string& name, 00252 parameters::eOperationMode mode=parameters::ListFileName); 00253 00254 /** 00255 * Sets parameters::mode to parameters::NameList and uses the list 00256 * \a names for loading the files. 00257 * 00258 * See also loadImageList(const std::string&, parameters::eOperationMode), 00259 * use(const std::string&, parameters::eOperationMode), 00260 * use(const std::list<std::string>&). 00261 * 00262 * @param names list of image filenames. 00263 */ 00264 loadImageList(const std::list<std::string>& names); 00265 00266 /** 00267 * destructor 00268 */ 00269 virtual ~loadImageList(); 00270 00271 /** 00272 * returns the name of this type ("loadImageList") 00273 */ 00274 virtual const char* getTypeName() const; 00275 00276 /** 00277 * copy data of "other" functor. 00278 * @param other the functor to be copied 00279 * @return a reference to this functor object 00280 */ 00281 loadImageList& copy(const loadImageList& other); 00282 00283 /** 00284 * alias for copy member 00285 * @param other the functor to be copied 00286 * @return a reference to this functor object 00287 */ 00288 loadImageList& operator=(const loadImageList& other); 00289 00290 /** 00291 * returns a pointer to a clone of this functor. 00292 */ 00293 virtual functor* clone() const; 00294 00295 /** 00296 * returns used parameters 00297 */ 00298 const parameters& getParameters() const; 00299 00300 00301 /** 00302 * Set the parameters and loads the list of filenames given in the 00303 * parameters into the functor. Returns \a false if cloning of 00304 * parameters failed or the source of the image files could not be 00305 * found. The functor is reset by setting new parameters. 00306 */ 00307 virtual bool updateParameters(); 00308 00309 /** 00310 * Returns true if more images are available for loading. 00311 */ 00312 bool hasNext() const; 00313 00314 /** 00315 * Returns the filename of the image which will be loaded by the 00316 * next call to an apply method. 00317 */ 00318 const std::string& getNextFileName() const; 00319 00320 /** 00321 * Starts over from the beginning of the list of image 00322 * filenames. However, the information is not reread from the 00323 * parameters. This only happens when setParameters(), use() or a 00324 * constructor are called. 00325 */ 00326 void rewind(); 00327 00328 /** 00329 * Returns the number of image filenames the functor knows. Note 00330 * that this is not necessarily the number of images that will be 00331 * loaded, since loading might fail for some images. 00332 */ 00333 int size() const; 00334 00335 /** 00336 * Sets one of two \a modes: parameters::ListFileName or 00337 * parameters::DirName and the corresponding \a name of the file 00338 * or directory in the parameters. Default is the filename of a 00339 * textfile containing image filenames. If \a false is returned, 00340 * there was an error loading the given file. 00341 * 00342 * See also loadImageList(const std::list<std::string>&), 00343 * loadImageList(const std::string&, parameters::eOperationMode), 00344 * use(const std::list<std::string>&). 00345 * 00346 * @param name name of a file containing image filenames or of a directory 00347 * @param mode parameters::ListFileName or parameters::DirName. 00348 * @return false on error 00349 */ 00350 bool use(const std::string& name, 00351 parameters::eOperationMode mode=parameters::ListFileName); 00352 00353 /** 00354 * Sets parameters::mode to parameters::NameList and uses the list 00355 * \a names for loading the files. If \a false is returned an 00356 * error occured. 00357 * 00358 * See also loadImageList(const std::list<std::string>&), 00359 * loadImageList(const std::string&, parameters::eOperationMode), 00360 * use(const std::string&, parameters::eOperationMode) 00361 * 00362 * @param names list of image filenames. 00363 * @return false on error 00364 */ 00365 bool use(const std::list<std::string>& names); 00366 00367 /** 00368 * load the next image from the list given in the parameters, via 00369 * the constructor or by calling use(). 00370 * 00371 * @param theImage the file specified in the parameters will be loaded 00372 * in this image. Note that independently of the sort 00373 * of image in the file, this will always be converted to 00374 * a color lti::image. 00375 * @return false on error 00376 */ 00377 bool apply(image& theImage); 00378 00379 /** 00380 * load the next image from the list given in the parameters, via 00381 * the constructor or by calling use(). 00382 * 00383 * Use this method if you know that the file contains a gray 00384 * valued image. If you try to load a 24-bit image with this 00385 * method, some quantization algorithms will be used to reduce the 00386 * number of colors to 256. 00387 * 00388 * @param theChannel the image on the file will be loaded here 00389 * @param colors theChannel contains just indexes to the pixel values 00390 * in this vector. 00391 * @return true if successful, false otherwise. 00392 */ 00393 bool apply(channel8& theChannel,lti::palette& colors); 00394 00395 /** 00396 * load the next image from the list given in the parameters, via 00397 * the constructor or by calling use(). 00398 * 00399 * Use this method if you know that the file contains a gray 00400 * valued image. If you try to load a 24-bit image with this 00401 * method, the intensity channel is extracted. 00402 * 00403 * @param theChannel the image on the file will be loaded here 00404 * @return true if successful, false otherwise. 00405 */ 00406 bool apply(channel& theChannel); 00407 00408 /** 00409 * load all images from the list given in the parameters, via 00410 * the constructor or by calling use(). 00411 * 00412 * @param theImages all files specified in the parameters will be 00413 * loaded. Note that independently of the kind of images in the 00414 * files, they are always converted to a color lti::image. 00415 * @return false on error 00416 */ 00417 bool apply(std::vector<image>& theImages); 00418 00419 /** 00420 * load all images from the list given in the parameters, via 00421 * the constructor or by calling use(). 00422 * 00423 * Use this method if you know that the file contains a gray 00424 * valued image. If you try to load a 24-bit image with this 00425 * method, some quantization algorithms will be used to reduce the 00426 * number of colors to 256. 00427 * 00428 * @param theChannels all images specified in the parameters 00429 * @param colors theChannels contains just indexes to the pixel values 00430 * in this vector. 00431 * @return false on error 00432 */ 00433 bool apply(std::vector<channel8>& theChannels, 00434 std::vector<lti::palette>& colors); 00435 00436 /** 00437 * load all images from the list given in the parameters, via 00438 * the constructor or by calling use(). 00439 * 00440 * Use this method if you know that the file contains a gray 00441 * valued image. If you try to load a 24-bit image with this 00442 * method, the intensity channel is extracted. 00443 * 00444 * @param theChannels all images specified in the parameters 00445 * @return true if successful, false otherwise. 00446 */ 00447 bool apply(std::vector<channel>& theChannels); 00448 00449 /** 00450 * check the header data of the next image and retrieve the size if the 00451 * filename specifies a valid image file. 00452 * 00453 * @param imageSize returns the size of the image: imageSize.x is the 00454 * number of columns and imageSize.y the number of 00455 * rows. 00456 * @return true if file is ok, false otherwise 00457 */ 00458 bool checkHeader(point& imageSize); 00459 00460 /** 00461 * check the header data of the next image and retrieve the size if the 00462 * filename specifies a valid image file. 00463 * 00464 * @param imageSize returns the size of the image: imageSize.x is the 00465 * number of columns and imageSize.y the numbeer of 00466 * rows. 00467 * @param trueColor true if the image contains 3x256 bits per pixel, 00468 * false, otherwise. If false, you can use the 00469 * methods to read channel8+palette 00470 * @return true if file is ok, false otherwise 00471 */ 00472 bool checkHeader(point& imageSize, 00473 bool& trueColor); 00474 00475 protected: 00476 00477 /** 00478 * internal list of filenames to be loaded 00479 */ 00480 std::list<std::string> fileNames; 00481 00482 /** iterator for fileNames */ 00483 std::list<std::string>::const_iterator iter; 00484 00485 /** empty dummy string */ 00486 const std::string emptyDummy; 00487 00488 /** functor used for loading images */ 00489 loadImage iLoader; 00490 00491 /** 00492 * Analyses the parameters and loads the filenames for the images 00493 * from the appropriate source. If anything goes wrong, the 00494 * statusString is set and false is returned. 00495 */ 00496 bool getImageNames(); 00497 00498 /** 00499 * Reads the textfile \a filename which contains filenames of images. 00500 * Returns false if the file could not be opened. 00501 */ 00502 bool readListFile(const std::string& filename); 00503 00504 /** 00505 * Selects all image filenames in the given directory for 00506 * reading. Returns false if the directory does not exist. 00507 */ 00508 bool readDirectory(const std::string& dirName); 00509 00510 /** return true if the filename has an image extension */ 00511 bool hasImageExtension(std::string& filename); 00512 }; 00513 } 00514 00515 #endif