-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfinish.py
More file actions
845 lines (585 loc) · 23.9 KB
/
Copy pathfinish.py
File metadata and controls
845 lines (585 loc) · 23.9 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
import math
import halide as hl
from utils import DENOISE_PASSES, TONE_MAP_PASSES, SHARPEN_STRENGTH
def black_white_level(input, black_point, white_point):
output = hl.Func("black_white_level_output")
x, y = hl.Var("x"), hl.Var("y")
white_factor = 65535 / (white_point - black_point)
output[x, y] = hl.u16_sat((hl.i32(input[x, y]) - black_point) * white_factor)
return output
def white_balance(input, width, height, white_balance_r, white_balance_g0, white_balance_g1, white_balance_b):
output = hl.Func("white_balance_output")
print(width, height, white_balance_r, white_balance_g0, white_balance_g1, white_balance_b)
x, y = hl.Var("x"), hl.Var("y")
rdom = hl.RDom([(0, width / 2), (0, height / 2)])
output[x, y] = hl.u16(0)
output[rdom.x * 2, rdom.y * 2] = hl.u16_sat(white_balance_r * hl.f32(input[rdom.x * 2, rdom.y * 2]))
output[rdom.x * 2 + 1, rdom.y * 2] = hl.u16_sat(white_balance_g0 * hl.f32(input[rdom.x * 2 + 1, rdom.y * 2]))
output[rdom.x * 2, rdom.y * 2 + 1] = hl.u16_sat(white_balance_g1 * hl.f32(input[rdom.x * 2, rdom.y * 2 + 1]))
output[rdom.x * 2 + 1, rdom.y * 2 + 1] = hl.u16_sat(white_balance_b * hl.f32(input[rdom.x * 2 + 1, rdom.y * 2 + 1]))
output.compute_root().parallel(y).vectorize(x, 16)
output.update(0).parallel(rdom.y)
output.update(1).parallel(rdom.y)
output.update(2).parallel(rdom.y)
output.update(3).parallel(rdom.y)
return output
def demosaic(input, width, height):
print(f'width: {width}, height: {height}')
f0 = hl.Buffer(hl.Int(32), [5, 5], "demosaic_f0")
f1 = hl.Buffer(hl.Int(32), [5, 5], "demosaic_f1")
f2 = hl.Buffer(hl.Int(32), [5, 5], "demosaic_f2")
f3 = hl.Buffer(hl.Int(32), [5, 5], "demosaic_f3")
f0.translate([-2, -2])
f1.translate([-2, -2])
f2.translate([-2, -2])
f3.translate([-2, -2])
d0 = hl.Func("demosaic_0")
d1 = hl.Func("demosaic_1")
d2 = hl.Func("demosaic_2")
d3 = hl.Func("demosaic_3")
output = hl.Func("demosaic_output")
x, y, c = hl.Var("x"), hl.Var("y"), hl.Var("c")
rdom0 = hl.RDom([(-2, 5), (-2, 5)])
# rdom1 = hl.RDom([(0, width / 2), (0, height / 2)])
input_mirror = hl.BoundaryConditions.mirror_interior(input, [(0, width), (0, height)])
f0.fill(0)
f1.fill(0)
f2.fill(0)
f3.fill(0)
f0_sum = 8
f1_sum = 16
f2_sum = 16
f3_sum = 16
f0[0, -2] = -1
f0[0, -1] = 2
f0[-2, 0] = -1
f0[-1, 0] = 2
f0[0, 0] = 4
f0[1, 0] = 2
f0[2, 0] = -1
f0[0, 1] = 2
f0[0, 2] = -1
f1[0, -2] = 1
f1[-1, -1] = -2
f1[1, -1] = -2
f1[-2, 0] = -2
f1[-1, 0] = 8
f1[0, 0] = 10
f1[1, 0] = 8
f1[2, 0] = -2
f1[-1, 1] = -2
f1[1, 1] = -2
f1[0, 2] = 1
f2[0, -2] = -2
f2[-1, -1] = -2
f2[0, -1] = 8
f2[1, -1] = -2
f2[-2, 0] = 1
f2[0, 0] = 10
f2[2, 0] = 1
f2[-1, 1] = -2
f2[0, 1] = 8
f2[1, 1] = -2
f2[0, 2] = -2
f3[0, -2] = -3
f3[-1, -1] = 4
f3[1, -1] = 4
f3[-2, 0] = -3
f3[0, 0] = 12
f3[2, 0] = -3
f3[-1, 1] = 4
f3[1, 1] = 4
f3[0, 2] = -3
d0[x, y] = hl.u16_sat(hl.sum(hl.i32(input_mirror[x + rdom0.x, y + rdom0.y]) * f0[rdom0.x, rdom0.y]) / f0_sum)
d1[x, y] = hl.u16_sat(hl.sum(hl.i32(input_mirror[x + rdom0.x, y + rdom0.y]) * f1[rdom0.x, rdom0.y]) / f1_sum)
d2[x, y] = hl.u16_sat(hl.sum(hl.i32(input_mirror[x + rdom0.x, y + rdom0.y]) * f2[rdom0.x, rdom0.y]) / f2_sum)
d3[x, y] = hl.u16_sat(hl.sum(hl.i32(input_mirror[x + rdom0.x, y + rdom0.y]) * f3[rdom0.x, rdom0.y]) / f3_sum)
R_row = y % 2 == 0
B_row = y % 2 != 0
R_col = x % 2 == 0
B_col = x % 2 != 0
at_R = c == 0
at_G = c == 1
at_B = c == 2
output[x, y, c] = hl.select(at_R & R_row & B_col, d1[x, y],
at_R & B_row & R_col, d2[x, y],
at_R & B_row & B_col, d3[x, y],
at_G & R_row & R_col, d0[x, y],
at_G & B_row & B_col, d0[x, y],
at_B & B_row & R_col, d1[x, y],
at_B & R_row & B_col, d2[x, y],
at_B & R_row & R_col, d3[x, y],
input[x, y])
d0.compute_root().parallel(y).vectorize(x, 16)
d1.compute_root().parallel(y).vectorize(x, 16)
d2.compute_root().parallel(y).vectorize(x, 16)
d3.compute_root().parallel(y).vectorize(x, 16)
output.compute_root().parallel(y).align_bounds(x, 2).unroll(x, 2).align_bounds(y, 2).unroll(y, 2).vectorize(x, 16)
return output
def rgb_to_yuv(input):
print(' rgb_to_yuv')
output = hl.Func("rgb_to_yuv_output")
x, y, c = hl.Var("x"), hl.Var("y"), hl.Var("c")
rdom = input[x, y, 0]
g = input[x, y, 1]
b = input[x, y, 2]
output[x, y, c] = hl.f32(0)
output[x, y, 0] = 0.2989 * rdom + 0.587 * g + 0.114 * b
output[x, y, 1] = -0.168935 * rdom - 0.331655 * g + 0.50059 * b
output[x, y, 2] = 0.499813 * rdom - 0.418531 * g + - 0.081282 * b
output.compute_root().parallel(y).vectorize(x, 16)
output.update(0).parallel(y).vectorize(x, 16)
output.update(1).parallel(y).vectorize(x, 16)
output.update(2).parallel(y).vectorize(x, 16)
return output
def bilateral_filter(input, width, height):
print(' bilateral_filter')
k = hl.Buffer(hl.Float(32), [7, 7], "gauss_kernel")
k.translate([-3, -3])
weights = hl.Func("bilateral_weights")
total_weights = hl.Func("bilateral_total_weights")
bilateral = hl.Func("bilateral")
output = hl.Func("bilateral_filter_output")
x, y, dx, dy, c = hl.Var("x"), hl.Var("y"), hl.Var("dx"), hl.Var("dy"), hl.Var("c")
rdom = hl.RDom([(-3, 7), (-3, 7)])
k.fill(0)
k[-3, -3] = 0.000690
k[-2, -3] = 0.002646
k[-1, -3] = 0.005923
k[0, -3] = 0.007748
k[1, -3] = 0.005923
k[2, -3] = 0.002646
k[3, -3] = 0.000690
k[-3, -2] = 0.002646
k[-2, -2] = 0.010149
k[-1, -2] = 0.022718
k[0, -2] = 0.029715
k[1, -2] = 0.022718
k[2, -2] = 0.010149
k[3, -2] = 0.002646
k[-3, -1] = 0.005923
k[-2, -1] = 0.022718
k[-1, -1] = 0.050855
k[0, -1] = 0.066517
k[1, -1] = 0.050855
k[2, -1] = 0.022718
k[3, -1] = 0.005923
k[-3, 0] = 0.007748
k[-2, 0] = 0.029715
k[-1, 0] = 0.066517
k[0, 0] = 0.087001
k[1, 0] = 0.066517
k[2, 0] = 0.029715
k[3, 0] = 0.007748
k[-3, 1] = 0.005923
k[-2, 1] = 0.022718
k[-1, 1] = 0.050855
k[0, 1] = 0.066517
k[1, 1] = 0.050855
k[2, 1] = 0.022718
k[3, 1] = 0.005923
k[-3, 2] = 0.002646
k[-2, 2] = 0.010149
k[-1, 2] = 0.022718
k[0, 2] = 0.029715
k[1, 2] = 0.022718
k[2, 2] = 0.010149
k[3, 2] = 0.002646
k[-3, 3] = 0.000690
k[-2, 3] = 0.002646
k[-1, 3] = 0.005923
k[0, 3] = 0.007748
k[1, 3] = 0.005923
k[2, 3] = 0.002646
k[3, 3] = 0.000690
input_mirror = hl.BoundaryConditions.mirror_interior(input, [(0, width), (0, height)])
dist = hl.cast(hl.Float(32),
hl.cast(hl.Int(32), input_mirror[x, y, c]) - hl.cast(hl.Int(32), input_mirror[x + dx, y + dy, c]))
sig2 = 100
threshold = 25000
score = hl.select(hl.abs(input_mirror[x + dx, y + dy, c]) > threshold, 0, hl.exp(-dist * dist / sig2))
weights[dx, dy, x, y, c] = k[dx, dy] * score
total_weights[x, y, c] = hl.sum(weights[rdom.x, rdom.y, x, y, c])
bilateral[x, y, c] = hl.sum(input_mirror[x + rdom.x, y + rdom.y, c] * weights[rdom.x, rdom.y, x, y, c]) / \
total_weights[x, y, c]
output[x, y, c] = hl.cast(hl.Float(32), input[x, y, c])
output[x, y, 1] = bilateral[x, y, 1]
output[x, y, 2] = bilateral[x, y, 2]
weights.compute_at(output, y).vectorize(x, 16)
output.compute_root().parallel(y).vectorize(x, 16)
output.update(0).parallel(y).vectorize(x, 16)
output.update(1).parallel(y).vectorize(x, 16)
return output
def gauss_15x15(input, name):
print(' gauss_15x15')
k = hl.Buffer(hl.Float(32), [15], "gauss_15x15")
k.translate([-7])
rdom = hl.RDom([(-7, 15)])
k.fill(0)
k[-7] = 0.004961
k[-6] = 0.012246
k[-5] = 0.026304
k[-4] = 0.049165
k[-3] = 0.079968
k[-2] = 0.113193
k[-1] = 0.139431
k[0] = 0.149464
k[7] = 0.004961
k[6] = 0.012246
k[5] = 0.026304
k[4] = 0.049165
k[3] = 0.079968
k[2] = 0.113193
k[1] = 0.139431
return gauss(input, k, rdom, name)
def desaturate_noise(input, width, height):
print(' desaturate_noise')
output = hl.Func("desaturate_noise_output")
x, y, c = hl.Var("x"), hl.Var("y"), hl.Var("c")
input_mirror = hl.BoundaryConditions.mirror_image(input, [(0, width), (0, height)])
blur = gauss_15x15(gauss_15x15(input_mirror, "desaturate_noise_blur1"), "desaturate_noise_blur_2")
factor = 1.4
threshold = 25000
output[x, y, c] = input[x, y, c]
output[x, y, 1] = hl.select((hl.abs(blur[x, y, 1]) / hl.abs(input[x, y, 1]) < factor) &
(hl.abs(input[x, y, 1]) < threshold) & (hl.abs(blur[x, y, 1]) < threshold),
0.7 * blur[x, y, 1] + 0.3 * input[x, y, 1], input[x, y, 1])
output[x, y, 2] = hl.select((hl.abs(blur[x, y, 2]) / hl.abs(input[x, y, 2]) < factor) &
(hl.abs(input[x, y, 2]) < threshold) & (hl.abs(blur[x, y, 2]) < threshold),
0.7 * blur[x, y, 2] + 0.3 * input[x, y, 2], input[x, y, 2])
output.compute_root().parallel(y).vectorize(x, 16)
return output
def increase_saturation(input, strength):
print(' increase saturation')
output = hl.Func("increase_saturation_output")
x, y, c = hl.Var("x"), hl.Var("y"), hl.Var("c")
output[x, y, c] = strength * input[x, y, c]
output[x, y, 0] = input[x, y, 0]
output.compute_root().parallel(y).vectorize(x, 16)
return output
def yuv_to_rgb(input):
print(' yuv_to_rgb')
output = hl.Func("yuv_to_rgb_output")
x, y, c = hl.Var("x"), hl.Var("y"), hl.Var("c")
Y = input[x, y, 0]
U = input[x, y, 1]
V = input[x, y, 2]
output[x, y, c] = hl.cast(hl.UInt(16), 0)
output[x, y, 0] = hl.u16_sat(Y + 1.403 * V)
output[x, y, 1] = hl.u16_sat(Y - 0.344 * U - 0.714 * V)
output[x, y, 2] = hl.u16_sat(Y + 1.77 * U)
output.compute_root().parallel(y).vectorize(x, 16)
output.update(0).parallel(y).vectorize(x, 16)
output.update(1).parallel(y).vectorize(x, 16)
output.update(2).parallel(y).vectorize(x, 16)
return output
def chroma_denoise(input, width, height, denoise_passes):
print(f'width: {width}, height: {height}, passes: {denoise_passes}')
output = rgb_to_yuv(input)
p = 0
if denoise_passes > 0:
output = bilateral_filter(output, width, height)
p += 1
while p < denoise_passes:
output = desaturate_noise(output, width, height)
p += 1
if denoise_passes > 2:
output = increase_saturation(output, 1.1)
return yuv_to_rgb(output)
def srgb(input, ccm):
srgb_matrix = hl.Func("srgb_matrix")
output = hl.Func("srgb_output")
x, y, c = hl.Var("x"), hl.Var("y"), hl.Var("c")
rdom = hl.RDom([(0, 3)])
srgb_matrix[x, y] = hl.f32(0)
srgb_matrix[0, 0] = hl.f32(ccm[0][0])
srgb_matrix[1, 0] = hl.f32(ccm[0][1])
srgb_matrix[2, 0] = hl.f32(ccm[0][2])
srgb_matrix[0, 1] = hl.f32(ccm[1][0])
srgb_matrix[1, 1] = hl.f32(ccm[1][1])
srgb_matrix[2, 1] = hl.f32(ccm[1][2])
srgb_matrix[0, 2] = hl.f32(ccm[2][0])
srgb_matrix[1, 2] = hl.f32(ccm[2][1])
srgb_matrix[2, 2] = hl.f32(ccm[2][2])
output[x, y, c] = hl.u16_sat(hl.sum(srgb_matrix[rdom, c] * input[x, y, rdom]))
return output
def gauss(input, k, rdom, name):
blur_x = hl.Func(name + "_x")
output = hl.Func(name)
x, y, c, xi, yi = hl.Var("x"), hl.Var("y"), hl.Var("c"), hl.Var("xi"), hl.Var("yi")
val = hl.Expr("val")
if input.dimensions() == 2:
blur_x[x, y] = hl.sum(input[x + rdom, y] * k[rdom])
val = hl.sum(blur_x[x, y + rdom] * k[rdom])
if input.output_types()[0] == hl.UInt(16):
val = hl.u16(val)
output[x, y] = val
else:
blur_x[x, y, c] = hl.sum(input[x + rdom, y, c] * k[rdom])
val = hl.sum(blur_x[x, y + rdom, c] * k[rdom])
if input.output_types()[0] == hl.UInt(16):
val = hl.u16(val)
output[x, y, c] = val
blur_x.compute_at(output, x).vectorize(x, 16)
output.compute_root().tile(x, y, xi, yi, 256, 128).vectorize(xi, 16).parallel(y)
return output
def gauss_7x7(input, name):
k = hl.Buffer(hl.Float(32), [7], "gauss_7x7_kernel")
k.translate([-3])
rdom = hl.RDom([(-3, 7)])
k.fill(0)
k[-3] = 0.026267
k[-2] = 0.100742
k[-1] = 0.225511
k[0] = 0.29496
k[1] = 0.225511
k[2] = 0.100742
k[3] = 0.026267
return gauss(input, k, rdom, name)
def diff(im1, im2, name):
output = hl.Func(name)
x, y, c = hl.Var("x"), hl.Var("y"), hl.Var("c")
if im1.dimensions() == 2:
output[x, y] = hl.i32(im1[x, y]) - hl.i32(im2[x, y])
else:
output[x, y, c] = hl.i32(im1[x, y, c]) - hl.i32(im2[x, y, c])
return output
def combine(im1, im2, width, height, dist):
init_mask1 = hl.Func("mask1_layer_0")
init_mask2 = hl.Func("mask2_layer_0")
accumulator = hl.Func("combine_accumulator")
output = hl.Func("combine_output")
x, y = hl.Var("x"), hl.Var("y")
im1_mirror = hl.BoundaryConditions.repeat_edge(im1, [(0, width), (0, height)])
im2_mirror = hl.BoundaryConditions.repeat_edge(im2, [(0, width), (0, height)])
unblurred1 = im1_mirror
unblurred2 = im2_mirror
blurred1 = gauss_7x7(im1_mirror, "img1_layer_0")
blurred2 = gauss_7x7(im2_mirror, "img2_layer_0")
weight1 = hl.f32(dist[im1_mirror[x, y]])
weight2 = hl.f32(dist[im2_mirror[x, y]])
init_mask1[x, y] = weight1 / (weight1 + weight2)
init_mask2[x, y] = 1 - init_mask1[x, y]
mask1 = init_mask1
mask2 = init_mask2
num_layers = 2
accumulator[x, y] = hl.i32(0)
for i in range(1, num_layers):
print(' layer', i)
prev_layer_str = str(i - 1)
layer_str = str(i)
laplace1 = diff(unblurred1, blurred1, "laplace1_layer_" + prev_layer_str)
laplace2 = diff(unblurred2, blurred2, "laplace2_layer_" + layer_str)
accumulator[x, y] += hl.i32(laplace1[x, y] * mask1[x, y]) + hl.i32(laplace2[x, y] * mask2[x, y])
unblurred1 = blurred1
unblurred2 = blurred2
blurred1 = gauss_7x7(blurred1, "img1_layer_" + layer_str)
blurred2 = gauss_7x7(blurred2, "img2_layer_" + layer_str)
mask1 = gauss_7x7(mask1, "mask1_layer_" + layer_str)
mask2 = gauss_7x7(mask2, "mask2_layer_" + layer_str)
accumulator[x, y] += hl.i32(blurred1[x, y] * mask1[x, y]) + hl.i32(blurred2[x, y] * mask2[x, y])
output[x, y] = hl.u16_sat(accumulator[x, y])
init_mask1.compute_root().parallel(y).vectorize(x, 16)
accumulator.compute_root().parallel(y).vectorize(x, 16)
for i in range(num_layers):
accumulator.update(i).parallel(y).vectorize(x, 16)
return output
def combine2(im1, im2, width, height, dist):
init_mask1 = hl.Func("mask1_layer_0")
init_mask2 = hl.Func("mask2_layer_0")
accumulator = hl.Func("combine_accumulator")
output = hl.Func("combine_output")
x, y = hl.Var("x"), hl.Var("y")
im1_mirror = hl.BoundaryConditions.repeat_edge(im1, [(0, width), (0, height)])
im2_mirror = hl.BoundaryConditions.repeat_edge(im2, [(0, width), (0, height)])
weight1 = hl.f32(dist[im1_mirror[x, y]])
weight2 = hl.f32(dist[im2_mirror[x, y]])
init_mask1[x, y] = weight1 / (weight1 + weight2)
init_mask2[x, y] = 1 - init_mask1[x, y]
mask1 = init_mask1
mask2 = init_mask2
accumulator[x, y] = hl.i32(0)
accumulator[x, y] += hl.i32(im1_mirror[x, y] * mask1[x, y]) + hl.i32(im2_mirror[x, y] * mask2[x, y])
output[x, y] = hl.u16_sat(accumulator[x, y])
init_mask1.compute_root().parallel(y).vectorize(x, 16)
accumulator.compute_root().parallel(y).vectorize(x, 16)
accumulator.update(0).parallel(y).vectorize(x, 16)
return output
def brighten(input, gain):
output = hl.Func("brighten_output")
x, y = hl.Var("x"), hl.Var("y")
output[x, y] = hl.u16_sat(gain * hl.u32(input[x, y]))
return output
def gamma_correct(input):
output = hl.Func("gamma_correct_output")
x, y, c = hl.Var("x"), hl.Var("y"), hl.Var("c")
cutoff = 200
gamma_toe = 12.92
gamma_pow = 0.416667
gamma_fac = 680.552897
gamma_con = -3604.425
if input.dimensions() == 2:
output[x, y] = hl.u16(hl.select(input[x, y] < cutoff,
gamma_toe * input[x, y],
gamma_fac * hl.pow(input[x, y], gamma_pow) + gamma_con))
else:
output[x, y, c] = hl.u16(hl.select(input[x, y, c] < cutoff,
gamma_toe * input[x, y, c],
gamma_fac * hl.pow(input[x, y, c], gamma_pow) + gamma_con))
output.compute_root().parallel(y).vectorize(x, 16)
return output
def gamma_inverse(input):
output = hl.Func("gamma_inverse_output")
x, y, c = hl.Var("x"), hl.Var("y"), hl.Var("c")
cutoff = 2575
gamma_toe = 0.0774
gamma_pow = 2.4
gamma_fac = 57632.49226
gamma_con = 0.055
if input.dimensions() == 2:
output[x, y] = hl.u16(hl.select(input[x, y] < cutoff,
gamma_toe * input[x, y],
hl.pow(hl.f32(input[x, y]) / 65535 + gamma_con, gamma_pow) * gamma_fac))
else:
output[x, y, c] = hl.u16(hl.select(input[x, y, c] < cutoff,
gamma_toe * input[x, y, c],
hl.pow(hl.f32(input[x, y, c]) / 65535 + gamma_con, gamma_pow) * gamma_fac))
output.compute_root().parallel(y).vectorize(x, 16)
return output
def tone_map(input, width, height, compression, gain):
print(f'Compression: {compression}, gain: {gain}')
normal_dist = hl.Func("luma_weight_distribution")
grayscale = hl.Func("grayscale")
output = hl.Func("tone_map_output")
x, y, c, v = hl.Var("x"), hl.Var("y"), hl.Var("c"), hl.Var("v")
rdom = hl.RDom([(0, 3)])
normal_dist[v] = hl.f32(hl.exp(-12.5 * hl.pow(hl.f32(v) / 65535 - 0.5, 2)))
grayscale[x, y] = hl.u16(hl.sum(hl.u32(input[x, y, rdom])) / 3)
dark = grayscale
comp_const = 1
gain_const = 1
comp_slope = (compression - comp_const) / (TONE_MAP_PASSES)
gain_slope = (gain - gain_const) / (TONE_MAP_PASSES)
for i in range(TONE_MAP_PASSES):
print(' pass', i)
norm_comp = i * comp_slope + comp_const
norm_gain = i * gain_slope + gain_const
bright = brighten(dark, norm_comp)
dark_gamma = gamma_correct(dark)
bright_gamma = gamma_correct(bright)
dark_gamma = combine2(dark_gamma, bright_gamma, width, height, normal_dist)
dark = brighten(gamma_inverse(dark_gamma), norm_gain)
output[x, y, c] = hl.u16_sat(hl.u32(input[x, y, c]) * hl.u32(dark[x, y]) / hl.u32(hl.max(1, grayscale[x, y])))
grayscale.compute_root().parallel(y).vectorize(x, 16)
normal_dist.compute_root().vectorize(v, 16)
return output
def shift_bayer_to_rggb(input, cfa_pattern):
print(f'cfa_pattern: {cfa_pattern}')
output = hl.Func("rggb_input")
x, y = hl.Var("x"), hl.Var("y")
cfa = hl.u16(cfa_pattern)
output[x, y] = hl.select(cfa == hl.u16(1), input[x, y],
cfa == hl.u16(2), input[x + 1, y],
cfa == hl.u16(4), input[x, y + 1],
cfa == hl.u16(3), input[x + 1, y + 1],
0)
return output
def contrast(input, strength, black_point):
output = hl.Func("contrast_output")
x, y, c = hl.Var("x"), hl.Var("y"), hl.Var("c")
scale = strength
inner_constant = math.pi / (2 * scale)
sin_constant = hl.sin(inner_constant)
slope = 65535 / (2 * sin_constant)
constant = slope * sin_constant
factor = math.pi / (scale * 65535)
val = factor * hl.cast(hl.Float(32), input[x, y, c])
output[x, y, c] = hl.u16_sat(slope * hl.sin(val - inner_constant) + constant)
white_scale = 65535 / (65535 - black_point)
output[x, y, c] = hl.u16_sat((hl.cast(hl.Int(32), output[x, y, c]) - black_point) * white_scale)
output.compute_root().parallel(y).vectorize(x, 16)
return output
def sharpen(input, strength):
output_yuv = hl.Func("sharpen_output")
x, y, c = hl.Var("x"), hl.Var("y"), hl.Var("c")
yuv_input = rgb_to_yuv(input)
small_blurred = gauss_7x7(yuv_input, "unsharp_small_blur")
large_blurred = gauss_7x7(small_blurred, "unsharp_large_blur")
difference_of_gauss = diff(small_blurred, large_blurred, "unsharp_DoG")
output_yuv[x, y, c] = yuv_input[x, y, c]
output_yuv[x, y, 0] = yuv_input[x, y, 0] + strength * difference_of_gauss[x, y, 0]
output = yuv_to_rgb(output_yuv)
output_yuv.compute_root().parallel(y).vectorize(x, 16)
return output
def u8bit_interleave(input):
output = hl.Func("8bit_interleaved_output")
x, y, c = hl.Var("x"), hl.Var("y"), hl.Var("c")
output[x, y, c] = hl.u8_sat(input[x, y, c] / 256)
output.compute_root().parallel(y).vectorize(x, 16)
return output
'''
Step 3 of HDR+ pipeline: finish
Finishes the merged burst images using the following steps:
1 : Shift Bayer image to RGGB
2 : Black- and white-level correction
3 : White balancing
4 : Demosaicing
5 : Chroma denoising
6 : sRGB color correction
7 : Tone mapping (global)
8 : Gamma correction
9 : Contrast adjustment
10 : Sharpening
11 : 8-bit interleaving
image : Halide buffer
The merged image to be finished
width : Integer
Width of the image
height : Integer
Height of the image
black_point : Integer
Black level of the image to be used for black and white level correction
white_point : Integer
White level of the image to be used for black and white level correction
white_balance_x : Float
White balance value for color X (R, G, G, B)
compression : Float
Compression value to be used for tone mapping
gain : Float
Gain value to be used for tone mapping
contrast_strength : Float
Contrast value to be used for contrast adjustment
cfa_pattern : Integer
Represents the Bayer pattern of the image, used to shift Bayer to RGGB
ccm : numpy.ndarray of shape (3, 4)
Color correction matrix, used for sRGB color correction
Returns: Halide buffer (finished image)
'''
def finish_image(image, width, height, black_point, white_point, white_balance_r, white_balance_g0, white_balance_g1,
white_balance_b, compression, gain, contrast_strength, cfa_pattern, ccm):
print(black_point, white_point, white_balance_r, white_balance_g0, white_balance_g1,
white_balance_b, compression, gain)
print("bayer_to_rggb")
bayer_shifted = shift_bayer_to_rggb(image, cfa_pattern)
print("black_white_level")
black_white_level_output = black_white_level(bayer_shifted, black_point, white_point)
print("white_balance")
white_balance_output = white_balance(black_white_level_output, width, height, white_balance_r, white_balance_g0,
white_balance_g1, white_balance_b)
print("demosaic")
demosaic_output = demosaic(white_balance_output, width, height)
print('chroma_denoise')
chroma_denoised_output = chroma_denoise(demosaic_output, width, height, DENOISE_PASSES)
print("srgb")
srgb_output = srgb(chroma_denoised_output, ccm)
print("tone_map")
tone_map_output = tone_map(srgb_output, width, height, compression, gain)
print("gamma_correct")
gamma_correct_output = gamma_correct(tone_map_output)
print('contrast')
contrast_output = contrast(gamma_correct_output, contrast_strength, black_point)
print('sharpen')
sharpen_output = sharpen(contrast_output, SHARPEN_STRENGTH)
print('u8bit_interleave')
u8bit_interleave_output = u8bit_interleave(sharpen_output)
return u8bit_interleave_output