openni_image_bayer_grbg.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2011 2011 Willow Garage, Inc.
00005  *    Suat Gedikli <gedikli@willowgarage.com>
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of Willow Garage, Inc. nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  */
00037 #include <openni_camera/openni_image_bayer_grbg.h>
00038 #include <sstream>
00039 #include <iostream>
00040 
00041 #define AVG(a,b) (((int)(a) + (int)(b)) >> 1)
00042 #define AVG3(a,b,c) (((int)(a) + (int)(b) + (int)(c)) / 3)
00043 #define AVG4(a,b,c,d) (((int)(a) + (int)(b) + (int)(c) + (int)(d)) >> 2)
00044 #define WAVG4(a,b,c,d,x,y)  ( ( ((int)(a) + (int)(b)) * (int)(x) + ((int)(c) + (int)(d)) * (int)(y) ) / ( 2 * ((int)(x) + (int(y))) ) )
00045 using namespace std;
00046 
00047 namespace openni_wrapper
00048 {
00049 
00050 ImageBayerGRBG::ImageBayerGRBG (boost::shared_ptr<xn::ImageMetaData> image_meta_data, DebayeringMethod method) throw ()
00051 : Image (image_meta_data)
00052 , debayering_method_ (method)
00053 {
00054 }
00055 
00056 ImageBayerGRBG::~ImageBayerGRBG () throw ()
00057 {
00058 }
00059 
00060 bool ImageBayerGRBG::isResizingSupported (unsigned input_width, unsigned input_height, unsigned output_width, unsigned output_height) const
00061 {
00062   return ImageBayerGRBG::resizingSupported (input_width, input_height, output_width, output_height);
00063 }
00064 
00065 void ImageBayerGRBG::fillGrayscale (unsigned width, unsigned height, unsigned char* gray_buffer, unsigned gray_line_step) const throw (OpenNIException)
00066 {
00067   if (width > image_md_->XRes () || height > image_md_->YRes ())
00068     THROW_OPENNI_EXCEPTION ("Upsampling not supported. Request was: %d x %d -> %d x %d", image_md_->XRes (), image_md_->YRes (), width, height);
00069 
00070   if (gray_line_step == 0)
00071     gray_line_step = width;
00072 
00073   // padding skip for destination image
00074   unsigned gray_line_skip = gray_line_step - width;
00075   if (image_md_->XRes () == width && image_md_->YRes () == height)
00076   { // if no downsampling
00077     const XnUInt8 *bayer_pixel = image_md_->WritableData ();
00078     int line_skip = image_md_->XRes ();
00079     if (debayering_method_ == Bilinear)
00080     {
00081       // first line GRGRGR
00082       for (register unsigned xIdx = 0; xIdx < width - 2; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
00083       {
00084         gray_buffer[0] = bayer_pixel[0]; // green pixel
00085         gray_buffer[1] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[1 + line_skip]); // interpolated green pixel
00086       }
00087       gray_buffer[0] = bayer_pixel[0];
00088       gray_buffer[1] = AVG (bayer_pixel[0], bayer_pixel[1 + line_skip]);
00089       gray_buffer += 2 + gray_line_skip;
00090       bayer_pixel += 2;
00091 
00092       for (register unsigned yIdx = 1; yIdx < height - 1; yIdx += 2)
00093       {
00094         // blue line
00095         gray_buffer[0] = AVG3 (bayer_pixel[-line_skip], bayer_pixel[line_skip], bayer_pixel[1]);
00096         gray_buffer[1] = bayer_pixel[1];
00097         gray_buffer += 2;
00098         bayer_pixel += 2;
00099         for (register unsigned xIdx = 2; xIdx < width; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
00100         {
00101           gray_buffer[0] = AVG4 (bayer_pixel[-line_skip], bayer_pixel[line_skip], bayer_pixel[-1], bayer_pixel[1]);
00102           gray_buffer[1] = bayer_pixel[1];
00103         }
00104         gray_buffer += gray_line_skip;
00105 
00106         // red line
00107         for (register unsigned xIdx = 0; xIdx < width - 2; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
00108         {
00109           gray_buffer[0] = bayer_pixel[0]; // green pixel
00110           gray_buffer[1] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[-line_skip + 1], bayer_pixel[line_skip + 1]); // interpolated green pixel
00111         }
00112         gray_buffer[0] = bayer_pixel[0];
00113         gray_buffer[1] = AVG3 (bayer_pixel[-line_skip + 1], bayer_pixel[line_skip + 1], bayer_pixel[-1]);
00114         gray_buffer += 2 + gray_line_skip;
00115         bayer_pixel += 2;
00116       }
00117 
00118       // last line BGBGBG
00119       gray_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-line_skip]);
00120       gray_buffer[1] = bayer_pixel[1];
00121       gray_buffer += 2;
00122       bayer_pixel += 2;
00123       for (register unsigned xIdx = 2; xIdx < width; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
00124       {
00125         gray_buffer[0] = AVG3 (bayer_pixel[-1], bayer_pixel[1], bayer_pixel[-line_skip]);
00126         gray_buffer[1] = bayer_pixel[1];
00127       }
00128     }
00129     else if (debayering_method_ == EdgeAware)
00130     {
00131       int dv, dh;
00132       // first line GRGRGR
00133       for (register unsigned xIdx = 0; xIdx < width - 2; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
00134       {
00135         gray_buffer[0] = bayer_pixel[0]; // green pixel
00136         gray_buffer[1] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[1 + line_skip]); // interpolated green pixel
00137       }
00138       gray_buffer[0] = bayer_pixel[0];
00139       gray_buffer[1] = AVG (bayer_pixel[0], bayer_pixel[1 + line_skip]);
00140       gray_buffer += 2 + gray_line_skip;
00141       bayer_pixel += 2;
00142 
00143       for (register unsigned yIdx = 1; yIdx < height - 1; yIdx += 2)
00144       {
00145         // blue line
00146         gray_buffer[0] = AVG3 (bayer_pixel[-line_skip], bayer_pixel[line_skip], bayer_pixel[1]);
00147         gray_buffer[1] = bayer_pixel[1];
00148         gray_buffer += 2;
00149         bayer_pixel += 2;
00150         for (register unsigned xIdx = 2; xIdx < width; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
00151         {
00152           dv = abs (bayer_pixel[-line_skip] - bayer_pixel[line_skip]);
00153           dh = abs (bayer_pixel[-1] - bayer_pixel[1]);
00154           if (dh > dv)
00155             gray_buffer[0] = AVG (bayer_pixel[-line_skip], bayer_pixel[line_skip]);
00156           else if (dv > dh)
00157             gray_buffer[0] = AVG (bayer_pixel[-1], bayer_pixel[1]);
00158           else
00159             gray_buffer[0] = AVG4 (bayer_pixel[-line_skip], bayer_pixel[line_skip], bayer_pixel[-1], bayer_pixel[1]);
00160 
00161           gray_buffer[1] = bayer_pixel[1];
00162         }
00163         gray_buffer += gray_line_skip;
00164 
00165         // red line
00166         for (register unsigned xIdx = 0; xIdx < width - 2; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
00167         {
00168           gray_buffer[0] = bayer_pixel[0];
00169 
00170           dv = abs (bayer_pixel[1 - line_skip] - bayer_pixel[1 + line_skip]);
00171           dh = abs (bayer_pixel[0] - bayer_pixel[2]);
00172           if (dh > dv)
00173             gray_buffer[1] = AVG (bayer_pixel[1 - line_skip], bayer_pixel[1 + line_skip]);
00174           else if (dv > dh)
00175             gray_buffer[1] = AVG (bayer_pixel[0], bayer_pixel[2]);
00176           else
00177             gray_buffer[1] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[-line_skip + 1], bayer_pixel[line_skip + 1]);
00178         }
00179         gray_buffer[0] = bayer_pixel[0];
00180         gray_buffer[1] = AVG3 (bayer_pixel[-line_skip + 1], bayer_pixel[line_skip + 1], bayer_pixel[-1]);
00181         gray_buffer += 2 + gray_line_skip;
00182         bayer_pixel += 2;
00183       }
00184 
00185       // last line BGBGBG
00186       gray_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-line_skip]);
00187       gray_buffer[1] = bayer_pixel[1];
00188       gray_buffer += 2;
00189       bayer_pixel += 2;
00190       for (register unsigned xIdx = 2; xIdx < width; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
00191       {
00192         gray_buffer[0] = AVG3 (bayer_pixel[-1], bayer_pixel[1], bayer_pixel[-line_skip]);
00193         gray_buffer[1] = bayer_pixel[1];
00194       }
00195     }
00196     else if (debayering_method_ == EdgeAwareWeighted)
00197     {
00198       int dv, dh;
00199       // first line GRGRGR
00200       for (register unsigned xIdx = 0; xIdx < width - 2; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
00201       {
00202         gray_buffer[0] = bayer_pixel[0]; // green pixel
00203         gray_buffer[1] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[1 + line_skip]); // interpolated green pixel
00204       }
00205       gray_buffer[0] = bayer_pixel[0];
00206       gray_buffer[1] = AVG (bayer_pixel[0], bayer_pixel[1 + line_skip]);
00207       gray_buffer += 2 + gray_line_skip;
00208       bayer_pixel += 2;
00209 
00210       for (register unsigned yIdx = 1; yIdx < height - 1; yIdx += 2)
00211       {
00212         // blue line
00213         gray_buffer[0] = AVG3 (bayer_pixel[-line_skip], bayer_pixel[line_skip], bayer_pixel[1]);
00214         gray_buffer[1] = bayer_pixel[1];
00215         gray_buffer += 2;
00216         bayer_pixel += 2;
00217         for (register unsigned xIdx = 2; xIdx < width; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
00218         {
00219           dv = abs (bayer_pixel[-line_skip] - bayer_pixel[line_skip]);
00220           dh = abs (bayer_pixel[-1] - bayer_pixel[1]);
00221 
00222           if (dv == 0 && dh == 0)
00223             gray_buffer[0] = AVG4 (bayer_pixel[-line_skip], bayer_pixel[line_skip], bayer_pixel[-1], bayer_pixel[1]);
00224           else
00225             gray_buffer[0] = WAVG4 (bayer_pixel[-line_skip], bayer_pixel[line_skip], bayer_pixel[-1], bayer_pixel[1], dh, dv);
00226 
00227           gray_buffer[1] = bayer_pixel[1];
00228         }
00229 
00230         gray_buffer += gray_line_skip;
00231 
00232         // red line
00233         for (register unsigned xIdx = 0; xIdx < width - 2; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
00234         {
00235           gray_buffer[0] = bayer_pixel[0];
00236 
00237           dv = abs (bayer_pixel[1 - line_skip] - bayer_pixel[1 + line_skip]);
00238           dh = abs (bayer_pixel[0] - bayer_pixel[2]);
00239 
00240           if (dv == 0 && dh == 0)
00241             gray_buffer[1] = AVG4 (bayer_pixel[1 - line_skip], bayer_pixel[1 + line_skip], bayer_pixel[0], bayer_pixel[2]);
00242           else
00243             gray_buffer[1] = WAVG4 (bayer_pixel[1 - line_skip], bayer_pixel[1 + line_skip], bayer_pixel[0], bayer_pixel[2], dh, dv);
00244         }
00245         gray_buffer[0] = bayer_pixel[0];
00246         gray_buffer[1] = AVG3 (bayer_pixel[-line_skip + 1], bayer_pixel[line_skip + 1], bayer_pixel[-1]);
00247         gray_buffer += 2 + gray_line_skip;
00248         bayer_pixel += 2;
00249       }
00250 
00251       // last line BGBGBG
00252       gray_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-line_skip]);
00253       gray_buffer[1] = bayer_pixel[1];
00254       gray_buffer += 2;
00255       bayer_pixel += 2;
00256       for (register unsigned xIdx = 2; xIdx < width; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
00257       {
00258         gray_buffer[0] = AVG3 (bayer_pixel[-1], bayer_pixel[1], bayer_pixel[-line_skip]);
00259         gray_buffer[1] = bayer_pixel[1];
00260       }
00261     }
00262     else
00263       THROW_OPENNI_EXCEPTION ("Unknown Debayering method: %d", (int)debayering_method_);
00264 
00265   }
00266   else // downsampling
00267   {
00268     if ((image_md_->XRes () >> 1) % width != 0 || (image_md_->YRes () >> 1) % height != 0)
00269       THROW_OPENNI_EXCEPTION ("Downsampling only possible for multiple of 2 in both dimensions. Request was %d x %d -> %d x %d.", image_md_->XRes (), image_md_->YRes (), width, height);
00270 
00271     // fast method -> simply takes each or each 2nd pixel-group to get gray values out
00272     register unsigned bayerXStep = image_md_->XRes () / width;
00273     register unsigned bayerYSkip = (image_md_->YRes () / height - 1) * image_md_->XRes ();
00274 
00275     // Downsampling and debayering at once
00276     register const XnUInt8* bayer_buffer = image_md_->WritableData ();
00277 
00278     for (register unsigned yIdx = 0; yIdx < height; ++yIdx, bayer_buffer += bayerYSkip, gray_buffer += gray_line_skip) // skip a line
00279     {
00280       for (register unsigned xIdx = 0; xIdx < width; ++xIdx, ++gray_buffer, bayer_buffer += bayerXStep)
00281       {
00282         *gray_buffer = AVG (bayer_buffer[0], bayer_buffer[ image_md_->XRes () + 1]);
00283       }
00284     }
00285   } // downsampling
00286 }
00287 
00288 void ImageBayerGRBG::fillRGB (unsigned width, unsigned height, unsigned char* rgb_buffer, unsigned rgb_line_step) const throw (OpenNIException)
00289 {
00290   if (width > image_md_->XRes () || height > image_md_->YRes ())
00291     THROW_OPENNI_EXCEPTION ("Upsampling only possible for multiple of 2 in both dimensions. Request was %d x %d -> %d x %d.", image_md_->XRes (), image_md_->YRes (), width, height);
00292 
00293   if (rgb_line_step == 0)
00294     rgb_line_step = width * 3;
00295 
00296   // padding skip for destination image
00297   unsigned rgb_line_skip = rgb_line_step - width * 3;
00298 
00299   if (image_md_->XRes () == width && image_md_->YRes () == height)
00300   {
00301     register const XnUInt8 *bayer_pixel = image_md_->WritableData ();
00302     register unsigned yIdx, xIdx;
00303 
00304     int bayer_line_step = image_md_->XRes ();
00305     int bayer_line_step2 = image_md_->XRes () << 1;
00306 
00307     if (debayering_method_ == Bilinear)
00308     {
00309       // first two pixel values for first two lines
00310       // Bayer         0 1 2
00311       //         0     G r g
00312       // line_step     b g b
00313       // line_step2    g r g
00314 
00315       rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00316       rgb_buffer[1] = bayer_pixel[0]; // green pixel
00317       rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
00318 
00319       // Bayer         0 1 2
00320       //         0     g R g
00321       // line_step     b g b
00322       // line_step2    g r g
00323       //rgb_pixel[3] = bayer_pixel[1];
00324       rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00325       rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00326 
00327       // BGBG line
00328       // Bayer         0 1 2
00329       //         0     g r g
00330       // line_step     B g b
00331       // line_step2    g r g
00332       rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00333       rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00334       //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00335 
00336       // pixel (1, 1)  0 1 2
00337       //         0     g r g
00338       // line_step     b G b
00339       // line_step2    g r g
00340       //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00341       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00342       //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
00343 
00344       rgb_buffer += 6;
00345       bayer_pixel += 2;
00346       // rest of the first two lines
00347 
00348       for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00349       {
00350         // GRGR line
00351         // Bayer        -1 0 1 2
00352         //           0   r G r g
00353         //   line_step   g b g b
00354         // line_step2    r g r g
00355         rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00356         rgb_buffer[1] = bayer_pixel[0];
00357         rgb_buffer[2] = bayer_pixel[bayer_line_step + 1];
00358 
00359         // Bayer        -1 0 1 2
00360         //          0    r g R g
00361         //  line_step    g b g b
00362         // line_step2    r g r g
00363         rgb_buffer[3] = bayer_pixel[1];
00364         rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00365         rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00366 
00367         // BGBG line
00368         // Bayer         -1 0 1 2
00369         //         0      r g r g
00370         // line_step      g B g b
00371         // line_step2     r g r g
00372         rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00373         rgb_buffer[rgb_line_step + 1] = AVG4 (bayer_pixel[0], bayer_pixel[bayer_line_step2], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00374         rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00375 
00376         // Bayer         -1 0 1 2
00377         //         0      r g r g
00378         // line_step      g b G b
00379         // line_step2     r g r g
00380         rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00381         rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00382         //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
00383       }
00384 
00385       // last two pixel values for first two lines
00386       // GRGR line
00387       // Bayer        -1 0 1
00388       //           0   r G r
00389       //   line_step   g b g
00390       // line_step2    r g r
00391       rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00392       rgb_buffer[1] = bayer_pixel[0];
00393       rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00394 
00395       // Bayer        -1 0 1
00396       //          0    r g R
00397       //  line_step    g b g
00398       // line_step2    r g r
00399       rgb_buffer[3] = bayer_pixel[1];
00400       rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00401       //rgb_pixel[5] = bayer_pixel[line_step];
00402 
00403       // BGBG line
00404       // Bayer        -1 0 1
00405       //          0    r g r
00406       //  line_step    g B g
00407       // line_step2    r g r
00408       rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00409       rgb_buffer[rgb_line_step + 1] = AVG4 (bayer_pixel[0], bayer_pixel[bayer_line_step2], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00410       //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00411 
00412       // Bayer         -1 0 1
00413       //         0      r g r
00414       // line_step      g b G
00415       // line_step2     r g r
00416       rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00417       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00418       //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00419 
00420       bayer_pixel += bayer_line_step + 2;
00421       rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
00422 
00423       // main processing
00424 
00425       for (yIdx = 2; yIdx < height - 2; yIdx += 2)
00426       {
00427         // first two pixel values
00428         // Bayer         0 1 2
00429         //        -1     b g b
00430         //         0     G r g
00431         // line_step     b g b
00432         // line_step2    g r g
00433 
00434         rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00435         rgb_buffer[1] = bayer_pixel[0]; // green pixel
00436         rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]); // blue;
00437 
00438         // Bayer         0 1 2
00439         //        -1     b g b
00440         //         0     g R g
00441         // line_step     b g b
00442         // line_step2    g r g
00443         //rgb_pixel[3] = bayer_pixel[1];
00444         rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00445         rgb_buffer[5] = AVG4 (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2], bayer_pixel[-bayer_line_step], bayer_pixel[2 - bayer_line_step]);
00446 
00447         // BGBG line
00448         // Bayer         0 1 2
00449         //         0     g r g
00450         // line_step     B g b
00451         // line_step2    g r g
00452         rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00453         rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00454         rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00455 
00456         // pixel (1, 1)  0 1 2
00457         //         0     g r g
00458         // line_step     b G b
00459         // line_step2    g r g
00460         //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00461         rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00462         rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00463 
00464         rgb_buffer += 6;
00465         bayer_pixel += 2;
00466         // continue with rest of the line
00467         for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00468         {
00469           // GRGR line
00470           // Bayer        -1 0 1 2
00471           //          -1   g b g b
00472           //           0   r G r g
00473           //   line_step   g b g b
00474           // line_step2    r g r g
00475           rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00476           rgb_buffer[1] = bayer_pixel[0];
00477           rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00478 
00479           // Bayer        -1 0 1 2
00480           //          -1   g b g b
00481           //          0    r g R g
00482           //  line_step    g b g b
00483           // line_step2    r g r g
00484           rgb_buffer[3] = bayer_pixel[1];
00485           rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00486           rgb_buffer[5] = AVG4 (bayer_pixel[-bayer_line_step], bayer_pixel[2 - bayer_line_step], bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00487 
00488           // BGBG line
00489           // Bayer         -1 0 1 2
00490           //         -1     g b g b
00491           //          0     r g r g
00492           // line_step      g B g b
00493           // line_step2     r g r g
00494           rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00495           rgb_buffer[rgb_line_step + 1] = AVG4 (bayer_pixel[0], bayer_pixel[bayer_line_step2], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00496           rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00497 
00498           // Bayer         -1 0 1 2
00499           //         -1     g b g b
00500           //          0     r g r g
00501           // line_step      g b G b
00502           // line_step2     r g r g
00503           rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00504           rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00505           rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00506         }
00507 
00508         // last two pixels of the line
00509         // last two pixel values for first two lines
00510         // GRGR line
00511         // Bayer        -1 0 1
00512         //           0   r G r
00513         //   line_step   g b g
00514         // line_step2    r g r
00515         rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00516         rgb_buffer[1] = bayer_pixel[0];
00517         rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00518 
00519         // Bayer        -1 0 1
00520         //          0    r g R
00521         //  line_step    g b g
00522         // line_step2    r g r
00523         rgb_buffer[3] = bayer_pixel[1];
00524         rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00525         //rgb_pixel[5] = bayer_pixel[line_step];
00526 
00527         // BGBG line
00528         // Bayer        -1 0 1
00529         //          0    r g r
00530         //  line_step    g B g
00531         // line_step2    r g r
00532         rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00533         rgb_buffer[rgb_line_step + 1] = AVG4 (bayer_pixel[0], bayer_pixel[bayer_line_step2], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00534         //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00535 
00536         // Bayer         -1 0 1
00537         //         0      r g r
00538         // line_step      g b G
00539         // line_step2     r g r
00540         rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00541         rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00542         //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00543 
00544         bayer_pixel += bayer_line_step + 2;
00545         rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
00546       }
00547 
00548       //last two lines
00549       // Bayer         0 1 2
00550       //        -1     b g b
00551       //         0     G r g
00552       // line_step     b g b
00553 
00554       rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00555       rgb_buffer[1] = bayer_pixel[0]; // green pixel
00556       rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
00557 
00558       // Bayer         0 1 2
00559       //        -1     b g b
00560       //         0     g R g
00561       // line_step     b g b
00562       //rgb_pixel[3] = bayer_pixel[1];
00563       rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00564       rgb_buffer[5] = AVG4 (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2], bayer_pixel[-bayer_line_step], bayer_pixel[2 - bayer_line_step]);
00565 
00566       // BGBG line
00567       // Bayer         0 1 2
00568       //        -1     b g b
00569       //         0     g r g
00570       // line_step     B g b
00571       //rgb_pixel[rgb_line_step    ] = bayer_pixel[1];
00572       rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00573       rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00574 
00575       // Bayer         0 1 2
00576       //        -1     b g b
00577       //         0     g r g
00578       // line_step     b G b
00579       //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00580       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00581       rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00582 
00583       rgb_buffer += 6;
00584       bayer_pixel += 2;
00585       // rest of the last two lines
00586       for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00587       {
00588         // GRGR line
00589         // Bayer       -1 0 1 2
00590         //        -1    g b g b
00591         //         0    r G r g
00592         // line_step    g b g b
00593         rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00594         rgb_buffer[1] = bayer_pixel[0];
00595         rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00596 
00597         // Bayer       -1 0 1 2
00598         //        -1    g b g b
00599         //         0    r g R g
00600         // line_step    g b g b
00601         rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
00602         rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00603         rgb_buffer[5] = AVG4 (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2], bayer_pixel[-bayer_line_step], bayer_pixel[-bayer_line_step + 2]);
00604 
00605         // BGBG line
00606         // Bayer       -1 0 1 2
00607         //        -1    g b g b
00608         //         0    r g r g
00609         // line_step    g B g b
00610         rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[-1], bayer_pixel[1]);
00611         rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00612         rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00613 
00614 
00615         // Bayer       -1 0 1 2
00616         //        -1    g b g b
00617         //         0    r g r g
00618         // line_step    g b G b
00619         //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
00620         rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00621         rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00622       }
00623 
00624       // last two pixel values for first two lines
00625       // GRGR line
00626       // Bayer       -1 0 1
00627       //        -1    g b g
00628       //         0    r G r
00629       // line_step    g b g
00630       rgb_buffer[rgb_line_step ] = rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00631       rgb_buffer[1] = bayer_pixel[0];
00632       rgb_buffer[5] = rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00633 
00634       // Bayer       -1 0 1
00635       //        -1    g b g
00636       //         0    r g R
00637       // line_step    g b g
00638       rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
00639       rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[-bayer_line_step + 1]);
00640       //rgb_pixel[5] = AVG( bayer_pixel[line_step], bayer_pixel[-line_step] );
00641 
00642       // BGBG line
00643       // Bayer       -1 0 1
00644       //        -1    g b g
00645       //         0    r g r
00646       // line_step    g B g
00647       //rgb_pixel[rgb_line_step    ] = AVG2( bayer_pixel[-1], bayer_pixel[1] );
00648       rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00649       rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00650 
00651       // Bayer       -1 0 1
00652       //        -1    g b g
00653       //         0    r g r
00654       // line_step    g b G
00655       //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
00656       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00657       //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00658     }
00659     else if (debayering_method_ == EdgeAware)
00660     {
00661       int dh, dv;
00662 
00663       // first two pixel values for first two lines
00664       // Bayer         0 1 2
00665       //         0     G r g
00666       // line_step     b g b
00667       // line_step2    g r g
00668 
00669       rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00670       rgb_buffer[1] = bayer_pixel[0]; // green pixel
00671       rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
00672 
00673       // Bayer         0 1 2
00674       //         0     g R g
00675       // line_step     b g b
00676       // line_step2    g r g
00677       //rgb_pixel[3] = bayer_pixel[1];
00678       rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00679       rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00680 
00681       // BGBG line
00682       // Bayer         0 1 2
00683       //         0     g r g
00684       // line_step     B g b
00685       // line_step2    g r g
00686       rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00687       rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00688       //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00689 
00690       // pixel (1, 1)  0 1 2
00691       //         0     g r g
00692       // line_step     b G b
00693       // line_step2    g r g
00694       //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00695       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00696       //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
00697 
00698       rgb_buffer += 6;
00699       bayer_pixel += 2;
00700       // rest of the first two lines
00701       for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00702       {
00703         // GRGR line
00704         // Bayer        -1 0 1 2
00705         //           0   r G r g
00706         //   line_step   g b g b
00707         // line_step2    r g r g
00708         rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00709         rgb_buffer[1] = bayer_pixel[0];
00710         rgb_buffer[2] = bayer_pixel[bayer_line_step + 1];
00711 
00712         // Bayer        -1 0 1 2
00713         //          0    r g R g
00714         //  line_step    g b g b
00715         // line_step2    r g r g
00716         rgb_buffer[3] = bayer_pixel[1];
00717         rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00718         rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00719 
00720         // BGBG line
00721         // Bayer         -1 0 1 2
00722         //         0      r g r g
00723         // line_step      g B g b
00724         // line_step2     r g r g
00725         rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00726         rgb_buffer[rgb_line_step + 1] = AVG4 (bayer_pixel[0], bayer_pixel[bayer_line_step2], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00727         rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00728 
00729         // Bayer         -1 0 1 2
00730         //         0      r g r g
00731         // line_step      g b G b
00732         // line_step2     r g r g
00733         rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00734         rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00735         //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
00736       }
00737 
00738       // last two pixel values for first two lines
00739       // GRGR line
00740       // Bayer        -1 0 1
00741       //           0   r G r
00742       //   line_step   g b g
00743       // line_step2    r g r
00744       rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00745       rgb_buffer[1] = bayer_pixel[0];
00746       rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00747 
00748       // Bayer        -1 0 1
00749       //          0    r g R
00750       //  line_step    g b g
00751       // line_step2    r g r
00752       rgb_buffer[3] = bayer_pixel[1];
00753       rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00754       //rgb_pixel[5] = bayer_pixel[line_step];
00755 
00756       // BGBG line
00757       // Bayer        -1 0 1
00758       //          0    r g r
00759       //  line_step    g B g
00760       // line_step2    r g r
00761       rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00762       rgb_buffer[rgb_line_step + 1] = AVG4 (bayer_pixel[0], bayer_pixel[bayer_line_step2], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00763       //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00764 
00765       // Bayer         -1 0 1
00766       //         0      r g r
00767       // line_step      g b G
00768       // line_step2     r g r
00769       rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00770       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00771       //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00772 
00773       bayer_pixel += bayer_line_step + 2;
00774       rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
00775       // main processing
00776       for (yIdx = 2; yIdx < height - 2; yIdx += 2)
00777       {
00778         // first two pixel values
00779         // Bayer         0 1 2
00780         //        -1     b g b
00781         //         0     G r g
00782         // line_step     b g b
00783         // line_step2    g r g
00784 
00785         rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00786         rgb_buffer[1] = bayer_pixel[0]; // green pixel
00787         rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]); // blue;
00788 
00789         // Bayer         0 1 2
00790         //        -1     b g b
00791         //         0     g R g
00792         // line_step     b g b
00793         // line_step2    g r g
00794         //rgb_pixel[3] = bayer_pixel[1];
00795         rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00796         rgb_buffer[5] = AVG4 (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2], bayer_pixel[-bayer_line_step], bayer_pixel[2 - bayer_line_step]);
00797 
00798         // BGBG line
00799         // Bayer         0 1 2
00800         //         0     g r g
00801         // line_step     B g b
00802         // line_step2    g r g
00803         rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00804         rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00805         rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00806 
00807         // pixel (1, 1)  0 1 2
00808         //         0     g r g
00809         // line_step     b G b
00810         // line_step2    g r g
00811         //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00812         rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00813         rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00814 
00815         rgb_buffer += 6;
00816         bayer_pixel += 2;
00817         // continue with rest of the line
00818         for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00819         {
00820           // GRGR line
00821           // Bayer        -1 0 1 2
00822           //          -1   g b g b
00823           //           0   r G r g
00824           //   line_step   g b g b
00825           // line_step2    r g r g
00826           rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00827           rgb_buffer[1] = bayer_pixel[0];
00828           rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00829 
00830           // Bayer        -1 0 1 2
00831           //          -1   g b g b
00832           //          0    r g R g
00833           //  line_step    g b g b
00834           // line_step2    r g r g
00835 
00836           dh = abs (bayer_pixel[0] - bayer_pixel[2]);
00837           dv = abs (bayer_pixel[-bayer_line_step + 1] - bayer_pixel[bayer_line_step + 1]);
00838 
00839           if (dh > dv)
00840             rgb_buffer[4] = AVG (bayer_pixel[-bayer_line_step + 1], bayer_pixel[bayer_line_step + 1]);
00841           else if (dv > dh)
00842             rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[2]);
00843           else
00844             rgb_buffer[4] = AVG4 (bayer_pixel[-bayer_line_step + 1], bayer_pixel[bayer_line_step + 1], bayer_pixel[0], bayer_pixel[2]);
00845 
00846           rgb_buffer[3] = bayer_pixel[1];
00847           rgb_buffer[5] = AVG4 (bayer_pixel[-bayer_line_step], bayer_pixel[2 - bayer_line_step], bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00848 
00849           // BGBG line
00850           // Bayer         -1 0 1 2
00851           //         -1     g b g b
00852           //          0     r g r g
00853           // line_step      g B g b
00854           // line_step2     r g r g
00855           rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00856           rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00857 
00858           dv = abs (bayer_pixel[0] - bayer_pixel[bayer_line_step2]);
00859           dh = abs (bayer_pixel[bayer_line_step - 1] - bayer_pixel[bayer_line_step + 1]);
00860 
00861           if (dv > dh)
00862             rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00863           else if (dh > dv)
00864             rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step2]);
00865           else
00866             rgb_buffer[rgb_line_step + 1] = AVG4 (bayer_pixel[0], bayer_pixel[bayer_line_step2], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00867 
00868           // Bayer         -1 0 1 2
00869           //         -1     g b g b
00870           //          0     r g r g
00871           // line_step      g b G b
00872           // line_step2     r g r g
00873           rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00874           rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00875           rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00876         }
00877 
00878         // last two pixels of the line
00879         // last two pixel values for first two lines
00880         // GRGR line
00881         // Bayer        -1 0 1
00882         //           0   r G r
00883         //   line_step   g b g
00884         // line_step2    r g r
00885         rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00886         rgb_buffer[1] = bayer_pixel[0];
00887         rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00888 
00889         // Bayer        -1 0 1
00890         //          0    r g R
00891         //  line_step    g b g
00892         // line_step2    r g r
00893         rgb_buffer[3] = bayer_pixel[1];
00894         rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00895         //rgb_pixel[5] = bayer_pixel[line_step];
00896 
00897         // BGBG line
00898         // Bayer        -1 0 1
00899         //          0    r g r
00900         //  line_step    g B g
00901         // line_step2    r g r
00902         rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00903         rgb_buffer[rgb_line_step + 1] = AVG4 (bayer_pixel[0], bayer_pixel[bayer_line_step2], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00904         //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00905 
00906         // Bayer         -1 0 1
00907         //         0      r g r
00908         // line_step      g b G
00909         // line_step2     r g r
00910         rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00911         rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00912         //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00913 
00914         bayer_pixel += bayer_line_step + 2;
00915         rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
00916       }
00917 
00918       //last two lines
00919       // Bayer         0 1 2
00920       //        -1     b g b
00921       //         0     G r g
00922       // line_step     b g b
00923 
00924       rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00925       rgb_buffer[1] = bayer_pixel[0]; // green pixel
00926       rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
00927 
00928       // Bayer         0 1 2
00929       //        -1     b g b
00930       //         0     g R g
00931       // line_step     b g b
00932       //rgb_pixel[3] = bayer_pixel[1];
00933       rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00934       rgb_buffer[5] = AVG4 (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2], bayer_pixel[-bayer_line_step], bayer_pixel[2 - bayer_line_step]);
00935 
00936       // BGBG line
00937       // Bayer         0 1 2
00938       //        -1     b g b
00939       //         0     g r g
00940       // line_step     B g b
00941       //rgb_pixel[rgb_line_step    ] = bayer_pixel[1];
00942       rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00943       rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00944 
00945       // Bayer         0 1 2
00946       //        -1     b g b
00947       //         0     g r g
00948       // line_step     b G b
00949       //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00950       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00951       rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00952 
00953       rgb_buffer += 6;
00954       bayer_pixel += 2;
00955       // rest of the last two lines
00956       for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00957       {
00958         // GRGR line
00959         // Bayer       -1 0 1 2
00960         //        -1    g b g b
00961         //         0    r G r g
00962         // line_step    g b g b
00963         rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00964         rgb_buffer[1] = bayer_pixel[0];
00965         rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00966 
00967         // Bayer       -1 0 1 2
00968         //        -1    g b g b
00969         //         0    r g R g
00970         // line_step    g b g b
00971         rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
00972         rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00973         rgb_buffer[5] = AVG4 (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2], bayer_pixel[-bayer_line_step], bayer_pixel[-bayer_line_step + 2]);
00974 
00975         // BGBG line
00976         // Bayer       -1 0 1 2
00977         //        -1    g b g b
00978         //         0    r g r g
00979         // line_step    g B g b
00980         rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[-1], bayer_pixel[1]);
00981         rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00982         rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00983 
00984 
00985         // Bayer       -1 0 1 2
00986         //        -1    g b g b
00987         //         0    r g r g
00988         // line_step    g b G b
00989         //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
00990         rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00991         rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00992       }
00993 
00994       // last two pixel values for first two lines
00995       // GRGR line
00996       // Bayer       -1 0 1
00997       //        -1    g b g
00998       //         0    r G r
00999       // line_step    g b g
01000       rgb_buffer[rgb_line_step ] = rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
01001       rgb_buffer[1] = bayer_pixel[0];
01002       rgb_buffer[5] = rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
01003 
01004       // Bayer       -1 0 1
01005       //        -1    g b g
01006       //         0    r g R
01007       // line_step    g b g
01008       rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
01009       rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[-bayer_line_step + 1]);
01010       //rgb_pixel[5] = AVG( bayer_pixel[line_step], bayer_pixel[-line_step] );
01011 
01012       // BGBG line
01013       // Bayer       -1 0 1
01014       //        -1    g b g
01015       //         0    r g r
01016       // line_step    g B g
01017       //rgb_pixel[rgb_line_step    ] = AVG2( bayer_pixel[-1], bayer_pixel[1] );
01018       rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
01019       rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
01020 
01021       // Bayer       -1 0 1
01022       //        -1    g b g
01023       //         0    r g r
01024       // line_step    g b G
01025       //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
01026       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01027       //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
01028     }
01029     else if (debayering_method_ == EdgeAwareWeighted)
01030     {
01031       int dh, dv;
01032 
01033       // first two pixel values for first two lines
01034       // Bayer         0 1 2
01035       //         0     G r g
01036       // line_step     b g b
01037       // line_step2    g r g
01038 
01039       rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
01040       rgb_buffer[1] = bayer_pixel[0]; // green pixel
01041       rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
01042 
01043       // Bayer         0 1 2
01044       //         0     g R g
01045       // line_step     b g b
01046       // line_step2    g r g
01047       //rgb_pixel[3] = bayer_pixel[1];
01048       rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
01049       rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
01050 
01051       // BGBG line
01052       // Bayer         0 1 2
01053       //         0     g r g
01054       // line_step     B g b
01055       // line_step2    g r g
01056       rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
01057       rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
01058       //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
01059 
01060       // pixel (1, 1)  0 1 2
01061       //         0     g r g
01062       // line_step     b G b
01063       // line_step2    g r g
01064       //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
01065       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01066       //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
01067 
01068       rgb_buffer += 6;
01069       bayer_pixel += 2;
01070       // rest of the first two lines
01071       for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
01072       {
01073         // GRGR line
01074         // Bayer        -1 0 1 2
01075         //           0   r G r g
01076         //   line_step   g b g b
01077         // line_step2    r g r g
01078         rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
01079         rgb_buffer[1] = bayer_pixel[0];
01080         rgb_buffer[2] = bayer_pixel[bayer_line_step + 1];
01081 
01082         // Bayer        -1 0 1 2
01083         //          0    r g R g
01084         //  line_step    g b g b
01085         // line_step2    r g r g
01086         rgb_buffer[3] = bayer_pixel[1];
01087         rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
01088         rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
01089 
01090         // BGBG line
01091         // Bayer         -1 0 1 2
01092         //         0      r g r g
01093         // line_step      g B g b
01094         // line_step2     r g r g
01095         rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
01096         rgb_buffer[rgb_line_step + 1] = AVG4 (bayer_pixel[0], bayer_pixel[bayer_line_step2], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
01097         rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
01098 
01099         // Bayer         -1 0 1 2
01100         //         0      r g r g
01101         // line_step      g b G b
01102         // line_step2     r g r g
01103         rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
01104         rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01105         //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
01106       }
01107 
01108       // last two pixel values for first two lines
01109       // GRGR line
01110       // Bayer        -1 0 1
01111       //           0   r G r
01112       //   line_step   g b g
01113       // line_step2    r g r
01114       rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
01115       rgb_buffer[1] = bayer_pixel[0];
01116       rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
01117 
01118       // Bayer        -1 0 1
01119       //          0    r g R
01120       //  line_step    g b g
01121       // line_step2    r g r
01122       rgb_buffer[3] = bayer_pixel[1];
01123       rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
01124       //rgb_pixel[5] = bayer_pixel[line_step];
01125 
01126       // BGBG line
01127       // Bayer        -1 0 1
01128       //          0    r g r
01129       //  line_step    g B g
01130       // line_step2    r g r
01131       rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
01132       rgb_buffer[rgb_line_step + 1] = AVG4 (bayer_pixel[0], bayer_pixel[bayer_line_step2], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
01133       //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
01134 
01135       // Bayer         -1 0 1
01136       //         0      r g r
01137       // line_step      g b G
01138       // line_step2     r g r
01139       rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
01140       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01141       //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
01142 
01143       bayer_pixel += bayer_line_step + 2;
01144       rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
01145       // main processing
01146       for (yIdx = 2; yIdx < height - 2; yIdx += 2)
01147       {
01148         // first two pixel values
01149         // Bayer         0 1 2
01150         //        -1     b g b
01151         //         0     G r g
01152         // line_step     b g b
01153         // line_step2    g r g
01154 
01155         rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
01156         rgb_buffer[1] = bayer_pixel[0]; // green pixel
01157         rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]); // blue;
01158 
01159         // Bayer         0 1 2
01160         //        -1     b g b
01161         //         0     g R g
01162         // line_step     b g b
01163         // line_step2    g r g
01164         //rgb_pixel[3] = bayer_pixel[1];
01165         rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
01166         rgb_buffer[5] = AVG4 (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2], bayer_pixel[-bayer_line_step], bayer_pixel[2 - bayer_line_step]);
01167 
01168         // BGBG line
01169         // Bayer         0 1 2
01170         //         0     g r g
01171         // line_step     B g b
01172         // line_step2    g r g
01173         rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
01174         rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
01175         rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
01176 
01177         // pixel (1, 1)  0 1 2
01178         //         0     g r g
01179         // line_step     b G b
01180         // line_step2    g r g
01181         //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
01182         rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01183         rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
01184 
01185         rgb_buffer += 6;
01186         bayer_pixel += 2;
01187         // continue with rest of the line
01188         for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
01189         {
01190           // GRGR line
01191           // Bayer        -1 0 1 2
01192           //          -1   g b g b
01193           //           0   r G r g
01194           //   line_step   g b g b
01195           // line_step2    r g r g
01196           rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
01197           rgb_buffer[1] = bayer_pixel[0];
01198           rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
01199 
01200           // Bayer        -1 0 1 2
01201           //          -1   g b g b
01202           //          0    r g R g
01203           //  line_step    g b g b
01204           // line_step2    r g r g
01205 
01206           dh = abs (bayer_pixel[0] - bayer_pixel[2]);
01207           dv = abs (bayer_pixel[-bayer_line_step + 1] - bayer_pixel[bayer_line_step + 1]);
01208 
01209           if (dv == 0 && dh == 0)
01210             rgb_buffer[4] = AVG4 (bayer_pixel[1 - bayer_line_step], bayer_pixel[1 + bayer_line_step], bayer_pixel[0], bayer_pixel[2]);
01211           else
01212             rgb_buffer[4] = WAVG4 (bayer_pixel[1 - bayer_line_step], bayer_pixel[1 + bayer_line_step], bayer_pixel[0], bayer_pixel[2], dh, dv);
01213           rgb_buffer[3] = bayer_pixel[1];
01214           rgb_buffer[5] = AVG4 (bayer_pixel[-bayer_line_step], bayer_pixel[2 - bayer_line_step], bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
01215 
01216           // BGBG line
01217           // Bayer         -1 0 1 2
01218           //         -1     g b g b
01219           //          0     r g r g
01220           // line_step      g B g b
01221           // line_step2     r g r g
01222           rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
01223           rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
01224 
01225           dv = abs (bayer_pixel[0] - bayer_pixel[bayer_line_step2]);
01226           dh = abs (bayer_pixel[bayer_line_step - 1] - bayer_pixel[bayer_line_step + 1]);
01227 
01228           if (dv == 0 && dh == 0)
01229             rgb_buffer[rgb_line_step + 1] = AVG4 (bayer_pixel[0], bayer_pixel[bayer_line_step2], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
01230           else
01231             rgb_buffer[rgb_line_step + 1] = WAVG4 (bayer_pixel[0], bayer_pixel[bayer_line_step2], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1], dh, dv);
01232 
01233           // Bayer         -1 0 1 2
01234           //         -1     g b g b
01235           //          0     r g r g
01236           // line_step      g b G b
01237           // line_step2     r g r g
01238           rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
01239           rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01240           rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
01241         }
01242 
01243         // last two pixels of the line
01244         // last two pixel values for first two lines
01245         // GRGR line
01246         // Bayer        -1 0 1
01247         //           0   r G r
01248         //   line_step   g b g
01249         // line_step2    r g r
01250         rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
01251         rgb_buffer[1] = bayer_pixel[0];
01252         rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
01253 
01254         // Bayer        -1 0 1
01255         //          0    r g R
01256         //  line_step    g b g
01257         // line_step2    r g r
01258         rgb_buffer[3] = bayer_pixel[1];
01259         rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
01260         //rgb_pixel[5] = bayer_pixel[line_step];
01261 
01262         // BGBG line
01263         // Bayer        -1 0 1
01264         //          0    r g r
01265         //  line_step    g B g
01266         // line_step2    r g r
01267         rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
01268         rgb_buffer[rgb_line_step + 1] = AVG4 (bayer_pixel[0], bayer_pixel[bayer_line_step2], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
01269         //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
01270 
01271         // Bayer         -1 0 1
01272         //         0      r g r
01273         // line_step      g b G
01274         // line_step2     r g r
01275         rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
01276         rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01277         //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
01278 
01279         bayer_pixel += bayer_line_step + 2;
01280         rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
01281       }
01282 
01283       //last two lines
01284       // Bayer         0 1 2
01285       //        -1     b g b
01286       //         0     G r g
01287       // line_step     b g b
01288 
01289       rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
01290       rgb_buffer[1] = bayer_pixel[0]; // green pixel
01291       rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
01292 
01293       // Bayer         0 1 2
01294       //        -1     b g b
01295       //         0     g R g
01296       // line_step     b g b
01297       //rgb_pixel[3] = bayer_pixel[1];
01298       rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
01299       rgb_buffer[5] = AVG4 (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2], bayer_pixel[-bayer_line_step], bayer_pixel[2 - bayer_line_step]);
01300 
01301       // BGBG line
01302       // Bayer         0 1 2
01303       //        -1     b g b
01304       //         0     g r g
01305       // line_step     B g b
01306       //rgb_pixel[rgb_line_step    ] = bayer_pixel[1];
01307       rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
01308       rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
01309 
01310       // Bayer         0 1 2
01311       //        -1     b g b
01312       //         0     g r g
01313       // line_step     b G b
01314       //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
01315       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01316       rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
01317 
01318       rgb_buffer += 6;
01319       bayer_pixel += 2;
01320       // rest of the last two lines
01321       for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
01322       {
01323         // GRGR line
01324         // Bayer       -1 0 1 2
01325         //        -1    g b g b
01326         //         0    r G r g
01327         // line_step    g b g b
01328         rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
01329         rgb_buffer[1] = bayer_pixel[0];
01330         rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
01331 
01332         // Bayer       -1 0 1 2
01333         //        -1    g b g b
01334         //         0    r g R g
01335         // line_step    g b g b
01336         rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
01337         rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
01338         rgb_buffer[5] = AVG4 (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2], bayer_pixel[-bayer_line_step], bayer_pixel[-bayer_line_step + 2]);
01339 
01340         // BGBG line
01341         // Bayer       -1 0 1 2
01342         //        -1    g b g b
01343         //         0    r g r g
01344         // line_step    g B g b
01345         rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[-1], bayer_pixel[1]);
01346         rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
01347         rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
01348 
01349 
01350         // Bayer       -1 0 1 2
01351         //        -1    g b g b
01352         //         0    r g r g
01353         // line_step    g b G b
01354         //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
01355         rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01356         rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
01357       }
01358 
01359       // last two pixel values for first two lines
01360       // GRGR line
01361       // Bayer       -1 0 1
01362       //        -1    g b g
01363       //         0    r G r
01364       // line_step    g b g
01365       rgb_buffer[rgb_line_step ] = rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
01366       rgb_buffer[1] = bayer_pixel[0];
01367       rgb_buffer[5] = rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
01368 
01369       // Bayer       -1 0 1
01370       //        -1    g b g
01371       //         0    r g R
01372       // line_step    g b g
01373       rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
01374       rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[-bayer_line_step + 1]);
01375       //rgb_pixel[5] = AVG( bayer_pixel[line_step], bayer_pixel[-line_step] );
01376 
01377       // BGBG line
01378       // Bayer       -1 0 1
01379       //        -1    g b g
01380       //         0    r g r
01381       // line_step    g B g
01382       //rgb_pixel[rgb_line_step    ] = AVG2( bayer_pixel[-1], bayer_pixel[1] );
01383       rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
01384       rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
01385 
01386       // Bayer       -1 0 1
01387       //        -1    g b g
01388       //         0    r g r
01389       // line_step    g b G
01390       //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
01391       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01392       //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
01393     }
01394     else
01395       THROW_OPENNI_EXCEPTION ("Unknwon debayering method: %d", (int)debayering_method_);
01396   }
01397   else
01398   {
01399     if (image_md_->XRes () % width != 0 || image_md_->YRes () % height != 0)
01400       THROW_OPENNI_EXCEPTION ("Downsampling only possible for integer scales in both dimensions. Request was %d x %d -> %d x %d.", image_md_->XRes (), image_md_->YRes (), width, height);
01401 
01402     // get each or each 2nd pixel group to find rgb values!
01403     register unsigned bayerXStep = image_md_->XRes () / width;
01404     register unsigned bayerYSkip = (image_md_->YRes () / height - 1) * image_md_->XRes ();
01405 
01406     // Downsampling and debayering at once
01407     register const XnUInt8* bayer_buffer = image_md_->WritableData ();
01408 
01409     for (register unsigned yIdx = 0; yIdx < height; ++yIdx, bayer_buffer += bayerYSkip, rgb_buffer += rgb_line_skip) // skip a line
01410     {
01411       for (register unsigned xIdx = 0; xIdx < width; ++xIdx, rgb_buffer += 3, bayer_buffer += bayerXStep)
01412       {
01413         rgb_buffer[ 2 ] = bayer_buffer[ image_md_->XRes () ];
01414         rgb_buffer[ 1 ] = AVG (bayer_buffer[0], bayer_buffer[ image_md_->XRes () + 1]);
01415         rgb_buffer[ 0 ] = bayer_buffer[ 1 ];
01416       }
01417     }
01418   }
01419 }
01420 } //namespace


openni_camera
Author(s): Patrick Mihelich, Suat Gedikli, Radu Bogdan Rusu
autogenerated on Mon Oct 6 2014 03:06:43