aimsdata 6.0.0
Neuroimaging data handling
converter_hsv.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_HSV_H
35#define AIMS_UTILITY_CONVERTER_HSV_H
36
37#include <cartobase/type/string_conversion.h>
38#include <cartobase/type/converter.h>
39#include <aims/rgb/rgb.h>
41#include <aims/hsv/hsv.h>
42
43#define MIN3(x,y,z) ((y) <= (z) ? \
44 ((x) <= (y) ? (x) : (y)) \
45 : \
46 ((x) <= (z) ? (x) : (z)))
47
48#define MAX3(x,y,z) ((y) >= (z) ? \
49 ((x) >= (y) ? (x) : (y)) \
50 : \
51 ((x) >= (z) ? (x) : (z)))
52
53
54namespace carto
55{
56 template <class T> inline
57 void rgbtohsv( const T &in, AimsHSV & out )
58 {
59 ::byte rgb_min, rgb_max;
60 rgb_min = MIN3(in.red(), in.green(), in.blue());
61 rgb_max = MAX3(in.red(), in.green(), in.blue());
62
63 /* Compute value */
64 out.value() = rgb_max;
65 if (out.value() == 0) {
66 out.hue() = out.saturation() = 0;
67 }
68 else {
69 /* Compute saturation */
70 out.saturation() = uint8_t( rint(255.0 * double(rgb_max - rgb_min)
71 / out.value()) );
72 if (out.saturation() == 0) {
73 out.hue() = 0;
74 }
75 else {
76 /* Compute hue */
77 if (rgb_max == in.red()) {
78 out.hue() = uint8_t( rint(43.0 * double(in.green()
79 - in.blue())/(rgb_max - rgb_min)) );
80 } else if (rgb_max == in.green()) {
81 out.hue() = 85 + uint8_t( rint(43.0 * double(in.blue()
82 - in.red())/(rgb_max - rgb_min)) );
83 } else /* rgb_max == rgb.b */ {
84 out.hue() = 171 + uint8_t( rint(43.0 * double(in.red()
85 - in.green())/(rgb_max - rgb_min)) );
86 }
87 }
88 }
89 }
90
91 template <class T> inline
92 void hsvtorgb( const AimsHSV &in, T & out )
93 {
94 double f, h, s, v, r0 = 360.0 / 255, r1 = 1.0 / 255;
95 long i, p, q, t;
96
97 if( in.saturation() == 0 )
98 {
99 out.red() = out.green() = out.blue() = in.value();
100 return;
101 }
102
103 h = double( in.hue() ) * r0;
104 s = double( in.saturation() ) * r1;
105 v = double( in.value() ) * r1;
106 i = long( floor( h / 60 ) ) % 6;
107 f = h / 60 - i;
108 p = ::byte( v * ( 1.0 - s ) * 255 );
109 q = ::byte( v * ( 1.0 - f * s ) * 255 );
110 t = ::byte( v * ( 1.0 - ( 1.0 - f ) * s ) * 255 );
111
112 switch( i ) {
113 case 0:
114 out.red() = in.value();
115 out.green() = t;
116 out.blue() = p;
117 break;
118 case 1:
119 out.red() = q;
120 out.green() = in.value();
121 out.blue() = p;
122 break;
123 case 2:
124 out.red() = p;
125 out.green() = in.value();
126 out.blue() = t;
127 break;
128 case 3:
129 out.red() = p;
130 out.green() = q;
131 out.blue() = in.value();
132 break;
133 case 4:
134 out.red() = t;
135 out.green() = p;
136 out.blue() = in.value();
137 break;
138 default:
139 out.red() = in.value();
140 out.green() = p;
141 out.blue() = q;
142 break;
143 }
144 }
145
146 template <class INP>
148 {
149 public :
150 void convert( const INP &in, AimsHSV & out ) const;
151 };
152
153 template<class INP>
154 inline void
155 RawConverter<INP,AimsHSV>::convert( const INP &in, AimsHSV & out ) const
156 {
157 AimsRGB tmp( (::byte) in, (::byte) in, (::byte) in );
158 rgbtohsv<AimsRGB>( tmp, out );
159 }
160
161 template <class OUTP>
163 {
164 public :
165 void convert( const AimsHSV &in, OUTP & out ) const;
166 };
167
168 template<class OUTP>
169 inline void
170 RawConverter<AimsHSV,OUTP>::convert( const AimsHSV &in, OUTP & out ) const
171 {
172 // First convert to RGB
173 AimsRGB tmp;
174 hsvtorgb<AimsRGB>( in, tmp );
175 out = (OUTP) ( sqrt( ( (double) tmp.red() ) * tmp.red()
176 + ( (double) tmp.green() ) * tmp.green()
177 + ( (double) tmp.blue() ) * tmp.blue() ) );
178 }
179
180 template <>
182 {
183 public :
184 void convert( const AimsHSV &in, AimsRGB & out ) const
185 {
186 hsvtorgb<AimsRGB>( in, out );
187 }
188 };
189
190 template <>
192 {
193 public :
194 void convert( const AimsHSV &in, AimsRGBA & out ) const
195 {
196 hsvtorgb<AimsRGBA>( in, out );
197 }
198 };
199
200
201 template <>
203 {
204 public :
205 void convert( const AimsRGB &in, AimsHSV & out ) const
206 {
207 rgbtohsv<AimsRGB>( in, out );
208 }
209 };
210
211 template <>
213 {
214 public :
215 void convert( const AimsRGBA &in, AimsHSV & out ) const
216 {
217 rgbtohsv<AimsRGBA>( in, out );
218 }
219 };
220
221}
222
223#endif
224
225
void convert(const AimsHSV &in, AimsRGBA &out) const
void convert(const AimsHSV &in, AimsRGB &out) const
void convert(const AimsRGBA &in, AimsHSV &out) const
void convert(const AimsRGB &in, AimsHSV &out) const
void convert(const INP &in, OUTP &out) const
const uint8_t & saturation() const
const uint8_t & value() const
const uint8_t & hue() const
const uint8_t & red() const
const uint8_t & green() const
const uint8_t & blue() const
#define MAX3(x, y, z)
#define MIN3(x, y, z)
carto::VoxelHSV AimsHSV
Definition hsv.h:41
void hsvtorgb(const AimsHSV &in, T &out)
void rgbtohsv(const T &in, AimsHSV &out)
carto::VoxelRGBA AimsRGBA
Definition rgb.h:44
carto::VoxelRGB AimsRGB
Definition rgb.h:43
uint8_t byte