OpenMesh
OpenMesh/Core/Utils/color_cast.hh
00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                               OpenMesh                                    *
00004  *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openmesh.org                                *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------* 
00008  *  This file is part of OpenMesh.                                           *
00009  *                                                                           *
00010  *  OpenMesh is free software: you can redistribute it and/or modify         * 
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenMesh is distributed in the hope that it will be useful,              *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenMesh.  If not,                                    *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/ 
00034 
00035 /*===========================================================================*\
00036  *                                                                           *             
00037  *   $Revision: 362 $                                                         *
00038  *   $Date: 2011-01-26 10:21:12 +0100 (Mi, 26 Jan 2011) $                   *
00039  *                                                                           *
00040 \*===========================================================================*/
00041 
00042 
00043 //=============================================================================
00044 //
00045 //  Helper Functions for binary reading / writing
00046 //
00047 //=============================================================================
00048 
00049 
00050 #ifndef OPENMESH_COLOR_CAST_HH
00051 #define OPENMESH_COLOR_CAST_HH
00052 
00053 
00054 //== INCLUDES =================================================================
00055 
00056 
00057 #include <OpenMesh/Core/System/config.h>
00058 #include <OpenMesh/Core/Utils/vector_cast.hh>
00059 
00060 //== NAMESPACES ===============================================================
00061 
00062 
00063 namespace OpenMesh {
00064 
00065 
00066 //=============================================================================
00067 
00068 
00072 
00073 //-----------------------------------------------------------------------------
00074 #ifndef DOXY_IGNORE_THIS
00075 
00077 template <typename dst_t, typename src_t>
00078 struct color_caster
00079 {
00080   typedef dst_t  return_type;
00081 
00082   inline static return_type cast(const src_t& _src)
00083   {
00084     dst_t dst;
00085     vector_copy(_src, dst, GenProg::Int2Type<vector_traits<dst_t>::size_>());
00086     return dst;
00087   }
00088 };
00089 
00090 
00091 template <>
00092 struct color_caster<Vec3uc,Vec3f>
00093 {
00094   typedef Vec3uc return_type;
00095 
00096   inline static return_type cast(const Vec3f& _src)
00097   {
00098     return Vec3uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
00099                                (unsigned char)(_src[1]* 255.0f + 0.5f),
00100                                (unsigned char)(_src[2]* 255.0f + 0.5f) );
00101   }
00102 };
00103 
00104 template <>
00105 struct color_caster<Vec3uc,Vec4f>
00106 {
00107   typedef Vec3uc return_type;
00108 
00109   inline static return_type cast(const Vec4f& _src)
00110   {
00111     return Vec3uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
00112                    (unsigned char)(_src[1]* 255.0f + 0.5f),
00113                    (unsigned char)(_src[2]* 255.0f + 0.5f) );
00114   }
00115 };
00116 
00117 template <>
00118 struct color_caster<Vec4uc,Vec3f>
00119 {
00120   typedef Vec4uc return_type;
00121 
00122   inline static return_type cast(const Vec3f& _src)
00123   {
00124     return Vec4uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
00125                    (unsigned char)(_src[1]* 255.0f + 0.5f),
00126                    (unsigned char)(_src[2]* 255.0f + 0.5f),
00127                    (unsigned char)(255) );
00128   }
00129 };
00130 
00131 template <>
00132 struct color_caster<Vec4uc,Vec4f>
00133 {
00134   typedef Vec4uc return_type;
00135 
00136   inline static return_type cast(const Vec4f& _src)
00137   {
00138     return Vec4uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
00139                    (unsigned char)(_src[1]* 255.0f + 0.5f),
00140                    (unsigned char)(_src[2]* 255.0f + 0.5f),
00141                    (unsigned char)(_src[3]* 255.0f + 0.5f) );
00142   }
00143 };
00144 
00145 template <>
00146 struct color_caster<Vec4uc,Vec3uc>
00147 {
00148   typedef Vec4uc return_type;
00149 
00150   inline static return_type cast(const Vec3uc& _src)
00151   {
00152     return Vec4uc( _src[0], _src[1], _src[2], 255 );
00153   }
00154 };
00155 
00156 template <>
00157 struct color_caster<Vec3f, Vec3uc>
00158 {
00159   typedef Vec3f return_type;
00160 
00161   inline static return_type cast(const Vec3uc& _src)
00162   {
00163     const float f = 1.0f / 255.0f;
00164     return Vec3f(_src[0] * f, _src[1] *  f, _src[2] * f );
00165   }
00166 };
00167 
00168 template <>
00169 struct color_caster<Vec3f, Vec4uc>
00170 {
00171   typedef Vec3f return_type;
00172 
00173   inline static return_type cast(const Vec4uc& _src)
00174   {
00175     const float f = 1.0f / 255.0f;
00176     return Vec3f(_src[0] * f, _src[1] *  f, _src[2] * f );
00177   }
00178 };
00179 
00180 template <>
00181 struct color_caster<Vec4f, Vec3uc>
00182 {
00183   typedef Vec4f return_type;
00184 
00185   inline static return_type cast(const Vec3uc& _src)
00186   {
00187     const float f = 1.0f / 255.0f;
00188     return Vec4f(_src[0] * f, _src[1] *  f, _src[2] * f, 1.0f );
00189   }
00190 };
00191 
00192 template <>
00193 struct color_caster<Vec4f, Vec4uc>
00194 {
00195   typedef Vec4f return_type;
00196 
00197   inline static return_type cast(const Vec4uc& _src)
00198   {
00199     const float f = 1.0f / 255.0f;
00200     return Vec4f(_src[0] * f, _src[1] *  f, _src[2] * f, _src[3] * f );
00201   }
00202 };
00203 
00204 // ----------------------------------------------------------------------------
00205 
00206 
00207 #ifndef DOXY_IGNORE_THIS
00208 
00209 #if !defined(OM_CC_MSVC)
00210 template <typename dst_t>
00211 struct color_caster<dst_t,dst_t>
00212 {
00213   typedef const dst_t&  return_type;
00214 
00215   inline static return_type cast(const dst_t& _src)
00216   {
00217     return _src;
00218   }
00219 };
00220 #endif
00221 
00222 #endif
00223 
00224 //-----------------------------------------------------------------------------
00225 
00226 
00227 template <typename dst_t, typename src_t>
00228 inline
00229 typename color_caster<dst_t, src_t>::return_type
00230 color_cast(const src_t& _src )
00231 {
00232   return color_caster<dst_t, src_t>::cast(_src);
00233 }
00234 
00235 #endif
00236 //-----------------------------------------------------------------------------
00237 
00239 
00240 
00241 //=============================================================================
00242 } // namespace OpenMesh
00243 //=============================================================================
00244 #endif // OPENMESH_COLOR_CAST_HH defined
00245 //=============================================================================
00246