| OpenMesh

color_cast.hh
1 /* ========================================================================= *
2 * *
3 * OpenMesh *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openmesh.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenMesh. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 * ========================================================================= */
41
42
43
44
45 //=============================================================================
46 //
47 // Helper Functions for binary reading / writing
48 //
49 //=============================================================================
50
51
52 #ifndef OPENMESH_COLOR_CAST_HH
53 #define OPENMESH_COLOR_CAST_HH
54
55
56 //== INCLUDES =================================================================
57
58
59 #include <OpenMesh/Core/System/config.h>
60 #include <OpenMesh/Core/Utils/vector_cast.hh>
61
62 //== NAMESPACES ===============================================================
63
64
65 namespace OpenMesh {
66
67
68 //=============================================================================
69
70
74
75 //-----------------------------------------------------------------------------
76 #ifndef DOXY_IGNORE_THIS
77
79 template <typename dst_t, typename src_t>
80 struct color_caster
81 {
82 typedef dst_t return_type;
83
84 inline static return_type cast(const src_t& _src)
85 {
86 dst_t dst;
87 vector_cast(_src, dst, GenProg::Int2Type<vector_traits<dst_t>::size_>());
88 return dst;
89 }
90 };
91
92
93 template <>
94 struct color_caster<Vec3uc,Vec3f>
95 {
96 typedef Vec3uc return_type;
97
98 inline static return_type cast(const Vec3f& _src)
99 {
100 return Vec3uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
101 (unsigned char)(_src[1]* 255.0f + 0.5f),
102 (unsigned char)(_src[2]* 255.0f + 0.5f) );
103 }
104 };
105
106 template <>
107 struct color_caster<Vec3uc,Vec4f>
108 {
109 typedef Vec3uc return_type;
110
111 inline static return_type cast(const Vec4f& _src)
112 {
113 return Vec3uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
114 (unsigned char)(_src[1]* 255.0f + 0.5f),
115 (unsigned char)(_src[2]* 255.0f + 0.5f) );
116 }
117 };
118
119 template <>
120 struct color_caster<Vec3i,Vec3f>
121 {
122 typedef Vec3i return_type;
123
124 inline static return_type cast(const Vec3f& _src)
125 {
126 return Vec3i( (int)(_src[0]* 255.0f + 0.5f),
127 (int)(_src[1]* 255.0f + 0.5f),
128 (int)(_src[2]* 255.0f + 0.5f) );
129 }
130 };
131
132 template <>
133 struct color_caster<Vec3i,Vec4f>
134 {
135 typedef Vec3i return_type;
136
137 inline static return_type cast(const Vec4f& _src)
138 {
139 return Vec3i( (int)(_src[0]* 255.0f + 0.5f),
140 (int)(_src[1]* 255.0f + 0.5f),
141 (int)(_src[2]* 255.0f + 0.5f) );
142 }
143 };
144
145 template <>
146 struct color_caster<Vec4i,Vec4f>
147 {
148 typedef Vec4i return_type;
149
150 inline static return_type cast(const Vec4f& _src)
151 {
152 return Vec4i( (int)(_src[0]* 255.0f + 0.5f),
153 (int)(_src[1]* 255.0f + 0.5f),
154 (int)(_src[2]* 255.0f + 0.5f),
155 (int)(_src[3]* 255.0f + 0.5f) );
156 }
157 };
158
159 template <>
160 struct color_caster<Vec3ui,Vec3f>
161 {
162 typedef Vec3ui return_type;
163
164 inline static return_type cast(const Vec3f& _src)
165 {
166 return Vec3ui( (unsigned int)(_src[0]* 255.0f + 0.5f),
167 (unsigned int)(_src[1]* 255.0f + 0.5f),
168 (unsigned int)(_src[2]* 255.0f + 0.5f) );
169 }
170 };
171
172 template <>
173 struct color_caster<Vec3ui,Vec4f>
174 {
175 typedef Vec3ui return_type;
176
177 inline static return_type cast(const Vec4f& _src)
178 {
179 return Vec3ui( (unsigned int)(_src[0]* 255.0f + 0.5f),
180 (unsigned int)(_src[1]* 255.0f + 0.5f),
181 (unsigned int)(_src[2]* 255.0f + 0.5f) );
182 }
183 };
184
185 template <>
186 struct color_caster<Vec4ui,Vec4f>
187 {
188 typedef Vec4ui return_type;
189
190 inline static return_type cast(const Vec4f& _src)
191 {
192 return Vec4ui( (unsigned int)(_src[0]* 255.0f + 0.5f),
193 (unsigned int)(_src[1]* 255.0f + 0.5f),
194 (unsigned int)(_src[2]* 255.0f + 0.5f),
195 (unsigned int)(_src[3]* 255.0f + 0.5f) );
196 }
197 };
198
199 template <>
200 struct color_caster<Vec4uc,Vec3f>
201 {
202 typedef Vec4uc return_type;
203
204 inline static return_type cast(const Vec3f& _src)
205 {
206 return Vec4uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
207 (unsigned char)(_src[1]* 255.0f + 0.5f),
208 (unsigned char)(_src[2]* 255.0f + 0.5f),
209 (unsigned char)(255) );
210 }
211 };
212
213 template <>
214 struct color_caster<Vec4f,Vec3f>
215 {
216 typedef Vec4f return_type;
217
218 inline static return_type cast(const Vec3f& _src)
219 {
220 return Vec4f( _src[0],
221 _src[1],
222 _src[2],
223 1.0f );
224 }
225 };
226
227 template <>
228 struct color_caster<Vec4ui,Vec3uc>
229 {
230 typedef Vec4ui return_type;
231
232 inline static return_type cast(const Vec3uc& _src)
233 {
234 return Vec4ui(_src[0],
235 _src[1],
236 _src[2],
237 255 );
238 }
239 };
240
241 template <>
242 struct color_caster<Vec4f,Vec3i>
243 {
244 typedef Vec4f return_type;
245
246 inline static return_type cast(const Vec3i& _src)
247 {
248 const float f = 1.0f / 255.0f;
249 return Vec4f(_src[0]*f, _src[1]*f, _src[2]*f, 1.0f );
250 }
251 };
252
253 template <>
254 struct color_caster<Vec4uc,Vec4f>
255 {
256 typedef Vec4uc return_type;
257
258 inline static return_type cast(const Vec4f& _src)
259 {
260 return Vec4uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
261 (unsigned char)(_src[1]* 255.0f + 0.5f),
262 (unsigned char)(_src[2]* 255.0f + 0.5f),
263 (unsigned char)(_src[3]* 255.0f + 0.5f) );
264 }
265 };
266
267 template <>
268 struct color_caster<Vec4f,Vec4i>
269 {
270 typedef Vec4f return_type;
271
272 inline static return_type cast(const Vec4i& _src)
273 {
274 const float f = 1.0f / 255.0f;
275 return Vec4f( _src[0] * f, _src[1] * f, _src[2] * f , _src[3] * f );
276 }
277 };
278
279 template <>
280 struct color_caster<Vec4uc,Vec3uc>
281 {
282 typedef Vec4uc return_type;
283
284 inline static return_type cast(const Vec3uc& _src)
285 {
286 return Vec4uc( _src[0], _src[1], _src[2], 255 );
287 }
288 };
289
290 template <>
291 struct color_caster<Vec3f, Vec3uc>
292 {
293 typedef Vec3f return_type;
294
295 inline static return_type cast(const Vec3uc& _src)
296 {
297 const float f = 1.0f / 255.0f;
298 return Vec3f(_src[0] * f, _src[1] * f, _src[2] * f );
299 }
300 };
301
302 template <>
303 struct color_caster<Vec3f, Vec4uc>
304 {
305 typedef Vec3f return_type;
306
307 inline static return_type cast(const Vec4uc& _src)
308 {
309 const float f = 1.0f / 255.0f;
310 return Vec3f(_src[0] * f, _src[1] * f, _src[2] * f );
311 }
312 };
313
314 template <>
315 struct color_caster<Vec4f, Vec3uc>
316 {
317 typedef Vec4f return_type;
318
319 inline static return_type cast(const Vec3uc& _src)
320 {
321 const float f = 1.0f / 255.0f;
322 return Vec4f(_src[0] * f, _src[1] * f, _src[2] * f, 1.0f );
323 }
324 };
325
326 template <>
327 struct color_caster<Vec4f, Vec4uc>
328 {
329 typedef Vec4f return_type;
330
331 inline static return_type cast(const Vec4uc& _src)
332 {
333 const float f = 1.0f / 255.0f;
334 return Vec4f(_src[0] * f, _src[1] * f, _src[2] * f, _src[3] * f );
335 }
336 };
337
338 // ----------------------------------------------------------------------------
339
340
341 #ifndef DOXY_IGNORE_THIS
342
343 #if !defined(OM_CC_MSVC)
344 template <typename dst_t>
345 struct color_caster<dst_t,dst_t>
346 {
347 typedef const dst_t& return_type;
348
349 inline static return_type cast(const dst_t& _src)
350 {
351 return _src;
352 }
353 };
354 #endif
355
356 #endif
357
358 //-----------------------------------------------------------------------------
359
360
361 template <typename dst_t, typename src_t>
362 inline
363 typename color_caster<dst_t, src_t>::return_type
364 color_cast(const src_t& _src )
365 {
366 return color_caster<dst_t, src_t>::cast(_src);
367 }
368
369 #endif
370 //-----------------------------------------------------------------------------
371
373
374
375 //=============================================================================
376 } // namespace OpenMesh
377 //=============================================================================
378 #endif // OPENMESH_COLOR_CAST_HH defined
379 //=============================================================================
380
OpenMesh::Vec4i
VectorT< signed int, 4 > Vec4i
4-int signed vector
Definition: Vector11T.hh:826
OpenMesh::Vec3ui
VectorT< unsigned int, 3 > Vec3ui
3-int unsigned vector
Definition: Vector11T.hh:809
OpenMesh::Vec3uc
VectorT< unsigned char, 3 > Vec3uc
3-byte unsigned vector
Definition: Vector11T.hh:801
OpenMesh
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
OpenMesh::Vec4ui
VectorT< unsigned int, 4 > Vec4ui
4-int unsigned vector
Definition: Vector11T.hh:828
OpenMesh::Vec3f
VectorT< float, 3 > Vec3f
3-float vector
Definition: Vector11T.hh:811
OpenMesh::Vec4uc
VectorT< unsigned char, 4 > Vec4uc
4-byte unsigned vector
Definition: Vector11T.hh:820
OpenMesh::vector_cast
void vector_cast(const src_t &_src, dst_t &_dst, GenProg::Int2Type< n >)
Cast vector type to another vector type by copying the vector elements.
Definition: vector_cast.hh:81
OpenMesh::Vec3i
VectorT< signed int, 3 > Vec3i
3-int signed vector
Definition: Vector11T.hh:807
OpenMesh::Vec4f
VectorT< float, 4 > Vec4f
4-float vector
Definition: Vector11T.hh:830
OpenMesh::vector_traits::size_
static const size_t size_
size/dimension of the vector
Definition: vector_traits.hh:97