zenjpeg 0.7.0

Pure Rust JPEG encoder/decoder with perceptual optimizations
Documentation
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
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
//! Core types for the v2 encoder API.

/// Quality/compression setting.
///
/// All variants map to internal quality through empirical lookup tables.
/// Results vary by image - these are rough approximations, not guarantees.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub enum Quality {
    /// Approximate jpegli quality scale (this is a fork, not exact jpegli).
    /// Range: 0.0–100.0, where ~90 is visually lossless for most images.
    ApproxJpegli(f32),

    /// Approximate mozjpeg quality behavior.
    /// Range: 0–100. Maps to quality producing similar file sizes.
    ApproxMozjpeg(u8),

    /// Approximate SSIMULACRA2 score target.
    /// Range: 0–100 (higher = better). 90+ is roughly visually lossless.
    ApproxSsim2(f32),

    /// Approximate Butteraugli distance target.
    /// Range: 0.0+ (lower = better). <1.0 excellent, <3.0 good.
    ApproxButteraugli(f32),
}

impl Default for Quality {
    fn default() -> Self {
        Quality::ApproxJpegli(90.0)
    }
}

impl From<f32> for Quality {
    fn from(q: f32) -> Self {
        Quality::ApproxJpegli(q)
    }
}

impl From<u8> for Quality {
    fn from(q: u8) -> Self {
        Quality::ApproxJpegli(q as f32)
    }
}

impl From<i32> for Quality {
    fn from(q: i32) -> Self {
        Quality::ApproxJpegli(q as f32)
    }
}

impl Quality {
    /// Convert to internal quality value (0.0-100.0 scale).
    #[must_use]
    pub fn to_internal(&self) -> f32 {
        match self {
            Quality::ApproxJpegli(q) => *q,
            Quality::ApproxMozjpeg(q) => mozjpeg_to_internal(*q),
            Quality::ApproxSsim2(score) => ssim2_to_internal(*score),
            Quality::ApproxButteraugli(dist) => butteraugli_to_internal(*dist),
        }
    }

    /// Quality value for mozjpeg's Robidoux table scaling formula.
    ///
    /// For `ApproxMozjpeg(q)`, returns `q` unchanged — the user specified a
    /// mozjpeg quality and expects tables matching `jpeg_set_quality(q)` with
    /// Robidoux base tables.
    ///
    /// For other variants, falls back to `to_internal()` as a best-effort
    /// mapping. This is imprecise (jpegli and mozjpeg quality scales differ
    /// substantially) but there's no lossless conversion between them.
    #[must_use]
    pub fn for_mozjpeg_tables(&self) -> u8 {
        match self {
            Quality::ApproxMozjpeg(q) => *q,
            _ => self.to_internal().round().clamp(1.0, 100.0) as u8,
        }
    }

    /// Convert to butteraugli distance.
    ///
    /// Uses the exact same formula as C++ jpegli's `jpegli_quality_to_distance`.
    #[must_use]
    pub fn to_distance(&self) -> f32 {
        // If already butteraugli distance, return it directly
        if let Quality::ApproxButteraugli(d) = self {
            return *d;
        }
        // Exact C++ jpegli formula from lib/jpegli/encode.cc:jpegli_quality_to_distance
        let q = self.to_internal();
        if q >= 100.0 {
            0.01
        } else if q >= 30.0 {
            0.1 + (100.0 - q) * 0.09
        } else {
            // Quadratic for very low quality
            53.0 / 3000.0 * q * q - 23.0 / 20.0 * q + 25.0
        }
    }
}

// Calibrated mozjpeg→jpegli quality mapping (4:4:4, DSSIM metric)
// From corpus testing on CID22-512 and Kodak datasets
const MOZJPEG_TO_JPEGLI: [(u8, u8); 10] = [
    (30, 28),
    (40, 37),
    (50, 47),
    (60, 55),
    (70, 65),
    (75, 71),
    (80, 77),
    (85, 83),
    (90, 89),
    (95, 94),
];

fn mozjpeg_to_internal(q: u8) -> f32 {
    if q >= 100 {
        return 100.0;
    }
    if q <= 30 {
        // Extrapolate below table range
        return (q as f32 / 30.0) * 28.0;
    }

    // Find bracketing entries and interpolate
    let mut lower = (30u8, 28u8);
    let mut upper = (95u8, 94u8);

    for &(moz_q, jpegli_q) in &MOZJPEG_TO_JPEGLI {
        if moz_q <= q && moz_q > lower.0 {
            lower = (moz_q, jpegli_q);
        }
        if moz_q >= q && moz_q < upper.0 {
            upper = (moz_q, jpegli_q);
        }
    }

    if lower.0 == upper.0 {
        return lower.1 as f32;
    }

    // Linear interpolation
    let t = (q - lower.0) as f32 / (upper.0 - lower.0) as f32;
    lower.1 as f32 + t * (upper.1 as f32 - lower.1 as f32)
}

// Calibrated SSIMULACRA2→jpegli quality mapping (4:4:4)
// SSIM2 scores: higher is better, 100 = identical
const SSIM2_TO_JPEGLI: [(u8, u8); 8] = [
    (70, 55), // Low quality
    (75, 65),
    (80, 73),
    (85, 80),
    (88, 85),
    (90, 88),
    (93, 92),
    (95, 95),
];

fn ssim2_to_internal(score: f32) -> f32 {
    if score >= 100.0 {
        return 100.0;
    }
    if score <= 70.0 {
        return (score / 70.0) * 55.0;
    }

    let q = score as u8;
    let mut lower = (70u8, 55u8);
    let mut upper = (95u8, 95u8);

    for &(ssim_score, jpegli_q) in &SSIM2_TO_JPEGLI {
        if ssim_score <= q && ssim_score > lower.0 {
            lower = (ssim_score, jpegli_q);
        }
        if ssim_score >= q && ssim_score < upper.0 {
            upper = (ssim_score, jpegli_q);
        }
    }

    if lower.0 == upper.0 {
        return lower.1 as f32;
    }

    let t = (score - lower.0 as f32) / (upper.0 - lower.0) as f32;
    lower.1 as f32 + t * (upper.1 as f32 - lower.1 as f32)
}

// Calibrated butteraugli→jpegli quality mapping
// Butteraugli: lower is better, 0 = identical, <1 excellent, <3 good
const BUTTERAUGLI_TO_JPEGLI: [(f32, f32); 7] = [
    (0.3, 96.0),
    (0.5, 93.0),
    (1.0, 88.0),
    (1.5, 82.0),
    (2.0, 76.0),
    (3.0, 68.0),
    (5.0, 55.0),
];

