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: 444 $                                                         *
00038  *   $Date: 2011-10-25 11:59:20 +0200 (Di, 25 Okt 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<Vec4f,Vec3f>
00133 {
00134   typedef Vec4f return_type;
00135 
00136   inline static return_type cast(const Vec3f& _src)
00137   {
00138     return Vec4f( _src[0],
00139                   _src[1],
00140                   _src[2],
00141                   1.0f );
00142   }
00143 };
00144 
00145 template <>
00146 struct color_caster<Vec4uc,Vec4f>
00147 {
00148   typedef Vec4uc return_type;
00149 
00150   inline static return_type cast(const Vec4f& _src)
00151   {
00152     return Vec4uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
00153                    (unsigned char)(_src[1]* 255.0f + 0.5f),
00154                    (unsigned char)(_src[2]* 255.0f + 0.5f),
00155                    (unsigned char)(_src[3]* 255.0f + 0.5f) );
00156   }
00157 };
00158 
00159 template <>
00160 struct color_caster<Vec4f,Vec4i>
00161 {
00162   typedef Vec4f return_type;
00163 
00164   inline static return_type cast(const Vec4i& _src)
00165   {
00166     const float f = 1.0f / 255.0f;
00167     return Vec4f( _src[0] * f, _src[1] *  f, _src[2] * f , _src[3] * f  );
00168   }
00169 };
00170 
00171 template <>
00172 struct color_caster<Vec4uc,Vec3uc>
00173 {
00174   typedef Vec4uc return_type;
00175 
00176   inline static return_type cast(const Vec3uc& _src)
00177   {
00178     return Vec4uc( _src[0], _src[1], _src[2], 255 );
00179   }
00180 };
00181 
00182 template <>
00183 struct color_caster<Vec3f, Vec3uc>
00184 {
00185   typedef Vec3f return_type;
00186 
00187   inline static return_type cast(const Vec3uc& _src)
00188   {
00189     const float f = 1.0f / 255.0f;
00190     return Vec3f(_src[0] * f, _src[1] *  f, _src[2] * f );
00191   }
00192 };
00193 
00194 template <>
00195 struct color_caster<Vec3f, Vec4uc>
00196 {
00197   typedef Vec3f return_type;
00198 
00199   inline static return_type cast(const Vec4uc& _src)
00200   {
00201     const float f = 1.0f / 255.0f;
00202     return Vec3f(_src[0] * f, _src[1] *  f, _src[2] * f );
00203   }
00204 };
00205 
00206 template <>
00207 struct color_caster<Vec4f, Vec3uc>
00208 {
00209   typedef Vec4f return_type;
00210 
00211   inline static return_type cast(const Vec3uc& _src)
00212   {
00213     const float f = 1.0f / 255.0f;
00214     return Vec4f(_src[0] * f, _src[1] *  f, _src[2] * f, 1.0f );
00215   }
00216 };
00217 
00218 template <>
00219 struct color_caster<Vec4f, Vec4uc>
00220 {
00221   typedef Vec4f return_type;
00222 
00223   inline static return_type cast(const Vec4uc& _src)
00224   {
00225     const float f = 1.0f / 255.0f;
00226     return Vec4f(_src[0] * f, _src[1] *  f, _src[2] * f, _src[3] * f );
00227   }
00228 };
00229 
00230 // ----------------------------------------------------------------------------
00231 
00232 
00233 #ifndef DOXY_IGNORE_THIS
00234 
00235 #if !defined(OM_CC_MSVC)
00236 template <typename dst_t>
00237 struct color_caster<dst_t,dst_t>
00238 {
00239   typedef const dst_t&  return_type;
00240 
00241   inline static return_type cast(const dst_t& _src)
00242   {
00243     return _src;
00244   }
00245 };
00246 #endif
00247 
00248 #endif
00249 
00250 //-----------------------------------------------------------------------------
00251 
00252 
00253 template <typename dst_t, typename src_t>
00254 inline
00255 typename color_caster<dst_t, src_t>::return_type
00256 color_cast(const src_t& _src )
00257 {
00258   return color_caster<dst_t, src_t>::cast(_src);
00259 }
00260 
00261 #endif
00262 //-----------------------------------------------------------------------------
00263 
00265 
00266 
00267 //=============================================================================
00268 } // namespace OpenMesh
00269 //=============================================================================
00270 #endif // OPENMESH_COLOR_CAST_HH defined
00271 //=============================================================================
00272