openjp2 0.7.2

Rust port of Openjpeg.
Documentation
// Split of the safe API (doc/api-review.md): see the facade in api.rs
// and doc/redesign-proposal.md B.3.2.

use super::*;

/// Header/decode driver for the codestream index/info and the dump text
/// (the jpeg2k `DumpImage` equivalent).
pub struct DumpImage {
  codec: Decoder,
  stream: Stream,
}

impl DumpImage {
  /// Wrap an existing decoder and stream for index/info/dump access (the
  /// decoder's header must already have been read).  Composes the dump
  /// facility with the low-level `Decoder` API.
  pub fn new(codec: Decoder, stream: Stream) -> Self {
    Self { codec, stream }
  }

  /// The wrapped decoder (index/info state lives on it).
  pub fn decoder(&self) -> &Decoder {
    &self.codec
  }

  #[allow(deprecated)]
  /// Wraps the given codestream bytes with default parameters.
  pub fn from_bytes(buf: &[u8]) -> Result<Self> {
    Self::from_bytes_with(buf, DecodeParameters::default())
  }

  /// Wraps the given codestream bytes with explicit decode parameters.
  pub fn from_bytes_with(buf: &[u8], params: DecodeParameters) -> Result<Self> {
    let format =
      detect_format(buf).ok_or_else(|| Error::Unsupported("unknown JPEG 2000 format".into()))?;
    let mut stream = Stream::from_bytes(buf.to_vec());
    let mut codec = Decoder::new(format)?;
    codec.apply_settings(&DecoderSettings {
      layers: params.layers,
      reduce: params.reduce,
      ignore_pclr_cmap_cdef: params.ignore_pclr_cmap_cdef,
      threads: params.threads,
      ..Default::default()
    })?;
    /* Mirror decode_stream_inner: the strict flag and the resource limits
    must reach the DumpImage decoder too (they used to be silently
    dropped). */
    codec.set_strict(params.strict)?;
    codec.set_limits(params.limits)?;
    let header = catch_codec_panic(true, || codec.read_header(&mut stream))?;
    if let Some(area) = params.decode_area {
      let mut core = header.into_core_image();
      codec.set_decode_area(&mut core, Some(area))?;
    }
    Ok(Self { codec, stream })
  }

  /// Wraps a codestream file with default parameters.
  pub fn from_file<P: AsRef<std::path::Path>>(path: P) -> Result<Self> {
    let buf = std::fs::read(path)?;
    Self::from_bytes(&buf)
  }

  /// Wraps a codestream file with explicit decode parameters.
  pub fn from_file_with<P: AsRef<std::path::Path>>(
    path: P,
    params: DecodeParameters,
  ) -> Result<Self> {
    let buf = std::fs::read(path)?;
    Self::from_bytes_with(&buf, params)
  }

  /// Decode the tile data (needed to populate the codestream index with
  /// the tile markers).
  pub fn decode(&mut self) -> Result<()> {
    let codec = &mut self.codec;
    let stream = &mut self.stream;
    /* The Decoder keeps its own header copy, so a failed decode cannot
    trap a retry (the retry simply fails again on the mid-state stream,
    matching the C). */
    catch_codec_panic(true, || codec.decode(stream).map(|_| ()))
  }

  /// The codestream index (tile-parts, markers, packets).
  pub fn get_codestream_index(&self) -> Option<crate::CodestreamIndex> {
    self.codec.cstr_index()
  }

  /// The codestream header info (v2).
  pub fn get_codestream_info(&self) -> Option<crate::CstrInfoV2> {
    self.codec.cstr_info()
  }

  /// The dump text for the given info flag.  A decoder panic inside the
  /// dump is contained (the panic message becomes the dump text) instead
  /// of aborting the host, consistent with the crate's decode policy.
  pub fn dump(&self, info_flag: i32) -> String {
    match catch_codec_panic(true, || {
      let mut out = String::new();
      self.codec.dump_codec(info_flag, &mut out);
      Ok(out)
    }) {
      Ok(out) => out,
      Err(err) => err.to_string(),
    }
  }
}

/// Dump the image header (dimensions, components, color space) — the
/// C's `OPJ_IMG_INFO` bit.
pub const IMG_INFO: i32 = 1;
/// Dump the codestream main header (COD/QCD/... marker details) — the
/// C's `OPJ_J2K_MH_INFO` bit.
pub const J2K_MH_INFO: i32 = 2;
/// Dump the main header in the indented form — the C's
/// `OPJ_J2K_MH_IND` bit.
pub const J2K_MH_IND: i32 = 16;
/// Everything: image header plus both main-header forms.
pub const ALL: i32 = IMG_INFO | J2K_MH_INFO | J2K_MH_IND;