OpenMesh

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
color_cast.hh
1 /*===========================================================================*\
2 * *
3 * OpenMesh *
4 * Copyright (C) 2001-2012 by Computer Graphics Group, RWTH Aachen *
5 * www.openmesh.org *
6 * *
7 *---------------------------------------------------------------------------*
8 * This file is part of OpenMesh. *
9 * *
10 * OpenMesh is free software: you can redistribute it and/or modify *
11 * it under the terms of the GNU Lesser General Public License as *
12 * published by the Free Software Foundation, either version 3 of *
13 * the License, or (at your option) any later version with the *
14 * following exceptions: *
15 * *
16 * If other files instantiate templates or use macros *
17 * or inline functions from this file, or you compile this file and *
18 * link it with other files to produce an executable, this file does *
19 * not by itself cause the resulting executable to be covered by the *
20 * GNU Lesser General Public License. This exception does not however *
21 * invalidate any other reasons why the executable file might be *
22 * covered by the GNU Lesser General Public License. *
23 * *
24 * OpenMesh is distributed in the hope that it will be useful, *
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27 * GNU Lesser General Public License for more details. *
28 * *
29 * You should have received a copy of the GNU LesserGeneral Public *
30 * License along with OpenMesh. If not, *
31 * see <http://www.gnu.org/licenses/>. *
32 * *
33 \*===========================================================================*/
34
35 /*===========================================================================*\
36 * *
37 * $Revision: 815 $ *
38 * $Date: 2013-02-27 14:25:42 +0100 (Mi, 27. Feb 2013) $ *
39 * *
40 \*===========================================================================*/
41
42
43 //=============================================================================
44 //
45 // Helper Functions for binary reading / writing
46 //
47 //=============================================================================
48
49
50 #ifndef OPENMESH_COLOR_CAST_HH
51 #define OPENMESH_COLOR_CAST_HH
52
53
54 //== INCLUDES =================================================================
55
56
57 #include <OpenMesh/Core/System/config.h>
58 #include <OpenMesh/Core/Utils/vector_cast.hh>
59
60 //== NAMESPACES ===============================================================
61
62
63 namespace OpenMesh {
64
65
66 //=============================================================================
67
68
72
73 //-----------------------------------------------------------------------------
74 #ifndef DOXY_IGNORE_THIS
75
77 template <typename dst_t, typename src_t>
78 struct color_caster
79 {
80 typedef dst_t return_type;
81
82 inline static return_type cast(const src_t& _src)
83 {
84 dst_t dst;
85 vector_copy(_src, dst, GenProg::Int2Type<vector_traits<dst_t>::size_>());
86 return dst;
87 }
88 };
89
90
91 template <>
92 struct color_caster<Vec3uc,Vec3f>
93 {
94 typedef Vec3uc return_type;
95
96 inline static return_type cast(const Vec3f& _src)
97 {
98 return Vec3uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
99 (unsigned char)(_src[1]* 255.0f + 0.5f),
100 (unsigned char)(_src[2]* 255.0f + 0.5f) );
101 }
102 };
103
104 template <>
105 struct color_caster<Vec3uc,Vec4f>
106 {
107 typedef Vec3uc return_type;
108
109 inline static return_type cast(const Vec4f& _src)
110 {
111 return Vec3uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
112 (unsigned char)(_src[1]* 255.0f + 0.5f),
113 (unsigned char)(_src[2]* 255.0f + 0.5f) );
114 }
115 };
116
117 template <>
118 struct color_caster<Vec3i,Vec3f>
119 {
120 typedef Vec3i return_type;
121
122 inline static return_type cast(const Vec3f& _src)
123 {
124 return Vec3i( (int)(_src[0]* 255.0f + 0.5f),
125 (int)(_src[1]* 255.0f + 0.5f),
126 (int)(_src[2]* 255.0f + 0.5f) );
127 }
128 };
129
130 template <>
131 struct color_caster<Vec3i,Vec4f>
132 {
133 typedef Vec3i return_type;
134
135 inline static return_type cast(const Vec4f& _src)
136 {
137 return Vec3i( (int)(_src[0]* 255.0f + 0.5f),
138 (int)(_src[1]* 255.0f + 0.5f),
139 (int)(_src[2]* 255.0f + 0.5f) );
140 }
141 };
142
143 template <>
144 struct color_caster<Vec4i,Vec4f>
145 {
146 typedef Vec4i return_type;
147
148 inline static return_type cast(const Vec4f& _src)
149 {
150 return Vec4i( (int)(_src[0]* 255.0f + 0.5f),
151 (int)(_src[1]* 255.0f + 0.5f),
152 (int)(_src[2]* 255.0f + 0.5f),
153 (int)(_src[3]* 255.0f + 0.5f) );
154 }
155 };
156
157 template <>
158 struct color_caster<Vec3ui,Vec3f>
159 {
160 typedef Vec3ui return_type;
161
162 inline static return_type cast(const Vec3f& _src)
163 {
164 return Vec3ui( (unsigned int)(_src[0]* 255.0f + 0.5f),
165 (unsigned int)(_src[1]* 255.0f + 0.5f),
166 (unsigned int)(_src[2]* 255.0f + 0.5f) );
167 }
168 };
169
170 template <>
171 struct color_caster<Vec3ui,Vec4f>
172 {
173 typedef Vec3ui return_type;
174
175 inline static return_type cast(const Vec4f& _src)
176 {
177 return Vec3ui( (unsigned int)(_src[0]* 255.0f + 0.5f),
178 (unsigned int)(_src[1]* 255.0f + 0.5f),
179 (unsigned int)(_src[2]* 255.0f + 0.5f) );
180 }
181 };
182
183 template <>
184 struct color_caster<Vec4ui,Vec4f>
185 {
186 typedef Vec4ui return_type;
187
188 inline static return_type cast(const Vec4f& _src)
189 {
190 return Vec4ui( (unsigned int)(_src[0]* 255.0f + 0.5f),
191 (unsigned int)(_src[1]* 255.0f + 0.5f),
192 (unsigned int)(_src[2]* 255.0f + 0.5f),
193 (unsigned int)(_src[3]* 255.0f + 0.5f) );
194 }
195 };
196
197 template <>
198 struct color_caster<Vec4uc,Vec3f>
199 {
200 typedef Vec4uc return_type;
201
202 inline static return_type cast(const Vec3f& _src)
203 {
204 return Vec4uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
205 (unsigned char)(_src[1]* 255.0f + 0.5f),
206 (unsigned char)(_src[2]* 255.0f + 0.5f),
207 (unsigned char)(255) );
208 }
209 };
210
211 template <>
212 struct color_caster<Vec4f,Vec3f>
213 {
214 typedef Vec4f return_type;
215
216 inline static return_type cast(const Vec3f& _src)
217 {
218 return Vec4f( _src[0],
219 _src[1],
220 _src[2],
221 1.0f );
222 }
223 };
224
225 template <>
226 struct color_caster<Vec4uc,Vec4f>
227 {
228 typedef Vec4uc return_type;
229
230 inline static return_type cast(const Vec4f& _src)
231 {
232 return Vec4uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
233 (unsigned char)(_src[1]* 255.0f + 0.5f),
234 (unsigned char)(_src[2]* 255.0f + 0.5f),
235 (unsigned char)(_src[3]* 255.0f + 0.5f) );
236 }
237 };
238
239 template <>
240 struct color_caster<Vec4f,Vec4i>
241 {
242 typedef Vec4f return_type;
243
244 inline static return_type cast(const Vec4i& _src)
245 {
246 const float f = 1.0f / 255.0f;
247 return Vec4f( _src[0] * f, _src[1] * f, _src[2] * f , _src[3] * f );
248 }
249 };
250
251 template <>
252 struct color_caster<Vec4uc,Vec3uc>
253 {
254 typedef Vec4uc return_type;
255
256 inline static return_type cast(const Vec3uc& _src)
257 {
258 return Vec4uc( _src[0], _src[1], _src[2], 255 );
259 }
260 };
261
262 template <>
263 struct color_caster<Vec3f, Vec3uc>
264 {
265 typedef Vec3f return_type;
266
267 inline static return_type cast(const Vec3uc& _src)
268 {
269 const float f = 1.0f / 255.0f;
270 return Vec3f(_src[0] * f, _src[1] * f, _src[2] * f );
271 }
272 };
273
274 template <>
275 struct color_caster<Vec3f, Vec4uc>
276 {
277 typedef Vec3f return_type;
278
279 inline static return_type cast(const Vec4uc& _src)
280 {
281 const float f = 1.0f / 255.0f;
282 return Vec3f(_src[0] * f, _src[1] * f, _src[2] * f );
283 }
284 };
285
286 template <>
287 struct color_caster<Vec4f, Vec3uc>
288 {
289 typedef Vec4f return_type;
290
291 inline static return_type cast(const Vec3uc& _src)
292 {
293 const float f = 1.0f / 255.0f;
294 return Vec4f(_src[0] * f, _src[1] * f, _src[2] * f, 1.0f );
295 }
296 };
297
298 template <>
299 struct color_caster<Vec4f, Vec4uc>
300 {
301 typedef Vec4f return_type;
302
303 inline static return_type cast(const Vec4uc& _src)
304 {
305 const float f = 1.0f / 255.0f;
306 return Vec4f(_src[0] * f, _src[1] * f, _src[2] * f, _src[3] * f );
307 }
308 };
309
310 // ----------------------------------------------------------------------------
311
312
313 #ifndef DOXY_IGNORE_THIS
314
315 #if !defined(OM_CC_MSVC)
316 template <typename dst_t>
317 struct color_caster<dst_t,dst_t>
318 {
319 typedef const dst_t& return_type;
320
321 inline static return_type cast(const dst_t& _src)
322 {
323 return _src;
324 }
325 };
326 #endif
327
328 #endif
329
330 //-----------------------------------------------------------------------------
331
332
333 template <typename dst_t, typename src_t>
334 inline
335 typename color_caster<dst_t, src_t>::return_type
336 color_cast(const src_t& _src )
337 {
338 return color_caster<dst_t, src_t>::cast(_src);
339 }
340
341 #endif
342 //-----------------------------------------------------------------------------
343
345
346
347 //=============================================================================
348 } // namespace OpenMesh
349 //=============================================================================
350 #endif // OPENMESH_COLOR_CAST_HH defined
351 //=============================================================================
352