debayer.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2012-, Open Perception, Inc.
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 the copyright holder(s) 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/io/debayer.h>
00038 
00039 #define AVG(a,b) static_cast<unsigned char>((int(a) + int(b)) >> 1)
00040 #define AVG3(a,b,c) static_cast<unsigned char>((int(a) + int(b) + int(c)) / 3)
00041 #define AVG4(a,b,c,d) static_cast<unsigned char>((int(a) + int(b) + int(c) + int(d)) >> 2)
00042 #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 ) )
00043 
00045 void
00046 pcl::io::DeBayer::debayerBilinear (
00047     const unsigned char *bayer_pixel, unsigned char *rgb_buffer,
00048     unsigned width, unsigned height, 
00049     int bayer_line_step,
00050     int bayer_line_step2,
00051     unsigned rgb_line_step) const
00052 {
00053   if (bayer_line_step == 0)
00054     bayer_line_step = width;
00055   if (bayer_line_step2 == 0)
00056     bayer_line_step2 = width << 1;
00057   if (rgb_line_step == 0)
00058     rgb_line_step = width * 3;
00059 
00060   // padding skip for destination image
00061   unsigned rgb_line_skip = rgb_line_step - width * 3;
00062   register unsigned yIdx, xIdx;
00063   // first two pixel values for first two lines
00064   // Bayer         0 1 2
00065   //         0     G r g
00066   // line_step     b g b
00067   // line_step2    g r g
00068 
00069   rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00070   rgb_buffer[1] = bayer_pixel[0]; // green pixel
00071   rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
00072 
00073   // Bayer         0 1 2
00074   //         0     g R g
00075   // line_step     b g b
00076   // line_step2    g r g
00077   //rgb_pixel[3] = bayer_pixel[1];
00078   rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00079   rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00080 
00081   // BGBG line
00082   // Bayer         0 1 2
00083   //         0     g r g
00084   // line_step     B g b
00085   // line_step2    g r g
00086   rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00087   rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00088   //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00089 
00090   // pixel (1, 1)  0 1 2
00091   //         0     g r g
00092   // line_step     b G b
00093   // line_step2    g r g
00094   //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00095   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00096   //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
00097 
00098   rgb_buffer += 6;
00099   bayer_pixel += 2;
00100   // rest of the first two lines
00101   for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00102   {
00103     // GRGR line
00104     // Bayer        -1 0 1 2
00105     //           0   r G r g
00106     //   line_step   g b g b
00107     // line_step2    r g r g
00108     rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00109     rgb_buffer[1] = bayer_pixel[0];
00110     rgb_buffer[2] = bayer_pixel[bayer_line_step + 1];
00111 
00112     // Bayer        -1 0 1 2
00113     //          0    r g R g
00114     //  line_step    g b g b
00115     // line_step2    r g r g
00116     rgb_buffer[3] = bayer_pixel[1];
00117     rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00118     rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00119 
00120     // BGBG line
00121     // Bayer         -1 0 1 2
00122     //         0      r g r g
00123     // line_step      g B g b
00124     // line_step2     r g r g
00125     rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00126     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]);
00127     rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00128 
00129     // Bayer         -1 0 1 2
00130     //         0      r g r g
00131     // line_step      g b G b
00132     // line_step2     r g r g
00133     rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00134     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00135     //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
00136   }
00137 
00138   // last two pixel values for first two lines
00139   // GRGR line
00140   // Bayer        -1 0 1
00141   //           0   r G r
00142   //   line_step   g b g
00143   // line_step2    r g r
00144   rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00145   rgb_buffer[1] = bayer_pixel[0];
00146   rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00147 
00148   // Bayer        -1 0 1
00149   //          0    r g R
00150   //  line_step    g b g
00151   // line_step2    r g r
00152   rgb_buffer[3] = bayer_pixel[1];
00153   rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00154   //rgb_pixel[5] = bayer_pixel[line_step];
00155 
00156   // BGBG line
00157   // Bayer        -1 0 1
00158   //          0    r g r
00159   //  line_step    g B g
00160   // line_step2    r g r
00161   rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00162   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]);
00163   //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00164 
00165   // Bayer         -1 0 1
00166   //         0      r g r
00167   // line_step      g b G
00168   // line_step2     r g r
00169   rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00170   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00171   //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00172 
00173   bayer_pixel += bayer_line_step + 2;
00174   rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
00175 
00176   // main processing
00177 
00178   for (yIdx = 2; yIdx < height - 2; yIdx += 2)
00179   {
00180     // first two pixel values
00181     // Bayer         0 1 2
00182     //        -1     b g b
00183     //         0     G r g
00184     // line_step     b g b
00185     // line_step2    g r g
00186 
00187     rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00188     rgb_buffer[1] = bayer_pixel[0]; // green pixel
00189     rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]); // blue;
00190 
00191     // Bayer         0 1 2
00192     //        -1     b g b
00193     //         0     g R g
00194     // line_step     b g b
00195     // line_step2    g r g
00196     //rgb_pixel[3] = bayer_pixel[1];
00197     rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00198     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]);
00199 
00200     // BGBG line
00201     // Bayer         0 1 2
00202     //         0     g r g
00203     // line_step     B g b
00204     // line_step2    g r g
00205     rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00206     rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00207     rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00208 
00209     // pixel (1, 1)  0 1 2
00210     //         0     g r g
00211     // line_step     b G b
00212     // line_step2    g r g
00213     //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00214     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00215     rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00216 
00217     rgb_buffer += 6;
00218     bayer_pixel += 2;
00219     // continue with rest of the line
00220     for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00221     {
00222       // GRGR line
00223       // Bayer        -1 0 1 2
00224       //          -1   g b g b
00225       //           0   r G r g
00226       //   line_step   g b g b
00227       // line_step2    r g r g
00228       rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00229       rgb_buffer[1] = bayer_pixel[0];
00230       rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00231 
00232       // Bayer        -1 0 1 2
00233       //          -1   g b g b
00234       //          0    r g R g
00235       //  line_step    g b g b
00236       // line_step2    r g r g
00237       rgb_buffer[3] = bayer_pixel[1];
00238       rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00239       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]);
00240 
00241       // BGBG line
00242       // Bayer         -1 0 1 2
00243       //         -1     g b g b
00244       //          0     r g r g
00245       // line_step      g B g b
00246       // line_step2     r g r g
00247       rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00248       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]);
00249       rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00250 
00251       // Bayer         -1 0 1 2
00252       //         -1     g b g b
00253       //          0     r g r g
00254       // line_step      g b G b
00255       // line_step2     r g r g
00256       rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00257       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00258       rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00259     }
00260 
00261     // last two pixels of the line
00262     // last two pixel values for first two lines
00263     // GRGR line
00264     // Bayer        -1 0 1
00265     //           0   r G r
00266     //   line_step   g b g
00267     // line_step2    r g r
00268     rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00269     rgb_buffer[1] = bayer_pixel[0];
00270     rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00271 
00272     // Bayer        -1 0 1
00273     //          0    r g R
00274     //  line_step    g b g
00275     // line_step2    r g r
00276     rgb_buffer[3] = bayer_pixel[1];
00277     rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00278     //rgb_pixel[5] = bayer_pixel[line_step];
00279 
00280     // BGBG line
00281     // Bayer        -1 0 1
00282     //          0    r g r
00283     //  line_step    g B g
00284     // line_step2    r g r
00285     rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00286     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]);
00287     //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00288 
00289     // Bayer         -1 0 1
00290     //         0      r g r
00291     // line_step      g b G
00292     // line_step2     r g r
00293     rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00294     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00295     //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00296 
00297     bayer_pixel += bayer_line_step + 2;
00298     rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
00299   }
00300 
00301   //last two lines
00302   // Bayer         0 1 2
00303   //        -1     b g b
00304   //         0     G r g
00305   // line_step     b g b
00306 
00307   rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00308   rgb_buffer[1] = bayer_pixel[0]; // green pixel
00309   rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
00310 
00311   // Bayer         0 1 2
00312   //        -1     b g b
00313   //         0     g R g
00314   // line_step     b g b
00315   //rgb_pixel[3] = bayer_pixel[1];
00316   rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00317   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]);
00318 
00319   // BGBG line
00320   // Bayer         0 1 2
00321   //        -1     b g b
00322   //         0     g r g
00323   // line_step     B g b
00324   //rgb_pixel[rgb_line_step    ] = bayer_pixel[1];
00325   rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00326   rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00327 
00328   // Bayer         0 1 2
00329   //        -1     b g b
00330   //         0     g r g
00331   // line_step     b G b
00332   //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00333   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00334   rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00335 
00336   rgb_buffer += 6;
00337   bayer_pixel += 2;
00338   // rest of the last two lines
00339   for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00340   {
00341     // GRGR line
00342     // Bayer       -1 0 1 2
00343     //        -1    g b g b
00344     //         0    r G r g
00345     // line_step    g b g b
00346     rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00347     rgb_buffer[1] = bayer_pixel[0];
00348     rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00349 
00350     // Bayer       -1 0 1 2
00351     //        -1    g b g b
00352     //         0    r g R g
00353     // line_step    g b g b
00354     rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
00355     rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00356     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]);
00357 
00358     // BGBG line
00359     // Bayer       -1 0 1 2
00360     //        -1    g b g b
00361     //         0    r g r g
00362     // line_step    g B g b
00363     rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[-1], bayer_pixel[1]);
00364     rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00365     rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00366 
00367 
00368     // Bayer       -1 0 1 2
00369     //        -1    g b g b
00370     //         0    r g r g
00371     // line_step    g b G b
00372     //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
00373     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00374     rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00375   }
00376 
00377   // last two pixel values for first two lines
00378   // GRGR line
00379   // Bayer       -1 0 1
00380   //        -1    g b g
00381   //         0    r G r
00382   // line_step    g b g
00383   rgb_buffer[rgb_line_step ] = rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00384   rgb_buffer[1] = bayer_pixel[0];
00385   rgb_buffer[5] = rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00386 
00387   // Bayer       -1 0 1
00388   //        -1    g b g
00389   //         0    r g R
00390   // line_step    g b g
00391   rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
00392   rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[-bayer_line_step + 1]);
00393   //rgb_pixel[5] = AVG( bayer_pixel[line_step], bayer_pixel[-line_step] );
00394 
00395   // BGBG line
00396   // Bayer       -1 0 1
00397   //        -1    g b g
00398   //         0    r g r
00399   // line_step    g B g
00400   //rgb_pixel[rgb_line_step    ] = AVG2( bayer_pixel[-1], bayer_pixel[1] );
00401   rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00402   rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00403 
00404   // Bayer       -1 0 1
00405   //        -1    g b g
00406   //         0    r g r
00407   // line_step    g b G
00408   //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
00409   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00410   //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00411 }
00412 
00414 void
00415 pcl::io::DeBayer::debayerEdgeAware (
00416     const unsigned char *bayer_pixel, unsigned char *rgb_buffer,
00417     unsigned width, unsigned height, 
00418     int bayer_line_step,
00419     int bayer_line_step2,
00420     unsigned rgb_line_step) const
00421 {
00422   if (bayer_line_step == 0)
00423     bayer_line_step = width;
00424   if (bayer_line_step2 == 0)
00425     bayer_line_step2 = width << 1;
00426   if (rgb_line_step == 0)
00427     rgb_line_step = width * 3;
00428 
00429   // padding skip for destination image
00430   unsigned rgb_line_skip = rgb_line_step - width * 3;
00431   register unsigned yIdx, xIdx;
00432   int dh, dv;
00433 
00434   // first two pixel values for first two lines
00435   // Bayer         0 1 2
00436   //         0     G r g
00437   // line_step     b g b
00438   // line_step2    g r g
00439 
00440   rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00441   rgb_buffer[1] = bayer_pixel[0]; // green pixel
00442   rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
00443 
00444   // Bayer         0 1 2
00445   //         0     g R g
00446   // line_step     b g b
00447   // line_step2    g r g
00448   //rgb_pixel[3] = bayer_pixel[1];
00449   rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00450   rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00451 
00452   // BGBG line
00453   // Bayer         0 1 2
00454   //         0     g r g
00455   // line_step     B g b
00456   // line_step2    g r g
00457   rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00458   rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00459   //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00460 
00461   // pixel (1, 1)  0 1 2
00462   //         0     g r g
00463   // line_step     b G b
00464   // line_step2    g r g
00465   //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00466   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00467   //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
00468 
00469   rgb_buffer += 6;
00470   bayer_pixel += 2;
00471   // rest of the first two lines
00472   for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00473   {
00474     // GRGR line
00475     // Bayer        -1 0 1 2
00476     //           0   r G r g
00477     //   line_step   g b g b
00478     // line_step2    r g r g
00479     rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00480     rgb_buffer[1] = bayer_pixel[0];
00481     rgb_buffer[2] = bayer_pixel[bayer_line_step + 1];
00482 
00483     // Bayer        -1 0 1 2
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] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00489     rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00490 
00491     // BGBG line
00492     // Bayer         -1 0 1 2
00493     //         0      r g r g
00494     // line_step      g B g b
00495     // line_step2     r g r g
00496     rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00497     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]);
00498     rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00499 
00500     // Bayer         -1 0 1 2
00501     //         0      r g r g
00502     // line_step      g b G b
00503     // line_step2     r g r g
00504     rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00505     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00506     //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
00507   }
00508 
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   // main processing
00547   for (yIdx = 2; yIdx < height - 2; yIdx += 2)
00548   {
00549     // first two pixel values
00550     // Bayer         0 1 2
00551     //        -1     b g b
00552     //         0     G r g
00553     // line_step     b g b
00554     // line_step2    g r g
00555 
00556     rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00557     rgb_buffer[1] = bayer_pixel[0]; // green pixel
00558     rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]); // blue;
00559 
00560     // Bayer         0 1 2
00561     //        -1     b g b
00562     //         0     g R g
00563     // line_step     b g b
00564     // line_step2    g r g
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     //         0     g r g
00572     // line_step     B g b
00573     // line_step2    g r g
00574     rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00575     rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00576     rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00577 
00578     // pixel (1, 1)  0 1 2
00579     //         0     g r g
00580     // line_step     b G b
00581     // line_step2    g r g
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     // continue with rest of the line
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       // line_step2    r g r g
00597       rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00598       rgb_buffer[1] = bayer_pixel[0];
00599       rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00600 
00601       // Bayer        -1 0 1 2
00602       //          -1   g b g b
00603       //          0    r g R g
00604       //  line_step    g b g b
00605       // line_step2    r g r g
00606 
00607       dh = abs (bayer_pixel[0] - bayer_pixel[2]);
00608       dv = abs (bayer_pixel[-bayer_line_step + 1] - bayer_pixel[bayer_line_step + 1]);
00609 
00610       if (dh > dv)
00611         rgb_buffer[4] = AVG (bayer_pixel[-bayer_line_step + 1], bayer_pixel[bayer_line_step + 1]);
00612       else if (dv > dh)
00613         rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[2]);
00614       else
00615         rgb_buffer[4] = AVG4 (bayer_pixel[-bayer_line_step + 1], bayer_pixel[bayer_line_step + 1], bayer_pixel[0], bayer_pixel[2]);
00616 
00617       rgb_buffer[3] = bayer_pixel[1];
00618       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]);
00619 
00620       // BGBG line
00621       // Bayer         -1 0 1 2
00622       //         -1     g b g b
00623       //          0     r g r g
00624       // line_step      g B g b
00625       // line_step2     r g r g
00626       rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00627       rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00628 
00629       dv = abs (bayer_pixel[0] - bayer_pixel[bayer_line_step2]);
00630       dh = abs (bayer_pixel[bayer_line_step - 1] - bayer_pixel[bayer_line_step + 1]);
00631 
00632       if (dv > dh)
00633         rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00634       else if (dh > dv)
00635         rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step2]);
00636       else
00637         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]);
00638 
00639       // Bayer         -1 0 1 2
00640       //         -1     g b g b
00641       //          0     r g r g
00642       // line_step      g b G b
00643       // line_step2     r g r g
00644       rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00645       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00646       rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00647     }
00648 
00649     // last two pixels of the line
00650     // last two pixel values for first two lines
00651     // GRGR line
00652     // Bayer        -1 0 1
00653     //           0   r G r
00654     //   line_step   g b g
00655     // line_step2    r g r
00656     rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00657     rgb_buffer[1] = bayer_pixel[0];
00658     rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00659 
00660     // Bayer        -1 0 1
00661     //          0    r g R
00662     //  line_step    g b g
00663     // line_step2    r g r
00664     rgb_buffer[3] = bayer_pixel[1];
00665     rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00666     //rgb_pixel[5] = bayer_pixel[line_step];
00667 
00668     // BGBG line
00669     // Bayer        -1 0 1
00670     //          0    r g r
00671     //  line_step    g B g
00672     // line_step2    r g r
00673     rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00674     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]);
00675     //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00676 
00677     // Bayer         -1 0 1
00678     //         0      r g r
00679     // line_step      g b G
00680     // line_step2     r g r
00681     rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00682     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00683     //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00684 
00685     bayer_pixel += bayer_line_step + 2;
00686     rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
00687   }
00688 
00689   //last two lines
00690   // Bayer         0 1 2
00691   //        -1     b g b
00692   //         0     G r g
00693   // line_step     b g b
00694 
00695   rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00696   rgb_buffer[1] = bayer_pixel[0]; // green pixel
00697   rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
00698 
00699   // Bayer         0 1 2
00700   //        -1     b g b
00701   //         0     g R g
00702   // line_step     b g b
00703   //rgb_pixel[3] = bayer_pixel[1];
00704   rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00705   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]);
00706 
00707   // BGBG line
00708   // Bayer         0 1 2
00709   //        -1     b g b
00710   //         0     g r g
00711   // line_step     B g b
00712   //rgb_pixel[rgb_line_step    ] = bayer_pixel[1];
00713   rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00714   rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00715 
00716   // Bayer         0 1 2
00717   //        -1     b g b
00718   //         0     g r g
00719   // line_step     b G b
00720   //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00721   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00722   rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00723 
00724   rgb_buffer += 6;
00725   bayer_pixel += 2;
00726   // rest of the last two lines
00727   for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00728   {
00729     // GRGR line
00730     // Bayer       -1 0 1 2
00731     //        -1    g b g b
00732     //         0    r G r g
00733     // line_step    g b g b
00734     rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00735     rgb_buffer[1] = bayer_pixel[0];
00736     rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00737 
00738     // Bayer       -1 0 1 2
00739     //        -1    g b g b
00740     //         0    r g R g
00741     // line_step    g b g b
00742     rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
00743     rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00744     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]);
00745 
00746     // BGBG line
00747     // Bayer       -1 0 1 2
00748     //        -1    g b g b
00749     //         0    r g r g
00750     // line_step    g B g b
00751     rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[-1], bayer_pixel[1]);
00752     rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00753     rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00754 
00755 
00756     // Bayer       -1 0 1 2
00757     //        -1    g b g b
00758     //         0    r g r g
00759     // line_step    g b G b
00760     //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
00761     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00762     rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00763   }
00764 
00765   // last two pixel values for first two lines
00766   // GRGR line
00767   // Bayer       -1 0 1
00768   //        -1    g b g
00769   //         0    r G r
00770   // line_step    g b g
00771   rgb_buffer[rgb_line_step ] = rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00772   rgb_buffer[1] = bayer_pixel[0];
00773   rgb_buffer[5] = rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00774 
00775   // Bayer       -1 0 1
00776   //        -1    g b g
00777   //         0    r g R
00778   // line_step    g b g
00779   rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
00780   rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[-bayer_line_step + 1]);
00781   //rgb_pixel[5] = AVG( bayer_pixel[line_step], bayer_pixel[-line_step] );
00782 
00783   // BGBG line
00784   // Bayer       -1 0 1
00785   //        -1    g b g
00786   //         0    r g r
00787   // line_step    g B g
00788   //rgb_pixel[rgb_line_step    ] = AVG2( bayer_pixel[-1], bayer_pixel[1] );
00789   rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00790   rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00791 
00792   // Bayer       -1 0 1
00793   //        -1    g b g
00794   //         0    r g r
00795   // line_step    g b G
00796   //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
00797   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00798   //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00799 }
00800 
00802 void
00803 pcl::io::DeBayer::debayerEdgeAwareWeighted (
00804     const unsigned char *bayer_pixel, unsigned char *rgb_buffer,
00805     unsigned width, unsigned height, 
00806     int bayer_line_step,
00807     int bayer_line_step2,
00808     unsigned rgb_line_step) const
00809 {
00810   if (bayer_line_step == 0)
00811     bayer_line_step = width;
00812   if (bayer_line_step2 == 0)
00813     bayer_line_step2 = width << 1;
00814   if (rgb_line_step == 0)
00815     rgb_line_step = width * 3;
00816 
00817   // padding skip for destination image
00818   unsigned rgb_line_skip = rgb_line_step - width * 3;
00819   register unsigned yIdx, xIdx;
00820   int dh, dv;
00821 
00822   // first two pixel values for first two lines
00823   // Bayer         0 1 2
00824   //         0     G r g
00825   // line_step     b g b
00826   // line_step2    g r g
00827 
00828   rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00829   rgb_buffer[1] = bayer_pixel[0]; // green pixel
00830   rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
00831 
00832   // Bayer         0 1 2
00833   //         0     g R g
00834   // line_step     b g b
00835   // line_step2    g r g
00836   //rgb_pixel[3] = bayer_pixel[1];
00837   rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00838   rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00839 
00840   // BGBG line
00841   // Bayer         0 1 2
00842   //         0     g r g
00843   // line_step     B g b
00844   // line_step2    g r g
00845   rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00846   rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00847   //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00848 
00849   // pixel (1, 1)  0 1 2
00850   //         0     g r g
00851   // line_step     b G b
00852   // line_step2    g r g
00853   //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00854   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00855   //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
00856 
00857   rgb_buffer += 6;
00858   bayer_pixel += 2;
00859   // rest of the first two lines
00860   for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00861   {
00862     // GRGR line
00863     // Bayer        -1 0 1 2
00864     //           0   r G r g
00865     //   line_step   g b g b
00866     // line_step2    r g r g
00867     rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00868     rgb_buffer[1] = bayer_pixel[0];
00869     rgb_buffer[2] = bayer_pixel[bayer_line_step + 1];
00870 
00871     // Bayer        -1 0 1 2
00872     //          0    r g R g
00873     //  line_step    g b g b
00874     // line_step2    r g r g
00875     rgb_buffer[3] = bayer_pixel[1];
00876     rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00877     rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00878 
00879     // BGBG line
00880     // Bayer         -1 0 1 2
00881     //         0      r g r g
00882     // line_step      g B g b
00883     // line_step2     r g r g
00884     rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00885     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]);
00886     rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00887 
00888     // Bayer         -1 0 1 2
00889     //         0      r g r g
00890     // line_step      g b G b
00891     // line_step2     r g r g
00892     rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00893     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00894     //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
00895   }
00896 
00897   // last two pixel values for first two lines
00898   // GRGR line
00899   // Bayer        -1 0 1
00900   //           0   r G r
00901   //   line_step   g b g
00902   // line_step2    r g r
00903   rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00904   rgb_buffer[1] = bayer_pixel[0];
00905   rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00906 
00907   // Bayer        -1 0 1
00908   //          0    r g R
00909   //  line_step    g b g
00910   // line_step2    r g r
00911   rgb_buffer[3] = bayer_pixel[1];
00912   rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00913   //rgb_pixel[5] = bayer_pixel[line_step];
00914 
00915   // BGBG line
00916   // Bayer        -1 0 1
00917   //          0    r g r
00918   //  line_step    g B g
00919   // line_step2    r g r
00920   rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00921   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]);
00922   //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00923 
00924   // Bayer         -1 0 1
00925   //         0      r g r
00926   // line_step      g b G
00927   // line_step2     r g r
00928   rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00929   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00930   //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00931 
00932   bayer_pixel += bayer_line_step + 2;
00933   rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
00934   // main processing
00935   for (yIdx = 2; yIdx < height - 2; yIdx += 2)
00936   {
00937     // first two pixel values
00938     // Bayer         0 1 2
00939     //        -1     b g b
00940     //         0     G r g
00941     // line_step     b g b
00942     // line_step2    g r g
00943 
00944     rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00945     rgb_buffer[1] = bayer_pixel[0]; // green pixel
00946     rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]); // blue;
00947 
00948     // Bayer         0 1 2
00949     //        -1     b g b
00950     //         0     g R g
00951     // line_step     b g b
00952     // line_step2    g r g
00953     //rgb_pixel[3] = bayer_pixel[1];
00954     rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00955     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]);
00956 
00957     // BGBG line
00958     // Bayer         0 1 2
00959     //         0     g r g
00960     // line_step     B g b
00961     // line_step2    g r g
00962     rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00963     rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00964     rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00965 
00966     // pixel (1, 1)  0 1 2
00967     //         0     g r g
00968     // line_step     b G b
00969     // line_step2    g r g
00970     //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00971     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00972     rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00973 
00974     rgb_buffer += 6;
00975     bayer_pixel += 2;
00976     // continue with rest of the line
00977     for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00978     {
00979       // GRGR line
00980       // Bayer        -1 0 1 2
00981       //          -1   g b g b
00982       //           0   r G r g
00983       //   line_step   g b g b
00984       // line_step2    r g r g
00985       rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00986       rgb_buffer[1] = bayer_pixel[0];
00987       rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00988 
00989       // Bayer        -1 0 1 2
00990       //          -1   g b g b
00991       //          0    r g R g
00992       //  line_step    g b g b
00993       // line_step2    r g r g
00994 
00995       dh = abs (bayer_pixel[0] - bayer_pixel[2]);
00996       dv = abs (bayer_pixel[-bayer_line_step + 1] - bayer_pixel[bayer_line_step + 1]);
00997 
00998       if (dv == 0 && dh == 0)
00999         rgb_buffer[4] = AVG4 (bayer_pixel[1 - bayer_line_step], bayer_pixel[1 + bayer_line_step], bayer_pixel[0], bayer_pixel[2]);
01000       else
01001         rgb_buffer[4] = WAVG4 (bayer_pixel[1 - bayer_line_step], bayer_pixel[1 + bayer_line_step], bayer_pixel[0], bayer_pixel[2], dh, dv);
01002       rgb_buffer[3] = bayer_pixel[1];
01003       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]);
01004 
01005       // BGBG line
01006       // Bayer         -1 0 1 2
01007       //         -1     g b g b
01008       //          0     r g r g
01009       // line_step      g B g b
01010       // line_step2     r g r g
01011       rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
01012       rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
01013 
01014       dv = abs (bayer_pixel[0] - bayer_pixel[bayer_line_step2]);
01015       dh = abs (bayer_pixel[bayer_line_step - 1] - bayer_pixel[bayer_line_step + 1]);
01016 
01017       if (dv == 0 && dh == 0)
01018         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]);
01019       else
01020         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);
01021 
01022       // Bayer         -1 0 1 2
01023       //         -1     g b g b
01024       //          0     r g r g
01025       // line_step      g b G b
01026       // line_step2     r g r g
01027       rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
01028       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01029       rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
01030     }
01031 
01032     // last two pixels of the line
01033     // last two pixel values for first two lines
01034     // GRGR line
01035     // Bayer        -1 0 1
01036     //           0   r G r
01037     //   line_step   g b g
01038     // line_step2    r g r
01039     rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
01040     rgb_buffer[1] = bayer_pixel[0];
01041     rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
01042 
01043     // Bayer        -1 0 1
01044     //          0    r g R
01045     //  line_step    g b g
01046     // line_step2    r g r
01047     rgb_buffer[3] = bayer_pixel[1];
01048     rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
01049     //rgb_pixel[5] = bayer_pixel[line_step];
01050 
01051     // BGBG line
01052     // Bayer        -1 0 1
01053     //          0    r g r
01054     //  line_step    g B g
01055     // line_step2    r g r
01056     rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
01057     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]);
01058     //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
01059 
01060     // Bayer         -1 0 1
01061     //         0      r g r
01062     // line_step      g b G
01063     // line_step2     r g r
01064     rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
01065     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01066     //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
01067 
01068     bayer_pixel += bayer_line_step + 2;
01069     rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
01070   }
01071 
01072   //last two lines
01073   // Bayer         0 1 2
01074   //        -1     b g b
01075   //         0     G r g
01076   // line_step     b g b
01077 
01078   rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
01079   rgb_buffer[1] = bayer_pixel[0]; // green pixel
01080   rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
01081 
01082   // Bayer         0 1 2
01083   //        -1     b g b
01084   //         0     g R g
01085   // line_step     b g b
01086   //rgb_pixel[3] = bayer_pixel[1];
01087   rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
01088   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]);
01089 
01090   // BGBG line
01091   // Bayer         0 1 2
01092   //        -1     b g b
01093   //         0     g r g
01094   // line_step     B g b
01095   //rgb_pixel[rgb_line_step    ] = bayer_pixel[1];
01096   rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
01097   rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
01098 
01099   // Bayer         0 1 2
01100   //        -1     b g b
01101   //         0     g r g
01102   // line_step     b G b
01103   //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
01104   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01105   rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
01106 
01107   rgb_buffer += 6;
01108   bayer_pixel += 2;
01109   // rest of the last two lines
01110   for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
01111   {
01112     // GRGR line
01113     // Bayer       -1 0 1 2
01114     //        -1    g b g b
01115     //         0    r G r g
01116     // line_step    g b g b
01117     rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
01118     rgb_buffer[1] = bayer_pixel[0];
01119     rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
01120 
01121     // Bayer       -1 0 1 2
01122     //        -1    g b g b
01123     //         0    r g R g
01124     // line_step    g b g b
01125     rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
01126     rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
01127     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]);
01128 
01129     // BGBG line
01130     // Bayer       -1 0 1 2
01131     //        -1    g b g b
01132     //         0    r g r g
01133     // line_step    g B g b
01134     rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[-1], bayer_pixel[1]);
01135     rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
01136     rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
01137 
01138 
01139     // Bayer       -1 0 1 2
01140     //        -1    g b g b
01141     //         0    r g r g
01142     // line_step    g b G b
01143     //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
01144     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01145     rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
01146   }
01147 
01148   // last two pixel values for first two lines
01149   // GRGR line
01150   // Bayer       -1 0 1
01151   //        -1    g b g
01152   //         0    r G r
01153   // line_step    g b g
01154   rgb_buffer[rgb_line_step ] = rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
01155   rgb_buffer[1] = bayer_pixel[0];
01156   rgb_buffer[5] = rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
01157 
01158   // Bayer       -1 0 1
01159   //        -1    g b g
01160   //         0    r g R
01161   // line_step    g b g
01162   rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
01163   rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[-bayer_line_step + 1]);
01164   //rgb_pixel[5] = AVG( bayer_pixel[line_step], bayer_pixel[-line_step] );
01165 
01166   // BGBG line
01167   // Bayer       -1 0 1
01168   //        -1    g b g
01169   //         0    r g r
01170   // line_step    g B g
01171   //rgb_pixel[rgb_line_step    ] = AVG2( bayer_pixel[-1], bayer_pixel[1] );
01172   rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
01173   rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
01174 
01175   // Bayer       -1 0 1
01176   //        -1    g b g
01177   //         0    r g r
01178   // line_step    g b G
01179   //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
01180   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01181   //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
01182 }
01183 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:23:21