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
impl Codestream
Sourcepub fn new() -> Self
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.
Sourcepub fn restart(&mut self)
pub fn restart(&mut self)
Resets the codestream so it can be reused for another encode/decode operation without reallocating.
Sourcepub fn access_siz(&self) -> &ParamSiz
pub fn access_siz(&self) -> &ParamSiz
Returns a shared reference to the SIZ (image/tile size) parameters.
Sourcepub fn access_siz_mut(&mut self) -> &mut ParamSiz
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.
Sourcepub fn access_cod(&self) -> &ParamCod
pub fn access_cod(&self) -> &ParamCod
Returns a shared reference to the COD (coding style) parameters.
Sourcepub fn access_cod_mut(&mut self) -> &mut ParamCod
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.
Sourcepub fn access_qcd(&self) -> &ParamQcd
pub fn access_qcd(&self) -> &ParamQcd
Returns a shared reference to the QCD (quantization) parameters.
Sourcepub fn access_qcd_mut(&mut self) -> &mut ParamQcd
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.
Sourcepub fn access_nlt(&self) -> &ParamNlt
pub fn access_nlt(&self) -> &ParamNlt
Returns a shared reference to the NLT (nonlinearity) parameters.
Sourcepub fn access_nlt_mut(&mut self) -> &mut ParamNlt
pub fn access_nlt_mut(&mut self) -> &mut ParamNlt
Returns a mutable reference to the NLT parameters for configuration.
Sourcepub fn enable_resilience(&mut self)
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.
Sourcepub fn set_planar(&mut self, planar: i32)
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)
Sourcepub fn set_profile(&mut self, name: &str) -> Result<()>
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.
Sourcepub fn set_tilepart_divisions(&mut self, value: u32)
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.
Sourcepub fn request_tlm_marker(&mut self, needed: bool)
pub fn request_tlm_marker(&mut self, needed: bool)
Requests that TLM (tile-part length) markers be written in the codestream header.
Sourcepub fn restrict_input_resolution(
&mut self,
skipped_res_for_data: u32,
skipped_res_for_recon: u32,
)
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.
Sourcepub fn write_headers(
&mut self,
file: &mut dyn OutfileBase,
comments: &[CommentExchange],
) -> Result<()>
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.
Sourcepub fn exchange(&mut self, line: &[i32], comp_num: u32) -> Result<Option<usize>>
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.
Sourcepub fn flush(&mut self, file: &mut dyn OutfileBase) -> Result<()>
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.
Sourcepub fn read_headers(&mut self, file: &mut dyn InfileBase) -> Result<()>
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.
Sourcepub fn create(&mut self, file: &mut dyn InfileBase) -> Result<()>
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.).
Sourcepub fn pull(&mut self, comp_num: u32) -> Option<Vec<i32>>
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.
Sourcepub fn get_num_tiles(&self) -> Size
pub fn get_num_tiles(&self) -> Size
Returns the number of tiles in the x and y directions.