rgbyuv.hh

Go to the documentation of this file.
00001 // Copyright (C) 2001, 2002, 2003, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 
00029 #ifndef OLENA_CONVERT_NRGBYUV_HH
00030 # define OLENA_CONVERT_NRGBYUV_HH
00031 
00032 # include <oln/convert/abstract/colorconv.hh>
00033 
00034 # include <ntg/color/rgb.hh>
00035 # include <ntg/color/yuv.hh>
00036 # include <ntg/basics.hh>
00037 
00038 # include <sstream>
00039 
00045 namespace oln {
00046 
00047   using namespace ntg;
00048 
00049   namespace convert {
00050     /* Functor for conversion from RGB to YUV.
00051     **
00052     ** \see f_rgb_to_hsl
00053     */
00054     template <unsigned inbits, unsigned outbits>
00055     struct f_rgb_to_yuv
00056       : public abstract::color_conversion<3, inbits, rgb_traits,
00057                                           3, outbits, yuv_traits, f_rgb_to_yuv<inbits, outbits> >
00058     {
00059       color<3, outbits, yuv_traits>
00060       doit(const color<3, inbits, rgb_traits>& v) const
00061       {
00062         vec<3, float> in = v.to_float();
00063         vec<3, float> out;
00064         out[yuv_Y] =
00065           + 0.177 * in[rgb_R] + 0.813  * in[rgb_G] + 0.01 * in[rgb_B];
00066         out[yuv_U] =
00067           - 0.083 * in[rgb_R] - 0.434 * in[rgb_G] + 0.437 * in[rgb_B];
00068         out[yuv_V] =
00069           + 0.583 * in[rgb_R] - 0.576  * in[rgb_G] + 0.071 * in[rgb_B];
00070         return out;
00071       }
00072 
00073       static std::string
00074       name()
00075       {
00076         std::ostringstream s;
00077         s << "f_rgb_to_yuv<" << inbits << ", " << outbits << '>';
00078         s.str();
00079       }
00080     };
00081     /* Conversion from RGB to YUV.
00082     **
00083     ** \see f_rgb_to_hsl
00084     */
00085     template <unsigned inbits, unsigned outbits>
00086     color<3, outbits, yuv_traits>
00087     rgb_to_yuv(const color<3, inbits, rgb_traits>& v)
00088     {
00089       f_rgb_to_yuv<inbits, outbits> f;
00090 
00091       return f(v);
00092     }
00093     /* Functor for conversion from YUV to RGB.
00094     **
00095     ** \see f_rgb_to_hsl
00096     */
00097     template<unsigned inbits, unsigned outbits>
00098     struct f_yuv_to_rgb
00099       : public abstract::color_conversion<3, inbits, yuv_traits,
00100                                           3, outbits, rgb_traits, f_yuv_to_rgb<inbits, outbits> >
00101     {
00102       color<3, outbits, rgb_traits>
00103       doit(const color<3, inbits, yuv_traits>& v) const
00104       {
00105         vec<3, float> in = v.to_float();
00106         vec<3, float> out;
00107         out[rgb_R] = 0.8702 * in[yuv_Y] - 0.2487 * in[yuv_U] + 1.4250 * in[yuv_V];
00108         out[rgb_G] = 1.0259 * in[yuv_Y] + 0.0259 * in[yuv_U] - 0.3072 * in[yuv_V];
00109         out[rgb_B] = 1.1837 * in[yuv_Y] + 2.2642 * in[yuv_U] - 0.0359 * in[yuv_V];
00110 
00111         return out;
00112       }
00113 
00114       static std::string
00115       name()
00116       {
00117         std::ostringstream s;
00118         s << "f_yuv_to_rgb<" << inbits << ", " << outbits << '>';
00119         s.str();
00120       }
00121     };
00122 
00123     /* Conversion from YUV to RGB.
00124     **
00125     ** \see f_rgb_to_hsl
00126     */
00127     template <unsigned inbits, unsigned outbits>
00128     color<3, outbits, rgb_traits>
00129     yuv_to_rgb(const color<3, inbits, yuv_traits>& v)
00130     {
00131       f_yuv_to_rgb<inbits, outbits> f;
00132 
00133       return f(v);
00134     }
00135 
00136   } // convert
00137 } // oln
00138 
00139 #endif // OLENA_CONVERT_RGBYUV_HH

Generated on Thu Apr 15 20:13:14 2004 for Olena by doxygen 1.3.6-20040222