OpenMesh
Loading...
Searching...
No Matches
color_cast.hh
1/* ========================================================================= *
2 * *
3 * OpenMesh *
4 * Copyright (c) 2001-2025, 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
65namespace OpenMesh {
66
67
68//=============================================================================
69
70
74
75//-----------------------------------------------------------------------------
76#ifndef DOXY_IGNORE_THIS
77
79template <typename dst_t, typename src_t>
80struct 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
93template <>
94struct 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
106template <>
107struct 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
119template <>
120struct 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
132template <>
133struct 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
145template <>
146struct 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
159template <>
160struct 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
172template <>
173struct 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
185template <>
186struct 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
199template <>
200struct 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
213template <>
214struct color_caster<Vec4ui,Vec3f>
215{
216 typedef Vec4ui return_type;
217
218 inline static return_type cast(const Vec3f& _src)
219 {
220 return Vec4ui( (unsigned int)(_src[0]* 255.0f + 0.5f),
221 (unsigned int)(_src[1]* 255.0f + 0.5f),
222 (unsigned int)(_src[2]* 255.0f + 0.5f),
223 (unsigned int)(255) );
224 }
225};
226
227template <>
228struct color_caster<Vec4f,Vec3f>
229{
230 typedef Vec4f return_type;
231
232 inline static return_type cast(const Vec3f& _src)
233 {
234 return Vec4f( _src[0],
235 _src[1],
236 _src[2],
237 1.0f );
238 }
239};
240
241template <>
242struct color_caster<Vec4ui,Vec3uc>
243{
244 typedef Vec4ui return_type;
245
246 inline static return_type cast(const Vec3uc& _src)
247 {
248 return Vec4ui(_src[0],
249 _src[1],
250 _src[2],
251 255 );
252 }
253};
254
255template <>
256struct color_caster<Vec4f,Vec3i>
257{
258 typedef Vec4f return_type;
259
260 inline static return_type cast(const Vec3i& _src)
261 {
262 const float f = 1.0f / 255.0f;
263 return Vec4f(_src[0]*f, _src[1]*f, _src[2]*f, 1.0f );
264 }
265};
266
267template <>
268struct color_caster<Vec4uc,Vec4f>
269{
270 typedef Vec4uc return_type;
271
272 inline static return_type cast(const Vec4f& _src)
273 {
274 return Vec4uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
275 (unsigned char)(_src[1]* 255.0f + 0.5f),
276 (unsigned char)(_src[2]* 255.0f + 0.5f),
277 (unsigned char)(_src[3]* 255.0f + 0.5f) );
278 }
279};
280
281template <>
282struct color_caster<Vec4f,Vec4i>
283{
284 typedef Vec4f return_type;
285
286 inline static return_type cast(const Vec4i& _src)
287 {
288 const float f = 1.0f / 255.0f;
289 return Vec4f( _src[0] * f, _src[1] * f, _src[2] * f , _src[3] * f );
290 }
291};
292
293template <>
294struct color_caster<Vec4uc,Vec3uc>
295{
296 typedef Vec4uc return_type;
297
298 inline static return_type cast(const Vec3uc& _src)
299 {
300 return Vec4uc( _src[0], _src[1], _src[2], 255 );
301 }
302};
303
304template <>
305struct color_caster<Vec3f, Vec3uc>
306{
307 typedef Vec3f return_type;
308
309 inline static return_type cast(const Vec3uc& _src)
310 {
311 const float f = 1.0f / 255.0f;
312 return Vec3f(_src[0] * f, _src[1] * f, _src[2] * f );
313 }
314};
315
316template <>
317struct color_caster<Vec3f, Vec4uc>
318{
319 typedef Vec3f return_type;
320
321 inline static return_type cast(const Vec4uc& _src)
322 {
323 const float f = 1.0f / 255.0f;
324 return Vec3f(_src[0] * f, _src[1] * f, _src[2] * f );
325 }
326};
327
328template <>
329struct color_caster<Vec4f, Vec3uc>
330{
331 typedef Vec4f return_type;
332
333 inline static return_type cast(const Vec3uc& _src)
334 {
335 const float f = 1.0f / 255.0f;
336 return Vec4f(_src[0] * f, _src[1] * f, _src[2] * f, 1.0f );
337 }
338};
339
340template <>
341struct color_caster<Vec4f, Vec4uc>
342{
343 typedef Vec4f return_type;
344
345 inline static return_type cast(const Vec4uc& _src)
346 {
347 const float f = 1.0f / 255.0f;
348 return Vec4f(_src[0] * f, _src[1] * f, _src[2] * f, _src[3] * f );
349 }
350};
351
352// ----------------------------------------------------------------------------
353
354
355#ifndef DOXY_IGNORE_THIS
356
357#if !defined(OM_CC_MSVC)
358template <typename dst_t>
359struct color_caster<dst_t,dst_t>
360{
361 typedef const dst_t& return_type;
362
363 inline static return_type cast(const dst_t& _src)
364 {
365 return _src;
366 }
367};
368#endif
369
370#endif
371
372//-----------------------------------------------------------------------------
373
374
375template <typename dst_t, typename src_t>
376inline
377typename color_caster<dst_t, src_t>::return_type
378color_cast(const src_t& _src )
379{
380 return color_caster<dst_t, src_t>::cast(_src);
381}
382
383#endif
384//-----------------------------------------------------------------------------
385
387
388
389//=============================================================================
390} // namespace OpenMesh
391//=============================================================================
392#endif // OPENMESH_COLOR_CAST_HH defined
393//=============================================================================
394
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition MeshItems.hh:59
VectorT< unsigned int, 4 > Vec4ui
4-int unsigned vector
Definition Vector11T.hh:868
VectorT< unsigned char, 3 > Vec3uc
3-byte unsigned vector
Definition Vector11T.hh:841
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
VectorT< signed int, 4 > Vec4i
4-int signed vector
Definition Vector11T.hh:866
VectorT< float, 4 > Vec4f
4-float vector
Definition Vector11T.hh:870
VectorT< unsigned char, 4 > Vec4uc
4-byte unsigned vector
Definition Vector11T.hh:860
VectorT< unsigned int, 3 > Vec3ui
3-int unsigned vector
Definition Vector11T.hh:849
VectorT< float, 3 > Vec3f
3-float vector
Definition Vector11T.hh:851
VectorT< signed int, 3 > Vec3i
3-int signed vector
Definition Vector11T.hh:847

Project OpenMesh, ©  Visual Computing Institute, RWTH Aachen. Documentation generated using doxygen .