last update 20 Sep 2009 |
00001 /* 00002 * Copyright (C) 2003 00003 * Department of Electronics, ITCR, Costa Rica 00004 * 00005 * 00006 * This file is part of the Computer Vision and Robotics Library (CVR-Lib) 00007 * 00008 * The CVR-Lib is free software; you can redistribute it and/or 00009 * modify it under the terms of the BSD License. 00010 * 00011 * All rights reserved. 00012 * 00013 * Redistribution and use in source and binary forms, with or without 00014 * modification, are permitted provided that the following conditions are met: 00015 * 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 00019 * 2. Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * 00023 * 3. Neither the name of the authors nor the names of its contributors may be 00024 * used to endorse or promote products derived from this software without 00025 * specific prior written permission. 00026 * 00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00028 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00029 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00030 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00031 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00032 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00033 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00034 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00035 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00036 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00037 * POSSIBILITY OF SUCH DAMAGE. 00038 */ 00039 00040 00041 00042 /** 00043 * \file cvrMergeYPbPrToImage.h 00044 * Merge YPbPr channels into a color image. 00045 * \author Pablo Alvarado 00046 * \date 04.01.2007 00047 * 00048 * $Id: cvrMergeYPbPrToImage.h,v 1.1 2007/04/05 22:56:36 alvarado Exp $ 00049 */ 00050 00051 00052 #ifndef _CVR_MERGE_Y_Pb_Pr_TO_IMAGE_H_ 00053 #define _CVR_MERGE_Y_Pb_Pr_TO_IMAGE_H_ 00054 00055 #include "cvrMergeImage.h" 00056 00057 namespace cvr { 00058 00059 /** 00060 * Creates RGB values from given YPbPr values by merging float or ubyte 00061 * values to an rgbaPixel, merging channels(floats) or channel8s(ubytes) to an 00062 * Image 00063 * 00064 * In the literature, technical and scientific, there is often confusion 00065 * among the color spaces YUV, YPbPr and YPbPr. Poynton in 00066 * http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html explains that 00067 * YUV is usually never correctly meant, because the color space normally 00068 * used for component digital video is the YCbCr (ITU-RS601 or CCIR-601). 00069 * Other devices use the YPbPr, but the "real" YUV is rarely employed. 00070 * 00071 * The CVR-Lib provides all three spaces: 00072 * 00073 * - YCbCr: cvr::mergeYCbCrToImage used by IEEE 1394 FireWire cameras 00074 * - YPbPr: cvr::mergeYPbPrToImage used by some WebCams 00075 * - YUV: cvr::mergeYUVToImage did they really meant to use this? 00076 * 00077 * Here, the inverse transformation of cvr::splitImageToYPbPr is followed: 00078 * 00079 * \f[ 00080 * \begin{bmatrix} 00081 * R \\ 00082 * G \\ 00083 * B 00084 * \end{bmatrix} 00085 * = 00086 * M^{-1} 00087 * \begin{bmatrix} 00088 * Y \\ 00089 * Pb \\ 00090 * Pr 00091 * \end{bmatrix} 00092 * \f] 00093 * where M is the matrix given in cvr::splitImageToYPbPr. 00094 * 00095 * If you know you have a YPbPr space but it was given to you as 00096 * YUV, then the equivalences are U=Pb and V=Pr. 00097 * 00098 * A way of noticing if you have a YPbPr color space is determining the range 00099 * of the values of each channel. Y should be in [0,1], while Pr and Pb 00100 * should be in [-0.5,0.5]. 00101 * 00102 * @ingroup gColor 00103 */ 00104 class mergeYPbPrToImage : public mergeImage { 00105 public: 00106 00107 /** 00108 * Constructor 00109 */ 00110 mergeYPbPrToImage(void); 00111 00112 00113 /** 00114 * Destructor 00115 */ 00116 ~mergeYPbPrToImage(); 00117 00118 /** 00119 * Return the name of this type 00120 */ 00121 const std::string& name() const; 00122 00123 /** 00124 * Copy data of "other" functor. 00125 * 00126 * @param other the functor to be copied 00127 * @return a reference to this functor object 00128 */ 00129 mergeYPbPrToImage& copy(const mergeYPbPrToImage& other); 00130 00131 /** 00132 * Alias for copy method. 00133 * 00134 * @param other the functor to be copied 00135 * @return a reference to this functor object 00136 */ 00137 mergeYPbPrToImage& operator=(const mergeYPbPrToImage& other); 00138 00139 /** 00140 * Returns a pointer to a clone of the functor. 00141 */ 00142 virtual mergeYPbPrToImage* clone() const; 00143 00144 /** 00145 * Returns a pointer to a new instance of this functor. 00146 */ 00147 virtual mergeYPbPrToImage* newInstance() const; 00148 00149 /** 00150 * Merge channels Y, Pb, Pr to an image. 00151 * 00152 * @param Y the Y channel, i.e. black&white 00153 * @param Pb the Pb channel, chromatic 00154 * @param Pr the Pr channel, chromatic 00155 * @param img the image to be splitted 00156 */ 00157 virtual bool apply(const matrix<float>& Y, 00158 const matrix<float>& Pb, 00159 const matrix<float>& Pr, 00160 image& img) const; 00161 00162 /** 00163 * Merge 8-bit-channels Y, Pb, Pr to an image. 00164 * 00165 * @param Y the Y channel, i.e. black&white 00166 * @param Pb the Pb channel, chromatic 00167 * @param Pr the Pr channel, chromatic 00168 * @param img the image to be splitted 00169 */ 00170 virtual bool apply(const matrix<ubyte>& Y, 00171 const matrix<ubyte>& Pb, 00172 const matrix<ubyte>& Pr, 00173 image& img) const; 00174 00175 /** 00176 * Merge the values Y, Pb and Pr to a pixel. 00177 * @param Y the Y value, i.e. black&white 00178 * @param Pb the Pb value, chromatic 00179 * @param Pr the Pr value, chromatic 00180 * @param pixel the merged pixel 00181 */ 00182 inline virtual bool apply(const float& Y, 00183 const float& Pb, 00184 const float& Pr, 00185 rgbaPixel& pixel) const; 00186 00187 /** 00188 * Merge the 8-bit-values Y, Pb and Pr to a pixel. 00189 * 00190 * @param Y the Y value, i.e. black&white 00191 * @param Pb the Pb value, chromatic 00192 * @param Pr the Pr value, chromatic 00193 * @param pixel the merged pixel 00194 */ 00195 inline virtual bool apply(const ubyte& Y, 00196 const ubyte& Pb, 00197 const ubyte& Pr, 00198 rgbaPixel& pixel) const; 00199 00200 /* 00201 * Merge the 8-bit-values Y, Pb and Pr to a pixel. 00202 * 00203 * This is a static method to do the conversion. It is required by 00204 * other functors which need a fast access to the conversion without 00205 * requiring an instance. 00206 * 00207 * \waring Be careful while using this method, since its static nature 00208 * makes you responsible for the previous initialization of the LUTs. For 00209 * that matter, just make sure that any instance of mergeYPbPrToImage is 00210 * created before calling this method. 00211 * 00212 * @param Y the Y value, i.e. black&white 00213 * @param Pb the Pb value, chromatic 00214 * @param Pr the Pr value, chromatic 00215 * @param pixel the merged pixel 00216 */ 00217 static bool convert(const ubyte& Y, 00218 const ubyte& Pb, 00219 const ubyte& Pr, 00220 rgbaPixel& pixel); 00221 00222 protected: 00223 00224 /** 00225 * Initialize the Look-Up-Tables 00226 */ 00227 virtual void initializeLUTs(); 00228 00229 /** 00230 * Look up tables to accelerate conversion YPbPr -> RGB 00231 */ 00232 //@{ 00233 /** 00234 * Partial Y results 00235 */ 00236 static const int* lutY_; 00237 00238 /** 00239 * Partial results with Pr (equivalent to V) for the red channel 00240 * computation. 00241 */ 00242 static const int* lutVr_; 00243 00244 /** 00245 * Partial results with Pb (equivalent to U) for the green channel 00246 * computation. 00247 */ 00248 static const int* lutUg_; 00249 00250 /** 00251 * Partial results with Pr (equivalent to V) for the green channel 00252 * computation. 00253 */ 00254 static const int* lutVg_; 00255 00256 /** 00257 * Partial results with Pb (equivalent to U) for the blue channel 00258 * computation. 00259 */ 00260 static const int* lutUb_; 00261 //@} 00262 00263 /** 00264 * Clip function 00265 * 00266 * Equivalent to min(255,max(0,val)) but maybe faster 00267 */ 00268 inline static ubyte clip(const int val); 00269 00270 }; 00271 } 00272 00273 #include "cvrMergeYPbPrToImage_inline.h" 00274 00275 #endif