CVR-Lib last update 20 Sep 2009

cvrIOBMP.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1998-2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  * Electronics Engineering School, ITCR, Costa Rica
00005  *
00006  *
00007  * This file is part of the Computer Vision and Robotics Library (CVR-Lib)
00008  *
00009  * The CVR-Lib is free software; you can redistribute it and/or
00010  * modify it under the terms of the BSD License.
00011  *
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *
00017  * 1. Redistributions of source code must retain the above copyright notice,
00018  *    this list of conditions and the following disclaimer.
00019  *
00020  * 2. Redistributions in binary form must reproduce the above copyright notice,
00021  *    this list of conditions and the following disclaimer in the documentation
00022  *    and/or other materials provided with the distribution.
00023  *
00024  * 3. Neither the name of the authors nor the names of its contributors may be
00025  *    used to endorse or promote products derived from this software without
00026  *    specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00029  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00030  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00031  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00032  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00033  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00034  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00035  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00036  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00037  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00038  * POSSIBILITY OF SUCH DAMAGE.
00039  */
00040 
00041 
00042 /**
00043  * \file   cvrIOBMP.h
00044  *         Contains the cvr::ioBMP class to deal with .bmp files.
00045  * \author Pablo Alvarado
00046  * \date   19.04.1999 (CVR-Lib 1)
00047  * \date   14.01.2006 (CVR-Lib )
00048  *
00049  * revisions ..: $Id: cvrIOBMP.h,v 1.4 2006/08/28 08:08:12 doerfler Exp $
00050  */
00051 
00052 
00053 #ifndef _CVR_IO_BMP_H_
00054 #define _CVR_IO_BMP_H_
00055 
00056 #include "cvrIOImageInterface.h"
00057 #include "cvrImage.h"
00058 #include <fstream>
00059 
00060 namespace cvr {
00061   /**
00062    * Class to load and save images and channels in the BMP format.
00063    *
00064    * You can use it the same way as the other image io classes.
00065    *
00066    * This class is NOT thread safe, in the sense that the same instance cannot
00067    * be used in different threads.  You can however load BMP files in paralell
00068    * if each thread uses its own instance of ioBMP (or if you protect your
00069    * instace with mutexes).
00070    *
00071    * @see ioPNG, ioJPEG
00072    *
00073    * @ingroup gIOImage
00074    */
00075   class ioBMP : public ioImageInterface {
00076   public:
00077     /**
00078      * Parameter class of the ioBMP class
00079      */
00080     class parameters : public ioImageInterface::parameters {
00081     public:
00082       /**
00083        * Default constructor
00084        */
00085       parameters();
00086 
00087       /**
00088        * Copy constructor
00089        */
00090       parameters(const parameters& other);
00091 
00092       /**
00093        * Copy method.
00094        */
00095       parameters& copy(const parameters& other);
00096 
00097       /**
00098        * Returns the name of this class.
00099        */
00100       virtual const std::string& name() const;
00101 
00102       /**
00103        * Returns a pointer to a clone of the parameters.
00104        */
00105       virtual parameters* clone() const;
00106 
00107       /**
00108        * Returns a pointer to a new instance of the parameters
00109        */
00110       virtual parameters* newInstance() const;
00111 
00112 
00113       /**
00114        * write the parameters in the given ioHandler
00115        * @param handler the ioHandler to be used
00116        * @param complete if true (the default) the enclosing begin/end will
00117        *        be also written, otherwise only the data block will be written.
00118        * @return true if write was successful
00119        */
00120       virtual bool write(ioHandler& handler,const bool complete=true) const;
00121 
00122       /**
00123        * write the parameters in the given ioHandler
00124        * @param handler the ioHandler to be used
00125        * @param complete if true (the default) the enclosing begin/end will
00126        *        be also written, otherwise only the data block will be written.
00127        * @return true if write was successful
00128        */
00129       virtual bool read(ioHandler& handler,const bool complete=true);
00130 
00131       // ------------------------------------------------
00132       // the parameters
00133       // ------------------------------------------------
00134 
00135       /**
00136        * Activate compression of the data.
00137        *
00138        * RLE4 or RLE8 will be used if the number of bits are 4 or 8.
00139        *
00140        * Default value: true.
00141        */
00142       bool compression;
00143 
00144       /**
00145        * Bits per pixel.
00146        *
00147        * Default value: 24
00148        */
00149       int bitsPerPixel;
00150     };
00151 
00152   protected:
00153     /**
00154      * Class header implements BITMAPFILEHEADER.
00155      *
00156      * This class is defined within the class cvr::ioBMP.
00157      */
00158     class header {
00159     public:
00160       /**
00161        * file type. For Bitmaps this must be 'BM'
00162        */
00163       const uint16 type ;
00164 
00165       /**
00166        * file size in byte
00167        */
00168       uint32 size ;
00169 
00170       /**
00171        * must be 0
00172        */
00173       const uint16 reserved1 ;
00174 
00175       /**
00176        * must be 0
00177        */
00178       const uint16 reserved2 ;
00179 
00180       /**
00181        * offset (in bytes) from this header to the bitmap pixels
00182        */
00183       uint32 offsetPixels ;
00184 
00185       /**
00186        * default constructor
00187        */
00188       header();
00189 
00190       /**
00191        * reads header from the stream "in".
00192        * @return false if stream had an invalid header.
00193        */
00194       bool read(std::ifstream& in);
00195 
00196       /**
00197        * writes header to the stream "out".
00198        * @return false if an error occurs.
00199        */
00200       bool write(std::ofstream& out) const;
00201 
00202       /**
00203        * size of the header (in bytes)
00204        */
00205       inline int length() const {return 14;};
00206     };
00207 
00208     /**
00209      * Class infoHeader implements BITMAPINFOHEADER
00210      *
00211      * This class is defined within the class cvr::ioBMP.
00212      */
00213     class infoHeader {
00214     public:
00215       /**
00216        * constructor
00217        */
00218       infoHeader();
00219 
00220       /**
00221        * size
00222        */
00223       mutable uint32 size;
00224 
00225       /**
00226        * width
00227        */
00228       uint32 width;
00229 
00230       /**
00231        * height
00232        */
00233       uint32 height;
00234 
00235       /**
00236        * planes
00237        */
00238       uint16 planes ;
00239 
00240       /**
00241        * bitcount
00242        */
00243       uint16 bitCount ;
00244 
00245       /**
00246        * compression
00247        */
00248       uint32 compression;
00249 
00250       /**
00251        * sizeImage
00252        */
00253       uint32 sizeImage;
00254 
00255       /**
00256        * xPixPerMeter
00257        */
00258       uint32 xPixPerMeter;
00259 
00260       /**
00261        * yPixPerMeter
00262        */
00263       uint32 yPixPerMeter;
00264 
00265       /**
00266        * colorsUsed
00267        */
00268       uint32 colorsUsed;
00269 
00270       /**
00271        * colorsImportant
00272        */
00273       uint32 colorsImportant;
00274 
00275       /**
00276        * read
00277        */
00278       bool read(std::ifstream& in);
00279 
00280       /**
00281        * write
00282        */
00283       bool write(std::ofstream& out) const;
00284 
00285       /**
00286        * size of the info header (in bytes)
00287        */
00288       inline int length() const {return 40;};
00289     };
00290 
00291     /**
00292      * Color Palette found in a BMP file.
00293      *
00294      * This class is defined within the class cvr::ioBMP.
00295      */
00296     class palette : public vector<rgbaPixel> {
00297     public:
00298       /**
00299        * default constructor
00300        * @param entries number of entries in the palette
00301        */
00302       palette(int entries = 0);
00303 
00304       /**
00305        * read the palette from the in stream
00306        * @param in input stream
00307        * @return true if everything is ok, false otherwise
00308        */
00309       bool read(std::ifstream& in);
00310 
00311 
00312       /**
00313        * write palette to the out stream
00314        * @param out output stream
00315        * @return true if everything is ok, false otherwise
00316        */
00317       bool write(std::ofstream& out) const;
00318 
00319       using vector<rgbaPixel>::read;
00320       using vector<rgbaPixel>::write;
00321 
00322     };
00323 
00324   public:
00325     /**
00326      * Default constructor
00327      */
00328     ioBMP();
00329 
00330     /**
00331      * Constructor with parameters
00332      */
00333     ioBMP(const parameters& par);
00334 
00335     /**
00336      * destructor
00337      */
00338     virtual ~ioBMP();
00339 
00340     /**
00341      * returns current parameters.
00342      */
00343     const parameters& getParameters() const;
00344 
00345     /**
00346      * Returns the name of this class
00347      */
00348     virtual const std::string& name() const;
00349 
00350     /**
00351      * Returns a pointer to a clone of this functor.
00352      */
00353     virtual ioBMP* clone() const;
00354 
00355     /**
00356      * Returns a pointer to a new instance of this functor.
00357      */
00358     virtual ioBMP* newInstance() const;
00359 
00360     /**
00361      * Load file with BMP image
00362      *
00363      * @param filename name of the file to be readed
00364      * @param theImage variable where the image will to be stored
00365      */
00366     virtual bool load(const std::string& filename,image& theImage);
00367 
00368     /**
00369      * Load BMP containing a labeled mask with its corresponding palette
00370      * entries.
00371      *
00372      * Use this method if you know that the file contains a gray valued image
00373      * or an indexed color image.  If you try to load a 24-bit image with this
00374      * method, then "false" will be returned.
00375      *
00376      * @param filename name of the file to be readed
00377      * @param theChannel variable where the image will be stored
00378      * @param colors the palette used will be stored here
00379      */
00380     virtual bool load(const std::string& filename,
00381                       matrix<ubyte>& theChannel,
00382                       cvr::palette& colors);
00383 
00384     /**
00385      * Load gray valued channel.
00386      *
00387      * Use this method if you know that the file contains a gray valued image.
00388      *
00389      * @param filename name of the file to be readed
00390      * @param theChannel variable where the image will be stored
00391      */
00392     virtual bool load(const std::string& filename,
00393                       matrix<ubyte>& theChannel);
00394 
00395     /**
00396      * Load gray valued channel.
00397      *
00398      * Use this method if you know that the file contains a gray valued image.
00399      *
00400      * @param filename name of the file to be readed
00401      * @param theChannel variable where the image will be stored
00402      */
00403     virtual bool load(const std::string& filename,
00404                       matrix<float>& theChannel);
00405 
00406     /**
00407      * Load BMP containing a labeled mask with its corresponding palette
00408      * entries.
00409      *
00410      * Use this method if you know that the file contains a gray valued image
00411      * or an indexed color image.  If you try to load a 24-bit image with this
00412      * method, then "false" will be returned.
00413      *
00414      * @param filename name of the file to be readed
00415      * @param theChannel variable where the image will be stored
00416      * @param colors the palette used will be stored here
00417      */
00418     virtual bool load(const std::string& filename,
00419                       matrix<int32>& theChannel,
00420                       cvr::palette& colors);
00421 
00422     /**
00423      * Load gray valued channel.
00424      *
00425      * Use this method if you know that the file contains a gray valued image.
00426      *
00427      * @param filename name of the file to be readed
00428      * @param theChannel variable where the image will be stored
00429      */
00430     virtual bool load(const std::string& filename,
00431                       matrix<int32>& theChannel);
00432 
00433     /**
00434      * Load a BMP from an opened stream.
00435      *
00436      * @param file an already opened stream for reading binary data
00437      * @param theImage variable where the image will to be stored
00438      */
00439     virtual bool load(std::ifstream& file, image& theImage);
00440 
00441     /**
00442      * This method loads an image or a matrix<ubyte>, depending on the contents
00443      * of the file header.
00444      *
00445      * If the number of bits per pixel is 24, the image will be loaded,
00446      * if the number of bits is less or equal 8, the channel and palette
00447      * will be initialized.
00448      *
00449      * The wrong type will be resized to (0,0)
00450      * @param file     and already opened stream for reading binary data
00451      * @param theImage if the file contains a 24 bit color image, this
00452      *                 parameter will be resized and initialized with
00453      *                 the contents of the file
00454      * @param theChannel if the file contains a 8 bit or less indexed image,
00455      *                 this parameter and the palette will be initialized
00456      *                 with the contents of the file.
00457      * @param colors   the color palette for the indexed image.
00458      * @return the number of bits per pixel of the loaded image or 0 if an
00459      *         error occured.  The valid values are 1, 4, 8, or 24
00460      */
00461     virtual int load(std::ifstream& file,
00462                      image& theImage,
00463                      matrix<ubyte>& theChannel,
00464                      cvr::palette& colors);
00465 
00466     /**
00467      * This method loads an image or a matrix<ubyte>, depending on the contents
00468      * of the file header.
00469      *
00470      * If the number of bits per pixel is 24, the image will be loaded,
00471      * if the number of bits is less or equal 8, the channel and palette
00472      * will be initialized.
00473      *
00474      * The wrong type will be resized to (0,0)
00475      * @param filename name of the file to be readed
00476      * @param theImage if the file contains a 24 bit color image, this
00477      *                 parameter will be resized and initialized with
00478      *                 the contents of the file
00479      * @param theChannel if the file contains a 8 bit or less indexed image,
00480      *                 this parameter and the palette will be initialized
00481      *                 with the contents of the file.
00482      * @param colors   the color palette for the indexed image.
00483      * @return the number of bits per pixel of the loaded image or 0 if an
00484      *         error occured.  The valid values are 1, 4, 8, or 24
00485      */
00486     int load(const std::string& filename,
00487              image& theImage,
00488              matrix<ubyte>& theChannel,
00489              cvr::palette& colors);
00490 
00491 
00492     /**
00493      * check the data of the bitmap header
00494      * @param filename name of the bitmap file to be tested
00495      * @param info contains the header data
00496      * @return true if file and header are ok
00497      */
00498     bool checkHeader(const std::string& filename,
00499                      headerInformation& info);
00500 
00501   private:
00502     /**
00503      * Load a 1-bit channel (2 colors).
00504      *
00505      * theChannel has to have the proper dimensions before calling this method.
00506      */
00507     bool load1bit(std::ifstream& in,matrix<ubyte>& theChannel);
00508 
00509     /**
00510      * Load a 4-bit channel (16 colors)
00511      *
00512      * theChannel has to have the proper dimensions before calling this method.
00513      */
00514     bool load4bit(std::ifstream& in,matrix<ubyte>& theChannel);
00515 
00516     /**
00517      * Load an 8-bit channel (256 colors)
00518      *
00519      * theChannel has to have the proper dimensions before calling this method.
00520      */
00521     bool load8bit(std::ifstream& in,matrix<ubyte>& theChannel);
00522 
00523     /**
00524      * Load a 24-bit image (2^24 colors)
00525      *
00526      * theChannel has to have the proper dimensions before calling this method.
00527      */
00528     bool load24bit(std::ifstream& in,image& theImage);
00529 
00530     /**
00531      * Last read header
00532      */
00533     header theHeader_;
00534 
00535     /**
00536      * Last read info header
00537      */
00538     infoHeader theInfoHeader_;
00539 
00540     /**
00541      * Last read palette
00542      */
00543     palette thePalette_;
00544 
00545   public:
00546     /**
00547      * Save an image as a 24 bit RGB bitmap image.
00548      */
00549     virtual bool save(const std::string& filename,
00550                       const image& theImage);
00551 
00552     /**
00553      * Save a channel8 as an 8 bit RGB bitmap image.
00554      *
00555      * @param filename name of the destination file
00556      * @param theChannel the channel to be save
00557      * @param colors the palette to be used.
00558      */
00559     virtual bool save(const std::string& filename,
00560                       const matrix<ubyte>& theChannel,
00561                       const cvr::palette& colors);
00562 
00563     /**
00564      * Save a channel8 as an 8 bit RGB bitmap image.
00565      *
00566      * @param filename name of the destination file
00567      * @param theChannel the channel to be save
00568      */
00569     virtual bool save(const std::string& filename,
00570                       const matrix<ubyte>& theChannel);
00571 
00572     /**
00573      * Save a floating point channel as an 8 bit RGB bitmap image.
00574      *
00575      * The values of the channel must be between 0.0f and 1.0f!
00576      *
00577      * @param filename name of the destination file
00578      * @param theChannel the channel to be saved
00579      */
00580     virtual bool save(const std::string& filename,
00581                       const matrix<float>& theChannel);
00582 
00583     /**
00584      * Save the contents as a integer mask.
00585      *
00586      * Default implementation returns the casting of the channel8 related
00587      * method.
00588      */
00589     virtual bool save(const std::string& filename,
00590                       const matrix<int32>& chnl,
00591                       const cvr::palette& pal);
00592 
00593     /**
00594      * Save the contents as a integer mask.
00595      *
00596      * Default implementation discards the palette of the other matrix<int32>
00597      * method.
00598      */
00599     virtual bool save(const std::string& filename,
00600                       const matrix<int32>& chnl);
00601 
00602   private:
00603     /**
00604      * Save 1 bit channel (2 colors)
00605      */
00606     bool save1bit(std::ofstream& out,const matrix<ubyte>& theChannel);
00607     /**
00608      * Save 4 bit channel (16 colors)
00609      */
00610     bool save4bit(std::ofstream& out,const matrix<ubyte>& theChannel);
00611     /**
00612      * Save 8 bit channel (256 colors)
00613      */
00614     bool save8bit(std::ofstream& out,const matrix<ubyte>& theChannel);
00615     /**
00616      * Save true color image
00617      */
00618     bool save24bit(std::ofstream& out,const image& theImage);
00619   };
00620 }  //namespace cvr
00621 
00622 #endif
00623 

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