latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * This file is part of the LTI-Computer Vision Library (LTI-Lib) 00006 * 00007 * The LTI-Lib is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License (LGPL) 00009 * as published by the Free Software Foundation; either version 2.1 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * The LTI-Lib is distributed in the hope that it will be 00013 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00014 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with the LTI-Lib; see the file LICENSE. If 00019 * not, write to the Free Software Foundation, Inc., 59 Temple Place - 00020 * Suite 330, Boston, MA 02111-1307, USA. 00021 */ 00022 00023 00024 /*---------------------------------------------------------------- 00025 * project ....: LTI Digital Image/Signal Processing Library 00026 * file .......: ltiJPEGFunctor.h 00027 * authors ....: Benjamin Winkler 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 27.10.00 00030 * revisions ..: $Id: ltiJPEGFunctor.h,v 1.8 2006/02/08 12:04:53 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_JPEGFUNCTOR_H_ 00034 #define _LTI_JPEGFUNCTOR_H_ 00035 00036 #include "ltiConfig.h" 00037 00038 #if defined HAVE_LIBJPEG || defined HAVE_LOCALJPEG 00039 00040 #include "ltiIOFunctor.h" 00041 00042 #include <fstream> 00043 #include <vector> 00044 #include <map> 00045 00046 00047 #ifdef HAVE_LIBJPEG 00048 #include <cstdio> 00049 #endif 00050 00051 namespace lti { 00052 00053 /** 00054 * Base class to functors which load and save images in JPEG format. 00055 * 00056 * There are two implementations of this functor. 00057 * - The first one uses the C++ JPEG interface from Miano (at 00058 * Colosseum Builders C++ Image Library), for which several files 00059 * are required. These are published under another licence and 00060 * therefore are not included in the default LTI-Lib. You can use 00061 * them in non-commercial applications and get them from 00062 * ltilib.sourceforge.net. See file ltilib/src/io/jpeg/00readme.txt 00063 * for more information. 00064 * If the file ltiJPEGFunctor.cpp and the directory jpeg/ are found 00065 * in ltilib/src/io, and HAVE_LIBJPEG is not defined in basis/ltiConfig.h, 00066 * this will be the implementation used. 00067 * 00068 * - The second implementation uses the standard JPEG Library, which is 00069 * usually installed in all Linux distributions, but not for Windows. 00070 * The implementation is included in the standard LTI-Lib, (file 00071 * is called ltiJPEGLibFunctor.cpp). 00072 * The autoconf script (./configure) will automatically detect if you 00073 * have the libjpeg installed, and activate this implementation if 00074 * possible. (see http://www.ijg.org/files/ and 00075 * http://freealter.org/doc_distrib/libgr-2.0.13/jpeg/Libjpeg.html) 00076 * 00077 * We recomend to use the libjpeg, it is more robust against not 100% 00078 * correct JPEG-files, and it is faster. Mianos' implementation is mainly 00079 * for windows users who do not want to install the jpeglib. 00080 * 00081 */ 00082 class ioJPEG : public ioFunctor { 00083 public: 00084 /** 00085 * Parameter class of ioJPEG 00086 */ 00087 class parameters : public ioFunctor::parameters { 00088 public: 00089 /** 00090 * Default constructor 00091 */ 00092 parameters(); 00093 00094 /** 00095 * copy constructor 00096 */ 00097 parameters(const parameters& other) : ioFunctor::parameters() { 00098 copy(other); 00099 }; 00100 00101 /** 00102 * copy member 00103 */ 00104 parameters& copy(const parameters& other); 00105 00106 /** 00107 * clone member 00108 */ 00109 virtual functor::parameters* clone() const; 00110 00111 /** 00112 * return the name of this type 00113 */ 00114 virtual const char* getTypeName() const; 00115 00116 /** 00117 * write the parameters in the given ioHandler 00118 * @param handler the ioHandler to be used 00119 * @param complete if true (the default) the enclosing begin/end will 00120 * be also written, otherwise only the data block will be written. 00121 * @return true if write was successful 00122 */ 00123 virtual bool write(ioHandler& handler,const bool complete=true) const; 00124 00125 /** 00126 * write the parameters in the given ioHandler 00127 * @param handler the ioHandler to be used 00128 * @param complete if true (the default) the enclosing begin/end will 00129 * be also written, otherwise only the data block will be written. 00130 * @return true if write was successful 00131 */ 00132 virtual bool read(ioHandler& handler,const bool complete=true); 00133 00134 # ifdef _LTI_MSC_6 00135 /** 00136 * this function is required by MSVC only, as a workaround for a 00137 * very awful bug, which exists since MSVC V.4.0, and still by 00138 * V.6.0 with all bugfixes (so called "service packs") remains 00139 * there... This method is public due to another bug, so please 00140 * NEVER EVER call this method directly: use read() instead! 00141 */ 00142 bool readMS(ioHandler& handler,const bool complete=true); 00143 00144 /** 00145 * this function is required by MSVC only, as a workaround for a 00146 * very awful bug, which exists since MSVC V.4.0, and still by 00147 * V.6.0 with all bugfixes (so called "service packs") remains 00148 * there... This method is public due to another bug, so please 00149 * NEVER EVER call this method directly: use write() instead! 00150 */ 00151 bool writeMS(ioHandler& handler,const bool complete=true) const; 00152 # endif 00153 00154 // ------------------------------------------------ 00155 // the parameters 00156 // ------------------------------------------------ 00157 00158 /** 00159 * Determines the compression of the image when saved. 00160 * Valid values range from 1 to 100. Default value is 75. 00161 */ 00162 int quality; 00163 00164 /** 00165 * Save in progressive or sequential mode. 00166 * Default is sequential mode (false). 00167 */ 00168 bool progressive; 00169 00170 /** 00171 * Save a comment chunk in the file. Default is an empty string. 00172 */ 00173 std::string comment; 00174 00175 /** 00176 * Number of rows between restart markers (0 = no restart markers) 00177 * when saving. 00178 * Default is 0. 00179 */ 00180 int rowsPerRestart; 00181 00182 // SamplingFrequency ... 00183 // ScanAttributes ... 00184 00185 }; 00186 00187 /** 00188 * default constructor 00189 */ 00190 ioJPEG(); 00191 00192 /** 00193 * destructor 00194 */ 00195 ~ioJPEG(); 00196 00197 /** 00198 * returns current parameters 00199 */ 00200 const parameters& getParameters() const; 00201 00202 /** 00203 * returns the name of this type 00204 */ 00205 virtual const char* getTypeName() const; 00206 }; 00207 00208 /** 00209 * Functor to read a JPEG file 00210 * It is NOT thread save, this means, the SAME instance can not be used 00211 * from different threads or processes at the same time. If this occurs 00212 * an unpredictible behaviour must be expected!. 00213 * If this is required, just use a different instance of the functor 00214 * for each thread! 00215 * Example: 00216 * 00217 * \code 00218 * 00219 * lti::image anImg; // an image 00220 * ... 00221 * lti::loadJPEG myImg; // the load functor 00222 * lti::loadJPEG::parameters param; // functor parameters 00223 * param.filename = "~/tmp/theFile.jpeg"; // name of the JPEG file 00224 * myImg.setParameters(param); // set the functor parameters 00225 * myImg.apply(anImg); // load the file 00226 * 00227 * \endcode 00228 * 00229 * You can also use the more confortable shortcut: 00230 * 00231 * \code 00232 * lti::image anImg; // an image 00233 * ... 00234 * lti::loadJPEG loadImg; // the load functor 00235 * loadImg.load("~/tmp/theFile.jpeg",img); // load the image. 00236 * \endcode 00237 * 00238 * @exception BadJpegStream is thrown when the Stream is corrupted. 00239 * @see saveJPEG 00240 * @see image 00241 * @see loadJPEG::parameters 00242 * 00243 * For more information on implementation issues see ioJPEG 00244 */ 00245 class loadJPEG : public ioJPEG { 00246 00247 public: 00248 00249 /** 00250 * default constructor 00251 */ 00252 loadJPEG(); 00253 00254 /** 00255 * destructor 00256 */ 00257 ~loadJPEG(); 00258 00259 /** 00260 * returns the name of this type 00261 */ 00262 virtual const char* getTypeName () const; 00263 00264 /** 00265 * loads a JPEG image 00266 * 00267 * @param theImage variable where the image is to be stored 00268 */ 00269 bool apply(image& theImage); 00270 00271 /** 00272 * load JPEG image 00273 * 00274 * @param theImage variable where the image is to be stored 00275 * @param isGray returns true when the image is gray scale 00276 */ 00277 bool apply(image& theImage, bool& isGray); 00278 00279 /** 00280 * shortcut for loading a JPEG 00281 * 00282 * @param filename name of the file to read 00283 * @param theImage variable where the image is stored 00284 */ 00285 bool load(const std::string& filename, image& theImage); 00286 00287 /** 00288 * shortcut for loading a JPEG 00289 * 00290 * @param filename name of the file to read 00291 * @param theImage variable where the image is stored 00292 * @param isGray will be set with true if the image contains only gray 00293 * values. 00294 */ 00295 bool load(const std::string& filename, image& theImage, bool& isGray); 00296 00297 /** 00298 * method for loading a JPEG image from an already opened 00299 * stream. 00300 */ 00301 bool load(std::ifstream& file,image& theImage); 00302 00303 /** 00304 * Returns the essential image information. 00305 * This function will search the file for all chunks with 00306 * essential picture information. Scanning stops before the first 00307 * StartOfScan-Block. That is, all comments that follow the image 00308 * data are not read! 00309 * 00310 * @param filename name of the file to check 00311 * @param imageSize size of the image 00312 * @param comment file comments (author, ...) 00313 * @param isGray returns true, if grayscale jpeg 00314 * 00315 * @return true if file is ok 00316 */ 00317 bool checkHeader (const std::string& filename, point& imageSize, 00318 std::string& comment, bool& isGray); 00319 00320 /** 00321 * returns a pointer to a clone of the functor. 00322 */ 00323 virtual functor* clone () const; 00324 00325 private: 00326 #ifdef HAVE_LIBJPEG 00327 /** 00328 * method for loading a JPEG image from an already opened 00329 * file. 00330 */ 00331 bool load(FILE* file,image& theImage,bool& isGray); 00332 00333 #endif 00334 }; 00335 00336 /** 00337 * Functor for writing a lti::image to a JPEG file 00338 * To save a JPEG image just initialize the parameters of the 00339 * "saveJPEG" object and call the apply member. 00340 * Example: 00341 * 00342 * \code 00343 * 00344 * lti::image anImg; // an image 00345 * ... 00346 * lti::saveJPEG saveImg; // the save functor 00347 * lti::saveJPEG::parameters param; // functor parameters 00348 * param.filename = "~/tmp/theFile.jpeg"; // name of the JPEG file 00349 * saveImg.setParameters(param); // set the functor parameters 00350 * saveImg.apply(anImg); // save the file 00351 * 00352 * \endcode 00353 * 00354 * You can also use the more confortable shortcut: 00355 * 00356 * \code 00357 * lti::image anImg; // an image 00358 * ... 00359 * lti::saveJPEG saveImg; // the save functor 00360 * saveImg.save("~/tmp/theFile.jpeg",img); // save the image. 00361 * \endcode 00362 * 00363 * @see loadJPEG 00364 * @see image 00365 * @see saveJPEG::parameters 00366 */ 00367 class saveJPEG : public ioJPEG { 00368 00369 public: 00370 00371 /** 00372 * default constructor 00373 */ 00374 saveJPEG(); 00375 00376 /** 00377 * destructor 00378 */ 00379 ~saveJPEG(); 00380 00381 /** 00382 * returns the name of this type 00383 */ 00384 virtual const char* getTypeName () const; 00385 00386 /** 00387 * save JPEG image 00388 * 00389 * @param theImage the image to be saved 00390 */ 00391 bool apply (const image& theImage); 00392 00393 /** 00394 * save JPEG image 00395 * 00396 * @param theChannel the channel to be saved 00397 */ 00398 bool apply (const channel& theChannel); 00399 00400 /** 00401 * save JPEG image 00402 * 00403 * @param theChannel the channel to be saved 00404 */ 00405 bool apply (const channel8& theChannel); 00406 00407 /** 00408 * shortcut for load JPEG 00409 * 00410 * @param filename name of the file to save 00411 * @param theImage the image to be saved 00412 */ 00413 bool save (const std::string& filename,const image &theImage); 00414 00415 /** 00416 * shortcut for load JPEG 00417 * 00418 * @param filename name of the file to save 00419 * @param theChannel the channel to be saved 00420 */ 00421 bool save (const std::string& filename,const channel& theChannel); 00422 00423 /** 00424 * shortcut for load JPEG 00425 * 00426 * @param filename name of the file to save 00427 * @param theChannel the channel to be saved 00428 */ 00429 bool save (const std::string& filename,const channel8& theChannel); 00430 00431 /** 00432 * method for saving a JPEG image to an already opened 00433 * stream. The stream is left open in this method. 00434 */ 00435 bool save(std::ofstream& file, const image& theImage); 00436 00437 00438 /** 00439 * returns a pointer to a clone of the functor. 00440 */ 00441 virtual functor* clone () const; 00442 00443 private: 00444 #ifdef HAVE_LIBJPEG 00445 00446 /** 00447 * method for saving a JPEG image to an already opened 00448 * file. 00449 */ 00450 bool save(FILE* file,const image& theImage); 00451 00452 #endif 00453 00454 }; 00455 } 00456 00457 #endif 00458 #endif