aimsdata  5.0.5
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/data/data_g.h>
43 #include <aims/rgb/rgb.h>
44 #include <aims/hsv/hsv.h>
46 
48 {
53 };
54 
56 {
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 // partial specialzation for AimsData
122 template <class T, class U>
123 class ChannelSelector < AimsData<T>, AimsData<U> >
124 {
125  public:
127  virtual ~ChannelSelector() {}
128 
129  AimsData<U> select( const AimsData<T>& input, const uint8_t channel );
130  void set( AimsData<T>& input, const uint8_t channel, const AimsData<U>& value );
131 };
132 
133 
134 template <class T, class U, class C> inline
135 U ChannelSelector< T, U, C >::select( const T& input, const uint8_t )
136 {
137  return (U)input;
138 }
139 
140 template <class T, class U, class C> inline
141 void ChannelSelector< T, U, C >::set( T& input, const uint8_t, const U& value )
142 {
143  input = value;
144 }
145 
146 template <class U> inline
147 U ChannelSelector< AimsRGB, U >::select( const AimsRGB& input, const uint8_t channel )
148 {
149  U output;
150 
151  switch(channel) {
152  case RedChannel :
153  channelconv.convert( input.red(), output );
154  break;
155  case GreenChannel :
156  channelconv.convert( input.green(), output );
157  break;
158  case BlueChannel :
159  channelconv.convert( input.blue(), output );
160  break;
161  default :
162  dataconv.convert( input, output );
163  }
164  return output;
165 }
166 
167 template <class U> inline
168 void ChannelSelector< AimsRGB, U >::set( AimsRGB& input, const uint8_t channel, const U& value )
169 {
170  switch(channel) {
171  case RedChannel :
172  channelconvset.convert( value, input.red() );
173  break;
174  case GreenChannel :
175  channelconvset.convert( value, input.green() );
176  break;
177  case BlueChannel :
178  channelconvset.convert( value, input.blue() );
179  break;
180  }
181 }
182 
183 template <class U> inline
184 U ChannelSelector< AimsRGBA, U >::select( const AimsRGBA& input, const uint8_t channel )
185 {
186  U output;
187 
188  switch(channel) {
189  case RedChannel :
190  channelconv.convert( input.red(), output );
191  break;
192  case GreenChannel :
193  channelconv.convert( input.green(), output );
194  break;
195  case BlueChannel :
196  channelconv.convert( input.blue(), output );
197  break;
198  case AlphaChannel :
199  channelconv.convert( input.alpha(), output );
200  break;
201  default :
202  dataconv.convert( input, output );
203  }
204 
205  return output;
206 }
207 
208 template <class U> inline
209 void ChannelSelector< AimsRGBA, U >::set( AimsRGBA& input, const uint8_t channel, const U& value )
210 {
211  switch(channel) {
212  case RedChannel :
213  channelconvset.convert( value, input.red() );
214  break;
215  case GreenChannel :
216  channelconvset.convert( value, input.green() );
217  break;
218  case BlueChannel :
219  channelconvset.convert( value, input.blue() );
220  break;
221  case AlphaChannel :
222  channelconvset.convert( value, input.alpha() );
223  break;
224  }
225 }
226 
227 template <class U> inline
228 U ChannelSelector< AimsHSV, U >::select( const AimsHSV& input, const uint8_t channel )
229 {
230  U output;
231 
232  switch(channel) {
233  case HueChannel :
234  channelconv.convert( input.hue(), output );
235  break;
236  case SaturationChannel :
237  channelconv.convert( input.saturation(), output );
238  break;
239  case ValueChannel :
240  channelconv.convert( input.value(), output );
241  break;
242  default :
243  dataconv.convert( input, output );
244  break;
245  }
246 
247  return output;
248 }
249 
250 
251 template <class U> inline
252 void ChannelSelector< AimsHSV, U >::set( AimsHSV& input, const uint8_t channel, const U& value )
253 {
254  switch(channel) {
255  case HueChannel :
256  channelconvset.convert( value, input.hue() );
257  break;
258  case SaturationChannel :
259  channelconvset.convert( value, input.saturation() );
260  break;
261  case ValueChannel :
262  channelconvset.convert( value, input.value() );
263  break;
264  }
265 }
266 
267 // AimsVector
268 
269 template <typename T, int D, typename U>
270 class ChannelSelector<AimsVector<T, D>, U>
271 {
272  public:
274  virtual ~ChannelSelector() {}
275 
276  U select( const AimsVector<T, D>& input, const uint8_t channel );
277  void set( AimsVector<T, D>& input, const uint8_t channel,
278  const U& value );
279 
280  private:
281  carto::ShallowConverter<U, T> channelconvset;
282  carto::ShallowConverter<T, U> channelconv;
283  // carto::ShallowConverter<AimsVector<T, D>, U> dataconv;
284 };
285 
286 template <typename T, int D, typename U>
287 inline
288 U ChannelSelector<AimsVector<T, D>, U>::select( const AimsVector<T, D> & input,
289  const uint8_t channel )
290 {
291  U output;
292 
293  channelconv.convert( input[channel], output );
294  return output;
295 }
296 
297 template <typename T, int D, typename U>
298 void ChannelSelector< AimsVector<T, D>, U>::set( AimsVector<T, D> & input,
299  const uint8_t channel,
300  const U& value )
301 {
302  channelconvset.convert( value, input[channel] );
303 }
304 
305 
306 // VolumeRef
307 
308 template<class T, class U> inline
310 ChannelSelector< carto::VolumeRef<T>,
311  carto::VolumeRef<U> >::select( const carto::VolumeRef<T>& input,
312  const uint8_t channel )
313 {
314  ChannelSelector< T, U > selector;
315  int x, y, z, t,
316  dx = input.getSizeX(),
317  dy = input.getSizeY(),
318  dz = input.getSizeZ(),
319  dt = input.getSizeT();
320 
321  std::vector<int> b = input.getBorders();
322 
323  carto::VolumeRef<U> output( dx, dy, dz, dt,
324  typename carto::VolumeRef<U>::Position4Di( b[0], b[2], b[4], b[6] ) );
325 
326  output->copyHeaderFrom( input.header() );
327  output.header().setProperty( "data_type", carto::DataTypeCode<U>::name() );
328 
329  for( t=-b[6]; t<dt+b[6]; ++t )
330  {
331  for( z=-b[4]; z<dz+b[4]; ++z )
332  {
333  for( y=-b[2]; y<dy+b[2]; ++y )
334  {
335  for( x=-b[0]; x<dx+b[0]; ++x )
336  output( x, y, z, t ) = selector.select( input( x, y, z, t ), channel );
337  }
338  }
339  }
340 
341  return output;
342 }
343 
344 template<class T, class U> inline
345 AimsData< U > ChannelSelector< AimsData< T >,
346  AimsData< U > >::select( const AimsData< T >& input,
347  const uint8_t channel )
348 {
349  ChannelSelector< carto::VolumeRef<T>, carto::VolumeRef<U> > selector;
350 
351  // Call volume channel selector
352  carto::VolumeRef<U> outputvolume = selector.select(input.volume(), channel);
353  AimsData<U> output = AimsData< U >( outputvolume );
354  output.setSizeXYZT( input.sizeX(),
355  input.sizeY(),
356  input.sizeZ(),
357  input.sizeT() );
358 
359  return output;
360 }
361 
362 
363 template<class T, class U> inline
364 void ChannelSelector< carto::VolumeRef<T>,
366  const uint8_t channel,
367  const carto::VolumeRef<U>& value )
368 {
369  ChannelSelector<T, U> selector;
370 
371  int x, y, z, t,
372  dx = input.getSizeX(),
373  dy = input.getSizeY(),
374  dz = input.getSizeZ(),
375  dt = input.getSizeT();
376 
377  for( t=0; t<dt; ++t )
378  {
379  for( z=0; z<dz; ++z )
380  {
381  for( y=0; y<dy; ++y )
382  {
383  for( x=0; x<dx; ++x )
384  selector.set( input( x, y, z, t ),
385  channel,
386  value( x, y, z, t ) );
387  }
388  }
389  }
390 }
391 
392 template<class T, class U> inline
393 void ChannelSelector< AimsData<T>,
394  AimsData<U> >::set( AimsData<T>& input,
395  const uint8_t channel,
396  const AimsData<U>& value )
397 {
398  ChannelSelector< carto::VolumeRef<T>,
399  carto::VolumeRef<U> > selector;
400 
401  carto::VolumeRef<T> volumeref(input.volume());
402  const carto::VolumeRef<U> valueref(value.volume());
403  selector.set( volumeref,
404  channel,
405  valueref );
406 
407 }
408 
409 
410 #endif
int getSizeX() const
const uint8_t & blue() const
int getSizeY() const
int getSizeZ() const
AimsRGBAChannel
Definition: channel.h:47
int getSizeT() const
float sizeZ() const
const uint8_t & red() const
float sizeT() const
virtual void convert(const INP &in, OUTP &out) const
float sizeX() const
const PropertySet & header() const
#define DECLARE_CHANNEL_SELECTOR_SPECIALIZED(T)
Definition: channel.h:88
#define DECLARE_CHANNEL_SELECTOR_CLASS()
Definition: channel.h:82
Volume< T >::Position4Di Position4Di
const uint8_t & hue() const
const uint8_t & alpha() const
const uint8_t & value() const
float sizeY() const
carto::rc_ptr< carto::Volume< T > > volume()
virtual void copyHeaderFrom(const PropertySet &other)
const uint8_t & green() const
void setSizeXYZT(float sizex=1.0f, float sizey=1.0f, float sizez=1.0f, float sizet=1.0f)
const uint8_t & blue() const
const uint8_t & saturation() const
AimsHSVChannel
Definition: channel.h:55
std::vector< int > getBorders() const
const uint8_t & green() const
const uint8_t & red() const