2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
|
|
|
use alloc::{vec, vec::Vec};
|
|
|
|
use core::convert::TryFrom;
|
|
|
|
#[cfg(feature = "std")]
|
2022-01-02 22:11:01 +00:00
|
|
|
use std::io::Write;
|
|
|
|
|
2022-01-02 21:47:13 +00:00
|
|
|
use crate::consts::{QOI_HEADER_SIZE, QOI_OP_INDEX, QOI_OP_RUN, QOI_PADDING, QOI_PADDING_SIZE};
|
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, ColorSpace};
|
2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
use crate::utils::GenericWriter;
|
|
|
|
use crate::utils::{cold, unlikely, BytesMut, Writer};
|
2021-11-28 12:36:12 +00:00
|
|
|
|
2022-01-03 12:56:03 +00:00
|
|
|
#[allow(clippy::cast_possible_truncation, unused_assignments)]
|
2022-01-02 22:11:01 +00:00
|
|
|
fn qoi_encode_impl<W: Writer, const N: usize>(mut buf: W, data: &[u8]) -> Result<usize>
|
2021-11-28 12:36:12 +00:00
|
|
|
where
|
2022-01-02 11:02:44 +00:00
|
|
|
Pixel<N>: SupportedChannels,
|
2021-11-28 12:36:12 +00:00
|
|
|
{
|
2022-01-02 22:11:01 +00:00
|
|
|
let cap = buf.capacity();
|
2021-11-28 12:36:12 +00:00
|
|
|
|
2021-12-30 15:01:16 +00:00
|
|
|
let mut index = [Pixel::new(); 256];
|
2021-11-28 12:36:12 +00:00
|
|
|
let mut px_prev = Pixel::new().with_a(0xff);
|
2022-01-03 12:56:03 +00:00
|
|
|
let mut hash_prev = Pixel::<N>::new().with_a(0xff).hash_index();
|
2021-12-29 19:41:39 +00:00
|
|
|
let mut run = 0_u8;
|
2022-01-02 11:02:44 +00:00
|
|
|
let mut px = Pixel::<N>::new().with_a(0xff);
|
2021-11-28 12:36:12 +00:00
|
|
|
|
2022-01-02 21:47:13 +00:00
|
|
|
let n_pixels = data.len() / N;
|
|
|
|
|
2022-01-02 11:02:44 +00:00
|
|
|
for (i, chunk) in data.chunks_exact(N).enumerate() {
|
2021-12-30 15:01:16 +00:00
|
|
|
px.read(chunk);
|
2021-11-28 12:36:12 +00:00
|
|
|
if px == px_prev {
|
|
|
|
run += 1;
|
2021-12-29 19:41:39 +00:00
|
|
|
if run == 62 || unlikely(i == n_pixels - 1) {
|
2022-01-02 22:11:01 +00:00
|
|
|
buf = buf.write_one(QOI_OP_RUN | (run - 1))?;
|
2021-12-29 19:41:39 +00:00
|
|
|
run = 0;
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if run != 0 {
|
2022-01-03 12:56:03 +00:00
|
|
|
#[cfg(not(feature = "reference"))]
|
|
|
|
{
|
2022-01-03 14:11:39 +00:00
|
|
|
buf = buf.write_one(if run == 1 && i != 1 {
|
2022-01-03 12:56:03 +00:00
|
|
|
QOI_OP_INDEX | (hash_prev as u8)
|
|
|
|
} else {
|
|
|
|
QOI_OP_RUN | (run - 1)
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
#[cfg(feature = "reference")]
|
|
|
|
{
|
|
|
|
buf = buf.write_one(QOI_OP_RUN | (run - 1))?;
|
|
|
|
}
|
2021-12-29 19:41:39 +00:00
|
|
|
run = 0;
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
2022-01-03 12:56:03 +00:00
|
|
|
hash_prev = px.hash_index();
|
|
|
|
let index_px = &mut index[hash_prev as usize];
|
2021-12-30 15:01:16 +00:00
|
|
|
let px_rgba = px.as_rgba(0xff);
|
|
|
|
if *index_px == px_rgba {
|
2022-01-03 12:56:03 +00:00
|
|
|
buf = buf.write_one(QOI_OP_INDEX | hash_prev)?;
|
2021-11-28 12:36:12 +00:00
|
|
|
} else {
|
2021-12-30 15:01:16 +00:00
|
|
|
*index_px = px_rgba;
|
2022-01-02 22:11:01 +00:00
|
|
|
buf = px.encode_into(px_prev, buf)?;
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
|
|
|
px_prev = px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 22:11:01 +00:00
|
|
|
buf = buf.write_many(&QOI_PADDING)?;
|
|
|
|
Ok(cap.saturating_sub(buf.capacity()))
|
2021-11-30 02:46:04 +00:00
|
|
|
}
|
2021-11-28 12:36:12 +00:00
|
|
|
|
2021-12-01 16:57:20 +00:00
|
|
|
#[inline]
|
2022-01-02 22:11:01 +00:00
|
|
|
fn qoi_encode_impl_all<W: Writer>(out: W, data: &[u8], channels: Channels) -> Result<usize> {
|
2021-11-30 02:46:04 +00:00
|
|
|
match channels {
|
2022-01-02 22:11:01 +00:00
|
|
|
Channels::Rgb => qoi_encode_impl::<_, 3>(out, data),
|
|
|
|
Channels::Rgba => qoi_encode_impl::<_, 4>(out, data),
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 16:57:20 +00:00
|
|
|
#[inline]
|
2022-01-02 21:47:13 +00:00
|
|
|
pub fn encoded_size_limit(width: u32, height: u32, channels: impl Into<u8>) -> usize {
|
|
|
|
let (width, height) = (width as usize, height as usize);
|
|
|
|
let n_pixels = width.saturating_mul(height);
|
|
|
|
QOI_HEADER_SIZE
|
|
|
|
+ n_pixels.saturating_mul(channels.into() as usize)
|
|
|
|
+ n_pixels
|
|
|
|
+ QOI_PADDING_SIZE
|
2021-11-30 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 16:57:20 +00:00
|
|
|
#[inline]
|
2022-01-02 21:47:13 +00:00
|
|
|
pub fn qoi_encode_to_buf(
|
|
|
|
buf: impl AsMut<[u8]>, data: impl AsRef<[u8]>, width: u32, height: u32,
|
|
|
|
) -> Result<usize> {
|
|
|
|
QoiEncoder::new(&data, width, height)?.encode_to_buf(buf)
|
|
|
|
}
|
|
|
|
|
2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(any(feature = "alloc", feature = "std"))]
|
2022-01-02 21:47:13 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn qoi_encode_to_vec(data: impl AsRef<[u8]>, width: u32, height: u32) -> Result<Vec<u8>> {
|
|
|
|
QoiEncoder::new(&data, width, height)?.encode_to_vec()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct QoiEncoder<'a> {
|
|
|
|
data: &'a [u8],
|
|
|
|
header: Header,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> QoiEncoder<'a> {
|
|
|
|
#[inline]
|
2022-01-03 11:30:37 +00:00
|
|
|
#[allow(clippy::cast_possible_truncation)]
|
2022-01-02 21:47:13 +00:00
|
|
|
pub fn new(data: &'a (impl AsRef<[u8]> + ?Sized), width: u32, height: u32) -> Result<Self> {
|
|
|
|
let data = data.as_ref();
|
|
|
|
let mut header =
|
|
|
|
Header::try_new(width, height, Channels::default(), ColorSpace::default())?;
|
|
|
|
let size = data.len();
|
|
|
|
let n_channels = size / header.n_pixels();
|
|
|
|
if header.n_pixels() * n_channels != size {
|
|
|
|
return Err(Error::InvalidImageLength { size, width, height });
|
|
|
|
}
|
|
|
|
header.channels = Channels::try_from(n_channels.min(0xff) as u8)?;
|
|
|
|
Ok(Self { data, header })
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub const fn with_colorspace(mut self, colorspace: ColorSpace) -> Self {
|
|
|
|
self.header = self.header.with_colorspace(colorspace);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub const fn channels(&self) -> Channels {
|
|
|
|
self.header.channels
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub const fn header(&self) -> &Header {
|
|
|
|
&self.header
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn encoded_size_limit(&self) -> usize {
|
|
|
|
self.header.encoded_size_limit()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn encode_to_buf(&self, mut buf: impl AsMut<[u8]>) -> Result<usize> {
|
|
|
|
let buf = buf.as_mut();
|
|
|
|
let size_required = self.encoded_size_limit();
|
|
|
|
if unlikely(buf.len() < size_required) {
|
|
|
|
return Err(Error::OutputBufferTooSmall { size: buf.len(), required: size_required });
|
|
|
|
}
|
|
|
|
let (head, tail) = buf.split_at_mut(QOI_HEADER_SIZE); // can't panic
|
|
|
|
head.copy_from_slice(&self.header.encode());
|
2022-01-02 22:11:01 +00:00
|
|
|
let n_written = qoi_encode_impl_all(BytesMut::new(tail), self.data, self.header.channels)?;
|
2022-01-02 21:47:13 +00:00
|
|
|
Ok(QOI_HEADER_SIZE + n_written)
|
|
|
|
}
|
|
|
|
|
2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(any(feature = "alloc", feature = "std"))]
|
2022-01-02 21:47:13 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn encode_to_vec(&self) -> Result<Vec<u8>> {
|
|
|
|
let mut out = vec![0_u8; self.encoded_size_limit()];
|
|
|
|
let size = self.encode_to_buf(&mut out)?;
|
|
|
|
out.truncate(size);
|
|
|
|
Ok(out)
|
|
|
|
}
|
2022-01-02 22:11:01 +00:00
|
|
|
|
2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(feature = "std")]
|
2022-01-02 22:11:01 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn encode_to_stream<W: Write>(&self, writer: &mut W) -> Result<usize> {
|
|
|
|
writer.write_all(&self.header.encode())?;
|
|
|
|
let n_written =
|
|
|
|
qoi_encode_impl_all(GenericWriter::new(writer), self.data, self.header.channels)?;
|
|
|
|
Ok(n_written + QOI_HEADER_SIZE)
|
|
|
|
}
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|