OpenMesh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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: 736 $ *
38  * $Date: 2012-10-08 09:30:49 +0200 (Mo, 08 Okt 2012) $ *
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<Vec4uc,Vec3f>
119 {
120  typedef Vec4uc return_type;
121 
122  inline static return_type cast(const Vec3f& _src)
123  {
124  return Vec4uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
125  (unsigned char)(_src[1]* 255.0f + 0.5f),
126  (unsigned char)(_src[2]* 255.0f + 0.5f),
127  (unsigned char)(255) );
128  }
129 };
130 
131 template <>
132 struct color_caster<Vec4f,Vec3f>
133 {
134  typedef Vec4f return_type;
135 
136  inline static return_type cast(const Vec3f& _src)
137  {
138  return Vec4f( _src[0],
139  _src[1],
140  _src[2],
141  1.0f );
142  }
143 };
144 
145 template <>
146 struct color_caster<Vec4uc,Vec4f>
147 {
148  typedef Vec4uc return_type;
149 
150  inline static return_type cast(const Vec4f& _src)
151  {
152  return Vec4uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
153  (unsigned char)(_src[1]* 255.0f + 0.5f),
154  (unsigned char)(_src[2]* 255.0f + 0.5f),
155  (unsigned char)(_src[3]* 255.0f + 0.5f) );
156  }
157 };
158 
159 template <>
160 struct color_caster<Vec4f,Vec4i>
161 {
162  typedef Vec4f return_type;
163 
164  inline static return_type cast(const Vec4i& _src)
165  {
166  const float f = 1.0f / 255.0f;
167  return Vec4f( _src[0] * f, _src[1] * f, _src[2] * f , _src[3] * f );
168  }
169 };
170 
171 template <>
172 struct color_caster<Vec4uc,Vec3uc>
173 {
174  typedef Vec4uc return_type;
175 
176  inline static return_type cast(const Vec3uc& _src)
177  {
178  return Vec4uc( _src[0], _src[1], _src[2], 255 );
179  }
180 };
181 
182 template <>
183 struct color_caster<Vec3f, Vec3uc>
184 {
185  typedef Vec3f return_type;
186 
187  inline static return_type cast(const Vec3uc& _src)
188  {
189  const float f = 1.0f / 255.0f;
190  return Vec3f(_src[0] * f, _src[1] * f, _src[2] * f );
191  }
192 };
193 
194 template <>
195 struct color_caster<Vec3f, Vec4uc>
196 {
197  typedef Vec3f return_type;
198 
199  inline static return_type cast(const Vec4uc& _src)
200  {
201  const float f = 1.0f / 255.0f;
202  return Vec3f(_src[0] * f, _src[1] * f, _src[2] * f );
203  }
204 };
205 
206 template <>
207 struct color_caster<Vec4f, Vec3uc>
208 {
209  typedef Vec4f return_type;
210 
211  inline static return_type cast(const Vec3uc& _src)
212  {
213  const float f = 1.0f / 255.0f;
214  return Vec4f(_src[0] * f, _src[1] * f, _src[2] * f, 1.0f );
215  }
216 };
217 
218 template <>
219 struct color_caster<Vec4f, Vec4uc>
220 {
221  typedef Vec4f return_type;
222 
223  inline static return_type cast(const Vec4uc& _src)
224  {
225  const float f = 1.0f / 255.0f;
226  return Vec4f(_src[0] * f, _src[1] * f, _src[2] * f, _src[3] * f );
227  }
228 };
229 
230 // ----------------------------------------------------------------------------
231 
232 
233 #ifndef DOXY_IGNORE_THIS
234 
235 #if !defined(OM_CC_MSVC)
236 template <typename dst_t>
237 struct color_caster<dst_t,dst_t>
238 {
239  typedef const dst_t& return_type;
240 
241  inline static return_type cast(const dst_t& _src)
242  {
243  return _src;
244  }
245 };
246 #endif
247 
248 #endif
249 
250 //-----------------------------------------------------------------------------
251 
252 
253 template <typename dst_t, typename src_t>
254 inline
255 typename color_caster<dst_t, src_t>::return_type
256 color_cast(const src_t& _src )
257 {
258  return color_caster<dst_t, src_t>::cast(_src);
259 }
260 
261 #endif
262 //-----------------------------------------------------------------------------
263 
265 
266 
267 //=============================================================================
268 } // namespace OpenMesh
269 //=============================================================================
270 #endif // OPENMESH_COLOR_CAST_HH defined
271 //=============================================================================
272 

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