fn butteraugli_to_internal(dist: f32) -> f32 {
    if dist <= 0.0 {
        return 100.0;
    }
    if dist <= 0.3 {
        return 96.0 + (0.3 - dist) / 0.3 * 4.0;
    }
    if dist >= 5.0 {
        return 55.0 - (dist - 5.0) * 3.0;
    }

    let mut lower = (0.3f32, 96.0f32);
    let mut upper = (5.0f32, 55.0f32);

    for &(ba_dist, jpegli_q) in &BUTTERAUGLI_TO_JPEGLI {
        if ba_dist <= dist && ba_dist > lower.0 {
            lower = (ba_dist, jpegli_q);
        }
        if ba_dist >= dist && ba_dist < upper.0 {
            upper = (ba_dist, jpegli_q);
        }
    }

    if (lower.0 - upper.0).abs() < 0.001 {
        return lower.1;
    }

    let t = (dist - lower.0) / (upper.0 - lower.0);
    lower.1 + t * (upper.1 - lower.1)
}

/// Output color space with bundled subsampling options.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ColorMode {
    /// Standard YCbCr with configurable chroma subsampling.
    YCbCr { subsampling: ChromaSubsampling },

    /// XYB perceptual color space (jpegli-specific).
    /// Computed internally from linear RGB input.
    Xyb { subsampling: XybSubsampling },

    /// Single-channel grayscale.
    Grayscale,
}

impl Default for ColorMode {
    fn default() -> Self {
        ColorMode::YCbCr {
            subsampling: ChromaSubsampling::None, // 4:4:4 - no subsampling
        }
    }
}

/// YCbCr chroma subsampling (spatial resolution).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ChromaSubsampling {
    /// 4:4:4 - No subsampling (full chroma resolution, best quality, largest files)
    None,
    /// 4:2:2 - Half horizontal resolution
    HalfHorizontal,
    /// 4:2:0 - Quarter resolution (half each direction, most common)
    Quarter,
    /// 4:4:0 - Half vertical resolution
    HalfVertical,
}

impl ChromaSubsampling {
    /// Horizontal subsampling factor (1 or 2).
    #[must_use]
    pub const fn h_factor(&self) -> u8 {
        match self {
            ChromaSubsampling::None | ChromaSubsampling::HalfVertical => 1,
            ChromaSubsampling::HalfHorizontal | ChromaSubsampling::Quarter => 2,
        }
    }

    /// Vertical subsampling factor (1 or 2).
    #[must_use]
    pub const fn v_factor(&self) -> u8 {
        match self {
            ChromaSubsampling::None | ChromaSubsampling::HalfHorizontal => 1,
            ChromaSubsampling::HalfVertical | ChromaSubsampling::Quarter => 2,
        }
    }

    /// Returns the horizontal sampling factor for luma.
    ///
    /// This is the luma block count in horizontal direction per MCU.
    /// Returns 1 for 4:4:4/4:4:0, returns 2 for 4:2:0/4:2:2.
    #[must_use]
    pub const fn h_samp_factor_luma(self) -> u8 {
        self.h_factor()
    }

    /// Returns the vertical sampling factor for luma.
    ///
    /// This is the luma block count in vertical direction per MCU.
    /// Returns 1 for 4:4:4/4:2:2, returns 2 for 4:2:0/4:4:0.
    #[must_use]
    pub const fn v_samp_factor_luma(self) -> u8 {
        self.v_factor()
    }

    /// Returns the MCU (Minimum Coded Unit) size for this subsampling mode.
    ///
    /// - 8 for 4:4:4 (no subsampling)
    /// - 16 for modes with 2x sampling (4:2:0, 4:2:2, 4:4:0)
    #[must_use]
    pub const fn mcu_size(self) -> usize {
        match self {
            ChromaSubsampling::None => 8,
            ChromaSubsampling::Quarter
            | ChromaSubsampling::HalfHorizontal
            | ChromaSubsampling::HalfVertical => 16,
        }
    }
}

/// XYB component subsampling.
///
/// Unlike YCbCr where only luma is full, XYB keeps X and Y full
/// even in subsampled mode - only B is reduced.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum XybSubsampling {
    /// X, Y, B all at full resolution (1x1, 1x1, 1x1)
    Full,
    /// X, Y full, B at quarter resolution (1x1, 1x1, 2x2)
    #[default]
    BQuarter,
}

/// Chroma downsampling algorithm for RGB->YCbCr conversion.
///
/// **Only applies to RGB/RGBX input.** Ignored for grayscale, YCbCr, and planar input.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum DownsamplingMethod {
    /// Simple box filter averaging (fast, matches C++ jpegli default)
    #[default]
    Box,
    /// Gamma-aware averaging (better color accuracy at edges)
    GammaAware,
    /// Iterative optimization (SharpYUV-style, best quality, ~3x slower)
    GammaAwareIterative,
}

impl DownsamplingMethod {
    /// Returns true if this method uses gamma-aware downsampling.
    #[must_use]
    pub const fn uses_gamma_aware(self) -> bool {
        matches!(self, Self::GammaAware | Self::GammaAwareIterative)
    }
}

/// Pixel data layout for raw byte input.
///
/// Describes channel order, bit depth, and color space interpretation.
/// Use with `encode_from_bytes()` when working with raw buffers.
///
/// For rgb crate types, use `encode_from_rgb()` which infers layout.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PixelLayout {
    // === 8-bit sRGB (gamma-encoded) ===
    /// RGB, 3 bytes/pixel, sRGB gamma
    Rgb8Srgb,
    /// BGR, 3 bytes/pixel, sRGB gamma (Windows/GDI order)
    Bgr8Srgb,
    /// RGBX, 4 bytes/pixel, sRGB gamma (4th byte ignored)
    Rgbx8Srgb,
    /// RGBA, 4 bytes/pixel, sRGB gamma (alpha ignored on encode)
    Rgba8Srgb,
    /// BGRX, 4 bytes/pixel, sRGB gamma (4th byte ignored)
    Bgrx8Srgb,
    /// BGRA, 4 bytes/pixel, sRGB gamma (alpha ignored on encode)
    Bgra8Srgb,
    /// Grayscale, 1 byte/pixel, sRGB gamma
    Gray8Srgb,

    // === 16-bit linear ===
    /// RGB, 6 bytes/pixel, linear light (0-65535)
    Rgb16Linear,
    /// RGBX, 8 bytes/pixel, linear light (4th channel ignored)
    Rgbx16Linear,
    /// RGBA, 8 bytes/pixel, linear light (alpha ignored on encode)
    Rgba16Linear,
    /// Grayscale, 2 bytes/pixel, linear light
    Gray16Linear,

    // === 32-bit float linear ===
    /// RGB, 12 bytes/pixel, linear light (0.0-1.0)
    RgbF32Linear,
    /// RGBX, 16 bytes/pixel, linear light (4th channel ignored)
    RgbxF32Linear,
    /// RGBA, 16 bytes/pixel, linear light (alpha ignored on encode)
    RgbaF32Linear,
    /// Grayscale, 4 bytes/pixel, linear light
    GrayF32Linear,

    // === Pre-converted YCbCr (skip RGB->YCbCr conversion) ===
    /// YCbCr interleaved, 3 bytes/pixel, u8
    YCbCr8,
    /// YCbCr interleaved, 12 bytes/pixel, f32
    YCbCrF32,
}

impl PixelLayout {
    /// Bytes per pixel for this layout.
    #[must_use]
    pub const fn bytes_per_pixel(&self) -> usize {
        match self {
            Self::Gray8Srgb => 1,
            Self::Gray16Linear => 2,
            Self::Rgb8Srgb | Self::Bgr8Srgb | Self::YCbCr8 => 3,
            Self::Rgbx8Srgb
            | Self::Rgba8Srgb
            | Self::Bgrx8Srgb
            | Self::Bgra8Srgb
            | Self::GrayF32Linear => 4,
            Self::Rgb16Linear => 6,
            Self::Rgbx16Linear | Self::Rgba16Linear => 8,
            Self::RgbF32Linear | Self::YCbCrF32 => 12,
            Self::RgbxF32Linear | Self::RgbaF32Linear => 16,
        }
    }

    /// Number of channels (including ignored channels).
    #[must_use]
    pub const fn channels(&self) -> usize {
        match self {
            Self::Gray8Srgb | Self::Gray16Linear | Self::GrayF32Linear => 1,
            Self::Rgb8Srgb
            | Self::Bgr8Srgb
            | Self::Rgb16Linear
            | Self::RgbF32Linear
            | Self::YCbCr8
            | Self::YCbCrF32 => 3,
            Self::Rgbx8Srgb
            | Self::Rgba8Srgb
            | Self::Bgrx8Srgb
            | Self::Bgra8Srgb
            | Self::Rgbx16Linear
            | Self::Rgba16Linear
            | Self::RgbxF32Linear
            | Self::RgbaF32Linear => 4,
        }
    }

    /// Whether this is a grayscale format.
    #[must_use]
    pub const fn is_grayscale(&self) -> bool {
        matches!(
            self,
            Self::Gray8Srgb | Self::Gray16Linear | Self::GrayF32Linear
        )
    }

    /// Whether this is pre-converted YCbCr.
    #[must_use]
    pub const fn is_ycbcr(&self) -> bool {
        matches!(self, Self::YCbCr8 | Self::YCbCrF32)
    }

    /// Whether this uses BGR channel order.
    #[must_use]
    pub const fn is_bgr(&self) -> bool {
        matches!(self, Self::Bgr8Srgb | Self::Bgrx8Srgb | Self::Bgra8Srgb)
    }

    /// Whether this is a float format (linear color space).
    #[must_use]
    pub const fn is_float(&self) -> bool {
        matches!(
            self,
            Self::RgbF32Linear
                | Self::RgbxF32Linear
                | Self::RgbaF32Linear
                | Self::GrayF32Linear
                | Self::YCbCrF32
        )
    }

    /// Whether this is a 16-bit format (linear color space).
    #[must_use]
    pub const fn is_16bit(&self) -> bool {
        matches!(
            self,
            Self::Rgb16Linear | Self::Rgbx16Linear | Self::Rgba16Linear | Self::Gray16Linear
        )
    }
}

/// Planar YCbCr data for a strip of rows.
///
/// Each plane has its own stride. All planes are f32.
#[derive(Clone, Copy, Debug)]
pub struct YCbCrPlanes<'a> {
    pub y: &'a [f32],
    pub y_stride: usize,
    pub cb: &'a [f32],
    pub cb_stride: usize,
    pub cr: &'a [f32],
    pub cr_stride: usize,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::PixelFormat;

    #[test]
    fn test_quality_default() {
        let q = Quality::default();
        assert!(matches!(q, Quality::ApproxJpegli(90.0)));
    }

    #[test]
    fn test_quality_from() {
        let q: Quality = 85.0.into();
        assert!(matches!(q, Quality::ApproxJpegli(85.0)));

        let q: Quality = 75u8.into();
        assert!(matches!(q, Quality::ApproxJpegli(75.0)));
    }

    #[test]
    fn test_pixel_layout_bytes() {
        assert_eq!(PixelLayout::Rgb8Srgb.bytes_per_pixel(), 3);
        assert_eq!(PixelLayout::Rgbx8Srgb.bytes_per_pixel(), 4);
        assert_eq!(PixelLayout::RgbF32Linear.bytes_per_pixel(), 12);
        assert_eq!(PixelLayout::Gray8Srgb.bytes_per_pixel(), 1);
    }

    #[test]
    fn test_chroma_subsampling_factors() {
        assert_eq!(ChromaSubsampling::None.h_factor(), 1);
        assert_eq!(ChromaSubsampling::None.v_factor(), 1);
        assert_eq!(ChromaSubsampling::Quarter.h_factor(), 2);
        assert_eq!(ChromaSubsampling::Quarter.v_factor(), 2);
        assert_eq!(ChromaSubsampling::HalfHorizontal.h_factor(), 2);
        assert_eq!(ChromaSubsampling::HalfHorizontal.v_factor(), 1);
    }

    #[test]
    fn test_optimization_preset_all_variants() {
        let all: Vec<_> = OptimizationPreset::all().collect();
        #[cfg(feature = "trellis")]
        assert_eq!(all.len(), 8);
        #[cfg(not(feature = "trellis"))]
        assert_eq!(all.len(), 2);
        // No duplicates
        let mut set = std::collections::HashSet::new();
        for p in &all {
            assert!(set.insert(p), "duplicate preset: {:?}", p);
        }
    }

    #[test]
    fn test_optimization_preset_progressive_jpegli() {
        assert!(!OptimizationPreset::JpegliBaseline.is_progressive());
        assert!(OptimizationPreset::JpegliProgressive.is_progressive());
    }

    #[cfg(feature = "trellis")]
    #[test]
    fn test_optimization_preset_progressive_trellis() {
        assert!(!OptimizationPreset::MozjpegBaseline.is_progressive());
        assert!(OptimizationPreset::MozjpegProgressive.is_progressive());
        assert!(OptimizationPreset::MozjpegMaxCompression.is_progressive());
        assert!(!OptimizationPreset::HybridBaseline.is_progressive());
        assert!(OptimizationPreset::HybridProgressive.is_progressive());
        assert!(OptimizationPreset::HybridMaxCompression.is_progressive());
    }

    #[test]
    fn test_optimization_preset_trellis_jpegli() {
        assert!(!OptimizationPreset::JpegliBaseline.uses_trellis());
        assert!(!OptimizationPreset::JpegliProgressive.uses_trellis());
    }

    #[cfg(feature = "trellis")]
    #[test]
    fn test_optimization_preset_trellis() {
        assert!(OptimizationPreset::MozjpegBaseline.uses_trellis());
        assert!(OptimizationPreset::MozjpegProgressive.uses_trellis());
        assert!(OptimizationPreset::MozjpegMaxCompression.uses_trellis());
        assert!(OptimizationPreset::HybridBaseline.uses_trellis());
        assert!(OptimizationPreset::HybridProgressive.uses_trellis());
        assert!(OptimizationPreset::HybridMaxCompression.uses_trellis());
    }

    #[test]
    fn test_optimization_preset_aq_jpegli() {
        assert!(OptimizationPreset::JpegliBaseline.uses_aq());
        assert!(OptimizationPreset::JpegliProgressive.uses_aq());
    }

    #[cfg(feature = "trellis")]
    #[test]
    fn test_optimization_preset_aq_trellis() {
        assert!(!OptimizationPreset::MozjpegBaseline.uses_aq());
        assert!(!OptimizationPreset::MozjpegProgressive.uses_aq());
        assert!(!OptimizationPreset::MozjpegMaxCompression.uses_aq());
        assert!(OptimizationPreset::HybridBaseline.uses_aq());
        assert!(OptimizationPreset::HybridProgressive.uses_aq());
        assert!(OptimizationPreset::HybridMaxCompression.uses_aq());
    }

    #[test]
    fn test_optimization_preset_scan_strategy_jpegli() {
        assert_eq!(
            OptimizationPreset::JpegliBaseline.scan_strategy(),
            ScanStrategy::Default
        );
    }

    #[cfg(feature = "trellis")]
    #[test]
    fn test_optimization_preset_scan_strategy_trellis() {
        assert_eq!(
            OptimizationPreset::MozjpegProgressive.scan_strategy(),
            ScanStrategy::Mozjpeg
        );
        assert_eq!(
            OptimizationPreset::MozjpegMaxCompression.scan_strategy(),
            ScanStrategy::Search
        );
        assert_eq!(
            OptimizationPreset::HybridMaxCompression.scan_strategy(),
            ScanStrategy::Search
        );
    }

    #[test]
    fn test_optimization_preset_quant_table_source_jpegli() {
        assert_eq!(
            OptimizationPreset::JpegliBaseline.quant_table_source(),
            QuantTableSource::Jpegli
        );
        assert_eq!(
            OptimizationPreset::JpegliProgressive.quant_table_source(),
            QuantTableSource::Jpegli
        );
    }

    #[cfg(feature = "trellis")]
    #[test]
    fn test_optimization_preset_quant_table_source_trellis() {
        assert_eq!(
            OptimizationPreset::MozjpegBaseline.quant_table_source(),
            QuantTableSource::MozjpegDefault
        );
        assert_eq!(
            OptimizationPreset::MozjpegProgressive.quant_table_source(),
            QuantTableSource::MozjpegDefault
        );
        assert_eq!(
            OptimizationPreset::MozjpegMaxCompression.quant_table_source(),
            QuantTableSource::MozjpegDefault
        );
        assert_eq!(
            OptimizationPreset::HybridBaseline.quant_table_source(),
            QuantTableSource::Jpegli
        );
        assert_eq!(
            OptimizationPreset::HybridProgressive.quant_table_source(),
            QuantTableSource::Jpegli
        );
        assert_eq!(
            OptimizationPreset::HybridMaxCompression.quant_table_source(),
            QuantTableSource::Jpegli
        );
    }

    #[test]
    fn test_quant_table_source_default() {
        assert_eq!(QuantTableSource::default(), QuantTableSource::Jpegli);
    }

    #[test]
    fn test_optimization_preset_display() {
        assert_eq!(
            OptimizationPreset::JpegliBaseline.to_string(),
            "jpegli-baseline"
        );
    }

    #[cfg(feature = "trellis")]
    #[test]
    fn test_optimization_preset_display_trellis() {
        assert_eq!(
            OptimizationPreset::HybridMaxCompression.to_string(),
            "hybrid-max"
        );
    }

    #[test]
    fn test_pixel_format_to_layout_conversion() {
        // 8-bit → sRGB gamma
        assert_eq!(PixelLayout::from(PixelFormat::Rgb), PixelLayout::Rgb8Srgb);
        assert_eq!(PixelLayout::from(PixelFormat::Rgba), PixelLayout::Rgba8Srgb);
        assert_eq!(PixelLayout::from(PixelFormat::Bgr), PixelLayout::Bgr8Srgb);
        assert_eq!(PixelLayout::from(PixelFormat::Bgra), PixelLayout::Bgra8Srgb);
        assert_eq!(PixelLayout::from(PixelFormat::Bgrx), PixelLayout::Bgrx8Srgb);
        assert_eq!(PixelLayout::from(PixelFormat::Gray), PixelLayout::Gray8Srgb);

        // 16-bit → linear light
        assert_eq!(
            PixelLayout::from(PixelFormat::Rgb16),
            PixelLayout::Rgb16Linear
        );
        assert_eq!(
            PixelLayout::from(PixelFormat::Rgba16),
            PixelLayout::Rgba16Linear
        );
        assert_eq!(
            PixelLayout::from(PixelFormat::Gray16),
            PixelLayout::Gray16Linear
        );

        // f32 → linear light
        assert_eq!(
            PixelLayout::from(PixelFormat::RgbF32),
            PixelLayout::RgbF32Linear
        );
        assert_eq!(
            PixelLayout::from(PixelFormat::RgbaF32),
            PixelLayout::RgbaF32Linear
        );
        assert_eq!(
            PixelLayout::from(PixelFormat::GrayF32),
            PixelLayout::GrayF32Linear
        );
    }

    #[test]
    fn test_pixel_layout_to_format_conversion() {
        // sRGB 8-bit → 8-bit format
        assert_eq!(PixelFormat::from(PixelLayout::Rgb8Srgb), PixelFormat::Rgb);
        assert_eq!(PixelFormat::from(PixelLayout::Bgr8Srgb), PixelFormat::Bgr);
        assert_eq!(PixelFormat::from(PixelLayout::Rgba8Srgb), PixelFormat::Rgba);
        assert_eq!(PixelFormat::from(PixelLayout::Rgbx8Srgb), PixelFormat::Rgba); // RGBX maps to RGBA

        // Linear 16-bit → 16-bit format
        assert_eq!(
            PixelFormat::from(PixelLayout::Rgb16Linear),
            PixelFormat::Rgb16
        );
        assert_eq!(
            PixelFormat::from(PixelLayout::Rgba16Linear),
            PixelFormat::Rgba16
        );

        // Linear f32 → f32 format
        assert_eq!(
            PixelFormat::from(PixelLayout::RgbF32Linear),
            PixelFormat::RgbF32
        );
        assert_eq!(
            PixelFormat::from(PixelLayout::RgbaF32Linear),
            PixelFormat::RgbaF32
        );
    }

    #[test]
    fn test_roundtrip_conversions() {
        // Test that common formats roundtrip correctly
        let formats = vec![
            PixelFormat::Rgb,
            PixelFormat::Bgr,
            PixelFormat::Gray,
            PixelFormat::Rgb16,
            PixelFormat::RgbF32,
        ];

        for pf in formats {
            let pl: PixelLayout = pf.into();
            let pf2: PixelFormat = pl.into();
            assert_eq!(
                pf, pf2,
                "Roundtrip failed for {:?} -> {:?} -> {:?}",
                pf, pl, pf2
            );
        }
    }
}

// =============================================================================
// Optimization Presets
// =============================================================================

/// Preset optimization modes for the encoder.
///
/// Each preset configures [`ProgressiveScanMode`], [`QuantTableConfig`], trellis,
/// deringing, and Huffman strategy to match a specific encoder profile.
/// The three lineages are:
///
/// - **Jpegli** — matches C jpegli/cjpegli behavior (AQ-driven, no trellis)
/// - **Mozjpeg** — matches C mozjpeg behavior (trellis-driven, no AQ)
/// - **Hybrid** — combines jpegli AQ + mozjpeg trellis (zenjpeg-only)
///
/// # Preset Matrix
///
/// | Preset | AQ | Trellis | Deringing | Tables | Scan |
/// |--------|-----|---------|-----------|--------|------|
/// | JpegliBaseline | ✓ | — | ✓ | Jpegli 3T | Baseline |
/// | JpegliProgressive | ✓ | — | ✓ | Jpegli 3T | Progressive |
/// | MozjpegBaseline | — | Thorough | — | Robidoux 2T | Baseline |
/// | MozjpegProgressive | — | Thorough | — | Robidoux 2T | ProgMozjpeg |
/// | MozjpegMaxCompression | — | Thorough | ✓ | Robidoux 2T | ProgSearch |
/// | HybridBaseline | ✓ | Adaptive | ✓ | Jpegli 3T | Baseline |
/// | HybridProgressive | ✓ | Adaptive | ✓ | Jpegli 3T | Progressive |
/// | HybridMaxCompression | ✓ | Thorough | ✓ | Jpegli 3T | ProgSearch |
///
/// # Exploration Gaps
///
/// Interesting combinations NOT covered by the 8 presets (build manually):
///
/// - **Robidoux + AQ**: `MozjpegRobidoux` tables with jpegli AQ. Tests whether
///   AQ can compensate for Robidoux's less precise frequency-dependent scaling.
/// - **Jpegli + trellis without AQ**: Inverse hybrid — tests trellis value
///   with jpegli's perceptual tables when AQ is disabled.
/// - **Universal deringing**: All presets with deringing=true. C mozjpeg only
///   enables it for JCP_MAX_COMPRESSION, but it may help mozjpeg baseline/prog too.
/// - **JpegliMaxCompression**: AQ + scan search + no trellis. Tests whether
///   scan search alone (without trellis overhead) is worthwhile for jpegli.
///
/// Use [`OptimizationPreset::all()`] to iterate all variants for search.
///
/// Individual settings can still be overridden after applying a preset
/// via [`EncoderConfig::optimization()`](super::encoder_config::EncoderConfig::optimization).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum OptimizationPreset {
    // === Jpegli lineage (matches C cjpegli) ===
    /// Baseline JPEG, optimized Huffman, jpegli AQ. No trellis.
    /// Matches `cjpegli --jpeg_type baseline`.
    JpegliBaseline,

    /// Progressive JPEG with jpegli scan script (freq split at 2/3,
    /// SA for all). Optimized Huffman. Jpegli AQ. No trellis.
    /// Matches default `cjpegli` output. This is the encoder default.
    JpegliProgressive,

    // === Mozjpeg lineage (matches C cjpeg/mozjpeg) ===
    /// Baseline JPEG, optimized Huffman, trellis (full search). No jpegli AQ.
    /// 2 quant tables (shared chroma), Robidoux tables. No deringing.
    /// Matches C mozjpeg default profile with baseline output.
    ///
    /// Requires the `trellis` feature.
    #[cfg(feature = "trellis")]
    MozjpegBaseline,

    /// Progressive with mozjpeg scan script (freq split at 8/9, no chroma SA).
    /// Trellis (full search). No AQ. No deringing. 2 quant tables, Robidoux.
    /// Matches C mozjpeg default profile with progressive output.
    ///
    /// Requires the `trellis` feature.
    #[cfg(feature = "trellis")]
    MozjpegProgressive,

    /// Mozjpeg progressive + scan search (64 candidates). Trellis (full search).
    /// No AQ. Deringing enabled. 2 quant tables, Robidoux.
    /// Matches C mozjpeg `JCP_MAX_COMPRESSION` profile.
    ///
    /// Requires the `trellis` feature.
    #[cfg(feature = "trellis")]
    MozjpegMaxCompression,

    // === Hybrid lineage (zenjpeg-unique) ===
    /// Baseline JPEG with jpegli AQ + trellis (Adaptive speed) + deringing.
    /// 3 quant tables (separate chroma), jpegli perceptual tables.
    ///
    /// Requires the `trellis` feature.
    #[cfg(feature = "trellis")]
    HybridBaseline,

    /// Jpegli AQ + trellis (Adaptive) + deringing + jpegli scan script.
    /// 3 quant tables. Typically the best quality/size tradeoff.
    ///
    /// Requires the `trellis` feature.
    #[cfg(feature = "trellis")]
    HybridProgressive,

    /// AQ + trellis (full search) + deringing + scan search (64 candidates).
    /// 3 quant tables. Slowest, smallest files.
    ///
    /// Requires the `trellis` feature.
    #[cfg(feature = "trellis")]
    HybridMaxCompression,
}

/// Effort level for convenience constructors.
///
/// Maps to the corresponding [`OptimizationPreset`] for the chosen color mode.
/// Use this with [`EncoderConfig::ycbcr_effort()`], [`EncoderConfig::xyb_effort()`],
/// or [`EncoderConfig::grayscale_effort()`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Effort {
    /// Fast encoding: jpegli baseline, no trellis.
    /// Maps to [`OptimizationPreset::JpegliBaseline`].
    Fast,

    /// Balanced quality/speed: AQ + adaptive trellis + progressive.
    /// Maps to [`OptimizationPreset::HybridProgressive`].
    ///
    /// Requires the `trellis` feature.
    #[cfg(feature = "trellis")]
    Balanced,

    /// Maximum compression: thorough trellis + scan optimization.
    /// Maps to [`OptimizationPreset::HybridMaxCompression`].
    ///
    /// Requires the `trellis` feature.
    #[cfg(feature = "trellis")]
    Max,
}

impl Effort {
    /// Convert to the corresponding [`OptimizationPreset`].
    #[must_use]
    pub const fn to_preset(self) -> OptimizationPreset {
        match self {
            Self::Fast => OptimizationPreset::JpegliBaseline,
            #[cfg(feature = "trellis")]
            Self::Balanced => OptimizationPreset::HybridProgressive,
            #[cfg(feature = "trellis")]
            Self::Max => OptimizationPreset::HybridMaxCompression,
        }
    }
}

/// All preset variants in a fixed array for iteration.
#[cfg(feature = "trellis")]
const ALL_PRESETS: [OptimizationPreset; 8] = [
    OptimizationPreset::JpegliBaseline,
    OptimizationPreset::JpegliProgressive,
    OptimizationPreset::MozjpegBaseline,
    OptimizationPreset::MozjpegProgressive,
    OptimizationPreset::MozjpegMaxCompression,
    OptimizationPreset::HybridBaseline,
    OptimizationPreset::HybridProgressive,
    OptimizationPreset::HybridMaxCompression,
];

/// All preset variants in a fixed array for iteration (without trellis).
#[cfg(not(feature = "trellis"))]
const ALL_PRESETS: [OptimizationPreset; 2] = [
    OptimizationPreset::JpegliBaseline,
    OptimizationPreset::JpegliProgressive,
];

impl OptimizationPreset {
    /// Returns an iterator over all preset variants.
    /// Useful for searching the optimization space across lineages.
    pub fn all() -> impl Iterator<Item = Self> {
        ALL_PRESETS.iter().copied()
    }

    /// Returns true if this preset uses progressive encoding.
    #[must_use]
    pub const fn is_progressive(self) -> bool {
        match self {
            Self::JpegliBaseline => false,
            Self::JpegliProgressive => true,
            #[cfg(feature = "trellis")]
            Self::MozjpegBaseline => false,
            #[cfg(feature = "trellis")]
            Self::MozjpegProgressive => true,
            #[cfg(feature = "trellis")]
            Self::MozjpegMaxCompression => true,
            #[cfg(feature = "trellis")]
            Self::HybridBaseline => false,
            #[cfg(feature = "trellis")]
            Self::HybridProgressive => true,
            #[cfg(feature = "trellis")]
            Self::HybridMaxCompression => true,
        }
    }

    /// Returns true if this preset enables trellis quantization.
    #[must_use]
    pub const fn uses_trellis(self) -> bool {
        match self {
            Self::JpegliBaseline | Self::JpegliProgressive => false,
            #[cfg(feature = "trellis")]
            Self::MozjpegBaseline
            | Self::MozjpegProgressive
            | Self::MozjpegMaxCompression
            | Self::HybridBaseline
            | Self::HybridProgressive
            | Self::HybridMaxCompression => true,
        }
    }

    /// Returns true if this preset uses jpegli AQ (adaptive quantization).
    #[must_use]
    pub const fn uses_aq(self) -> bool {
        match self {
            Self::JpegliBaseline | Self::JpegliProgressive => true,
            #[cfg(feature = "trellis")]
            Self::MozjpegBaseline | Self::MozjpegProgressive | Self::MozjpegMaxCompression => false,
            #[cfg(feature = "trellis")]
            Self::HybridBaseline | Self::HybridProgressive | Self::HybridMaxCompression => true,
        }
    }

    /// Returns the quantization table source for this preset.
    #[must_use]
    pub const fn quant_table_source(self) -> QuantTableSource {
        match self {
            #[cfg(feature = "trellis")]
            Self::MozjpegBaseline | Self::MozjpegProgressive | Self::MozjpegMaxCompression => {
                QuantTableSource::MozjpegDefault
            }
            _ => QuantTableSource::Jpegli,
        }
    }

    /// Returns the scan strategy for this preset.
    #[must_use]
    pub const fn scan_strategy(self) -> ScanStrategy {
        match self {
            Self::JpegliBaseline => ScanStrategy::Default,
            Self::JpegliProgressive => ScanStrategy::Default,
            #[cfg(feature = "trellis")]
            Self::MozjpegBaseline | Self::HybridBaseline => ScanStrategy::Default,
            #[cfg(feature = "trellis")]
            Self::HybridProgressive => ScanStrategy::Default,
            #[cfg(feature = "trellis")]
            Self::MozjpegProgressive => ScanStrategy::Mozjpeg,
            #[cfg(feature = "trellis")]
            Self::MozjpegMaxCompression | Self::HybridMaxCompression => ScanStrategy::Search,
        }
    }

    /// Returns the short name for display/logging.
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::JpegliBaseline => "jpegli-baseline",
            Self::JpegliProgressive => "jpegli-progressive",
            #[cfg(feature = "trellis")]
            Self::MozjpegBaseline => "mozjpeg-baseline",
            #[cfg(feature = "trellis")]
            Self::MozjpegProgressive => "mozjpeg-progressive",
            #[cfg(feature = "trellis")]
            Self::MozjpegMaxCompression => "mozjpeg-max",
            #[cfg(feature = "trellis")]
            Self::HybridBaseline => "hybrid-baseline",
            #[cfg(feature = "trellis")]
            Self::HybridProgressive => "hybrid-progressive",
            #[cfg(feature = "trellis")]
            Self::HybridMaxCompression => "hybrid-max",
        }
    }
}

impl core::fmt::Display for OptimizationPreset {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str(self.name())
    }
}

// =============================================================================
// Parallel Encoding Configuration
// =============================================================================

/// Parallel encoding strategy.
///
/// Controls how the encoder uses multiple threads for improved throughput.
/// Parallel encoding uses JPEG restart markers to enable independent encoding
/// of image segments, which are then concatenated.
///
/// # Restart Marker Behavior
///
/// Parallel encoding requires restart markers between segments. When enabled:
/// - If `restart_interval` is 0 or too small, it will be **increased** to an
///   optimal value based on thread count and image size
/// - If `restart_interval` is already set to a reasonable value, it will be
///   preserved (parallel encoding respects user-specified intervals)
///
/// Restart markers add ~2 bytes per interval but enable:
/// - Parallel encoding/decoding
/// - Error recovery in corrupted streams
/// - Random access to image regions
///
/// # Performance
///
/// Parallel encoding is most beneficial for larger images (512x512+):
/// - 2 threads: ~1.2-1.6x speedup
/// - 4 threads: ~1.3-1.7x speedup
/// - Diminishing returns beyond 4 threads for typical images
///
/// Small images (<256x256) may see no benefit or slight overhead.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
#[non_exhaustive]
#[cfg(feature = "parallel")]
pub enum ParallelEncoding {
    /// Automatically configure parallel encoding.
    ///
    /// Uses available CPU cores and selects an optimal restart interval
    /// based on image dimensions. The restart interval will be increased
    /// if needed, but never decreased below the user-specified value.
    #[default]
    Auto,
}

/// Huffman table strategy for entropy encoding.
///
/// Controls how Huffman tables are selected for the JPEG output:
/// Huffman table selection strategy.
///
/// - `Optimize`: Two-pass encoding collects symbol frequencies, then builds optimal tables.
///   Produces the smallest files. Required for progressive mode.
/// - `Fixed`: Single-pass encoding with general-purpose trained Huffman tables.
///   Fastest, with ~2.5% overhead vs per-image optimal.
/// - `FixedAnnexK`: Single-pass with JPEG Annex K standard tables.
///   Maximum compatibility, ~5-12% larger than optimal.
/// - `Custom`: Single-pass encoding with caller-provided tables.
///   Use `HuffmanTableSet::annex_k()` for the original JPEG standard tables,
///   or provide your own pre-computed tables.
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub enum HuffmanStrategy {
    /// Two-pass: collect frequencies, build optimal tables.
    /// Smallest files, but cannot stream output (must buffer all blocks).
    #[default]
    Optimize,
    /// Single-pass: use general-purpose trained tables (~2.5% overhead).
    /// Enables streaming output for sequential mode.
    Fixed,
    /// Single-pass: use JPEG Annex K standard tables (~5-12% overhead).
    /// Maximum decoder compatibility. Enables streaming output.
    FixedAnnexK,
    /// Single-pass: use caller-provided tables.
    /// Use `HuffmanTableSet::annex_k()` for Annex K, or provide custom tables.
    Custom(Box<crate::huffman::optimize::HuffmanTableSet>),
}

impl From<bool> for HuffmanStrategy {
    /// `true` → `Optimize`, `false` → `Fixed`
    fn from(optimize: bool) -> Self {
        if optimize {
            HuffmanStrategy::Optimize
        } else {
            HuffmanStrategy::Fixed
        }
    }
}

impl From<crate::huffman::optimize::HuffmanTableSet> for HuffmanStrategy {
    fn from(tables: crate::huffman::optimize::HuffmanTableSet) -> Self {
        HuffmanStrategy::Custom(Box::new(tables))
    }
}

/// Progressive scan script strategy.
///
/// Controls how progressive JPEG scan scripts are generated. All progressive
/// modes use optimized Huffman tables (two-pass encoding).
///
/// # Baseline JPEG
///
/// For baseline (non-progressive) encoding, this setting has no effect.
/// Use `.progressive(false)` to disable progressive mode.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ScanStrategy {
    /// Use encoder's default progressive script (jpegli-style).
    ///
    /// - Frequency split at AC coefficients 2/3
    /// - Successive approximation for all components (Al=2 → 1 → 0)
    /// - Separate DC scans per component
    ///
    /// This is the default when `.progressive(true)` is used.
    #[default]
    Default,

    /// Search 64 candidate scripts and pick the smallest (mozjpeg-style).
    ///
    /// Tests combinations of:
    /// - Frequency splits at [2, 5, 8, 12, 18]
    /// - Successive approximation levels 0-3 for luma, 0-2 for chroma
    /// - DC scan interleaving options
    ///
    /// Typically saves 1-3% vs fixed scripts, at the cost of ~2x encode time.
    Search,

    /// mozjpeg's default progressive script.
    ///
    /// - Frequency split at AC coefficients 8/9
    /// - No successive approximation for chroma (full precision in one pass)
    /// - Separate DC scans per component
    ///
    /// Use this for closest parity with C mozjpeg's default progressive output.
    Mozjpeg,
}

/// Source of quantization tables for encoding.
///
/// Controls which base quantization tables and scaling formula are used:
///
/// - **Jpegli** (default) — jpegli perceptual defaults with distance-based,
///   frequency-dependent scaling and adaptive zero-bias.
/// - **MozjpegDefault** — Nicolas Robidoux's psychovisual tables with
///   libjpeg's quality-scaling formula and neutral zero-bias (0.5 AC offset).
///
/// For other mozjpeg presets (MSSIM, Klein, etc.), use the `mozjpeg-tables`
/// feature and supply custom tables via
/// [`EncoderConfig::quant_tables()`](super::encoder_config::EncoderConfig::quant_tables).
///
/// **Prefer [`QuantTableConfig`]** for new code — it bundles table source,
/// chroma table count, and custom tables into one type-safe enum that
/// prevents invalid combinations.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum QuantTableSource {
    /// Jpegli perceptual defaults (distance-based, frequency-dependent scaling).
    #[default]
    Jpegli,
    /// Mozjpeg Robidoux tables (psychovisual, quality-scaled).
    MozjpegDefault,
}

/// Unified quantization table configuration.
///
/// Bundles the table source, chroma table layout, and custom table overrides
/// into a single type, preventing invalid combinations such as:
///
/// - `MozjpegDefault` + separate chroma tables (mozjpeg always uses 2 tables)
/// - Custom tables + a quant source (custom overrides everything)
///
/// # Variants
///
/// | Variant | Tables | Source | Notes |
/// |---------|--------|--------|-------|
/// | `Jpegli` | 3 (Y, Cb, Cr) | jpegli perceptual | Default, matches `jpegli_set_distance()` |
/// | `JpegliSharedChroma` | 2 (Y, shared) | jpegli perceptual | Matches `jpeg_set_quality()` |
/// | `MozjpegRobidoux` | 2 (Y, shared) | Robidoux psychovisual | Matches C mozjpeg default |
/// | `Custom` | (user-defined) | user-defined | Full control via `EncodingTables` |
#[derive(Clone, Debug, Default, PartialEq)]
#[non_exhaustive]
pub enum QuantTableConfig {
    /// Jpegli perceptual defaults with 3 separate quantization tables (Y, Cb, Cr).
    ///
    /// Matches C++ jpegli's `jpegli_set_distance()` behavior. Each chroma
    /// component gets its own optimized table for best quality.
    #[default]
    Jpegli,

    /// Jpegli perceptual defaults with 2 quantization tables (Y, shared chroma).
    ///
    /// Matches C++ jpegli's `jpeg_set_quality()` behavior. Cb and Cr share
    /// the same table — produces slightly larger files than 3-table mode
    /// but is compatible with encoders that use `jpeg_set_quality()`.
    JpegliSharedChroma,

    /// Mozjpeg Robidoux psychovisual tables with 2 quantization tables.
    ///
    /// Uses Nicolas Robidoux's psychovisual tables scaled by libjpeg's
    /// quality formula. Always 2 tables (shared chroma), matching C mozjpeg.
    MozjpegRobidoux,

    /// User-provided custom encoding tables.
    ///
    /// Overrides both quantization tables and zero-bias configuration.
    /// Use `EncodingTables::default_ycbcr()` as a starting point for modifications.
    Custom(Box<super::tuning::EncodingTables>),

    /// Glassa low-BPP optimized tables for extreme compression (Q3-Q25).
    ///
    /// SA-optimized tables that achieve +20 to +33 pareto gains at ultra-low
    /// bitrates (0.15-0.50 BPP). Aggressively zeros high-frequency coefficients.
    ///
    /// # When to Use
    ///
    /// - Thumbnails (<100px)
    /// - LQIP (low-quality image placeholders)
    /// - Progressive loading placeholders
    /// - Any use case where quality < Q30 is acceptable
    ///
    /// # When NOT to Use
    ///
    /// - Q30+: No benefit over mozjpeg defaults
    /// - High quality: Use [`Jpegli`](Self::Jpegli) instead
    ///
    /// The inner `u8` is the quality level (3-25 recommended).
    GlassaLowBpp(u8),
}

impl QuantTableConfig {
    /// Returns the internal `QuantTableSource` for this configuration.
    ///
    /// Custom and GlassaLowBpp tables return `Jpegli` (ignored when custom tables are present).
    #[must_use]
    pub const fn quant_source(&self) -> QuantTableSource {
        match self {
            Self::Jpegli | Self::JpegliSharedChroma | Self::Custom(_) | Self::GlassaLowBpp(_) => {
                QuantTableSource::Jpegli
            }
            Self::MozjpegRobidoux => QuantTableSource::MozjpegDefault,
        }
    }

    /// Returns whether Cb and Cr use separate quantization tables.
    #[must_use]
    pub const fn separate_chroma_tables(&self) -> bool {
        match self {
            Self::Jpegli => true,
            // Glassa uses shared chroma (mozjpeg-style 2 tables)
            Self::JpegliSharedChroma | Self::MozjpegRobidoux | Self::GlassaLowBpp(_) => false,
            // Custom tables define their own layout; default to separate.
            Self::Custom(_) => true,
        }
    }

    /// Returns the custom encoding tables, if any.
    ///
    /// For `GlassaLowBpp`, this generates tables on-demand via interpolation.
    #[must_use]
    pub fn custom_tables(&self) -> Option<super::tuning::EncodingTables> {
        match self {
            Self::Custom(t) => Some((**t).clone()),
            Self::GlassaLowBpp(q) => Some(super::tables::glassa::tables_for_quality(*q)),
            _ => None,
        }
    }
}

/// JPEG scan mode — baseline vs progressive, with script strategy.
///
/// Bundles the progressive flag and scan script strategy into a single type,
/// preventing invalid combinations such as:
///
/// - Baseline + Search scan strategy (search only applies to progressive)
/// - Baseline + Mozjpeg scan script (mozjpeg script is progressive-only)
///
/// All progressive modes automatically enable optimized Huffman tables.
///
/// # Variants
///
/// | Variant | Progressive | Script | Notes |
/// |---------|-------------|--------|-------|
/// | `Baseline` | No | N/A | Sequential JPEG (SOF0/SOF1) |
/// | `Progressive` | Yes | jpegli default | Freq split at 2/3, SA for all |
/// | `ProgressiveMozjpeg` | Yes | mozjpeg default | Freq split at 8/9, no chroma SA |
/// | `ProgressiveSearch` | Yes | search (64 candidates) | Best compression, ~2x slower |
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ProgressiveScanMode {
    /// Baseline (sequential) JPEG.
    ///
    /// Single scan containing all components. No progressive refinement.
    /// Compatible with all JPEG decoders.
    #[default]
    Baseline,

    /// Progressive JPEG with jpegli's default scan script.
    ///
    /// - Frequency split at AC coefficients 2/3
    /// - Successive approximation for all components (Al=2 → 1 → 0)
    /// - Separate DC scans per component
    Progressive,

    /// Progressive JPEG with mozjpeg's default scan script.
    ///
    /// - Frequency split at AC coefficients 8/9
    /// - No successive approximation for chroma
    /// - Separate DC scans per component
    ///
    /// Closest parity with C mozjpeg's default progressive output.
    ProgressiveMozjpeg,

    /// Progressive JPEG with scan search optimization.
    ///
    /// Tests 64 candidate scan configurations (frequency splits, SA levels,
    /// DC interleaving) and picks the smallest. Typically saves 1-3% vs
    /// fixed scripts, at the cost of ~2x encode time.
    ProgressiveSearch,
}

impl ProgressiveScanMode {
    /// Returns true if this mode uses progressive encoding.
    #[must_use]
    pub const fn is_progressive(self) -> bool {
        !matches!(self, Self::Baseline)
    }

    /// Returns the internal `ScanStrategy` for this mode.
    ///
    /// Baseline returns `Default` (ignored for non-progressive encoding).
    #[must_use]
    pub const fn scan_strategy(self) -> ScanStrategy {
        match self {
            Self::Baseline | Self::Progressive => ScanStrategy::Default,
            Self::ProgressiveMozjpeg => ScanStrategy::Mozjpeg,
            Self::ProgressiveSearch => ScanStrategy::Search,
        }
    }
}

impl From<bool> for ProgressiveScanMode {
    /// `true` → `Progressive`, `false` → `Baseline`
    fn from(progressive: bool) -> Self {
        if progressive {
            ProgressiveScanMode::Progressive
        } else {
            ProgressiveScanMode::Baseline
        }
    }
}

/// Expert configuration overlay for advanced encoding options.
///
/// Use with [`EncoderConfig::expert()`] to customize quantization tables
/// and trellis/hybrid rate-distortion optimization.
///
/// # Example
///
/// ```rust,ignore
/// use zenjpeg::encode::{EncoderConfig, ExpertConfig, QuantTableConfig, ChromaSubsampling};
///
/// let config = EncoderConfig::ycbcr(85, ChromaSubsampling::Quarter)
///     .expert(ExpertConfig::default()
///         .tables(QuantTableConfig::MozjpegRobidoux));
/// ```
#[cfg(feature = "trellis")]
#[derive(Clone, Debug, Default)]
pub struct ExpertConfig {
    /// Quantization table selection.
    ///
    /// Defaults to `QuantTableConfig::Jpegli`.
    pub tables: Option<QuantTableConfig>,

    /// Trellis quantization (standalone rate-distortion optimization).
    ///
    /// When set, enables mozjpeg-compatible trellis quantization.
    /// Mutually exclusive with `hybrid` - if both set, `hybrid` takes priority.
    pub trellis: Option<super::trellis::TrellisConfig>,

    /// Hybrid AQ+trellis (adaptive quantization coupled with trellis).
    ///
    /// When set with `enabled: true`, combines jpegli's adaptive quantization
    /// with trellis optimization. Takes priority over standalone `trellis`.
    pub hybrid: Option<super::trellis::HybridConfig>,
}

#[cfg(feature = "trellis")]
impl ExpertConfig {
    /// Set quantization tables.
    #[must_use]
    pub fn tables(mut self, tables: QuantTableConfig) -> Self {
        self.tables = Some(tables);
        self
    }

    /// Set standalone trellis configuration.
    #[must_use]
    pub fn trellis(mut self, config: super::trellis::TrellisConfig) -> Self {
        self.trellis = Some(config);
        self
    }

    /// Set hybrid AQ+trellis configuration.
    #[must_use]
    pub fn hybrid(mut self, config: super::trellis::HybridConfig) -> Self {
        self.hybrid = Some(config);
        self
    }
}

/// Convert from legacy PixelFormat to explicit PixelLayout.
///
/// Assumes standard color space conventions:
/// - 8-bit formats → sRGB gamma encoding
/// - 16-bit formats → linear light
/// - f32 formats → linear light
impl From<crate::types::PixelFormat> for PixelLayout {
    fn from(format: crate::types::PixelFormat) -> Self {
        use crate::types::PixelFormat;
        match format {
            // 8-bit → sRGB gamma (standard assumption)
            PixelFormat::Gray => Self::Gray8Srgb,
            PixelFormat::Rgb => Self::Rgb8Srgb,
            PixelFormat::Rgba => Self::Rgba8Srgb,
            PixelFormat::Bgr => Self::Bgr8Srgb,
            PixelFormat::Bgra => Self::Bgra8Srgb,
            PixelFormat::Bgrx => Self::Bgrx8Srgb,

            // 16-bit → linear light
            PixelFormat::Gray16 => Self::Gray16Linear,
            PixelFormat::Rgb16 => Self::Rgb16Linear,
            PixelFormat::Rgba16 => Self::Rgba16Linear,

            // f32 → linear light
            PixelFormat::GrayF32 => Self::GrayF32Linear,
            PixelFormat::RgbF32 => Self::RgbF32Linear,
            PixelFormat::RgbaF32 => Self::RgbaF32Linear,

            // CMYK → treat as RGB (best effort, will be converted)
            PixelFormat::Cmyk => Self::Rgba8Srgb,
        }
    }
}