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