zencodec 0.1.14

Shared traits and types for zen* image codecs
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
//! Codec capability descriptors.
//!
//! Encoders return [`EncodeCapabilities`] and decoders return
//! [`DecodeCapabilities`] describing what they support. This lets callers
//! discover behavior before calling methods that might be no-ops or expensive.
//!
//! [`UnsupportedOperation`] provides a standard enum for codecs to report
//! which operations they don't support. Use [`CodecErrorExt`](crate::CodecErrorExt)
//! to find these in any error's source chain.

use core::fmt;

/// Identifies an operation that a codec does not support.
///
/// Codecs include this in their error types (e.g. as a variant payload)
/// so callers can generically detect "this codec doesn't support this
/// operation" without downcasting. See [`CodecErrorExt`](crate::CodecErrorExt).
///
/// # Example
///
/// ```
/// use zencodec::UnsupportedOperation;
///
/// let op = UnsupportedOperation::DecodeInto;
/// assert_eq!(format!("{op}"), "unsupported operation: decode_into");
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum UnsupportedOperation {
    /// `Encoder::push_rows()` + `finish()` (row-level encode).
    RowLevelEncode,
    /// `Encoder::encode_from()` (pull-from-source encode).
    PullEncode,
    /// All `AnimationFrameEncoder` methods (animation encoding).
    AnimationEncode,
    /// `Decoder::decode_into()` (decode into caller buffer).
    DecodeInto,
    /// `Decoder::decode_rows()` (row-level decode).
    RowLevelDecode,
    /// All `AnimationFrameDecoder` methods (animation decoding).
    AnimationDecode,
    /// A specific pixel format is not supported.
    PixelFormat,
    /// All `MultiPageDecoder` methods (multi-image decode).
    MultiImageDecode,
}

impl UnsupportedOperation {
    /// Short name for the operation (suitable for error messages).
    pub const fn name(self) -> &'static str {
        match self {
            Self::RowLevelEncode => "row_level_encode",
            Self::PullEncode => "pull_encode",
            Self::AnimationEncode => "animation_encode",
            Self::DecodeInto => "decode_into",
            Self::RowLevelDecode => "row_level_decode",
            Self::AnimationDecode => "animation_decode",
            Self::PixelFormat => "pixel_format",
            Self::MultiImageDecode => "multi_image_decode",
        }
    }
}

impl fmt::Display for UnsupportedOperation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "unsupported operation: {}", self.name())
    }
}

impl core::error::Error for UnsupportedOperation {}

// ===========================================================================
// EncodeCapabilities
// ===========================================================================

/// Describes what an encoder supports.
///
/// Returned by [`EncoderConfig::capabilities()`](crate::encode::EncoderConfig::capabilities)
/// as a `&'static` reference. Uses getter methods so fields can be added
/// without breaking changes.
///
/// # Example
///
/// ```
/// use zencodec::encode::EncodeCapabilities;
///
/// static CAPS: EncodeCapabilities = EncodeCapabilities::new()
///     .with_icc(true)
///     .with_exif(true)
///     .with_xmp(true)
///     .with_stop(true)
///     .with_native_gray(true);
///
/// assert!(CAPS.icc());
/// assert!(CAPS.native_gray());
/// ```
#[derive(Clone, PartialEq)]
#[non_exhaustive]
pub struct EncodeCapabilities {
    // Metadata embedding
    icc: bool,
    exif: bool,
    xmp: bool,
    cicp: bool,
    // Operation support
    stop: bool,
    animation: bool,
    push_rows: bool,
    encode_from: bool,
    // Format capabilities
    lossy: bool,
    lossless: bool,
    hdr: bool,
    gain_map: bool,
    native_gray: bool,
    native_16bit: bool,
    native_f32: bool,
    native_alpha: bool,
    // Limit enforcement
    enforces_max_pixels: bool,
    enforces_max_memory: bool,
    // Tuning ranges
    effort_range: Option<[i32; 2]>,
    quality_range: Option<[f32; 2]>,
    // Threading
    threads_supported_range: (u16, u16),
}

impl Default for EncodeCapabilities {
    fn default() -> Self {
        Self::new()
    }
}

impl EncodeCapabilities {
    /// Empty capabilities (everything disabled, single-threaded).
    pub const EMPTY: Self = Self::new();

