rust-j2k 0.4.0

Decodes JPEG 2000 images in pure Rust, for programs that need to read them without linking a C library.
Documentation
//! What [`read_header`](crate::read_header) returns: an image's shape and
//! metadata, read without decoding any tile data.

use crate::codestream::markers::Siz;
use crate::jp2::Jp2Metadata;

/// An image's header: what [`decode`](crate::decode) would return, without
/// the samples.
///
/// The struct is `#[non_exhaustive]`, so more of the header can be added
/// without a breaking change.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Header {
    /// The image area's width, as [`Image::width`](crate::Image::width): on
    /// the reduced grid under a resolution reduction.
    pub width: u32,
    /// The image area's height, as [`Image::height`](crate::Image::height):
    /// on the reduced grid under a resolution reduction.
    pub height: u32,
    /// One entry per [`Image::components`](crate::Image::components) entry,
    /// in the same order: for a palettized JP2 file, the palette's channels.
    pub components: Vec<ComponentInfo>,
    /// The largest
    /// [`resolution_reduction`](crate::DecodeOptions::resolution_reduction)
    /// the main header allows: the fewest decomposition levels any component
    /// has in the main header's `COD` and `COC` markers. A larger reduction is
    /// [`Error::InvalidOptions`](crate::Error::InvalidOptions). A tile-part
    /// `COD` or `COC` with fewer levels can still refuse a reduction up to
    /// this one, but only the decode finds that.
    pub max_reduction: u8,
    /// What [`decode`](crate::decode) would put on
    /// [`Image::jp2`](crate::Image::jp2): `None` for a raw codestream.
    pub jp2: Option<Jp2Metadata>,
}

/// One output channel's sample format and size, as on the
/// [`Component`](crate::Component) that [`decode`](crate::decode) would
/// return.
///
/// The struct is `#[non_exhaustive]`, so more of a channel's description can
/// be added without a breaking change.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ComponentInfo {
    /// Bits per sample: 1 to 31 for unsigned samples, 1 to 32 for signed
    /// ones, as [`Component::bit_depth`](crate::Component::bit_depth).
    pub bit_depth: u8,
    /// Whether the samples are signed.
    pub signed: bool,
    /// Horizontal sub-sampling factor on the reference grid; `1` means no
    /// sub-sampling.
    pub x_sampling: u8,
    /// Vertical sub-sampling factor on the reference grid; `1` means no
    /// sub-sampling.
    pub y_sampling: u8,
    /// The channel's width in samples, as
    /// [`Component::width`](crate::Component::width): `ceil(x1 / dx) -
    /// ceil(x0 / dx)` for the image area `x0..x1` and the sub-sampling `dx`.
    /// Under a resolution reduction of `r` levels each end is reduced on its
    /// own, as OpenJPEG has it: `ceil(ceil(x1 / dx) / 2^r) - ceil(ceil(x0 /
    /// dx) / 2^r)`. Neither is `ceil(width / dx)`, nor the full width halved:
    /// those are off by one when the image area starts at an odd offset.
    pub width: u32,
    /// The channel's height in samples, as
    /// [`Component::height`](crate::Component::height); see
    /// [`width`](Self::width).
    pub height: u32,
}

impl ComponentInfo {
    /// A channel of this format and sub-sampling, sized from `siz` at a
    /// resolution reduction. A zero sub-sampling factor, which the parser
    /// rejects, gives an empty channel.
    pub(crate) fn on_grid(
        siz: &Siz,
        bit_depth: u8,
        signed: bool,
        x_sampling: u8,
        y_sampling: u8,
        reduction: u8,
    ) -> Self {
        let (width, height) = siz
            .sampled_extent_at(x_sampling, y_sampling, reduction)
            .unwrap_or((0, 0));
        ComponentInfo {
            bit_depth,
            signed,
            x_sampling,
            y_sampling,
            width,
            height,
        }
    }

    /// The codestream's components as coded, sized at a resolution reduction.
    pub(crate) fn of_siz(siz: &Siz, reduction: u8) -> Vec<Self> {
        siz.components
            .iter()
            .map(|c| {
                Self::on_grid(
                    siz,
                    c.bit_depth,
                    c.signed,
                    c.x_sampling,
                    c.y_sampling,
                    reduction,
                )
            })
            .collect()
    }
}