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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
//! Bayer-specific demosaicing algorithms.
//!
//! This module contains demosaicing algorithms designed for standard 2x2 Bayer
//! color filter arrays found in most cameras (Sony, Canon, Nikon, DNG, etc.).
use super::{Demosaic, DemosaicError};
use crate::core::image::{CfaPattern, RawImage};
use rayon::prelude::*;
// =============================================================================
// AMaZE — Aliasing Minimization and Zipper Elimination
// =============================================================================
/// AMaZE (Aliasing Minimization and Zipper Elimination) demosaicing algorithm.
///
/// Industry standard for high-detail, low-noise images. Uses directional
/// interpolation with adaptive selection based on local gradient homogeneity,
/// followed by color-difference reconstruction for non-green channels.
///
/// Key features:
/// - Gradient-based direction selection (horizontal vs vertical)
/// - Laplacian second-derivative correction for green interpolation
/// - Color-difference model for R/B reconstruction
/// - Homogeneity-driven blending at ambiguous edges
///
/// Reference: [RawTherapee AMaZE implementation](https://github.com/RawTherapee/RawTherapee)
pub struct Amaze;
impl Demosaic for Amaze {
fn demosaic_into(&self, raw: &RawImage, output: &mut [u16]) -> Result<(), DemosaicError> {
let width = raw.active_area().size.width as usize;
let height = raw.active_area().size.height as usize;
let x_off = raw.active_area().origin.x as usize;
let y_off = raw.active_area().origin.y as usize;
let raw_w = raw.width() as usize;
let expected_size = width * height * 3;
if output.len() != expected_size {
return Err(DemosaicError::BufferSizeMismatch {
expected: expected_size,
actual: output.len(),
});
}
if width < 6 || height < 6 {
return Err(DemosaicError::InvalidDimensions);
}
let white = raw.white_level() as f32;
// Determine color at each CFA position: 0=Red, 1=Green(R-row), 2=Blue, 3=Green(B-row)
let fc = |x: usize, y: usize| -> u8 {
let ax = x + x_off;
let ay = y + y_off;
match raw.cfa_pattern() {
CfaPattern::Rggb => match (ax % 2, ay % 2) {
(0, 0) => 0, // R
(1, 0) => 1, // G on R-row
(0, 1) => 3, // G on B-row
_ => 2, // B
},
CfaPattern::Grbg => match (ax % 2, ay % 2) {
(0, 0) => 1,
(1, 0) => 0,
(0, 1) => 2,
_ => 3,
},
CfaPattern::Gbrg => match (ax % 2, ay % 2) {
(0, 0) => 3,
(1, 0) => 2,
(0, 1) => 0,
_ => 1,
},
CfaPattern::Bggr => match (ax % 2, ay % 2) {
(0, 0) => 2,
(1, 0) => 3,
(0, 1) => 1,
_ => 0,
},
}
};
// Safe accessor into raw data with mirror-padding at borders
let get = |x: isize, y: isize| -> f32 {
let cx = x.clamp(0, (width as isize) - 1) as usize;
let cy = y.clamp(0, (height as isize) - 1) as usize;
raw.data[(cy + y_off) * raw_w + (cx + x_off)] as f32
};
// ── Step 1: Green channel interpolation ──────────────────────
// Allocate green plane
let mut green = vec![0.0f32; width * height];
// For green pixels, just copy. For R/B pixels, interpolate green.
for y in 0..height {
for x in 0..width {
let color = fc(x, y);
let ix = x as isize;
let iy = y as isize;
if color == 1 || color == 3 {
// Green pixel — copy directly
green[y * width + x] = get(ix, iy);
} else {
// Red or Blue pixel — interpolate green using directional gradients
// Horizontal gradient (Laplacian-weighted)
let dh = (get(ix - 1, iy) - get(ix + 1, iy)).abs()
+ (2.0 * get(ix, iy) - get(ix - 2, iy) - get(ix + 2, iy)).abs();
// Vertical gradient
let dv = (get(ix, iy - 1) - get(ix, iy + 1)).abs()
+ (2.0 * get(ix, iy) - get(ix, iy - 2) - get(ix, iy + 2)).abs();
// Horizontal green estimate with 2nd-derivative correction
let gh = (get(ix - 1, iy) + get(ix + 1, iy)) * 0.5
+ (2.0 * get(ix, iy) - get(ix - 2, iy) - get(ix + 2, iy)) * 0.25;
// Vertical green estimate with 2nd-derivative correction
let gv = (get(ix, iy - 1) + get(ix, iy + 1)) * 0.5
+ (2.0 * get(ix, iy) - get(ix, iy - 2) - get(ix, iy + 2)) * 0.25;
// Adaptive direction selection
let eps = 1e-5;
if dh < dv * 0.5 {
// Strong horizontal preference
green[y * width + x] = gh;
} else if dv < dh * 0.5 {
// Strong vertical preference
green[y * width + x] = gv;
} else {
// Blend based on gradient ratio
let wh = 1.0 / (dh + eps);
let wv = 1.0 / (dv + eps);
green[y * width + x] = (wh * gh + wv * gv) / (wh + wv);
}
// Clamp to valid range
green[y * width + x] = green[y * width + x].max(0.0);
}
}
}
// ── Step 2: Homogeneity-based green refinement ───────────────
// Compute horizontal and vertical green estimates for homogeneity test
let mut gh_plane = vec![0.0f32; width * height];
let mut gv_plane = vec![0.0f32; width * height];
for y in 0..height {
for x in 0..width {
let color = fc(x, y);
let ix = x as isize;
let iy = y as isize;
if color == 1 || color == 3 {
gh_plane[y * width + x] = get(ix, iy);
gv_plane[y * width + x] = get(ix, iy);
} else {
gh_plane[y * width + x] = (get(ix - 1, iy) + get(ix + 1, iy)) * 0.5
+ (2.0 * get(ix, iy) - get(ix - 2, iy) - get(ix + 2, iy)) * 0.25;
gv_plane[y * width + x] = (get(ix, iy - 1) + get(ix, iy + 1)) * 0.5
+ (2.0 * get(ix, iy) - get(ix, iy - 2) - get(ix, iy + 2)) * 0.25;
}
}
}
// Compute homogeneity in a 3x3 window around each pixel
let mut h_homo = vec![0i32; width * height];
let mut v_homo = vec![0i32; width * height];
let border = 3usize;
for y in border..height.saturating_sub(border) {
for x in border..width.saturating_sub(border) {
let color = fc(x, y);
if color == 1 || color == 3 {
continue;
}
let mut hh = 0i32;
let mut vh = 0i32;
for dy in -1i32..=1 {
for dx in -1i32..=1 {
let nx = (x as i32 + dx) as usize;
let ny = (y as i32 + dy) as usize;
let idx = ny * width + nx;
// Color-difference homogeneity for horizontal
let cdh = (gh_plane[idx] - get(nx as isize, ny as isize)).abs();
// Color-difference homogeneity for vertical
let cdv = (gv_plane[idx] - get(nx as isize, ny as isize)).abs();
// Luminance homogeneity
let lh = (gh_plane[idx] - gh_plane[y * width + x]).abs();
let lv = (gv_plane[idx] - gv_plane[y * width + x]).abs();
let eps_h = cdh + lh;
let eps_v = cdv + lv;
if eps_h < eps_v {
hh += 1;
} else if eps_v < eps_h {
vh += 1;
}
}
}
h_homo[y * width + x] = hh;
v_homo[y * width + x] = vh;
}
}
// Refine green using homogeneity
for y in border..height.saturating_sub(border) {
for x in border..width.saturating_sub(border) {
let color = fc(x, y);
if color == 1 || color == 3 {
continue;
}
let idx = y * width + x;
let hh = h_homo[idx];
let vh = v_homo[idx];
if hh > vh + 1 {
green[idx] = gh_plane[idx];
} else if vh > hh + 1 {
green[idx] = gv_plane[idx];
} else {
// Blend
let wh = (hh + 1) as f32;
let wv = (vh + 1) as f32;
green[idx] = (wh * gh_plane[idx] + wv * gv_plane[idx]) / (wh + wv);
}
green[idx] = green[idx].max(0.0);
}
}
// Free intermediate buffers
drop(gh_plane);
drop(gv_plane);
drop(h_homo);
drop(v_homo);
// ── Step 3: R/B channel reconstruction via color-difference ──
// Build full R and B planes using color-difference interpolation.
// Color differences (R-G) and (B-G) vary more smoothly than raw R/B,
// so interpolating them produces fewer artifacts.
let mut red = vec![0.0f32; width * height];
let mut blue = vec![0.0f32; width * height];
// First pass: fill in known values and compute color differences
let mut cd_rg = vec![0.0f32; width * height]; // R - G
let mut cd_bg = vec![0.0f32; width * height]; // B - G
for y in 0..height {
for x in 0..width {
let idx = y * width + x;
let color = fc(x, y);
let val = get(x as isize, y as isize);
match color {
0 => {
// Red pixel
red[idx] = val;
cd_rg[idx] = val - green[idx];
}
2 => {
// Blue pixel
blue[idx] = val;
cd_bg[idx] = val - green[idx];
}
_ => {}
}
}
}
// Second pass: interpolate color differences at missing positions
// For green pixels on red rows: need both R and B via color-difference
// For green pixels on blue rows: need both R and B via color-difference
for y in 0..height {
for x in 0..width {
let idx = y * width + x;
let color = fc(x, y);
let ix = x as isize;
let iy = y as isize;
match color {
0 => {
// Red pixel — need blue. Blue neighbors are at diagonals.
let mut sum = 0.0f32;
let mut count = 0.0f32;
for &(dx, dy) in &[(-1i32, -1i32), (1, -1), (-1, 1), (1, 1)] {
let nx = ix + dx as isize;
let ny = iy + dy as isize;
if nx >= 0 && nx < width as isize && ny >= 0 && ny < height as isize {
let nidx = ny as usize * width + nx as usize;
if fc(nx as usize, ny as usize) == 2 {
sum += cd_bg[nidx];
count += 1.0;
}
}
}
if count > 0.0 {
blue[idx] = green[idx] + sum / count;
} else {
blue[idx] = green[idx];
}
}
2 => {
// Blue pixel — need red. Red neighbors are at diagonals.
let mut sum = 0.0f32;
let mut count = 0.0f32;
for &(dx, dy) in &[(-1i32, -1i32), (1, -1), (-1, 1), (1, 1)] {
let nx = ix + dx as isize;
let ny = iy + dy as isize;
if nx >= 0 && nx < width as isize && ny >= 0 && ny < height as isize {
let nidx = ny as usize * width + nx as usize;
if fc(nx as usize, ny as usize) == 0 {
sum += cd_rg[nidx];
count += 1.0;
}
}
}
if count > 0.0 {
red[idx] = green[idx] + sum / count;
} else {
red[idx] = green[idx];
}
}
1 => {
// Green on R-row — need R (horizontal neighbors) and B (vertical neighbors)
let mut sum_r = 0.0f32;
let mut cnt_r = 0.0f32;
for &dx in &[-1i32, 1] {
let nx = ix + dx as isize;
if nx >= 0 && nx < width as isize {
let nidx = y * width + nx as usize;
if fc(nx as usize, y) == 0 {
sum_r += cd_rg[nidx];
cnt_r += 1.0;
}
}
}
red[idx] = green[idx] + if cnt_r > 0.0 { sum_r / cnt_r } else { 0.0 };
let mut sum_b = 0.0f32;
let mut cnt_b = 0.0f32;
for &dy in &[-1i32, 1] {
let ny = iy + dy as isize;
if ny >= 0 && ny < height as isize {
let nidx = ny as usize * width + x;
if fc(x, ny as usize) == 2 {
sum_b += cd_bg[nidx];
cnt_b += 1.0;
}
}
}
blue[idx] = green[idx] + if cnt_b > 0.0 { sum_b / cnt_b } else { 0.0 };
}
3 => {
// Green on B-row — need B (horizontal neighbors) and R (vertical neighbors)
let mut sum_b = 0.0f32;
let mut cnt_b = 0.0f32;
for &dx in &[-1i32, 1] {
let nx = ix + dx as isize;
if nx >= 0 && nx < width as isize {
let nidx = y * width + nx as usize;
if fc(nx as usize, y) == 2 {
sum_b += cd_bg[nidx];
cnt_b += 1.0;
}
}
}
blue[idx] = green[idx] + if cnt_b > 0.0 { sum_b / cnt_b } else { 0.0 };
let mut sum_r = 0.0f32;
let mut cnt_r = 0.0f32;
for &dy in &[-1i32, 1] {
let ny = iy + dy as isize;
if ny >= 0 && ny < height as isize {
let nidx = ny as usize * width + x;
if fc(x, ny as usize) == 0 {
sum_r += cd_rg[nidx];
cnt_r += 1.0;
}
}
}
red[idx] = green[idx] + if cnt_r > 0.0 { sum_r / cnt_r } else { 0.0 };
}
_ => unreachable!(),
}
}
}
// ── Step 4: Zipper artifact reduction ────────────────────────
// Detect and correct zipper artifacts by checking local color-difference
// smoothness and replacing outliers with median-filtered values.
let mut red_out = red.clone();
let mut blue_out = blue.clone();
for y in 2..height.saturating_sub(2) {
for x in 2..width.saturating_sub(2) {
let idx = y * width + x;
let g = green[idx];
// Check red-green difference smoothness
let cd_r = red[idx] - g;
let cd_r_h1 = red[idx.wrapping_sub(1)] - green[idx.wrapping_sub(1)];
let cd_r_h2 = red[idx + 1] - green[idx + 1];
let cd_r_v1 = red[(y - 1) * width + x] - green[(y - 1) * width + x];
let cd_r_v2 = red[(y + 1) * width + x] - green[(y + 1) * width + x];
// If center color-difference is an outlier, smooth it
let avg_cd_r = (cd_r_h1 + cd_r_h2 + cd_r_v1 + cd_r_v2) * 0.25;
let var_r = (cd_r_h1 - avg_cd_r).abs()
+ (cd_r_h2 - avg_cd_r).abs()
+ (cd_r_v1 - avg_cd_r).abs()
+ (cd_r_v2 - avg_cd_r).abs();
if (cd_r - avg_cd_r).abs() > var_r * 1.5 + 1.0 {
red_out[idx] = g + avg_cd_r;
}
// Same for blue-green
let cd_b = blue[idx] - g;
let cd_b_h1 = blue[idx.wrapping_sub(1)] - green[idx.wrapping_sub(1)];
let cd_b_h2 = blue[idx + 1] - green[idx + 1];
let cd_b_v1 = blue[(y - 1) * width + x] - green[(y - 1) * width + x];
let cd_b_v2 = blue[(y + 1) * width + x] - green[(y + 1) * width + x];
let avg_cd_b = (cd_b_h1 + cd_b_h2 + cd_b_v1 + cd_b_v2) * 0.25;
let var_b = (cd_b_h1 - avg_cd_b).abs()
+ (cd_b_h2 - avg_cd_b).abs()
+ (cd_b_v1 - avg_cd_b).abs()
+ (cd_b_v2 - avg_cd_b).abs();
if (cd_b - avg_cd_b).abs() > var_b * 1.5 + 1.0 {
blue_out[idx] = g + avg_cd_b;
}
}
}
// ── Step 5: Write output ─────────────────────────────────────
// Convert from planar f32 to interleaved u16 output
output
.par_chunks_mut(width * 3)
.enumerate()
.for_each(|(y, row)| {
for x in 0..width {
let idx = y * width + x;
let out_idx = x * 3;
row[out_idx] = red_out[idx].round().clamp(0.0, white) as u16;
row[out_idx + 1] = green[idx].round().clamp(0.0, white) as u16;
row[out_idx + 2] = blue_out[idx].round().clamp(0.0, white) as u16;
}
});
Ok(())
}
}
// =============================================================================
// LMMSE — Linear Minimum Mean Square Error
// =============================================================================
/// LMMSE (Linear Minimum Mean Square Error) demosaicing algorithm.
///
/// High-ISO specialist that treats noise as a statistical probability.
/// Particularly effective for images shot at high ISO where noise is prominent.
pub struct Lmmse;
impl Demosaic for Lmmse {
fn demosaic_into(&self, raw: &RawImage, output: &mut [u16]) -> Result<(), DemosaicError> {
let width = raw.active_area().size.width as usize;
let height = raw.active_area().size.height as usize;
let x_off = raw.active_area().origin.x as usize;
let y_off = raw.active_area().origin.y as usize;
let raw_w = raw.width() as usize;
let expected_size = width * height * 3;
if output.len() != expected_size {
return Err(DemosaicError::BufferSizeMismatch {
expected: expected_size,
actual: output.len(),
});
}
if width < 6 || height < 6 {
return Err(DemosaicError::InvalidDimensions);
}
let white = raw.white_level() as f32;
// CFA color at each active-area position
let fc = |x: usize, y: usize| -> u8 {
let ax = x + x_off;
let ay = y + y_off;
match raw.cfa_pattern() {
CfaPattern::Rggb => match (ax % 2, ay % 2) {
(0, 0) => 0,
(1, 0) => 1,
(0, 1) => 3,
_ => 2,
},
CfaPattern::Grbg => match (ax % 2, ay % 2) {
(0, 0) => 1,
(1, 0) => 0,
(0, 1) => 2,
_ => 3,
},
CfaPattern::Gbrg => match (ax % 2, ay % 2) {
(0, 0) => 3,
(1, 0) => 2,
(0, 1) => 0,
_ => 1,
},
CfaPattern::Bggr => match (ax % 2, ay % 2) {
(0, 0) => 2,
(1, 0) => 3,
(0, 1) => 1,
_ => 0,
},
}
};
// Mirror-padded accessor into the raw data (active area coordinates)
let get = |x: isize, y: isize| -> f32 {
let cx = x.clamp(0, (width as isize) - 1) as usize;
let cy = y.clamp(0, (height as isize) - 1) as usize;
raw.data[(cy + y_off) * raw_w + (cx + x_off)] as f32
};
// ── Step 1: Compute horizontal and vertical green estimates ──
let mut gh = vec![0.0f32; width * height];
let mut gv = vec![0.0f32; width * height];
for y in 0..height {
for x in 0..width {
let color = fc(x, y);
let ix = x as isize;
let iy = y as isize;
if color == 1 || color == 3 {
// Green pixel — copy to both directional planes
let val = get(ix, iy);
gh[y * width + x] = val;
gv[y * width + x] = val;
} else {
// Non-green pixel — LMMSE horizontal estimate
// gh = 0.5*(G[x-1]+G[x+1]) + 0.25*(2*raw[x]-raw[x-2]-raw[x+2])
let est_h = 0.5 * (get(ix - 1, iy) + get(ix + 1, iy))
+ 0.25 * (2.0 * get(ix, iy) - get(ix - 2, iy) - get(ix + 2, iy));
gh[y * width + x] = est_h.clamp(0.0, white);
// LMMSE vertical estimate
let est_v = 0.5 * (get(ix, iy - 1) + get(ix, iy + 1))
+ 0.25 * (2.0 * get(ix, iy) - get(ix, iy - 2) - get(ix, iy + 2));
gv[y * width + x] = est_v.clamp(0.0, white);
}
}
}
// ── Step 2: Variance-based adaptive blending of green estimates ──
let mut green = vec![0.0f32; width * height];
let eps = 1e-5f32;
let half_win = 2usize; // 5-pixel window radius
for y in 0..height {
for x in 0..width {
let color = fc(x, y);
if color == 1 || color == 3 {
green[y * width + x] = gh[y * width + x]; // already the raw value
continue;
}
// Compute local variance of (raw - green_est) along each direction
let mut sum_h = 0.0f32;
let mut sum_sq_h = 0.0f32;
let mut sum_v = 0.0f32;
let mut sum_sq_v = 0.0f32;
let mut n = 0.0f32;
for dy in -(half_win as isize)..=(half_win as isize) {
for dx in -(half_win as isize)..=(half_win as isize) {
let nx = (x as isize + dx).clamp(0, (width as isize) - 1) as usize;
let ny = (y as isize + dy).clamp(0, (height as isize) - 1) as usize;
let nidx = ny * width + nx;
let raw_val = get(nx as isize, ny as isize);
let dh = raw_val - gh[nidx];
let dv = raw_val - gv[nidx];
sum_h += dh;
sum_sq_h += dh * dh;
sum_v += dv;
sum_sq_v += dv * dv;
n += 1.0;
}
}
let var_h = (sum_sq_h - sum_h * sum_h / n) / n;
let var_v = (sum_sq_v - sum_v * sum_v / n) / n;
// Weight inversely proportional to variance
let wh = 1.0 / (var_h + eps);
let wv = 1.0 / (var_v + eps);
let idx = y * width + x;
green[idx] = ((wh * gh[idx] + wv * gv[idx]) / (wh + wv)).clamp(0.0, white);
}
}
drop(gh);
drop(gv);
// ── Step 3: R/B interpolation via color-difference bilinear ──
// Compute color differences at known positions, then interpolate.
let mut cd_rg = vec![0.0f32; width * height]; // R - G at R pixels
let mut cd_bg = vec![0.0f32; width * height]; // B - G at B pixels
for y in 0..height {
for x in 0..width {
let idx = y * width + x;
match fc(x, y) {
0 => cd_rg[idx] = get(x as isize, y as isize) - green[idx],
2 => cd_bg[idx] = get(x as isize, y as isize) - green[idx],
_ => {}
}
}
}
let mut red = vec![0.0f32; width * height];
let mut blue = vec![0.0f32; width * height];
for y in 0..height {
for x in 0..width {
let idx = y * width + x;
let color = fc(x, y);
let ix = x as isize;
let iy = y as isize;
match color {
0 => {
// Red pixel — R is known, need B from diagonal bilinear
red[idx] = get(ix, iy);
let mut sum = 0.0f32;
let mut cnt = 0.0f32;
for &(dx, dy) in &[(-1i32, -1i32), (1, -1), (-1, 1), (1, 1)] {
let nx = ix + dx as isize;
let ny = iy + dy as isize;
if nx >= 0 && nx < width as isize && ny >= 0 && ny < height as isize {
let nidx = ny as usize * width + nx as usize;
if fc(nx as usize, ny as usize) == 2 {
sum += cd_bg[nidx];
cnt += 1.0;
}
}
}
blue[idx] = (green[idx] + if cnt > 0.0 { sum / cnt } else { 0.0 })
.clamp(0.0, white);
}
2 => {
// Blue pixel — B is known, need R from diagonal bilinear
blue[idx] = get(ix, iy);
let mut sum = 0.0f32;
let mut cnt = 0.0f32;
for &(dx, dy) in &[(-1i32, -1i32), (1, -1), (-1, 1), (1, 1)] {
let nx = ix + dx as isize;
let ny = iy + dy as isize;
if nx >= 0 && nx < width as isize && ny >= 0 && ny < height as isize {
let nidx = ny as usize * width + nx as usize;
if fc(nx as usize, ny as usize) == 0 {
sum += cd_rg[nidx];
cnt += 1.0;
}
}
}
red[idx] = (green[idx] + if cnt > 0.0 { sum / cnt } else { 0.0 })
.clamp(0.0, white);
}
1 => {
// Green on R-row: R from horizontal neighbors, B from vertical
let mut sr = 0.0f32;
let mut cr = 0.0f32;
for &dx in &[-1i32, 1] {
let nx = ix + dx as isize;
if nx >= 0 && nx < width as isize {
let nidx = y * width + nx as usize;
if fc(nx as usize, y) == 0 {
sr += cd_rg[nidx];
cr += 1.0;
}
}
}
red[idx] =
(green[idx] + if cr > 0.0 { sr / cr } else { 0.0 }).clamp(0.0, white);
let mut sb = 0.0f32;
let mut cb = 0.0f32;
for &dy in &[-1i32, 1] {
let ny = iy + dy as isize;
if ny >= 0 && ny < height as isize {
let nidx = ny as usize * width + x;
if fc(x, ny as usize) == 2 {
sb += cd_bg[nidx];
cb += 1.0;
}
}
}
blue[idx] =
(green[idx] + if cb > 0.0 { sb / cb } else { 0.0 }).clamp(0.0, white);
}
3 => {
// Green on B-row: B from horizontal neighbors, R from vertical
let mut sb = 0.0f32;
let mut cb = 0.0f32;
for &dx in &[-1i32, 1] {
let nx = ix + dx as isize;
if nx >= 0 && nx < width as isize {
let nidx = y * width + nx as usize;
if fc(nx as usize, y) == 2 {
sb += cd_bg[nidx];
cb += 1.0;
}
}
}
blue[idx] =
(green[idx] + if cb > 0.0 { sb / cb } else { 0.0 }).clamp(0.0, white);
let mut sr = 0.0f32;
let mut cr = 0.0f32;
for &dy in &[-1i32, 1] {
let ny = iy + dy as isize;
if ny >= 0 && ny < height as isize {
let nidx = ny as usize * width + x;
if fc(x, ny as usize) == 0 {
sr += cd_rg[nidx];
cr += 1.0;
}
}
}
red[idx] =
(green[idx] + if cr > 0.0 { sr / cr } else { 0.0 }).clamp(0.0, white);
}
_ => unreachable!(),
}
}
}
// ── Step 4: Write interleaved RGB output ──────────────────────
output
.par_chunks_mut(width * 3)
.enumerate()
.for_each(|(y, row)| {
for x in 0..width {
let idx = y * width + x;
let out = x * 3;
row[out] = red[idx].round().clamp(0.0, white) as u16;
row[out + 1] = green[idx].round().clamp(0.0, white) as u16;
row[out + 2] = blue[idx].round().clamp(0.0, white) as u16;
}
});
Ok(())
}
}
// =============================================================================
// RCD — Ratio Corrected Demosaicing
// =============================================================================
/// RCD (Ratio Corrected Demosaicing) algorithm.
///
/// Fast, high-quality alternative to AMaZE that's particularly good for
/// organic shapes and natural textures.
pub struct Rcd;
impl Demosaic for Rcd {
fn demosaic_into(&self, raw: &RawImage, output: &mut [u16]) -> Result<(), DemosaicError> {
let width = raw.active_area().size.width as usize;
let height = raw.active_area().size.height as usize;
let x_off = raw.active_area().origin.x as usize;
let y_off = raw.active_area().origin.y as usize;
let raw_w = raw.width() as usize;
let expected_size = width * height * 3;
if output.len() != expected_size {
return Err(DemosaicError::BufferSizeMismatch {
expected: expected_size,
actual: output.len(),
});
}
if width < 6 || height < 6 {
return Err(DemosaicError::InvalidDimensions);
}
let white = raw.white_level() as f32;
// CFA color at each active-area position
let fc = |x: usize, y: usize| -> u8 {
let ax = x + x_off;
let ay = y + y_off;
match raw.cfa_pattern() {
CfaPattern::Rggb => match (ax % 2, ay % 2) {
(0, 0) => 0,
(1, 0) => 1,
(0, 1) => 3,
_ => 2,
},
CfaPattern::Grbg => match (ax % 2, ay % 2) {
(0, 0) => 1,
(1, 0) => 0,
(0, 1) => 2,
_ => 3,
},
CfaPattern::Gbrg => match (ax % 2, ay % 2) {
(0, 0) => 3,
(1, 0) => 2,
(0, 1) => 0,
_ => 1,
},
CfaPattern::Bggr => match (ax % 2, ay % 2) {
(0, 0) => 2,
(1, 0) => 3,
(0, 1) => 1,
_ => 0,
},
}
};
// Mirror-padded accessor
let get = |x: isize, y: isize| -> f32 {
let cx = x.clamp(0, (width as isize) - 1) as usize;
let cy = y.clamp(0, (height as isize) - 1) as usize;
raw.data[(cy + y_off) * raw_w + (cx + x_off)] as f32
};
// ── Step 1: Green channel interpolation (adaptive directional) ──
let mut green = vec![0.0f32; width * height];
for y in 0..height {
for x in 0..width {
let color = fc(x, y);
let ix = x as isize;
let iy = y as isize;
if color == 1 || color == 3 {
green[y * width + x] = get(ix, iy);
} else {
// RCD uses same adaptive directional green interpolation as AMaZE
let dh = (get(ix - 1, iy) - get(ix + 1, iy)).abs()
+ (2.0 * get(ix, iy) - get(ix - 2, iy) - get(ix + 2, iy)).abs();
let dv = (get(ix, iy - 1) - get(ix, iy + 1)).abs()
+ (2.0 * get(ix, iy) - get(ix, iy - 2) - get(ix, iy + 2)).abs();
let gh = (get(ix - 1, iy) + get(ix + 1, iy)) * 0.5
+ (2.0 * get(ix, iy) - get(ix - 2, iy) - get(ix + 2, iy)) * 0.25;
let gv = (get(ix, iy - 1) + get(ix, iy + 1)) * 0.5
+ (2.0 * get(ix, iy) - get(ix, iy - 2) - get(ix, iy + 2)) * 0.25;
let eps = 1e-5f32;
let g = if dh < dv * 0.5 {
gh
} else if dv < dh * 0.5 {
gv
} else {
let wh = 1.0 / (dh + eps);
let wv = 1.0 / (dv + eps);
(wh * gh + wv * gv) / (wh + wv)
};
green[y * width + x] = g.max(0.0);
}
}
}
// ── Step 2: R/B reconstruction using color ratios ─────────────
// The RCD insight: R/G and B/G ratios are smoother than R-G differences.
// We interpolate ratios and then multiply back by the interpolated green.
// First build ratio planes at known positions.
// ratio_rg[idx] = R[idx] / G[idx] at R pixels (else 1.0 placeholder)
// ratio_bg[idx] = B[idx] / G[idx] at B pixels (else 1.0 placeholder)
let mut ratio_rg = vec![1.0f32; width * height];
let mut ratio_bg = vec![1.0f32; width * height];
for y in 0..height {
for x in 0..width {
let idx = y * width + x;
let g = green[idx].max(1.0); // avoid division by zero
match fc(x, y) {
0 => ratio_rg[idx] = get(x as isize, y as isize) / g,
2 => ratio_bg[idx] = get(x as isize, y as isize) / g,
_ => {}
}
}
}
let mut red = vec![0.0f32; width * height];
let mut blue = vec![0.0f32; width * height];
for y in 0..height {
for x in 0..width {
let idx = y * width + x;
let color = fc(x, y);
let ix = x as isize;
let iy = y as isize;
match color {
0 => {
// R pixel — R is known; B interpolated from diagonal ratio bilinear
red[idx] = get(ix, iy);
let mut sum_ratio = 0.0f32;
let mut cnt = 0.0f32;
for &(dx, dy) in &[(-1i32, -1i32), (1, -1), (-1, 1), (1, 1)] {
let nx = ix + dx as isize;
let ny = iy + dy as isize;
if nx >= 0
&& nx < width as isize
&& ny >= 0
&& ny < height as isize
&& fc(nx as usize, ny as usize) == 2
{
sum_ratio += ratio_bg[ny as usize * width + nx as usize];
cnt += 1.0;
}
}
let r = if cnt > 0.0 { sum_ratio / cnt } else { 1.0 };
blue[idx] = (green[idx] * r).clamp(0.0, white);
}
2 => {
// B pixel — B is known; R interpolated from diagonal ratio bilinear
blue[idx] = get(ix, iy);
let mut sum_ratio = 0.0f32;
let mut cnt = 0.0f32;
for &(dx, dy) in &[(-1i32, -1i32), (1, -1), (-1, 1), (1, 1)] {
let nx = ix + dx as isize;
let ny = iy + dy as isize;
if nx >= 0
&& nx < width as isize
&& ny >= 0
&& ny < height as isize
&& fc(nx as usize, ny as usize) == 0
{
sum_ratio += ratio_rg[ny as usize * width + nx as usize];
cnt += 1.0;
}
}
let r = if cnt > 0.0 { sum_ratio / cnt } else { 1.0 };
red[idx] = (green[idx] * r).clamp(0.0, white);
}
1 => {
// Green on R-row:
// R from horizontal R/G ratio pairs
// B from vertical B/G ratio pairs
let mut sr = 0.0f32;
let mut cr = 0.0f32;
for &dx in &[-1i32, 1] {
let nx = ix + dx as isize;
if nx >= 0 && nx < width as isize && fc(nx as usize, y) == 0 {
sr += ratio_rg[y * width + nx as usize];
cr += 1.0;
}
}
let rr = if cr > 0.0 { sr / cr } else { 1.0 };
red[idx] = (green[idx] * rr).clamp(0.0, white);
let mut sb = 0.0f32;
let mut cb = 0.0f32;
for &dy in &[-1i32, 1] {
let ny = iy + dy as isize;
if ny >= 0 && ny < height as isize && fc(x, ny as usize) == 2 {
sb += ratio_bg[ny as usize * width + x];
cb += 1.0;
}
}
let rb = if cb > 0.0 { sb / cb } else { 1.0 };
blue[idx] = (green[idx] * rb).clamp(0.0, white);
}
3 => {
// Green on B-row:
// B from horizontal B/G ratio pairs
// R from vertical R/G ratio pairs
let mut sb = 0.0f32;
let mut cb = 0.0f32;
for &dx in &[-1i32, 1] {
let nx = ix + dx as isize;
if nx >= 0 && nx < width as isize && fc(nx as usize, y) == 2 {
sb += ratio_bg[y * width + nx as usize];
cb += 1.0;
}
}
let rb = if cb > 0.0 { sb / cb } else { 1.0 };
blue[idx] = (green[idx] * rb).clamp(0.0, white);
let mut sr = 0.0f32;
let mut cr = 0.0f32;
for &dy in &[-1i32, 1] {
let ny = iy + dy as isize;
if ny >= 0 && ny < height as isize && fc(x, ny as usize) == 0 {
sr += ratio_rg[ny as usize * width + x];
cr += 1.0;
}
}
let rr = if cr > 0.0 { sr / cr } else { 1.0 };
red[idx] = (green[idx] * rr).clamp(0.0, white);
}
_ => unreachable!(),
}
}
}
// ── Step 3: Write interleaved RGB output ──────────────────────
output
.par_chunks_mut(width * 3)
.enumerate()
.for_each(|(y, row)| {
for x in 0..width {
let idx = y * width + x;
let out = x * 3;
row[out] = red[idx].round().clamp(0.0, white) as u16;
row[out + 1] = green[idx].round().clamp(0.0, white) as u16;
row[out + 2] = blue[idx].round().clamp(0.0, white) as u16;
}
});
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::image::{Point, Rect, Size};
fn create_test_raw(width: u32, height: u32, pattern: CfaPattern, value: u16) -> RawImage {
let size = Size::new(width, height);
let active_area = Rect::new(Point::ORIGIN, size);
RawImage::builder(size, active_area, 14, pattern)
.white_level(16383)
.data(vec![value; (width * height) as usize])
.build()
}
fn create_gradient_raw(width: u32, height: u32, pattern: CfaPattern) -> RawImage {
let size = Size::new(width, height);
let active_area = Rect::new(Point::ORIGIN, size);
let mut data = vec![0u16; (width * height) as usize];
for y in 0..height {
for x in 0..width {
// Smooth gradient — easy for demosaic
let val =
((x as f32 / width as f32 + y as f32 / height as f32) * 0.5 * 8000.0) as u16;
data[(y * width + x) as usize] = val;
}
}
RawImage::builder(size, active_area, 14, pattern)
.white_level(16383)
.data(data)
.build()
}
#[test]
fn test_amaze_correct_output_size() {
let raw = create_test_raw(20, 20, CfaPattern::Rggb, 5000);
let mut output = vec![0u16; 20 * 20 * 3];
assert!(Amaze.demosaic_into(&raw, &mut output).is_ok());
}
#[test]
fn test_amaze_wrong_buffer_size() {
let raw = create_test_raw(20, 20, CfaPattern::Rggb, 5000);
let mut output = vec![0u16; 50];
assert!(matches!(
Amaze.demosaic_into(&raw, &mut output),
Err(DemosaicError::BufferSizeMismatch { .. })
));
}
#[test]
fn test_amaze_too_small_image() {
let raw = create_test_raw(4, 4, CfaPattern::Rggb, 5000);
let mut output = vec![0u16; 4 * 4 * 3];
assert!(matches!(
Amaze.demosaic_into(&raw, &mut output),
Err(DemosaicError::InvalidDimensions)
));
}
#[test]
fn test_amaze_uniform_produces_uniform() {
let raw = create_test_raw(20, 20, CfaPattern::Rggb, 5000);
let rgb = Amaze.demosaic(&raw);
// Interior pixels should be close to 5000
for y in 4..16 {
for x in 4..16 {
let idx = (y * 20 + x) * 3;
for c in 0..3 {
let val = rgb.data[idx + c];
assert!(
(val as i32 - 5000).abs() < 500,
"pixel ({},{}) ch {} = {}, expected ~5000",
x,
y,
c,
val
);
}
}
}
}
#[test]
fn test_amaze_all_cfa_patterns() {
for pattern in [
CfaPattern::Rggb,
CfaPattern::Grbg,
CfaPattern::Gbrg,
CfaPattern::Bggr,
] {
let raw = create_test_raw(20, 20, pattern, 3000);
let rgb = Amaze.demosaic(&raw);
assert_eq!(rgb.width(), 20);
assert_eq!(rgb.height(), 20);
assert_eq!(rgb.data.len(), 20 * 20 * 3);
// All values should be non-negative and bounded
for val in &rgb.data {
assert!(
*val <= 16383,
"pattern {:?}: value {} too high",
pattern,
val
);
}
}
}
#[test]
fn test_amaze_gradient_smooth() {
let raw = create_gradient_raw(40, 40, CfaPattern::Rggb);
let rgb = Amaze.demosaic(&raw);
// Check that output is reasonably smooth (no huge jumps between neighbors)
for y in 5..35 {
for x in 5..35 {
let idx = (y * 40 + x) * 3;
let idx_right = (y * 40 + x + 1) * 3;
let idx_down = ((y + 1) * 40 + x) * 3;
for c in 0..3 {
let diff_h = (rgb.data[idx + c] as i32 - rgb.data[idx_right + c] as i32).abs();
let diff_v = (rgb.data[idx + c] as i32 - rgb.data[idx_down + c] as i32).abs();
assert!(
diff_h < 1000,
"horizontal jump at ({},{}) ch {}: {}",
x,
y,
c,
diff_h
);
assert!(
diff_v < 1000,
"vertical jump at ({},{}) ch {}: {}",
x,
y,
c,
diff_v
);
}
}
}
}
#[test]
fn test_amaze_preserves_known_green() {
// Create image where all values are distinct
let mut raw = create_test_raw(10, 10, CfaPattern::Rggb, 0);
// Set known green pixel at (1,0) which is G in RGGB
raw.data[1] = 7000;
let rgb = Amaze.demosaic(&raw);
// Green channel at (1,0) should be exactly 7000
// pixel (1, 0): row=0, col=1, so index = 1 * 3 + 1 = 4
let g = rgb.data[3 + 1];
assert_eq!(
g, 7000,
"green pixel should be preserved exactly, got {}",
g
);
}
#[test]
fn test_amaze_with_active_area() {
let size = Size::new(30, 30);
let active_area = Rect::from_coords(5, 5, 20, 20);
let raw = RawImage::builder(size, active_area, 14, CfaPattern::Rggb)
.white_level(16383)
.data(vec![4000u16; 30 * 30])
.build();
let rgb = Amaze.demosaic(&raw);
assert_eq!(rgb.width(), 20);
assert_eq!(rgb.height(), 20);
assert_eq!(rgb.data.len(), 20 * 20 * 3);
}
#[test]
fn test_amaze_respects_white_level_clamp() {
// Feed values at white_level — output must never exceed white_level
let white_level: u16 = 16383;
let raw = create_test_raw(20, 20, CfaPattern::Rggb, white_level);
let mut output = vec![0u16; 20 * 20 * 3];
Amaze.demosaic_into(&raw, &mut output).unwrap();
for (i, &v) in output.iter().enumerate() {
assert!(
v <= white_level,
"pixel {} has value {} exceeding white_level {}",
i,
v,
white_level
);
}
}
// ── LMMSE tests ───────────────────────────────────────────────────────────
#[test]
fn test_lmmse_correct_output_size() {
let raw = create_test_raw(20, 20, CfaPattern::Rggb, 5000);
let mut output = vec![0u16; 20 * 20 * 3];
assert!(Lmmse.demosaic_into(&raw, &mut output).is_ok());
}
#[test]
fn test_lmmse_wrong_buffer_size() {
let raw = create_test_raw(20, 20, CfaPattern::Rggb, 5000);
let mut output = vec![0u16; 50];
assert!(matches!(
Lmmse.demosaic_into(&raw, &mut output),
Err(DemosaicError::BufferSizeMismatch { .. })
));
}
#[test]
fn test_lmmse_too_small_image() {
let raw = create_test_raw(4, 4, CfaPattern::Rggb, 5000);
let mut output = vec![0u16; 4 * 4 * 3];
assert!(matches!(
Lmmse.demosaic_into(&raw, &mut output),
Err(DemosaicError::InvalidDimensions)
));
}
#[test]
fn test_lmmse_uniform_produces_uniform() {
let raw = create_test_raw(20, 20, CfaPattern::Rggb, 5000);
let rgb = Lmmse.demosaic(&raw);
for y in 4..16 {
for x in 4..16 {
let idx = (y * 20 + x) * 3;
for c in 0..3 {
let val = rgb.data[idx + c];
assert!(
(val as i32 - 5000).abs() < 500,
"LMMSE pixel ({},{}) ch {} = {}, expected ~5000",
x,
y,
c,
val
);
}
}
}
}
#[test]
fn test_lmmse_all_cfa_patterns() {
for pattern in [
CfaPattern::Rggb,
CfaPattern::Grbg,
CfaPattern::Gbrg,
CfaPattern::Bggr,
] {
let raw = create_test_raw(20, 20, pattern, 3000);
let rgb = Lmmse.demosaic(&raw);
assert_eq!(rgb.width(), 20);
assert_eq!(rgb.height(), 20);
assert_eq!(rgb.data.len(), 20 * 20 * 3);
for val in &rgb.data {
assert!(
*val <= 16383,
"LMMSE pattern {:?}: value {} too high",
pattern,
val
);
}
}
}
#[test]
fn test_lmmse_gradient_smooth() {
let raw = create_gradient_raw(40, 40, CfaPattern::Rggb);
let rgb = Lmmse.demosaic(&raw);
for y in 5..35 {
for x in 5..35 {
let idx = (y * 40 + x) * 3;
let idx_right = (y * 40 + x + 1) * 3;
let idx_down = ((y + 1) * 40 + x) * 3;
for c in 0..3 {
let diff_h = (rgb.data[idx + c] as i32 - rgb.data[idx_right + c] as i32).abs();
let diff_v = (rgb.data[idx + c] as i32 - rgb.data[idx_down + c] as i32).abs();
assert!(
diff_h < 1000,
"LMMSE horizontal jump at ({},{}) ch {}: {}",
x,
y,
c,
diff_h
);
assert!(
diff_v < 1000,
"LMMSE vertical jump at ({},{}) ch {}: {}",
x,
y,
c,
diff_v
);
}
}
}
}
#[test]
fn test_lmmse_with_active_area() {
let size = Size::new(30, 30);
let active_area = Rect::from_coords(5, 5, 20, 20);
let raw = RawImage::builder(size, active_area, 14, CfaPattern::Rggb)
.white_level(16383)
.data(vec![4000u16; 30 * 30])
.build();
let rgb = Lmmse.demosaic(&raw);
assert_eq!(rgb.width(), 20);
assert_eq!(rgb.height(), 20);
assert_eq!(rgb.data.len(), 20 * 20 * 3);
}
// ── RCD tests ─────────────────────────────────────────────────────────────
#[test]
fn test_rcd_correct_output_size() {
let raw = create_test_raw(20, 20, CfaPattern::Rggb, 5000);
let mut output = vec![0u16; 20 * 20 * 3];
assert!(Rcd.demosaic_into(&raw, &mut output).is_ok());
}
#[test]
fn test_rcd_wrong_buffer_size() {
let raw = create_test_raw(20, 20, CfaPattern::Rggb, 5000);
let mut output = vec![0u16; 50];
assert!(matches!(
Rcd.demosaic_into(&raw, &mut output),
Err(DemosaicError::BufferSizeMismatch { .. })
));
}
#[test]
fn test_rcd_too_small_image() {
let raw = create_test_raw(4, 4, CfaPattern::Rggb, 5000);
let mut output = vec![0u16; 4 * 4 * 3];
assert!(matches!(
Rcd.demosaic_into(&raw, &mut output),
Err(DemosaicError::InvalidDimensions)
));
}
#[test]
fn test_rcd_uniform_produces_uniform() {
let raw = create_test_raw(20, 20, CfaPattern::Rggb, 5000);
let rgb = Rcd.demosaic(&raw);
for y in 4..16 {
for x in 4..16 {
let idx = (y * 20 + x) * 3;
for c in 0..3 {
let val = rgb.data[idx + c];
assert!(
(val as i32 - 5000).abs() < 500,
"RCD pixel ({},{}) ch {} = {}, expected ~5000",
x,
y,
c,
val
);
}
}
}
}
#[test]
fn test_rcd_all_cfa_patterns() {
for pattern in [
CfaPattern::Rggb,
CfaPattern::Grbg,
CfaPattern::Gbrg,
CfaPattern::Bggr,
] {
let raw = create_test_raw(20, 20, pattern, 3000);
let rgb = Rcd.demosaic(&raw);
assert_eq!(rgb.width(), 20);
assert_eq!(rgb.height(), 20);
assert_eq!(rgb.data.len(), 20 * 20 * 3);
for val in &rgb.data {
assert!(
*val <= 16383,
"RCD pattern {:?}: value {} too high",
pattern,
val
);
}
}
}
#[test]
fn test_rcd_gradient_smooth() {
let raw = create_gradient_raw(40, 40, CfaPattern::Rggb);
let rgb = Rcd.demosaic(&raw);
for y in 5..35 {
for x in 5..35 {
let idx = (y * 40 + x) * 3;
let idx_right = (y * 40 + x + 1) * 3;
let idx_down = ((y + 1) * 40 + x) * 3;
for c in 0..3 {
let diff_h = (rgb.data[idx + c] as i32 - rgb.data[idx_right + c] as i32).abs();
let diff_v = (rgb.data[idx + c] as i32 - rgb.data[idx_down + c] as i32).abs();
assert!(
diff_h < 1000,
"RCD horizontal jump at ({},{}) ch {}: {}",
x,
y,
c,
diff_h
);
assert!(
diff_v < 1000,
"RCD vertical jump at ({},{}) ch {}: {}",
x,
y,
c,
diff_v
);
}
}
}
}
#[test]
fn test_rcd_with_active_area() {
let size = Size::new(30, 30);
let active_area = Rect::from_coords(5, 5, 20, 20);
let raw = RawImage::builder(size, active_area, 14, CfaPattern::Rggb)
.white_level(16383)
.data(vec![4000u16; 30 * 30])
.build();
let rgb = Rcd.demosaic(&raw);
assert_eq!(rgb.width(), 20);
assert_eq!(rgb.height(), 20);
assert_eq!(rgb.data.len(), 20 * 20 * 3);
}
}