    /// Create capabilities with everything disabled.
    pub const fn new() -> Self {
        Self {
            icc: false,
            exif: false,
            xmp: false,
            cicp: false,
            stop: false,
            animation: false,
            push_rows: false,
            encode_from: false,
            lossy: false,
            lossless: false,
            hdr: false,
            gain_map: false,
            native_gray: false,
            native_16bit: false,
            native_f32: false,
            native_alpha: false,
            enforces_max_pixels: false,
            enforces_max_memory: false,
            effort_range: None,
            quality_range: None,
            threads_supported_range: (1, 1),
        }
    }

    // --- Getters ---

    /// Whether the encoder embeds ICC color profiles from `with_metadata`.
    pub const fn icc(&self) -> bool {
        self.icc
    }
    /// Whether the encoder embeds EXIF data from `with_metadata`.
    pub const fn exif(&self) -> bool {
        self.exif
    }
    /// Whether the encoder embeds XMP data from `with_metadata`.
    pub const fn xmp(&self) -> bool {
        self.xmp
    }
    /// Whether the encoder embeds CICP color description from `with_metadata`.
    pub const fn cicp(&self) -> bool {
        self.cicp
    }
    /// Whether `with_stop` on encode jobs is respected (not a no-op).
    pub const fn stop(&self) -> bool {
        self.stop
    }
    /// Whether the codec supports encoding animation (multiple frames).
    pub const fn animation(&self) -> bool {
        self.animation
    }
    /// Whether `push_rows()` / `finish()` work (row-level encode).
    pub const fn push_rows(&self) -> bool {
        self.push_rows
    }
    /// Whether `encode_from()` works (pull-from-source encode).
    pub const fn encode_from(&self) -> bool {
        self.encode_from
    }
    /// Whether the codec supports lossy encoding.
    pub const fn lossy(&self) -> bool {
        self.lossy
    }
    /// Whether the codec supports mathematically lossless encoding.
    pub const fn lossless(&self) -> bool {
        self.lossless
    }
    /// Whether the codec supports HDR content.
    pub const fn hdr(&self) -> bool {
        self.hdr
    }
    /// Whether the codec supports gain map (HDR/SDR) embedding.
    pub const fn gain_map(&self) -> bool {
        self.gain_map
    }
    /// Whether the codec supports grayscale natively.
    pub const fn native_gray(&self) -> bool {
        self.native_gray
    }
    /// Whether the codec supports 16-bit per channel natively.
    pub const fn native_16bit(&self) -> bool {
        self.native_16bit
    }
    /// Whether the codec handles f32 pixel data natively.
    pub const fn native_f32(&self) -> bool {
        self.native_f32
    }
    /// Whether the codec handles alpha channel natively.
    pub const fn native_alpha(&self) -> bool {
        self.native_alpha
    }
    /// Whether the codec enforces `max_pixels` limits.
    pub const fn enforces_max_pixels(&self) -> bool {
        self.enforces_max_pixels
    }
    /// Whether the codec enforces `max_memory_bytes` limits.
    pub const fn enforces_max_memory(&self) -> bool {
        self.enforces_max_memory
    }

    /// Meaningful effort range `[min, max]`.
    ///
    /// `None` means the codec has no effort tuning.
    pub const fn effort_range(&self) -> Option<[i32; 2]> {
        self.effort_range
    }

    /// Meaningful quality range `[min, max]` on the calibrated 0.0–100.0 scale.
    ///
    /// `None` means the codec is lossless-only.
    pub const fn quality_range(&self) -> Option<[f32; 2]> {
        self.quality_range
    }

    /// Supported thread count range `(min, max)`.
    ///
    /// `(1, 1)` means single-threaded only.
    /// `(1, 16)` means the encoder can use 1 to 16 threads.
    pub const fn threads_supported_range(&self) -> (u16, u16) {
        self.threads_supported_range
    }

