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