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


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:16:02