    /// Check whether this encoder supports a given operation.
    ///
    /// Returns `true` if the capability flag corresponding to `op` is set.
    /// Returns `false` for decode-only operations or [`PixelFormat`](UnsupportedOperation::PixelFormat)
    /// (which depends on the specific format, not a static flag).
    ///
    /// # Example
    ///
    /// ```
    /// use zencodec::UnsupportedOperation;
    /// use zencodec::encode::EncodeCapabilities;
    ///
    /// static CAPS: EncodeCapabilities = EncodeCapabilities::new()
    ///     .with_animation(true)
    ///     .with_push_rows(true);
    ///
    /// assert!(CAPS.supports(UnsupportedOperation::AnimationEncode));
    /// assert!(CAPS.supports(UnsupportedOperation::RowLevelEncode));
    /// assert!(!CAPS.supports(UnsupportedOperation::PullEncode));
    /// assert!(!CAPS.supports(UnsupportedOperation::DecodeInto));
    /// ```
    pub const fn supports(&self, op: UnsupportedOperation) -> bool {
        match op {
            UnsupportedOperation::RowLevelEncode => self.push_rows,
            UnsupportedOperation::PullEncode => self.encode_from,
            UnsupportedOperation::AnimationEncode => self.animation,
            UnsupportedOperation::DecodeInto
            | UnsupportedOperation::RowLevelDecode
            | UnsupportedOperation::AnimationDecode
            | UnsupportedOperation::MultiImageDecode
            | UnsupportedOperation::PixelFormat => false,
        }
    }

    // --- Const builder methods ---

    /// Set whether the encoder embeds ICC color profiles.
    pub const fn with_icc(mut self, v: bool) -> Self {
        self.icc = v;
        self
    }
    /// Set whether the encoder embeds EXIF data.
    pub const fn with_exif(mut self, v: bool) -> Self {
        self.exif = v;
        self
    }
    /// Set whether the encoder embeds XMP data.
    pub const fn with_xmp(mut self, v: bool) -> Self {
        self.xmp = v;
        self
    }
    /// Set whether the encoder embeds CICP color description.
    pub const fn with_cicp(mut self, v: bool) -> Self {
        self.cicp = v;
        self
    }
    /// Set whether cooperative cancellation via [`Stop`](enough::Stop) is supported.
    pub const fn with_stop(mut self, v: bool) -> Self {
        self.stop = v;
        self
    }
    /// Set whether animation encoding is supported.
    pub const fn with_animation(mut self, v: bool) -> Self {
        self.animation = v;
        self
    }
    /// Set whether row-level (`push_rows`/`finish`) encoding is supported.
    pub const fn with_push_rows(mut self, v: bool) -> Self {
        self.push_rows = v;
        self
    }
    /// Set whether pull-from-source (`encode_from`) encoding is supported.
    pub const fn with_encode_from(mut self, v: bool) -> Self {
        self.encode_from = v;
        self
    }
    /// Set whether lossy encoding is supported.
    pub const fn with_lossy(mut self, v: bool) -> Self {
        self.lossy = v;
        self
    }
    /// Set whether lossless encoding is supported.
    pub const fn with_lossless(mut self, v: bool) -> Self {
        self.lossless = v;
        self
    }
    /// Set whether HDR content is supported.
    pub const fn with_hdr(mut self, v: bool) -> Self {
        self.hdr = v;
        self
    }
    /// Set whether gain map (HDR/SDR) embedding is supported.
    pub const fn with_gain_map(mut self, v: bool) -> Self {
        self.gain_map = v;
        self
    }
    /// Set whether native grayscale encoding is supported.
    pub const fn with_native_gray(mut self, v: bool) -> Self {
        self.native_gray = v;
        self
    }
    /// Set whether native 16-bit per channel encoding is supported.
    pub const fn with_native_16bit(mut self, v: bool) -> Self {
        self.native_16bit = v;
        self
    }
    /// Set whether native f32 pixel data is supported.
    pub const fn with_native_f32(mut self, v: bool) -> Self {
        self.native_f32 = v;
        self
    }
    /// Set whether native alpha channel is supported.
    pub const fn with_native_alpha(mut self, v: bool) -> Self {
        self.native_alpha = v;
        self
    }
    /// Set whether the encoder enforces `max_pixels` limits.
    pub const fn with_enforces_max_pixels(mut self, v: bool) -> Self {
        self.enforces_max_pixels = v;
        self
    }
    /// Set whether the encoder enforces `max_memory_bytes` limits.
    pub const fn with_enforces_max_memory(mut self, v: bool) -> Self {
        self.enforces_max_memory = v;
        self
    }

    /// Set the meaningful effort range `[min, max]`.
    pub const fn with_effort_range(mut self, min: i32, max: i32) -> Self {
        assert!(min <= max, "effort range: min must be <= max");
        self.effort_range = Some([min, max]);
        self
    }

