Skip to main content

rust_j2k/
header.rs

1//! What [`read_header`](crate::read_header) returns: an image's shape and
2//! metadata, read without decoding any tile data.
3
4use crate::codestream::markers::Siz;
5use crate::jp2::Jp2Metadata;
6
7/// An image's header: what [`decode`](crate::decode) would return, without
8/// the samples.
9///
10/// The struct is `#[non_exhaustive]`, so more of the header can be added
11/// without a breaking change.
12#[derive(Debug, Clone, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct Header {
15    /// The image area's width, as [`Image::width`](crate::Image::width): on
16    /// the reduced grid under a resolution reduction.
17    pub width: u32,
18    /// The image area's height, as [`Image::height`](crate::Image::height):
19    /// on the reduced grid under a resolution reduction.
20    pub height: u32,
21    /// One entry per [`Image::components`](crate::Image::components) entry,
22    /// in the same order: for a palettized JP2 file, the palette's channels.
23    pub components: Vec<ComponentInfo>,
24    /// The largest
25    /// [`resolution_reduction`](crate::DecodeOptions::resolution_reduction)
26    /// the main header allows: the fewest decomposition levels any component
27    /// has in the main header's `COD` and `COC` markers. A larger reduction is
28    /// [`Error::InvalidOptions`](crate::Error::InvalidOptions). A tile-part
29    /// `COD` or `COC` with fewer levels can still refuse a reduction up to
30    /// this one, but only the decode finds that.
31    pub max_reduction: u8,
32    /// What [`decode`](crate::decode) would put on
33    /// [`Image::jp2`](crate::Image::jp2): `None` for a raw codestream.
34    pub jp2: Option<Jp2Metadata>,
35}
36
37/// One output channel's sample format and size, as on the
38/// [`Component`](crate::Component) that [`decode`](crate::decode) would
39/// return.
40///
41/// The struct is `#[non_exhaustive]`, so more of a channel's description can
42/// be added without a breaking change.
43#[derive(Debug, Clone, PartialEq, Eq)]
44#[non_exhaustive]
45pub struct ComponentInfo {
46    /// Bits per sample: 1 to 31 for unsigned samples, 1 to 32 for signed
47    /// ones, as [`Component::bit_depth`](crate::Component::bit_depth).
48    pub bit_depth: u8,
49    /// Whether the samples are signed.
50    pub signed: bool,
51    /// Horizontal sub-sampling factor on the reference grid; `1` means no
52    /// sub-sampling.
53    pub x_sampling: u8,
54    /// Vertical sub-sampling factor on the reference grid; `1` means no
55    /// sub-sampling.
56    pub y_sampling: u8,
57    /// The channel's width in samples, as
58    /// [`Component::width`](crate::Component::width): `ceil(x1 / dx) -
59    /// ceil(x0 / dx)` for the image area `x0..x1` and the sub-sampling `dx`.
60    /// Under a resolution reduction of `r` levels each end is reduced on its
61    /// own, as OpenJPEG has it: `ceil(ceil(x1 / dx) / 2^r) - ceil(ceil(x0 /
62    /// dx) / 2^r)`. Neither is `ceil(width / dx)`, nor the full width halved:
63    /// those are off by one when the image area starts at an odd offset.
64    pub width: u32,
65    /// The channel's height in samples, as
66    /// [`Component::height`](crate::Component::height); see
67    /// [`width`](Self::width).
68    pub height: u32,
69}
70
71impl ComponentInfo {
72    /// A channel of this format and sub-sampling, sized from `siz` at a
73    /// resolution reduction. A zero sub-sampling factor, which the parser
74    /// rejects, gives an empty channel.
75    pub(crate) fn on_grid(
76        siz: &Siz,
77        bit_depth: u8,
78        signed: bool,
79        x_sampling: u8,
80        y_sampling: u8,
81        reduction: u8,
82    ) -> Self {
83        let (width, height) = siz
84            .sampled_extent_at(x_sampling, y_sampling, reduction)
85            .unwrap_or((0, 0));
86        ComponentInfo {
87            bit_depth,
88            signed,
89            x_sampling,
90            y_sampling,
91            width,
92            height,
93        }
94    }
95
96    /// The codestream's components as coded, sized at a resolution reduction.
97    pub(crate) fn of_siz(siz: &Siz, reduction: u8) -> Vec<Self> {
98        siz.components
99            .iter()
100            .map(|c| {
101                Self::on_grid(
102                    siz,
103                    c.bit_depth,
104                    c.signed,
105                    c.x_sampling,
106                    c.y_sampling,
107                    reduction,
108                )
109            })
110            .collect()
111    }
112}