Skip to main content

Codestream

Struct Codestream 

Source
pub struct Codestream { /* private fields */ }
Expand description

The main codestream interface for encoding and decoding HTJ2K images.

Codestream is the public wrapper over the internal codec engine, mirroring the C++ ojph::codestream class (which uses a pImpl pattern).

§Examples

Lossless encode + decode round-trip (single-component 8-bit)

use openjph_core::codestream::Codestream;
use openjph_core::file::{MemOutfile, MemInfile};
use openjph_core::types::{Point, Size};

// Configure a minimal 8×8 grayscale image
let (w, h) = (8u32, 8u32);
let mut cs = Codestream::new();
cs.access_siz_mut().set_image_extent(Point::new(w, h));
cs.access_siz_mut().set_num_components(1);
cs.access_siz_mut().set_comp_info(0, Point::new(1, 1), 8, false);
cs.access_siz_mut().set_tile_size(Size::new(w, h));
cs.access_cod_mut().set_num_decomposition(0);
cs.access_cod_mut().set_reversible(true);
cs.access_cod_mut().set_color_transform(false);
cs.set_planar(0);

// Encode
let mut out = MemOutfile::new();
cs.write_headers(&mut out, &[]).unwrap();
let row = vec![42i32; w as usize];
for _ in 0..h { cs.exchange(&row, 0).unwrap(); }
cs.flush(&mut out).unwrap();

// Decode
let data = out.get_data().to_vec();
let mut inp = MemInfile::new(&data);
let mut dec = Codestream::new();
dec.read_headers(&mut inp).unwrap();
dec.create(&mut inp).unwrap();
for _ in 0..h {
    let line = dec.pull(0).unwrap();
    assert_eq!(line, row);
}

Implementations§

Source§

impl Codestream

Source

pub fn new() -> Self

Creates a new codestream with default parameters.

All marker segments (SIZ, COD, QCD, NLT) are initialised to sensible defaults. You must at least set the image extent, number of components, and component info before encoding.

Source

pub fn restart(&mut self)

Resets the codestream so it can be reused for another encode/decode operation without reallocating.

Source

pub fn access_siz(&self) -> &ParamSiz

Returns a shared reference to the SIZ (image/tile size) parameters.

Source

pub fn access_siz_mut(&mut self) -> &mut ParamSiz

Returns a mutable reference to the SIZ parameters for configuration.

Use this before calling write_headers() to set image extent, tile size, number of components, and per-component info.

Source

pub fn access_cod(&self) -> &ParamCod

Returns a shared reference to the COD (coding style) parameters.

Source

pub fn access_cod_mut(&mut self) -> &mut ParamCod

Returns a mutable reference to the COD parameters for configuration.

Use this before calling write_headers() to set decomposition levels, reversibility, block sizes, and color transform.

Source

pub fn access_qcd(&self) -> &ParamQcd

Returns a shared reference to the QCD (quantization) parameters.

Source

pub fn access_qcd_mut(&mut self) -> &mut ParamQcd

Returns a mutable reference to the QCD parameters for configuration.

For lossy (irreversible) compression, use set_delta() to set the base quantization step size.

Source

pub fn access_nlt(&self) -> &ParamNlt

Returns a shared reference to the NLT (nonlinearity) parameters.

Source

pub fn access_nlt_mut(&mut self) -> &mut ParamNlt

Returns a mutable reference to the NLT parameters for configuration.

Source

pub fn enable_resilience(&mut self)

Enables resilient (error-tolerant) mode for decoding.

When enabled, the decoder attempts to recover from malformed tile-part headers rather than returning an error.

Source

pub fn set_planar(&mut self, planar: i32)

Sets the line exchange mode.

  • 0 — interleaved (all components for each line exchanged together)
  • non-zero — planar (one component at a time)
Source

pub fn set_profile(&mut self, name: &str) -> Result<()>

Sets the codestream profile.

Accepted values: "IMF", "BROADCAST", "CINEMA2K", "CINEMA4K", etc.

§Errors

Returns OjphError::InvalidParam if name is not a recognised profile string.

Source

pub fn set_tilepart_divisions(&mut self, value: u32)

Sets tilepart division flags (bit-field).

Use 0x1 for resolution-based divisions and 0x2 for component-based divisions.

Source

pub fn request_tlm_marker(&mut self, needed: bool)

Requests that TLM (tile-part length) markers be written in the codestream header.

Source

pub fn restrict_input_resolution( &mut self, skipped_res_for_data: u32, skipped_res_for_recon: u32, )

Restricts the number of resolution levels used during decoding.

skipped_res_for_data controls how many resolution levels are skipped when reading tile data. skipped_res_for_recon controls how many are skipped for reconstruction.

Source

pub fn write_headers( &mut self, file: &mut dyn OutfileBase, comments: &[CommentExchange], ) -> Result<()>

Writes the codestream main header (SOC through to the end of the main header) into file.

Optional COM (comment) markers can be included via comments.

§Errors

Returns an error if parameter validation fails or writing to file encounters an I/O error.

Source

pub fn exchange(&mut self, line: &[i32], comp_num: u32) -> Result<Option<usize>>

Pushes one line of image data for the specified component.

Call this repeatedly (height × num_components times in interleaved mode, or height times per component in planar mode) to supply the full image.

Returns Some(next_line_index) while more lines are needed, or None when all lines for the current tile have been pushed.

§Errors

Returns an error if the internal encoder encounters a problem.

Source

pub fn flush(&mut self, file: &mut dyn OutfileBase) -> Result<()>

Flushes the encoder: encodes all pending tiles and writes tile data plus the EOC (end of codestream) marker to file.

§Errors

Returns an error on I/O failure or if the encoder state is invalid.

Source

pub fn read_headers(&mut self, file: &mut dyn InfileBase) -> Result<()>

Reads codestream main headers from file.

After this call, you can inspect image parameters through access_siz() and friends.

§Errors

Returns an error if the stream does not contain valid JPEG 2000 codestream headers.

Source

pub fn create(&mut self, file: &mut dyn InfileBase) -> Result<()>

Builds internal decoding structures and decodes tile data from file.

Must be called after read_headers(). After this call, decoded lines can be retrieved with pull().

§Errors

Returns an error if decoding fails (corrupt data, unsupported features, etc.).

Source

pub fn pull(&mut self, comp_num: u32) -> Option<Vec<i32>>

Pulls the next decoded line for the given comp_num.

Returns None when all lines for this component have been returned.

Source

pub fn is_planar(&self) -> bool

Returns true if planar mode is active.

Source

pub fn get_num_tiles(&self) -> Size

Returns the number of tiles in the x and y directions.

Trait Implementations§

Source§

impl Debug for Codestream

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Codestream

Source§

fn default() -> Codestream

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.