    /// Set the meaningful quality range `[min, max]`.
    pub const fn with_quality_range(mut self, min: f32, max: f32) -> Self {
        assert!(min <= max, "quality range: min must be <= max");
        self.quality_range = Some([min, max]);
        self
    }

    /// Set supported thread count range.
    pub const fn with_threads_supported_range(mut self, min: u16, max: u16) -> Self {
        assert!(min >= 1, "threads range: min must be >= 1");
        assert!(min <= max, "threads range: min must be <= max");
        self.threads_supported_range = (min, max);
        self
    }
}

impl fmt::Debug for EncodeCapabilities {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut s = f.debug_struct("EncodeCapabilities");
        s.field("icc", &self.icc)
            .field("exif", &self.exif)
            .field("xmp", &self.xmp)
            .field("cicp", &self.cicp)
            .field("stop", &self.stop)
            .field("animation", &self.animation)
            .field("lossy", &self.lossy)
            .field("lossless", &self.lossless)
            .field("hdr", &self.hdr)
            .field("gain_map", &self.gain_map)
            .field("native_gray", &self.native_gray)
            .field("native_16bit", &self.native_16bit)
            .field("native_f32", &self.native_f32)
            .field("native_alpha", &self.native_alpha)
            .field("push_rows", &self.push_rows)
            .field("encode_from", &self.encode_from)
            .field("enforces_max_pixels", &self.enforces_max_pixels)
            .field("enforces_max_memory", &self.enforces_max_memory)
            .field("threads_supported_range", &self.threads_supported_range);
        if let Some(range) = &self.effort_range {
            s.field("effort_range", range);
        }
        if let Some(range) = &self.quality_range {
            s.field("quality_range", range);
        }
        s.finish()
    }
}

// ===========================================================================
// DecodeCapabilities
// ===========================================================================

/// Describes what a decoder supports.
///
/// Returned by [`DecoderConfig::capabilities()`](crate::decode::DecoderConfig::capabilities)
/// as a `&'static` reference. Uses getter methods so fields can be added
/// without breaking changes.
///
/// # Example
///
/// ```
/// use zencodec::decode::DecodeCapabilities;
///
/// static CAPS: DecodeCapabilities = DecodeCapabilities::new()
///     .with_icc(true)
///     .with_exif(true)
///     .with_stop(true)
///     .with_cheap_probe(true);
///
/// assert!(CAPS.icc());
/// assert!(CAPS.cheap_probe());
/// ```
#[derive(Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct DecodeCapabilities {
    // Metadata extraction
    icc: bool,
    exif: bool,
    xmp: bool,
    cicp: bool,
    // Operation support
    stop: bool,
    animation: bool,
    multi_image: bool,
    cheap_probe: bool,
    decode_into: bool,
    streaming: bool,
    // Format capabilities
    hdr: bool,
    gain_map: bool,
    native_gray: bool,
    native_16bit: bool,
    native_f32: bool,
    native_alpha: bool,
    // Limit enforcement
    enforces_max_pixels: bool,
    enforces_max_memory: bool,
    enforces_max_input_bytes: bool,
    // Threading
    threads_supported_range: (u16, u16),
}

impl Default for DecodeCapabilities {
    fn default() -> Self {
        Self::new()
    }
}

impl DecodeCapabilities {
    /// Empty capabilities (everything disabled, single-threaded).
    pub const EMPTY: Self = Self::new();

    /// Create capabilities with everything disabled.
    pub const fn new() -> Self {
        Self {
            icc: false,
            exif: false,
            xmp: false,
            cicp: false,
            stop: false,
            animation: false,
            multi_image: false,
            cheap_probe: false,
            decode_into: false,
            streaming: false,
            hdr: false,
            gain_map: false,
            native_gray: false,
            native_16bit: false,
            native_f32: false,
            native_alpha: false,
            enforces_max_pixels: false,
            enforces_max_memory: false,
            enforces_max_input_bytes: false,
            threads_supported_range: (1, 1),
        }
    }

    // --- Getters ---

