-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathJavaImageFunctions.java
More file actions
4945 lines (4252 loc) · 251 KB
/
Copy pathJavaImageFunctions.java
File metadata and controls
4945 lines (4252 loc) · 251 KB
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
package net.sourceforge.opencamera;
import android.graphics.Bitmap;
//import android.util.Log;
import java.util.List;
public class JavaImageFunctions {
private static final String TAG = "JavaImageFunctions";
static class CreateMTBApplyFunction implements JavaImageProcessing.ApplyFunctionInterface {
private final boolean use_mtb;
private final int median_value;
CreateMTBApplyFunction(boolean use_mtb, int median_value) {
this.use_mtb = use_mtb;
this.median_value = median_value;
}
@Override
public void init(int n_threads) {
}
@Override
public void apply(JavaImageProcessing.CachedBitmap output, int thread_index, int off_x, int off_y, int this_width, int this_height) {
// unused
throw new RuntimeException("not implemented");
}
@Override
public void apply(JavaImageProcessing.CachedBitmap output, int thread_index, int [] pixels, int off_x, int off_y, int this_width, int this_height) {
int [] pixels_out = output.getCachedPixelsI();
if( use_mtb ) {
for(int y=off_y,c=0;y<off_y+this_height;y++) {
for(int x=off_x;x<off_x+this_width;x++,c++) {
// this code is performance critical; note it's faster to avoid calls to Color.red/green/blue()
int color = pixels[c];
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = color & 0xFF;
int value = Math.max(r, g);
value = Math.max(value, b);
// ignore small differences to reduce effect of noise - this helps testHDR22
int diff;
if( value > median_value )
diff = value - median_value;
else
diff = median_value - value;
if( diff <= 4 ) // should be same value as min_diff_c in HDRProcessor.autoAlignment()
pixels_out[c] = 127 << 24;
else if( value <= median_value )
pixels_out[c] = 0;
else
pixels_out[c] = 255 << 24;
}
}
}
else {
for(int y=off_y,c=0;y<off_y+this_height;y++) {
for(int x=off_x;x<off_x+this_width;x++,c++) {
// this code is performance critical; note it's faster to avoid calls to Color.red/green/blue()
int color = pixels[c];
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = color & 0xFF;
int value = Math.max(r, g);
value = Math.max(value, b);
pixels_out[c] = value << 24;
}
}
}
}
@Override
public void apply(JavaImageProcessing.CachedBitmap output, int thread_index, byte [] pixels, int off_x, int off_y, int this_width, int this_height) {
// unused
throw new RuntimeException("not implemented");
}
}
static class AlignMTBApplyFunction implements JavaImageProcessing.ApplyFunctionInterface {
private int [][] errors = null;
private final boolean use_mtb;
private final Bitmap bitmap0;
private JavaImageProcessing.FastAccessBitmap [] fast_bitmap0;
private final Bitmap bitmap1;
private JavaImageProcessing.FastAccessBitmap [] fast_bitmap1;
private final int offset_x, offset_y;
private final int step_size;
AlignMTBApplyFunction(boolean use_mtb, Bitmap bitmap0, Bitmap bitmap1, int offset_x, int offset_y, int step_size) {
this.use_mtb = use_mtb;
this.bitmap0 = bitmap0;
this.bitmap1 = bitmap1;
this.offset_x = offset_x;
this.offset_y = offset_y;
this.step_size = step_size;
}
@Override
public void init(int n_threads) {
errors = new int[n_threads][];
fast_bitmap0 = new JavaImageProcessing.FastAccessBitmap[n_threads];
fast_bitmap1 = new JavaImageProcessing.FastAccessBitmap[n_threads];
for(int i=0;i<n_threads;i++) {
fast_bitmap0[i] = new JavaImageProcessing.FastAccessBitmap(bitmap0);
fast_bitmap1[i] = new JavaImageProcessing.FastAccessBitmap(bitmap1);
}
}
@Override
public void apply(JavaImageProcessing.CachedBitmap output, int thread_index, int off_x, int off_y, int this_width, int this_height) {
if( errors[thread_index] == null )
errors[thread_index] = new int[9];
/* We want to sample every step_size'th pixel. Because this is awkward to set up (and wasn't possible
in renderscript version), instead we fake it by sampling over an input bitmap of size
(width/step_size, height/step_size), and then scaling the coordinates by step_size.
The reason we want to sample every step_size'th pixel is it's good enough for the algorithm to work,
and is much faster.
*/
int bitmap0_width = bitmap0.getWidth();
int bitmap1_width = bitmap1.getWidth();
int bitmap1_height = bitmap1.getHeight();
if( use_mtb ) {
int sy = off_y, ey = off_y+this_height;
while( sy*step_size+offset_y < step_size )
sy++;
while( (ey-1)*step_size+offset_y >= bitmap1_height-step_size )
ey--;
for(int cy=sy;cy<ey;cy++) {
int y = cy*step_size;
int y_plus_offset = y+offset_y;
fast_bitmap0[thread_index].getPixel(0, y); // force cache to cover rows needed by this row
int bitmap0_cache_y = fast_bitmap0[thread_index].getCacheY();
int y_rel_bitmap0_cache = y-bitmap0_cache_y;
int [] bitmap0_cache_pixels = fast_bitmap0[thread_index].getCachedPixelsI();
fast_bitmap1[thread_index].ensureCache(y_plus_offset-step_size, y_plus_offset+step_size); // force cache to cover rows needed by this row
int bitmap1_cache_y = fast_bitmap1[thread_index].getCacheY();
int y_rel_bitmap1_cache = y-bitmap1_cache_y;
int [] bitmap1_cache_pixels = fast_bitmap1[thread_index].getCachedPixelsI();
int y_rel_bitmap1_cache_plus_offset = y_rel_bitmap1_cache+offset_y;
int sx = off_x, ex = off_x+this_width;
while( sx*step_size+offset_x < step_size )
sx++;
while( (ex-1)*step_size+offset_x >= bitmap1_width-step_size )
ex--;
for(int cx=sx;cx<ex;cx++) {
int x = cx*step_size;
int x_plus_offset = x+offset_x;
//if( x_plus_offset >= step_size && x_plus_offset < bitmap1_width-step_size && y_plus_offset >= step_size && y_plus_offset < bitmap1_height-step_size )
{
//int pixel0 = fast_bitmap0[thread_index].getPixel(x, y) >>> 24;
int pixel0 = bitmap0_cache_pixels[y_rel_bitmap0_cache*bitmap0_width+x] >>> 24;
/*int c=0;
for(int dy=-1;dy<=1;dy++) {
for(int dx=-1;dx<=1;dx++) {
int pixel1 = fast_bitmap1[thread_index].getPixel(x_plus_offset+dx*step_size, y_plus_offset+dy*step_size) >>> 24;
if( pixel0 != pixel1 && pixel0 != 127 && pixel1 != 127 ) {
// check against 127 to ignore noise - see CreateMTBApplyFunction
errors[thread_index][c]++;
}
c++;
}
}*/
// unroll loops
// check against 127 to ignore noise - see CreateMTBApplyFunction
int pixel1;
//pixel1 = fast_bitmap1[thread_index].getPixel(x_plus_offset-step_size, y_plus_offset-step_size) >>> 24;
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset-step_size)*bitmap1_width+(x_plus_offset-step_size)] >>> 24;
if( pixel0 != pixel1 && pixel0 != 127 && pixel1 != 127 ) {
errors[thread_index][0]++;
}
//pixel1 = fast_bitmap1[thread_index].getPixel(x_plus_offset, y_plus_offset-step_size) >>> 24;
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset-step_size)*bitmap1_width+(x_plus_offset)] >>> 24;
if( pixel0 != pixel1 && pixel0 != 127 && pixel1 != 127 ) {
errors[thread_index][1]++;
}
//pixel1 = fast_bitmap1[thread_index].getPixel(x_plus_offset+step_size, y_plus_offset-step_size) >>> 24;
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset-step_size)*bitmap1_width+(x_plus_offset+step_size)] >>> 24;
if( pixel0 != pixel1 && pixel0 != 127 && pixel1 != 127 ) {
errors[thread_index][2]++;
}
//pixel1 = fast_bitmap1[thread_index].getPixel(x_plus_offset-step_size, y_plus_offset) >>> 24;
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset)*bitmap1_width+(x_plus_offset-step_size)] >>> 24;
if( pixel0 != pixel1 && pixel0 != 127 && pixel1 != 127 ) {
errors[thread_index][3]++;
}
//pixel1 = fast_bitmap1[thread_index].getPixel(x_plus_offset, y_plus_offset) >>> 24;
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset)*bitmap1_width+(x_plus_offset)] >>> 24;
if( pixel0 != pixel1 && pixel0 != 127 && pixel1 != 127 ) {
errors[thread_index][4]++;
}
//pixel1 = fast_bitmap1[thread_index].getPixel(x_plus_offset+step_size, y_plus_offset) >>> 24;
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset)*bitmap1_width+(x_plus_offset+step_size)] >>> 24;
if( pixel0 != pixel1 && pixel0 != 127 && pixel1 != 127 ) {
errors[thread_index][5]++;
}
//pixel1 = fast_bitmap1[thread_index].getPixel(x_plus_offset-step_size, y_plus_offset+step_size) >>> 24;
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset+step_size)*bitmap1_width+(x_plus_offset-step_size)] >>> 24;
if( pixel0 != pixel1 && pixel0 != 127 && pixel1 != 127 ) {
errors[thread_index][6]++;
}
//pixel1 = fast_bitmap1[thread_index].getPixel(x_plus_offset, y_plus_offset+step_size) >>> 24;
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset+step_size)*bitmap1_width+(x_plus_offset)] >>> 24;
if( pixel0 != pixel1 && pixel0 != 127 && pixel1 != 127 ) {
errors[thread_index][7]++;
}
//pixel1 = fast_bitmap1[thread_index].getPixel(x_plus_offset+step_size, y_plus_offset+step_size) >>> 24;
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset+step_size)*bitmap1_width+(x_plus_offset+step_size)] >>> 24;
if( pixel0 != pixel1 && pixel0 != 127 && pixel1 != 127 ) {
errors[thread_index][8]++;
}
}
}
}
}
else {
int sy = off_y, ey = off_y+this_height;
while( sy*step_size+offset_y < step_size )
sy++;
while( (ey-1)*step_size+offset_y >= bitmap1_height-step_size )
ey--;
for(int cy=sy;cy<ey;cy++) {
//for(int cy=off_y;cy<off_y+this_height;cy++) {
int y = cy*step_size;
int y_plus_offset = y+offset_y;
fast_bitmap0[thread_index].getPixel(0, y); // force cache to cover rows needed by this row
int bitmap0_cache_y = fast_bitmap0[thread_index].getCacheY();
int y_rel_bitmap0_cache = y-bitmap0_cache_y;
int [] bitmap0_cache_pixels = fast_bitmap0[thread_index].getCachedPixelsI();
fast_bitmap1[thread_index].ensureCache(y_plus_offset-step_size, y_plus_offset+step_size); // force cache to cover rows needed by this row
int bitmap1_cache_y = fast_bitmap1[thread_index].getCacheY();
int y_rel_bitmap1_cache = y-bitmap1_cache_y;
int [] bitmap1_cache_pixels = fast_bitmap1[thread_index].getCachedPixelsI();
int y_rel_bitmap1_cache_plus_offset = y_rel_bitmap1_cache+offset_y;
int sx = off_x, ex = off_x+this_width;
while( sx*step_size+offset_x < step_size )
sx++;
while( (ex-1)*step_size+offset_x >= bitmap1_width-step_size )
ex--;
for(int cx=sx;cx<ex;cx++) {
//for(int cx=off_x;cx<off_x+this_width;cx++) {
int x = cx*step_size;
int x_plus_offset = x+offset_x;
//if( x_plus_offset >= step_size && x_plus_offset < bitmap1_width-step_size && y_plus_offset >= step_size && y_plus_offset < bitmap1_height-step_size )
{
//int pixel0 = fast_bitmap0[thread_index].getPixel(x, y) >>> 24;
int pixel0 = bitmap0_cache_pixels[y_rel_bitmap0_cache*bitmap0_width+x] >>> 24;
/*if( MyDebug.LOG ) {
Log.d(TAG, "int = " + fast_bitmap0[thread_index].getPixel(x, y));
Log.d(TAG, "pixel0 = " + pixel0);
}*/
/*int c=0;
for(int dy=-1;dy<=1;dy++) {
for(int dx=-1;dx<=1;dx++) {
//int pixel1 = fast_bitmap1[thread_index].getPixel(x_plus_offset+dx*step_size, y_plus_offset+dy*step_size) >>> 24;
int pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset+dy*step_size)*bitmap1_width+(x_plus_offset+dx*step_size)] >>> 24;
int diff = pixel1 - pixel0;
//if( Math.abs(diff) > 255 )
// throw new RuntimeException("diff too high: " + diff);
int diff2 = diff*diff;
//diff2 = pixel0;
//if( MyDebug.LOG )
// Log.d(TAG, "diff = " + diff);
if( errors[thread_index][c] < 2000000000 ) { // avoid risk of overflow
errors[thread_index][c] += diff2;
}
c++;
}
}*/
// unroll loops
int pixel1;
int diff;
final int overflow_check_c = 2000000000;
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset-step_size)*bitmap1_width+(x_plus_offset-step_size)] >>> 24;
diff = pixel1 - pixel0;
//if( Math.abs(diff) > 255 )
// throw new RuntimeException("diff too high: " + diff);
if( errors[thread_index][0] < overflow_check_c ) { // avoid risk of overflow
errors[thread_index][0] += diff*diff;
}
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset-step_size)*bitmap1_width+(x_plus_offset)] >>> 24;
diff = pixel1 - pixel0;
//if( Math.abs(diff) > 255 )
// throw new RuntimeException("diff too high: " + diff);
if( errors[thread_index][1] < overflow_check_c ) { // avoid risk of overflow
errors[thread_index][1] += diff*diff;
}
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset-step_size)*bitmap1_width+(x_plus_offset+step_size)] >>> 24;
diff = pixel1 - pixel0;
//if( Math.abs(diff) > 255 )
// throw new RuntimeException("diff too high: " + diff);
if( errors[thread_index][2] < overflow_check_c ) { // avoid risk of overflow
errors[thread_index][2] += diff*diff;
}
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset)*bitmap1_width+(x_plus_offset-step_size)] >>> 24;
diff = pixel1 - pixel0;
//if( Math.abs(diff) > 255 )
// throw new RuntimeException("diff too high: " + diff);
if( errors[thread_index][3] < overflow_check_c ) { // avoid risk of overflow
errors[thread_index][3] += diff*diff;
}
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset)*bitmap1_width+(x_plus_offset)] >>> 24;
diff = pixel1 - pixel0;
//if( Math.abs(diff) > 255 )
// throw new RuntimeException("diff too high: " + diff);
if( errors[thread_index][4] < overflow_check_c ) { // avoid risk of overflow
errors[thread_index][4] += diff*diff;
}
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset)*bitmap1_width+(x_plus_offset+step_size)] >>> 24;
diff = pixel1 - pixel0;
//if( Math.abs(diff) > 255 )
// throw new RuntimeException("diff too high: " + diff);
if( errors[thread_index][5] < overflow_check_c ) { // avoid risk of overflow
errors[thread_index][5] += diff*diff;
}
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset+step_size)*bitmap1_width+(x_plus_offset-step_size)] >>> 24;
diff = pixel1 - pixel0;
//if( Math.abs(diff) > 255 )
// throw new RuntimeException("diff too high: " + diff);
if( errors[thread_index][6] < overflow_check_c ) { // avoid risk of overflow
errors[thread_index][6] += diff*diff;
}
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset+step_size)*bitmap1_width+(x_plus_offset)] >>> 24;
diff = pixel1 - pixel0;
//if( Math.abs(diff) > 255 )
// throw new RuntimeException("diff too high: " + diff);
if( errors[thread_index][7] < overflow_check_c ) { // avoid risk of overflow
errors[thread_index][7] += diff*diff;
}
pixel1 = bitmap1_cache_pixels[(y_rel_bitmap1_cache_plus_offset+step_size)*bitmap1_width+(x_plus_offset+step_size)] >>> 24;
diff = pixel1 - pixel0;
//if( Math.abs(diff) > 255 )
// throw new RuntimeException("diff too high: " + diff);
if( errors[thread_index][8] < overflow_check_c ) { // avoid risk of overflow
errors[thread_index][8] += diff*diff;
}
}
}
}
}
}
@Override
public void apply(JavaImageProcessing.CachedBitmap output, int thread_index, int [] pixels, int off_x, int off_y, int this_width, int this_height) {
// unused
throw new RuntimeException("not implemented");
}
@Override
public void apply(JavaImageProcessing.CachedBitmap output, int thread_index, byte [] pixels, int off_x, int off_y, int this_width, int this_height) {
// unused
throw new RuntimeException("not implemented");
}
int [] getErrors() {
int [] total_errors = new int[errors[0].length];
// for each errors, add its entries to the total errors
for(int [] error : errors) {
for (int j=0;j<error.length;j++) {
total_errors[j] += error[j];
}
}
return total_errors;
}
}
/* Simplified brighten algorithm for gain/gamma only, used for DRO algorithm.
*/
static class DROBrightenApplyFunction implements JavaImageProcessing.ApplyFunctionInterface {
private final float gain_A, gain_B; // see comments below
private final float gamma;
private final float low_x;
private final float mid_x;
private final float max_x;
private final float [] value_to_gamma_scale_lut = new float[256]; // look up table for performance
DROBrightenApplyFunction(float gain, float gamma, float low_x, float mid_x, float max_x) {
/* We want A and B s.t.:
float alpha = (value-low_x)/(mid_x-low_x);
float new_value = (1.0-alpha)*low_x + alpha*gain*mid_x;
We should be able to write this as new_value = A * value + B
alpha = value/(mid_x-low_x) - low_x/(mid_x-low_x)
new_value = low_x - value*low_x/(mid_x-low_x) + low_x^2/(mid_x-low_x) +
value*gain*mid_x/(mid_x-low_x) - gain*mid_x*low_x/(mid_x-low_x)
So A = (gain*mid_x - low_x)/(mid_x-low_x)
B = low_x + low_x^2/(mid_x-low_x) - gain*mid_x*low_x/(mid_x-low_x)
= (low_x*mid_x - low_x^2 + low_x^2 - gain*mid_x*low_x)/(mid_x-low_x)
= (low_x*mid_x - gain*mid_x*low_x)/(mid_x-low_x)
= low_x*mid_x*(1-gain)/(mid_x-low_x)
*/
this.gamma = gamma;
this.low_x = low_x;
this.mid_x = mid_x;
this.max_x = max_x;
if( mid_x > low_x ) {
this.gain_A = (gain * mid_x - low_x) / (mid_x - low_x);
this.gain_B = low_x*mid_x*(1.0f-gain)/ (mid_x - low_x);
}
else {
this.gain_A = 1.0f;
this.gain_B = 0.0f;
}
for(int value=0;value<256;value++) {
float new_value = (float)Math.pow(value/max_x, gamma) * 255.0f;
value_to_gamma_scale_lut[value] = new_value / value;
}
}
@Override
public void init(int n_threads) {
}
@Override
public void apply(JavaImageProcessing.CachedBitmap output, int thread_index, int off_x, int off_y, int this_width, int this_height) {
// unused
throw new RuntimeException("not implemented");
}
@Override
public void apply(JavaImageProcessing.CachedBitmap output, int thread_index, int [] pixels, int off_x, int off_y, int this_width, int this_height) {
int [] pixels_out = output.getCachedPixelsI();
for(int y=off_y,c=0;y<off_y+this_height;y++) {
for(int x=off_x;x<off_x+this_width;x++,c++) {
// this code is performance critical; note it's faster to avoid calls to Color.red/green/blue()
int color = pixels[c];
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = color & 0xFF;
float fr = r, fg = g, fb = b;
float value = Math.max(fr, fg);
value = Math.max(value, fb);
// apply piecewise function of gain vs gamma
if( value <= low_x ) {
// don't scale
}
else if( value <= mid_x ) {
//float alpha = (value-low_x)/(mid_x-low_x);
//float new_value = (1.0-alpha)*low_x + alpha*gain*mid_x;
// gain_A and gain_B should be set so that new_value meets the commented out code above
// This code is critical for performance!
fr *= (gain_A + gain_B/value);
fg *= (gain_A + gain_B/value);
fb *= (gain_A + gain_B/value);
}
else {
// use LUT for performance
/*float new_value = (float)Math.pow(value/max_x, gamma) * 255.0f;
float gamma_scale = new_value / value;*/
float gamma_scale = value_to_gamma_scale_lut[(int)(value+0.5f)];
fr *= gamma_scale;
fg *= gamma_scale;
fb *= gamma_scale;
}
r = (int)(fr+0.5f);
g = (int)(fg+0.5f);
b = (int)(fb+0.5f);
r = Math.max(0, Math.min(255, r));
g = Math.max(0, Math.min(255, g));
b = Math.max(0, Math.min(255, b));
// this code is performance critical; note it's faster to avoid calls to Color.argb()
pixels_out[c] = (255 << 24) | (r << 16) | (g << 8) | b;
}
}
}
@Override
public void apply(JavaImageProcessing.CachedBitmap output, int thread_index, byte [] pixels, int off_x, int off_y, int this_width, int this_height) {
byte [] pixels_out = output.getCachedPixelsB();
for(int y=off_y,c=0;y<off_y+this_height;y++) {
for(int x=off_x;x<off_x+this_width;x++,c+=4) {
int r = pixels[c];
int g = pixels[c+1];
int b = pixels[c+2];
// bytes are signed!
if( r < 0 )
r += 256;
if( g < 0 )
g += 256;
if( b < 0 )
b += 256;
float fr = r, fg = g, fb = b;
float value = Math.max(fr, fg);
value = Math.max(value, fb);
// apply piecewise function of gain vs gamma
if( value <= low_x ) {
// don't scale
}
else if( value <= mid_x ) {
//float alpha = (value-low_x)/(mid_x-low_x);
//float new_value = (1.0-alpha)*low_x + alpha*gain*mid_x;
// gain_A and gain_B should be set so that new_value meets the commented out code above
// This code is critical for performance!
fr *= (gain_A + gain_B/value);
fg *= (gain_A + gain_B/value);
fb *= (gain_A + gain_B/value);
}
else {
float new_value = (float)Math.pow(value/max_x, gamma) * 255.0f;
float gamma_scale = new_value / value;
fr *= gamma_scale;
fg *= gamma_scale;
fb *= gamma_scale;
}
r = (int)(fr+0.5f);
g = (int)(fg+0.5f);
b = (int)(fb+0.5f);
r = Math.max(0, Math.min(255, r));
g = Math.max(0, Math.min(255, g));
b = Math.max(0, Math.min(255, b));
pixels_out[c] = (byte)r;
pixels_out[c+1] = (byte)g;
pixels_out[c+2] = (byte)b;
pixels_out[c+3] = (byte)255;
}
}
}
}
/** Class to store floating point rgb values, along with luminance.
*/
private static class RGBf_luminance {
float fr, fg, fb;
float lum;
// set from RGB101010 format
/*void setRGB101010(int rgb) {
this.fr = (float)((rgb) & 0x3FF) / 4.0f;
this.fg = (float)((rgb >> 10) & 0x3FF) / 4.0f;
this.fb = (float)((rgb >> 20) & 0x3FF) / 4.0f;
this.lum = Math.max(Math.max(fr, fg), fb);
}*/
void setRGB(float fr, float fg, float fb) {
this.fr = fr;
this.fg = fg;
this.fb = fb;
this.lum = Math.max(Math.max(fr, fg), fb);
}
void setRGB(final float [] pixels_in_rgbf, int x, int y, int width) {
int indx = (y*width+x)*3;
setRGB(pixels_in_rgbf[indx], pixels_in_rgbf[indx+1], pixels_in_rgbf[indx+2]);
}
}
static class AvgApplyFunction implements JavaImageProcessing.ApplyFunctionInterface {
private final float [] pixels_rgbf; // output
private final Bitmap bitmap_new; // new bitmap being added to the input
private JavaImageProcessing.FastAccessBitmap [] fast_bitmap_new;
private final Bitmap bitmap_orig; // original bitmap (first image)
private JavaImageProcessing.FastAccessBitmap [] fast_bitmap_orig;
private final int width, height;
private final int offset_x_new, offset_y_new;
private final float avg_factor;
private final float wiener_C;
private final float wiener_C_cutoff;
final int radius = 2; // must be less than the radius we actually read from below
//final int n_pixels_c = 5; // number of pixels we read from
//final int [] sample_x = new int[]{-2, 2, 0, -2, 2};
//final int [] sample_y = new int[]{-2, -2, 0, 2, 2};
/*final float [] pixels_avg_fr;
final float [] pixels_avg_fg;
final float [] pixels_avg_fb;*/
AvgApplyFunction(float [] pixels_rgbf, Bitmap bitmap_new, Bitmap bitmap_orig, int width, int height, int offset_x_new, int offset_y_new, float avg_factor, float wiener_C, float wiener_C_cutoff) {
this.pixels_rgbf = pixels_rgbf;
this.bitmap_new = bitmap_new;
this.bitmap_orig = bitmap_orig;
this.width = width;
this.height = height;
this.offset_x_new = offset_x_new;
this.offset_y_new = offset_y_new;
this.avg_factor = avg_factor;
this.wiener_C = wiener_C;
this.wiener_C_cutoff = wiener_C_cutoff;
/*this.pixels_avg_fr = new float[width];
this.pixels_avg_fg = new float[width];
this.pixels_avg_fb = new float[width];*/
}
@Override
public void init(int n_threads) {
fast_bitmap_new = new JavaImageProcessing.FastAccessBitmap[n_threads];
fast_bitmap_orig = new JavaImageProcessing.FastAccessBitmap[n_threads];
for(int i=0;i<n_threads;i++) {
fast_bitmap_new[i] = new JavaImageProcessing.FastAccessBitmap(bitmap_new);
fast_bitmap_orig[i] = new JavaImageProcessing.FastAccessBitmap(bitmap_orig);
}
}
@Override
public void apply(JavaImageProcessing.CachedBitmap output, int thread_index, int off_x, int off_y, int this_width, int this_height) {
apply(output, thread_index, (int[]) null, off_x, off_y, this_width, this_height);
}
@Override
public void apply(JavaImageProcessing.CachedBitmap output, int thread_index, int [] pixels, int off_x, int off_y, int this_width, int this_height) {
final float avg_factorp1 = avg_factor+1.0f;
for(int y=off_y,c=0;y<off_y+this_height;y++) {
/*if( MyDebug.LOG )
Log.d(TAG, "y = " + y);*/
int pixels_rgbf_indx = 3*y*width;
if( y+offset_y_new < 0 || y+offset_y_new >= height ) {
if( pixels != null ) {
for(int x=off_x;x<off_x+this_width;x++,c++,pixels_rgbf_indx+=3) {
// this code is performance critical; note it's faster to avoid calls to Color.red/green/blue()
int color = pixels[c];
/*this.pixels_rgbf[3*(y*width + x)] = (float)((color >> 16) & 0xFF);
this.pixels_rgbf[3*(y*width + x)+1] = (float)((color >> 8) & 0xFF);
this.pixels_rgbf[3*(y*width + x)+2] = (float)(color & 0xFF);*/
this.pixels_rgbf[pixels_rgbf_indx] = (float)((color >> 16) & 0xFF);
this.pixels_rgbf[pixels_rgbf_indx+1] = (float)((color >> 8) & 0xFF);
this.pixels_rgbf[pixels_rgbf_indx+2] = (float)(color & 0xFF);
}
}
// else leave pixels_rgbf unchanged for this row
continue;
}
fast_bitmap_orig[thread_index].getPixel(0, Math.min(y+2, height-1)); // force cache to cover rows needed by this row
int bitmap_orig_cache_y = fast_bitmap_orig[thread_index].getCacheY();
int y_rel_bitmap_orig_cache = y-bitmap_orig_cache_y;
int [] bitmap_orig_cache_pixels = fast_bitmap_orig[thread_index].getCachedPixelsI();
int y_new = y+offset_y_new;
//fast_bitmap_new[thread_index].getPixel(0, y+offset_y_new); // force cache to cover row y
fast_bitmap_new[thread_index].getPixel(0, Math.min(y_new+2, height-1)); // force cache to cover rows needed by this row
int bitmap_new_cache_y = fast_bitmap_new[thread_index].getCacheY();
int y_rel_bitmap_new_cache = y_new-bitmap_new_cache_y;
int [] bitmap_new_cache_pixels = fast_bitmap_new[thread_index].getCachedPixelsI();
/*int x = off_x;
for(;x+offset_x_new < 0;x++,c++) {
// this code is performance critical; note it's faster to avoid calls to Color.red/green/blue()
int color = pixels[c];
this.pixels_rgbf[3*(y*width + x)] = (float)((color >> 16) & 0xFF);
this.pixels_rgbf[3*(y*width + x)+1] = (float)((color >> 8) & 0xFF);
this.pixels_rgbf[3*(y*width + x)+2] = (float)(color & 0xFF);
}
for(;x<off_x+this_width;x++,c++) {*/
/*int saved_c = c;
int saved_pixels_rgbf_indx = pixels_rgbf_indx;
if( pixels != null ) {
for(int x=off_x;x<off_x+this_width;x++,pixels_rgbf_indx+=3) {
// read from integer format
int color = pixels[c++];
pixels_avg_fr[x] = (float)((color >> 16) & 0xFF);
pixels_avg_fg[x] = (float)((color >> 8) & 0xFF);
pixels_avg_fb[x] = (float)(color & 0xFF);
}
}
else {
for(int x=off_x;x<off_x+this_width;x++,pixels_rgbf_indx+=3) {
// read from floating point format
pixels_avg_fr[x] = this.pixels_rgbf[pixels_rgbf_indx];
pixels_avg_fg[x] = this.pixels_rgbf[pixels_rgbf_indx+1];
pixels_avg_fb[x] = this.pixels_rgbf[pixels_rgbf_indx+2];
}
}
pixels_rgbf_indx = saved_pixels_rgbf_indx;
c = saved_c;*/
for(int x=off_x;x<off_x+this_width;x++,pixels_rgbf_indx+=3) {
// this code is performance critical; note it's faster to avoid calls to Color.red/green/blue()
/*int color = pixels[c];
float pixel_avg_fr = (float)((color >> 16) & 0xFF);
float pixel_avg_fg = (float)((color >> 8) & 0xFF);
float pixel_avg_fb = (float)(color & 0xFF);*/
float pixel_avg_fr, pixel_avg_fg, pixel_avg_fb;
if( pixels != null ) {
// read from integer format
int color = pixels[c++];
pixel_avg_fr = (float)((color >> 16) & 0xFF);
pixel_avg_fg = (float)((color >> 8) & 0xFF);
pixel_avg_fb = (float)(color & 0xFF);
}
else {
// read from floating point format
pixel_avg_fr = this.pixels_rgbf[pixels_rgbf_indx];
pixel_avg_fg = this.pixels_rgbf[pixels_rgbf_indx+1];
pixel_avg_fb = this.pixels_rgbf[pixels_rgbf_indx+2];
}
/*float pixel_avg_fr = pixels_avg_fr[x];
float pixel_avg_fg = pixels_avg_fg[x];
float pixel_avg_fb = pixels_avg_fb[x];*/
int x_new = x+offset_x_new;
if( x_new >= 0 && x_new < width ) {
//if( x_new < width ) {
//{
//int pixel_new = bitmap_new.getPixel(x+offset_x_new, y+offset_y_new);
//int pixel_new = fast_bitmap_new[thread_index].getPixel(x+offset_x_new, y+offset_y_new);
//int pixel_new = bitmap_new_cache_pixels[(y+offset_y_new-bitmap_new_cache_y)*width+(x+offset_x_new)];
int pixel_new = bitmap_new_cache_pixels[y_rel_bitmap_new_cache*width+x_new];
float pixel_new_fr = (float)((pixel_new >> 16) & 0xFF);
float pixel_new_fg = (float)((pixel_new >> 8) & 0xFF);
float pixel_new_fb = (float)(pixel_new & 0xFF);
// temporal merging
// smaller value of wiener_C means stronger filter (i.e., less averaging)
// diff based on rgb
//float diff_r = pixel_avg_fr - pixel_new_fr;
//float diff_g = pixel_avg_fg - pixel_new_fg;
//float diff_b = pixel_avg_fb - pixel_new_fb;
//float L = diff_r*diff_r + diff_g*diff_g + diff_b*diff_b;
// diff based on neighbourhood [sampling a subset of pixels]
// this helps testAvg24, testAvg28, testAvg31, testAvg33, testAvg39
float L = 0.0f;
if( x-radius >= 0 && x+radius < width &&
y-radius >= 0 && y+radius < height &&
x_new-radius >= 0 && x_new+radius < width &&
y_new-radius >= 0 && y_new+radius < height ) {
final int n_pixels_c = 5; // number of pixels we read from
// average of diffs:
/*for(int i=0;i<n_pixels_c;i++) {
int sx = sample_x[i];
int sy = sample_y[i];
//int pixel_orig = bitmap_orig.getPixel(x+sx, y+sy);
//int pixel_orig = fast_bitmap_orig[thread_index].getPixel(x+sx, y+sy);
//int pixel_orig = bitmap_orig_cache_pixels[(y+sy-bitmap_orig_cache_y)*width+(x+sx)];
int pixel_orig = bitmap_orig_cache_pixels[(y_rel_bitmap_orig_cache+sy)*width+(x+sx)];
float pixel_orig_fr = (float)((pixel_orig >> 16) & 0xFF);
float pixel_orig_fg = (float)((pixel_orig >> 8) & 0xFF);
float pixel_orig_fb = (float)(pixel_orig & 0xFF);
float pixel_new_sample_fr, pixel_new_sample_fg, pixel_new_sample_fb;
if( sx == 0 && sy == 0 ) {
pixel_new_sample_fr = pixel_new_fr;
pixel_new_sample_fg = pixel_new_fg;
pixel_new_sample_fb = pixel_new_fb;
}
else {
//int pixel_new_sample = bitmap_new.getPixel(ox+sx, oy+sy);
//int pixel_new_sample = fast_bitmap_new[thread_index].getPixel(ox+sx, oy+sy);
//int pixel_new_sample = bitmap_new_cache_pixels[(oy+sy-bitmap_new_cache_y)*width+(ox+sx)];
//int pixel_new_sample = bitmap_new_cache_pixels[(y_new+sy-bitmap_new_cache_y)*width+(x_new+sx)];
int pixel_new_sample = bitmap_new_cache_pixels[(y_rel_bitmap_new_cache+sy)*width+(x_new+sx)];
pixel_new_sample_fr = (float)((pixel_new_sample >> 16) & 0xFF);
pixel_new_sample_fg = (float)((pixel_new_sample >> 8) & 0xFF);
pixel_new_sample_fb = (float)(pixel_new_sample & 0xFF);
}
float diff_r = pixel_orig_fr - pixel_new_sample_fr;
float diff_g = pixel_orig_fg - pixel_new_sample_fg;
float diff_b = pixel_orig_fb - pixel_new_sample_fb;
L += diff_r*diff_r + diff_g*diff_g + diff_b*diff_b;
}*/
// unroll loop for performance:
int pixel_orig;
float pixel_orig_fr, pixel_orig_fg, pixel_orig_fb;
int pixel_new_sample;
float pixel_new_sample_fr, pixel_new_sample_fg, pixel_new_sample_fb;
float diff_r, diff_g, diff_b;
pixel_orig = bitmap_orig_cache_pixels[(y_rel_bitmap_orig_cache-2)*width+(x-2)];
pixel_orig_fr = (float)((pixel_orig >> 16) & 0xFF);
pixel_orig_fg = (float)((pixel_orig >> 8) & 0xFF);
pixel_orig_fb = (float)(pixel_orig & 0xFF);
pixel_new_sample = bitmap_new_cache_pixels[(y_rel_bitmap_new_cache-2)*width+(x_new-2)];
pixel_new_sample_fr = (float)((pixel_new_sample >> 16) & 0xFF);
pixel_new_sample_fg = (float)((pixel_new_sample >> 8) & 0xFF);
pixel_new_sample_fb = (float)(pixel_new_sample & 0xFF);
diff_r = pixel_orig_fr - pixel_new_sample_fr;
diff_g = pixel_orig_fg - pixel_new_sample_fg;
diff_b = pixel_orig_fb - pixel_new_sample_fb;
L += diff_r*diff_r + diff_g*diff_g + diff_b*diff_b;
pixel_orig = bitmap_orig_cache_pixels[(y_rel_bitmap_orig_cache-2)*width+(x+2)];
pixel_orig_fr = (float)((pixel_orig >> 16) & 0xFF);
pixel_orig_fg = (float)((pixel_orig >> 8) & 0xFF);
pixel_orig_fb = (float)(pixel_orig & 0xFF);
pixel_new_sample = bitmap_new_cache_pixels[(y_rel_bitmap_new_cache-2)*width+(x_new+2)];
pixel_new_sample_fr = (float)((pixel_new_sample >> 16) & 0xFF);
pixel_new_sample_fg = (float)((pixel_new_sample >> 8) & 0xFF);
pixel_new_sample_fb = (float)(pixel_new_sample & 0xFF);
diff_r = pixel_orig_fr - pixel_new_sample_fr;
diff_g = pixel_orig_fg - pixel_new_sample_fg;
diff_b = pixel_orig_fb - pixel_new_sample_fb;
L += diff_r*diff_r + diff_g*diff_g + diff_b*diff_b;
pixel_orig = bitmap_orig_cache_pixels[(y_rel_bitmap_orig_cache)*width+(x)];
pixel_orig_fr = (float)((pixel_orig >> 16) & 0xFF);
pixel_orig_fg = (float)((pixel_orig >> 8) & 0xFF);
pixel_orig_fb = (float)(pixel_orig & 0xFF);
pixel_new_sample_fr = pixel_new_fr;
pixel_new_sample_fg = pixel_new_fg;
pixel_new_sample_fb = pixel_new_fb;
diff_r = pixel_orig_fr - pixel_new_sample_fr;
diff_g = pixel_orig_fg - pixel_new_sample_fg;
diff_b = pixel_orig_fb - pixel_new_sample_fb;
L += diff_r*diff_r + diff_g*diff_g + diff_b*diff_b;
pixel_orig = bitmap_orig_cache_pixels[(y_rel_bitmap_orig_cache+2)*width+(x-2)];
pixel_orig_fr = (float)((pixel_orig >> 16) & 0xFF);
pixel_orig_fg = (float)((pixel_orig >> 8) & 0xFF);
pixel_orig_fb = (float)(pixel_orig & 0xFF);
pixel_new_sample = bitmap_new_cache_pixels[(y_rel_bitmap_new_cache+2)*width+(x_new-2)];
pixel_new_sample_fr = (float)((pixel_new_sample >> 16) & 0xFF);
pixel_new_sample_fg = (float)((pixel_new_sample >> 8) & 0xFF);
pixel_new_sample_fb = (float)(pixel_new_sample & 0xFF);
diff_r = pixel_orig_fr - pixel_new_sample_fr;
diff_g = pixel_orig_fg - pixel_new_sample_fg;
diff_b = pixel_orig_fb - pixel_new_sample_fb;
L += diff_r*diff_r + diff_g*diff_g + diff_b*diff_b;
pixel_orig = bitmap_orig_cache_pixels[(y_rel_bitmap_orig_cache+2)*width+(x+2)];
pixel_orig_fr = (float)((pixel_orig >> 16) & 0xFF);
pixel_orig_fg = (float)((pixel_orig >> 8) & 0xFF);
pixel_orig_fb = (float)(pixel_orig & 0xFF);
pixel_new_sample = bitmap_new_cache_pixels[(y_rel_bitmap_new_cache+2)*width+(x_new+2)];
pixel_new_sample_fr = (float)((pixel_new_sample >> 16) & 0xFF);
pixel_new_sample_fg = (float)((pixel_new_sample >> 8) & 0xFF);
pixel_new_sample_fb = (float)(pixel_new_sample & 0xFF);
diff_r = pixel_orig_fr - pixel_new_sample_fr;
diff_g = pixel_orig_fg - pixel_new_sample_fg;
diff_b = pixel_orig_fb - pixel_new_sample_fb;
L += diff_r*diff_r + diff_g*diff_g + diff_b*diff_b;
L /= n_pixels_c;
}
else {
float diff_r = pixel_avg_fr - pixel_new_fr;
float diff_g = pixel_avg_fg - pixel_new_fg;
float diff_b = pixel_avg_fb - pixel_new_fb;
L = diff_r*diff_r + diff_g*diff_g + diff_b*diff_b;
}
// debug mode: only works if limited to 2 images being merged
/*L = sqrt(L);
L = fmin(L, 255.0f);
pixel_new_f.r = L;
pixel_new_f.g = L;
pixel_new_f.b = L;
return pixel_new_f;*/
// diff based on luminance
/*float value_avg = fmax(pixel_avg_f.r, pixel_avg_f.g);
value_avg = fmax(value_avg, pixel_avg_f.b);
float value_new = fmax(pixel_new_f.r, pixel_new_f.g);
value_new = fmax(value_new, pixel_new_f.b);
float diff = value_avg - value_new;
float L = 3.0f*diff*diff;*/
//L = 0.0f; // test no wiener filter
/*float value_avg = fmax(pixel_avg_f.r, pixel_avg_f.g);
value_avg = fmax(value_avg, pixel_avg_f.b);
float value_new = fmax(pixel_new_f.r, pixel_new_f.g);
value_new = fmax(value_new, pixel_new_f.b);
//float value = 0.5f*(value_avg + value_new)/127.5f;
float value = 0.5f*(value_avg + value_new);
value = fmax(value, 8.0f);
value = fmin(value, 32.0f);
value /= 32.0f;*/
//float value = 1.0f;
// relative scaling:
/*float value_avg = fmax(pixel_avg_f.r, pixel_avg_f.g);
value_avg = fmax(value_avg, pixel_avg_f.b);
float value_new = fmax(pixel_new_f.r, pixel_new_f.g);
value_new = fmax(value_new, pixel_new_f.b);
float value = 0.5*(value_avg + value_new);
//float value = fmax(value_avg, value_new);
value = fmax(value, 64.0f);
L *= 64.0f/value;
//float L_scale = 64.0f/value;
//L *= L_scale*L_scale;
*/
//L = 0.0f; // test no deghosting
if( L > wiener_C_cutoff ) {
// error too large, so no contribution for new image pixel
// stick with pixel_avg
// reduces ghosting in: testAvg13, testAvg25, testAvg26, testAvg29, testAvg31
}
else {
float weight = L/(L+wiener_C); // lower weight means more averaging
float weight1 = 1.0f-weight;
pixel_new_fr = weight * pixel_avg_fr + weight1 * pixel_new_fr;
pixel_new_fg = weight * pixel_avg_fg + weight1 * pixel_new_fg;
pixel_new_fb = weight * pixel_avg_fb + weight1 * pixel_new_fb;
/*float weight = L/(L+wiener_C); // lower weight means more averaging
weight = fmin(weight, max_weight);
if( L > wiener_C_cutoff ) {
// error too large, so no contribution for new image pixel
// reduces ghosting in: testAvg13, testAvg25, testAvg26, testAvg29, testAvg31
weight = max_weight;
}
pixel_new_f = weight * pixel_avg_f + (1.0-weight) * pixel_new_f;*/
pixel_avg_fr = (avg_factor*pixel_avg_fr + pixel_new_fr)/avg_factorp1;
pixel_avg_fg = (avg_factor*pixel_avg_fg + pixel_new_fg)/avg_factorp1;
pixel_avg_fb = (avg_factor*pixel_avg_fb + pixel_new_fb)/avg_factorp1;
}
}
/*this.pixels_rgbf[3*(y*width + x)] = pixel_avg_fr;
this.pixels_rgbf[3*(y*width + x)+1] = pixel_avg_fg;
this.pixels_rgbf[3*(y*width + x)+2] = pixel_avg_fb;*/
this.pixels_rgbf[pixels_rgbf_indx] = pixel_avg_fr;
this.pixels_rgbf[pixels_rgbf_indx+1] = pixel_avg_fg;
this.pixels_rgbf[pixels_rgbf_indx+2] = pixel_avg_fb;
}
}
}
@Override
public void apply(JavaImageProcessing.CachedBitmap output, int thread_index, byte [] pixels, int off_x, int off_y, int this_width, int this_height) {
// unused
throw new RuntimeException("not implemented");
}
}
static class AvgBrightenApplyFunction implements JavaImageProcessing.ApplyFunctionInterface {
//private final int [] pixels_in;
private final float [] pixels_in_rgbf;
private final int width, height;
private final DROBrightenApplyFunction brighten;
private final float median_filter_strength, black_level, white_level;
private final float [] value_to_gamma_scale_lut = new float[256]; // look up table for performance
AvgBrightenApplyFunction(/*int [] pixels_in,*/ float [] pixels_in_rgbf, int width, int height, float gain, float gamma, float low_x, float mid_x, float max_x, float median_filter_strength, float black_level) {
//this.pixels_in = pixels_in;
this.pixels_in_rgbf = pixels_in_rgbf;
this.width = width;
this.height = height;
this.brighten = new DROBrightenApplyFunction(gain, gamma, low_x, mid_x, max_x);
this.median_filter_strength = median_filter_strength;
this.black_level = black_level;
this.white_level = 255.0f / (255.0f - black_level);
for(int value=0;value<256;value++) {
float new_value = (float)Math.pow(value/brighten.max_x, brighten.gamma) * 255.0f;