openni_image_bayer_grbg.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011 2011 Willow Garage, Inc.
5  * Suat Gedikli <gedikli@willowgarage.com>
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
38 #include <sstream>
39 #include <iostream>
40 
41 #define AVG(a,b) (((int)(a) + (int)(b)) >> 1)
42 #define AVG3(a,b,c) (((int)(a) + (int)(b) + (int)(c)) / 3)
43 #define AVG4(a,b,c,d) (((int)(a) + (int)(b) + (int)(c) + (int)(d)) >> 2)
44 #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))) ) )
45 using namespace std;
46 
47 namespace openni_wrapper
48 {
49 
50 ImageBayerGRBG::ImageBayerGRBG (boost::shared_ptr<xn::ImageMetaData> image_meta_data, DebayeringMethod method) throw ()
51 : Image (image_meta_data)
52 , debayering_method_ (method)
53 {
54 }
55 
56 ImageBayerGRBG::~ImageBayerGRBG () throw ()
57 {
58 }
59 
60 bool ImageBayerGRBG::isResizingSupported (unsigned input_width, unsigned input_height, unsigned output_width, unsigned output_height) const
61 {
62  return ImageBayerGRBG::resizingSupported (input_width, input_height, output_width, output_height);
63 }
64 
65 void ImageBayerGRBG::fillGrayscale (unsigned width, unsigned height, unsigned char* gray_buffer, unsigned gray_line_step) const throw (OpenNIException)
66 {
67  if (width > image_md_->XRes () || height > image_md_->YRes ())
68  THROW_OPENNI_EXCEPTION ("Upsampling not supported. Request was: %d x %d -> %d x %d", image_md_->XRes (), image_md_->YRes (), width, height);
69 
70  if (gray_line_step == 0)
71  gray_line_step = width;
72 
73  // padding skip for destination image
74  unsigned gray_line_skip = gray_line_step - width;
75  if (image_md_->XRes () == width && image_md_->YRes () == height)
76  { // if no downsampling
77  const XnUInt8 *bayer_pixel = image_md_->WritableData ();
78  int line_skip = image_md_->XRes ();
79  if (debayering_method_ == Bilinear)
80  {
81  // first line GRGRGR
82  for (register unsigned xIdx = 0; xIdx < width - 2; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
83  {
84  gray_buffer[0] = bayer_pixel[0]; // green pixel
85  gray_buffer[1] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[1 + line_skip]); // interpolated green pixel
86  }
87  gray_buffer[0] = bayer_pixel[0];
88  gray_buffer[1] = AVG (bayer_pixel[0], bayer_pixel[1 + line_skip]);
89  gray_buffer += 2 + gray_line_skip;
90  bayer_pixel += 2;
91 
92  for (register unsigned yIdx = 1; yIdx < height - 1; yIdx += 2)
93  {
94  // blue line
95  gray_buffer[0] = AVG3 (bayer_pixel[-line_skip], bayer_pixel[line_skip], bayer_pixel[1]);
96  gray_buffer[1] = bayer_pixel[1];
97  gray_buffer += 2;
98  bayer_pixel += 2;
99  for (register unsigned xIdx = 2; xIdx < width; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
100  {
101  gray_buffer[0] = AVG4 (bayer_pixel[-line_skip], bayer_pixel[line_skip], bayer_pixel[-1], bayer_pixel[1]);
102  gray_buffer[1] = bayer_pixel[1];
103  }
104  gray_buffer += gray_line_skip;
105 
106  // red line
107  for (register unsigned xIdx = 0; xIdx < width - 2; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
108  {
109  gray_buffer[0] = bayer_pixel[0]; // green pixel
110  gray_buffer[1] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[-line_skip + 1], bayer_pixel[line_skip + 1]); // interpolated green pixel
111  }
112  gray_buffer[0] = bayer_pixel[0];
113  gray_buffer[1] = AVG3 (bayer_pixel[-line_skip + 1], bayer_pixel[line_skip + 1], bayer_pixel[-1]);
114  gray_buffer += 2 + gray_line_skip;
115  bayer_pixel += 2;
116  }
117 
118  // last line BGBGBG
119  gray_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-line_skip]);
120  gray_buffer[1] = bayer_pixel[1];
121  gray_buffer += 2;
122  bayer_pixel += 2;
123  for (register unsigned xIdx = 2; xIdx < width; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
124  {
125  gray_buffer[0] = AVG3 (bayer_pixel[-1], bayer_pixel[1], bayer_pixel[-line_skip]);
126  gray_buffer[1] = bayer_pixel[1];
127  }
128  }
129  else if (debayering_method_ == EdgeAware)
130  {
131  int dv, dh;
132  // first line GRGRGR
133  for (register unsigned xIdx = 0; xIdx < width - 2; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
134  {
135  gray_buffer[0] = bayer_pixel[0]; // green pixel
136  gray_buffer[1] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[1 + line_skip]); // interpolated green pixel
137  }
138  gray_buffer[0] = bayer_pixel[0];
139  gray_buffer[1] = AVG (bayer_pixel[0], bayer_pixel[1 + line_skip]);
140  gray_buffer += 2 + gray_line_skip;
141  bayer_pixel += 2;
142 
143  for (register unsigned yIdx = 1; yIdx < height - 1; yIdx += 2)
144  {
145  // blue line
146  gray_buffer[0] = AVG3 (bayer_pixel[-line_skip], bayer_pixel[line_skip], bayer_pixel[1]);
147  gray_buffer[1] = bayer_pixel[1];
148  gray_buffer += 2;
149  bayer_pixel += 2;
150  for (register unsigned xIdx = 2; xIdx < width; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
151  {
152  dv = abs (bayer_pixel[-line_skip] - bayer_pixel[line_skip]);
153  dh = abs (bayer_pixel[-1] - bayer_pixel[1]);
154  if (dh > dv)
155  gray_buffer[0] = AVG (bayer_pixel[-line_skip], bayer_pixel[line_skip]);
156  else if (dv > dh)
157  gray_buffer[0] = AVG (bayer_pixel[-1], bayer_pixel[1]);
158  else
159  gray_buffer[0] = AVG4 (bayer_pixel[-line_skip], bayer_pixel[line_skip], bayer_pixel[-1], bayer_pixel[1]);
160 
161  gray_buffer[1] = bayer_pixel[1];
162  }
163  gray_buffer += gray_line_skip;
164 
165  // red line
166  for (register unsigned xIdx = 0; xIdx < width - 2; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
167  {
168  gray_buffer[0] = bayer_pixel[0];
169 
170  dv = abs (bayer_pixel[1 - line_skip] - bayer_pixel[1 + line_skip]);
171  dh = abs (bayer_pixel[0] - bayer_pixel[2]);
172  if (dh > dv)
173  gray_buffer[1] = AVG (bayer_pixel[1 - line_skip], bayer_pixel[1 + line_skip]);
174  else if (dv > dh)
175  gray_buffer[1] = AVG (bayer_pixel[0], bayer_pixel[2]);
176  else
177  gray_buffer[1] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[-line_skip + 1], bayer_pixel[line_skip + 1]);
178  }
179  gray_buffer[0] = bayer_pixel[0];
180  gray_buffer[1] = AVG3 (bayer_pixel[-line_skip + 1], bayer_pixel[line_skip + 1], bayer_pixel[-1]);
181  gray_buffer += 2 + gray_line_skip;
182  bayer_pixel += 2;
183  }
184 
185  // last line BGBGBG
186  gray_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-line_skip]);
187  gray_buffer[1] = bayer_pixel[1];
188  gray_buffer += 2;
189  bayer_pixel += 2;
190  for (register unsigned xIdx = 2; xIdx < width; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
191  {
192  gray_buffer[0] = AVG3 (bayer_pixel[-1], bayer_pixel[1], bayer_pixel[-line_skip]);
193  gray_buffer[1] = bayer_pixel[1];
194  }
195  }
196  else if (debayering_method_ == EdgeAwareWeighted)
197  {
198  int dv, dh;
199  // first line GRGRGR
200  for (register unsigned xIdx = 0; xIdx < width - 2; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
201  {
202  gray_buffer[0] = bayer_pixel[0]; // green pixel
203  gray_buffer[1] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[1 + line_skip]); // interpolated green pixel
204  }
205  gray_buffer[0] = bayer_pixel[0];
206  gray_buffer[1] = AVG (bayer_pixel[0], bayer_pixel[1 + line_skip]);
207  gray_buffer += 2 + gray_line_skip;
208  bayer_pixel += 2;
209 
210  for (register unsigned yIdx = 1; yIdx < height - 1; yIdx += 2)
211  {
212  // blue line
213  gray_buffer[0] = AVG3 (bayer_pixel[-line_skip], bayer_pixel[line_skip], bayer_pixel[1]);
214  gray_buffer[1] = bayer_pixel[1];
215  gray_buffer += 2;
216  bayer_pixel += 2;
217  for (register unsigned xIdx = 2; xIdx < width; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
218  {
219  dv = abs (bayer_pixel[-line_skip] - bayer_pixel[line_skip]);
220  dh = abs (bayer_pixel[-1] - bayer_pixel[1]);
221 
222  if (dv == 0 && dh == 0)
223  gray_buffer[0] = AVG4 (bayer_pixel[-line_skip], bayer_pixel[line_skip], bayer_pixel[-1], bayer_pixel[1]);
224  else
225  gray_buffer[0] = WAVG4 (bayer_pixel[-line_skip], bayer_pixel[line_skip], bayer_pixel[-1], bayer_pixel[1], dh, dv);
226 
227  gray_buffer[1] = bayer_pixel[1];
228  }
229 
230  gray_buffer += gray_line_skip;
231 
232  // red line
233  for (register unsigned xIdx = 0; xIdx < width - 2; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
234  {
235  gray_buffer[0] = bayer_pixel[0];
236 
237  dv = abs (bayer_pixel[1 - line_skip] - bayer_pixel[1 + line_skip]);
238  dh = abs (bayer_pixel[0] - bayer_pixel[2]);
239 
240  if (dv == 0 && dh == 0)
241  gray_buffer[1] = AVG4 (bayer_pixel[1 - line_skip], bayer_pixel[1 + line_skip], bayer_pixel[0], bayer_pixel[2]);
242  else
243  gray_buffer[1] = WAVG4 (bayer_pixel[1 - line_skip], bayer_pixel[1 + line_skip], bayer_pixel[0], bayer_pixel[2], dh, dv);
244  }
245  gray_buffer[0] = bayer_pixel[0];
246  gray_buffer[1] = AVG3 (bayer_pixel[-line_skip + 1], bayer_pixel[line_skip + 1], bayer_pixel[-1]);
247  gray_buffer += 2 + gray_line_skip;
248  bayer_pixel += 2;
249  }
250 
251  // last line BGBGBG
252  gray_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-line_skip]);
253  gray_buffer[1] = bayer_pixel[1];
254  gray_buffer += 2;
255  bayer_pixel += 2;
256  for (register unsigned xIdx = 2; xIdx < width; xIdx += 2, gray_buffer += 2, bayer_pixel += 2)
257  {
258  gray_buffer[0] = AVG3 (bayer_pixel[-1], bayer_pixel[1], bayer_pixel[-line_skip]);
259  gray_buffer[1] = bayer_pixel[1];
260  }
261  }
262  else
263  THROW_OPENNI_EXCEPTION ("Unknown Debayering method: %d", (int)debayering_method_);
264 
265  }
266  else // downsampling
267  {
268  if ((image_md_->XRes () >> 1) % width != 0 || (image_md_->YRes () >> 1) % height != 0)
269  THROW_OPENNI_EXCEPTION ("Downsampling only possible for multiple of 2 in both dimensions. Request was %d x %d -> %d x %d.", image_md_->XRes (), image_md_->YRes (), width, height);
270 
271  // fast method -> simply takes each or each 2nd pixel-group to get gray values out
272  register unsigned bayerXStep = image_md_->XRes () / width;
273  register unsigned bayerYSkip = (image_md_->YRes () / height - 1) * image_md_->XRes ();
274 
275  // Downsampling and debayering at once
276  register const XnUInt8* bayer_buffer = image_md_->WritableData ();
277 
278  for (register unsigned yIdx = 0; yIdx < height; ++yIdx, bayer_buffer += bayerYSkip, gray_buffer += gray_line_skip) // skip a line
279  {
280  for (register unsigned xIdx = 0; xIdx < width; ++xIdx, ++gray_buffer, bayer_buffer += bayerXStep)
281  {
282  *gray_buffer = AVG (bayer_buffer[0], bayer_buffer[ image_md_->XRes () + 1]);
283  }
284  }
285  } // downsampling
286 }
287 
288 void ImageBayerGRBG::fillRGB (unsigned width, unsigned height, unsigned char* rgb_buffer, unsigned rgb_line_step) const throw (OpenNIException)
289 {
290  if (width > image_md_->XRes () || height > image_md_->YRes ())
291  THROW_OPENNI_EXCEPTION ("Upsampling only possible for multiple of 2 in both dimensions. Request was %d x %d -> %d x %d.", image_md_->XRes (), image_md_->YRes (), width, height);
292 
293  if (rgb_line_step == 0)
294  rgb_line_step = width * 3;
295 
296  // padding skip for destination image
297  unsigned rgb_line_skip = rgb_line_step - width * 3;
298 
299  if (image_md_->XRes () == width && image_md_->YRes () == height)
300  {
301  register const XnUInt8 *bayer_pixel = image_md_->WritableData ();
302  register unsigned yIdx, xIdx;
303 
304  int bayer_line_step = image_md_->XRes ();
305  int bayer_line_step2 = image_md_->XRes () << 1;
306 
307  if (debayering_method_ == Bilinear)
308  {
309  // first two pixel values for first two lines
310  // Bayer 0 1 2
311  // 0 G r g
312  // line_step b g b
313  // line_step2 g r g
314 
315  rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
316  rgb_buffer[1] = bayer_pixel[0]; // green pixel
317  rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
318 
319  // Bayer 0 1 2
320  // 0 g R g
321  // line_step b g b
322  // line_step2 g r g
323  //rgb_pixel[3] = bayer_pixel[1];
324  rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
325  rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
326 
327  // BGBG line
328  // Bayer 0 1 2
329  // 0 g r g
330  // line_step B g b
331  // line_step2 g r g
332  rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
333  rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
334  //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
335 
336  // pixel (1, 1) 0 1 2
337  // 0 g r g
338  // line_step b G b
339  // line_step2 g r g
340  //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
341  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
342  //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
343 
344  rgb_buffer += 6;
345  bayer_pixel += 2;
346  // rest of the first two lines
347 
348  for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
349  {
350  // GRGR line
351  // Bayer -1 0 1 2
352  // 0 r G r g
353  // line_step g b g b
354  // line_step2 r g r g
355  rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
356  rgb_buffer[1] = bayer_pixel[0];
357  rgb_buffer[2] = bayer_pixel[bayer_line_step + 1];
358 
359  // Bayer -1 0 1 2
360  // 0 r g R g
361  // line_step g b g b
362  // line_step2 r g r g
363  rgb_buffer[3] = bayer_pixel[1];
364  rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
365  rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
366 
367  // BGBG line
368  // Bayer -1 0 1 2
369  // 0 r g r g
370  // line_step g B g b
371  // line_step2 r g r g
372  rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
373  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]);
374  rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
375 
376  // Bayer -1 0 1 2
377  // 0 r g r g
378  // line_step g b G b
379  // line_step2 r g r g
380  rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
381  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
382  //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
383  }
384 
385  // last two pixel values for first two lines
386  // GRGR line
387  // Bayer -1 0 1
388  // 0 r G r
389  // line_step g b g
390  // line_step2 r g r
391  rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
392  rgb_buffer[1] = bayer_pixel[0];
393  rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
394 
395  // Bayer -1 0 1
396  // 0 r g R
397  // line_step g b g
398  // line_step2 r g r
399  rgb_buffer[3] = bayer_pixel[1];
400  rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
401  //rgb_pixel[5] = bayer_pixel[line_step];
402 
403  // BGBG line
404  // Bayer -1 0 1
405  // 0 r g r
406  // line_step g B g
407  // line_step2 r g r
408  rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
409  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]);
410  //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
411 
412  // Bayer -1 0 1
413  // 0 r g r
414  // line_step g b G
415  // line_step2 r g r
416  rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
417  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
418  //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
419 
420  bayer_pixel += bayer_line_step + 2;
421  rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
422 
423  // main processing
424 
425  for (yIdx = 2; yIdx < height - 2; yIdx += 2)
426  {
427  // first two pixel values
428  // Bayer 0 1 2
429  // -1 b g b
430  // 0 G r g
431  // line_step b g b
432  // line_step2 g r g
433 
434  rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
435  rgb_buffer[1] = bayer_pixel[0]; // green pixel
436  rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]); // blue;
437 
438  // Bayer 0 1 2
439  // -1 b g b
440  // 0 g R g
441  // line_step b g b
442  // line_step2 g r g
443  //rgb_pixel[3] = bayer_pixel[1];
444  rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
445  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]);
446 
447  // BGBG line
448  // Bayer 0 1 2
449  // 0 g r g
450  // line_step B g b
451  // line_step2 g r g
452  rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
453  rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
454  rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
455 
456  // pixel (1, 1) 0 1 2
457  // 0 g r g
458  // line_step b G b
459  // line_step2 g r g
460  //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
461  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
462  rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
463 
464  rgb_buffer += 6;
465  bayer_pixel += 2;
466  // continue with rest of the line
467  for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
468  {
469  // GRGR line
470  // Bayer -1 0 1 2
471  // -1 g b g b
472  // 0 r G r g
473  // line_step g b g b
474  // line_step2 r g r g
475  rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
476  rgb_buffer[1] = bayer_pixel[0];
477  rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
478 
479  // Bayer -1 0 1 2
480  // -1 g b g b
481  // 0 r g R g
482  // line_step g b g b
483  // line_step2 r g r g
484  rgb_buffer[3] = bayer_pixel[1];
485  rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
486  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]);
487 
488  // BGBG line
489  // Bayer -1 0 1 2
490  // -1 g b g b
491  // 0 r g r g
492  // line_step g B g b
493  // line_step2 r g r g
494  rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
495  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]);
496  rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
497 
498  // Bayer -1 0 1 2
499  // -1 g b g b
500  // 0 r g r g
501  // line_step g b G b
502  // line_step2 r g r g
503  rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
504  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
505  rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
506  }
507 
508  // last two pixels of the line
509  // last two pixel values for first two lines
510  // GRGR line
511  // Bayer -1 0 1
512  // 0 r G r
513  // line_step g b g
514  // line_step2 r g r
515  rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
516  rgb_buffer[1] = bayer_pixel[0];
517  rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
518 
519  // Bayer -1 0 1
520  // 0 r g R
521  // line_step g b g
522  // line_step2 r g r
523  rgb_buffer[3] = bayer_pixel[1];
524  rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
525  //rgb_pixel[5] = bayer_pixel[line_step];
526 
527  // BGBG line
528  // Bayer -1 0 1
529  // 0 r g r
530  // line_step g B g
531  // line_step2 r g r
532  rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
533  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]);
534  //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
535 
536  // Bayer -1 0 1
537  // 0 r g r
538  // line_step g b G
539  // line_step2 r g r
540  rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
541  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
542  //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
543 
544  bayer_pixel += bayer_line_step + 2;
545  rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
546  }
547 
548  //last two lines
549  // Bayer 0 1 2
550  // -1 b g b
551  // 0 G r g
552  // line_step b g b
553 
554  rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
555  rgb_buffer[1] = bayer_pixel[0]; // green pixel
556  rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
557 
558  // Bayer 0 1 2
559  // -1 b g b
560  // 0 g R g
561  // line_step b g b
562  //rgb_pixel[3] = bayer_pixel[1];
563  rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
564  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]);
565 
566  // BGBG line
567  // Bayer 0 1 2
568  // -1 b g b
569  // 0 g r g
570  // line_step B g b
571  //rgb_pixel[rgb_line_step ] = bayer_pixel[1];
572  rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
573  rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
574 
575  // Bayer 0 1 2
576  // -1 b g b
577  // 0 g r g
578  // line_step b G b
579  //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
580  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
581  rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
582 
583  rgb_buffer += 6;
584  bayer_pixel += 2;
585  // rest of the last two lines
586  for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
587  {
588  // GRGR line
589  // Bayer -1 0 1 2
590  // -1 g b g b
591  // 0 r G r g
592  // line_step g b g b
593  rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
594  rgb_buffer[1] = bayer_pixel[0];
595  rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
596 
597  // Bayer -1 0 1 2
598  // -1 g b g b
599  // 0 r g R g
600  // line_step g b g b
601  rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
602  rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
603  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]);
604 
605  // BGBG line
606  // Bayer -1 0 1 2
607  // -1 g b g b
608  // 0 r g r g
609  // line_step g B g b
610  rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[-1], bayer_pixel[1]);
611  rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
612  rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
613 
614 
615  // Bayer -1 0 1 2
616  // -1 g b g b
617  // 0 r g r g
618  // line_step g b G b
619  //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
620  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
621  rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
622  }
623 
624  // last two pixel values for first two lines
625  // GRGR line
626  // Bayer -1 0 1
627  // -1 g b g
628  // 0 r G r
629  // line_step g b g
630  rgb_buffer[rgb_line_step ] = rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
631  rgb_buffer[1] = bayer_pixel[0];
632  rgb_buffer[5] = rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
633 
634  // Bayer -1 0 1
635  // -1 g b g
636  // 0 r g R
637  // line_step g b g
638  rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
639  rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[-bayer_line_step + 1]);
640  //rgb_pixel[5] = AVG( bayer_pixel[line_step], bayer_pixel[-line_step] );
641 
642  // BGBG line
643  // Bayer -1 0 1
644  // -1 g b g
645  // 0 r g r
646  // line_step g B g
647  //rgb_pixel[rgb_line_step ] = AVG2( bayer_pixel[-1], bayer_pixel[1] );
648  rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
649  rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
650 
651  // Bayer -1 0 1
652  // -1 g b g
653  // 0 r g r
654  // line_step g b G
655  //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
656  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
657  //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
658  }
659  else if (debayering_method_ == EdgeAware)
660  {
661  int dh, dv;
662 
663  // first two pixel values for first two lines
664  // Bayer 0 1 2
665  // 0 G r g
666  // line_step b g b
667  // line_step2 g r g
668 
669  rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
670  rgb_buffer[1] = bayer_pixel[0]; // green pixel
671  rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
672 
673  // Bayer 0 1 2
674  // 0 g R g
675  // line_step b g b
676  // line_step2 g r g
677  //rgb_pixel[3] = bayer_pixel[1];
678  rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
679  rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
680 
681  // BGBG line
682  // Bayer 0 1 2
683  // 0 g r g
684  // line_step B g b
685  // line_step2 g r g
686  rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
687  rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
688  //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
689 
690  // pixel (1, 1) 0 1 2
691  // 0 g r g
692  // line_step b G b
693  // line_step2 g r g
694  //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
695  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
696  //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
697 
698  rgb_buffer += 6;
699  bayer_pixel += 2;
700  // rest of the first two lines
701  for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
702  {
703  // GRGR line
704  // Bayer -1 0 1 2
705  // 0 r G r g
706  // line_step g b g b
707  // line_step2 r g r g
708  rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
709  rgb_buffer[1] = bayer_pixel[0];
710  rgb_buffer[2] = bayer_pixel[bayer_line_step + 1];
711 
712  // Bayer -1 0 1 2
713  // 0 r g R g
714  // line_step g b g b
715  // line_step2 r g r g
716  rgb_buffer[3] = bayer_pixel[1];
717  rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
718  rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
719 
720  // BGBG line
721  // Bayer -1 0 1 2
722  // 0 r g r g
723  // line_step g B g b
724  // line_step2 r g r g
725  rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
726  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]);
727  rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
728 
729  // Bayer -1 0 1 2
730  // 0 r g r g
731  // line_step g b G b
732  // line_step2 r g r g
733  rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
734  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
735  //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
736  }
737 
738  // last two pixel values for first two lines
739  // GRGR line
740  // Bayer -1 0 1
741  // 0 r G r
742  // line_step g b g
743  // line_step2 r g r
744  rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
745  rgb_buffer[1] = bayer_pixel[0];
746  rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
747 
748  // Bayer -1 0 1
749  // 0 r g R
750  // line_step g b g
751  // line_step2 r g r
752  rgb_buffer[3] = bayer_pixel[1];
753  rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
754  //rgb_pixel[5] = bayer_pixel[line_step];
755 
756  // BGBG line
757  // Bayer -1 0 1
758  // 0 r g r
759  // line_step g B g
760  // line_step2 r g r
761  rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
762  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]);
763  //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
764 
765  // Bayer -1 0 1
766  // 0 r g r
767  // line_step g b G
768  // line_step2 r g r
769  rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
770  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
771  //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
772 
773  bayer_pixel += bayer_line_step + 2;
774  rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
775  // main processing
776  for (yIdx = 2; yIdx < height - 2; yIdx += 2)
777  {
778  // first two pixel values
779  // Bayer 0 1 2
780  // -1 b g b
781  // 0 G r g
782  // line_step b g b
783  // line_step2 g r g
784 
785  rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
786  rgb_buffer[1] = bayer_pixel[0]; // green pixel
787  rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]); // blue;
788 
789  // Bayer 0 1 2
790  // -1 b g b
791  // 0 g R g
792  // line_step b g b
793  // line_step2 g r g
794  //rgb_pixel[3] = bayer_pixel[1];
795  rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
796  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]);
797 
798  // BGBG line
799  // Bayer 0 1 2
800  // 0 g r g
801  // line_step B g b
802  // line_step2 g r g
803  rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
804  rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
805  rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
806 
807  // pixel (1, 1) 0 1 2
808  // 0 g r g
809  // line_step b G b
810  // line_step2 g r g
811  //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
812  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
813  rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
814 
815  rgb_buffer += 6;
816  bayer_pixel += 2;
817  // continue with rest of the line
818  for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
819  {
820  // GRGR line
821  // Bayer -1 0 1 2
822  // -1 g b g b
823  // 0 r G r g
824  // line_step g b g b
825  // line_step2 r g r g
826  rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
827  rgb_buffer[1] = bayer_pixel[0];
828  rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
829 
830  // Bayer -1 0 1 2
831  // -1 g b g b
832  // 0 r g R g
833  // line_step g b g b
834  // line_step2 r g r g
835 
836  dh = abs (bayer_pixel[0] - bayer_pixel[2]);
837  dv = abs (bayer_pixel[-bayer_line_step + 1] - bayer_pixel[bayer_line_step + 1]);
838 
839  if (dh > dv)
840  rgb_buffer[4] = AVG (bayer_pixel[-bayer_line_step + 1], bayer_pixel[bayer_line_step + 1]);
841  else if (dv > dh)
842  rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[2]);
843  else
844  rgb_buffer[4] = AVG4 (bayer_pixel[-bayer_line_step + 1], bayer_pixel[bayer_line_step + 1], bayer_pixel[0], bayer_pixel[2]);
845 
846  rgb_buffer[3] = bayer_pixel[1];
847  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]);
848 
849  // BGBG line
850  // Bayer -1 0 1 2
851  // -1 g b g b
852  // 0 r g r g
853  // line_step g B g b
854  // line_step2 r g r g
855  rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
856  rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
857 
858  dv = abs (bayer_pixel[0] - bayer_pixel[bayer_line_step2]);
859  dh = abs (bayer_pixel[bayer_line_step - 1] - bayer_pixel[bayer_line_step + 1]);
860 
861  if (dv > dh)
862  rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
863  else if (dh > dv)
864  rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step2]);
865  else
866  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]);
867 
868  // Bayer -1 0 1 2
869  // -1 g b g b
870  // 0 r g r g
871  // line_step g b G b
872  // line_step2 r g r g
873  rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
874  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
875  rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
876  }
877 
878  // last two pixels of the line
879  // last two pixel values for first two lines
880  // GRGR line
881  // Bayer -1 0 1
882  // 0 r G r
883  // line_step g b g
884  // line_step2 r g r
885  rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
886  rgb_buffer[1] = bayer_pixel[0];
887  rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
888 
889  // Bayer -1 0 1
890  // 0 r g R
891  // line_step g b g
892  // line_step2 r g r
893  rgb_buffer[3] = bayer_pixel[1];
894  rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
895  //rgb_pixel[5] = bayer_pixel[line_step];
896 
897  // BGBG line
898  // Bayer -1 0 1
899  // 0 r g r
900  // line_step g B g
901  // line_step2 r g r
902  rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
903  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]);
904  //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
905 
906  // Bayer -1 0 1
907  // 0 r g r
908  // line_step g b G
909  // line_step2 r g r
910  rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
911  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
912  //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
913 
914  bayer_pixel += bayer_line_step + 2;
915  rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
916  }
917 
918  //last two lines
919  // Bayer 0 1 2
920  // -1 b g b
921  // 0 G r g
922  // line_step b g b
923 
924  rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
925  rgb_buffer[1] = bayer_pixel[0]; // green pixel
926  rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
927 
928  // Bayer 0 1 2
929  // -1 b g b
930  // 0 g R g
931  // line_step b g b
932  //rgb_pixel[3] = bayer_pixel[1];
933  rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
934  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]);
935 
936  // BGBG line
937  // Bayer 0 1 2
938  // -1 b g b
939  // 0 g r g
940  // line_step B g b
941  //rgb_pixel[rgb_line_step ] = bayer_pixel[1];
942  rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
943  rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
944 
945  // Bayer 0 1 2
946  // -1 b g b
947  // 0 g r g
948  // line_step b G b
949  //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
950  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
951  rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
952 
953  rgb_buffer += 6;
954  bayer_pixel += 2;
955  // rest of the last two lines
956  for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
957  {
958  // GRGR line
959  // Bayer -1 0 1 2
960  // -1 g b g b
961  // 0 r G r g
962  // line_step g b g b
963  rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
964  rgb_buffer[1] = bayer_pixel[0];
965  rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
966 
967  // Bayer -1 0 1 2
968  // -1 g b g b
969  // 0 r g R g
970  // line_step g b g b
971  rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
972  rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
973  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]);
974 
975  // BGBG line
976  // Bayer -1 0 1 2
977  // -1 g b g b
978  // 0 r g r g
979  // line_step g B g b
980  rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[-1], bayer_pixel[1]);
981  rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
982  rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
983 
984 
985  // Bayer -1 0 1 2
986  // -1 g b g b
987  // 0 r g r g
988  // line_step g b G b
989  //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
990  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
991  rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
992  }
993 
994  // last two pixel values for first two lines
995  // GRGR line
996  // Bayer -1 0 1
997  // -1 g b g
998  // 0 r G r
999  // line_step g b g
1000  rgb_buffer[rgb_line_step ] = rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
1001  rgb_buffer[1] = bayer_pixel[0];
1002  rgb_buffer[5] = rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
1003 
1004  // Bayer -1 0 1
1005  // -1 g b g
1006  // 0 r g R
1007  // line_step g b g
1008  rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
1009  rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[-bayer_line_step + 1]);
1010  //rgb_pixel[5] = AVG( bayer_pixel[line_step], bayer_pixel[-line_step] );
1011 
1012  // BGBG line
1013  // Bayer -1 0 1
1014  // -1 g b g
1015  // 0 r g r
1016  // line_step g B g
1017  //rgb_pixel[rgb_line_step ] = AVG2( bayer_pixel[-1], bayer_pixel[1] );
1018  rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
1019  rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
1020 
1021  // Bayer -1 0 1
1022  // -1 g b g
1023  // 0 r g r
1024  // line_step g b G
1025  //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
1026  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
1027  //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
1028  }
1029  else if (debayering_method_ == EdgeAwareWeighted)
1030  {
1031  int dh, dv;
1032 
1033  // first two pixel values for first two lines
1034  // Bayer 0 1 2
1035  // 0 G r g
1036  // line_step b g b
1037  // line_step2 g r g
1038 
1039  rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
1040  rgb_buffer[1] = bayer_pixel[0]; // green pixel
1041  rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
1042 
1043  // Bayer 0 1 2
1044  // 0 g R g
1045  // line_step b g b
1046  // line_step2 g r g
1047  //rgb_pixel[3] = bayer_pixel[1];
1048  rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
1049  rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
1050 
1051  // BGBG line
1052  // Bayer 0 1 2
1053  // 0 g r g
1054  // line_step B g b
1055  // line_step2 g r g
1056  rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
1057  rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
1058  //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
1059 
1060  // pixel (1, 1) 0 1 2
1061  // 0 g r g
1062  // line_step b G b
1063  // line_step2 g r g
1064  //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
1065  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
1066  //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
1067 
1068  rgb_buffer += 6;
1069  bayer_pixel += 2;
1070  // rest of the first two lines
1071  for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
1072  {
1073  // GRGR line
1074  // Bayer -1 0 1 2
1075  // 0 r G r g
1076  // line_step g b g b
1077  // line_step2 r g r g
1078  rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
1079  rgb_buffer[1] = bayer_pixel[0];
1080  rgb_buffer[2] = bayer_pixel[bayer_line_step + 1];
1081 
1082  // Bayer -1 0 1 2
1083  // 0 r g R g
1084  // line_step g b g b
1085  // line_step2 r g r g
1086  rgb_buffer[3] = bayer_pixel[1];
1087  rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
1088  rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
1089 
1090  // BGBG line
1091  // Bayer -1 0 1 2
1092  // 0 r g r g
1093  // line_step g B g b
1094  // line_step2 r g r g
1095  rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
1096  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]);
1097  rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
1098 
1099  // Bayer -1 0 1 2
1100  // 0 r g r g
1101  // line_step g b G b
1102  // line_step2 r g r g
1103  rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
1104  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
1105  //rgb_pixel[rgb_line_step + 5] = AVG( bayer_pixel[line_step] , bayer_pixel[line_step+2] );
1106  }
1107 
1108  // last two pixel values for first two lines
1109  // GRGR line
1110  // Bayer -1 0 1
1111  // 0 r G r
1112  // line_step g b g
1113  // line_step2 r g r
1114  rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
1115  rgb_buffer[1] = bayer_pixel[0];
1116  rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
1117 
1118  // Bayer -1 0 1
1119  // 0 r g R
1120  // line_step g b g
1121  // line_step2 r g r
1122  rgb_buffer[3] = bayer_pixel[1];
1123  rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
1124  //rgb_pixel[5] = bayer_pixel[line_step];
1125 
1126  // BGBG line
1127  // Bayer -1 0 1
1128  // 0 r g r
1129  // line_step g B g
1130  // line_step2 r g r
1131  rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
1132  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]);
1133  //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
1134 
1135  // Bayer -1 0 1
1136  // 0 r g r
1137  // line_step g b G
1138  // line_step2 r g r
1139  rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
1140  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
1141  //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
1142 
1143  bayer_pixel += bayer_line_step + 2;
1144  rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
1145  // main processing
1146  for (yIdx = 2; yIdx < height - 2; yIdx += 2)
1147  {
1148  // first two pixel values
1149  // Bayer 0 1 2
1150  // -1 b g b
1151  // 0 G r g
1152  // line_step b g b
1153  // line_step2 g r g
1154 
1155  rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
1156  rgb_buffer[1] = bayer_pixel[0]; // green pixel
1157  rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]); // blue;
1158 
1159  // Bayer 0 1 2
1160  // -1 b g b
1161  // 0 g R g
1162  // line_step b g b
1163  // line_step2 g r g
1164  //rgb_pixel[3] = bayer_pixel[1];
1165  rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
1166  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]);
1167 
1168  // BGBG line
1169  // Bayer 0 1 2
1170  // 0 g r g
1171  // line_step B g b
1172  // line_step2 g r g
1173  rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
1174  rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
1175  rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
1176 
1177  // pixel (1, 1) 0 1 2
1178  // 0 g r g
1179  // line_step b G b
1180  // line_step2 g r g
1181  //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
1182  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
1183  rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
1184 
1185  rgb_buffer += 6;
1186  bayer_pixel += 2;
1187  // continue with rest of the line
1188  for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
1189  {
1190  // GRGR line
1191  // Bayer -1 0 1 2
1192  // -1 g b g b
1193  // 0 r G r g
1194  // line_step g b g b
1195  // line_step2 r g r g
1196  rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
1197  rgb_buffer[1] = bayer_pixel[0];
1198  rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
1199 
1200  // Bayer -1 0 1 2
1201  // -1 g b g b
1202  // 0 r g R g
1203  // line_step g b g b
1204  // line_step2 r g r g
1205 
1206  dh = abs (bayer_pixel[0] - bayer_pixel[2]);
1207  dv = abs (bayer_pixel[-bayer_line_step + 1] - bayer_pixel[bayer_line_step + 1]);
1208 
1209  if (dv == 0 && dh == 0)
1210  rgb_buffer[4] = AVG4 (bayer_pixel[1 - bayer_line_step], bayer_pixel[1 + bayer_line_step], bayer_pixel[0], bayer_pixel[2]);
1211  else
1212  rgb_buffer[4] = WAVG4 (bayer_pixel[1 - bayer_line_step], bayer_pixel[1 + bayer_line_step], bayer_pixel[0], bayer_pixel[2], dh, dv);
1213  rgb_buffer[3] = bayer_pixel[1];
1214  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]);
1215 
1216  // BGBG line
1217  // Bayer -1 0 1 2
1218  // -1 g b g b
1219  // 0 r g r g
1220  // line_step g B g b
1221  // line_step2 r g r g
1222  rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
1223  rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
1224 
1225  dv = abs (bayer_pixel[0] - bayer_pixel[bayer_line_step2]);
1226  dh = abs (bayer_pixel[bayer_line_step - 1] - bayer_pixel[bayer_line_step + 1]);
1227 
1228  if (dv == 0 && dh == 0)
1229  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]);
1230  else
1231  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);
1232 
1233  // Bayer -1 0 1 2
1234  // -1 g b g b
1235  // 0 r g r g
1236  // line_step g b G b
1237  // line_step2 r g r g
1238  rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
1239  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
1240  rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
1241  }
1242 
1243  // last two pixels of the line
1244  // last two pixel values for first two lines
1245  // GRGR line
1246  // Bayer -1 0 1
1247  // 0 r G r
1248  // line_step g b g
1249  // line_step2 r g r
1250  rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
1251  rgb_buffer[1] = bayer_pixel[0];
1252  rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
1253 
1254  // Bayer -1 0 1
1255  // 0 r g R
1256  // line_step g b g
1257  // line_step2 r g r
1258  rgb_buffer[3] = bayer_pixel[1];
1259  rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
1260  //rgb_pixel[5] = bayer_pixel[line_step];
1261 
1262  // BGBG line
1263  // Bayer -1 0 1
1264  // 0 r g r
1265  // line_step g B g
1266  // line_step2 r g r
1267  rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
1268  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]);
1269  //rgb_pixel[rgb_line_step + 2] = bayer_pixel[line_step];
1270 
1271  // Bayer -1 0 1
1272  // 0 r g r
1273  // line_step g b G
1274  // line_step2 r g r
1275  rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
1276  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
1277  //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
1278 
1279  bayer_pixel += bayer_line_step + 2;
1280  rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
1281  }
1282 
1283  //last two lines
1284  // Bayer 0 1 2
1285  // -1 b g b
1286  // 0 G r g
1287  // line_step b g b
1288 
1289  rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1]; // red pixel
1290  rgb_buffer[1] = bayer_pixel[0]; // green pixel
1291  rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step]; // blue;
1292 
1293  // Bayer 0 1 2
1294  // -1 b g b
1295  // 0 g R g
1296  // line_step b g b
1297  //rgb_pixel[3] = bayer_pixel[1];
1298  rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
1299  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]);
1300 
1301  // BGBG line
1302  // Bayer 0 1 2
1303  // -1 b g b
1304  // 0 g r g
1305  // line_step B g b
1306  //rgb_pixel[rgb_line_step ] = bayer_pixel[1];
1307  rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
1308  rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
1309 
1310  // Bayer 0 1 2
1311  // -1 b g b
1312  // 0 g r g
1313  // line_step b G b
1314  //rgb_pixel[rgb_line_step + 3] = AVG( bayer_pixel[1] , bayer_pixel[line_step2+1] );
1315  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
1316  rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
1317 
1318  rgb_buffer += 6;
1319  bayer_pixel += 2;
1320  // rest of the last two lines
1321  for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
1322  {
1323  // GRGR line
1324  // Bayer -1 0 1 2
1325  // -1 g b g b
1326  // 0 r G r g
1327  // line_step g b g b
1328  rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
1329  rgb_buffer[1] = bayer_pixel[0];
1330  rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
1331 
1332  // Bayer -1 0 1 2
1333  // -1 g b g b
1334  // 0 r g R g
1335  // line_step g b g b
1336  rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
1337  rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
1338  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]);
1339 
1340  // BGBG line
1341  // Bayer -1 0 1 2
1342  // -1 g b g b
1343  // 0 r g r g
1344  // line_step g B g b
1345  rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[-1], bayer_pixel[1]);
1346  rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
1347  rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
1348 
1349 
1350  // Bayer -1 0 1 2
1351  // -1 g b g b
1352  // 0 r g r g
1353  // line_step g b G b
1354  //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
1355  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
1356  rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
1357  }
1358 
1359  // last two pixel values for first two lines
1360  // GRGR line
1361  // Bayer -1 0 1
1362  // -1 g b g
1363  // 0 r G r
1364  // line_step g b g
1365  rgb_buffer[rgb_line_step ] = rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
1366  rgb_buffer[1] = bayer_pixel[0];
1367  rgb_buffer[5] = rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
1368 
1369  // Bayer -1 0 1
1370  // -1 g b g
1371  // 0 r g R
1372  // line_step g b g
1373  rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
1374  rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[-bayer_line_step + 1]);
1375  //rgb_pixel[5] = AVG( bayer_pixel[line_step], bayer_pixel[-line_step] );
1376 
1377  // BGBG line
1378  // Bayer -1 0 1
1379  // -1 g b g
1380  // 0 r g r
1381  // line_step g B g
1382  //rgb_pixel[rgb_line_step ] = AVG2( bayer_pixel[-1], bayer_pixel[1] );
1383  rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
1384  rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
1385 
1386  // Bayer -1 0 1
1387  // -1 g b g
1388  // 0 r g r
1389  // line_step g b G
1390  //rgb_pixel[rgb_line_step + 3] = bayer_pixel[1];
1391  rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
1392  //rgb_pixel[rgb_line_step + 5] = bayer_pixel[line_step];
1393  }
1394  else
1395  THROW_OPENNI_EXCEPTION ("Unknwon debayering method: %d", (int)debayering_method_);
1396  }
1397  else
1398  {
1399  if (image_md_->XRes () % width != 0 || image_md_->YRes () % height != 0)
1400  THROW_OPENNI_EXCEPTION ("Downsampling only possible for integer scales in both dimensions. Request was %d x %d -> %d x %d.", image_md_->XRes (), image_md_->YRes (), width, height);
1401 
1402  // get each or each 2nd pixel group to find rgb values!
1403  register unsigned bayerXStep = image_md_->XRes () / width;
1404  register unsigned bayerYSkip = (image_md_->YRes () / height - 1) * image_md_->XRes ();
1405 
1406  // Downsampling and debayering at once
1407  register const XnUInt8* bayer_buffer = image_md_->WritableData ();
1408 
1409  for (register unsigned yIdx = 0; yIdx < height; ++yIdx, bayer_buffer += bayerYSkip, rgb_buffer += rgb_line_skip) // skip a line
1410  {
1411  for (register unsigned xIdx = 0; xIdx < width; ++xIdx, rgb_buffer += 3, bayer_buffer += bayerXStep)
1412  {
1413  rgb_buffer[ 2 ] = bayer_buffer[ image_md_->XRes () ];
1414  rgb_buffer[ 1 ] = AVG (bayer_buffer[0], bayer_buffer[ image_md_->XRes () + 1]);
1415  rgb_buffer[ 0 ] = bayer_buffer[ 1 ];
1416  }
1417  }
1418  }
1419 }
1420 } //namespace
#define THROW_OPENNI_EXCEPTION(format,...)
#define AVG4(a, b, c, d)
#define AVG3(a, b, c)
#define WAVG4(a, b, c, d, x, y)
Image class containing just a reference to image meta data. Thus this class just provides an interfac...
Definition: openni_image.h:54
General exception class.
#define AVG(a, b)


openni_camera
Author(s): Patrick Mihelich, Suat Gedikli, Radu Bogdan Rusu
autogenerated on Wed Jun 5 2019 20:15:22