OpenMesh
Loading...
Searching...
No Matches
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 * $Revision$ *
45 * $Date$ *
46 * *
47\*===========================================================================*/
48
49
50//=============================================================================
51//
52// Helper Functions for binary reading / writing
53//
54//=============================================================================
55
56
57#ifndef OPENMESH_COLOR_CAST_HH
58#define OPENMESH_COLOR_CAST_HH
59
60
61//== INCLUDES =================================================================
62
63
64#include <OpenMesh/Core/System/config.h>
65#include <OpenMesh/Core/Utils/vector_cast.hh>
66
67//== NAMESPACES ===============================================================
68
69
70namespace OpenMesh {
71
72
73//=============================================================================
74
75
79
80//-----------------------------------------------------------------------------
81#ifndef DOXY_IGNORE_THIS
82
84template <typename dst_t, typename src_t>
85struct color_caster
86{
87 typedef dst_t return_type;
88
89 inline static return_type cast(const src_t& _src)
90 {
91 dst_t dst;
92 vector_cast(_src, dst, GenProg::Int2Type<vector_traits<dst_t>::size_>());
93 return dst;
94 }
95};
96
97
98template <>
99struct color_caster<Vec3uc,Vec3f>
100{
101 typedef Vec3uc return_type;
102
103 inline static return_type cast(const Vec3f& _src)
104 {
105 return Vec3uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
106 (unsigned char)(_src[1]* 255.0f + 0.5f),
107 (unsigned char)(_src[2]* 255.0f + 0.5f) );
108 }
109};
110
111template <>
112struct color_caster<Vec3uc,Vec4f>
113{
114 typedef Vec3uc return_type;
115
116 inline static return_type cast(const Vec4f& _src)
117 {
118 return Vec3uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
119 (unsigned char)(_src[1]* 255.0f + 0.5f),
120 (unsigned char)(_src[2]* 255.0f + 0.5f) );
121 }
122};
123
124template <>
125struct color_caster<Vec3i,Vec3f>
126{
127 typedef Vec3i return_type;
128
129 inline static return_type cast(const Vec3f& _src)
130 {
131 return Vec3i( (int)(_src[0]* 255.0f + 0.5f),
132 (int)(_src[1]* 255.0f + 0.5f),
133 (int)(_src[2]* 255.0f + 0.5f) );
134 }
135};
136
137template <>
138struct color_caster<Vec3i,Vec4f>
139{
140 typedef Vec3i return_type;
141
142 inline static return_type cast(const Vec4f& _src)
143 {
144 return Vec3i( (int)(_src[0]* 255.0f + 0.5f),
145 (int)(_src[1]* 255.0f + 0.5f),
146 (int)(_src[2]* 255.0f + 0.5f) );
147 }
148};
149
150template <>
151struct color_caster<Vec4i,Vec4f>
152{
153 typedef Vec4i return_type;
154
155 inline static return_type cast(const Vec4f& _src)
156 {
157 return Vec4i( (int)(_src[0]* 255.0f + 0.5f),
158 (int)(_src[1]* 255.0f + 0.5f),
159 (int)(_src[2]* 255.0f + 0.5f),
160 (int)(_src[3]* 255.0f + 0.5f) );
161 }
162};
163
164template <>
165struct color_caster<Vec3ui,Vec3f>
166{
167 typedef Vec3ui return_type;
168
169 inline static return_type cast(const Vec3f& _src)
170 {
171 return Vec3ui( (unsigned int)(_src[0]* 255.0f + 0.5f),
172 (unsigned int)(_src[1]* 255.0f + 0.5f),
173 (unsigned int)(_src[2]* 255.0f + 0.5f) );
174 }
175};
176
177template <>
178struct color_caster<Vec3ui,Vec4f>
179{
180 typedef Vec3ui return_type;
181
182 inline static return_type cast(const Vec4f& _src)
183 {
184 return Vec3ui( (unsigned int)(_src[0]* 255.0f + 0.5f),
185 (unsigned int)(_src[1]* 255.0f + 0.5f),
186 (unsigned int)(_src[2]* 255.0f + 0.5f) );
187 }
188};
189
190template <>
191struct color_caster<Vec4ui,Vec4f>
192{
193 typedef Vec4ui return_type;
194
195 inline static return_type cast(const Vec4f& _src)
196 {
197 return Vec4ui( (unsigned int)(_src[0]* 255.0f + 0.5f),
198 (unsigned int)(_src[1]* 255.0f + 0.5f),
199 (unsigned int)(_src[2]* 255.0f + 0.5f),
200 (unsigned int)(_src[3]* 255.0f + 0.5f) );
201 }
202};
203
204template <>
205struct color_caster<Vec4uc,Vec3f>
206{
207 typedef Vec4uc return_type;
208
209 inline static return_type cast(const Vec3f& _src)
210 {
211 return Vec4uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
212 (unsigned char)(_src[1]* 255.0f + 0.5f),
213 (unsigned char)(_src[2]* 255.0f + 0.5f),
214 (unsigned char)(255) );
215 }
216};
217
218template <>
219struct color_caster<Vec4f,Vec3f>
220{
221 typedef Vec4f return_type;
222
223 inline static return_type cast(const Vec3f& _src)
224 {
225 return Vec4f( _src[0],
226 _src[1],
227 _src[2],
228 1.0f );
229 }
230};
231
232template <>
233struct color_caster<Vec4ui,Vec3uc>
234{
235 typedef Vec4ui return_type;
236
237 inline static return_type cast(const Vec3uc& _src)
238 {
239 return Vec4ui(_src[0],
240 _src[1],
241 _src[2],
242 255 );
243 }
244};
245
246template <>
247struct color_caster<Vec4f,Vec3i>
248{
249 typedef Vec4f return_type;
250
251 inline static return_type cast(const Vec3i& _src)
252 {
253 const float f = 1.0f / 255.0f;
254 return Vec4f(_src[0]*f, _src[1]*f, _src[2]*f, 1.0f );
255 }
256};
257
258template <>
259struct color_caster<Vec4uc,Vec4f>
260{
261 typedef Vec4uc return_type;
262
263 inline static return_type cast(const Vec4f& _src)
264 {
265 return Vec4uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
266 (unsigned char)(_src[1]* 255.0f + 0.5f),
267 (unsigned char)(_src[2]* 255.0f + 0.5f),
268 (unsigned char)(_src[3]* 255.0f + 0.5f) );
269 }
270};
271
272template <>
273struct color_caster<Vec4f,Vec4i>
274{
275 typedef Vec4f return_type;
276
277 inline static return_type cast(const Vec4i& _src)
278 {
279 const float f = 1.0f / 255.0f;
280 return Vec4f( _src[0] * f, _src[1] * f, _src[2] * f , _src[3] * f );
281 }
282};
283
284template <>
285struct color_caster<Vec4uc,Vec3uc>
286{
287 typedef Vec4uc return_type;
288
289 inline static return_type cast(const Vec3uc& _src)
290 {
291 return Vec4uc( _src[0], _src[1], _src[2], 255 );
292 }
293};
294
295template <>
296struct color_caster<Vec3f, Vec3uc>
297{
298 typedef Vec3f return_type;
299
300 inline static return_type cast(const Vec3uc& _src)
301 {
302 const float f = 1.0f / 255.0f;
303 return Vec3f(_src[0] * f, _src[1] * f, _src[2] * f );
304 }
305};
306
307template <>
308struct color_caster<Vec3f, Vec4uc>
309{
310 typedef Vec3f return_type;
311
312 inline static return_type cast(const Vec4uc& _src)
313 {
314 const float f = 1.0f / 255.0f;
315 return Vec3f(_src[0] * f, _src[1] * f, _src[2] * f );
316 }
317};
318
319template <>
320struct color_caster<Vec4f, Vec3uc>
321{
322 typedef Vec4f return_type;
323
324 inline static return_type cast(const Vec3uc& _src)
325 {
326 const float f = 1.0f / 255.0f;
327 return Vec4f(_src[0] * f, _src[1] * f, _src[2] * f, 1.0f );
328 }
329};
330
331template <>
332struct color_caster<Vec4f, Vec4uc>
333{
334 typedef Vec4f return_type;
335
336 inline static return_type cast(const Vec4uc& _src)
337 {
338 const float f = 1.0f / 255.0f;
339 return Vec4f(_src[0] * f, _src[1] * f, _src[2] * f, _src[3] * f );
340 }
341};
342
343// ----------------------------------------------------------------------------
344
345
346#ifndef DOXY_IGNORE_THIS
347
348#if !defined(OM_CC_MSVC)
349template <typename dst_t>
350struct color_caster<dst_t,dst_t>
351{
352 typedef const dst_t& return_type;
353
354 inline static return_type cast(const dst_t& _src)
355 {
356 return _src;
357 }
358};
359#endif
360
361#endif
362
363//-----------------------------------------------------------------------------
364
365
366template <typename dst_t, typename src_t>
367inline
368typename color_caster<dst_t, src_t>::return_type
369color_cast(const src_t& _src )
370{
371 return color_caster<dst_t, src_t>::cast(_src);
372}
373
374#endif
375//-----------------------------------------------------------------------------
376
378
379
380//=============================================================================
381} // namespace OpenMesh
382//=============================================================================
383#endif // OPENMESH_COLOR_CAST_HH defined
384//=============================================================================
385
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
VectorT< unsigned int, 4 > Vec4ui
4-int unsigned vector
Definition: Vector11T.hh:786
VectorT< unsigned char, 3 > Vec3uc
3-byte unsigned vector
Definition: Vector11T.hh:759
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:86
VectorT< signed int, 4 > Vec4i
4-int signed vector
Definition: Vector11T.hh:784
VectorT< float, 4 > Vec4f
4-float vector
Definition: Vector11T.hh:788
VectorT< unsigned char, 4 > Vec4uc
4-byte unsigned vector
Definition: Vector11T.hh:778
VectorT< unsigned int, 3 > Vec3ui
3-int unsigned vector
Definition: Vector11T.hh:767
VectorT< float, 3 > Vec3f
3-float vector
Definition: Vector11T.hh:769
VectorT< signed int, 3 > Vec3i
3-int signed vector
Definition: Vector11T.hh:765

Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .