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