|
degate 0.1.1
|
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 __TIFFWRITER_H__ 00023 #define __TIFFWRITER_H__ 00024 00025 #include <list> 00026 #include <tr1/memory> 00027 #include "tiffio.h" 00028 00029 #include <globals.h> 00030 #include <degate_exceptions.h> 00031 #include <PixelPolicies.h> 00032 #include <StoragePolicies.h> 00033 #include <ImageWriterBase.h> 00034 #include <FileSystem.h> 00035 #include <Image.h> 00036 00037 #include <errno.h> 00038 00039 namespace degate { 00040 00041 00042 /** 00043 * The TIFFWriter parses tiff images. 00044 */ 00045 00046 template<class ImageType> 00047 class TIFFWriter : public ImageWriterBase<ImageType> { 00048 00049 public: 00050 00051 using ImageWriterBase<ImageType>::get_filename; 00052 using ImageWriterBase<ImageType>::get_width; 00053 using ImageWriterBase<ImageType>::get_height; 00054 00055 00056 TIFFWriter(unsigned int width, unsigned int height, 00057 std::string const& filename) : 00058 ImageWriterBase<ImageType>(width, height, filename) {} 00059 00060 virtual ~TIFFWriter() { } 00061 00062 /** 00063 * exception FileSystemException 00064 */ 00065 bool write_image(std::tr1::shared_ptr<ImageType> img); 00066 }; 00067 00068 00069 template<class ImageType> 00070 bool TIFFWriter<ImageType>::write_image(std::tr1::shared_ptr<ImageType> img) { 00071 00072 TIFF * tif = TIFFOpen(get_filename().c_str(), "w"); 00073 if(tif == NULL) { 00074 throw FileSystemException(strerror(errno)); 00075 } 00076 00077 size_t npixels = get_width() * get_height(); 00078 00079 char * raster = (char*) _TIFFmalloc(npixels * 3); 00080 if(raster == NULL) return false; 00081 00082 for(unsigned int y = 0; y < get_height(); y++) { 00083 for(unsigned int x = 0; x < get_width(); x++) { 00084 00085 rgba_pixel_t p = 00086 img->get_pixel_as<rgba_pixel_t>(x, y); 00087 00088 raster[3*(y * get_width() + x)] = MASK_R(p); 00089 raster[3*(y * get_width() + x)+1] = MASK_G(p); 00090 raster[3*(y * get_width() + x)+2] = MASK_B(p); 00091 } 00092 } 00093 00094 00095 // Write the tiff tags to the file 00096 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, get_width()); 00097 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, get_height()); 00098 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); 00099 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); 00100 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); 00101 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); 00102 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3); 00103 00104 bool ret = true; 00105 00106 // Actually write the image 00107 if(TIFFWriteEncodedStrip(tif, 0, raster, npixels * 3) == 0) { 00108 ret = false; 00109 } 00110 00111 if(tif != NULL) TIFFClose(tif); 00112 _TIFFfree(raster); 00113 00114 return ret; 00115 } 00116 00117 00118 } 00119 00120 #endif
1.7.4