j2k_types/decode_payload.rs
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Borrowed-input byte descriptors shared by prepared decode plans.
4
5/// Byte range inside a complete encoded J2K, JP2, or JPH input.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub struct J2kCodestreamRange {
8 /// Zero-based offset from the beginning of the encoded input.
9 pub offset: usize,
10 /// Number of bytes in the range.
11 pub length: usize,
12}
13
14impl J2kCodestreamRange {
15 /// End offset, or `None` when the range overflows `usize`.
16 #[must_use]
17 pub const fn end(self) -> Option<usize> {
18 self.offset.checked_add(self.length)
19 }
20}
21
22/// One retained cleanup/refinement record for an HTJ2K code block.
23///
24/// The first record for a code block contains its cleanup range and may
25/// contain the first refinement fragment. When refinement bytes are split
26/// across packets, immediately following continuation records have an empty
27/// cleanup range and a non-empty refinement range. Consumers use the code
28/// block's declared refinement length to determine where the next block starts.
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
30pub struct HtCodeBlockPayloadRanges {
31 /// Cleanup-pass payload range.
32 pub cleanup: J2kCodestreamRange,
33 /// SigProp/MagRef payload fragment, when refinement is present.
34 pub refinement: Option<J2kCodestreamRange>,
35}
36
37/// Ordered encoded-input fragment span for one classic JPEG 2000 code block.
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
39pub struct J2kClassicCodeBlockPayload {
40 /// Index of the first fragment in the owning plan's range array.
41 pub first_range: usize,
42 /// Number of ordered fragments contributing to this code block.
43 pub range_count: usize,
44 /// Total bytes after concatenating every fragment.
45 pub combined_length: usize,
46}
47
48impl J2kClassicCodeBlockPayload {
49 /// Exclusive fragment index, or `None` when the descriptor overflows.
50 #[must_use]
51 pub const fn end_range(self) -> Option<usize> {
52 self.first_range.checked_add(self.range_count)
53 }
54}