-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwf_filtering.cpp
1941 lines (1465 loc) · 43.7 KB
/
wf_filtering.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
WF debanding code
Copyright 2011 by Yan Vladimirovich
Used in LibRaw with author permission.
LibRaw is free software; you can redistribute it and/or modify
it under the terms of the one of two licenses as you choose:
1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
(See file LICENSE.LGPL provided in LibRaw distribution archive for details).
2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
(See file LICENSE.CDDL provided in LibRaw distribution archive for details).
*/
#define P1 imgdata.idata
#define S imgdata.sizes
#define O imgdata.params
#define C imgdata.color
#define T imgdata.thumbnail
#define IO libraw_internal_data.internal_output_params
#define ID libraw_internal_data.internal_data
int LibRaw::wf_remove_banding()
{
#define WF_IMGMODE_BAYER4PLANE 4
#define WF_IMGMODE_BAYER1PLANE 1
#define WF_GREENMODE_IND 0
#define WF_GREENMODE_GX_XG 1
#define WF_GREENMODE_XG_GX 2
#define WF_DEBANDING_OK 0
#define WF_DEBANDING_NOTBAYER2X2 1
#define WF_DEBANDING_TOOSMALL 2
#define WF_GAUSS_PIRAMID_SIZE 4
#define WF_MAXTRESHOLD 65536
#define WF_BAYERSRC(row, col, c) ((ushort(*)[4])imgdata.image)[((row) >> IO.shrink)*S.iwidth + ((col) >> IO.shrink)][c]
#define WF_BAYERGAU(l, row, col) (gauss_pyramid[l])[((row) >> IO.shrink)*S.iwidth + ((col) >> IO.shrink)]
#define WF_BAYERDFG(l, row, col) (difwg_pyramid[l])[((row) >> IO.shrink)*S.iwidth + ((col) >> IO.shrink)]
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define WF_i_1TO4 for(int i=0; i<4; i++)
// too small?
if (S.width<128 || S.height<128)
return WF_DEBANDING_TOOSMALL;
// is 2x2 bayer?
int bayer2x2flag=-1;
for(int row_shift=0; row_shift<=8; row_shift+=2)
{
for(int col_shift=0; col_shift<=8; col_shift+=2)
{
if ((FC(0,0)!=FC(row_shift, col_shift)) ||
(FC(1,0)!=FC(row_shift+1, col_shift)) ||
(FC(0,1)!=FC(row_shift, col_shift+1)) ||
(FC(1,1)!=FC(row_shift+1, col_shift+1)))
{
bayer2x2flag=0;
}
}
}
if (bayer2x2flag==0)
return WF_DEBANDING_NOTBAYER2X2;
int x_green_flag = -1;
int width_d2, height_d2;
int width_p1_d2, height_p1_d2;
width_d2 = S.width/2;
height_d2 = S.height/2;
width_p1_d2 = (S.width+1)/2;
height_p1_d2 = (S.height+1)/2;
ushort val_max_c[4]={0,0,0,0};
ushort val_max;
ushort dummy_pixel=0;
ushort *dummy_line;
dummy_line = (ushort*)calloc(S.width, sizeof(ushort)*4);
for(int i=0; i<S.width*4; i++)
dummy_line[i]=0;
// Searching max value for increasing bit-depth
for(int row_d2=0; row_d2<height_p1_d2; row_d2++)
{
int row, row_p1;
ushort *src[4];
ushort *src_first, *src_plast, *src_last;
row = row_d2*2;
row_p1 = row+1;
WF_i_1TO4 src[i] = &WF_BAYERSRC((i<2)?row:row_p1, i&1, FC((i<2)?row:row_p1, i&1));
if (row_p1==S.height)
src[2]=src[3]=dummy_line;
src_first = &WF_BAYERSRC(row, 0, FC(row, 0));
src_plast = &WF_BAYERSRC(row, width_d2*2-2, FC(row, 0));
src_last = &WF_BAYERSRC(row, width_p1_d2*2-2, FC(row, 0));
do
{
// Do
WF_i_1TO4 val_max_c[i]=MAX(val_max_c[i], *src[i]);
// Next 4 pixel or exit
if (src[0]<src_plast)
{
WF_i_1TO4 src[i]+=8;
}
else if(src[0]>src_first && src[0]<src_last)
{
WF_i_1TO4 src[i]=i&1?&dummy_pixel:src[i]+8;
}
else break;
}
while(1);
}
val_max=MAX(MAX(val_max_c[0], val_max_c[1]), MAX(val_max_c[2], val_max_c[3]));
// end of searching max value
if (val_max==0)
return WF_DEBANDING_OK;
int data_shift;
int data_mult;
int val_max_s;
data_shift = 15;
val_max_s = val_max;
if (val_max_s >= (1 << 8)) { val_max_s >>= 8; data_shift -= 8; }
if (val_max_s >= (1 << 4)) { val_max_s >>= 4; data_shift -= 4; }
if (val_max_s >= (1 << 2)) { val_max_s >>= 2; data_shift -= 2; }
if (val_max_s >= (1 << 1)) { data_shift -= 1; }
data_mult = 1<<data_shift;
val_max <<= data_shift;
// Bit shift
for(int row_d2=0; row_d2<height_p1_d2; row_d2++)
{
int row, row_p1;
ushort *src[4];
ushort *src_first, *src_plast, *src_last;
row = row_d2*2;
row_p1 = row+1;
WF_i_1TO4 src[i] = &WF_BAYERSRC((i<2)?row:row_p1, i&1, FC((i<2)?row:row_p1, i&1));
if (row_p1==S.height)
src[2]=src[3]=dummy_line;
src_first = &WF_BAYERSRC(row, 0, FC(row, 0));
src_plast = &WF_BAYERSRC(row, width_d2*2-2, FC(row, 0));
src_last = &WF_BAYERSRC(row, width_p1_d2*2-2, FC(row, 0));
do
{
// Do
WF_i_1TO4 (*src[i])<<=data_shift;
// Next 4 pixel or exit
if (src[0]<src_plast)
{
WF_i_1TO4 src[i]+=8;
}
else if(src[0]>src_first && src[0]<src_last)
{
WF_i_1TO4 src[i]=i&1?&dummy_pixel:src[i]+8;
}
else break;
}
while(1);
}
ushort *gauss_pyramid[WF_GAUSS_PIRAMID_SIZE];
ushort *difwg_pyramid[WF_GAUSS_PIRAMID_SIZE];
for(int i=0; i<WF_GAUSS_PIRAMID_SIZE; i++)
{
gauss_pyramid[i] = (ushort*)calloc(S.width*S.height, sizeof(ushort));
difwg_pyramid[i] = (ushort*)calloc(S.width*S.height, sizeof(ushort));
}
int radius3x3 [4]={3, 3, 3, 0}; // as gau r=24
int radius3x14[4]={14, 14, 14, 0}; // as gau r=420
int radius3x45[4]={45, 45, 45, 0}; // as gau r=4140
// Making 4-level gaussian pyramid
if (x_green_flag)
{
wf_bayer4_green_blur (0, imgdata.image, WF_IMGMODE_BAYER4PLANE, gauss_pyramid[0], WF_IMGMODE_BAYER1PLANE);
wf_bayer4_igauss_filter(1, gauss_pyramid[0], WF_IMGMODE_BAYER1PLANE, gauss_pyramid[0], WF_IMGMODE_BAYER1PLANE);
}
else
{
wf_bayer4_igauss_filter(1, imgdata.image, WF_IMGMODE_BAYER4PLANE, gauss_pyramid[0], WF_IMGMODE_BAYER1PLANE);
}
wf_bayer4_block_filter (radius3x3, gauss_pyramid[0], WF_IMGMODE_BAYER1PLANE, gauss_pyramid[1], WF_IMGMODE_BAYER1PLANE); // as gau r=24
wf_bayer4_block_filter (radius3x14, gauss_pyramid[0], WF_IMGMODE_BAYER1PLANE, gauss_pyramid[2], WF_IMGMODE_BAYER1PLANE); // as gau r=420
wf_bayer4_block_filter (radius3x45, gauss_pyramid[0], WF_IMGMODE_BAYER1PLANE, gauss_pyramid[3], WF_IMGMODE_BAYER1PLANE); // as gau r=4140
// Energy multiplyers for laplasyan pyramid
float dfg_mult[WF_GAUSS_PIRAMID_SIZE]={1.560976, 8.196011, 180.413773, 3601.427246/3.0};
/* dif_mult[0]=1.0/wf_filter_energy(0, 0, 0, 1);
dif_mult[1]=1.0/wf_filter_energy(0, 1, 0, 24);
dif_mult[2]=1.0/wf_filter_energy(0, 24, 0, 420);
dif_mult[3]=1.0/wf_filter_energy(0, 420, 0, 4140);*/
float dfg_mulg[WF_GAUSS_PIRAMID_SIZE]={1.235223, 19.813868, 365.148407, 7208.362793/3.0};
/* dif_mulg[0]=1.0/wf_filter_energy(0, 0, 1, 1);
dif_mulg[1]=1.0/wf_filter_energy(1, 1, 1, 24);
dif_mulg[2]=1.0/wf_filter_energy(1, 24, 1, 420);
dif_mulg[3]=1.0/wf_filter_energy(1, 420, 1, 4140);*/
float dfg_mlcc[WF_GAUSS_PIRAMID_SIZE][4];
long int dfg_dmax[WF_GAUSS_PIRAMID_SIZE][4];
int green_mode;
if ( x_green_flag && (imgdata.idata.cdesc[FC(0, 0)] == imgdata.idata.cdesc[FC(1, 1)]) )
green_mode = WF_GREENMODE_GX_XG;
else if ( x_green_flag && (imgdata.idata.cdesc[FC(0, 1)] == imgdata.idata.cdesc[FC(1, 0)]) )
green_mode = WF_GREENMODE_XG_GX;
else
green_mode = WF_GREENMODE_IND;
for(int l=0; l<WF_GAUSS_PIRAMID_SIZE; l++)
{
switch (green_mode)
{
case WF_GREENMODE_GX_XG:
dfg_mlcc[l][0]=dfg_mlcc[l][3]=dfg_mulg[l];
dfg_dmax[l][0]=dfg_dmax[l][3]=65535/dfg_mulg[l];
dfg_mlcc[l][1]=dfg_mlcc[l][2]=dfg_mult[l];
dfg_dmax[l][1]=dfg_dmax[l][2]=65535/dfg_mult[l];
break;
case WF_GREENMODE_XG_GX:
dfg_mlcc[l][1]=dfg_mlcc[l][2]=dfg_mulg[l];
dfg_dmax[l][1]=dfg_dmax[l][2]=65535/dfg_mulg[l];
dfg_mlcc[l][0]=dfg_mlcc[l][3]=dfg_mult[l];
dfg_dmax[l][0]=dfg_dmax[l][3]=65535/dfg_mult[l];
break;
case WF_GREENMODE_IND:
dfg_mlcc[l][0]=dfg_mlcc[l][1]=dfg_mlcc[l][2]=dfg_mlcc[l][3]=dfg_mult[l];
dfg_dmax[l][0]=dfg_dmax[l][1]=dfg_dmax[l][2]=dfg_dmax[l][3]=65535/dfg_mult[l];
break;
}
}
// laplasyan energy
for(int row_d2=0; row_d2<height_p1_d2; row_d2++)
{
int row, row_p1;
ushort *src[4];
ushort *gau[WF_GAUSS_PIRAMID_SIZE][4];
ushort *dfg[WF_GAUSS_PIRAMID_SIZE][4];
row = row_d2*2;
row_p1 = row+1;
WF_i_1TO4 src[i] = &WF_BAYERSRC((i<2)?row:row_p1, i&1, FC((i<2)?row:row_p1, i&1));
if (row_p1==S.height)
src[2]=src[3]=dummy_line;
for(int l=0; l<WF_GAUSS_PIRAMID_SIZE; l++)
{
WF_i_1TO4 gau[l][i] = &WF_BAYERGAU(l, (i<2)?row:row_p1, i&1);
WF_i_1TO4 dfg[l][i] = &WF_BAYERDFG(l, (i<2)?row:row_p1, i&1);
if ((row+1)==S.height)
dfg[l][2]=dfg[l][3]=gau[l][2]=gau[l][3]=dummy_line;
}
ushort *src_first, *src_last, *src_last2;
src_first = &WF_BAYERSRC(row, 0, FC(row, 0));
src_last = &WF_BAYERSRC(row, width_d2*2-2, FC(row, 0));
src_last2 = &WF_BAYERSRC(row, width_p1_d2*2-2, FC(row, 0));
do
{
long int val_gau[4];
long int val_dif[4];
long int val_src[4];
WF_i_1TO4 val_src[i]=*src[i];
for(int l=0; l<WF_GAUSS_PIRAMID_SIZE; l++)
{
WF_i_1TO4 val_gau[i]=*gau[l][i];
WF_i_1TO4 val_dif[i]=val_src[i]-val_gau[i];
WF_i_1TO4 val_src[i]=val_gau[i];
WF_i_1TO4 val_dif[i]*=val_dif[i];
WF_i_1TO4
if(val_dif[i]<dfg_dmax[l][i])
{
val_dif[i]*=dfg_mlcc[l][i];
*dfg[l][i] =val_dif[i];
}
else
{
*dfg[l][i]=65535;
}
}
// Next 4 pixel or exit
if (src[0]<src_last)
{
WF_i_1TO4 src[i]+=8;
for (int l=0; l<WF_GAUSS_PIRAMID_SIZE; l++)
WF_i_1TO4 gau[l][i]+=2;
for (int l=0; l<WF_GAUSS_PIRAMID_SIZE; l++)
WF_i_1TO4 dfg[l][i]+=2;
}
else if(src[0]>src_first && src[0]<src_last2)
{
WF_i_1TO4 src[i]=i&1?&dummy_pixel:src[i]+8;
for (int l=0; l<WF_GAUSS_PIRAMID_SIZE; l++)
WF_i_1TO4 gau[l][i]=i&1?&dummy_pixel:gau[l][i]+2;
for (int l=0; l<WF_GAUSS_PIRAMID_SIZE; l++)
WF_i_1TO4 dfg[l][i]=i&1?&dummy_pixel:dfg[l][i]+2;
}
else break;
}
while(1);
}
int radius2x32 [3]={32, 32, 0};
int radius2x56 [3]={56, 56, 0};
int radius2x90 [3]={90, 90, 0};
int radius2x104[3]={104, 104, 0};
if (x_green_flag)
{
for(int i=0;i<4;i++)
wf_bayer4_green_blur (0, difwg_pyramid[i], WF_IMGMODE_BAYER1PLANE, difwg_pyramid[i], WF_IMGMODE_BAYER1PLANE);
}
wf_bayer4_block_filter (radius2x32, difwg_pyramid[0], WF_IMGMODE_BAYER1PLANE, difwg_pyramid[0], WF_IMGMODE_BAYER1PLANE);
wf_bayer4_block_filter (radius2x56, difwg_pyramid[1], WF_IMGMODE_BAYER1PLANE, difwg_pyramid[1], WF_IMGMODE_BAYER1PLANE);
wf_bayer4_block_filter (radius2x90, difwg_pyramid[2], WF_IMGMODE_BAYER1PLANE, difwg_pyramid[2], WF_IMGMODE_BAYER1PLANE);
wf_bayer4_block_filter (radius2x104, difwg_pyramid[3], WF_IMGMODE_BAYER1PLANE, difwg_pyramid[3], WF_IMGMODE_BAYER1PLANE);
float (*banding_col)[4];
float (*banding_row)[4];
float (*banding_col_count)[4];
float (*banding_row_count)[4];
banding_col = (float(*)[4])calloc(height_p1_d2, sizeof(float)*4);
banding_col_count = (float(*)[4])calloc(height_p1_d2, sizeof(float)*4);
banding_row = (float(*)[4])calloc(width_p1_d2, sizeof(float)*4);
banding_row_count = (float(*)[4])calloc(width_p1_d2, sizeof(float)*4);
for(int row_d2=0; row_d2<height_p1_d2; row_d2++)
WF_i_1TO4 banding_col[row_d2][i]=banding_col_count[row_d2][i]=0;
for(int col_d2=0; col_d2<width_p1_d2; col_d2++)
WF_i_1TO4 banding_row[col_d2][i]=banding_row_count[col_d2][i]=0;
long int val_accepted;
float treshold[4];
WF_i_1TO4 treshold[i]=imgdata.params.wf_deband_treshold[FC(i>>1,i&1)];
val_accepted = val_max-3*MAX(MAX(treshold[0],treshold[1]),MAX(treshold[2],treshold[3]));
float (*tr_weight)[4];
tr_weight=(float(*)[4])calloc(WF_MAXTRESHOLD*4, sizeof(float));
WF_i_1TO4 treshold[i]*=data_mult;
for(int v=0; v<WF_MAXTRESHOLD; v++)
{
for(int i=0; i<4; i++)
{
if (v<treshold[i]*treshold[i])
tr_weight[v][i] = 1.0;
else if (v*5<6*treshold[i]*treshold[i])
tr_weight[v][i] = 6.0-5.0*float(v)/(treshold[i]*treshold[i]);
else
tr_weight[v][i] = 0.0;
}
}
for(int row_d2=0; row_d2<height_p1_d2; row_d2++)
{
int row, row_p1;
ushort *src[4];
ushort *gau[WF_GAUSS_PIRAMID_SIZE][4];
ushort *dfg[WF_GAUSS_PIRAMID_SIZE][4];
row = row_d2*2;
row_p1 = row+1;
WF_i_1TO4 src[i] = &WF_BAYERSRC((i<2)?row:row_p1, i&1, FC((i<2)?row:row_p1, i&1));
if (row_p1==S.height)
src[2]=src[3]=dummy_line;
for(int l=0; l<WF_GAUSS_PIRAMID_SIZE; l++)
{
WF_i_1TO4 gau[l][i] = &WF_BAYERGAU(l, (i<2)?row:row_p1, i&1);
WF_i_1TO4 dfg[l][i] = &WF_BAYERDFG(l, (i<2)?row:row_p1, i&1);
if (row_p1==S.height)
dfg[l][2]=dfg[l][3]=gau[l][2]=gau[l][3]=dummy_line;
}
ushort *src_first, *src_last, *src_last2;
src_first = &WF_BAYERSRC(row, 0, FC(row, 0));
src_last = &WF_BAYERSRC(row, width_d2*2-2, FC(row, 0));
src_last2 = &WF_BAYERSRC(row, width_p1_d2*2-2, FC(row, 0));
int col_d2 = 0;
do
{
float val_src[4];
float bsum[4]={0,0,0,0};
float wsum[4]={0,0,0,0};
WF_i_1TO4 val_src[i]=*src[i];
for(int l=0; l<WF_GAUSS_PIRAMID_SIZE; l++)
{
float val_dif[4];
float val_gau[4];
float wght[4];
WF_i_1TO4 val_gau[i] = *gau[l][i];
WF_i_1TO4 val_dif[i] = val_src[i]-val_gau[i];
WF_i_1TO4 val_src[i] = val_gau[i];
WF_i_1TO4 wght[i] = tr_weight[*dfg[l][i]][i];
WF_i_1TO4 wsum[i] += wght[i];
WF_i_1TO4 bsum[i] += wght[i]*val_dif[i];
}
//WF_i_1TO4 *src[i]=bsum[i];
WF_i_1TO4 wsum[i]*=wsum[i];
WF_i_1TO4 banding_col [row_d2][i] += bsum[i]*wsum[i];
WF_i_1TO4 banding_col_count[row_d2][i] += wsum[i];
WF_i_1TO4 banding_row [col_d2][i] += bsum[i]*wsum[i];
WF_i_1TO4 banding_row_count[col_d2][i] += wsum[i];
// Next 4 pixel or exit
if (src[0]<src_last)
{
WF_i_1TO4 src[i]+=8;
for (int l=0; l<WF_GAUSS_PIRAMID_SIZE; l++)
WF_i_1TO4 gau[l][i]+=2;
for (int l=0; l<WF_GAUSS_PIRAMID_SIZE; l++)
WF_i_1TO4 dfg[l][i]+=2;
}
else if(src[0]>src_first && src[0]<src_last2)
{
WF_i_1TO4 src[i]=i&1?&dummy_pixel:src[i]+8;
for (int l=0; l<WF_GAUSS_PIRAMID_SIZE; l++)
WF_i_1TO4 gau[l][i]=i&1?&dummy_pixel:gau[l][i]+2;
for (int l=0; l<WF_GAUSS_PIRAMID_SIZE; l++)
WF_i_1TO4 dfg[l][i]=i&1?&dummy_pixel:dfg[l][i]+2;
}
else break;
col_d2++;
}
while(1);
}
float bsum[4], bmean[4];
int (*banding_col_i)[4];
int (*banding_row_i)[4];
banding_col_i = (int(*)[4])calloc(height_p1_d2, sizeof(int)*4);
banding_row_i = (int(*)[4])calloc(width_p1_d2, sizeof(int)*4);
// cols
WF_i_1TO4 bsum[i]=bmean[i]=0;
for(int row_d2=0; row_d2<height_p1_d2; row_d2++)
for(int i=0; i<4; i++)
{
if (banding_col_count[row_d2][i]>0)
{
banding_col[row_d2][i]=banding_col[row_d2][i]/banding_col_count[row_d2][i];
bsum[i]+=banding_col[row_d2][i];
}
}
WF_i_1TO4 bmean[i]=bsum[i]/(i<2?height_d2:height_p1_d2);
for(int row_d2=0; row_d2<height_p1_d2; row_d2++)
for(int i=0; i<4; i++)
banding_col_i[row_d2][i]=int(banding_col[row_d2][i]-bmean[i]);
// rows
WF_i_1TO4 bsum[i]=bmean[i]=0;
for(int col_d2=0; col_d2<width_p1_d2; col_d2++)
for(int i=0; i<4; i++)
{
if (banding_row_count[col_d2][i]>0)
{
banding_row[col_d2][i]=(banding_row[col_d2][i]/banding_row_count[col_d2][i]);
bsum[i]+=banding_row[col_d2][i];
}
}
WF_i_1TO4 bmean[i]=bsum[i]/(i<2?width_d2:width_p1_d2);
for(int col_d2=0; col_d2<width_p1_d2; col_d2++)
for(int i=0; i<4; i++)
if (banding_row_count[col_d2][i]>0)
banding_row_i[col_d2][i]=int(banding_row[col_d2][i]-bmean[i]);
for(int row_d2=0; row_d2<height_p1_d2; row_d2++)
{
int row, row_p1;
ushort *src[4];
ushort *src_first, *src_plast, *src_last;
row = row_d2*2;
row_p1 = row+1;
WF_i_1TO4 src[i] = &WF_BAYERSRC((i<2)?row:row_p1, i&1, FC((i<2)?row:row_p1, i&1));
if (row_p1==S.height)
src[2]=src[3]=dummy_line;
src_first = &WF_BAYERSRC(row, 0, FC(row, 0));
src_plast = &WF_BAYERSRC(row, width_d2*2-2, FC(row, 0));
src_last = &WF_BAYERSRC(row, width_p1_d2*2-2, FC(row, 0));
int col_d2=0;
do
{
// Do
int val_new[4];
WF_i_1TO4 val_new[i]=*src[i];
WF_i_1TO4 val_new[i]-=banding_col_i[row_d2][i];
WF_i_1TO4 val_new[i]-=banding_row_i[col_d2][i];
for(int i=0; i<4; i++)
{
if (*src[i]>=val_accepted)
{
val_new[i]=*src[i]>>data_shift;
}
else
{
if (val_new[i]>val_max)
val_new[i]=val_max;
else if (val_new[i]<0)
val_new[i]=0;
val_new[i]>>=data_shift;
}
}
WF_i_1TO4 *src[i]=val_new[i];
// Next 4 pixel or exit
if (src[0]<src_plast)
{
WF_i_1TO4 src[i]+=8;
}
else if(src[0]>src_first && src[0]<src_last)
{
WF_i_1TO4 src[i]=i&1?&dummy_pixel:src[i]+8;
}
else break;
col_d2++;
}
while(1);
}
free(banding_col_i);
free(banding_row_i);
free(tr_weight);
free(banding_col);
free(banding_col_count);
free(banding_row);
free(banding_row_count);
for(int i=0; i<WF_GAUSS_PIRAMID_SIZE; i++)
{
free(gauss_pyramid[i]);
free(difwg_pyramid[i]);
}
free(dummy_line);
return WF_DEBANDING_OK;
}
double LibRaw::wf_filter_energy(int r1_greenmode, int r1, int r2_greenmode, int r2)
{
/*
This function caclulates energy of laplasyan piramid level.
Laplasyan level is difference between two 2D gaussian (exactly, binominal) convolutions with radius r1 and r2
Convolution is done on bayer data, 4 channels, and if (greenmode), additive on green channel.
Not optimized, because now it's used only for precalculations.
*/
#define WF_MAXFILTERSIZE 10000
int rmin, rmax;
int rmin_greenmode, rmax_greenmode;
if (r1>r2)
{
rmax=r1;
rmin=r2;
rmax_greenmode=r1_greenmode;
rmin_greenmode=r2_greenmode;
}
else
{
rmax=r2;
rmin=r1;
rmax_greenmode=r2_greenmode;
rmin_greenmode=r1_greenmode;
}
int rmin_x2_p1, rmax_x2_p1;
rmin_x2_p1=rmin*2+1;
rmax_x2_p1=rmax*2+1;
double gau_kernel_rmin[WF_MAXFILTERSIZE];
double gau_kernel_rmax[WF_MAXFILTERSIZE];
for(int i=0; i<rmax_x2_p1; i++)
gau_kernel_rmin[i]=0;
gau_kernel_rmin[1]=1.0;
for(int i=2; i<=rmin_x2_p1; i++)
{
for(int j=i; j>0; j--)
gau_kernel_rmin[j]=0.5*(gau_kernel_rmin[j]+gau_kernel_rmin[j-1]);
}
for(int i=0; i<=rmax_x2_p1; i++)
gau_kernel_rmax[i]=gau_kernel_rmin[i];
for(int i=rmin_x2_p1+1; i<=rmax_x2_p1; i++)
{
for(int j=i; j>0; j--)
gau_kernel_rmax[j]=0.5*(gau_kernel_rmax[j]+gau_kernel_rmax[j-1]);
}
double wmin_sum, wmax_sum, energy_sum;
wmin_sum=0;
wmax_sum=0;
energy_sum=0;
for(int row=-rmax*2-1; row<=rmax*2+1; row++)
{
for(int col=-rmax*2-1; col<=rmax*2+1; col++)
{
double wght_rmax=0;
double wght_rmin=0;
#define WF_WMAX(row, col) (((abs(row)<=rmax*2)&&(abs(col)<=rmax*2))?gau_kernel_rmax[abs(row)/2+rmax+1]*gau_kernel_rmax[abs(col)/2+rmax+1]:0)
#define WF_WMIN(row, col) (((abs(row)<=rmin*2)&&(abs(col)<=rmin*2))?gau_kernel_rmin[abs(row)/2+rmin+1]*gau_kernel_rmin[abs(col)/2+rmin+1]:0)
if ( ((row&1)==0) && ((col&1)==0))
{
wght_rmax = WF_WMAX(row, col);
wght_rmin = WF_WMIN(row, col);
}
if (rmax_greenmode)
{
if ( ((row&1)==0) && ((col&1)==0))
wght_rmax = 0.5*wght_rmax;
else if ( ((row&1)==1) && ((col&1)==1))
{
wght_rmax = 0.125*(WF_WMAX(row-1, col-1)+WF_WMAX(row-1, col+1)+WF_WMAX(row+1, col-1)+WF_WMAX(row+1, col+1));
}
}
if (rmin_greenmode)
{
if ( ((row&1)==0) && ((col&1)==0))
wght_rmin = 0.5*wght_rmin;
else if ( ((row&1)==1) && ((col&1)==1))
{
wght_rmin = 0.125*(WF_WMIN(row-1, col-1)+WF_WMIN(row-1, col+1)+WF_WMIN(row+1, col-1)+WF_WMIN(row+1, col+1));
}
}
wmin_sum+=wght_rmin;
wmax_sum+=wght_rmax;
energy_sum+=(wght_rmax-wght_rmin)*(wght_rmax-wght_rmin);
}
}
return energy_sum;
}
void LibRaw::wf_bayer4_green_blur(int mode, void* src_image, int src_imgmode, void* dst_image, int dst_imgmode)
{
/*
This function filters green (or any "diagonal") channel of bayer4 data with "X" kernel,
1 1
4
1 1
*/
#define WF_BAYERSRC4(row, col, c) ((ushort(*)[4])src_image)[((row) >> IO.shrink)*S.iwidth + ((col) >> IO.shrink)][c]
#define WF_BAYERSRC1(row, col) ((ushort*)src_image) [((row) >> IO.shrink)*S.iwidth + ((col) >> IO.shrink)]
#define WF_BAYERDST4(row, col, c) ((ushort(*)[4])dst_image)[((row) >> IO.shrink)*S.iwidth + ((col) >> IO.shrink)][c]
#define WF_BAYERDST1(row, col) ((ushort*)dst_image) [((row) >> IO.shrink)*S.iwidth + ((col) >> IO.shrink)]
int green_mode;
if ( imgdata.idata.cdesc[FC(0, 0)] == imgdata.idata.cdesc[FC(1, 1)] )
green_mode = WF_GREENMODE_GX_XG;
else if ( imgdata.idata.cdesc[FC(0, 1)] == imgdata.idata.cdesc[FC(1, 0)] )
green_mode = WF_GREENMODE_XG_GX;
else
green_mode = WF_GREENMODE_IND;
int src_h_shift, dst_h_shift, src_h_shift_x2;
if (src_imgmode == WF_IMGMODE_BAYER1PLANE)
src_h_shift = 2 >> IO.shrink;
else if (src_imgmode == WF_IMGMODE_BAYER4PLANE)
src_h_shift = 8 >> IO.shrink;
src_h_shift_x2 = src_h_shift*2;
if (dst_imgmode == WF_IMGMODE_BAYER1PLANE)
dst_h_shift = 2 >> IO.shrink;
else if (dst_imgmode == WF_IMGMODE_BAYER4PLANE)
dst_h_shift = 8 >> IO.shrink;
int row, col;
long int *line_filtered;
line_filtered = (long int*) calloc(S.width, sizeof(*line_filtered));
ushort *src, *src_c, *src_u1, *src_u2, *src_d1, *src_d2, *dst_c, *src_ca, *dst_ca, *dst_rb;
int start_col, start_col_left, row_up, row_dn;
if ( green_mode != WF_GREENMODE_IND)
{
for(row=0; row<S.height; row++)
{
if (row == 0)
row_up = 1;
else
row_up = row-1;
if (row == S.height-1)
row_dn = S.height-2;
else
row_dn = row+1;
if ( green_mode == WF_GREENMODE_GX_XG )
start_col = row & 1;
else
start_col = ( row+1 ) & 1;
if ( start_col == 0 )
start_col_left = 1;
else
start_col_left = 0;
switch (src_imgmode)
{
case WF_IMGMODE_BAYER1PLANE:
src_c = &WF_BAYERSRC1(row, start_col);
src_u1 = &WF_BAYERSRC1(row_up, start_col_left);
src_d1 = &WF_BAYERSRC1(row_dn, start_col_left);
src_u2 = &WF_BAYERSRC1(row_up, start_col+1);
src_d2 = &WF_BAYERSRC1(row_dn, start_col+1);
break;
case WF_IMGMODE_BAYER4PLANE:
src_c = &WF_BAYERSRC4(row, start_col, FC(row, start_col));
src_u1 = &WF_BAYERSRC4(row_up, start_col_left, FC(row_up, start_col_left));
src_d1 = &WF_BAYERSRC4(row_dn, start_col_left, FC(row_dn, start_col_left));
src_u2 = &WF_BAYERSRC4(row_up, start_col+1, FC(row_up, start_col+1));
src_d2 = &WF_BAYERSRC4(row_dn, start_col+1, FC(row_dn, start_col+1));
break;
}
long int sum_l1, sum_l2;
sum_l1 = *src_u1 + *src_d1;
sum_l2 = *src_u2 + *src_d2;
if (start_col == 0)
{
// Edges
line_filtered[start_col] = sum_l1 + sum_l2 + (*src_c)*4;
src_u2 += src_h_shift;
src_d2 += src_h_shift;
sum_l2 = *src_u2 + *src_d2;
src_c += src_h_shift;
start_col=2;
}
int width_m_3 = S.width-3;
// Main
for (col=start_col; col<width_m_3; col+=2)
{
line_filtered[col] = sum_l1 + sum_l2 + 4*(*src_c);
src_u1 += src_h_shift_x2;
src_d1 += src_h_shift_x2;
sum_l1 = *src_u1 + *src_d1;
src_c += src_h_shift;
col+=2;
line_filtered[col] = sum_l1 + sum_l2 + 4*(*src_c);
src_u2 += src_h_shift_x2;
src_d2 += src_h_shift_x2;
sum_l2 = *src_u2 + *src_d2;
src_c += src_h_shift;
}
// Right edge
if (col == S.width-1)
{
line_filtered[col] = 2*sum_l1 + 4*(*src_c);
}
else if (col == S.width-2)
{
line_filtered[col] = sum_l1 + sum_l2 + 4*(*src_c);
}
else if (col == S.width-3)
{
line_filtered[col] = sum_l1 + sum_l2 + 4*(*src_c);
src_c += src_h_shift;
col+=2;
line_filtered[col] = 2*sum_l2 + 4*(*src_c);
}
if (row>0)
{
if ( green_mode == WF_GREENMODE_GX_XG )
start_col = ( row+1 ) & 1;
else
start_col = row & 1;
switch (dst_imgmode)
{
case WF_IMGMODE_BAYER1PLANE:
dst_c = &WF_BAYERDST1(row-1, start_col);
break;
case WF_IMGMODE_BAYER4PLANE:
dst_c = &WF_BAYERDST4(row-1, start_col, FC(row-1, start_col));
break;
}
for (col=start_col; col<S.width; col+=2)
{
*dst_c=(line_filtered[col])>>3;
dst_c+=dst_h_shift;
}
if (src_image != dst_image)
{
// copy red or blue channel
if ( green_mode == WF_GREENMODE_GX_XG )
start_col = row & 1;
else
start_col = (row+1) & 1;
switch (src_imgmode)
{
case WF_IMGMODE_BAYER1PLANE:
src = &WF_BAYERSRC1(row-1, start_col);
break;
case WF_IMGMODE_BAYER4PLANE:
src = &WF_BAYERSRC4(row-1, start_col, FC(row-1, start_col));
break;
}
switch (dst_imgmode)
{
case WF_IMGMODE_BAYER1PLANE: