2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
|
|
|
use alloc::{vec, vec::Vec};
|
|
|
|
#[cfg(feature = "std")]
|
2022-01-02 15:59:25 +00:00
|
|
|
use std::io::Read;
|
2022-01-01 21:03:47 +00:00
|
|
|
|
2021-12-30 22:44:59 +00:00
|
|
|
// TODO: can be removed once https://github.com/rust-lang/rust/issues/74985 is stable
|
2022-01-03 10:04:22 +00:00
|
|
|
use bytemuck::{cast_slice_mut, Pod};
|
2021-11-28 12:36:12 +00:00
|
|
|
|
2021-12-29 19:41:39 +00:00
|
|
|
use crate::consts::{
|
|
|
|
QOI_HEADER_SIZE, QOI_OP_DIFF, QOI_OP_INDEX, QOI_OP_LUMA, QOI_OP_RGB, QOI_OP_RGBA, QOI_OP_RUN,
|
2022-01-01 21:02:08 +00:00
|
|
|
QOI_PADDING, QOI_PADDING_SIZE,
|
2021-12-29 19:41:39 +00:00
|
|
|
};
|
2021-11-28 12:36:12 +00:00
|
|
|
use crate::error::{Error, Result};
|
|
|
|
use crate::header::Header;
|
|
|
|
use crate::pixel::{Pixel, SupportedChannels};
|
2022-01-02 21:47:13 +00:00
|
|
|
use crate::types::Channels;
|
2021-12-30 22:44:59 +00:00
|
|
|
use crate::utils::{cold, unlikely};
|
2021-11-28 12:36:12 +00:00
|
|
|
|
2021-12-31 10:37:56 +00:00
|
|
|
const QOI_OP_INDEX_END: u8 = QOI_OP_INDEX | 0x3f;
|
|
|
|
const QOI_OP_RUN_END: u8 = QOI_OP_RUN | 0x3d; // <- note, 0x3d (not 0x3f)
|
|
|
|
const QOI_OP_DIFF_END: u8 = QOI_OP_DIFF | 0x3f;
|
|
|
|
const QOI_OP_LUMA_END: u8 = QOI_OP_LUMA | 0x3f;
|
|
|
|
|
2022-01-01 18:58:18 +00:00
|
|
|
#[inline]
|
|
|
|
fn qoi_decode_impl_slice<const N: usize, const RGBA: bool>(
|
|
|
|
data: &[u8], out: &mut [u8],
|
2022-01-01 21:02:08 +00:00
|
|
|
) -> Result<usize>
|
2021-11-28 12:36:12 +00:00
|
|
|
where
|
|
|
|
Pixel<N>: SupportedChannels,
|
2021-12-30 22:44:59 +00:00
|
|
|
[u8; N]: Pod,
|
2021-11-28 12:36:12 +00:00
|
|
|
{
|
2022-01-01 18:58:18 +00:00
|
|
|
let mut pixels = cast_slice_mut::<_, [u8; N]>(out);
|
2022-01-01 21:02:08 +00:00
|
|
|
let data_len = data.len();
|
2022-01-01 18:58:18 +00:00
|
|
|
let mut data = data;
|
2021-11-28 12:36:12 +00:00
|
|
|
|
2022-01-02 16:14:12 +00:00
|
|
|
let mut index = [Pixel::<N>::new(); 256];
|
|
|
|
let mut px = Pixel::<N>::new().with_a(0xff);
|
2021-12-30 22:44:59 +00:00
|
|
|
|
2021-12-31 10:37:56 +00:00
|
|
|
while let [px_out, ptail @ ..] = pixels {
|
|
|
|
pixels = ptail;
|
|
|
|
match data {
|
|
|
|
[b1 @ QOI_OP_INDEX..=QOI_OP_INDEX_END, dtail @ ..] => {
|
2022-01-01 18:58:18 +00:00
|
|
|
px = index[*b1 as usize];
|
2022-01-02 16:14:12 +00:00
|
|
|
*px_out = px.into();
|
2021-12-31 10:37:56 +00:00
|
|
|
data = dtail;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
[QOI_OP_RGB, r, g, b, dtail @ ..] => {
|
2022-01-02 16:14:12 +00:00
|
|
|
px.update_rgb(*r, *g, *b);
|
2021-12-31 10:37:56 +00:00
|
|
|
data = dtail;
|
|
|
|
}
|
|
|
|
[QOI_OP_RGBA, r, g, b, a, dtail @ ..] if RGBA => {
|
2022-01-02 16:14:12 +00:00
|
|
|
px.update_rgba(*r, *g, *b, *a);
|
2021-12-31 10:37:56 +00:00
|
|
|
data = dtail;
|
|
|
|
}
|
|
|
|
[b1 @ QOI_OP_RUN..=QOI_OP_RUN_END, dtail @ ..] => {
|
2022-01-02 16:14:12 +00:00
|
|
|
*px_out = px.into();
|
2022-01-01 18:58:18 +00:00
|
|
|
let run = ((b1 & 0x3f) as usize).min(pixels.len());
|
2021-12-31 10:37:56 +00:00
|
|
|
let (phead, ptail) = pixels.split_at_mut(run); // can't panic
|
2022-01-02 16:14:12 +00:00
|
|
|
phead.fill(px.into());
|
2021-12-31 10:37:56 +00:00
|
|
|
pixels = ptail;
|
|
|
|
data = dtail;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
[b1 @ QOI_OP_DIFF..=QOI_OP_DIFF_END, dtail @ ..] => {
|
2022-01-02 16:14:12 +00:00
|
|
|
px.update_diff(*b1);
|
2021-12-31 10:37:56 +00:00
|
|
|
data = dtail;
|
|
|
|
}
|
|
|
|
[b1 @ QOI_OP_LUMA..=QOI_OP_LUMA_END, b2, dtail @ ..] => {
|
2022-01-02 16:14:12 +00:00
|
|
|
px.update_luma(*b1, *b2);
|
2021-12-31 10:37:56 +00:00
|
|
|
data = dtail;
|
2021-12-30 22:44:59 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
cold();
|
2022-01-01 18:58:18 +00:00
|
|
|
if unlikely(data.len() < QOI_PADDING_SIZE) {
|
2022-01-03 18:12:06 +00:00
|
|
|
return Err(Error::UnexpectedBufferEnd);
|
2021-12-31 10:37:56 +00:00
|
|
|
}
|
2021-11-29 11:30:09 +00:00
|
|
|
}
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
2022-01-01 18:58:18 +00:00
|
|
|
|
2022-01-02 16:14:12 +00:00
|
|
|
index[px.hash_index() as usize] = px;
|
|
|
|
*px_out = px.into();
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-01 21:02:08 +00:00
|
|
|
if unlikely(data.len() < QOI_PADDING_SIZE) {
|
|
|
|
return Err(Error::UnexpectedBufferEnd);
|
2022-01-03 11:30:37 +00:00
|
|
|
} else if unlikely(data[..QOI_PADDING_SIZE] != QOI_PADDING) {
|
2022-01-01 21:02:08 +00:00
|
|
|
return Err(Error::InvalidPadding);
|
2021-12-01 17:01:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-01 21:02:08 +00:00
|
|
|
Ok(data_len.saturating_sub(data.len()).saturating_sub(QOI_PADDING_SIZE))
|
2021-12-01 17:01:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-01 18:58:18 +00:00
|
|
|
#[inline]
|
|
|
|
fn qoi_decode_impl_slice_all(
|
|
|
|
data: &[u8], out: &mut [u8], channels: u8, src_channels: u8,
|
2022-01-01 21:02:08 +00:00
|
|
|
) -> Result<usize> {
|
2022-01-01 18:58:18 +00:00
|
|
|
match (channels, src_channels) {
|
|
|
|
(3, 3) => qoi_decode_impl_slice::<3, false>(data, out),
|
|
|
|
(3, 4) => qoi_decode_impl_slice::<3, true>(data, out),
|
|
|
|
(4, 3) => qoi_decode_impl_slice::<4, false>(data, out),
|
|
|
|
(4, 4) => qoi_decode_impl_slice::<4, true>(data, out),
|
|
|
|
_ => {
|
|
|
|
cold();
|
2022-01-01 21:02:55 +00:00
|
|
|
Err(Error::InvalidChannels { channels })
|
2022-01-01 18:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-01-01 21:02:08 +00:00
|
|
|
pub fn qoi_decode_to_buf(buf: impl AsMut<[u8]>, data: impl AsRef<[u8]>) -> Result<Header> {
|
|
|
|
let mut decoder = QoiDecoder::new(&data)?;
|
|
|
|
decoder.decode_to_buf(buf)?;
|
|
|
|
Ok(*decoder.header())
|
2022-01-01 18:58:18 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
2021-12-01 17:01:41 +00:00
|
|
|
#[inline]
|
2022-01-01 21:02:08 +00:00
|
|
|
pub fn qoi_decode_to_vec(data: impl AsRef<[u8]>) -> Result<(Header, Vec<u8>)> {
|
|
|
|
let mut decoder = QoiDecoder::new(&data)?;
|
|
|
|
let out = decoder.decode_to_vec()?;
|
|
|
|
Ok((*decoder.header(), out))
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
2021-12-01 16:01:58 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn qoi_decode_header(data: impl AsRef<[u8]>) -> Result<Header> {
|
2021-12-31 10:34:27 +00:00
|
|
|
Header::decode(data)
|
2021-12-01 16:01:58 +00:00
|
|
|
}
|
2022-01-01 21:02:08 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct QoiDecoder<'a> {
|
|
|
|
data: &'a [u8],
|
|
|
|
header: Header,
|
2022-01-02 21:47:13 +00:00
|
|
|
channels: Channels,
|
2022-01-01 21:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> QoiDecoder<'a> {
|
|
|
|
#[inline]
|
|
|
|
pub fn new(data: &'a (impl AsRef<[u8]> + ?Sized)) -> Result<Self> {
|
|
|
|
let data = data.as_ref();
|
|
|
|
let header = Header::decode(data)?;
|
|
|
|
let data = &data[QOI_HEADER_SIZE..]; // can't panic
|
|
|
|
Ok(Self { data, header, channels: header.channels })
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-01-02 21:47:13 +00:00
|
|
|
pub const fn with_channels(mut self, channels: Channels) -> Self {
|
2022-01-01 21:02:08 +00:00
|
|
|
self.channels = channels;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-01-02 21:47:13 +00:00
|
|
|
pub const fn channels(&self) -> Channels {
|
2022-01-01 21:02:08 +00:00
|
|
|
self.channels
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-01-01 21:02:55 +00:00
|
|
|
pub const fn header(&self) -> &Header {
|
2022-01-01 21:02:08 +00:00
|
|
|
&self.header
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-01-01 21:02:55 +00:00
|
|
|
pub const fn data(self) -> &'a [u8] {
|
2022-01-01 21:02:08 +00:00
|
|
|
self.data
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-01-01 21:02:55 +00:00
|
|
|
pub fn decode_to_buf(&mut self, mut buf: impl AsMut<[u8]>) -> Result<usize> {
|
2022-01-01 21:02:08 +00:00
|
|
|
let buf = buf.as_mut();
|
2022-01-02 21:47:13 +00:00
|
|
|
let size = self.header.n_pixels() * self.channels.as_u8() as usize;
|
2022-01-01 21:02:08 +00:00
|
|
|
if unlikely(buf.len() < size) {
|
|
|
|
return Err(Error::OutputBufferTooSmall { size: buf.len(), required: size });
|
|
|
|
}
|
2022-01-02 21:47:13 +00:00
|
|
|
let n_read = qoi_decode_impl_slice_all(
|
|
|
|
self.data,
|
|
|
|
buf,
|
|
|
|
self.channels.as_u8(),
|
|
|
|
self.header.channels.as_u8(),
|
|
|
|
)?;
|
2022-01-01 21:02:08 +00:00
|
|
|
self.data = &self.data[n_read..]; // can't panic
|
2022-01-01 21:02:55 +00:00
|
|
|
Ok(size)
|
2022-01-01 21:02:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
2022-01-01 21:02:08 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn decode_to_vec(&mut self) -> Result<Vec<u8>> {
|
2022-01-02 21:47:13 +00:00
|
|
|
let mut out = vec![0; self.header.n_pixels() * self.channels.as_u8() as usize];
|
2022-01-01 21:02:08 +00:00
|
|
|
self.decode_to_buf(&mut out).map(|_| out)
|
|
|
|
}
|
2022-01-01 21:03:47 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(any(feature = "std"))]
|
2022-01-01 21:03:47 +00:00
|
|
|
#[inline]
|
2022-01-02 15:59:25 +00:00
|
|
|
fn qoi_decode_impl_stream<R: Read, const N: usize, const RGBA: bool>(
|
|
|
|
data: &mut R, out: &mut [u8],
|
2022-01-01 21:03:47 +00:00
|
|
|
) -> Result<()>
|
|
|
|
where
|
|
|
|
Pixel<N>: SupportedChannels,
|
|
|
|
[u8; N]: Pod,
|
|
|
|
{
|
2022-01-02 15:59:25 +00:00
|
|
|
let mut pixels = cast_slice_mut::<_, [u8; N]>(out);
|
|
|
|
|
2022-01-02 16:14:12 +00:00
|
|
|
let mut index = [Pixel::<N>::new(); 256];
|
|
|
|
let mut px = Pixel::<N>::new().with_a(0xff);
|
2022-01-01 21:03:47 +00:00
|
|
|
|
2022-01-02 15:59:25 +00:00
|
|
|
while let [px_out, ptail @ ..] = pixels {
|
|
|
|
pixels = ptail;
|
2022-01-01 21:03:47 +00:00
|
|
|
let mut p = [0];
|
|
|
|
data.read_exact(&mut p)?;
|
|
|
|
let [b1] = p;
|
|
|
|
match b1 {
|
|
|
|
QOI_OP_INDEX..=QOI_OP_INDEX_END => {
|
|
|
|
px = index[b1 as usize];
|
2022-01-02 16:14:12 +00:00
|
|
|
*px_out = px.into();
|
2022-01-01 21:03:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QOI_OP_RGB => {
|
|
|
|
let mut p = [0; 3];
|
|
|
|
data.read_exact(&mut p)?;
|
2022-01-02 16:14:12 +00:00
|
|
|
px.update_rgb(p[0], p[1], p[2]);
|
2022-01-01 21:03:47 +00:00
|
|
|
}
|
|
|
|
QOI_OP_RGBA if RGBA => {
|
|
|
|
let mut p = [0; 4];
|
|
|
|
data.read_exact(&mut p)?;
|
2022-01-02 16:14:12 +00:00
|
|
|
px.update_rgba(p[0], p[1], p[2], p[3]);
|
2022-01-01 21:03:47 +00:00
|
|
|
}
|
|
|
|
QOI_OP_RUN..=QOI_OP_RUN_END => {
|
2022-01-02 16:14:12 +00:00
|
|
|
*px_out = px.into();
|
2022-01-02 15:59:25 +00:00
|
|
|
let run = ((b1 & 0x3f) as usize).min(pixels.len());
|
|
|
|
let (phead, ptail) = pixels.split_at_mut(run); // can't panic
|
2022-01-02 16:14:12 +00:00
|
|
|
phead.fill(px.into());
|
2022-01-02 15:59:25 +00:00
|
|
|
pixels = ptail;
|
2022-01-01 21:03:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QOI_OP_DIFF..=QOI_OP_DIFF_END => {
|
2022-01-02 16:14:12 +00:00
|
|
|
px.update_diff(b1);
|
2022-01-01 21:03:47 +00:00
|
|
|
}
|
|
|
|
QOI_OP_LUMA..=QOI_OP_LUMA_END => {
|
|
|
|
let mut p = [0];
|
|
|
|
data.read_exact(&mut p)?;
|
|
|
|
let [b2] = p;
|
2022-01-02 16:14:12 +00:00
|
|
|
px.update_luma(b1, b2);
|
2022-01-01 21:03:47 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
cold();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 16:14:12 +00:00
|
|
|
index[px.hash_index() as usize] = px;
|
|
|
|
*px_out = px.into();
|
2022-01-01 21:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut p = [0_u8; QOI_PADDING_SIZE];
|
|
|
|
data.read_exact(&mut p)?;
|
|
|
|
if unlikely(p != QOI_PADDING) {
|
|
|
|
return Err(Error::InvalidPadding);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(feature = "std")]
|
2022-01-01 21:03:47 +00:00
|
|
|
#[inline]
|
2022-01-02 15:59:25 +00:00
|
|
|
fn qoi_decode_impl_stream_all<R: Read>(
|
2022-01-02 16:14:12 +00:00
|
|
|
data: &mut R, out: &mut [u8], channels: u8, src_channels: u8,
|
2022-01-01 21:03:47 +00:00
|
|
|
) -> Result<()> {
|
|
|
|
match (channels, src_channels) {
|
2022-01-02 15:59:25 +00:00
|
|
|
(3, 3) => qoi_decode_impl_stream::<_, 3, false>(data, out),
|
|
|
|
(3, 4) => qoi_decode_impl_stream::<_, 3, true>(data, out),
|
|
|
|
(4, 3) => qoi_decode_impl_stream::<_, 4, false>(data, out),
|
|
|
|
(4, 4) => qoi_decode_impl_stream::<_, 4, true>(data, out),
|
2022-01-01 21:03:47 +00:00
|
|
|
_ => {
|
|
|
|
cold();
|
|
|
|
Err(Error::InvalidChannels { channels })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(feature = "std")]
|
2022-01-01 21:03:47 +00:00
|
|
|
pub struct QoiStreamDecoder<R> {
|
|
|
|
reader: R,
|
|
|
|
header: Header,
|
2022-01-02 21:47:13 +00:00
|
|
|
channels: Channels,
|
2022-01-01 21:03:47 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(feature = "std")]
|
2022-01-01 21:03:47 +00:00
|
|
|
impl<R: Read> QoiStreamDecoder<R> {
|
|
|
|
#[inline]
|
|
|
|
pub fn new(mut reader: R) -> Result<Self> {
|
|
|
|
let mut b = [0; QOI_HEADER_SIZE];
|
|
|
|
reader.read_exact(&mut b)?;
|
|
|
|
let header = Header::decode(b)?;
|
|
|
|
Ok(Self { reader, header, channels: header.channels })
|
|
|
|
}
|
|
|
|
|
2022-01-02 21:47:13 +00:00
|
|
|
pub fn with_channels(mut self, channels: Channels) -> Self {
|
2022-01-01 21:03:47 +00:00
|
|
|
self.channels = channels;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-01-02 21:47:13 +00:00
|
|
|
pub fn channels(&self) -> Channels {
|
2022-01-01 21:03:47 +00:00
|
|
|
self.channels
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn header(&self) -> &Header {
|
|
|
|
&self.header
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn reader(&self) -> &R {
|
|
|
|
&self.reader
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn into_reader(self) -> R {
|
|
|
|
self.reader
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn decode_to_buf(&mut self, mut buf: impl AsMut<[u8]>) -> Result<usize> {
|
2022-01-02 16:14:12 +00:00
|
|
|
let buf = buf.as_mut();
|
2022-01-02 21:47:13 +00:00
|
|
|
let size = self.header.n_pixels() * self.channels.as_u8() as usize;
|
2022-01-01 21:03:47 +00:00
|
|
|
if unlikely(buf.len() < size) {
|
|
|
|
return Err(Error::OutputBufferTooSmall { size: buf.len(), required: size });
|
|
|
|
}
|
2022-01-02 21:47:13 +00:00
|
|
|
qoi_decode_impl_stream_all(
|
|
|
|
&mut self.reader,
|
|
|
|
buf,
|
|
|
|
self.channels.as_u8(),
|
|
|
|
self.header.channels.as_u8(),
|
|
|
|
)?;
|
2022-01-02 15:59:25 +00:00
|
|
|
Ok(size)
|
2022-01-01 21:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn decode_to_vec(&mut self) -> Result<Vec<u8>> {
|
2022-01-02 21:47:13 +00:00
|
|
|
let mut out = vec![0; self.header.n_pixels() * self.channels.as_u8() as usize];
|
2022-01-02 15:59:25 +00:00
|
|
|
let _ = self.decode_to_buf(&mut out)?;
|
2022-01-01 21:03:47 +00:00
|
|
|
Ok(out)
|
|
|
|
}
|
2022-01-01 21:02:08 +00:00
|
|
|
}
|