2021-11-28 12:36:12 +00:00
|
|
|
use std::slice;
|
|
|
|
|
2021-11-29 22:26:20 +00:00
|
|
|
use crate::colorspace::ColorSpace;
|
2021-11-28 12:36:12 +00:00
|
|
|
use crate::consts::{
|
2021-12-29 19:41:39 +00:00
|
|
|
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,
|
2021-11-28 12:36:12 +00:00
|
|
|
};
|
|
|
|
use crate::error::{Error, Result};
|
|
|
|
use crate::header::Header;
|
|
|
|
use crate::pixel::{Pixel, SupportedChannels};
|
2021-12-01 16:57:20 +00:00
|
|
|
use crate::utils::unlikely;
|
2021-11-28 12:36:12 +00:00
|
|
|
|
|
|
|
struct WriteBuf {
|
|
|
|
start: *const u8,
|
|
|
|
current: *mut u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WriteBuf {
|
2021-12-02 15:55:56 +00:00
|
|
|
pub const unsafe fn new(ptr: *mut u8) -> Self {
|
2021-11-28 12:36:12 +00:00
|
|
|
Self { start: ptr, current: ptr }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn write<const N: usize>(&mut self, v: [u8; N]) {
|
|
|
|
unsafe {
|
2021-12-29 19:41:39 +00:00
|
|
|
// TODO: single write via deref?
|
2021-11-28 12:36:12 +00:00
|
|
|
let mut i = 0;
|
|
|
|
while i < N {
|
|
|
|
self.current.add(i).write(v[i]);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
self.current = self.current.add(N);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn push(&mut self, v: u8) {
|
|
|
|
unsafe {
|
|
|
|
self.current.write(v);
|
|
|
|
self.current = self.current.add(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
unsafe { self.current.offset_from(self.start).max(0) as usize }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 19:41:39 +00:00
|
|
|
fn qoi_encode_impl<const CHANNELS: usize>(
|
2021-11-30 02:46:04 +00:00
|
|
|
out: &mut [u8], data: &[u8], width: u32, height: u32, colorspace: ColorSpace,
|
|
|
|
) -> Result<usize>
|
2021-11-28 12:36:12 +00:00
|
|
|
where
|
2021-11-30 02:46:04 +00:00
|
|
|
Pixel<CHANNELS>: SupportedChannels,
|
2021-11-28 12:36:12 +00:00
|
|
|
{
|
2021-11-30 02:46:04 +00:00
|
|
|
let max_len = encode_size_required(width, height, CHANNELS as u8);
|
2021-12-01 16:57:20 +00:00
|
|
|
if unlikely(out.len() < max_len) {
|
2021-11-30 02:46:04 +00:00
|
|
|
return Err(Error::OutputBufferTooSmall { size: out.len(), required: max_len });
|
|
|
|
}
|
|
|
|
|
2021-11-28 12:36:12 +00:00
|
|
|
let n_pixels = (width as usize) * (height as usize);
|
2021-12-01 16:57:20 +00:00
|
|
|
if unlikely(data.is_empty()) {
|
2021-11-28 12:36:12 +00:00
|
|
|
return Err(Error::EmptyImage { width, height });
|
2021-12-29 13:00:16 +00:00
|
|
|
} else if unlikely(n_pixels > QOI_PIXELS_MAX) {
|
|
|
|
return Err(Error::ImageTooLarge { width, height });
|
2021-12-01 16:57:20 +00:00
|
|
|
} else if unlikely(n_pixels * CHANNELS != data.len()) {
|
2021-11-30 02:46:04 +00:00
|
|
|
return Err(Error::BadEncodingDataSize { size: data.len(), expected: n_pixels * CHANNELS });
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let pixels = unsafe {
|
|
|
|
// Safety: we've verified that n_pixels * N == data.len()
|
2021-12-02 15:55:56 +00:00
|
|
|
slice::from_raw_parts::<Pixel<CHANNELS>>(data.as_ptr().cast(), n_pixels)
|
2021-11-28 12:36:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut buf = unsafe {
|
|
|
|
// Safety: all write ops are guaranteed to not go outside allocation
|
2021-11-30 02:46:04 +00:00
|
|
|
WriteBuf::new(out.as_mut_ptr())
|
2021-11-28 12:36:12 +00:00
|
|
|
};
|
|
|
|
|
2021-12-02 15:55:56 +00:00
|
|
|
let header =
|
|
|
|
Header { width, height, channels: CHANNELS as u8, colorspace, ..Header::default() };
|
2021-11-28 12:36:12 +00:00
|
|
|
buf.write(header.to_bytes());
|
|
|
|
|
|
|
|
let mut index = [Pixel::new(); 64];
|
|
|
|
let mut px_prev = Pixel::new().with_a(0xff);
|
2021-12-29 19:41:39 +00:00
|
|
|
let mut run = 0_u8;
|
2021-11-28 12:36:12 +00:00
|
|
|
|
|
|
|
for (i, &px) in pixels.iter().enumerate() {
|
|
|
|
if px == px_prev {
|
|
|
|
run += 1;
|
2021-12-29 19:41:39 +00:00
|
|
|
if run == 62 || unlikely(i == n_pixels - 1) {
|
|
|
|
buf.push(QOI_OP_RUN | (run - 1));
|
|
|
|
run = 0;
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if run != 0 {
|
2021-12-29 19:41:39 +00:00
|
|
|
buf.push(QOI_OP_RUN | (run - 1));
|
|
|
|
run = 0;
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
|
|
|
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))
|
|
|
|
};
|
2021-12-29 19:41:39 +00:00
|
|
|
let px4 = px.as_rgba(0xff);
|
|
|
|
if *index_px == px4 {
|
|
|
|
buf.push(QOI_OP_INDEX | index_pos);
|
2021-11-28 12:36:12 +00:00
|
|
|
} else {
|
2021-12-29 19:41:39 +00:00
|
|
|
*index_px = px4;
|
|
|
|
|
|
|
|
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()]);
|
|
|
|
}
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
2021-12-29 19:41:39 +00:00
|
|
|
} else {
|
|
|
|
// 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)]);
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
px_prev = px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 13:07:54 +00:00
|
|
|
buf.write(QOI_PADDING);
|
2021-11-30 02:46:04 +00:00
|
|
|
Ok(buf.len())
|
|
|
|
}
|
2021-11-28 12:36:12 +00:00
|
|
|
|
2021-12-01 16:57:20 +00:00
|
|
|
#[inline]
|
2021-12-29 19:41:39 +00:00
|
|
|
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>,
|
2021-11-30 02:46:04 +00:00
|
|
|
) -> Result<usize> {
|
2021-12-29 19:41:39 +00:00
|
|
|
let out = out.as_mut();
|
|
|
|
let data = data.as_ref();
|
|
|
|
let colorspace = colorspace.into();
|
2021-11-30 02:46:04 +00:00
|
|
|
match channels {
|
2021-12-29 19:41:39 +00:00
|
|
|
3 => qoi_encode_impl::<3>(out, data, width, height, colorspace),
|
|
|
|
4 => qoi_encode_impl::<4>(out, data, width, height, colorspace),
|
2021-11-30 02:46:04 +00:00
|
|
|
_ => Err(Error::InvalidChannels { channels }),
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 16:57:20 +00:00
|
|
|
#[inline]
|
2021-12-29 19:41:39 +00:00
|
|
|
pub fn qoi_encode_to_vec(
|
|
|
|
data: impl AsRef<[u8]>, width: u32, height: u32, channels: u8,
|
|
|
|
colorspace: impl Into<ColorSpace>,
|
2021-11-28 12:36:12 +00:00
|
|
|
) -> Result<Vec<u8>> {
|
2021-12-29 19:41:39 +00:00
|
|
|
let data = data.as_ref();
|
|
|
|
let colorspace = colorspace.into();
|
2021-11-30 02:46:04 +00:00
|
|
|
let mut out = Vec::with_capacity(encode_size_required(width, height, channels));
|
|
|
|
unsafe {
|
|
|
|
out.set_len(out.capacity());
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
2021-12-29 19:41:39 +00:00
|
|
|
let size = qoi_encode_to_buf(&mut out, data, width, height, channels, colorspace)?;
|
2021-11-30 02:46:04 +00:00
|
|
|
out.truncate(size);
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
|
2021-12-01 16:57:20 +00:00
|
|
|
#[inline]
|
2021-11-30 02:46:04 +00:00
|
|
|
pub fn encode_size_required(width: u32, height: u32, channels: u8) -> usize {
|
|
|
|
let (width, height) = (width as usize, height as usize);
|
|
|
|
let n_pixels = width.saturating_mul(height);
|
2021-12-29 13:07:54 +00:00
|
|
|
QOI_HEADER_SIZE + n_pixels.saturating_mul(usize::from(channels)) + n_pixels + QOI_PADDING_SIZE
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|