anatomist 6.0.4
3D neuroimaging data viewer
colortraits.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
35#ifndef ANA_COLOR_COLORTRAITS_H
36#define ANA_COLOR_COLORTRAITS_H
37
39#include <aims/def/general.h>
40#include <aims/vector/vector.h>
41#include <aims/rgb/rgb.h>
42
43namespace anatomist
44{
45
46 template<typename T> class ColorScalarPaletteTraits
47 {
48 public:
49 ColorScalarPaletteTraits( const AObjectPalette* pal, const T & mini,
50 const T & maxi, const T & mini2,
51 const T & maxi2,
52 double min1, double max1, double zero1,
53 double min2, double max2, double zero2 );
54 ColorScalarPaletteTraits( const AObjectPalette* pal, const T & mini,
55 const T & maxi,
56 double min1, double max1, double zero1 );
57 AimsRGBA color( const T & ) const;
58 void setup( const T & mini, const T & maxi,
59 const T & mini2, const T & maxi2,
60 double min1, double max1, double zero1,
61 double min2, double max2, double zero2 );
62 void setup1D( int dim, const T & mini, const T & maxi,
63 double min1, double max1, double zero );
64 T neutralColor() const;
65 void paletteCoords( double val0, double val1, int & px, int & py ) const;
66 void paletteCoord( int dim, double val0, int & px ) const;
67 void paletteCoord0( double val0, int & px ) const;
68 void paletteCoord1( double val0, int & px ) const;
69
70 private:
71 const AObjectPalette *palette;
72 const carto::Volume<AimsRGBA> *colors;
73 double scale0;
74 double scale1;
75 double decal0;
76 double decal1;
77 int cmin0;
78 int cmin1;
79 int cmax0;
80 int cmax1;
81 // for negative 0-centered values
82 int czero0;
83 int czero1;
84 double scalen0;
85 double scalen1;
86 double decaln0;
87 double decaln1;
88 int cminn0;
89 int cminn1;
90 int cmaxn0;
91 int cmaxn1;
92
93 };
94
95
96 template<typename T> class ColorNoPaletteTraits
97 {
98 public:
99 ColorNoPaletteTraits( const AObjectPalette*, const T & mini,
100 const T & maxi, double min, double max );
101 AimsRGBA color( const T & ) const;
102 T neutralColor() const;
103 };
104
105 namespace internal
106 {
107
109 template<typename T> struct ColorTraitsType
110 {
112 };
113
114 }
115
117
129 template<typename T> class ColorTraits
130 {
131 public:
134 ColorTraits( const AObjectPalette *palette, const T & mini, const T & maxi,
135 const T & mini2, const T & maxi2,
136 double min1, double max1, double zero1, double min2, double max2,
137 double zero2 );
169 ColorTraits( const AObjectPalette *palette, const T & mini, const T & maxi,
170 double min1, double max1, double zero1 );
171 AimsRGBA color( const T & ) const;
173 T neutralColor() const;
174 // val0 is the pixel index in the output cmap, px in the input palette
175 void paletteCoords( double val0, double val1, int & px, int & py ) const;
176 void paletteCoord( int dim, double val0, int & px ) const;
177 void paletteCoord0( double val0, int & px ) const;
178 void paletteCoord1( double val0, int & px ) const;
179
180
181 private:
182 typename internal::ColorTraitsType<T>::traitstype _traitstype;
183 };
184
185
186 // implementation specializations
187
188 namespace internal
189 {
190
191/* template<> struct ColorTraitsType<AimsRGB>
192 {
193 typedef ColorNoPaletteTraits<AimsRGB> traitstype;
194 };
195
196 template<> struct ColorTraitsType<AimsRGBA>
197 {
198 typedef ColorNoPaletteTraits<AimsRGBA> traitstype;
199 };
200*/
201
202 }
203
204
205 template<typename T> inline
206 ColorTraits<T>::ColorTraits( const AObjectPalette* pal, const T & mini,
207 const T & maxi, const T & mini2,
208 const T & maxi2,
209 double min1, double max1, double zero1,
210 double min2, double max2, double zero2 )
211 : _traitstype( pal, mini, maxi, mini2, maxi2, min1, max1, zero1,
212 min2, max2, zero2 )
213 {
214 }
215
216
217 template<typename T> inline
218 ColorTraits<T>::ColorTraits( const AObjectPalette* pal, const T & mini,
219 const T & maxi, double min1, double max1,
220 double zero1 )
221 : _traitstype( pal, mini, maxi, min1, max1, zero1 )
222 {
223 }
224
225 template <typename T> inline
226 AimsRGBA ColorTraits<T>::color( const T & in ) const
227 {
228 return _traitstype.color( in );
229 }
230
231 template <typename T> inline
233 {
234 return _traitstype.neutralColor();
235 }
236
237 template <typename T> inline
238 void ColorTraits<T>::paletteCoords( double val0, double val1,
239 int & px, int & py ) const
240 {
241 _traitstype.paletteCoords( val0, val1, px, py );
242
243 }
244
245 template <typename T> inline
246 void ColorTraits<T>::paletteCoord( int dim, double val0, int & px ) const
247 {
248 _traitstype.paletteCoord( dim, val0, px );
249 }
250
251
252 template <typename T> inline
253 void ColorTraits<T>::paletteCoord0( double val0, int & px ) const
254 {
255 _traitstype.paletteCoord0( val0, px );
256 }
257
258
259 template <typename T> inline
260 void ColorTraits<T>::paletteCoord1( double val0, int & px ) const
261 {
262 _traitstype.paletteCoord1( val0, px );
263 }
264
265 // ---
266
267 template<typename T> inline
269 const T &, const T &,
270 double, double )
271 {
272 }
273
274 template <typename T> inline
276 {
277 // this assumes AimsRGBA::AimsRGBA(const T&) is defined on T
278 return in;
279 }
280
281 template <typename T> inline
283 {
284 return 0;
285 }
286
287 template <> inline
289 {
290 // transparent color
291 return AimsRGBA( 0, 0, 0, 0 );
292 }
293
294 // ---
295
296 template <typename T> inline
298 * pal,
299 const T & mini,
300 const T & maxi,
301 const T & mini2,
302 const T & maxi2,
303 double min1,
304 double max1,
305 double zero1,
306 double min2,
307 double max2,
308 double zero2 )
309 : palette( pal )
310 {
311 setup( mini, maxi, mini2, maxi2, min1, max1, zero1, min2, max2, zero2 );
312 }
313
314
315 template <typename T> inline
317 * pal,
318 const T & mini,
319 const T & maxi,
320 double min1,
321 double max1,
322 double zero1 )
323 : palette( pal )
324 {
325 setup( mini, maxi, mini, maxi, min1, max1, zero1, 0., 1., 0. );
326 }
327
328
329 template <typename T> inline
331 double val0, double val1, int & ival0, int & ival1 ) const
332 {
333 paletteCoord0( val0, ival0 );
334 paletteCoord1( val0, ival0 );
335 }
336
337
338 template <typename T> inline
340 int dim, double val0, int & ival0 ) const
341 {
342 if( dim != 0 )
343 paletteCoord1( val0, ival0 );
344 else
345 paletteCoord0( val0, ival0 );
346 }
347
348
349 template <typename T> inline
351 double val0, int & ival0 ) const
352 {
353 double fval0;
354
355 if( !palette->zeroCenteredAxis1() )
356 {
357 // Comparisons are written this way to accommodate NaN and Inf
358 fval0 = scale0 * val0 + decal0;
359
360 if( fval0 > cmin0 && fval0 < cmax0 )
361 {
362 ival0 = static_cast<int>( fval0 );
363 }
364 else if( fval0 <= cmin0 )
365 ival0 = cmin0;
366 else if( fval0 >= cmax0 )
367 ival0 = cmax0;
368 else
369 ival0 = cmin0;
370 }
371 else
372 {
373 if( val0 >= czero0 )
374 {
375 fval0 = scale0 * val0 + decal0;
376 if( fval0 > cmin0 && fval0 < cmax0 )
377 ival0 = static_cast<int>( fval0 );
378 else if( fval0 <= cmin0 )
379 ival0 = cmin0;
380 else if( fval0 >= cmax0 )
381 ival0 = cmax0;
382 else
383 ival0 = cmin0;
384 }
385 else
386 {
387 fval0 = scalen0 * val0 + decaln0;
388 if( fval0 > cminn0 && fval0 < cmaxn0 )
389 ival0 = static_cast<int>( fval0 );
390 else if( fval0 <= cminn0 )
391 ival0 = cminn0;
392 else if( fval0 >= cmaxn0 )
393 ival0 = cmaxn0;
394 else
395 ival0 = cminn0;
396 }
397 }
398 }
399
400
401 template <typename T> inline
403 double val0, int & ival0 ) const
404 {
405 if( ( !palette->is2dMode()
406 && palette->palette1DMapping() == AObjectPalette::FIRSTLINE )
407 || colors->getSizeY() == 1 )
408 ival0 = 0 ;
409 else
410 {
411 double fval0;
412
413 if( !palette->zeroCenteredAxis2() )
414 {
415 // Comparisons are written this way to accommodate NaN and Inf
416 fval0 = scale1 * val0 + decal1;
417
418 if( fval0 > cmin1 && fval0 < cmax1 )
419 {
420 ival0 = static_cast<int>( fval0 );
421 }
422 else if( fval0 <= cmin1 )
423 ival0 = cmin1;
424 else if( fval0 >= cmax1 )
425 ival0 = cmax1;
426 else
427 ival0 = cmin1;
428 }
429 else
430 {
431 if( val0 >= czero1 )
432 {
433 fval0 = scale1 * val0 + decal1;
434 if( fval0 > cmin1 && fval0 < cmax1 )
435 ival0 = static_cast<int>( fval0 );
436 else if( fval0 <= cmin1 )
437 ival0 = cmin1;
438 else if( fval0 >= cmax1 )
439 ival0 = cmax1;
440 else
441 ival0 = cmin1;
442 }
443 else
444 {
445 fval0 = scalen1 * val0 + decaln1;
446 if( fval0 > cminn1 && fval0 < cmaxn1 )
447 ival0 = static_cast<int>( fval0 );
448 else if( fval0 <= cminn1 )
449 ival0 = cminn1;
450 else if( fval0 >= cmaxn1 )
451 ival0 = cmaxn1;
452 else
453 ival0 = cminn1;
454 }
455 }
456 }
457 }
458
459
460 template <typename T> inline
462 {
463 int ival0;
464 paletteCoord0( in, ival0 );
465
466 return colors->at( ival0, 0 );
467 }
468
469 template <typename T> inline
471 {
472 return 0;
473 }
474
475 template <> inline
477 {
478 AimsRGBA col;
479
480 int ival0;
481
482 paletteCoord0( in.red(), ival0 );
483 col[0] = colors->at( ival0, 0 )[0];
484
485 paletteCoord0( in.green(), ival0 );
486 col[1] = colors->at( ival0, 0 )[1];
487
488 paletteCoord0( in.blue(), ival0 );
489 col[2] = colors->at( ival0, 0 )[2];
490
491 double val = static_cast<double>( std::sqrt( in.red() * in.red()
492 + in.green() * in.green()
493 + in.blue() * in.blue() ) );
494 paletteCoord0( val, ival0 );
495 col[3] = colors->at( ival0, 0 )[3];
496
497 return col;
498 }
499
500
501 template <> inline
503 const
504 {
505 AimsRGBA col;
506
507 int ival0;
508
509 paletteCoord0( in.red(), ival0 );
510 col[0] = colors->at( ival0, 0 )[0];
511
512 paletteCoord0( in.green(), ival0 );
513 col[1] = colors->at( ival0, 0 )[1];
514
515 paletteCoord0( in.blue(), ival0 );
516 col[2] = colors->at( ival0, 0 )[2];
517
518 paletteCoord0( in.alpha(), ival0 );
519 col[3] = colors->at( ival0, 0 )[3];
520
521 return col;
522 }
523
524}
525
526#endif
AimsRGBA color(const T &) const
ColorNoPaletteTraits(const AObjectPalette *, const T &mini, const T &maxi, double min, double max)
void setup(const T &mini, const T &maxi, const T &mini2, const T &maxi2, double min1, double max1, double zero1, double min2, double max2, double zero2)
void paletteCoord(int dim, double val0, int &px) const
void paletteCoord0(double val0, int &px) const
void paletteCoord1(double val0, int &px) const
AimsRGBA color(const T &) const
void setup1D(int dim, const T &mini, const T &maxi, double min1, double max1, double zero)
void paletteCoords(double val0, double val1, int &px, int &py) const
ColorScalarPaletteTraits(const AObjectPalette *pal, const T &mini, const T &maxi, const T &mini2, const T &maxi2, double min1, double max1, double zero1, double min2, double max2, double zero2)
void paletteCoords(double val0, double val1, int &px, int &py) const
T neutralColor() const
returns a black / transparent / zero color
void paletteCoord0(double val0, int &px) const
void paletteCoord1(double val0, int &px) const
ColorTraits(const AObjectPalette *palette, const T &mini, const T &maxi, const T &mini2, const T &maxi2, double min1, double max1, double zero1, double min2, double max2, double zero2)
Constructor for 2D palettes.
AimsRGBA color(const T &) const
void paletteCoord(int dim, double val0, int &px) const
const uint8_t & alpha() const
const uint8_t & blue() const
const uint8_t & red() const
const uint8_t & green() const
const uint8_t & red() const
const uint8_t & green() const
const uint8_t & blue() const
T min(const Volume< T > &vol)
T max(const Volume< T > &vol)
carto::VoxelRGBA AimsRGBA
carto::VoxelRGB AimsRGB
switch to the right color traits type
ColorScalarPaletteTraits< T > traitstype