    /// Whether the decoder extracts ICC color profiles into `ImageInfo`.
    pub const fn icc(&self) -> bool {
        self.icc
    }
    /// Whether the decoder extracts EXIF data into `ImageInfo`.
    pub const fn exif(&self) -> bool {
        self.exif
    }
    /// Whether the decoder extracts XMP data into `ImageInfo`.
    pub const fn xmp(&self) -> bool {
        self.xmp
    }
    /// Whether the decoder extracts CICP color description into `ImageInfo`.
    pub const fn cicp(&self) -> bool {
        self.cicp
    }
    /// Whether `with_stop` on decode jobs is respected (not a no-op).
    pub const fn stop(&self) -> bool {
        self.stop
    }
    /// Whether the codec supports decoding animation (multiple frames).
    pub const fn animation(&self) -> bool {
        self.animation
    }
    /// Whether this decoder supports multi-image containers (TIFF, HEIF, ICO).
    ///
    /// True for codecs with independently-addressable images.
    /// False for single-image and animation-only codecs.
    pub const fn multi_image(&self) -> bool {
        self.multi_image
    }
    /// Whether `probe()` is cheap (header parse only, not a full decode).
    pub const fn cheap_probe(&self) -> bool {
        self.cheap_probe
    }
    /// Whether `decode_into()` is implemented.
    pub const fn decode_into(&self) -> bool {
        self.decode_into
    }
    /// Whether `StreamingDecode` / `streaming_decoder()` is implemented.
    pub const fn streaming(&self) -> bool {
        self.streaming
    }
    /// Whether the codec supports HDR content.
    pub const fn hdr(&self) -> bool {
        self.hdr
    }
    /// Whether the codec supports gain map (HDR/SDR) extraction.
    pub const fn gain_map(&self) -> bool {
        self.gain_map
    }
    /// Whether the codec supports grayscale natively.
    pub const fn native_gray(&self) -> bool {
        self.native_gray
    }
    /// Whether the codec supports 16-bit per channel natively.
    pub const fn native_16bit(&self) -> bool {
        self.native_16bit
    }
    /// Whether the codec handles f32 pixel data natively.
    pub const fn native_f32(&self) -> bool {
        self.native_f32
    }
    /// Whether the codec handles alpha channel natively.
    pub const fn native_alpha(&self) -> bool {
        self.native_alpha
    }
    /// Whether the codec enforces `max_pixels` limits.
    pub const fn enforces_max_pixels(&self) -> bool {
        self.enforces_max_pixels
    }
    /// Whether the codec enforces `max_memory_bytes` limits.
    pub const fn enforces_max_memory(&self) -> bool {
        self.enforces_max_memory
    }
    /// Whether the codec enforces `max_input_bytes` limits.
    pub const fn enforces_max_input_bytes(&self) -> bool {
        self.enforces_max_input_bytes
    }

    /// Supported thread count range `(min, max)`.
    ///
    /// `(1, 1)` means single-threaded only.
    /// `(1, 8)` means the decoder can use 1 to 8 threads.
    pub const fn threads_supported_range(&self) -> (u16, u16) {
        self.threads_supported_range
    }

    /// Check whether this decoder supports a given operation.
    ///
    /// Returns `true` if the capability flag corresponding to `op` is set.
    /// Returns `false` for encode-only operations or [`PixelFormat`](UnsupportedOperation::PixelFormat)
    /// (which depends on the specific format, not a static flag).
    ///
    /// # Example
    ///
    /// ```
    /// use zencodec::UnsupportedOperation;
    /// use zencodec::decode::DecodeCapabilities;
    ///
    /// static CAPS: DecodeCapabilities = DecodeCapabilities::new()
    ///     .with_animation(true)
    ///     .with_decode_into(true);
    ///
    /// assert!(CAPS.supports(UnsupportedOperation::AnimationDecode));
    /// assert!(CAPS.supports(UnsupportedOperation::DecodeInto));
    /// assert!(!CAPS.supports(UnsupportedOperation::RowLevelDecode));
    /// assert!(!CAPS.supports(UnsupportedOperation::RowLevelEncode));
    /// ```
    pub const fn supports(&self, op: UnsupportedOperation) -> bool {
        match op {
            UnsupportedOperation::DecodeInto => self.decode_into,
            UnsupportedOperation::RowLevelDecode => self.streaming,
            UnsupportedOperation::AnimationDecode => self.animation,
            UnsupportedOperation::MultiImageDecode => self.multi_image,
            UnsupportedOperation::RowLevelEncode
            | UnsupportedOperation::PullEncode
            | UnsupportedOperation::AnimationEncode
            | UnsupportedOperation::PixelFormat => false,
        }
    }

    // --- Const builder methods ---

