edge_aware.cpp
Go to the documentation of this file.
00001 #include "edge_aware.h"
00002 
00003 #define AVG(a,b) (((int)(a) + (int)(b)) >> 1)
00004 #define AVG3(a,b,c) (((int)(a) + (int)(b) + (int)(c)) / 3)
00005 #define AVG4(a,b,c,d) (((int)(a) + (int)(b) + (int)(c) + (int)(d)) >> 2)
00006 #define WAVG4(a,b,c,d,x,y)  ( ( ((int)(a) + (int)(b)) * (int)(x) + ((int)(c) + (int)(d)) * (int)(y) ) / ( 2 * ((int)(x) + (int(y))) ) )
00007 using namespace std;
00008 
00009 namespace image_proc {
00010 
00011 void debayerEdgeAware(const cv::Mat& bayer, cv::Mat& color)
00012 {
00013   unsigned width = bayer.cols;
00014   unsigned height = bayer.rows;
00015   unsigned rgb_line_step = color.step[0];
00016   unsigned rgb_line_skip = rgb_line_step - width * 3;
00017   int bayer_line_step = bayer.step[0];
00018   int bayer_line_step2 = bayer_line_step * 2;
00019 
00020   unsigned char* rgb_buffer = color.datastart;
00021   unsigned char* bayer_pixel = bayer.datastart;
00022   unsigned yIdx, xIdx;
00023 
00024   int dh, dv;
00025 
00026   // first two pixel values for first two lines
00027   // Bayer         0 1 2
00028   //         0     G r g
00029   // line_step     b g b
00030   // line_step2    g r g
00031 
00032   rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00033   rgb_buffer[1] = bayer_pixel[0]; // green pixel
00034   rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
00035 
00036   // Bayer         0 1 2
00037   //         0     g R g
00038   // line_step     b g b
00039   // line_step2    g r g
00040   //rgb_pixel[3] = bayer_pixel[1];
00041   rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00042   rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00043 
00044   // BGBG line
00045   // Bayer         0 1 2
00046   //         0     g r g
00047   // line_step     B g b
00048   // line_step2    g r g
00049   rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00050   rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00051   //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00052 
00053   // pixel (1, 1)  0 1 2
00054   //         0     g r g
00055   // line_step     b G b
00056   // line_step2    g r g
00057   //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00058   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00059   //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
00060 
00061   rgb_buffer += 6;
00062   bayer_pixel += 2;
00063   // rest of the first two lines
00064   for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00065   {
00066     // GRGR line
00067     // Bayer        -1 0 1 2
00068     //           0   r G r g
00069     //   line_step   g b g b
00070     // line_step2    r g r g
00071     rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00072     rgb_buffer[1] = bayer_pixel[0];
00073     rgb_buffer[2] = bayer_pixel[bayer_line_step + 1];
00074 
00075     // Bayer        -1 0 1 2
00076     //          0    r g R g
00077     //  line_step    g b g b
00078     // line_step2    r g r g
00079     rgb_buffer[3] = bayer_pixel[1];
00080     rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00081     rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00082 
00083     // BGBG line
00084     // Bayer         -1 0 1 2
00085     //         0      r g r g
00086     // line_step      g B g b
00087     // line_step2     r g r g
00088     rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00089     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]);
00090     rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00091 
00092     // Bayer         -1 0 1 2
00093     //         0      r g r g
00094     // line_step      g b G b
00095     // line_step2     r g r g
00096     rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00097     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00098     //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
00099   }
00100 
00101   // last two pixel values for first two lines
00102   // GRGR line
00103   // Bayer        -1 0 1
00104   //           0   r G r
00105   //   line_step   g b g
00106   // line_step2    r g r
00107   rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00108   rgb_buffer[1] = bayer_pixel[0];
00109   rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00110 
00111   // Bayer        -1 0 1
00112   //          0    r g R
00113   //  line_step    g b g
00114   // line_step2    r g r
00115   rgb_buffer[3] = bayer_pixel[1];
00116   rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00117   //rgb_pixel[5] = bayer_pixel[line_step];
00118 
00119   // BGBG line
00120   // Bayer        -1 0 1
00121   //          0    r g r
00122   //  line_step    g B g
00123   // line_step2    r g r
00124   rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00125   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]);
00126   //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00127 
00128   // Bayer         -1 0 1
00129   //         0      r g r
00130   // line_step      g b G
00131   // line_step2     r g r
00132   rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00133   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00134   //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00135 
00136   bayer_pixel += bayer_line_step + 2;
00137   rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
00138   // main processing
00139   for (yIdx = 2; yIdx < height - 2; yIdx += 2)
00140   {
00141     // first two pixel values
00142     // Bayer         0 1 2
00143     //        -1     b g b
00144     //         0     G r g
00145     // line_step     b g b
00146     // line_step2    g r g
00147     
00148     rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00149     rgb_buffer[1] = bayer_pixel[0]; // green pixel
00150     rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]); // blue;
00151     
00152     // Bayer         0 1 2
00153     //        -1     b g b
00154     //         0     g R g
00155     // line_step     b g b
00156     // line_step2    g r g
00157     //rgb_pixel[3] = bayer_pixel[1];
00158     rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00159     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]);
00160     
00161     // BGBG line
00162     // Bayer         0 1 2
00163     //         0     g r g
00164     // line_step     B g b
00165     // line_step2    g r g
00166     rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00167     rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00168     rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00169     
00170     // pixel (1, 1)  0 1 2
00171     //         0     g r g
00172     // line_step     b G b
00173     // line_step2    g r g
00174     //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00175     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00176     rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00177     
00178     rgb_buffer += 6;
00179     bayer_pixel += 2;
00180     // continue with rest of the line
00181     for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00182     {
00183       // GRGR line
00184       // Bayer        -1 0 1 2
00185       //          -1   g b g b
00186       //           0   r G r g
00187       //   line_step   g b g b
00188       // line_step2    r g r g
00189       rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00190       rgb_buffer[1] = bayer_pixel[0];
00191       rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00192       
00193       // Bayer        -1 0 1 2
00194       //          -1   g b g b
00195       //          0    r g R g
00196       //  line_step    g b g b
00197       // line_step2    r g r g
00198       
00199       dh = abs (bayer_pixel[0] - bayer_pixel[2]);
00200       dv = abs (bayer_pixel[-bayer_line_step + 1] - bayer_pixel[bayer_line_step + 1]);
00201       
00202       if (dh > dv)
00203         rgb_buffer[4] = AVG (bayer_pixel[-bayer_line_step + 1], bayer_pixel[bayer_line_step + 1]);
00204       else if (dv > dh)
00205         rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[2]);
00206       else
00207         rgb_buffer[4] = AVG4 (bayer_pixel[-bayer_line_step + 1], bayer_pixel[bayer_line_step + 1], bayer_pixel[0], bayer_pixel[2]);
00208       
00209       rgb_buffer[3] = bayer_pixel[1];
00210       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]);
00211       
00212       // BGBG line
00213       // Bayer         -1 0 1 2
00214       //         -1     g b g b
00215       //          0     r g r g
00216       // line_step      g B g b
00217       // line_step2     r g r g
00218       rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00219       rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00220       
00221       dv = abs (bayer_pixel[0] - bayer_pixel[bayer_line_step2]);
00222       dh = abs (bayer_pixel[bayer_line_step - 1] - bayer_pixel[bayer_line_step + 1]);
00223       
00224       if (dv > dh)
00225         rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00226       else if (dh > dv)
00227         rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step2]);
00228       else
00229         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]);
00230       
00231       // Bayer         -1 0 1 2
00232       //         -1     g b g b
00233       //          0     r g r g
00234       // line_step      g b G b
00235       // line_step2     r g r g
00236       rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00237       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00238       rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00239     }
00240     
00241     // last two pixels of the line
00242     // last two pixel values for first two lines
00243     // GRGR line
00244     // Bayer        -1 0 1
00245     //           0   r G r
00246     //   line_step   g b g
00247     // line_step2    r g r
00248     rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00249     rgb_buffer[1] = bayer_pixel[0];
00250     rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00251     
00252     // Bayer        -1 0 1
00253     //          0    r g R
00254     //  line_step    g b g
00255     // line_step2    r g r
00256     rgb_buffer[3] = bayer_pixel[1];
00257     rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00258     //rgb_pixel[5] = bayer_pixel[line_step];
00259     
00260     // BGBG line
00261     // Bayer        -1 0 1
00262     //          0    r g r
00263     //  line_step    g B g
00264     // line_step2    r g r
00265     rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00266     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]);
00267     //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00268     
00269     // Bayer         -1 0 1
00270     //         0      r g r
00271     // line_step      g b G
00272     // line_step2     r g r
00273     rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00274     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00275     //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00276     
00277     bayer_pixel += bayer_line_step + 2;
00278     rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
00279   }
00280   
00281   //last two lines
00282   // Bayer         0 1 2
00283   //        -1     b g b
00284   //         0     G r g
00285   // line_step     b g b
00286   
00287   rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00288   rgb_buffer[1] = bayer_pixel[0]; // green pixel
00289   rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
00290   
00291   // Bayer         0 1 2
00292   //        -1     b g b
00293   //         0     g R g
00294   // line_step     b g b
00295   //rgb_pixel[3] = bayer_pixel[1];
00296   rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00297   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]);
00298   
00299   // BGBG line
00300   // Bayer         0 1 2
00301   //        -1     b g b
00302   //         0     g r g
00303   // line_step     B g b
00304   //rgb_pixel[rgb_line_step    ] = bayer_pixel[1];
00305   rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00306   rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00307   
00308   // Bayer         0 1 2
00309   //        -1     b g b
00310   //         0     g r g
00311   // line_step     b G b
00312   //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00313   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00314   rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00315   
00316   rgb_buffer += 6;
00317   bayer_pixel += 2;
00318   // rest of the last two lines
00319   for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00320   {
00321     // GRGR line
00322     // Bayer       -1 0 1 2
00323     //        -1    g b g b
00324     //         0    r G r g
00325     // line_step    g b g b
00326     rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00327     rgb_buffer[1] = bayer_pixel[0];
00328     rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00329     
00330     // Bayer       -1 0 1 2
00331     //        -1    g b g b
00332     //         0    r g R g
00333     // line_step    g b g b
00334     rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
00335     rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00336     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]);
00337     
00338     // BGBG line
00339     // Bayer       -1 0 1 2
00340     //        -1    g b g b
00341     //         0    r g r g
00342     // line_step    g B g b
00343     rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[-1], bayer_pixel[1]);
00344     rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00345     rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00346     
00347     
00348     // Bayer       -1 0 1 2
00349     //        -1    g b g b
00350     //         0    r g r g
00351     // line_step    g b G b
00352     //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
00353     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00354     rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00355   }
00356   
00357   // last two pixel values for first two lines
00358   // GRGR line
00359   // Bayer       -1 0 1
00360   //        -1    g b g
00361   //         0    r G r
00362   // line_step    g b g
00363   rgb_buffer[rgb_line_step ] = rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00364   rgb_buffer[1] = bayer_pixel[0];
00365   rgb_buffer[5] = rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00366   
00367   // Bayer       -1 0 1
00368   //        -1    g b g
00369   //         0    r g R
00370   // line_step    g b g
00371   rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
00372   rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[-bayer_line_step + 1]);
00373   //rgb_pixel[5] = AVG( bayer_pixel[line_step], bayer_pixel[-line_step] );
00374   
00375   // BGBG line
00376   // Bayer       -1 0 1
00377   //        -1    g b g
00378   //         0    r g r
00379   // line_step    g B g
00380   //rgb_pixel[rgb_line_step    ] = AVG2( bayer_pixel[-1], bayer_pixel[1] );
00381   rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00382   rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00383   
00384   // Bayer       -1 0 1
00385   //        -1    g b g
00386   //         0    r g r
00387   // line_step    g b G
00388   //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
00389   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00390   //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00391 }
00392 
00393 void debayerEdgeAwareWeighted(const cv::Mat& bayer, cv::Mat& color)
00394 {
00395   unsigned width = bayer.cols;
00396   unsigned height = bayer.rows;
00397   unsigned rgb_line_step = color.step[0];
00398   unsigned rgb_line_skip = rgb_line_step - width * 3;
00399   int bayer_line_step = bayer.step[0];
00400   int bayer_line_step2 = bayer_line_step * 2;
00401 
00402   unsigned char* rgb_buffer = color.datastart;
00403   unsigned char* bayer_pixel = bayer.datastart;
00404   unsigned yIdx, xIdx;
00405 
00406   int dh, dv;
00407 
00408   // first two pixel values for first two lines
00409   // Bayer         0 1 2
00410   //         0     G r g
00411   // line_step     b g b
00412   // line_step2    g r g
00413 
00414   rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00415   rgb_buffer[1] = bayer_pixel[0]; // green pixel
00416   rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
00417   
00418   // Bayer         0 1 2
00419   //         0     g R g
00420   // line_step     b g b
00421   // line_step2    g r g
00422   //rgb_pixel[3] = bayer_pixel[1];
00423   rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00424   rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00425   
00426   // BGBG line
00427   // Bayer         0 1 2
00428   //         0     g r g
00429   // line_step     B g b
00430   // line_step2    g r g
00431   rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00432   rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00433   //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00434   
00435   // pixel (1, 1)  0 1 2
00436   //         0     g r g
00437   // line_step     b G b
00438   // line_step2    g r g
00439   //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00440   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00441   //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
00442   
00443   rgb_buffer += 6;
00444   bayer_pixel += 2;
00445   // rest of the first two lines
00446   for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00447   {
00448     // GRGR line
00449     // Bayer        -1 0 1 2
00450     //           0   r G r g
00451     //   line_step   g b g b
00452     // line_step2    r g r g
00453     rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00454     rgb_buffer[1] = bayer_pixel[0];
00455     rgb_buffer[2] = bayer_pixel[bayer_line_step + 1];
00456     
00457     // Bayer        -1 0 1 2
00458     //          0    r g R g
00459     //  line_step    g b g b
00460     // line_step2    r g r g
00461     rgb_buffer[3] = bayer_pixel[1];
00462     rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00463     rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00464     
00465     // BGBG line
00466     // Bayer         -1 0 1 2
00467     //         0      r g r g
00468     // line_step      g B g b
00469     // line_step2     r g r g
00470     rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00471     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]);
00472     rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00473     
00474     // Bayer         -1 0 1 2
00475     //         0      r g r g
00476     // line_step      g b G b
00477     // line_step2     r g r g
00478     rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00479     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00480     //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
00481   }
00482   
00483   // last two pixel values for first two lines
00484   // GRGR line
00485   // Bayer        -1 0 1
00486   //           0   r G r
00487   //   line_step   g b g
00488   // line_step2    r g r
00489   rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00490   rgb_buffer[1] = bayer_pixel[0];
00491   rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00492   
00493   // Bayer        -1 0 1
00494   //          0    r g R
00495   //  line_step    g b g
00496   // line_step2    r g r
00497   rgb_buffer[3] = bayer_pixel[1];
00498   rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00499   //rgb_pixel[5] = bayer_pixel[line_step];
00500   
00501   // BGBG line
00502   // Bayer        -1 0 1
00503   //          0    r g r
00504   //  line_step    g B g
00505   // line_step2    r g r
00506   rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00507   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]);
00508   //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00509   
00510   // Bayer         -1 0 1
00511   //         0      r g r
00512   // line_step      g b G
00513   // line_step2     r g r
00514   rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00515   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00516   //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00517   
00518   bayer_pixel += bayer_line_step + 2;
00519   rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
00520   // main processing
00521   for (yIdx = 2; yIdx < height - 2; yIdx += 2)
00522   {
00523     // first two pixel values
00524     // Bayer         0 1 2
00525     //        -1     b g b
00526     //         0     G r g
00527     // line_step     b g b
00528     // line_step2    g r g
00529     
00530     rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00531     rgb_buffer[1] = bayer_pixel[0]; // green pixel
00532     rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]); // blue;
00533     
00534     // Bayer         0 1 2
00535     //        -1     b g b
00536     //         0     g R g
00537     // line_step     b g b
00538     // line_step2    g r g
00539     //rgb_pixel[3] = bayer_pixel[1];
00540     rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00541     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]);
00542     
00543     // BGBG line
00544     // Bayer         0 1 2
00545     //         0     g r g
00546     // line_step     B g b
00547     // line_step2    g r g
00548     rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00549     rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00550     rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00551     
00552     // pixel (1, 1)  0 1 2
00553     //         0     g r g
00554     // line_step     b G b
00555     // line_step2    g r g
00556     //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00557     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00558     rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00559     
00560     rgb_buffer += 6;
00561     bayer_pixel += 2;
00562     // continue with rest of the line
00563     for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00564     {
00565       // GRGR line
00566       // Bayer        -1 0 1 2
00567       //          -1   g b g b
00568       //           0   r G r g
00569       //   line_step   g b g b
00570       // line_step2    r g r g
00571       rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00572       rgb_buffer[1] = bayer_pixel[0];
00573       rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00574       
00575       // Bayer        -1 0 1 2
00576       //          -1   g b g b
00577       //          0    r g R g
00578       //  line_step    g b g b
00579       // line_step2    r g r g
00580       
00581       dh = abs (bayer_pixel[0] - bayer_pixel[2]);
00582       dv = abs (bayer_pixel[-bayer_line_step + 1] - bayer_pixel[bayer_line_step + 1]);
00583       
00584       if (dv == 0 && dh == 0)
00585         rgb_buffer[4] = AVG4 (bayer_pixel[1 - bayer_line_step], bayer_pixel[1 + bayer_line_step], bayer_pixel[0], bayer_pixel[2]);
00586       else
00587         rgb_buffer[4] = WAVG4 (bayer_pixel[1 - bayer_line_step], bayer_pixel[1 + bayer_line_step], bayer_pixel[0], bayer_pixel[2], dh, dv);
00588       rgb_buffer[3] = bayer_pixel[1];
00589       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]);
00590       
00591       // BGBG 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[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00598       rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00599       
00600       dv = abs (bayer_pixel[0] - bayer_pixel[bayer_line_step2]);
00601       dh = abs (bayer_pixel[bayer_line_step - 1] - bayer_pixel[bayer_line_step + 1]);
00602       
00603       if (dv == 0 && dh == 0)
00604         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]);
00605       else
00606         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);
00607       
00608       // Bayer         -1 0 1 2
00609       //         -1     g b g b
00610       //          0     r g r g
00611       // line_step      g b G b
00612       // line_step2     r g r g
00613       rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00614       rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00615       rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00616     }
00617     
00618     // last two pixels of the line
00619     // last two pixel values for first two lines
00620     // GRGR line
00621     // Bayer        -1 0 1
00622     //           0   r G r
00623     //   line_step   g b g
00624     // line_step2    r g r
00625     rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00626     rgb_buffer[1] = bayer_pixel[0];
00627     rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00628     
00629     // Bayer        -1 0 1
00630     //          0    r g R
00631     //  line_step    g b g
00632     // line_step2    r g r
00633     rgb_buffer[3] = bayer_pixel[1];
00634     rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00635     //rgb_pixel[5] = bayer_pixel[line_step];
00636     
00637     // BGBG line
00638     // Bayer        -1 0 1
00639     //          0    r g r
00640     //  line_step    g B g
00641     // line_step2    r g r
00642     rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00643     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]);
00644     //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
00645     
00646     // Bayer         -1 0 1
00647     //         0      r g r
00648     // line_step      g b G
00649     // line_step2     r g r
00650     rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00651     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00652     //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00653     
00654     bayer_pixel += bayer_line_step + 2;
00655     rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
00656   }
00657   
00658   //last two lines
00659   // Bayer         0 1 2
00660   //        -1     b g b
00661   //         0     G r g
00662   // line_step     b g b
00663   
00664   rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
00665   rgb_buffer[1] = bayer_pixel[0]; // green pixel
00666   rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
00667   
00668   // Bayer         0 1 2
00669   //        -1     b g b
00670   //         0     g R g
00671   // line_step     b g b
00672   //rgb_pixel[3] = bayer_pixel[1];
00673   rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00674   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]);
00675   
00676   // BGBG line
00677   // Bayer         0 1 2
00678   //        -1     b g b
00679   //         0     g r g
00680   // line_step     B g b
00681   //rgb_pixel[rgb_line_step    ] = bayer_pixel[1];
00682   rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00683   rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00684 
00685   // Bayer         0 1 2
00686   //        -1     b g b
00687   //         0     g r g
00688   // line_step     b G b
00689   //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
00690   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00691   rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00692   
00693   rgb_buffer += 6;
00694   bayer_pixel += 2;
00695   // rest of the last two lines
00696   for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00697   {
00698     // GRGR line
00699     // Bayer       -1 0 1 2
00700     //        -1    g b g b
00701     //         0    r G r g
00702     // line_step    g b g b
00703     rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00704     rgb_buffer[1] = bayer_pixel[0];
00705     rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00706     
00707     // Bayer       -1 0 1 2
00708     //        -1    g b g b
00709     //         0    r g R g
00710     // line_step    g b g b
00711     rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
00712     rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00713     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]);
00714 
00715     // BGBG line
00716     // Bayer       -1 0 1 2
00717     //        -1    g b g b
00718     //         0    r g r g
00719     // line_step    g B g b
00720     rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[-1], bayer_pixel[1]);
00721     rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00722     rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00723     
00724     
00725     // Bayer       -1 0 1 2
00726     //        -1    g b g b
00727     //         0    r g r g
00728     // line_step    g b G b
00729     //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
00730     rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00731     rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00732   }
00733   
00734   // last two pixel values for first two lines
00735   // GRGR line
00736   // Bayer       -1 0 1
00737   //        -1    g b g
00738   //         0    r G r
00739   // line_step    g b g
00740   rgb_buffer[rgb_line_step ] = rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00741   rgb_buffer[1] = bayer_pixel[0];
00742   rgb_buffer[5] = rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00743   
00744   // Bayer       -1 0 1
00745   //        -1    g b g
00746   //         0    r g R
00747   // line_step    g b g
00748   rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
00749   rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[-bayer_line_step + 1]);
00750   //rgb_pixel[5] = AVG( bayer_pixel[line_step], bayer_pixel[-line_step] );
00751   
00752   // BGBG line
00753   // Bayer       -1 0 1
00754   //        -1    g b g
00755   //         0    r g r
00756   // line_step    g B g
00757   //rgb_pixel[rgb_line_step    ] = AVG2( bayer_pixel[-1], bayer_pixel[1] );
00758   rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00759   rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00760   
00761   // Bayer       -1 0 1
00762   //        -1    g b g
00763   //         0    r g r
00764   // line_step    g b G
00765   //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
00766   rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00767   //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
00768 
00769 }
00770 
00771 } // namespace image_proc


image_proc
Author(s): Patrick Mihelich, Kurt Konolige, Jeremy Leibs
autogenerated on Fri Jan 3 2014 11:24:35