image.h

Go to the documentation of this file.
00001 // distributable pairwise
00002 // stuart meikle
00003 // begun Tue Feb 22 10:45:47 2000
00004 
00005 // NOTE: an image is just a matrix of 'T' type objects. I'm not going to attempt
00006 // to build in palette information here -> that would be the job of who-ever/whatever
00007 // interprets and renders the image, not a property of the image itself.
00008 
00009 // Thu May  4 14:05:13 2000. stu the meikle
00010 // added Iiiiiiiiiiimage Maaaaaagick stuff today. (Its good so you have to say
00011 // like you're introducing it on stage). enables images to be written to file in
00012 // numerous formats.
00013 
00014 // Tue Sep 26 13:56:01 - Bala Amavasai
00015 // added readFromFile - read image from file
00016 //      rawData - pointer to the data - useful for directly thunking data in KLT
00017 
00018 // Wed Mar 7 19:06:02 - Bala Amavasai
00019 // ImageMagick functions are all F-ed up since versions from 5.2.9 are 16-bit.
00020 // readFromFile seems okay but writeToFile doesn't want to work
00021 // added    readFromFilePGM - read PGM file
00022 //         writeToFilePGM  - write PGM to file
00023 // the above two functions require netpbm to be installed - note that some buggy installations
00024 // do not include shopts, in which case compile it 'yerself and install it!
00025 
00026 // Sat May  4 14:24:14 2002 - Stu Meikle
00027 // some tidying up. made the destructor virtual, which is more useful for images which
00028 // have pointers at each pixel. (edgel images etc).
00029 // addded a switch for objectID, because it's a big structure! (remember histograms
00030 // are images but often not much bigger than 255 bytes).
00031 
00032 // SUGGESTION: keep all QT specific code in the image files only. 
00033 // (in case someone needs to remove / switch it in the future)
00034 //
00035 // $Header: /cvs/mimas2/include/image.h,v 1.3 2005/08/16 20:46:51 engmb Exp $
00036 //
00037 
00038 #ifndef IMAGE_H
00039 #define IMAGE_H
00040 
00041 #include "mimasconfig.h"
00042 #include <boost/multi_array.hpp>
00043 #include <algorithm>
00044 #include <cassert>
00045 #include <cstdio>
00046 #include <cstdlib>
00047 #include <iostream>
00048 #include <string>
00049 #include "image_ref.h"
00050 #include "multi_array_op.h"
00051 #include "mimasexception.h"
00052 
00053 namespace mimas {
00054 
00062 template<typename T>
00063 class image: public image_ref< T >
00064 {
00065 public:
00066   
00067   image(void) {}
00068   
00070     /* image( const image< T > &_image )
00071   {
00072     operator=( _image );
00073     } */
00074 
00075   template< typename U, typename UPtr >
00076     image( const const_image_ref< U, UPtr > &_image ) {
00077     operator=( _image );
00078   };
00079   
00080   virtual void init( int w , int h ) {
00081     // Only resize, if necessary.
00082     if ( w != image< T >::width || h != image< T >::height ) {
00083       storage = boost::shared_array< T >( new T[ w * h ] );
00084       // pgh requires this
00085       std::fill( storage.get(), storage.get() + w * h, T() );
00086       image< T >::width = w;
00087       image< T >::height = h;
00088       image< T >::data = storage.get();
00089     };
00090   }
00091 
00092   virtual void clear( void ) {
00093     storage.reset();
00094     image< T >::width = 0;
00095     image< T >::height = 0;
00096     image< T >::data = NULL;
00097   }
00098 
00099   /*
00100   !!!!!! the compiler do a segmentation fault on this code. 
00101       image<T> subImage(const Pixel ul, const Pixel lr) const
00102       {
00103   assert(ul.x < getWidth() && lr.x < getWidth()
00104          && ul.y < getHeight() && lr.y < getHeight());
00105   typedef typename boost::multi_array<T,2>::index_range range;
00106   typedef boost::multi_array<T,2>::array_view<2>::type mm_sub_array_t;
00107   mm_sub_array_t subArray =
00108     //      rawData()[boost::indices[range(ul.y, lr.y)][range(ul.x, lr.x)]];
00109     rawData()[boost::indices[range(1, 12)][range(32, 45)]];
00110   return image<T>(boost::multi_array<T,2>(subArray));
00111       }
00112       */
00113 
00114 
00115   //The upper left corner is included the lower right is excluded
00116   image<T> sub_image(const mimas::pixel ul, const mimas::pixel lr) const
00117   { 
00118     assert(ul.x < (unsigned)(image_ref<T>::getWidth()) && lr.x < (unsigned)(image_ref<T>::getWidth())
00119            && ul.y < (unsigned)(image_ref<T>::getHeight()) && lr.y < (unsigned)(image_ref<T>::getHeight()));
00120     assert(ul.x < lr.x && ul.y < lr.y);
00121     
00122     image<T> im;
00123     im.init(lr.x - ul.x , lr.y - ul.y);
00124     for(int x = 0; x < im.getWidth();x++) 
00125       for(int y = 0; y < im.getWidth();y++)
00126         {
00127           im.setPixel(x, y, image< T >::getPixel(x + ul.x ,y + ul.y));
00128         }
00129     return im;
00130   }
00131   
00132   image<T> sub_image(const int top_left_x, const int top_left_y, const int width , const int height) const
00133   { 
00134     assert(top_left_x < (unsigned)(image_ref<T>::getWidth()) && top_left_x + width < (unsigned)(image_ref<T>::getWidth())
00135            && top_left_y < (unsigned)(image_ref<T>::getHeight()) && top_left_y + height < (unsigned)(image_ref<T>::getHeight()));
00136     
00137     image<T> im;
00138     im.init(width, height);
00139     for(int x = 0; x < im.getWidth();x++) 
00140       for(int y = 0; y < im.getWidth();y++)
00141         {
00142           im.setPixel(x, y, image< T >::getPixel(top_left_x + x , top_left_y + y));
00143         }
00144     return im;
00145   }
00146   
00147   image<T>& operator=( const image<T> &_image )
00148   {
00149     image< T >::dummy = T();
00150     init( _image.getWidth(), _image.getHeight() );
00151     const T *p = _image.rawData();
00152     T *q = image< T >::rawData();
00153     int size = image< T >::width * image< T >::height;
00154     for ( int i=0; i<size; i++ )
00155       *q++ = (T)*p++;
00156     return (*this);
00157   };
00158   
00159   template< typename U, typename UPtr >
00160   image& operator=( const const_image_ref<U,UPtr> &_image )
00161   {
00162     image< T >::dummy = T();
00163     init( _image.getWidth(), _image.getHeight() );
00164     const U *p = _image.rawData();
00165     T *q = image< T >::rawData();
00166     int size = image< T >::width * image< T >::height;
00167     for ( int i=0; i<size; i++ )
00168       *q++ = (T)*p++;
00169     return (*this);
00170   };
00171   
00172 protected:
00174   boost::shared_array< T > storage;
00175 };
00176 
00177 }
00178 
00179 #endif

[GNU/Linux] [Qt] [Mesa] [STL] [Lapack] [Boost] [Magick++] [Xalan-C and Xerces-C] [doxygen] [graphviz] [FFTW] [popt] [xine] [Gnuplot] [gnu-arch] [gcc] [gstreamer] [autoconf/automake/make] [freshmeat.net] [opensource.org] [sourceforge.net] [MMVL]
mimas 2.1 - Copyright Mon Oct 30 11:31:17 2006, Bala Amavasai, Stuart Meikle, Arul Selvan, Fabio Caparrelli, Jan Wedekind, Manuel Boissenin, ...