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/io/debayer.h>
00038
00039 #define AVG(a,b) static_cast<unsigned char>((int(a) + int(b)) >> 1)
00040 #define AVG3(a,b,c) static_cast<unsigned char>((int(a) + int(b) + int(c)) / 3)
00041 #define AVG4(a,b,c,d) static_cast<unsigned char>((int(a) + int(b) + int(c) + int(d)) >> 2)
00042 #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 ) )
00043
00045 void
00046 pcl::io::DeBayer::debayerBilinear (
00047 const unsigned char *bayer_pixel, unsigned char *rgb_buffer,
00048 unsigned width, unsigned height,
00049 int bayer_line_step,
00050 int bayer_line_step2,
00051 unsigned rgb_line_step) const
00052 {
00053 if (bayer_line_step == 0)
00054 bayer_line_step = width;
00055 if (bayer_line_step2 == 0)
00056 bayer_line_step2 = width << 1;
00057 if (rgb_line_step == 0)
00058 rgb_line_step = width * 3;
00059
00060
00061 unsigned rgb_line_skip = rgb_line_step - width * 3;
00062 register unsigned yIdx, xIdx;
00063
00064
00065
00066
00067
00068
00069 rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1];
00070 rgb_buffer[1] = bayer_pixel[0];
00071 rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00072
00073
00074
00075
00076
00077
00078 rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00079 rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00080
00081
00082
00083
00084
00085
00086 rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00087 rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00088
00089
00090
00091
00092
00093
00094
00095 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00096
00097
00098 rgb_buffer += 6;
00099 bayer_pixel += 2;
00100
00101 for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00102 {
00103
00104
00105
00106
00107
00108 rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00109 rgb_buffer[1] = bayer_pixel[0];
00110 rgb_buffer[2] = bayer_pixel[bayer_line_step + 1];
00111
00112
00113
00114
00115
00116 rgb_buffer[3] = bayer_pixel[1];
00117 rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00118 rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00119
00120
00121
00122
00123
00124
00125 rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00126 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]);
00127 rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00128
00129
00130
00131
00132
00133 rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00134 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00135
00136 }
00137
00138
00139
00140
00141
00142
00143
00144 rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00145 rgb_buffer[1] = bayer_pixel[0];
00146 rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00147
00148
00149
00150
00151
00152 rgb_buffer[3] = bayer_pixel[1];
00153 rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00154
00155
00156
00157
00158
00159
00160
00161 rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00162 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]);
00163
00164
00165
00166
00167
00168
00169 rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00170 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00171
00172
00173 bayer_pixel += bayer_line_step + 2;
00174 rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
00175
00176
00177
00178 for (yIdx = 2; yIdx < height - 2; yIdx += 2)
00179 {
00180
00181
00182
00183
00184
00185
00186
00187 rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1];
00188 rgb_buffer[1] = bayer_pixel[0];
00189 rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00190
00191
00192
00193
00194
00195
00196
00197 rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00198 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]);
00199
00200
00201
00202
00203
00204
00205 rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00206 rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00207 rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00208
00209
00210
00211
00212
00213
00214 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00215 rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00216
00217 rgb_buffer += 6;
00218 bayer_pixel += 2;
00219
00220 for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00221 {
00222
00223
00224
00225
00226
00227
00228 rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00229 rgb_buffer[1] = bayer_pixel[0];
00230 rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00231
00232
00233
00234
00235
00236
00237 rgb_buffer[3] = bayer_pixel[1];
00238 rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00239 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]);
00240
00241
00242
00243
00244
00245
00246
00247 rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00248 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]);
00249 rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00250
00251
00252
00253
00254
00255
00256 rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00257 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00258 rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268 rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00269 rgb_buffer[1] = bayer_pixel[0];
00270 rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00271
00272
00273
00274
00275
00276 rgb_buffer[3] = bayer_pixel[1];
00277 rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00278
00279
00280
00281
00282
00283
00284
00285 rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00286 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]);
00287
00288
00289
00290
00291
00292
00293 rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00294 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00295
00296
00297 bayer_pixel += bayer_line_step + 2;
00298 rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
00299 }
00300
00301
00302
00303
00304
00305
00306
00307 rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1];
00308 rgb_buffer[1] = bayer_pixel[0];
00309 rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00310
00311
00312
00313
00314
00315
00316 rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00317 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]);
00318
00319
00320
00321
00322
00323
00324
00325 rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00326 rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00327
00328
00329
00330
00331
00332
00333 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00334 rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00335
00336 rgb_buffer += 6;
00337 bayer_pixel += 2;
00338
00339 for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00340 {
00341
00342
00343
00344
00345
00346 rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00347 rgb_buffer[1] = bayer_pixel[0];
00348 rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00349
00350
00351
00352
00353
00354 rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
00355 rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00356 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]);
00357
00358
00359
00360
00361
00362
00363 rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[-1], bayer_pixel[1]);
00364 rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00365 rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00366
00367
00368
00369
00370
00371
00372
00373 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00374 rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00375 }
00376
00377
00378
00379
00380
00381
00382
00383 rgb_buffer[rgb_line_step ] = rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00384 rgb_buffer[1] = bayer_pixel[0];
00385 rgb_buffer[5] = rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00386
00387
00388
00389
00390
00391 rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
00392 rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[-bayer_line_step + 1]);
00393
00394
00395
00396
00397
00398
00399
00400
00401 rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00402 rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00403
00404
00405
00406
00407
00408
00409 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00410
00411 }
00412
00414 void
00415 pcl::io::DeBayer::debayerEdgeAware (
00416 const unsigned char *bayer_pixel, unsigned char *rgb_buffer,
00417 unsigned width, unsigned height,
00418 int bayer_line_step,
00419 int bayer_line_step2,
00420 unsigned rgb_line_step) const
00421 {
00422 if (bayer_line_step == 0)
00423 bayer_line_step = width;
00424 if (bayer_line_step2 == 0)
00425 bayer_line_step2 = width << 1;
00426 if (rgb_line_step == 0)
00427 rgb_line_step = width * 3;
00428
00429
00430 unsigned rgb_line_skip = rgb_line_step - width * 3;
00431 register unsigned yIdx, xIdx;
00432 int dh, dv;
00433
00434
00435
00436
00437
00438
00439
00440 rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1];
00441 rgb_buffer[1] = bayer_pixel[0];
00442 rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00443
00444
00445
00446
00447
00448
00449 rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00450 rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00451
00452
00453
00454
00455
00456
00457 rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00458 rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00459
00460
00461
00462
00463
00464
00465
00466 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00467
00468
00469 rgb_buffer += 6;
00470 bayer_pixel += 2;
00471
00472 for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00473 {
00474
00475
00476
00477
00478
00479 rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00480 rgb_buffer[1] = bayer_pixel[0];
00481 rgb_buffer[2] = bayer_pixel[bayer_line_step + 1];
00482
00483
00484
00485
00486
00487 rgb_buffer[3] = bayer_pixel[1];
00488 rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00489 rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00490
00491
00492
00493
00494
00495
00496 rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00497 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]);
00498 rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
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
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 for (yIdx = 2; yIdx < height - 2; yIdx += 2)
00548 {
00549
00550
00551
00552
00553
00554
00555
00556 rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1];
00557 rgb_buffer[1] = bayer_pixel[0];
00558 rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00559
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 rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00575 rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
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
00597 rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00598 rgb_buffer[1] = bayer_pixel[0];
00599 rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00600
00601
00602
00603
00604
00605
00606
00607 dh = abs (bayer_pixel[0] - bayer_pixel[2]);
00608 dv = abs (bayer_pixel[-bayer_line_step + 1] - bayer_pixel[bayer_line_step + 1]);
00609
00610 if (dh > dv)
00611 rgb_buffer[4] = AVG (bayer_pixel[-bayer_line_step + 1], bayer_pixel[bayer_line_step + 1]);
00612 else if (dv > dh)
00613 rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[2]);
00614 else
00615 rgb_buffer[4] = AVG4 (bayer_pixel[-bayer_line_step + 1], bayer_pixel[bayer_line_step + 1], bayer_pixel[0], bayer_pixel[2]);
00616
00617 rgb_buffer[3] = bayer_pixel[1];
00618 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]);
00619
00620
00621
00622
00623
00624
00625
00626 rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00627 rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00628
00629 dv = abs (bayer_pixel[0] - bayer_pixel[bayer_line_step2]);
00630 dh = abs (bayer_pixel[bayer_line_step - 1] - bayer_pixel[bayer_line_step + 1]);
00631
00632 if (dv > dh)
00633 rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00634 else if (dh > dv)
00635 rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step2]);
00636 else
00637 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]);
00638
00639
00640
00641
00642
00643
00644 rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00645 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00646 rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00647 }
00648
00649
00650
00651
00652
00653
00654
00655
00656 rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00657 rgb_buffer[1] = bayer_pixel[0];
00658 rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00659
00660
00661
00662
00663
00664 rgb_buffer[3] = bayer_pixel[1];
00665 rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00666
00667
00668
00669
00670
00671
00672
00673 rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00674 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]);
00675
00676
00677
00678
00679
00680
00681 rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00682 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00683
00684
00685 bayer_pixel += bayer_line_step + 2;
00686 rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
00687 }
00688
00689
00690
00691
00692
00693
00694
00695 rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1];
00696 rgb_buffer[1] = bayer_pixel[0];
00697 rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00698
00699
00700
00701
00702
00703
00704 rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00705 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]);
00706
00707
00708
00709
00710
00711
00712
00713 rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00714 rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00715
00716
00717
00718
00719
00720
00721 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00722 rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00723
00724 rgb_buffer += 6;
00725 bayer_pixel += 2;
00726
00727 for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00728 {
00729
00730
00731
00732
00733
00734 rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00735 rgb_buffer[1] = bayer_pixel[0];
00736 rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00737
00738
00739
00740
00741
00742 rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
00743 rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00744 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]);
00745
00746
00747
00748
00749
00750
00751 rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[-1], bayer_pixel[1]);
00752 rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00753 rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00754
00755
00756
00757
00758
00759
00760
00761 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00762 rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00763 }
00764
00765
00766
00767
00768
00769
00770
00771 rgb_buffer[rgb_line_step ] = rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00772 rgb_buffer[1] = bayer_pixel[0];
00773 rgb_buffer[5] = rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00774
00775
00776
00777
00778
00779 rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
00780 rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[-bayer_line_step + 1]);
00781
00782
00783
00784
00785
00786
00787
00788
00789 rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
00790 rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00791
00792
00793
00794
00795
00796
00797 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00798
00799 }
00800
00802 void
00803 pcl::io::DeBayer::debayerEdgeAwareWeighted (
00804 const unsigned char *bayer_pixel, unsigned char *rgb_buffer,
00805 unsigned width, unsigned height,
00806 int bayer_line_step,
00807 int bayer_line_step2,
00808 unsigned rgb_line_step) const
00809 {
00810 if (bayer_line_step == 0)
00811 bayer_line_step = width;
00812 if (bayer_line_step2 == 0)
00813 bayer_line_step2 = width << 1;
00814 if (rgb_line_step == 0)
00815 rgb_line_step = width * 3;
00816
00817
00818 unsigned rgb_line_skip = rgb_line_step - width * 3;
00819 register unsigned yIdx, xIdx;
00820 int dh, dv;
00821
00822
00823
00824
00825
00826
00827
00828 rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1];
00829 rgb_buffer[1] = bayer_pixel[0];
00830 rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00831
00832
00833
00834
00835
00836
00837 rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00838 rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00839
00840
00841
00842
00843
00844
00845 rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00846 rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00847
00848
00849
00850
00851
00852
00853
00854 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00855
00856
00857 rgb_buffer += 6;
00858 bayer_pixel += 2;
00859
00860 for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00861 {
00862
00863
00864
00865
00866
00867 rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00868 rgb_buffer[1] = bayer_pixel[0];
00869 rgb_buffer[2] = bayer_pixel[bayer_line_step + 1];
00870
00871
00872
00873
00874
00875 rgb_buffer[3] = bayer_pixel[1];
00876 rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1]);
00877 rgb_buffer[rgb_line_step + 5] = rgb_buffer[5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00878
00879
00880
00881
00882
00883
00884 rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00885 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]);
00886 rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00887
00888
00889
00890
00891
00892 rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00893 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00894
00895 }
00896
00897
00898
00899
00900
00901
00902
00903 rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00904 rgb_buffer[1] = bayer_pixel[0];
00905 rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
00906
00907
00908
00909
00910
00911 rgb_buffer[3] = bayer_pixel[1];
00912 rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
00913
00914
00915
00916
00917
00918
00919
00920 rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
00921 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]);
00922
00923
00924
00925
00926
00927
00928 rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00929 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00930
00931
00932 bayer_pixel += bayer_line_step + 2;
00933 rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
00934
00935 for (yIdx = 2; yIdx < height - 2; yIdx += 2)
00936 {
00937
00938
00939
00940
00941
00942
00943
00944 rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1];
00945 rgb_buffer[1] = bayer_pixel[0];
00946 rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00947
00948
00949
00950
00951
00952
00953
00954 rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
00955 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]);
00956
00957
00958
00959
00960
00961
00962 rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
00963 rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[bayer_line_step2]);
00964 rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
00965
00966
00967
00968
00969
00970
00971 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
00972 rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
00973
00974 rgb_buffer += 6;
00975 bayer_pixel += 2;
00976
00977 for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
00978 {
00979
00980
00981
00982
00983
00984
00985 rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
00986 rgb_buffer[1] = bayer_pixel[0];
00987 rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
00988
00989
00990
00991
00992
00993
00994
00995 dh = abs (bayer_pixel[0] - bayer_pixel[2]);
00996 dv = abs (bayer_pixel[-bayer_line_step + 1] - bayer_pixel[bayer_line_step + 1]);
00997
00998 if (dv == 0 && dh == 0)
00999 rgb_buffer[4] = AVG4 (bayer_pixel[1 - bayer_line_step], bayer_pixel[1 + bayer_line_step], bayer_pixel[0], bayer_pixel[2]);
01000 else
01001 rgb_buffer[4] = WAVG4 (bayer_pixel[1 - bayer_line_step], bayer_pixel[1 + bayer_line_step], bayer_pixel[0], bayer_pixel[2], dh, dv);
01002 rgb_buffer[3] = bayer_pixel[1];
01003 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]);
01004
01005
01006
01007
01008
01009
01010
01011 rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
01012 rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
01013
01014 dv = abs (bayer_pixel[0] - bayer_pixel[bayer_line_step2]);
01015 dh = abs (bayer_pixel[bayer_line_step - 1] - bayer_pixel[bayer_line_step + 1]);
01016
01017 if (dv == 0 && dh == 0)
01018 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]);
01019 else
01020 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);
01021
01022
01023
01024
01025
01026
01027 rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
01028 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01029 rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
01030 }
01031
01032
01033
01034
01035
01036
01037
01038
01039 rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
01040 rgb_buffer[1] = bayer_pixel[0];
01041 rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = rgb_buffer[5] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
01042
01043
01044
01045
01046
01047 rgb_buffer[3] = bayer_pixel[1];
01048 rgb_buffer[4] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
01049
01050
01051
01052
01053
01054
01055
01056 rgb_buffer[rgb_line_step ] = AVG4 (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1], bayer_pixel[-1], bayer_pixel[bayer_line_step2 - 1]);
01057 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]);
01058
01059
01060
01061
01062
01063
01064 rgb_buffer[rgb_line_step + 3] = AVG (bayer_pixel[1], bayer_pixel[bayer_line_step2 + 1]);
01065 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01066
01067
01068 bayer_pixel += bayer_line_step + 2;
01069 rgb_buffer += rgb_line_step + 6 + rgb_line_skip;
01070 }
01071
01072
01073
01074
01075
01076
01077
01078 rgb_buffer[rgb_line_step + 3] = rgb_buffer[rgb_line_step ] = rgb_buffer[3] = rgb_buffer[0] = bayer_pixel[1];
01079 rgb_buffer[1] = bayer_pixel[0];
01080 rgb_buffer[rgb_line_step + 2] = rgb_buffer[2] = bayer_pixel[bayer_line_step];
01081
01082
01083
01084
01085
01086
01087 rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
01088 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]);
01089
01090
01091
01092
01093
01094
01095
01096 rgb_buffer[rgb_line_step + 1] = AVG (bayer_pixel[0], bayer_pixel[bayer_line_step + 1]);
01097 rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
01098
01099
01100
01101
01102
01103
01104 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01105 rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
01106
01107 rgb_buffer += 6;
01108 bayer_pixel += 2;
01109
01110 for (xIdx = 2; xIdx < width - 2; xIdx += 2, rgb_buffer += 6, bayer_pixel += 2)
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[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
01120
01121
01122
01123
01124
01125 rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
01126 rgb_buffer[4] = AVG4 (bayer_pixel[0], bayer_pixel[2], bayer_pixel[bayer_line_step + 1], bayer_pixel[1 - bayer_line_step]);
01127 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]);
01128
01129
01130
01131
01132
01133
01134 rgb_buffer[rgb_line_step ] = AVG (bayer_pixel[-1], bayer_pixel[1]);
01135 rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
01136 rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
01137
01138
01139
01140
01141
01142
01143
01144 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01145 rgb_buffer[rgb_line_step + 5] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[bayer_line_step + 2]);
01146 }
01147
01148
01149
01150
01151
01152
01153
01154 rgb_buffer[rgb_line_step ] = rgb_buffer[0] = AVG (bayer_pixel[1], bayer_pixel[-1]);
01155 rgb_buffer[1] = bayer_pixel[0];
01156 rgb_buffer[5] = rgb_buffer[2] = AVG (bayer_pixel[bayer_line_step], bayer_pixel[-bayer_line_step]);
01157
01158
01159
01160
01161
01162 rgb_buffer[rgb_line_step + 3] = rgb_buffer[3] = bayer_pixel[1];
01163 rgb_buffer[4] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step + 1], bayer_pixel[-bayer_line_step + 1]);
01164
01165
01166
01167
01168
01169
01170
01171
01172 rgb_buffer[rgb_line_step + 1] = AVG3 (bayer_pixel[0], bayer_pixel[bayer_line_step - 1], bayer_pixel[bayer_line_step + 1]);
01173 rgb_buffer[rgb_line_step + 5] = rgb_buffer[rgb_line_step + 2] = bayer_pixel[bayer_line_step];
01174
01175
01176
01177
01178
01179
01180 rgb_buffer[rgb_line_step + 4] = bayer_pixel[bayer_line_step + 1];
01181
01182 }
01183