Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
65 #include <OpenMesh/Core/Utils/vector_cast.hh>
66 
67 //== NAMESPACES ===============================================================
68 
69 
70 namespace OpenMesh {
71 
72 
73 //=============================================================================
74 
75 
79 
80 //-----------------------------------------------------------------------------
81 #ifndef DOXY_IGNORE_THIS
82 
84 template <typename dst_t, typename src_t>
85 struct 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 
98 template <>
99 struct 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 
111 template <>
112 struct 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 
124 template <>
125 struct 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 
137 template <>
138 struct 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 
150 template <>
151 struct 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 
164 template <>
165 struct 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 
177 template <>
178 struct 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 
190 template <>
191 struct 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 
204 template <>
205 struct 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 
218 template <>
219 struct 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 
232 template <>
233 struct 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 
246 template <>
247 struct 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 
258 template <>
259 struct 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 
272 template <>
273 struct 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 
284 template <>
285 struct 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 
295 template <>
296 struct 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 
307 template <>
308 struct 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 
319 template <>
320 struct 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 
331 template <>
332 struct 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)
349 template <typename dst_t>
350 struct 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 
366 template <typename dst_t, typename src_t>
367 inline
368 typename color_caster<dst_t, src_t>::return_type
369 color_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 
VectorT< signed int, 3 > Vec3i
Definition: Vector11T.hh:765
VectorT< signed int, 4 > Vec4i
Definition: Vector11T.hh:784
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< unsigned int, 3 > Vec3ui
Definition: Vector11T.hh:767
VectorT< unsigned char, 4 > Vec4uc
Definition: Vector11T.hh:778
VectorT< unsigned char, 3 > Vec3uc
Definition: Vector11T.hh:759
VectorT< unsigned int, 4 > Vec4ui
Definition: Vector11T.hh:786
VectorT< float, 3 > Vec3f
Definition: Vector11T.hh:769
VectorT< float, 4 > Vec4f
Definition: Vector11T.hh:788