CVR-Lib last update 20 Sep 2009

cvrIOImageInterface.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   cvrIOImageInterface.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: cvrIOImageInterface.h,v 1.5 2007/10/07 03:17:30 alvarado Exp $
00049  */
00050 
00051 #ifndef _CVR_IO_IMAGE_INTERFACE_H_
00052 #define _CVR_IO_IMAGE_INTERFACE_H_
00053 
00054 #include "cvrParametersManager.h"
00055 #include "cvrMatrix.h"
00056 #include "cvrImage.h"
00057 
00058 namespace cvr {
00059   /**
00060    * Base class to all classes that read from or write to a image file.
00061    *
00062    * It is a very simple class with nothing but an empty definition of a
00063    * parameters class with the attribute "filename".
00064    *
00065    * Note that these classes cannot be considered CVR-Lib functors, as they
00066    * lack the for the later mandatory apply() methods.
00067    *
00068    * \ingroup gInterfaces
00069    */
00070   class ioImageInterface : public object,
00071                            public status,
00072                            public parametersManager {
00073   public:
00074     /**
00075      * ioFunctor parameters class
00076      */
00077     class parameters : public parametersManager::parameters {
00078     public:
00079       /**
00080        * Default constructor
00081        */
00082       parameters();
00083 
00084       /**
00085        * Copy constructor
00086        */
00087       parameters(const parameters& other);
00088 
00089       /**
00090        * Destructor
00091        */
00092       ~parameters();
00093 
00094       /**
00095        * Copy member
00096        */
00097       parameters& copy(const parameters& other);
00098 
00099       /**
00100        * Copy member
00101        */
00102       parameters& operator=(const parameters& other);
00103 
00104       /**
00105        * Returns the name of this class.
00106        */
00107       const std::string& name() const;
00108 
00109       /**
00110        * Returns a pointer to a clone of the parameters
00111        */
00112       virtual parameters* clone() const = 0;
00113 
00114       /**
00115        * Returns a pointer to a new instance of the parameters
00116        */
00117       virtual parameters* newInstance() const = 0;
00118 
00119       /**
00120        * Write the parameters in the given ioHandler
00121        * @param handler the ioHandler to be used
00122        * @param complete if true (the default) the enclosing begin/end will
00123        *        be also written, otherwise only the data block will be written.
00124        * @return true if write was successful
00125        */
00126       virtual bool write(ioHandler& handler,const bool complete=true) const;
00127 
00128       /**
00129        * Write the parameters in the given ioHandler
00130        * @param handler the ioHandler to be used
00131        * @param complete if true (the default) the enclosing begin/end will
00132        *        be also written, otherwise only the data block will be written.
00133        * @return true if write was successful
00134        */
00135       virtual bool read(ioHandler& handler,const bool complete=true);
00136 
00137     };
00138 
00139     /**
00140      * Common header information block.
00141      *
00142      * All inherited classes have to provide a way to get information from the
00143      * file without reading it completely.  The information can vary among the
00144      * file formats, but usually this common data set can be obtained.
00145      */
00146     class headerInformation : public ioObject {
00147     public:
00148       /**
00149        * Default constructor
00150        */
00151       headerInformation();
00152 
00153       /**
00154        * Default constructor
00155        */
00156       headerInformation(const headerInformation& other);
00157 
00158       /**
00159        * Returns the name of this class
00160        */
00161       virtual const std::string& name() const;
00162 
00163       /**
00164        * Returns a copy of this object
00165        */
00166       virtual headerInformation* clone() const;
00167 
00168       /**
00169        * Returns a copy of this object
00170        */
00171       virtual headerInformation* newInstance() const;
00172 
00173       /**
00174        * Copy
00175        */
00176       headerInformation& copy(const headerInformation& other);
00177 
00178       /**
00179        * Copy
00180        */
00181       headerInformation& operator=(const headerInformation& other);
00182 
00183       /**
00184        * Write the parameters in the given ioHandler
00185        *
00186        * @param handler the ioHandler to be used
00187        * @param complete if true (the default) the enclosing begin/end will
00188        *        be also written, otherwise only the data block will be written.
00189        * @return true if write was successful
00190        *
00191        * This method has to be overloaded.  If not it always returns false.
00192        */
00193       virtual bool write(ioHandler& handler, const bool complete=true) const;
00194 
00195       /**
00196        * Read the parameters from the given ioHandler
00197        *
00198        * @param handler the ioHandler to be used
00199        * @param complete if true (the default) the enclosing begin/end will
00200        *        be also written, otherwise only the data block will be written.
00201        * @return true if write was successful
00202        *
00203        * This method has to be overloaded.  If not it always returns false.
00204        */
00205       virtual bool read(ioHandler& handler,const bool complete=true);
00206 
00207       /**
00208        * A std::string coding the data type.  It is usually a 3-characters long
00209        * string.
00210        */
00211       std::string type;
00212 
00213       /**
00214        * Size of the image in the file
00215        */
00216       ipoint size;
00217 
00218       /**
00219        * Number of bits per pixel in the file
00220        */
00221       int bpp;
00222 
00223       /**
00224        * Number of color entries in an internal palette, which usually has to
00225        * be less than 2^bpp, and zero if there is no palette.
00226        */
00227       int colorEntries;
00228     };
00229 
00230     /**
00231      * Default constructor
00232      */
00233     ioImageInterface();
00234 
00235     /**
00236      * Copy constructor
00237      */
00238     ioImageInterface(const ioImageInterface& other);
00239 
00240     /**
00241      * destructor
00242      */
00243     virtual ~ioImageInterface();
00244 
00245     /**
00246      * returns current parameters.
00247      */
00248     const parameters& getParameters() const;
00249 
00250     /**
00251      * Returns the name of this class
00252      */
00253     virtual const std::string& name() const;
00254 
00255     /**
00256      * Returns a pointer to a clone of the functor.
00257      */
00258     virtual ioImageInterface* clone() const = 0;
00259 
00260     /**
00261      * Returns a pointer to a new instance of this functor.
00262      */
00263     virtual ioImageInterface* newInstance() const = 0;
00264 
00265     /**
00266      * Copy operator
00267      */
00268     ioImageInterface& copy(const ioImageInterface& other);
00269 
00270     /**
00271      * Copy operator
00272      */
00273     ioImageInterface& operator=(const ioImageInterface& other);
00274 
00275     /**
00276      * @name Loading images and channels
00277      */
00278     //@{
00279     /**
00280      * Load file as a color image.
00281      */
00282     virtual bool load(const std::string& filename,
00283                       image& img) = 0;
00284 
00285     /**
00286      * Load a channel8 as a label mask with its corresponding palette.
00287      *
00288      * If the file contained a color image, then the functor has to quantize
00289      * the colors and returned the quantized table.
00290      */
00291     virtual bool load(const std::string& filename,
00292                       matrix<ubyte>& chnl,
00293                       palette& pal) = 0;
00294 
00295     /**
00296      * Load the contents as a gray channel.
00297      *
00298      * Default implementation just discards the palette of
00299      * load(const std::string&,matrix<ubyte>&,palette&)
00300      */
00301     virtual bool load(const std::string& filename,
00302                       matrix<ubyte>& chnl);
00303 
00304 
00305     /**
00306      * Load a floating point channel.  Not many file formats support floating
00307      * point channels, so the default implementation just casts a channel8 into
00308      * the float.
00309      */
00310     virtual bool load(const std::string& filename,
00311                       matrix<float>& chnl);
00312 
00313     /**
00314      * Load the contents as a integer mask
00315      *
00316      * Default implementation returns the casting of the channel8 related
00317      * method.
00318      */
00319     virtual bool load(const std::string& filename,
00320                       matrix<int32>& chnl,
00321                       palette& pal);
00322 
00323     /**
00324      * Load the contents as a integer mask
00325      *
00326      * Default implementation discards the palette of the other matrix<int32>
00327      * method.
00328      */
00329     virtual bool load(const std::string& filename,
00330                       matrix<int32>& chnl);
00331 
00332     /**
00333      * Check the file header for common information.
00334      *
00335      * All classes implementing this interface usually provide additional
00336      * methods to check other options of the specific file formats, or overload
00337      * the headerInformation to include additional items.
00338      */
00339     virtual bool checkHeader(const std::string& filename,
00340                              headerInformation& info) = 0;
00341     //@}
00342 
00343 
00344     /**
00345      * @name Saving images and channels
00346      */
00347     //@{
00348     /**
00349      * Save file as a color image.
00350      */
00351     virtual bool save(const std::string& filename,
00352                       const image& img) = 0;
00353 
00354     /**
00355      * Save a channel8 as a label mask with its corresponding palette.
00356      *
00357      * If the file contained a color image, then the functor has to quantize
00358      * the colors and returned the quantized table.
00359      */
00360     virtual bool save(const std::string& filename,
00361                       const matrix<ubyte>& chnl,
00362                       const palette& pal) = 0;
00363 
00364     /**
00365      * Save the contents as a gray channel.
00366      *
00367      * Default implementation just discards the palette of
00368      * save(const std::string&,matrix<ubyte>&,palette&)
00369      */
00370     virtual bool save(const std::string& filename,
00371                       const matrix<ubyte>& chnl);
00372 
00373 
00374     /**
00375      * Save a floating point channel.
00376      *
00377      * Not many file formats support floating point channels, so the default
00378      * implementation just casts the channel to a channel8, which is saved.
00379      * You normally lose precision.
00380      */
00381     virtual bool save(const std::string& filename,
00382                       const matrix<float>& chnl);
00383 
00384     /**
00385      * Save the contents as a integer mask.
00386      *
00387      * Default implementation returns the casting of the channel8 related
00388      * method.
00389      */
00390     virtual bool save(const std::string& filename,
00391                       const matrix<int32>& chnl,
00392                       const palette& pal);
00393 
00394     /**
00395      * Save the contents as a integer mask.
00396      *
00397      * Default implementation discards the palette of the other matrix<int32>
00398      * method.
00399      */
00400     virtual bool save(const std::string& filename,
00401                       const matrix<int32>& chnl);
00402 
00403     //@}
00404   protected:
00405     /**
00406      * Method to get a read-only reference to a 256 long gray-palette.
00407      */
00408     const palette& getGrayPalette() const;
00409   };
00410 
00411 }
00412 
00413 #endif

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