degate  0.1.2
Image.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 __IMAGE_H__
00023 #define __IMAGE_H__
00024 
00025 
00026 #ifdef WORDS_BIGENDIAN
00027 #define MASK_R(r) (r >> 24)
00028 #define MASK_G(g) ((g >> 16) & 0xff)
00029 #define MASK_B(b) ((b >> 8) & 0xff)
00030 #define MASK_A(a) (a & 0xff)
00031 #define MERGE_CHANNELS(r,g,b,a) ((r << 24) | (g <<16) | (b << 8) | a)
00032 #else
00033 #define MASK_R(r) (r & 0xff)
00034 #define MASK_G(g) ((g >> 8) & 0xff)
00035 #define MASK_B(b) ((b >> 16) & 0xff)
00036 #define MASK_A(a) (a >> 24)
00037 #define MERGE_CHANNELS(r,g,b,a) ((a << 24) | (b <<16) | (g << 8) | r)
00038 #endif
00039 
00040 // 75 * 255 + 147 * 255 + 35 * 255 = 65535
00041 #define RGBA_TO_GS_BY_PTR(pix_ptr) RGBA_TO_GS_BY_VAL(*pix_ptr)
00042 #define RGBA_TO_GS_BY_VAL(pix) (((75 * MASK_R(pix)) + (147 * MASK_G(pix)) + (35 * MASK_B(pix))) >> 8)
00043 
00044 
00045 #include "globals.h"
00046 #include "PixelPolicies.h"
00047 #include "StoragePolicies.h"
00048 #include "TileImage.h"
00049 #include "FileSystem.h"
00050 #include "Configuration.h"
00051 #include "TypeTraits.h"
00052 #include "TypeConstraints.h"
00053 #include "ImageManipulation.h"
00054 
00055 
00056 namespace degate {
00057 
00058   class ImageBase {
00059 
00060   private:
00061 
00062     BoundingBox bounding_box;
00063 
00064   public:
00065 
00066     ImageBase(unsigned int _width, unsigned int _height) :
00067       bounding_box(_width, _height) {}
00068 
00069     virtual ~ImageBase() {}
00070 
00071     /**
00072      * Get the width of an image.
00073      */
00074 
00075     inline unsigned int get_width() const {
00076       return bounding_box.get_width();
00077     }
00078 
00079     /**
00080      * Get the height of an image.
00081      */
00082 
00083     inline unsigned int get_height() const {
00084       return bounding_box.get_height();
00085     }
00086 
00087     /**
00088      * Get the bounding box that represents the image.
00089      */
00090     inline BoundingBox const& get_bounding_box() const {
00091       return bounding_box;
00092     }
00093 
00094   };
00095 
00096 
00097   /**
00098    * The generic templated class for image objects.
00099    *
00100    * This class can be used with StoragePolicy_Memory and
00101    * StoragePolicy_TempFile.
00102    *
00103    * Every image constructor should have a two parameter
00104    * version. This is necessary for image processing plugins,
00105    * because plugins have to create temporary images. The two
00106    * parameter constructor should then make reasonable default
00107    * assumtions about storage. Temporary images are really always
00108    * temporary. This means that we can create empty images in
00109    * degate's temp directory. The constructor should only get
00110    * the width and height of an image.
00111    */
00112 
00113   template<class PixelPolicy,
00114            template <class _PixelPolicy> class StoragePolicy>
00115   class Image : public ImageBase,
00116                 public StoragePolicy<PixelPolicy> {
00117 
00118   public:
00119 
00120     /**
00121      * The constructor.
00122      */
00123 
00124     Image(unsigned int _width, unsigned int _height) :
00125       ImageBase(_width, _height),
00126       StoragePolicy<PixelPolicy>(_width, _height) {}
00127 
00128     /**
00129      * The destructor.
00130      */
00131     virtual ~Image() {}
00132 
00133 
00134     /**
00135      * Get pixel with conversion.
00136      */
00137     template<typename PixelTypeDst>
00138     inline PixelTypeDst get_pixel_as(unsigned int x, unsigned int y) {
00139       return convert_pixel<PixelTypeDst, typename PixelPolicy::pixel_type>(this->get_pixel(x, y));
00140     }
00141 
00142     /**
00143      * Set pixel with conversion.
00144      */
00145     template<typename PixelTypeSrc>
00146     inline void set_pixel_as(unsigned int x, unsigned int y, PixelTypeSrc p) {
00147       this->set_pixel(x, y,
00148                       convert_pixel<typename PixelPolicy::pixel_type, PixelTypeSrc>(p));
00149     }
00150 
00151   };
00152 
00153   typedef std::shared_ptr<ImageBase> ImageBase_shptr;
00154 
00155   /**
00156    * Partial template specialization for the storage policy StoragePolicy_PersistentFile.
00157    */
00158 
00159   template<class PixelPolicy>
00160   class Image<PixelPolicy, StoragePolicy_PersistentFile> :
00161     public ImageBase,
00162     public StoragePolicy_PersistentFile<PixelPolicy> {
00163 
00164   public:
00165 
00166     /**
00167      * @todo The third parameter must be optional. Create a temp image instead.
00168      */
00169     Image(unsigned int _width,
00170           unsigned int _height,
00171           std::string const& filename) :
00172       ImageBase(_width, _height),
00173       StoragePolicy_PersistentFile<PixelPolicy>(_width,
00174                                                 _height,
00175                                                 filename) {}
00176 
00177     virtual ~Image() {}
00178 
00179 
00180     /**
00181      * Get pixel with conversion.
00182      */
00183     template<typename PixelTypeDst>
00184     inline PixelTypeDst get_pixel_as(unsigned int x, unsigned int y) {
00185       return convert_pixel<PixelTypeDst, typename PixelPolicy::pixel_type>(this->get_pixel(x, y));
00186     }
00187 
00188     /**
00189      * Set pixel with conversion.
00190      */
00191     template<typename PixelTypeSrc>
00192     inline void set_pixel_as(unsigned int x, unsigned int y, PixelTypeSrc p) {
00193       this->set_pixel(x, y,
00194                       convert_pixel<typename PixelPolicy::pixel_type, PixelTypeSrc>(p));
00195     }
00196 
00197 
00198   };
00199 
00200 
00201   /**
00202    * Partial template specialization for the storage policy StoragePolicy_Tile.
00203    */
00204 
00205   template<class PixelPolicy>
00206   class Image<PixelPolicy, StoragePolicy_Tile> :
00207     public ImageBase,
00208     public StoragePolicy_Tile<PixelPolicy> {
00209 
00210   public:
00211 
00212     /**
00213      * Constructor for temporary virtual images.
00214      */
00215 
00216     Image(unsigned int _width,
00217           unsigned int _height,
00218           unsigned int _tile_width_exp = 10) :
00219       ImageBase(_width, _height),
00220       StoragePolicy_Tile<PixelPolicy>(_width, _height,
00221                                       create_temp_directory(),
00222                                       false,
00223                                       _tile_width_exp) {}
00224 
00225     /**
00226      * Constructor for persistent virtual images.
00227      */
00228 
00229     Image(unsigned int _width,
00230           unsigned int _height,
00231           std::string const& directory,
00232           bool persistent = true,
00233           unsigned int _tile_width_exp = 10) :
00234       ImageBase(_width, _height),
00235       StoragePolicy_Tile<PixelPolicy>(_width, _height,
00236                                       directory,
00237                                       persistent,
00238                                       _tile_width_exp) {}
00239 
00240     /**
00241      * The dtor.
00242      */
00243 
00244     virtual ~Image() {}
00245 
00246     /**
00247      * Get pixel with conversion.
00248      */
00249     template<typename PixelTypeDst>
00250     inline PixelTypeDst get_pixel_as(unsigned int x, unsigned int y) {
00251       return convert_pixel<PixelTypeDst, typename PixelPolicy::pixel_type>(this->get_pixel(x, y));
00252     }
00253 
00254     /**
00255      * Set pixel with conversion.
00256      */
00257     template<typename PixelTypeSrc>
00258     inline void set_pixel_as(unsigned int x, unsigned int y, PixelTypeSrc p) {
00259       this->set_pixel(x, y,
00260                       convert_pixel<typename PixelPolicy::pixel_type, PixelTypeSrc>(p));
00261     }
00262   };
00263 
00264 
00265   /**
00266    * Typedefs for common types of virtual images.
00267    */
00268 
00269 
00270   typedef Image<PixelPolicy_RGBA, StoragePolicy_Tile> TileImage_RGBA;
00271   typedef Image<PixelPolicy_GS_DOUBLE, StoragePolicy_Tile> TileImage_GS_DOUBLE;
00272   typedef Image<PixelPolicy_GS_BYTE, StoragePolicy_Tile> TileImage_GS_BYTE;
00273 
00274   typedef std::shared_ptr<TileImage_RGBA> TileImage_RGBA_shptr;
00275   typedef std::shared_ptr<TileImage_GS_DOUBLE> TileImage_GS_DOUBLE_shptr;
00276   typedef std::shared_ptr<TileImage_GS_BYTE> TileImage_GS_BYTE_shptr;
00277 
00278 
00279   typedef Image<PixelPolicy_RGBA, StoragePolicy_Tile> BackgroundImage;
00280   typedef std::shared_ptr<BackgroundImage> BackgroundImage_shptr;
00281 
00282 
00283   typedef Image<PixelPolicy_RGBA, StoragePolicy_TempFile> TempImage_RGBA;
00284   typedef Image<PixelPolicy_GS_DOUBLE, StoragePolicy_TempFile> TempImage_GS_DOUBLE;
00285   typedef Image<PixelPolicy_GS_BYTE, StoragePolicy_TempFile> TempImage_GS_BYTE;
00286 
00287   typedef std::shared_ptr<TempImage_RGBA> TempImage_RGBA_shptr;
00288   typedef std::shared_ptr<TempImage_GS_DOUBLE> TempImage_GS_DOUBLE_shptr;
00289   typedef std::shared_ptr<TempImage_GS_BYTE> TempImage_GS_BYTE_shptr;
00290 
00291 
00292   typedef Image<PixelPolicy_RGBA, StoragePolicy_PersistentFile> PersistentImage_RGBA;
00293   typedef std::shared_ptr<PersistentImage_RGBA> PersistentImage_RGBA_shptr;
00294 
00295 
00296   typedef Image<PixelPolicy_RGBA, StoragePolicy_Memory> MemoryImage;
00297   typedef std::shared_ptr<MemoryImage> MemoryImage_shptr;
00298 
00299 
00300   typedef Image<PixelPolicy_GS_BYTE, StoragePolicy_Memory> MemoryImage_GS_BYTE;
00301   typedef Image<PixelPolicy_GS_DOUBLE, StoragePolicy_Memory> MemoryImage_GS_DOUBLE;
00302   typedef Image<PixelPolicy_RGBA, StoragePolicy_Memory> MemoryImage_RGBA;
00303   typedef std::shared_ptr<MemoryImage_GS_BYTE> MemoryImage_GS_BYTE_shptr;
00304   typedef std::shared_ptr<MemoryImage_GS_DOUBLE> MemoryImage_GS_DOUBLE_shptr;
00305   typedef std::shared_ptr<MemoryImage_RGBA> MemoryImage_shptr;
00306 
00307   typedef MemoryImage RendererImage;
00308   typedef MemoryImage_shptr RendererImage_shptr;
00309 
00310   // typedef for the image format in which we store template images within memory
00311   typedef MemoryImage GateTemplateImage;
00312   typedef MemoryImage_shptr GateTemplateImage_shptr;
00313 
00314 
00315 }
00316 
00317 #endif
00318