CVR-Lib last update 20 Sep 2009

cvrIOImage.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1998-2006
00003  * Electronics Engineering School, ITCR, Costa Rica
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   cvrIOImage.h
00043  *         Parent class of all functors that write to or read from a file as
00044  *         its main task.
00045  * \author Pablo Alvarado
00046  * \date   11.01.2006
00047  *
00048  * $Id: cvrIOImage.h,v 1.3 2007/04/08 03:12:38 alvarado Exp $
00049  */
00050 
00051 #ifndef _CVR_IO_IMAGE_H_
00052 #define _CVR_IO_IMAGE_H_
00053 
00054 #include "cvrIOImageInterface.h"
00055 #include "cvrMatrix.h"
00056 #include "cvrImage.h"
00057 #include <map>
00058 #include <string>
00059 
00060 namespace cvr {
00061   /**
00062    * Class to read/write image files.
00063    *
00064    * This class makes use of all its sibling classes to save or load image
00065    * files in all supported formats (bmp, jpg, png).
00066    *
00067    * Example:
00068    * \code
00069    * cvr::ioImage imgFiler; // create the object to save/load images
00070    * cvr::image img(256,256,cvr::White); // create a white image 256x256
00071    * imgFiler.save("myImage.png",img);   // save image as png file
00072    *
00073    * cvr::image other;                   // another variable
00074    * imgFiler.load("myImage.png",other); // load the recently created file.
00075    * \endcode
00076    */
00077   class ioImage : public ioImageInterface {
00078   public:
00079 
00080     /**
00081      * Default constructor
00082      */
00083     ioImage();
00084 
00085     /**
00086      * Copy constructor
00087      */
00088     ioImage(const ioImage& other);
00089 
00090     /**
00091      * destructor
00092      */
00093     virtual ~ioImage();
00094 
00095     /**
00096      * returns current parameters.
00097      */
00098     const parameters& getParameters() const;
00099 
00100     /**
00101      * Returns the name of this class
00102      */
00103     virtual const std::string& name() const;
00104 
00105     /**
00106      * Returns a pointer to a clone of the functor.
00107      */
00108     virtual ioImage* clone() const;
00109 
00110     /**
00111      * Returns a pointer to a new instance of this functor.
00112      */
00113     virtual ioImage* newInstance() const;
00114 
00115     /**
00116      * Copy operator
00117      */
00118     ioImage& copy(const ioImage& other);
00119 
00120     /**
00121      * Copy operator
00122      */
00123     ioImage& operator=(const ioImage& other);
00124 
00125     /**
00126      * @name Loading images and channels
00127      */
00128     //@{
00129     /**
00130      * Load file as a color image.
00131      */
00132     virtual bool load(const std::string& filename,
00133                       image& img);
00134 
00135     /**
00136      * Load a channel8 as a label mask with its corresponding palette.
00137      *
00138      * If the file contained a color image, then the functor has to quantize
00139      * the colors and returned the quantized table.
00140      */
00141     virtual bool load(const std::string& filename,
00142                       matrix<ubyte>& chnl,
00143                       palette& pal);
00144 
00145     /**
00146      * Load the contents as a gray channel.
00147      *
00148      * Default implementation just discards the palette of
00149      * load(const std::string&,matrix<ubyte>&,palette&)
00150      */
00151     virtual bool load(const std::string& filename,
00152                       matrix<ubyte>& chnl);
00153 
00154 
00155     /**
00156      * Load a floating point channel.  Not many file formats support floating
00157      * point channels, so the default implementation just casts a channel8 into
00158      * the float.
00159      */
00160     virtual bool load(const std::string& filename,
00161                       matrix<float>& chnl);
00162 
00163     /**
00164      * Load the contents as a integer mask
00165      *
00166      * Default implementation returns the casting of the channel8 related
00167      * method.
00168      */
00169     virtual bool load(const std::string& filename,
00170                       matrix<int32>& chnl,
00171                       palette& pal);
00172 
00173     /**
00174      * Load the contents as a integer mask
00175      *
00176      * Default implementation discards the palette of the other matrix<int32>
00177      * method.
00178      */
00179     virtual bool load(const std::string& filename,
00180                       matrix<int32>& chnl);
00181 
00182     /**
00183      * Check the file header for common information.
00184      *
00185      * All classes implementing this interface usually provide additional
00186      * methods to check other options of the specific file formats, or overload
00187      * the headerInformation to include additional items.
00188      */
00189     virtual bool checkHeader(const std::string& filename,
00190                              headerInformation& info);
00191     //@}
00192 
00193 
00194     /**
00195      * @name Saving images and channels
00196      */
00197     //@{
00198     /**
00199      * Save file as a color image.
00200      */
00201     virtual bool save(const std::string& filename,
00202                       const image& img);
00203 
00204     /**
00205      * Save a channel8 as a label mask with its corresponding palette.
00206      *
00207      * If the file contained a color image, then the functor has to quantize
00208      * the colors and returned the quantized table.
00209      */
00210     virtual bool save(const std::string& filename,
00211                       const matrix<ubyte>& chnl,
00212                       const palette& pal);
00213 
00214     /**
00215      * Save the contents as a gray channel.
00216      *
00217      * Default implementation just discards the palette of
00218      * save(const std::string&,matrix<ubyte>&,palette&)
00219      */
00220     virtual bool save(const std::string& filename,
00221                       const matrix<ubyte>& chnl);
00222 
00223 
00224     /**
00225      * Save a floating point channel.
00226      *
00227      * Not many file formats support floating point channels, so the default
00228      * implementation just casts the channel to a channel8, which is saved.
00229      * You normally lose precision.
00230      */
00231     virtual bool save(const std::string& filename,
00232                       const matrix<float>& chnl);
00233 
00234     /**
00235      * Save the contents as a integer mask.
00236      *
00237      * Default implementation returns the casting of the channel8 related
00238      * method.
00239      */
00240     virtual bool save(const std::string& filename,
00241                       const matrix<int32>& chnl,
00242                       const palette& pal);
00243 
00244     /**
00245      * Save the contents as a integer mask.
00246      *
00247      * Default implementation discards the palette of the other matrix<int32>
00248      * method.
00249      */
00250     virtual bool save(const std::string& filename,
00251                       const matrix<int32>& chnl);
00252 
00253     //@}
00254 
00255     /**
00256      * Returns true if the given extension \p ext is supported (i.e. can be
00257      * loaded/saved). The argument is not case sensitive.
00258      *
00259      * This function is dedicated for more sophisticated IO interfaces that
00260      * use ioImage to actually load different image types.
00261      */
00262     bool isSupportedExtension(const std::string& ext) const;
00263 
00264 
00265   protected:
00266     /**
00267      * Local object repository
00268      */
00269     std::map<std::string,ioImageInterface*> ioObjects_;
00270 
00271     /**
00272      * Build repository
00273      *
00274      * Use the factory to create the repository
00275      */
00276     bool buildRepository();
00277 
00278     /**
00279      * Clear repository
00280      *
00281      * Remove all objects in the repository
00282      */
00283     bool clearRepository();
00284 
00285     /**
00286      * Get file extension
00287      */
00288     std::string getFileExtension(const std::string& filename) const;
00289 
00290     /**
00291      * Search an appropriate functor for the given file extension, or
00292      * null if there is nothing available.
00293      */
00294     ioImageInterface* get(const std::string& ext);
00295 
00296 
00297   };
00298 
00299 }
00300 
00301 #endif

Generated on Sun Sep 20 22:07:59 2009 for CVR-Lib by Doxygen 1.5.8