use super::*;
use crate::c_api_types::OPJ_OFF;
use crate::consts::j2k::J2K_MAX_POCS;
use crate::consts::J2K_MAXRLVLS;
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ColorSpace {
#[default]
Unknown,
Srgb,
Gray,
Sycc,
Eycc,
Cmyk,
}
impl From<OPJ_COLOR_SPACE> for ColorSpace {
fn from(cs: OPJ_COLOR_SPACE) -> Self {
match cs {
OPJ_CLRSPC_SRGB => ColorSpace::Srgb,
OPJ_CLRSPC_GRAY => ColorSpace::Gray,
OPJ_CLRSPC_SYCC => ColorSpace::Sycc,
OPJ_CLRSPC_EYCC => ColorSpace::Eycc,
OPJ_CLRSPC_CMYK => ColorSpace::Cmyk,
_ => ColorSpace::Unknown,
}
}
}
impl From<ColorSpace> for OPJ_COLOR_SPACE {
fn from(cs: ColorSpace) -> Self {
match cs {
ColorSpace::Unknown => OPJ_CLRSPC_UNKNOWN,
ColorSpace::Srgb => OPJ_CLRSPC_SRGB,
ColorSpace::Gray => OPJ_CLRSPC_GRAY,
ColorSpace::Sycc => OPJ_CLRSPC_SYCC,
ColorSpace::Eycc => OPJ_CLRSPC_EYCC,
ColorSpace::Cmyk => OPJ_CLRSPC_CMYK,
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ProgressionOrder {
#[default]
Lrcp,
Rlcp,
Rpcl,
Pcrl,
Cprl,
}
impl From<crate::c_api_types::OPJ_PROG_ORDER> for ProgressionOrder {
fn from(p: crate::c_api_types::OPJ_PROG_ORDER) -> Self {
use crate::c_api_types::{OPJ_CPRL, OPJ_PCRL, OPJ_RLCP, OPJ_RPCL};
match p {
OPJ_RLCP => ProgressionOrder::Rlcp,
OPJ_RPCL => ProgressionOrder::Rpcl,
OPJ_PCRL => ProgressionOrder::Pcrl,
OPJ_CPRL => ProgressionOrder::Cprl,
_ => ProgressionOrder::Lrcp,
}
}
}
impl From<ProgressionOrder> for crate::c_api_types::OPJ_PROG_ORDER {
fn from(p: ProgressionOrder) -> Self {
use crate::c_api_types::{OPJ_CPRL, OPJ_LRCP, OPJ_PCRL, OPJ_RLCP, OPJ_RPCL};
match p {
ProgressionOrder::Lrcp => OPJ_LRCP,
ProgressionOrder::Rlcp => OPJ_RLCP,
ProgressionOrder::Rpcl => OPJ_RPCL,
ProgressionOrder::Pcrl => OPJ_PCRL,
ProgressionOrder::Cprl => OPJ_CPRL,
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DecodeArea {
pub start_x: u32,
pub start_y: u32,
pub end_x: u32,
pub end_y: u32,
}
impl DecodeArea {
pub fn new(start_x: u32, start_y: u32, end_x: u32, end_y: u32) -> Self {
Self {
start_x,
start_y,
end_x,
end_y,
}
}
}
impl std::str::FromStr for DecodeArea {
type Err = std::num::ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let dims = s
.splitn(4, ':')
.map(|v| v.parse::<u32>())
.collect::<Result<Vec<u32>, _>>()?;
Ok(Self::new(
dims.first().copied().unwrap_or(0),
dims.get(1).copied().unwrap_or(0),
dims.get(2).copied().unwrap_or(0),
dims.get(3).copied().unwrap_or(0),
))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DecodeLimits {
pub max_image_width: u32,
pub max_image_height: u32,
pub max_tile_pixels: u64,
pub max_mem_alloc: u64,
pub max_metadata_len: usize,
}
pub const DEFAULT_DECODE_MEM_LIMIT: u64 = 1 << 30;
impl DecodeLimits {
pub(crate) fn effective_max_mem(&self) -> u64 {
if self.max_mem_alloc == 0 {
DEFAULT_DECODE_MEM_LIMIT
} else {
self.max_mem_alloc
}
}
}
impl DecodeLimits {
pub const fn new() -> Self {
Self {
max_image_width: 0,
max_image_height: 0,
max_tile_pixels: 0,
max_mem_alloc: 0,
max_metadata_len: 0,
}
}
pub fn max_image_size(mut self, width: u32, height: u32) -> Self {
self.max_image_width = width;
self.max_image_height = height;
self
}
pub fn max_tile_pixels(mut self, pixels: u64) -> Self {
self.max_tile_pixels = pixels;
self
}
pub fn max_mem_alloc(mut self, bytes: u64) -> Self {
self.max_mem_alloc = bytes;
self
}
pub fn max_metadata_len(mut self, bytes: usize) -> Self {
self.max_metadata_len = bytes;
self
}
}
#[derive(Debug, Clone, Default)]
pub struct DecodeParameters {
pub reduce: u32,
pub layers: u32,
pub strict: bool,
pub decode_area: Option<DecodeArea>,
pub ignore_pclr_cmap_cdef: bool,
pub threads: usize,
pub limits: DecodeLimits,
}
impl DecodeParameters {
pub fn new() -> Self {
Self::default()
}
pub fn reduce(mut self, reduce: u32) -> Self {
self.reduce = reduce;
self
}
pub fn layers(mut self, layers: u32) -> Self {
self.layers = layers;
self
}
pub fn strict(mut self, strict: bool) -> Self {
self.strict = strict;
self
}
pub fn limits(mut self, limits: DecodeLimits) -> Self {
self.limits = limits;
self
}
pub fn decode_area(mut self, area: Option<DecodeArea>) -> Self {
self.decode_area = area;
self
}
pub fn threads(mut self, n: usize) -> Self {
self.threads = n;
self
}
pub fn ignore_pclr_cmap_cdef(mut self) -> Self {
self.ignore_pclr_cmap_cdef = true;
self
}
}
#[derive(Clone, Debug)]
pub struct EncoderParameters {
pub tile_size_on: bool,
pub cp_tx0: core::ffi::c_int,
pub cp_ty0: core::ffi::c_int,
pub cp_tdx: core::ffi::c_int,
pub cp_tdy: core::ffi::c_int,
pub cp_disto_alloc: core::ffi::c_int,
pub cp_fixed_alloc: core::ffi::c_int,
pub cp_fixed_quality: core::ffi::c_int,
pub cp_matrice: Option<Vec<core::ffi::c_int>>,
pub cp_comment: Option<Vec<u8>>,
pub csty: core::ffi::c_int,
pub prog_order: crate::api::ProgressionOrder,
pub pocs: [crate::types::Poc; J2K_MAX_POCS as usize],
pub numpocs: u32,
pub tcp_numlayers: core::ffi::c_int,
pub tcp_rates: [core::ffi::c_float; 100],
pub tcp_distoratio: [core::ffi::c_float; 100],
pub numresolution: core::ffi::c_int,
pub cblockw_init: core::ffi::c_int,
pub cblockh_init: core::ffi::c_int,
pub mode: core::ffi::c_int,
pub irreversible: core::ffi::c_int,
pub roi_compno: core::ffi::c_int,
pub roi_shift: core::ffi::c_int,
pub res_spec: core::ffi::c_int,
pub prcw_init: [core::ffi::c_int; J2K_MAXRLVLS as usize],
pub prch_init: [core::ffi::c_int; J2K_MAXRLVLS as usize],
pub index_on: core::ffi::c_int,
pub image_offset_x0: core::ffi::c_int,
pub image_offset_y0: core::ffi::c_int,
pub subsampling_dx: core::ffi::c_int,
pub subsampling_dy: core::ffi::c_int,
pub decod_format: core::ffi::c_int,
pub cod_format: core::ffi::c_int,
pub cp_cinema: u32,
pub max_comp_size: core::ffi::c_int,
pub cp_rsiz: u16,
pub tp_on: core::ffi::c_char,
pub tp_flag: core::ffi::c_char,
pub tcp_mct: core::ffi::c_char,
pub jpip_on: core::ffi::c_int,
pub mct_data: Option<(Vec<f32>, Vec<i32>)>,
pub max_cs_size: core::ffi::c_int,
pub rsiz: u16,
}
impl Default for EncoderParameters {
fn default() -> Self {
Self {
tile_size_on: Default::default(),
cp_tx0: Default::default(),
cp_ty0: Default::default(),
cp_tdx: Default::default(),
cp_tdy: Default::default(),
cp_disto_alloc: Default::default(),
cp_fixed_alloc: Default::default(),
cp_fixed_quality: Default::default(),
cp_matrice: None,
cp_comment: None,
csty: Default::default(),
prog_order: crate::api::ProgressionOrder::Lrcp,
pocs: Default::default(),
numpocs: Default::default(),
tcp_numlayers: Default::default(),
tcp_rates: [Default::default(); 100],
tcp_distoratio: [Default::default(); 100],
numresolution: 6i32,
cblockw_init: 64i32,
cblockh_init: 64i32,
mode: Default::default(),
irreversible: Default::default(),
roi_compno: -1,
roi_shift: Default::default(),
res_spec: Default::default(),
prcw_init: [Default::default(); J2K_MAXRLVLS as usize],
prch_init: [Default::default(); J2K_MAXRLVLS as usize],
index_on: Default::default(),
image_offset_x0: Default::default(),
image_offset_y0: Default::default(),
subsampling_dx: 1,
subsampling_dy: 1,
decod_format: -1,
cod_format: -1,
cp_cinema: OPJ_OFF,
max_comp_size: Default::default(),
cp_rsiz: 0,
tp_on: Default::default(),
tp_flag: Default::default(),
tcp_mct: Default::default(),
jpip_on: Default::default(),
mct_data: None,
max_cs_size: Default::default(),
rsiz: Default::default(),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct EncodeParameters {
inner: crate::EncoderParameters,
}
impl EncodeParameters {
pub fn new() -> Self {
Self {
inner: crate::EncoderParameters::default(),
}
}
pub fn num_resolutions(mut self, n: u32) -> Self {
self.inner.numresolution = n as i32;
self
}
pub fn num_layers(mut self, n: u32) -> Self {
self.inner.tcp_numlayers = n as i32;
self
}
pub fn irreversible(mut self, yes: bool) -> Self {
self.inner.irreversible = yes as i32;
self
}
pub fn progression_order(mut self, order: ProgressionOrder) -> Self {
self.inner.prog_order = order;
self
}
pub fn tile_size(mut self, w: u32, h: u32) -> Self {
self.inner.tile_size_on = true;
self.inner.cp_tdx = w as i32;
self.inner.cp_tdy = h as i32;
self
}
pub fn layer_rate(mut self, rate: f32) -> Self {
let n = self.inner.tcp_numlayers as usize;
debug_assert!(n < 100, "layer_rate past the 100-layer limit");
if n < 100 {
self.inner.cp_disto_alloc = 1;
self.inner.tcp_rates[n] = rate;
self.inner.tcp_numlayers += 1;
}
self
}
pub fn comment(mut self, comment: &str) -> Self {
self.inner.cp_comment = Some(comment.as_bytes().to_vec());
self
}
pub fn codeblock_width(mut self, w: u32) -> Self {
self.inner.cblockw_init = w as i32;
self
}
pub fn codeblock_height(mut self, h: u32) -> Self {
self.inner.cblockh_init = h as i32;
self
}
pub fn low_level(&mut self) -> &mut crate::EncoderParameters {
&mut self.inner
}
pub fn into_inner(self) -> crate::EncoderParameters {
self.inner
}
}