anatomist 6.0.18
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 );
172 ColorTraits( const AObjectPalette *palette, const T & mini, const T & maxi,
173 double min1, double max1, double zero1 );
174 AimsRGBA color( const T & ) const;
176 T neutralColor() const;
177 // val0 is the pixel index in the output cmap, px in the input palette
178 void paletteCoords( double val0, double val1, int & px, int & py ) const;
179 void paletteCoord( int dim, double val0, int & px ) const;
180 void paletteCoord0( double val0, int & px ) const;
181 void paletteCoord1( double val0, int & px ) const;
182
183
184 private:
185 typename internal::ColorTraitsType<T>::traitstype _traitstype;
186 };
187
188
189 // implementation specializations
190
191 namespace internal
192 {
193
194/* template<> struct ColorTraitsType<AimsRGB>
195 {
196 typedef ColorNoPaletteTraits<AimsRGB> traitstype;
197 };
198
199 template<> struct ColorTraitsType<AimsRGBA>
200 {
201 typedef ColorNoPaletteTraits<AimsRGBA> traitstype;
202 };
203*/
204
205 }
206
207
208 template<typename T> inline
209 ColorTraits<T>::ColorTraits( const AObjectPalette* pal, const T & mini,
210 const T & maxi, const T & mini2,
211 const T & maxi2,
212 double min1, double max1, double zero1,
213 double min2, double max2, double zero2 )
214 : _traitstype( pal, mini, maxi, mini2, maxi2, min1, max1, zero1,
215 min2, max2, zero2 )
216 {
217 }
218
219
220 template<typename T> inline
221 ColorTraits<T>::ColorTraits( const AObjectPalette* pal, const T & mini,
222 const T & maxi, double min1, double max1,
223 double zero1 )
224 : _traitstype( pal, mini, maxi, min1, max1, zero1 )
225 {
226 }
227
228 template <typename T> inline
229 AimsRGBA ColorTraits<T>::color( const T & in ) const
230 {
231 return _traitstype.color( in );
232 }
233
234 template <typename T> inline
236 {
237 return _traitstype.neutralColor();
238 }
239
240 template <typename T> inline
241 void ColorTraits<T>::paletteCoords( double val0, double val1,
242 int & px, int & py ) const
243 {
244 _traitstype.paletteCoords( val0, val1, px, py );
245
246 }
247
248 template <typename T> inline
249 void ColorTraits<T>::paletteCoord( int dim, double val0, int & px ) const
250 {
251 _traitstype.paletteCoord( dim, val0, px );
252 }
253
254
255 template <typename T> inline
256 void ColorTraits<T>::paletteCoord0( double val0, int & px ) const
257 {
258 _traitstype.paletteCoord0( val0, px );
259 }
260
261
262 template <typename T> inline
263 void ColorTraits<T>::paletteCoord1( double val0, int & px ) const
264 {
265 _traitstype.paletteCoord1( val0, px );
266 }
267
268 // ---
269
270 template<typename T> inline
272 const T &, const T &,
273 double, double )
274 {
275 }
276
277 template <typename T> inline
279 {
280 // this assumes AimsRGBA::AimsRGBA(const T&) is defined on T
281 return in;
282 }
283
284 template <typename T> inline
286 {
287 return 0;
288 }
289
290 template <> inline
292 {
293 // transparent color
294 return AimsRGBA( 0, 0, 0, 0 );
295 }
296
297 // ---
298
299 template <typename T> inline
301 * pal,
302 const T & mini,
303 const T & maxi,
304 const T & mini2,
305 const T & maxi2,
306 double min1,
307 double max1,
308 double zero1,
309 double min2,
310 double max2,
311 double zero2 )
312 : palette( pal )
313 {
314 setup( mini, maxi, mini2, maxi2, min1, max1, zero1, min2, max2, zero2 );
315 }
316
317
318 template <typename T> inline
320 * pal,
321 const T & mini,
322 const T & maxi,
323 double min1,
324 double max1,
325 double zero1 )
326 : palette( pal )
327 {
328 setup( mini, maxi, mini, maxi, min1, max1, zero1, 0., 1., 0. );
329 }
330
331
332 template <typename T> inline
334 double val0, double val1, int & ival0, int & ival1 ) const
335 {
336 paletteCoord0( val0, ival0 );
337 paletteCoord1( val1, ival1 );
338 }
339
340
341 template <typename T> inline
343 int dim, double val0, int & ival0 ) const
344 {
345 if( dim != 0 )
346 paletteCoord1( val0, ival0 );
347 else
348 paletteCoord0( val0, ival0 );
349 }
350
351
352 template <typename T> inline
354 double val0, int & ival0 ) const
355 {
356 double fval0;
357
358 if( !palette->zeroCenteredAxis1() )
359 {
360 // Comparisons are written this way to accommodate NaN and Inf
361 fval0 = scale0 * val0 + decal0;
362
363 if( fval0 > cmin0 && fval0 < cmax0 )
364 {
365 ival0 = static_cast<int>( fval0 );
366 }
367 else if( fval0 <= cmin0 )
368 ival0 = cmin0;
369 else if( fval0 >= cmax0 )
370 ival0 = cmax0;
371 else
372 ival0 = cmin0;
373 }
374 else
375 {
376 if( val0 >= czero0 )
377 {
378 fval0 = scale0 * val0 + decal0;
379 if( fval0 > cmin0 && fval0 < cmax0 )
380 ival0 = static_cast<int>( fval0 );
381 else if( fval0 <= cmin0 )
382 ival0 = cmin0;
383 else if( fval0 >= cmax0 )
384 ival0 = cmax0;
385 else
386 ival0 = cmin0;
387 }
388 else
389 {
390 fval0 = scalen0 * val0 + decaln0;
391 if( fval0 > cminn0 && fval0 < cmaxn0 )
392 ival0 = static_cast<int>( fval0 );
393 else if( fval0 <= cminn0 )
394 ival0 = cminn0;
395 else if( fval0 >= cmaxn0 )
396 ival0 = cmaxn0;
397 else
398 ival0 = cminn0;
399 }
400 }
401 }
402
403
404 template <typename T> inline
406 double val0, int & ival0 ) const
407 {
408 if( ( !palette->is2dMode()
409 && palette->palette1DMapping() == AObjectPalette::FIRSTLINE )
410 || colors->getSizeY() == 1 )
411 ival0 = 0 ;
412 else
413 {
414 double fval0;
415
416 if( !palette->zeroCenteredAxis2() )
417 {
418 // Comparisons are written this way to accommodate NaN and Inf
419 fval0 = scale1 * val0 + decal1;
420
421 if( fval0 > cmin1 && fval0 < cmax1 )
422 {
423 ival0 = static_cast<int>( fval0 );
424 }
425 else if( fval0 <= cmin1 )
426 ival0 = cmin1;
427 else if( fval0 >= cmax1 )
428 ival0 = cmax1;
429 else
430 ival0 = cmin1;
431 }
432 else
433 {
434 if( val0 >= czero1 )
435 {
436 fval0 = scale1 * val0 + decal1;
437 if( fval0 > cmin1 && fval0 < cmax1 )
438 ival0 = static_cast<int>( fval0 );
439 else if( fval0 <= cmin1 )
440 ival0 = cmin1;
441 else if( fval0 >= cmax1 )
442 ival0 = cmax1;
443 else
444 ival0 = cmin1;
445 }
446 else
447 {
448 fval0 = scalen1 * val0 + decaln1;
449 if( fval0 > cminn1 && fval0 < cmaxn1 )
450 ival0 = static_cast<int>( fval0 );
451 else if( fval0 <= cminn1 )
452 ival0 = cminn1;
453 else if( fval0 >= cmaxn1 )
454 ival0 = cmaxn1;
455 else
456 ival0 = cminn1;
457 }
458 }
459 }
460 }
461
462
463 template <typename T> inline
465 {
466 int ival0;
467 paletteCoord0( in, ival0 );
468
469 return colors->at( ival0, 0 );
470 }
471
472 template <typename T> inline
474 {
475 return 0;
476 }
477
478 template <> inline
480 {
481 AimsRGBA col;
482
483 int ival0;
484
485 paletteCoord0( in.red(), ival0 );
486 col[0] = colors->at( ival0, 0 )[0];
487
488 paletteCoord0( in.green(), ival0 );
489 col[1] = colors->at( ival0, 0 )[1];
490
491 paletteCoord0( in.blue(), ival0 );
492 col[2] = colors->at( ival0, 0 )[2];
493
494 double val = static_cast<double>( std::sqrt( in.red() * in.red()
495 + in.green() * in.green()
496 + in.blue() * in.blue() ) );
497 paletteCoord0( val, ival0 );
498 col[3] = colors->at( ival0, 0 )[3];
499
500 return col;
501 }
502
503
504 template <> inline
506 const
507 {
508 AimsRGBA col;
509
510 int ival0;
511
512 paletteCoord0( in.red(), ival0 );
513 col[0] = colors->at( ival0, 0 )[0];
514
515 paletteCoord0( in.green(), ival0 );
516 col[1] = colors->at( ival0, 0 )[1];
517
518 paletteCoord0( in.blue(), ival0 );
519 col[2] = colors->at( ival0, 0 )[2];
520
521 paletteCoord0( in.alpha(), ival0 );
522 col[3] = colors->at( ival0, 0 )[3];
523
524 return col;
525 }
526
527}
528
529#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