degate  0.1.2
ImageReaderFactory.h
Go to the documentation of this file.
00001 /* -*-c++-*-
00002 
00003  This file is part of the IC reverse engineering tool degate.
00004 
00005  Copyright 2008, 2009, 2010 by Martin Schobert
00006 
00007  Degate is free software: you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation, either version 3 of the License, or
00010  any later version.
00011 
00012  Degate is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with degate. If not, see <http://www.gnu.org/licenses/>.
00019 
00020 */
00021 
00022 #ifndef __IMAGEREADERFACTORY_H__
00023 #define __IMAGEREADERFACTORY_H__
00024 
00025 #include <list>
00026 #include <memory>
00027 #include <degate_exceptions.h>
00028 
00029 #include <StoragePolicies.h>
00030 #include <PixelPolicies.h>
00031 #include <FileSystem.h>
00032 #include <ImageReaderBase.h>
00033 #include <TIFFReader.h>
00034 #include <JPEGReader.h>
00035 
00036 
00037 namespace degate{
00038 
00039   typedef std::list<std::string> file_format_collection;
00040 
00041   /**
00042    * A factory for creating image reader objects.
00043    */
00044 
00045   template<class ImageType>
00046   class ImageReaderFactory {
00047 
00048   public:
00049 
00050   private:
00051 
00052     file_format_collection file_formats;
00053 
00054   public:
00055 
00056     ImageReaderFactory() {
00057       // Tiff file format parser
00058       file_formats.push_back("tif");
00059       file_formats.push_back("tiff");
00060 
00061       // Jpeg file format parser
00062       file_formats.push_back("jpg");
00063       file_formats.push_back("jpeg");
00064     }
00065 
00066 
00067     ~ImageReaderFactory() {}
00068 
00069     /**
00070      * Get a list of parseable file formats.
00071      */
00072     file_format_collection const& get_file_formats() const {
00073       return file_formats;
00074     }
00075 
00076     /**
00077      * Get a concrete image reader object for a file name.
00078      * @param filename The file name is evaluated. Depending on the file suffix an
00079      *   image reader is choosen. The file must not exists for the reader selection,
00080      *   but the reader will fail, if it can't read the image.
00081      * @return Returns an image reader object that is able to read the image.
00082      * @exception InvalidFileFormatException This exception is thrown if the there is no
00083      *   reader that can read the file.
00084      */
00085 
00086     std::shared_ptr<class ImageReaderBase<ImageType> >
00087     get_reader(std::string const & filename) {
00088 
00089       std::string suffix(get_file_suffix(filename).c_str());
00090 
00091       /*
00092         Convert suffix string to a lowercase string.
00093 
00094         This comment should not be necessary, if there would be a simple
00095         way to convert it. You may have seen other programming languages
00096         with a simple convenient way to lowercase strings. But hey! The
00097         transform is really generic, You can even apply it to strings. Wow!
00098         BTW: If you omit the static cast, you will get a long error message
00099         from your compiler. Great!
00100       */
00101 
00102       std::transform(suffix.begin(), suffix.end(), suffix.begin(),
00103                      static_cast<int (*)(int)>(std::tolower));
00104 
00105       // Ok. Enough sarcasm. Let us finish the job ...
00106 
00107       if(suffix == "tif" || suffix == "tiff")
00108         return std::shared_ptr<ImageReaderBase<ImageType> >
00109           (new TIFFReader<ImageType>(filename));
00110       else if(suffix == "jpg" || suffix == "jpeg")
00111         return std::shared_ptr<ImageReaderBase<ImageType> >
00112           (new JPEGReader<ImageType>(filename));
00113       else throw InvalidFileFormatException();
00114     }
00115 
00116 
00117   };
00118 
00119 }
00120 
00121 
00122 
00123 
00124 #endif