degate  0.1.2
TIFFReader.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 __TIFFREADER_H__
00023 #define __TIFFREADER_H__
00024 
00025 #include <list>
00026 #include <memory>
00027 #include "tiffio.h"
00028 
00029 //#include "ImageReaderFactory.h"
00030 #include "StoragePolicies.h"
00031 #include "ImageReaderBase.h"
00032 
00033 namespace degate {
00034 
00035 
00036   /**
00037    * The TIFFReader parses tiff images.
00038    */
00039 
00040   template<class ImageType>
00041   class TIFFReader : public ImageReaderBase<ImageType> {
00042 
00043   private:
00044 
00045     TIFF* tif;
00046 
00047 
00048   public:
00049 
00050     using ImageReaderBase<ImageType>::get_filename;
00051     using ImageReaderBase<ImageType>::set_width;
00052     using ImageReaderBase<ImageType>::set_height;
00053     using ImageReaderBase<ImageType>::get_width;
00054     using ImageReaderBase<ImageType>::get_height;
00055 
00056     TIFFReader(std::string const& filename) :
00057       ImageReaderBase<ImageType>(filename),
00058       tif(NULL) {}
00059 
00060     ~TIFFReader() {
00061       if(tif != NULL) TIFFClose(tif);
00062     }
00063 
00064     bool read();
00065 
00066     bool get_image(std::shared_ptr<ImageType>);
00067 
00068 
00069   };
00070 
00071   template<class ImageType>
00072   bool TIFFReader<ImageType>::read() {
00073     tif = TIFFOpen(get_filename().c_str(), "r");
00074     if(tif == NULL) return false;
00075 
00076     uint32 w, h;
00077     TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
00078     TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
00079 
00080     set_width(w);
00081     set_height(h);
00082 
00083     return true;
00084   }
00085 
00086   template<class ImageType>
00087   bool TIFFReader<ImageType>::get_image(std::shared_ptr<ImageType> img) {
00088 
00089     size_t npixels = get_width() * get_height();
00090     uint32 * raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32));
00091     if(raster == NULL) return false;
00092 
00093     if(TIFFReadRGBAImage(tif, get_width(), get_height(), raster, 0)) {
00094       for(unsigned int y = 0; y < get_height(); y++) {
00095         for(unsigned int x = 0; x < get_width(); x++) {
00096 
00097           uint32 v = raster[y * get_width() + x];
00098 
00099           img->set_pixel(x, get_height() - y -1, v);
00100 
00101         }
00102       }
00103 
00104     }
00105     _TIFFfree(raster);
00106     return true;
00107   }
00108 
00109 
00110 }
00111 
00112 #endif