aimsdata  4.7.0
Neuroimaging data handling
converter_rgb.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_CONVERTER_RGB_H
35 #define AIMS_UTILITY_CONVERTER_RGB_H
36 
38 #include <aims/rgb/rgb.h>
39 
40 namespace carto
41 {
42 
43  template <class INP>
44  class RawConverter<INP, AimsRGB>
45  {
46  public :
47  void convert( const INP &in, AimsRGB & out ) const;
48  };
49 
50  template<class INP>
51  inline void
52  RawConverter<INP,AimsRGB>::convert( const INP &in, AimsRGB & out ) const
53  {
54  out = AimsRGB( (byte) in, (byte) in, (byte) in );
55  }
56 
57  template <class INP>
58  class RawConverter<INP, AimsRGBA>
59  {
60  public :
61  void convert( const INP &in, AimsRGBA & out ) const;
62  };
63 
64  template<class INP>
65  inline void
66  RawConverter<INP,AimsRGBA>::convert( const INP &in, AimsRGBA & out ) const
67  {
68  out = AimsRGBA( (byte) in, (byte) in, (byte) in, 255 );
69  }
70 
71  template <class OUTP>
72  class RawConverter<AimsRGB, OUTP>
73  {
74  public :
75  void convert( const AimsRGB &in, OUTP & out ) const;
76  };
77 
78  template<class OUTP>
79  inline void
80  RawConverter<AimsRGB,OUTP>::convert( const AimsRGB &in, OUTP & out ) const
81  {
82  out = (OUTP) ( sqrt( ( (double) in.red() ) * in.red()
83  + ( (double) in.green() ) * in.green()
84  + ( (double) in.blue() ) * in.blue() ) );
85  }
86 
87  template <class OUTP>
88  class RawConverter<AimsRGBA, OUTP>
89  {
90  public :
91  void convert( const AimsRGBA &in, OUTP & out ) const;
92  };
93 
94  template<class OUTP>
95  inline void
96  RawConverter<AimsRGBA,OUTP>::convert( const AimsRGBA &in, OUTP & out ) const
97  {
98  out = (OUTP) ( sqrt( ( (double) in.red() ) * in.red()
99  + ( (double) in.green() ) * in.green()
100  + ( (double) in.blue() ) * in.blue() ) );
101  }
102 
103  template <>
105  {
106  public :
107  void convert( const AimsRGB &in, AimsRGBA & out ) const
108  {
109  out = in;
110  }
111  };
112 
113 
114  template <>
116  {
117  public :
118  void convert( const AimsRGBA &in, AimsRGB & out ) const
119  {
120  out = in;
121  }
122  };
123 
124 }
125 
126 //--- Vector/RGB conversion --------------------------------------------------
127 #include <aims/data/data_g.h>
128 #include <aims/vector/vector.h>
129 #include <limits>
130 
131 namespace carto {
132 
133  template <typename T, int D>
135  {
136  public:
137  void convert( const AimsVector<T,D> &in, AimsRGB &out ) const
138  {
139  for( uint8_t i=0; i<3 && i<in.size(); ++i )
140  out[i] = in[i];
141  }
142  };
143 
144 
145  template <typename T, int D>
147  {
148  public:
149  void convert( const AimsRGB &in, AimsVector<T,D> &out ) const
150  {
151  for( uint8_t i=0; i<3 && i<out.size(); ++i )
152  out[i] = in[i];
153  }
154  };
155 
156 
157  template <typename T, int D>
159  {
160  public:
161  void convert( const AimsVector<T,D> &in, AimsRGBA &out ) const
162  {
163  for( uint8_t i=0; i<4 && i<in.size(); ++i )
164  out[i] = in[i];
165  }
166  };
167 
168 
169  template <typename T, int D>
171  {
172  public:
173  void convert( const AimsRGBA &in, AimsVector<T,D> &out ) const
174  {
175  for( uint8_t i=0; i<4 && i<out.size(); ++i )
176  out[i] = in[i];
177  }
178  };
179 
180 
181 //--- Vector/RGB rescaler ----------------------------------------------------
182 
183  template <typename T, int D>
185  {
186  public:
187  Rescaler();
188  Rescaler( const RescalerInfo & info );
189 
190  void convert( const AimsData<AimsVector<T,D> > &in, AimsData<AimsRGB> & out ) const;
191 
192  private:
193  RescalerInfo _info;
194  };
195 
196  template <typename T, int D>
197  inline
199  _info()
200  {}
201 
202  template <typename T, int D>
203  inline
205  const RescalerInfo & info ):
206  _info(info)
207  {}
208 
209  template <typename T, int D>
210  void
212  const AimsData<AimsVector<T,D> > &in, AimsData<AimsRGB> &out ) const
213  {
214  if( out.dimX() == 0 )
215  out = AimsData<AimsRGB>( in.dimX(), in.dimY(), in.dimZ(),
216  in.dimT(), in.borderWidth() );
217 
218  out.setSizeX( in.sizeX() );
219  out.setSizeY( in.sizeY() );
220  out.setSizeZ( in.sizeZ() );
221  out.setSizeT( in.sizeT() );
222 
223  if( in.header() )
224  out.setHeader( in.header()->cloneHeader( true ) );
225 
226  RescalerInfo info( _info );
227  if( std::isnan( info.vmin ) ) {
228  if ( ! info.usevtypelimits ) {
230  for( int k=0; k<in.dimZ(); ++k )
231  for( int j=0; j<in.dimY(); ++j )
232  for( int i=0; i<in.dimX(); ++i )
233  {
234  for( int c = 0; c < in(i, j, k).size(); ++c )
235  if( (double) in(i, j, k)[c] < info.vmin )
236  info.vmin = (double) in(i, j, k)[c];
237  }
238  }
239  }
240 
241  if( std::isnan( info.vmax ) ) {
242  if ( ! info.usevtypelimits ) {
244  for( int k=0; k<in.dimZ(); ++k )
245  for( int j=0; j<in.dimY(); ++j )
246  for( int i=0; i<in.dimX(); ++i )
247  {
248  for( int c = 0; c < in(i, j, k).size(); ++c )
249  if( (double) in(i, j, k)[c] > info.vmax )
250  info.vmax = (double) in(i, j, k)[c];
251  }
252  }
253  }
254 
255  DefaultedRescalerInfo<T, uint8_t> defaultedinfo( info );
256 
257  int x, y, z, t, dx = out.dimX(), dy = out.dimY(), dz = out.dimZ(),
258  dt = out.dimT(), ox = dx, oy = dy, oz = dz, ot = dt;
259  if( in.dimX() < dx )
260  dx = in.dimX();
261  if( in.dimY() < dy )
262  dy = in.dimY();
263  if( in.dimZ() < dz )
264  dz = in.dimZ();
265  if( in.dimT() < dt )
266  dt = in.dimT();
267 
268  for( t=0; t<dt; ++t )
269  {
270  for( z=0; z<dz; ++z )
271  {
272  for( y=0; y<dy; ++y )
273  {
274  for( x=0; x<dx; ++x )
275  for( int c=0; c < in( x, y, z, t ).size() && c<3; ++c )
276  out( x, y, z, t )[c] =
277  defaultedinfo.getScaledValue( in( x, y, z, t )[c] );
278  for( ; x<ox; ++x )
279  out( x, y, z, t ) = AimsRGB(0);
280  }
281  for( ; y<oy; ++y )
282  for( x=0; x<ox; ++x )
283  out( x, y, z, t ) = AimsRGB(0);
284  }
285  for( ; z<oz; ++z )
286  for( y=0; y<oy; ++y )
287  for( x=0; x<ox; ++x )
288  out( x, y, z, t ) = AimsRGB(0);
289  }
290 
291  for( ; t<ot; ++t )
292  for( z=0; z<oz; ++z )
293  for( y=0; y<oy; ++y )
294  for( x=0; x<ox; ++x )
295  out( x, y, z, t ) = AimsRGB(0);
296 
297  float scf = 1.;
299  *h = dynamic_cast<aims::PythonHeader *>( out.header() );
300  if( !h )
301  h = new aims::PythonHeader;
302  h->getProperty( "scale_factor", scf );
303  scf *= defaultedinfo.getScale();
304  h->setProperty( "scale_factor", scf );
305  h->setProperty( "data_type", DataTypeCode<AimsRGB>::dataType() );
306  if( h->hasProperty( "disk_data_type" ) )
307  h->removeProperty( "disk_data_type" );
308  }
309 
310  template <typename T, int D>
312  {
313  public:
314  Rescaler();
315  Rescaler( const RescalerInfo & info );
316 
317  void convert( const AimsData<AimsRGB> &in, AimsData<AimsVector<T,D> > & out ) const;
318 
319  private:
320  RescalerInfo _info;
321  };
322 
323  template <typename T, int D>
324  inline
326  _info()
327  {}
328 
329  template <typename T, int D>
330  inline
332  const RescalerInfo & info ):
333  _info(info)
334  {}
335 
336  template <typename T, int D>
337  void
339  const AimsData<AimsRGB> &in, AimsData<AimsVector<T,D> > &out ) const
340  {
341  if( out.dimX() == 0 )
342  out = AimsData<AimsVector<T,D> >( in.dimX(), in.dimY(), in.dimZ(),
343  in.dimT(), in.borderWidth() );
344 
345  out.setSizeX( in.sizeX() );
346  out.setSizeY( in.sizeY() );
347  out.setSizeZ( in.sizeZ() );
348  out.setSizeT( in.sizeT() );
349 
350  if( in.header() )
351  out.setHeader( in.header()->cloneHeader( true ) );
352 
353  RescalerInfo info( _info );
354  if( std::isnan( info.vmin ) ) {
355  if ( ! info.usevtypelimits ) {
357  for( int k=0; k<in.dimZ(); ++k )
358  for( int j=0; j<in.dimY(); ++j )
359  for( int i=0; i<in.dimX(); ++i )
360  {
361  for( int c = 0; c < 3; ++c )
362  if( (double) in(i, j, k)[c] < info.vmin )
363  info.vmin = (double) in(i, j, k)[c];
364  }
365  }
366  }
367 
368  if( std::isnan( info.vmax ) ) {
369  if ( ! info.usevtypelimits ) {
371  for( int k=0; k<in.dimZ(); ++k )
372  for( int j=0; j<in.dimY(); ++j )
373  for( int i=0; i<in.dimX(); ++i )
374  {
375  for( int c = 0; c < 3; ++c )
376  if( (double) in(i, j, k)[c] > info.vmax )
377  info.vmax = (double) in(i, j, k)[c];
378  }
379  }
380  }
381 
382  DefaultedRescalerInfo<uint8_t, T> defaultedinfo( info );
383 
384  int x, y, z, t, dx = out.dimX(), dy = out.dimY(), dz = out.dimZ(),
385  dt = out.dimT(), ox = dx, oy = dy, oz = dz, ot = dt;
386  if( in.dimX() < dx )
387  dx = in.dimX();
388  if( in.dimY() < dy )
389  dy = in.dimY();
390  if( in.dimZ() < dz )
391  dz = in.dimZ();
392  if( in.dimT() < dt )
393  dt = in.dimT();
394 
395  for( t=0; t<dt; ++t )
396  {
397  for( z=0; z<dz; ++z )
398  {
399  for( y=0; y<dy; ++y )
400  {
401  for( x=0; x<dx; ++x )
402  for( int c=0; c < out( x, y, z, t ).size() && c<3; ++c )
403  out( x, y, z, t )[c] =
404  defaultedinfo.getScaledValue( in( x, y, z, t )[c] );
405  for( ; x<ox; ++x )
406  out( x, y, z, t ) = AimsRGB(0);
407  }
408  for( ; y<oy; ++y )
409  for( x=0; x<ox; ++x )
410  out( x, y, z, t ) = AimsRGB(0);
411  }
412  for( ; z<oz; ++z )
413  for( y=0; y<oy; ++y )
414  for( x=0; x<ox; ++x )
415  out( x, y, z, t ) = AimsRGB(0);
416  }
417 
418  for( ; t<ot; ++t )
419  for( z=0; z<oz; ++z )
420  for( y=0; y<oy; ++y )
421  for( x=0; x<ox; ++x )
422  out( x, y, z, t ) = AimsRGB(0);
423 
424  float scf = 1.;
426  *h = dynamic_cast<aims::PythonHeader *>( out.header() );
427  if( !h )
428  h = new aims::PythonHeader;
429  h->getProperty( "scale_factor", scf );
430  scf *= defaultedinfo.getScale();
431  h->setProperty( "scale_factor", scf );
432  h->setProperty( "data_type", DataTypeCode<AimsRGB>::dataType() );
433  if( h->hasProperty( "disk_data_type" ) )
434  h->removeProperty( "disk_data_type" );
435  }
436 
437 }
438 
439 #endif
440 
441 
carto::VoxelRGB AimsRGB
Definition: rgb.h:43
void setSizeT(float sizet)
void convert(const AimsRGBA &in, AimsRGB &out) const
void convert(const INP &in, OUTP &out) const
int dimZ() const
virtual bool getProperty(const std::string &, Object &) const
const uint8_t & blue() const
Attributed python-like header, stores all needed information about an object, currently used for volu...
Definition: pheader.h:51
float sizeZ() const
const uint8_t & red() const
int dimY() const
void convert(const AimsVector< T, D > &in, AimsRGB &out) const
float sizeT() const
void convert(const AimsVector< T, D > &in, AimsRGBA &out) const
void setSizeX(float sizex)
virtual bool removeProperty(const std::string &)
void setSizeY(float sizey)
OUTP getScaledValue(INP value) const
void setHeader(aims::Header *hdr)
int size() const
float sizeX() const
virtual Header * cloneHeader(bool keepUuid=false) const =0
static _Tp min()
void convert(const AimsRGB &in, AimsVector< T, D > &out) const
const aims::Header * header() const
float sizeY() const
int dimT() const
const uint8_t & green() const
virtual void setProperty(const std::string &, Object)
uint8_t byte
const uint8_t & blue() const
virtual bool hasProperty(const std::string &) const
void convert(const AimsRGB &in, AimsRGBA &out) const
void setSizeZ(float sizez)
void convert(const INP &in, OUTP &out) const
const uint8_t & green() const
carto::VoxelRGBA AimsRGBA
Definition: rgb.h:44
const uint8_t & red() const
int dimX() const
int borderWidth() const
Usefull offsets for A.I.M.S.
Definition: border.h:135
void convert(const AimsRGBA &in, AimsVector< T, D > &out) const
static _Tp max()