    /// Set whether the decoder extracts ICC color profiles.
    pub const fn with_icc(mut self, v: bool) -> Self {
        self.icc = v;
        self
    }
    /// Set whether the decoder extracts EXIF data.
    pub const fn with_exif(mut self, v: bool) -> Self {
        self.exif = v;
        self
    }
    /// Set whether the decoder extracts XMP data.
    pub const fn with_xmp(mut self, v: bool) -> Self {
        self.xmp = v;
        self
    }
    /// Set whether the decoder extracts CICP color description.
    pub const fn with_cicp(mut self, v: bool) -> Self {
        self.cicp = v;
        self
    }
    /// Set whether cooperative cancellation via [`Stop`](enough::Stop) is supported.
    pub const fn with_stop(mut self, v: bool) -> Self {
        self.stop = v;
        self
    }
    /// Set whether animation decoding is supported.
    pub const fn with_animation(mut self, v: bool) -> Self {
        self.animation = v;
        self
    }
    /// Set whether multi-image container decoding is supported.
    pub const fn with_multi_image(mut self, v: bool) -> Self {
        self.multi_image = v;
        self
    }
    /// Set whether `probe()` is cheap (header parse only).
    pub const fn with_cheap_probe(mut self, v: bool) -> Self {
        self.cheap_probe = v;
        self
    }
    /// Set whether `decode_into()` is implemented.
    pub const fn with_decode_into(mut self, v: bool) -> Self {
        self.decode_into = v;
        self
    }
    /// Set whether `StreamingDecode` / `streaming_decoder()` is supported.
    pub const fn with_streaming(mut self, v: bool) -> Self {
        self.streaming = v;
        self
    }
    /// Set whether HDR content is supported.
    pub const fn with_hdr(mut self, v: bool) -> Self {
        self.hdr = v;
        self
    }
    /// Set whether gain map (HDR/SDR) extraction is supported.
    pub const fn with_gain_map(mut self, v: bool) -> Self {
        self.gain_map = v;
        self
    }
    /// Set whether native grayscale decoding is supported.
    pub const fn with_native_gray(mut self, v: bool) -> Self {
        self.native_gray = v;
        self
    }
    /// Set whether native 16-bit per channel decoding is supported.
    pub const fn with_native_16bit(mut self, v: bool) -> Self {
        self.native_16bit = v;
        self
    }
    /// Set whether native f32 pixel data is supported.
    pub const fn with_native_f32(mut self, v: bool) -> Self {
        self.native_f32 = v;
        self
    }
    /// Set whether native alpha channel is supported.
    pub const fn with_native_alpha(mut self, v: bool) -> Self {
        self.native_alpha = v;
        self
    }
    /// Set whether the decoder enforces `max_pixels` limits.
    pub const fn with_enforces_max_pixels(mut self, v: bool) -> Self {
        self.enforces_max_pixels = v;
        self
    }
    /// Set whether the decoder enforces `max_memory_bytes` limits.
    pub const fn with_enforces_max_memory(mut self, v: bool) -> Self {
        self.enforces_max_memory = v;
        self
    }
    /// Set whether the decoder enforces `max_input_bytes` limits.
    pub const fn with_enforces_max_input_bytes(mut self, v: bool) -> Self {
        self.enforces_max_input_bytes = v;
        self
    }

    /// Set supported thread count range.
    pub const fn with_threads_supported_range(mut self, min: u16, max: u16) -> Self {
        assert!(min >= 1, "threads range: min must be >= 1");
        assert!(min <= max, "threads range: min must be <= max");
        self.threads_supported_range = (min, max);
        self
    }
}

