Big rework: everything now matches the latest spec
This commit is contained in:
parent
6ed5f13487
commit
b0467472ae
4 changed files with 144 additions and 261 deletions
202
src/encode.rs
202
src/encode.rs
|
|
@ -2,8 +2,8 @@ use std::slice;
|
|||
|
||||
use crate::colorspace::ColorSpace;
|
||||
use crate::consts::{
|
||||
QOI_COLOR, QOI_DIFF_16, QOI_DIFF_24, QOI_DIFF_8, QOI_HEADER_SIZE, QOI_INDEX, QOI_PADDING,
|
||||
QOI_PADDING_SIZE, QOI_PIXELS_MAX, QOI_RUN_16, QOI_RUN_8,
|
||||
QOI_HEADER_SIZE, QOI_OP_DIFF, QOI_OP_INDEX, QOI_OP_LUMA, QOI_OP_RGB, QOI_OP_RGBA, QOI_OP_RUN,
|
||||
QOI_PADDING, QOI_PADDING_SIZE, QOI_PIXELS_MAX,
|
||||
};
|
||||
use crate::error::{Error, Result};
|
||||
use crate::header::Header;
|
||||
|
|
@ -23,6 +23,7 @@ impl WriteBuf {
|
|||
#[inline]
|
||||
pub fn write<const N: usize>(&mut self, v: [u8; N]) {
|
||||
unsafe {
|
||||
// TODO: single write via deref?
|
||||
let mut i = 0;
|
||||
while i < N {
|
||||
self.current.add(i).write(v[i]);
|
||||
|
|
@ -46,84 +47,7 @@ impl WriteBuf {
|
|||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn encode_diff_canonical<const N: usize>(
|
||||
px: Pixel<N>, px_prev: Pixel<N>, buf: &mut WriteBuf,
|
||||
) -> Option<(bool, bool, bool, bool)> {
|
||||
let vr = i16::from(px.r()) - i16::from(px_prev.r());
|
||||
let vg = i16::from(px.g()) - i16::from(px_prev.g());
|
||||
let vb = i16::from(px.b()) - i16::from(px_prev.b());
|
||||
let va = i16::from(px.a_or(0)) - i16::from(px_prev.a_or(0));
|
||||
|
||||
let (vr_16, vg_16, vb_16, va_16) = (vr + 16, vg + 16, vb + 16, va + 16);
|
||||
if vr_16 | vg_16 | vb_16 | va_16 | 31 == 31 {
|
||||
loop {
|
||||
if va == 0 {
|
||||
let (vr_2, vg_2, vb_2) = (vr + 2, vg + 2, vb + 2);
|
||||
if vr_2 | vg_2 | vb_2 | 3 == 3 {
|
||||
buf.write([QOI_DIFF_8 | (vr_2 << 4 | vg_2 << 2 | vb_2) as u8]);
|
||||
break;
|
||||
}
|
||||
let (vg_8, vb_8) = (vg + 8, vb + 8);
|
||||
if vg_8 | vb_8 | 15 == 15 {
|
||||
buf.write([QOI_DIFF_16 | vr_16 as u8, (vg_8 << 4 | vb_8) as u8]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
buf.write([
|
||||
QOI_DIFF_24 | (vr_16 >> 1) as u8,
|
||||
(vr_16 << 7 | vg_16 << 2 | vb_16 >> 3) as u8,
|
||||
(vb_16 << 5 | va_16) as u8,
|
||||
]);
|
||||
break;
|
||||
}
|
||||
None
|
||||
} else {
|
||||
Some((vr != 0, vg != 0, vb != 0, va != 0))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn encode_diff_wrapping<const N: usize>(
|
||||
px: Pixel<N>, px_prev: Pixel<N>, buf: &mut WriteBuf,
|
||||
) -> Option<(bool, bool, bool, bool)> {
|
||||
let vr = px.r().wrapping_sub(px_prev.r());
|
||||
let vg = px.g().wrapping_sub(px_prev.g());
|
||||
let vb = px.b().wrapping_sub(px_prev.b());
|
||||
let va = px.a_or(0).wrapping_sub(px_prev.a_or(0));
|
||||
|
||||
let (vr_16, vg_16, vb_16, va_16) =
|
||||
(vr.wrapping_add(16), vg.wrapping_add(16), vb.wrapping_add(16), va.wrapping_add(16));
|
||||
|
||||
if vr_16 | vg_16 | vb_16 | va_16 | 31 == 31 {
|
||||
loop {
|
||||
if va == 0 {
|
||||
let (vr_2, vg_2, vb_2) =
|
||||
(vr.wrapping_add(2), vg.wrapping_add(2), vb.wrapping_add(2));
|
||||
if vr_2 | vg_2 | vb_2 | 3 == 3 {
|
||||
buf.write([QOI_DIFF_8 | vr_2 << 4 | vg_2 << 2 | vb_2]);
|
||||
break;
|
||||
}
|
||||
let (vg_8, vb_8) = (vg.wrapping_add(8), vb.wrapping_add(8));
|
||||
if vg_8 | vb_8 | 15 == 15 {
|
||||
buf.write([QOI_DIFF_16 | vr_16, vg_8 << 4 | vb_8]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
buf.write([
|
||||
QOI_DIFF_24 | vr_16 >> 1,
|
||||
vr_16 << 7 | vg_16 << 2 | vb_16 >> 3,
|
||||
vb_16 << 5 | va_16,
|
||||
]);
|
||||
break;
|
||||
}
|
||||
None
|
||||
} else {
|
||||
Some((vr != 0, vg != 0, vb != 0, va != 0))
|
||||
}
|
||||
}
|
||||
|
||||
fn qoi_encode_impl<const CHANNELS: usize, const CANONICAL: bool>(
|
||||
fn qoi_encode_impl<const CHANNELS: usize>(
|
||||
out: &mut [u8], data: &[u8], width: u32, height: u32, colorspace: ColorSpace,
|
||||
) -> Result<usize>
|
||||
where
|
||||
|
|
@ -159,61 +83,56 @@ where
|
|||
|
||||
let mut index = [Pixel::new(); 64];
|
||||
let mut px_prev = Pixel::new().with_a(0xff);
|
||||
let mut run = 0_u16;
|
||||
|
||||
let next_run = |buf: &mut WriteBuf, run: &mut u16| {
|
||||
let mut r = *run;
|
||||
if r < 33 {
|
||||
r -= 1;
|
||||
buf.push(QOI_RUN_8 | (r as u8));
|
||||
} else {
|
||||
r -= 33;
|
||||
buf.write([QOI_RUN_16 | ((r >> 8) as u8), (r & 0xff) as u8]);
|
||||
}
|
||||
*run = 0;
|
||||
};
|
||||
let mut run = 0_u8;
|
||||
|
||||
for (i, &px) in pixels.iter().enumerate() {
|
||||
if px == px_prev {
|
||||
run += 1;
|
||||
if run == 0x2020 || i == n_pixels - 1 {
|
||||
next_run(&mut buf, &mut run);
|
||||
if run == 62 || unlikely(i == n_pixels - 1) {
|
||||
buf.push(QOI_OP_RUN | (run - 1));
|
||||
run = 0;
|
||||
}
|
||||
} else {
|
||||
if run != 0 {
|
||||
next_run(&mut buf, &mut run);
|
||||
buf.push(QOI_OP_RUN | (run - 1));
|
||||
run = 0;
|
||||
}
|
||||
let index_pos = px.hash_index();
|
||||
let index_px = unsafe {
|
||||
// Safety: hash_index() is computed mod 64, so it will never go out of bounds
|
||||
index.get_unchecked_mut(usize::from(index_pos))
|
||||
};
|
||||
if *index_px == px {
|
||||
buf.push(QOI_INDEX | index_pos);
|
||||
let px4 = px.as_rgba(0xff);
|
||||
if *index_px == px4 {
|
||||
buf.push(QOI_OP_INDEX | index_pos);
|
||||
} else {
|
||||
*index_px = px;
|
||||
*index_px = px4;
|
||||
|
||||
let nonzero = if CANONICAL {
|
||||
encode_diff_canonical::<CHANNELS>(px, px_prev, &mut buf)
|
||||
if px.a_or(0) == px_prev.a_or(0) {
|
||||
let vr = px.r().wrapping_sub(px_prev.r());
|
||||
let vg = px.g().wrapping_sub(px_prev.g());
|
||||
let vb = px.b().wrapping_sub(px_prev.b());
|
||||
|
||||
let vg_r = vr.wrapping_sub(vg);
|
||||
let vg_b = vb.wrapping_sub(vg);
|
||||
|
||||
// TODO maybe add an outer check for vg_32
|
||||
let (vr_2, vg_2, vb_2) =
|
||||
(vr.wrapping_add(2), vg.wrapping_add(2), vb.wrapping_add(2));
|
||||
if vr_2 | vg_2 | vb_2 | 3 == 3 {
|
||||
buf.push(QOI_OP_DIFF | vr_2 << 4 | vg_2 << 2 | vb_2);
|
||||
} else {
|
||||
let (vg_32, vg_r_8, vg_b_8) =
|
||||
(vg.wrapping_add(32), vg_r.wrapping_add(8), vg_b.wrapping_add(8));
|
||||
if vg_r_8 | vg_b_8 | 15 == 15 && vg_32 | 63 == 63 {
|
||||
buf.write([QOI_OP_LUMA | vg_32, vg_r_8 << 4 | vg_b_8]);
|
||||
} else {
|
||||
buf.write([QOI_OP_RGB, px.r(), px.g(), px.b()]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
encode_diff_wrapping::<CHANNELS>(px, px_prev, &mut buf)
|
||||
};
|
||||
|
||||
if let Some((r, g, b, a)) = nonzero {
|
||||
let c = ((r as u8) << 3) | ((g as u8) << 2) | ((b as u8) << 1) | (a as u8);
|
||||
buf.push(QOI_COLOR | c);
|
||||
if r {
|
||||
buf.push(px.r());
|
||||
}
|
||||
if g {
|
||||
buf.push(px.g());
|
||||
}
|
||||
if b {
|
||||
buf.push(px.b());
|
||||
}
|
||||
if a {
|
||||
buf.push(px.a_or(0));
|
||||
}
|
||||
// TODO: or 2 write ops? (QOI_OP_RGBA and px.into_array())
|
||||
buf.write([QOI_OP_RGBA, px.r(), px.g(), px.b(), px.a_or(0xff)]);
|
||||
}
|
||||
}
|
||||
px_prev = px;
|
||||
|
|
@ -225,26 +144,32 @@ where
|
|||
}
|
||||
|
||||
#[inline]
|
||||
pub fn encode_to_buf_impl<const CANONICAL: bool>(
|
||||
out: &mut [u8], data: &[u8], width: u32, height: u32, channels: u8, colorspace: ColorSpace,
|
||||
pub fn qoi_encode_to_buf(
|
||||
mut out: impl AsMut<[u8]>, data: impl AsRef<[u8]>, width: u32, height: u32, channels: u8,
|
||||
colorspace: impl Into<ColorSpace>,
|
||||
) -> Result<usize> {
|
||||
let out = out.as_mut();
|
||||
let data = data.as_ref();
|
||||
let colorspace = colorspace.into();
|
||||
match channels {
|
||||
3 => qoi_encode_impl::<3, CANONICAL>(out, data, width, height, colorspace),
|
||||
4 => qoi_encode_impl::<4, CANONICAL>(out, data, width, height, colorspace),
|
||||
3 => qoi_encode_impl::<3>(out, data, width, height, colorspace),
|
||||
4 => qoi_encode_impl::<4>(out, data, width, height, colorspace),
|
||||
_ => Err(Error::InvalidChannels { channels }),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn encode_to_vec_impl<const CANONICAL: bool>(
|
||||
data: &[u8], width: u32, height: u32, channels: u8, colorspace: ColorSpace,
|
||||
pub fn qoi_encode_to_vec(
|
||||
data: impl AsRef<[u8]>, width: u32, height: u32, channels: u8,
|
||||
colorspace: impl Into<ColorSpace>,
|
||||
) -> Result<Vec<u8>> {
|
||||
let data = data.as_ref();
|
||||
let colorspace = colorspace.into();
|
||||
let mut out = Vec::with_capacity(encode_size_required(width, height, channels));
|
||||
unsafe {
|
||||
out.set_len(out.capacity());
|
||||
}
|
||||
let size =
|
||||
encode_to_buf_impl::<CANONICAL>(&mut out, data, width, height, channels, colorspace)?;
|
||||
let size = qoi_encode_to_buf(&mut out, data, width, height, channels, colorspace)?;
|
||||
out.truncate(size);
|
||||
Ok(out)
|
||||
}
|
||||
|
|
@ -255,26 +180,3 @@ pub fn encode_size_required(width: u32, height: u32, channels: u8) -> usize {
|
|||
let n_pixels = width.saturating_mul(height);
|
||||
QOI_HEADER_SIZE + n_pixels.saturating_mul(usize::from(channels)) + n_pixels + QOI_PADDING_SIZE
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn qoi_encode_to_vec(
|
||||
data: impl AsRef<[u8]>, width: u32, height: u32, channels: u8,
|
||||
colorspace: impl Into<ColorSpace>,
|
||||
) -> Result<Vec<u8>> {
|
||||
encode_to_vec_impl::<false>(data.as_ref(), width, height, channels, colorspace.into())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn qoi_encode_to_buf(
|
||||
mut out: impl AsMut<[u8]>, data: impl AsRef<[u8]>, width: u32, height: u32, channels: u8,
|
||||
colorspace: impl Into<ColorSpace>,
|
||||
) -> Result<usize> {
|
||||
encode_to_buf_impl::<false>(
|
||||
out.as_mut(),
|
||||
data.as_ref(),
|
||||
width,
|
||||
height,
|
||||
channels,
|
||||
colorspace.into(),
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue