last update 20 Sep 2009 |
00001 /* 00002 * Copyright (C) 2006 00003 * Peter Doerfler 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 cvrLoadImageList.h 00043 * Contains the class cvr::loadImageList, which has still 00044 * to be documented by Peter Doerfler 00045 * \author Peter Doerfler 00046 * \date 10.2.2006 00047 * 00048 * revisions ..: $Id: cvrLoadImageList.h,v 1.4 2007/10/06 00:22:24 alvarado Exp $ 00049 */ 00050 00051 #ifndef _CVR_LOAD_IMAGE_LIST_H_ 00052 #define _CVR_LOAD_IMAGE_LIST_H_ 00053 00054 #include "cvrObject.h" 00055 #include "cvrParametersManager.h" 00056 #include "cvrStatus.h" 00057 00058 #include "cvrMatrix.h" 00059 #include "cvrImage.h" 00060 00061 #include "cvrIOImage.h" 00062 #include <list> 00063 #include <vector> 00064 #include <string> 00065 00066 namespace cvr { 00067 00068 /** 00069 * This class allows convenient loading of a set of images. It can be 00070 * supplied with a directory, a text file containing image filenames, or 00071 * forward iterators to strings. To be consistent over different operating 00072 * systems the filenames are sorted alphabetically. 00073 * 00074 * Using this class is somewhat similar to using an iterator. However, to 00075 * make sure no copying is involved in loading the images the destination 00076 * objects are arguments to the load() member functions instead of 00077 * dereferencing an iterator. The following example shows how to use the 00078 * class: 00079 * 00080 * \code 00081 * loadImageList loader; 00082 * loader.useDirectory("/home/foo/bar"); 00083 * viewer2D viewer; 00084 * viewer2D::interaction action; 00085 * image img; 00086 * while (loader.hasNext()) { 00087 * loader.load(img); 00088 * viewer.show(img); 00089 * viewer.waitKey(action); 00090 * } 00091 * \endcode 00092 * 00093 * The example first finds all image files in the directory 00094 * /home/foo/bar. Then it shows them consecutively, each image until a key 00095 * is pressed. 00096 * 00097 * There are also \c load() member functions that load pointers to all 00098 * images at once into a std::vector. This can be useful e.g. if the lag of 00099 * loading the images can't be tolerated in latter processing. \b NOTE that 00100 * the user is responsible for deleting the allocated memory. Not using this 00101 * way of memory managment can result in excessive copying of the images. 00102 * 00103 * There are also member functions to retrieve the name of the next image 00104 * before actually loading it and to retrieve all filenames. 00105 * 00106 * You can confine the image filetype you want to load by setting 00107 * parameters::fileType to something other than "ALL". Also you can 00108 * configure via continueOnError that errors while loading a file should be 00109 * ignored and the next valid image should be returned instead. 00110 * 00111 * @see loadImageList::parameters. 00112 * 00113 * @ingroup IOImage 00114 */ 00115 class loadImageList 00116 : public object, public parametersManager, public status { 00117 public: 00118 /** 00119 * The parameters for the class loadImageList 00120 */ 00121 class parameters : public parametersManager::parameters { 00122 public: 00123 /** 00124 * Default constructor 00125 */ 00126 parameters(); 00127 00128 /** 00129 * Copy constructor 00130 * @param other the parameters object to be copied 00131 */ 00132 parameters(const parameters& other); 00133 00134 /** 00135 * Destructor 00136 */ 00137 ~parameters(); 00138 00139 /** 00140 * Copy the contents of a parameters object 00141 * @param other the parameters object to be copied 00142 * @return a reference to this parameters object 00143 */ 00144 parameters& copy(const parameters& other); 00145 00146 /** 00147 * Copy the contents of a parameters object 00148 * @param other the parameters object to be copied 00149 * @return a reference to this parameters object 00150 */ 00151 parameters& operator=(const parameters& other); 00152 00153 /** 00154 * Returns the complete name of the parameters class. 00155 */ 00156 virtual const std::string& name() const; 00157 00158 /** 00159 * Returns a pointer to a clone of the parameters. 00160 */ 00161 virtual parameters* clone() const; 00162 00163 /** 00164 * Returns a pointer to a new instance of the parameters. 00165 */ 00166 virtual parameters* newInstance() const; 00167 00168 /** 00169 * Write the parameters in the given ioHandler 00170 * @param handler the ioHandler to be used 00171 * @param complete if true (the default) the enclosing begin/end will 00172 * be also written, otherwise only the data block will be written. 00173 * @return true if write was successful 00174 */ 00175 virtual bool write(ioHandler& handler,const bool complete=true) const; 00176 00177 /** 00178 * Read the parameters from 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 read(ioHandler& handler,const bool complete=true); 00185 00186 // ------------------------------------------------ 00187 // the parameters 00188 // ------------------------------------------------ 00189 00190 /** 00191 * If true errors on reading an image file are ignored and the next 00192 * image is tried. This can be useful for batch processing. 00193 * 00194 * Default: false 00195 */ 00196 bool continueOnError; 00197 00198 /** 00199 * Determines the filetypes that should be loaded. Available filetypes 00200 * can be found by looking at the subclasses of 00201 * cvr::IOImageInterface. Just use the typical three letter 00202 * abbreviations, e.g. PNG for portable network graphics. You can also 00203 * use ALL to load all available file types. 00204 * 00205 * Default: "ALL" 00206 */ 00207 std::string fileType; 00208 00209 }; 00210 00211 /** 00212 * Default constructor 00213 */ 00214 loadImageList(); 00215 00216 /** 00217 * Construct using the given parameters 00218 */ 00219 loadImageList(const parameters& par); 00220 00221 /** 00222 * Copy constructor 00223 * @param other the object to be copied 00224 */ 00225 loadImageList(const loadImageList& other); 00226 00227 /** 00228 * Destructor 00229 */ 00230 virtual ~loadImageList(); 00231 00232 /** 00233 * Copy data of "other" functor. 00234 * @param other the functor to be copied 00235 * @return a reference to this functor object 00236 */ 00237 loadImageList& copy(const loadImageList& other); 00238 00239 /** 00240 * Alias for copy member 00241 * @param other the functor to be copied 00242 * @return a reference to this functor object 00243 */ 00244 loadImageList& operator=(const loadImageList& other); 00245 00246 /** 00247 * Returns the complete name of the functor class 00248 */ 00249 virtual const std::string& name() const; 00250 00251 /** 00252 * Returns a pointer to a clone of this functor. 00253 */ 00254 virtual loadImageList* clone() const; 00255 00256 /** 00257 * Returns a pointer to a new instance of this functor. 00258 */ 00259 virtual loadImageList* newInstance() const; 00260 00261 /** 00262 * Returns used parameters 00263 */ 00264 const parameters& getParameters() const; 00265 00266 /** 00267 * Makes sure the given parameters esp. fileType are valid. 00268 */ 00269 virtual bool updateParameters(); 00270 00271 /** 00272 * @name Flow control. 00273 */ 00274 //@{ 00275 00276 /** 00277 * Compute the total size of images available since the last use(). 00278 * 00279 * Please note that the size is computed, and therefore it is 00280 * somehow slow. Never use this method to check if all images 00281 * have been loaded; use hasNext() instead. 00282 */ 00283 int computeSize() const; 00284 00285 /** 00286 * After calling rewind the next image loaded by a single image load() 00287 * function will be the first one in the list. 00288 */ 00289 void rewind(); 00290 00291 /** 00292 * Returns true if there are more images to be loaded on the list. 00293 */ 00294 bool hasNext() const; 00295 00296 /** 00297 * Skips the current image filename on the list and without actually 00298 * loading it. For example if you call rewind() and then skip() then the 00299 * next load() will load the second image file on the list. 00300 * 00301 * \warning Note that analogous to load it is your responsibility to check 00302 * whether the list hasNext(). 00303 */ 00304 void skip(); 00305 00306 //@} 00307 00308 /** 00309 * @name Set the source for the filenames 00310 */ 00311 //@{ 00312 00313 /** 00314 * Use all image filenames as specified in the parameters that can be 00315 * found in \p dirname. 00316 */ 00317 bool useDirectory(const std::string& dirname); 00318 00319 /** 00320 * Use all image filenames as specified in the parameters that are present 00321 * in \p filename. 00322 * 00323 * \b Note that lines starting with a semi-colon are regarded as comment 00324 * lines and that the image filenames contained in \p filename are \b NOT 00325 * sorted alphabetically. 00326 */ 00327 bool useFileList(const std::string& filename); 00328 00329 /** 00330 * Use the valid image filenames contained in the range \p first to \p 00331 * last: [first; last). The iterators must be valid input iterators and 00332 * dereference to std::string. 00333 * 00334 * \b Note that the image filenames contained in the range are \b NOT 00335 * sorted alphabetically. 00336 */ 00337 template <typename InputIterator> 00338 bool useRange(InputIterator first, InputIterator last); 00339 00340 //@} 00341 00342 /** 00343 * @name Load single image 00344 * 00345 * Get the next image/channel etc. from the given list of filenames. 00346 */ 00347 //@{ 00348 00349 /** 00350 * Returns the filename for the image that will be loaded by the \b next 00351 * call to load(). 00352 * 00353 * \b Note that if parameters::continueOnError is true the filename 00354 * returned by this member function might actually not be loaded by the 00355 * next load() call in case an error occurs. Consider using 00356 * getLastLoadedFilename() instead. 00357 */ 00358 const std::string& getNextFilename() const; 00359 00360 /** 00361 * Returns the name of the image file that was loaded by the last call to 00362 * one of the single image load() member functions. This member function 00363 * is a bit slower than getNextFilename() but is recommended of 00364 * parameters::continueOnError is true. 00365 */ 00366 const std::string& getLastLoadedFilename() const; 00367 00368 /** 00369 * Load file as a color image. 00370 * 00371 * @param img the RGB image 00372 */ 00373 bool load(image& img); 00374 00375 /** 00376 * Load a channel8 as a label mask with its corresponding palette. 00377 * 00378 * If the file containes a color image, then the functor has to quantize 00379 * the colors and returns the quantized table. 00380 * 00381 * @param chnl index channel 00382 * @param pal corresponding palette 00383 */ 00384 bool load(matrix<ubyte>& chnl,palette& pal); 00385 00386 /** 00387 * Load the contents as a gray channel. 00388 * 00389 * Default implementation just discards the palette of 00390 * load(matrix<ubyte>&, palette&) 00391 * 00392 * @param chnl 8-bit gray channel8 00393 */ 00394 bool load(matrix<ubyte>& chnl); 00395 00396 /** 00397 * Load a floating point channel. Not many file formats support floating 00398 * point channels, so the default implementation just casts a channel8 into 00399 * the float. 00400 * 00401 * @param chnl floating point gray channel 00402 */ 00403 bool load(matrix<float>& chnl); 00404 00405 /** 00406 * Load the contents as a integer mask 00407 * 00408 * Default implementation returns the casting of the channel8 related 00409 * method. 00410 * 00411 * @param chnl integer index channel 00412 * @param pal corresponding palette 00413 */ 00414 bool load(matrix<int32>& chnl,palette& pal); 00415 00416 /** 00417 * Load the contents as a integer mask 00418 * 00419 * Default implementation discards the palette of the other matrix<int32> 00420 * method. 00421 * 00422 * @param chnl integer mask 00423 */ 00424 bool load(matrix<int32>& chnl); 00425 //@} 00426 00427 /** 00428 * @name Load all images/channels etc. 00429 * 00430 * Load all images at once from the given list of filenames. 00431 */ 00432 //@{ 00433 00434 /** 00435 * Puts the filenames of all files into \p filenames in the same order 00436 * that they are in the load() member functions for the complete set. 00437 */ 00438 void getFilenames(std::vector<std::string>& filenames) const; 00439 00440 /** 00441 * Load all files as a color image. 00442 * 00443 * @param img the RGB image 00444 */ 00445 bool load(std::vector<image*>& img); 00446 00447 /** 00448 * Load a channel8 as a label mask with its corresponding palette. 00449 * 00450 * If the file containes a color image, then the functor has to quantize 00451 * the colors and returnes the quantized table. 00452 * 00453 * @param chnl index channel 00454 * @param pal corresponding palette 00455 */ 00456 bool load(std::vector<matrix<ubyte>*>& chnl, 00457 std::vector<palette*>& pal); 00458 00459 /** 00460 * Load the contents as a gray channel. 00461 * 00462 * Default implementation just discards the palette of 00463 * load(matrix<ubyte>&, palette&) 00464 * 00465 * @param chnl 8-bit gray channel8 00466 */ 00467 bool load(std::vector<matrix<ubyte>*>& chnl); 00468 00469 /** 00470 * Load a floating point channel. Not many file formats support floating 00471 * point channels, so the default implementation just casts a channel8 into 00472 * the float. 00473 * 00474 * @param chnl floating point gray channel 00475 */ 00476 bool load(std::vector<matrix<float>*>& chnl); 00477 00478 /** 00479 * Load the contents as a integer mask 00480 * 00481 * Default implementation returns the casting of the channel8 related 00482 * method. 00483 * 00484 * @param chnl integer index channel 00485 * @param pal corresponding palette 00486 */ 00487 bool load(std::vector<matrix<int32>*>& chnl, 00488 std::vector<palette*>& pal); 00489 00490 /** 00491 * Load the contents as a integer mask 00492 * 00493 * Default implementation discards the palette of the other matrix<int32> 00494 * method. 00495 * 00496 * @param chnl integer mask 00497 */ 00498 bool load(std::vector<matrix<int32>*>& chnl); 00499 //@} 00500 00501 protected: 00502 /** 00503 * IOImage object used for actually loading the images and for checking 00504 * valid filetypes when "ALL" is selected 00505 */ 00506 ioImage loader_; 00507 00508 /** 00509 * This list stores all valid image filenames to be loaded. 00510 */ 00511 std::list<std::string> filenames_; 00512 00513 /** 00514 * Iterator pointing to the next filename to load 00515 */ 00516 std::list<std::string>::const_iterator it_; 00517 00518 /** 00519 * If true all filetypes supported by cvr::IOImage should be considered. 00520 */ 00521 bool loadAllImageTypes_; 00522 00523 /** 00524 * Correct file extension if only one type is to be considered. 00525 */ 00526 std::string fileExt_; 00527 00528 00529 /** 00530 * checks whether a filename has the extension specified in the parameters 00531 */ 00532 bool hasValidFileExtension(const std::string& filename) const; 00533 00534 /** 00535 * @name Helper functions for loading 00536 */ 00537 //@{ 00538 00539 /** 00540 * load an image of type T 00541 */ 00542 template <class T> 00543 bool loadHelp(T& dest); 00544 00545 /** 00546 * load a channel of type T and a corresponding palette 00547 */ 00548 template <class T> 00549 bool loadHelp(T& dest, palette& pal); 00550 00551 /** 00552 * load all images of type T 00553 */ 00554 template <class T> 00555 bool loadHelp(std::vector<T*>& dest); 00556 00557 /** 00558 * load all channels of type T and corresponding palettes 00559 */ 00560 template <class T> 00561 bool loadHelp(std::vector<T*>& dest, std::vector<palette*>& pal); 00562 00563 //@} 00564 00565 private: 00566 00567 /** 00568 * dummy string needed to return empty string in getLastLoadedFilename() 00569 */ 00570 const std::string emptyString_; 00571 }; 00572 } 00573 00574 #include "cvrLoadImageList_template.h" 00575 00576 #endif 00577