LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiMergeYUVToImage.h

00001 /*
00002  * Copyright (C) 2003, 2004, 2005, 2006, 2007
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  * 
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public 
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.  
00021 j */ 
00022 
00023  
00024 /*--------------------------------------------------------------------
00025  * project ....: LTI-Lib: Image Processing and Computer Vision Library
00026  * file .......: ltiSplitImageToYUV.h
00027  * authors ....: Volker Schmirgel, Pablo Alvarado
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 17.10.2003
00030  * revisions ..: $Id: ltiMergeYUVToImage.h,v 1.8 2007/01/05 19:22:32 alvarado Exp $
00031  */
00032 
00033 #ifndef _LTI_MERGE_Y_U_V_TO_IMAGE_H_
00034 #define _LTI_MERGE_Y_U_V_TO_IMAGE_H_
00035 
00036 #include "ltiMergeImage.h"
00037 #include "ltiMergeYPbPrToImage.h"
00038 
00039 namespace lti {
00040 
00041   /**
00042    * Compute RGB values from given YUV values by merging float or ubyte values
00043    * to an rgbPixel, merging channels(floats) or channel8s(ubytes) to an Image
00044    *
00045    * In the literature, technical and scientific, there is often confusion
00046    * among the color spaces YUV, YPbPr and YPbPr.  Poynton in
00047    * http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html explains that
00048    * YUV is usually never correctly meant, because the color space normally
00049    * used for component digital video is the YCbCr (ITU-RS601 or CCIR-601).
00050    * Other devices use the YPbPr, but the "real" YUV is rarely employed.
00051    *
00052    * The LTI-Lib provides all three spaces:
00053    *
00054    * - YCbCr: lti::mergeYCbCrToImage used by IEEE 1394 FireWire cameras
00055    * - YPbPr: lti::mergeYPbPrToImage used by some WebCams
00056    * - YUV:   lti::mergeYUVToImage   did they really meant to use this?
00057    *
00058    * Here, the inverse transformation of lti::splitImageToYUV is followed
00059    *
00060    * \f[
00061    *   \begin{bmatrix}
00062    *     R \\
00063    *     G \\
00064    *     B
00065    *   \end{bmatrix}
00066    *   =
00067    *   M^{-1}
00068    *   \begin{bmatrix}
00069    *     Y \\
00070    *     U \\
00071    *     V
00072    *   \end{bmatrix}
00073    * \f]
00074    * where M is the matrix given in lti::splitImageToYUV.
00075    *
00076    * If you use \c ubyte values, then this functor is equivalent to
00077    * lti::mergeYPbPrToImage, as the U and V values have to be linearly mapped
00078    * to make use of the limited range from 0 to 255.
00079    *
00080    * @ingroup gColor
00081    */
00082   class mergeYUVToImage : public mergeImage {
00083   public:
00084 
00085     /**
00086      * constructor
00087      */
00088     mergeYUVToImage(void);
00089 
00090     
00091     /**
00092      * destructor
00093      */
00094     ~mergeYUVToImage();
00095 
00096     /**
00097      * return the name of this type
00098      */
00099     virtual const char* getTypeName() const;
00100       
00101     /**
00102      * copy data of "other" functor.
00103      * @param other the functor to be copied
00104      * @return a reference to this functor object
00105      */
00106     mergeYUVToImage& copy(const mergeYUVToImage& other);
00107     
00108     /**
00109      * alias for copy member
00110      * @param other the functor to be copied
00111      * @return a reference to this functor object
00112      */
00113     mergeYUVToImage& operator=(const mergeYUVToImage& other);
00114 
00115     /**
00116      * returns a pointer to a clone of the functor.
00117      */
00118     virtual functor* clone() const;
00119 
00120     /**
00121      * merge channels Y, U, V to an image
00122      * @param Y the Y channel, i.e. black&white
00123      * @param U the U channel, chromatic 
00124      * @param V the V channel, chromatic
00125      * @param img the image to be splitted
00126      */
00127     virtual bool apply(const matrix<float>& Y,
00128                        const matrix<float>& U,
00129                        const matrix<float>& V,
00130                        image& img) const;
00131 
00132     /**
00133      * merge  8-bit-channels Y, U, V to an image
00134      * @param Y the Y channel, i.e. black&white
00135      * @param U the U channel, chromatic 
00136      * @param V the V channel, chromatic
00137      * @param img the image to be splitted
00138      */
00139     virtual bool apply(const channel8& Y,
00140                        const channel8& U,
00141                        const channel8& V,
00142                        image& img) const;
00143 
00144     /**
00145      * merge the  values Y, U and V
00146      * to a pixel
00147      * @param Y the Y value, i.e. black&white
00148      * @param U the U  value, chromatic 
00149      * @param V the V value, chromatic 
00150      * @param pixel the merged pixel
00151      */
00152     inline virtual bool apply(const float& Y,
00153            const float& U,
00154            const float& V,
00155            rgbPixel& pixel) const;
00156 
00157     /**
00158      * merge the   8-bit-values Y, U and V
00159      * to a pixel
00160      * @param Y the Y value, i.e. black&white
00161      * @param U the U  value, chromatic 
00162      * @param V the V value, chromatic 
00163      * @param pixel the merged pixel
00164      */ 
00165     inline virtual bool apply(const ubyte& Y,
00166                               const ubyte& U,
00167                               const ubyte& V,
00168                               rgbPixel& pixel) const;
00169 
00170   protected:
00171 
00172     /**
00173      * Clip function
00174      *
00175      * Equivalent to min(255,max(0,val)) but maybe faster
00176      */
00177     inline ubyte clip(const int val) const;
00178   };
00179 
00180   //
00181   // -------------------------------------
00182   // Implementation of inline methods
00183   // -------------------------------------
00184   //
00185 
00186   inline ubyte mergeYUVToImage::clip(const int val) const {
00187     if (val>255) {
00188       return 255;
00189     }
00190     if (val<0) {
00191       return 0;
00192     }
00193     return static_cast<ubyte>(val);
00194   }
00195 
00196   // create rgbPixel (ubyte)  from YUV float values
00197   inline bool mergeYUVToImage::apply(const float& c1,
00198                                      const float& c2,
00199                                      const float& c3,
00200                                      rgbPixel& pixel) const {
00201 
00202     // The following coefficients are tuned to produce 0% of error of
00203     // RGB -> YUV -> RGB convertions.  Please DO NOT CHANGE!
00204     const float Y = 255.f*c1 + 0.5f;
00205 
00206     pixel.set(clip(static_cast<int>(Y + c3*290.67f)),
00207               clip(static_cast<int>(Y - c2*100.63f 
00208                                       - c3*148.06f)),
00209               clip(static_cast<int>(Y + c2*518.18f)),
00210               0);
00211 
00212     return true;
00213   }
00214 
00215   // merge 8-bit-values to create an rgbPixel
00216   inline bool mergeYUVToImage::apply(const ubyte& c1,
00217                                      const ubyte& c2,
00218                                      const ubyte& c3,
00219                                      rgbPixel& pixel) const {
00220     return mergeYPbPrToImage::convert(c1,c2,c3,pixel);
00221   }
00222 }
00223 #endif

Generated on Sat Apr 10 15:25:51 2010 for LTI-Lib by Doxygen 1.6.1