178 |
kaklik |
1 |
// distributable pairwise |
|
|
2 |
// stuart meikle |
|
|
3 |
// begun Tue Feb 22 10:45:47 2000 |
|
|
4 |
|
|
|
5 |
// NOTE: an image is just a matrix of 'T' type objects. I'm not going to attempt |
|
|
6 |
// to build in palette information here -> that would be the job of who-ever/whatever |
|
|
7 |
// interprets and renders the image, not a property of the image itself. |
|
|
8 |
|
|
|
9 |
// Thu May 4 14:05:13 2000. stu the meikle |
|
|
10 |
// added Iiiiiiiiiiimage Maaaaaagick stuff today. (Its good so you have to say |
|
|
11 |
// like you're introducing it on stage). enables images to be written to file in |
|
|
12 |
// numerous formats. |
|
|
13 |
|
|
|
14 |
// Tue Sep 26 13:56:01 - Bala Amavasai |
|
|
15 |
// added readFromFile - read image from file |
|
|
16 |
// rawData - pointer to the data - useful for directly thunking data in KLT |
|
|
17 |
|
|
|
18 |
// Wed Mar 7 19:06:02 - Bala Amavasai |
|
|
19 |
// ImageMagick functions are all F-ed up since versions from 5.2.9 are 16-bit. |
|
|
20 |
// readFromFile seems okay but writeToFile doesn't want to work |
|
|
21 |
// added readFromFilePGM - read PGM file |
|
|
22 |
// writeToFilePGM - write PGM to file |
|
|
23 |
// the above two functions require netpbm to be installed - note that some buggy installations |
|
|
24 |
// do not include shopts, in which case compile it 'yerself and install it! |
|
|
25 |
|
|
|
26 |
// Sat May 4 14:24:14 2002 - Stu Meikle |
|
|
27 |
// some tidying up. made the destructor virtual, which is more useful for images which |
|
|
28 |
// have pointers at each pixel. (edgel images etc). |
|
|
29 |
// addded a switch for objectID, because it's a big structure! (remember histograms |
|
|
30 |
// are images but often not much bigger than 255 bytes). |
|
|
31 |
|
|
|
32 |
// SUGGESTION: keep all QT specific code in the image files only. |
|
|
33 |
// (in case someone needs to remove / switch it in the future) |
|
|
34 |
// |
|
|
35 |
// $Header: /cvs/mimas2/include/image.h,v 1.3 2005/08/16 20:46:51 engmb Exp $ |
|
|
36 |
// |
|
|
37 |
|
|
|
38 |
#ifndef IMAGE_H |
|
|
39 |
#define IMAGE_H |
|
|
40 |
|
|
|
41 |
#include <boost/multi_array.hpp> |
|
|
42 |
#include <algorithm> |
|
|
43 |
#include <cassert> |
|
|
44 |
#include <cstdio> |
|
|
45 |
#include <cstdlib> |
|
|
46 |
#include <iostream> |
|
|
47 |
#include <string> |
|
|
48 |
#include "image_ref.h" |
|
|
49 |
#include "multi_array_op.h" |
|
|
50 |
#include "mimasexception.h" |
|
|
51 |
|
|
|
52 |
namespace mimas { |
|
|
53 |
|
|
|
54 |
/** Template class for images. |
|
|
55 |
Hmmm. Well. image is a generic image class. You specify the pixel type |
|
|
56 |
at creation time. So if you want an image of ints do |
|
|
57 |
\code |
|
|
58 |
image<int> my_image; |
|
|
59 |
\endcode |
|
|
60 |
and so on. */ |
|
|
61 |
template<typename T> |
|
|
62 |
class image: public image_ref< T > |
|
|
63 |
{ |
|
|
64 |
public: |
|
|
65 |
|
|
|
66 |
image(void) {} |
|
|
67 |
|
|
|
68 |
/// Copy-constructor. |
|
|
69 |
/* image( const image< T > &_image ) |
|
|
70 |
{ |
|
|
71 |
operator=( _image ); |
|
|
72 |
} */ |
|
|
73 |
|
|
|
74 |
template< typename U, typename UPtr > |
|
|
75 |
image( const const_image_ref< U, UPtr > &_image ) { |
|
|
76 |
operator=( _image ); |
|
|
77 |
}; |
|
|
78 |
|
|
|
79 |
virtual void init( int w , int h ) { |
|
|
80 |
// Only resize, if necessary. |
|
|
81 |
if ( w != image< T >::width || h != image< T >::height ) { |
|
|
82 |
storage = boost::shared_array< T >( new T[ w * h ] ); |
|
|
83 |
// pgh requires this |
|
|
84 |
std::fill( storage.get(), storage.get() + w * h, T() ); |
|
|
85 |
image< T >::width = w; |
|
|
86 |
image< T >::height = h; |
|
|
87 |
image< T >::data = storage.get(); |
|
|
88 |
}; |
|
|
89 |
} |
|
|
90 |
|
|
|
91 |
virtual void clear( void ) { |
|
|
92 |
storage.reset(); |
|
|
93 |
image< T >::width = 0; |
|
|
94 |
image< T >::height = 0; |
|
|
95 |
image< T >::data = NULL; |
|
|
96 |
} |
|
|
97 |
|
|
|
98 |
/* |
|
|
99 |
!!!!!! the compiler do a segmentation fault on this code. |
|
|
100 |
image<T> subImage(const Pixel ul, const Pixel lr) const |
|
|
101 |
{ |
|
|
102 |
assert(ul.x < getWidth() && lr.x < getWidth() |
|
|
103 |
&& ul.y < getHeight() && lr.y < getHeight()); |
|
|
104 |
typedef typename boost::multi_array<T,2>::index_range range; |
|
|
105 |
typedef boost::multi_array<T,2>::array_view<2>::type mm_sub_array_t; |
|
|
106 |
mm_sub_array_t subArray = |
|
|
107 |
// rawData()[boost::indices[range(ul.y, lr.y)][range(ul.x, lr.x)]]; |
|
|
108 |
rawData()[boost::indices[range(1, 12)][range(32, 45)]]; |
|
|
109 |
return image<T>(boost::multi_array<T,2>(subArray)); |
|
|
110 |
} |
|
|
111 |
*/ |
|
|
112 |
|
|
|
113 |
image<T>& operator=( const image<T> &_image ) |
|
|
114 |
{ |
|
|
115 |
image< T >::dummy = T(); |
|
|
116 |
init( _image.getWidth(), _image.getHeight() ); |
|
|
117 |
const T *p = _image.rawData(); |
|
|
118 |
T *q = image< T >::rawData(); |
|
|
119 |
int size = image< T >::width * image< T >::height; |
|
|
120 |
for ( int i=0; i<size; i++ ) |
|
|
121 |
*q++ = (T)*p++; |
|
|
122 |
return (*this); |
|
|
123 |
}; |
|
|
124 |
|
|
|
125 |
template< typename U, typename UPtr > |
|
|
126 |
image& operator=( const const_image_ref<U,UPtr> &_image ) |
|
|
127 |
{ |
|
|
128 |
image< T >::dummy = T(); |
|
|
129 |
init( _image.getWidth(), _image.getHeight() ); |
|
|
130 |
const U *p = _image.rawData(); |
|
|
131 |
T *q = image< T >::rawData(); |
|
|
132 |
int size = image< T >::width * image< T >::height; |
|
|
133 |
for ( int i=0; i<size; i++ ) |
|
|
134 |
*q++ = (T)*p++; |
|
|
135 |
return (*this); |
|
|
136 |
}; |
|
|
137 |
|
|
|
138 |
protected: |
|
|
139 |
/// Storage. |
|
|
140 |
boost::shared_array< T > storage; |
|
|
141 |
}; |
|
|
142 |
|
|
|
143 |
} |
|
|
144 |
|
|
|
145 |
#endif |