00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
00071
00072
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
00082 if ( w != image< T >::width || h != image< T >::height ) {
00083 storage = boost::shared_array< T >( new T[ w * h ] );
00084
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
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
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