impl fmt::Debug for DecodeCapabilities {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut s = f.debug_struct("DecodeCapabilities");
        s.field("icc", &self.icc)
            .field("exif", &self.exif)
            .field("xmp", &self.xmp)
            .field("cicp", &self.cicp)
            .field("stop", &self.stop)
            .field("animation", &self.animation)
            .field("multi_image", &self.multi_image)
            .field("cheap_probe", &self.cheap_probe)
            .field("decode_into", &self.decode_into)
            .field("streaming", &self.streaming)
            .field("hdr", &self.hdr)
            .field("gain_map", &self.gain_map)
            .field("native_gray", &self.native_gray)
            .field("native_16bit", &self.native_16bit)
            .field("native_f32", &self.native_f32)
            .field("native_alpha", &self.native_alpha)
            .field("enforces_max_pixels", &self.enforces_max_pixels)
            .field("enforces_max_memory", &self.enforces_max_memory)
            .field("enforces_max_input_bytes", &self.enforces_max_input_bytes)
            .field("threads_supported_range", &self.threads_supported_range);
        s.finish()
    }
}

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

    #[test]
    fn encode_default_all_false() {
        let caps = EncodeCapabilities::new();
        assert!(!caps.icc());
        assert!(!caps.exif());
        assert!(!caps.xmp());
        assert!(!caps.cicp());
        assert!(!caps.stop());
        assert!(!caps.animation());
        assert!(!caps.push_rows());
        assert!(!caps.encode_from());
        assert!(!caps.lossy());
        assert!(!caps.lossless());
        assert!(!caps.hdr());
        assert!(!caps.native_gray());
        assert!(!caps.native_16bit());
        assert!(!caps.native_f32());
        assert!(!caps.native_alpha());
        assert!(!caps.enforces_max_pixels());
        assert!(!caps.enforces_max_memory());
        assert!(caps.effort_range().is_none());
        assert!(caps.quality_range().is_none());
        assert_eq!(caps.threads_supported_range(), (1, 1));
    }

    #[test]
    fn decode_default_all_false() {
        let caps = DecodeCapabilities::new();
        assert!(!caps.icc());
        assert!(!caps.exif());
        assert!(!caps.xmp());
        assert!(!caps.cicp());
        assert!(!caps.stop());
        assert!(!caps.animation());
        assert!(!caps.cheap_probe());
        assert!(!caps.decode_into());
        assert!(!caps.streaming());
        assert!(!caps.hdr());
        assert!(!caps.native_gray());
        assert!(!caps.native_16bit());
        assert!(!caps.native_f32());
        assert!(!caps.native_alpha());
        assert!(!caps.enforces_max_pixels());
        assert!(!caps.enforces_max_memory());
        assert!(!caps.enforces_max_input_bytes());
        assert_eq!(caps.threads_supported_range(), (1, 1));
    }

    #[test]
    fn encode_builder() {
        let caps = EncodeCapabilities::new()
            .with_icc(true)
            .with_stop(true)
            .with_native_gray(true)
            .with_animation(true)
            .with_native_16bit(true)
            .with_hdr(true)
            .with_threads_supported_range(1, 8);
        assert!(caps.icc());
        assert!(!caps.exif());
        assert!(caps.stop());
        assert!(caps.native_gray());
        assert!(caps.animation());
        assert!(caps.native_16bit());
        assert!(!caps.lossless());
        assert!(caps.hdr());
        assert_eq!(caps.threads_supported_range(), (1, 8));
    }

    #[test]
    fn decode_builder() {
        let caps = DecodeCapabilities::new()
            .with_icc(true)
            .with_cheap_probe(true)
            .with_stop(true)
            .with_animation(true)
            .with_enforces_max_input_bytes(true)
            .with_threads_supported_range(1, 4);
        assert!(caps.icc());
        assert!(caps.cheap_probe());
        assert!(caps.stop());
        assert!(caps.animation());
        assert!(caps.enforces_max_input_bytes());
        assert_eq!(caps.threads_supported_range(), (1, 4));
    }

    #[test]
    fn encode_static_construction() {
        static CAPS: EncodeCapabilities = EncodeCapabilities::new()
            .with_icc(true)
            .with_exif(true)
            .with_xmp(true)
            .with_stop(true)
            .with_animation(true)
            .with_lossless(true)
            .with_cicp(true)
            .with_effort_range(0, 100)
            .with_quality_range(0.0, 100.0)
            .with_threads_supported_range(1, 16);
        assert!(CAPS.icc());
        assert!(CAPS.stop());
        assert!(!CAPS.native_gray());
        assert!(CAPS.animation());
        assert!(CAPS.lossless());
        assert!(CAPS.cicp());
        assert_eq!(CAPS.effort_range(), Some([0, 100]));
        assert_eq!(CAPS.quality_range(), Some([0.0, 100.0]));
        assert_eq!(CAPS.threads_supported_range(), (1, 16));
    }

    #[test]
    fn decode_static_construction() {
        static CAPS: DecodeCapabilities = DecodeCapabilities::new()
            .with_icc(true)
            .with_cheap_probe(true)
            .with_stop(true)
            .with_animation(true)
            .with_enforces_max_pixels(true)
            .with_enforces_max_input_bytes(true);
        assert!(CAPS.icc());
        assert!(CAPS.cheap_probe());
        assert!(CAPS.enforces_max_pixels());
        assert!(!CAPS.enforces_max_memory());
        assert!(CAPS.enforces_max_input_bytes());
    }

    #[test]
    fn encode_effort_quality_ranges() {
        let caps = EncodeCapabilities::new()
            .with_effort_range(0, 10)
            .with_quality_range(0.0, 100.0);
        assert_eq!(caps.effort_range(), Some([0i32, 10]));
        assert_eq!(caps.quality_range(), Some([0.0, 100.0]));
    }

    #[test]
    fn encode_supports() {
        let caps = EncodeCapabilities::new()
            .with_push_rows(true)
            .with_encode_from(true)
            .with_animation(true);
        assert!(caps.supports(UnsupportedOperation::RowLevelEncode));
        assert!(caps.supports(UnsupportedOperation::PullEncode));
        assert!(caps.supports(UnsupportedOperation::AnimationEncode));
        assert!(!caps.supports(UnsupportedOperation::DecodeInto));
        assert!(!caps.supports(UnsupportedOperation::RowLevelDecode));
        assert!(!caps.supports(UnsupportedOperation::AnimationDecode));
        assert!(!caps.supports(UnsupportedOperation::PixelFormat));
    }

    #[test]
    fn decode_supports() {
        let caps = DecodeCapabilities::new()
            .with_decode_into(true)
            .with_streaming(true)
            .with_animation(true);
        assert!(caps.supports(UnsupportedOperation::DecodeInto));
        assert!(caps.supports(UnsupportedOperation::RowLevelDecode));
        assert!(caps.supports(UnsupportedOperation::AnimationDecode));
        assert!(!caps.supports(UnsupportedOperation::RowLevelEncode));
        assert!(!caps.supports(UnsupportedOperation::PullEncode));
        assert!(!caps.supports(UnsupportedOperation::AnimationEncode));
        assert!(!caps.supports(UnsupportedOperation::PixelFormat));
    }

    #[test]
    fn supports_empty_all_false() {
        let enc = EncodeCapabilities::new();
        let dec = DecodeCapabilities::new();
        for op in [
            UnsupportedOperation::RowLevelEncode,
            UnsupportedOperation::PullEncode,
            UnsupportedOperation::AnimationEncode,
            UnsupportedOperation::DecodeInto,
            UnsupportedOperation::RowLevelDecode,
            UnsupportedOperation::AnimationDecode,
            UnsupportedOperation::PixelFormat,
        ] {
            assert!(
                !enc.supports(op),
                "EncodeCapabilities::EMPTY.supports({op:?})"
            );
            assert!(
                !dec.supports(op),
                "DecodeCapabilities::EMPTY.supports({op:?})"
            );
        }
    }

    #[test]
    fn unsupported_operation_display() {
        assert_eq!(
            alloc::format!("{}", UnsupportedOperation::RowLevelEncode),
            "unsupported operation: row_level_encode"
        );
        assert_eq!(
            alloc::format!("{}", UnsupportedOperation::DecodeInto),
            "unsupported operation: decode_into"
        );
        assert_eq!(
            alloc::format!("{}", UnsupportedOperation::PixelFormat),
            "unsupported operation: pixel_format"
        );
    }

    #[test]
    fn unsupported_operation_name() {
        assert_eq!(UnsupportedOperation::PullEncode.name(), "pull_encode");
        assert_eq!(
            UnsupportedOperation::AnimationEncode.name(),
            "animation_encode"
        );
        assert_eq!(
            UnsupportedOperation::RowLevelDecode.name(),
            "row_level_decode"
        );
        assert_eq!(
            UnsupportedOperation::AnimationDecode.name(),
            "animation_decode"
        );
    }

    #[test]
    fn unsupported_operation_is_error() {
        let op = UnsupportedOperation::DecodeInto;
        let err: &dyn core::error::Error = &op;
        assert!(err.source().is_none());
    }

    #[test]
    fn unsupported_operation_eq_hash() {
        assert_eq!(
            UnsupportedOperation::DecodeInto,
            UnsupportedOperation::DecodeInto
        );
        assert_ne!(
            UnsupportedOperation::DecodeInto,
            UnsupportedOperation::PullEncode
        );
    }
}