use super::math::*;
use super::openjpeg::*;
pub struct ImageCompRef<'a> {
pub comp: &'a CoreImageComp,
pub adjust: i32,
pub data: &'a [i32],
}
pub(crate) fn image_comp_header_update(p_image_header: &mut CoreImage, p_cp: &Cp) {
let l_x0 = uint_max(p_cp.tx0, p_image_header.x0);
let l_y0 = uint_max(p_cp.ty0, p_image_header.y0);
let l_x1 = p_cp
.tx0
.wrapping_add(p_cp.tw.wrapping_sub(1u32).wrapping_mul(p_cp.tdx));
let l_y1 = p_cp
.ty0
.wrapping_add(p_cp.th.wrapping_sub(1u32).wrapping_mul(p_cp.tdy));
let l_x1 = uint_min(uint_adds(l_x1, p_cp.tdx), p_image_header.x1);
let l_y1 = uint_min(uint_adds(l_y1, p_cp.tdy), p_image_header.y1);
for comp in &mut p_image_header.comps {
let l_comp_x0 = uint_ceildiv(l_x0, comp.dx);
let l_comp_y0 = uint_ceildiv(l_y0, comp.dy);
let l_comp_x1 = uint_ceildiv(l_x1, comp.dx);
let l_comp_y1 = uint_ceildiv(l_y1, comp.dy);
let l_width =
uint_ceildivpow2(l_comp_x1, comp.factor) - uint_ceildivpow2(l_comp_x0, comp.factor);
let l_height =
uint_ceildivpow2(l_comp_y1, comp.factor) - uint_ceildivpow2(l_comp_y0, comp.factor);
comp.w = l_width;
comp.h = l_height;
comp.x0 = l_comp_x0;
comp.y0 = l_comp_y0;
}
}
pub(crate) fn copy_image_header_to_core(p_image_src: &CoreImage, p_image_dest: &mut CoreImage) {
*p_image_dest = p_image_src.clone_header_only();
}
#[derive(Clone)]
pub enum ICCProfile {
ICC(Vec<u8>),
CIELab(Vec<u8>),
}
pub const CIE_LAB_BYTE_SIZE: usize = 36;
impl ICCProfile {
pub fn new_icc(data: &[u8]) -> Self {
ICCProfile::ICC(data.to_vec())
}
pub fn new_cielab(data: &[u8]) -> Self {
ICCProfile::CIELab(data.to_vec())
}
}
#[derive(Debug)]
pub enum ICCProfileRef<'a> {
ICC(&'a [u8]),
CIELab(&'a [u8]),
}
impl<'a> ICCProfileRef<'a> {
pub fn as_slice(&self) -> &[u8] {
match self {
ICCProfileRef::ICC(data) => data,
ICCProfileRef::CIELab(data) => data,
}
}
pub fn is_cielab(&self) -> bool {
matches!(self, ICCProfileRef::CIELab(_))
}
pub fn len(&self) -> usize {
match self {
ICCProfileRef::ICC(data) => data.len(),
ICCProfileRef::CIELab(data) => data.len(),
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
#[derive(Debug)]
pub enum ICCProfileMut<'a> {
ICC(&'a mut [u8]),
CIELab(&'a mut [u8]),
}
impl<'a> ICCProfileMut<'a> {
pub fn as_mut_slice(&mut self) -> &mut [u8] {
match self {
ICCProfileMut::ICC(data) => data,
ICCProfileMut::CIELab(data) => data,
}
}
pub fn is_cielab(&self) -> bool {
matches!(self, ICCProfileMut::CIELab(_))
}
}
impl ICCProfile {
pub fn as_slice(&self) -> &[u8] {
match self {
ICCProfile::ICC(data) => data,
ICCProfile::CIELab(data) => data,
}
}
}
#[derive(Clone, Default)]
pub struct CoreImageComp {
pub dx: u32,
pub dy: u32,
pub w: u32,
pub h: u32,
pub x0: u32,
pub y0: u32,
pub prec: u32,
pub bpp: u32,
pub sgnd: u32,
pub resno_decoded: u32,
pub factor: u32,
pub alpha: u16,
pub data: Vec<i32>,
}
#[derive(Clone, Default)]
pub struct CoreImage {
pub x0: u32,
pub y0: u32,
pub x1: u32,
pub y1: u32,
pub numcomps: u32,
pub color_space: crate::api::ColorSpace,
pub comps: Vec<CoreImageComp>,
pub icc_profile_buf: Vec<u8>,
pub icc_profile_is_cielab: bool,
}
impl CoreImageComp {
pub fn data(&self) -> Option<&[i32]> {
Some(&self.data)
}
pub fn data_mut(&mut self) -> Option<&mut [i32]> {
Some(&mut self.data)
}
pub fn alloc_data(&mut self) -> bool {
let len = (self.w as usize).wrapping_mul(self.h as usize);
let mut data = Vec::new();
if data.try_reserve_exact(len).is_err() {
return false;
}
data.resize(len, 0);
self.data = data;
true
}
pub fn clear_data(&mut self) {
self.data.clear();
}
pub fn set_dims(&mut self, w: u32, h: u32) {
if self.w == w && self.h == h {
return;
}
self.w = w;
self.h = h;
self.data.clear();
}
pub fn set_data(&mut self, data: &[i32]) -> bool {
self.data = data.to_vec();
true
}
pub fn copy_props(&mut self, other: &CoreImageComp) {
self.data.clear();
self.dx = other.dx;
self.dy = other.dy;
self.w = other.w;
self.h = other.h;
self.x0 = other.x0;
self.y0 = other.y0;
self.prec = other.prec;
self.bpp = other.bpp;
self.sgnd = other.sgnd;
self.resno_decoded = other.resno_decoded;
self.factor = other.factor;
self.alpha = other.alpha;
}
pub fn copy(&mut self, other: &CoreImageComp) -> bool {
*self = other.clone();
true
}
pub fn clip(&mut self, precision: u32) {
let signed = self.sgnd != 0;
let (min, max) = match (precision, signed) {
(0..=31, false) => (0, (1i64 << precision) - 1),
(1..=31, true) => {
let max = (1i64 << (precision - 1)) - 1;
let min = -max - 1;
(min, max)
}
_ => (0, i64::MAX),
};
for val in self.data.iter_mut() {
*val = (*val as i64).clamp(min, max) as i32;
}
self.prec = precision;
}
pub fn scale(&mut self, precision: u32) {
if self.prec == precision {
return;
}
if !crate::types::prec_valid(precision) || !crate::types::prec_valid(self.prec) {
return;
}
if self.prec < precision {
let signed = self.sgnd != 0;
let old_prec = self.prec;
if signed {
let new_max = 1i64 << (precision - 1);
let old_max = 1i64 << (old_prec - 1);
for val in self.data.iter_mut() {
*val = (((*val as i64) * new_max) / old_max) as i32;
}
} else {
let new_max = (1u64 << precision) - 1;
let old_max = (1u64 << old_prec) - 1;
for val in self.data.iter_mut() {
*val = (((*val as u64) * new_max) / old_max) as i32;
}
}
self.prec = precision;
return;
}
let sgnd = self.sgnd != 0;
let shift = self.prec - precision;
if sgnd {
for val in self.data.iter_mut() {
*val >>= shift;
}
} else {
for val in self.data.iter_mut() {
*val = ((*val as u32) >> shift) as i32;
}
}
self.prec = precision;
}
}
impl CoreImage {
pub fn new() -> CoreImage {
CoreImage::default()
}
pub fn tile_create(
cmptparms: &[CoreImageComp],
clrspc: crate::api::ColorSpace,
) -> Option<CoreImage> {
let mut image = CoreImage {
color_space: clrspc,
..Default::default()
};
if !image.alloc_comps(cmptparms.len() as u32) {
return None;
}
image.comps.clone_from_slice(cmptparms);
Some(image)
}
pub fn comps(&self) -> Option<&[CoreImageComp]> {
Some(&self.comps)
}
pub fn comps_mut(&mut self) -> Option<&mut [CoreImageComp]> {
Some(&mut self.comps)
}
pub fn comps_data_iter(&self) -> Option<impl Iterator<Item = ImageCompRef<'_>>> {
Some(self.comps.iter().map(|comp| ImageCompRef {
comp,
adjust: if comp.sgnd != 0 && crate::types::prec_valid(comp.prec) {
1 << (comp.prec - 1)
} else {
0
},
data: &comp.data,
}))
}
pub fn take_comps(&mut self) -> CoreImage {
let mut image = CoreImage {
x0: self.x0,
y0: self.y0,
x1: self.x1,
y1: self.y1,
numcomps: self.numcomps,
color_space: self.color_space,
comps: core::mem::take(&mut self.comps),
..Default::default()
};
self.numcomps = 0;
image
}
pub fn set_rgb(&mut self, w: usize, h: usize, r: &[i32], g: &[i32], b: &[i32]) -> bool {
let Some(len) = w.checked_mul(h) else {
return false;
};
if r.len() != len || g.len() != len || b.len() != len || self.comps.len() < 3 {
return false;
}
let w = w as u32;
let h = h as u32;
self.comps[0].set_dims(w, h);
self.comps[1].set_dims(w, h);
self.comps[2].set_dims(w, h);
self.comps[1].dx = self.comps[0].dx;
self.comps[2].dx = self.comps[0].dx;
self.comps[1].dy = self.comps[0].dy;
self.comps[2].dy = self.comps[0].dy;
self.comps[0].set_data(r);
self.comps[1].set_data(g);
self.comps[2].set_data(b);
self.color_space = crate::api::ColorSpace::Srgb;
true
}
pub fn comps_data_mut_iter(&mut self) -> Option<impl Iterator<Item = &mut [i32]>> {
Some(self.comps.iter_mut().map(|c| &mut c.data[..]))
}
pub fn take_icc_profile(&mut self) -> Option<ICCProfile> {
if self.icc_profile_buf.is_empty() && !self.icc_profile_is_cielab {
None
} else {
let buf = core::mem::take(&mut self.icc_profile_buf);
let profile = if self.icc_profile_is_cielab {
ICCProfile::new_cielab(&buf)
} else {
ICCProfile::new_icc(&buf)
};
self.icc_profile_is_cielab = false;
Some(profile)
}
}
pub fn clear_icc_profile(&mut self) {
self.icc_profile_buf.clear();
self.icc_profile_is_cielab = false;
}
pub(crate) fn clone_header_only(&self) -> CoreImage {
CoreImage {
x0: self.x0,
y0: self.y0,
x1: self.x1,
y1: self.y1,
numcomps: self.numcomps,
color_space: self.color_space,
comps: self
.comps
.iter()
.map(|c| CoreImageComp {
dx: c.dx,
dy: c.dy,
w: c.w,
h: c.h,
x0: c.x0,
y0: c.y0,
prec: c.prec,
bpp: c.bpp,
sgnd: c.sgnd,
resno_decoded: c.resno_decoded,
factor: c.factor,
alpha: c.alpha,
data: Vec::new(),
})
.collect(),
icc_profile_buf: self.icc_profile_buf.clone(),
icc_profile_is_cielab: self.icc_profile_is_cielab,
}
}
pub fn alloc_comps(&mut self, numcomps: u32) -> bool {
self.comps.clear();
self.numcomps = numcomps;
self
.comps
.resize(numcomps as usize, CoreImageComp::default());
true
}
pub fn comp0_dims_prec(&self) -> (usize, usize, i32) {
if let Some(comp) = self.comps.first() {
return (comp.w as usize, comp.h as usize, comp.prec as i32);
}
(0, 0, 0)
}
pub fn comps_same_dims(&self) -> bool {
if let Some((c0, comps)) = self.comps.split_first() {
for comp in comps {
if comp.w != c0.w || comp.h != c0.h || comp.dx != c0.dx || comp.dy != c0.dy {
return false;
}
}
return true;
}
false
}
pub fn comps_match(&self) -> bool {
if let Some((c0, comps)) = self.comps.split_first() {
for comp in comps {
if comp.w != c0.w
|| comp.h != c0.h
|| comp.dx != c0.dx
|| comp.dy != c0.dy
|| comp.prec != c0.prec
|| comp.sgnd != c0.sgnd
{
return false;
}
}
return true;
}
false
}
}
impl CoreImageComp {
pub fn width(&self) -> u32 {
self.w
}
pub fn height(&self) -> u32 {
self.h
}
pub fn precision(&self) -> u32 {
self.prec
}
pub fn bpp(&self) -> u32 {
self.bpp
}
pub fn is_alpha(&self) -> bool {
self.alpha != 0
}
pub fn is_signed(&self) -> bool {
self.sgnd != 0
}
#[cfg(feature = "image")]
pub fn data_u8(&self) -> impl Iterator<Item = u8> + '_ {
crate::api::image_conv::comp_u8(self)
}
#[cfg(feature = "image")]
pub fn data_u16(&self) -> impl Iterator<Item = u16> + '_ {
crate::api::image_conv::comp_u16(self)
}
}