aimsdata  5.1.2
Neuroimaging data handling
channel.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 #ifndef AIMS_UTILITY_CHANNEL_H
35 #define AIMS_UTILITY_CHANNEL_H
36 
37 #include <iomanip>
38 #include <cartobase/type/types.h>
42 #include <aims/rgb/rgb.h>
43 #include <aims/hsv/hsv.h>
46 
48 {
52  AlphaChannel = 3
53 };
54 
56 {
59  ValueChannel = 2
60 };
61 
62 #define DECLARE_CHANNEL_SELECTOR_NAME() \
63 class ChannelSelector \
64 
65 
66 #define DECLARE_CHANNEL_SELECTOR_SPEC( T, U, C ) \
67 { \
68  public: \
69  ChannelSelector() {} \
70  virtual ~ChannelSelector() {} \
71 \
72  U select( const T& input, const uint8_t channel ); \
73  void set( T& input, const uint8_t channel, const U& value ); \
74 \
75  private: \
76  carto::ShallowConverter<U, C> channelconvset; \
77  carto::ShallowConverter<C, U> channelconv; \
78  carto::ShallowConverter<T, U> dataconv; \
79 }; \
80 
81 
82 #define DECLARE_CHANNEL_SELECTOR_CLASS() \
83 template <class T, class U, class C = byte > \
84 DECLARE_CHANNEL_SELECTOR_NAME() \
85 DECLARE_CHANNEL_SELECTOR_SPEC( T, U, C ) \
86 
87 
88 #define DECLARE_CHANNEL_SELECTOR_SPECIALIZED( T ) \
89 template <class U> \
90 DECLARE_CHANNEL_SELECTOR_NAME() < T, U > \
91 DECLARE_CHANNEL_SELECTOR_SPEC( T, U, byte ) \
92 
93 
106 
107 
108 // partial specialzation for VolumeRef
109 template <class T, class U>
110 class ChannelSelector < carto::VolumeRef<T>, carto::VolumeRef<U> >
111 {
112  public:
114  virtual ~ChannelSelector() {}
115 
116  carto::VolumeRef<U> select( const carto::VolumeRef<T>& input, const uint8_t channel );
117  void set( carto::VolumeRef<T>& input, const uint8_t channel, const carto::VolumeRef<U>& value );
118 };
119 
120 
121 template <class T, class U, class C> inline
122 U ChannelSelector< T, U, C >::select( const T& input, const uint8_t )
123 {
124  return (U)input;
125 }
126 
127 template <class T, class U, class C> inline
128 void ChannelSelector< T, U, C >::set( T& input, const uint8_t, const U& value )
129 {
130  input = value;
131 }
132 
133 template <class U> inline
134 U ChannelSelector< AimsRGB, U >::select( const AimsRGB& input, const uint8_t channel )
135 {
136  U output;
137 
138  switch(channel) {
139  case RedChannel :
140  channelconv.convert( input.red(), output );
141  break;
142  case GreenChannel :
143  channelconv.convert( input.green(), output );
144  break;
145  case BlueChannel :
146  channelconv.convert( input.blue(), output );
147  break;
148  default :
149  dataconv.convert( input, output );
150  }
151  return output;
152 }
153 
154 template <class U> inline
155 void ChannelSelector< AimsRGB, U >::set( AimsRGB& input, const uint8_t channel, const U& value )
156 {
157  switch(channel) {
158  case RedChannel :
159  channelconvset.convert( value, input.red() );
160  break;
161  case GreenChannel :
162  channelconvset.convert( value, input.green() );
163  break;
164  case BlueChannel :
165  channelconvset.convert( value, input.blue() );
166  break;
167  }
168 }
169 
170 template <class U> inline
171 U ChannelSelector< AimsRGBA, U >::select( const AimsRGBA& input, const uint8_t channel )
172 {
173  U output;
174 
175  switch(channel) {
176  case RedChannel :
177  channelconv.convert( input.red(), output );
178  break;
179  case GreenChannel :
180  channelconv.convert( input.green(), output );
181  break;
182  case BlueChannel :
183  channelconv.convert( input.blue(), output );
184  break;
185  case AlphaChannel :
186  channelconv.convert( input.alpha(), output );
187  break;
188  default :
189  dataconv.convert( input, output );
190  }
191 
192  return output;
193 }
194 
195 template <class U> inline
196 void ChannelSelector< AimsRGBA, U >::set( AimsRGBA& input, const uint8_t channel, const U& value )
197 {
198  switch(channel) {
199  case RedChannel :
200  channelconvset.convert( value, input.red() );
201  break;
202  case GreenChannel :
203  channelconvset.convert( value, input.green() );
204  break;
205  case BlueChannel :
206  channelconvset.convert( value, input.blue() );
207  break;
208  case AlphaChannel :
209  channelconvset.convert( value, input.alpha() );
210  break;
211  }
212 }
213 
214 template <class U> inline
215 U ChannelSelector< AimsHSV, U >::select( const AimsHSV& input, const uint8_t channel )
216 {
217  U output;
218 
219  switch(channel) {
220  case HueChannel :
221  channelconv.convert( input.hue(), output );
222  break;
223  case SaturationChannel :
224  channelconv.convert( input.saturation(), output );
225  break;
226  case ValueChannel :
227  channelconv.convert( input.value(), output );
228  break;
229  default :
230  dataconv.convert( input, output );
231  break;
232  }
233 
234  return output;
235 }
236 
237 
238 template <class U> inline
239 void ChannelSelector< AimsHSV, U >::set( AimsHSV& input, const uint8_t channel, const U& value )
240 {
241  switch(channel) {
242  case HueChannel :
243  channelconvset.convert( value, input.hue() );
244  break;
245  case SaturationChannel :
246  channelconvset.convert( value, input.saturation() );
247  break;
248  case ValueChannel :
249  channelconvset.convert( value, input.value() );
250  break;
251  }
252 }
253 
254 // AimsVector
255 
256 template <typename T, int D, typename U>
257 class ChannelSelector<AimsVector<T, D>, U>
258 {
259  public:
261  virtual ~ChannelSelector() {}
262 
263  U select( const AimsVector<T, D>& input, const uint8_t channel );
264  void set( AimsVector<T, D>& input, const uint8_t channel,
265  const U& value );
266 
267  private:
268  carto::ShallowConverter<U, T> channelconvset;
269  carto::ShallowConverter<T, U> channelconv;
270  // carto::ShallowConverter<AimsVector<T, D>, U> dataconv;
271 };
272 
273 template <typename T, int D, typename U>
274 inline
275 U ChannelSelector<AimsVector<T, D>, U>::select( const AimsVector<T, D> & input,
276  const uint8_t channel )
277 {
278  U output;
279 
280  channelconv.convert( input[channel], output );
281  return output;
282 }
283 
284 template <typename T, int D, typename U>
285 void ChannelSelector< AimsVector<T, D>, U>::set( AimsVector<T, D> & input,
286  const uint8_t channel,
287  const U& value )
288 {
289  channelconvset.convert( value, input[channel] );
290 }
291 
292 
293 // VolumeRef
294 
295 template<class T, class U> inline
297 ChannelSelector< carto::VolumeRef<T>,
298  carto::VolumeRef<U> >::select( const carto::VolumeRef<T>& input,
299  const uint8_t channel )
300 {
301  ChannelSelector< T, U > selector;
302  int x, y, z, t,
303  dx = input.getSizeX(),
304  dy = input.getSizeY(),
305  dz = input.getSizeZ(),
306  dt = input.getSizeT();
307 
308  std::vector<int> b = input.getBorders();
309 
310  carto::VolumeRef<U> output( dx, dy, dz, dt,
311  typename carto::VolumeRef<U>::Position4Di( b[0], b[2], b[4], b[6] ) );
312 
313  output->copyHeaderFrom( input.header() );
314  output.header().setProperty( "data_type", carto::DataTypeCode<U>::name() );
315 
316  for( t=-b[6]; t<dt+b[6]; ++t )
317  {
318  for( z=-b[4]; z<dz+b[4]; ++z )
319  {
320  for( y=-b[2]; y<dy+b[2]; ++y )
321  {
322  for( x=-b[0]; x<dx+b[0]; ++x )
323  output( x, y, z, t ) = selector.select( input( x, y, z, t ), channel );
324  }
325  }
326  }
327 
328  return output;
329 }
330 
331 
332 template<class T, class U> inline
333 void ChannelSelector< carto::VolumeRef<T>,
335  const uint8_t channel,
336  const carto::VolumeRef<U>& value )
337 {
338  ChannelSelector<T, U> selector;
339 
340  int x, y, z, t,
341  dx = input.getSizeX(),
342  dy = input.getSizeY(),
343  dz = input.getSizeZ(),
344  dt = input.getSizeT();
345 
346  for( t=0; t<dt; ++t )
347  {
348  for( z=0; z<dz; ++z )
349  {
350  for( y=0; y<dy; ++y )
351  {
352  for( x=0; x<dx; ++x )
353  selector.set( input( x, y, z, t ),
354  channel,
355  value( x, y, z, t ) );
356  }
357  }
358  }
359 }
360 
361 
362 #endif
#define DECLARE_CHANNEL_SELECTOR_CLASS()
Definition: channel.h:82
#define DECLARE_CHANNEL_SELECTOR_SPECIALIZED(T)
Definition: channel.h:88
AimsRGBAChannel
Definition: channel.h:48
@ GreenChannel
Definition: channel.h:50
@ BlueChannel
Definition: channel.h:51
@ RedChannel
Definition: channel.h:49
@ AlphaChannel
Definition: channel.h:52
AimsHSVChannel
Definition: channel.h:56
@ SaturationChannel
Definition: channel.h:58
@ HueChannel
Definition: channel.h:57
@ ValueChannel
Definition: channel.h:59
virtual void convert(const INP &in, OUTP &out) const
void setProperty(const std::string &, const T &)
int getSizeZ() const
std::vector< int > getBorders() const
virtual void copyHeaderFrom(const PropertySet &other)
int getSizeT() const
int getSizeY() const
const PropertySet & header() const
int getSizeX() const
Volume< T >::Position4Di Position4Di
const uint8_t & hue() const
const uint8_t & saturation() const
const uint8_t & value() const
const uint8_t & green() const
const uint8_t & blue() const
const uint8_t & alpha() const
const uint8_t & red() const
const uint8_t & red() const
const uint8_t & blue() const
const uint8_t & green() const