last update 20 Sep 2009 |
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 cvrIOJPEG.h 00044 * Contains the cvr::ioJPEG class to deal with .jpeg files. 00045 * \author Benjamin Winkler 00046 * \author Pablo Alvarado 00047 * \date 27.10.1999 (CVR-Lib 1) 00048 * \date 16.01.2006 (CVR-Lib ) 00049 * 00050 * revisions ..: $Id: cvrIOJPEG.h,v 1.2 2006/08/28 08:08:12 doerfler Exp $ 00051 */ 00052 00053 00054 #ifndef _CVR_IO_JPEG_H_ 00055 #define _CVR_IO_JPEG_H_ 00056 00057 #include "cvrConfig.h" 00058 00059 #if defined HAVE_LIBJPEG || defined HAVE_LOCALJPEG 00060 00061 #include "cvrIOImageInterface.h" 00062 #include <fstream> 00063 00064 #ifdef HAVE_LIBJPEG 00065 #include <cstdio> 00066 #endif 00067 00068 namespace cvr { 00069 00070 /** 00071 * Class to read and write images store in the Joint Picture Expert Group 00072 * file format (JPEG). 00073 * 00074 * It is NOT thread save, this means, the SAME instance can not be used 00075 * from different threads or processes at the same time. If this occurs 00076 * an unpredictible behaviour must be expected!. 00077 * If this is required, just use a different instance of the functor 00078 * for each thread, or use mutexes to avoid parallel accesses. 00079 * 00080 * Example: 00081 * 00082 * \code 00083 * 00084 * cvr::image anImg; // an image 00085 * ... 00086 * cvr::ioJPEG loader; // the load/save class 00087 * loader.load("~/tmp/theFile.jpeg",img); // load the image. 00088 * \endcode 00089 * 00090 * @exception BadJpegStream is thrown when the Stream is corrupted. 00091 * 00092 * @see ioImage 00093 * 00094 * 00095 * There are two implementations of this functor. 00096 * 00097 * - The first one uses the C++ JPEG interface from Miano (at 00098 * Colosseum Builders C++ Image Library), for which several files 00099 * are required. These are published under another licence and 00100 * therefore are not included in the default CVR-Lib . You can use 00101 * them in non-commercial applications and get them from 00102 * ltilib.sourceforge.net. See file ltilib/src/io/jpeg/00readme.txt 00103 * for more information. 00104 * If the file cvrJPEGFunctor.cpp and the directory jpeg/ are found 00105 * in ltilib/src/io, and HAVE_LIBJPEG is not defined in basis/cvrConfig.h, 00106 * this will be the implementation used. 00107 * 00108 * - The second implementation uses the standard JPEG Library, which is 00109 * usually installed in all Linux distributions, but not for Windows. 00110 * The implementation is included in the standard CVR-Lib , (file 00111 * is called cvrJPEGLibFunctor.cpp). 00112 * The autoconf script (./configure) will automatically detect if you 00113 * have the libjpeg installed, and activate this implementation if 00114 * possible. (see http://www.ijg.org/files/ and 00115 * http://freealter.org/doc_distrib/libgr-2.0.13/jpeg/Libjpeg.html) 00116 * 00117 * We recomend to use the libjpeg, it is more robust against not 100% 00118 * correct JPEG-files, and it is faster. Mianos' implementation is mainly 00119 * for windows users who do not want to install the jpeglib. 00120 * 00121 */ 00122 class ioJPEG : public ioImageInterface { 00123 public: 00124 /** 00125 * Parameter class of ioJPEG 00126 */ 00127 class parameters : public ioImageInterface::parameters { 00128 public: 00129 /** 00130 * Default constructor 00131 */ 00132 parameters(); 00133 00134 /** 00135 * copy constructor 00136 */ 00137 parameters(const parameters& other); 00138 00139 /** 00140 * Copy member 00141 */ 00142 parameters& copy(const parameters& other); 00143 00144 /** 00145 * Copy operator 00146 */ 00147 parameters& operator=(const parameters& other); 00148 00149 /** 00150 * Returns the name of this class. 00151 */ 00152 virtual const std::string& name() const; 00153 00154 /** 00155 * Returns a pointer to a clone of the parameters. 00156 */ 00157 virtual parameters* clone() const; 00158 00159 /** 00160 * Returns a pointer to a new instance of the parameters 00161 */ 00162 virtual parameters* newInstance() const; 00163 00164 00165 /** 00166 * Write the parameters in the given ioHandler 00167 * @param handler the ioHandler to be used 00168 * @param complete if true (the default) the enclosing begin/end will 00169 * be also written, otherwise only the data block will be written. 00170 * @return true if write was successful 00171 */ 00172 virtual bool write(ioHandler& handler,const bool complete=true) const; 00173 00174 /** 00175 * Write the parameters in the given ioHandler 00176 * @param handler the ioHandler to be used 00177 * @param complete if true (the default) the enclosing begin/end will 00178 * be also written, otherwise only the data block will be written. 00179 * @return true if write was successful 00180 */ 00181 virtual bool read(ioHandler& handler,const bool complete=true); 00182 00183 // ------------------------------------------------ 00184 // the parameters 00185 // ------------------------------------------------ 00186 00187 /** 00188 * Determines the compression of the image when saved. 00189 * Valid values range from 1 to 100. Default value is 75. 00190 */ 00191 int quality; 00192 00193 /** 00194 * Save in progressive or sequential mode. 00195 * Default is sequential mode (false). 00196 */ 00197 bool progressive; 00198 00199 /** 00200 * Save a comment chunk in the file. Default is an empty string. 00201 */ 00202 std::string comment; 00203 00204 /** 00205 * Number of rows between restart markers (0 = no restart markers) 00206 * when saving. 00207 * Default is 0. 00208 */ 00209 int rowsPerRestart; 00210 }; 00211 00212 /** 00213 * Default constructor 00214 */ 00215 ioJPEG(); 00216 00217 /** 00218 * Constructor with parameters 00219 */ 00220 ioJPEG(const parameters& other); 00221 00222 /** 00223 * Copy constructor 00224 */ 00225 ioJPEG(const ioJPEG& other); 00226 00227 /** 00228 * Destructor 00229 */ 00230 ~ioJPEG(); 00231 00232 /** 00233 * Returns current parameters 00234 */ 00235 const parameters& getParameters() const; 00236 00237 /** 00238 * Returns the name of this class 00239 */ 00240 virtual const std::string& name() const; 00241 00242 /** 00243 * Returns a pointer to a clone of this functor. 00244 */ 00245 virtual ioJPEG* clone() const; 00246 00247 /** 00248 * Returns a pointer to a new instance of this functor. 00249 */ 00250 virtual ioJPEG* newInstance() const; 00251 00252 /** 00253 * Load true-color image. 00254 * 00255 * @param filename name of the file to read 00256 * @param theImage variable where the image is stored 00257 */ 00258 virtual bool load(const std::string& filename, image& theImage); 00259 00260 /** 00261 * Load indexed image 00262 */ 00263 virtual bool load(const std::string& filename, 00264 matrix<ubyte>& theImage, 00265 palette& colors); 00266 00267 /** 00268 * Load the contents as a gray channel. 00269 * 00270 * Default implementation just discards the palette of 00271 * load(const std::string&,matrix<ubyte>&,palette&) 00272 */ 00273 virtual bool load(const std::string& filename, 00274 matrix<ubyte>& chnl); 00275 00276 00277 /** 00278 * Load a floating point channel. Not many file formats support floating 00279 * point channels, so the default implementation just casts a channel8 into 00280 * the float. 00281 */ 00282 virtual bool load(const std::string& filename, 00283 matrix<float>& chnl); 00284 00285 /** 00286 * Load the contents as a integer mask 00287 * 00288 * Default implementation returns the casting of the channel8 related 00289 * method. 00290 */ 00291 virtual bool load(const std::string& filename, 00292 matrix<int32>& chnl, 00293 palette& pal); 00294 00295 /** 00296 * Load the contents as a integer mask 00297 * 00298 * Default implementation discards the palette of the other matrix<int32> 00299 * method. 00300 */ 00301 virtual bool load(const std::string& filename, 00302 matrix<int32>& chnl); 00303 00304 00305 /** 00306 * @name Additional methods not in ioImageInterface 00307 */ 00308 //@{ 00309 /** 00310 * Load image with image type 00311 * 00312 * @param filename name of the file to read 00313 * @param theImage variable where the image is stored 00314 * @param isGray will be set with true if the image contains only gray 00315 * values. 00316 */ 00317 bool load(const std::string& filename, image& theImage, bool& isGray); 00318 //@} 00319 00320 /** 00321 * Returns the essential image information. 00322 * This function will search the file for all chunks with 00323 * essential picture information. Scanning stops before the first 00324 * StartOfScan-Block. That is, all comments that follow the image 00325 * data are not read! 00326 * 00327 * @param filename name of the file to check 00328 * @param info contains the header data 00329 * @return true if file and header are ok 00330 */ 00331 bool checkHeader(const std::string& filename, 00332 headerInformation& info); 00333 00334 private: 00335 #ifdef HAVE_LIBJPEG 00336 /** 00337 * method for loading a JPEG image from an already opened 00338 * file. 00339 */ 00340 bool load(FILE* file,image& theImage,bool& isGray); 00341 #endif 00342 public: 00343 00344 /** 00345 * Save an image in JPEG format. 00346 * 00347 * @param filename name of the file to save 00348 * @param theImage the image to be saved 00349 */ 00350 virtual bool save (const std::string& filename, 00351 const image &theImage); 00352 00353 /** 00354 * Save an "indexed" image contained in the given channel8 (indices) and 00355 * the given color palette 00356 */ 00357 virtual bool save(const std::string& filename, 00358 const matrix<ubyte>& theChannel, 00359 const cvr::palette& colors); 00360 00361 00362 /** 00363 * shortcut for load JPEG 00364 * 00365 * @param filename name of the file to save 00366 * @param theChannel the channel to be saved 00367 */ 00368 virtual bool save (const std::string& filename, 00369 const matrix<ubyte>& theChannel); 00370 00371 /** 00372 * Save a channel as a gray valued JPEG. 00373 * 00374 * @param filename name of the file to save 00375 * @param theChannel the channel to be saved 00376 */ 00377 bool save (const std::string& filename,const matrix<float>& theChannel); 00378 00379 /** 00380 * Save the contents as a integer mask. 00381 * 00382 * Default implementation returns the casting of the channel8 related 00383 * method. 00384 */ 00385 virtual bool save(const std::string& filename, 00386 const matrix<int32>& chnl, 00387 const palette& pal); 00388 00389 /** 00390 * Save the contents as a integer mask. 00391 * 00392 * Default implementation discards the palette of the other matrix<int32> 00393 * method. 00394 */ 00395 virtual bool save(const std::string& filename, 00396 const matrix<int32>& chnl); 00397 00398 00399 private: 00400 #ifdef HAVE_LIBJPEG 00401 00402 /** 00403 * method for saving a JPEG image to an already opened 00404 * file. 00405 */ 00406 bool save(FILE* file,const image& theImage); 00407 00408 #endif 00409 00410 }; 00411 } 00412 00413 #endif 00414 #endif 00415