use super::*;
pub struct DumpImage {
codec: Decoder,
stream: Stream,
}
impl DumpImage {
pub fn new(codec: Decoder, stream: Stream) -> Self {
Self { codec, stream }
}
pub fn decoder(&self) -> &Decoder {
&self.codec
}
#[allow(deprecated)]
pub fn from_bytes(buf: &[u8]) -> Result<Self> {
Self::from_bytes_with(buf, DecodeParameters::default())
}
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()
})?;
codec.set_strict(params.strict)?;
codec.set_limits(params.limits)?;
catch_codec_panic(true, || codec.read_header(&mut stream).map(|_| ()))?;
Ok(Self { codec, stream })
}
pub fn from_file<P: AsRef<std::path::Path>>(path: P) -> Result<Self> {
let buf = std::fs::read(path)?;
Self::from_bytes(&buf)
}
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)
}
pub fn decode(&mut self) -> Result<()> {
let codec = &mut self.codec;
let stream = &mut self.stream;
catch_codec_panic(true, || codec.decode(stream).map(|_| ()))
}
pub fn get_codestream_index(&self) -> Option<crate::CodestreamIndex> {
self.codec.cstr_index()
}
pub fn get_codestream_info(&self) -> Option<crate::CstrInfoV2> {
self.codec.cstr_info()
}
pub fn dump(&self, info_flag: i32) -> String {
let mut out = String::new();
self.codec.dump_codec(info_flag, &mut out);
out
}
}
pub const IMG_INFO: i32 = 1;
pub const J2K_MH_INFO: i32 = 2;
pub const J2K_MH_IND: i32 = 16;
pub const ALL: i32 = IMG_INFO | J2K_MH_INFO | J2K_MH_IND;