2021-12-31 10:34:27 +00:00
|
|
|
use std::convert::TryInto;
|
|
|
|
|
2021-11-29 22:26:20 +00:00
|
|
|
use crate::colorspace::ColorSpace;
|
2021-11-28 12:36:12 +00:00
|
|
|
use crate::consts::{
|
2022-01-02 17:16:05 +00:00
|
|
|
QOI_HEADER_SIZE, QOI_OP_INDEX, 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};
|
2022-01-02 17:16:05 +00:00
|
|
|
use crate::utils::{unlikely, BytesMut};
|
2021-11-28 12:36:12 +00:00
|
|
|
|
2021-12-31 10:37:56 +00:00
|
|
|
#[allow(clippy::cast_possible_truncation)]
|
2022-01-02 11:02:44 +00:00
|
|
|
fn qoi_encode_impl<const N: 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
|
2022-01-02 11:02:44 +00:00
|
|
|
Pixel<N>: SupportedChannels,
|
2021-11-28 12:36:12 +00:00
|
|
|
{
|
2022-01-02 11:02:44 +00:00
|
|
|
let max_len = encode_size_required(width, height, N 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 });
|
2022-01-02 11:02:44 +00:00
|
|
|
} else if unlikely(n_pixels * N != data.len()) {
|
|
|
|
return Err(Error::BadEncodingDataSize { size: data.len(), expected: n_pixels * N });
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
|
|
|
|
2021-12-30 15:01:16 +00:00
|
|
|
let out_size = out.len();
|
2022-01-02 17:16:05 +00:00
|
|
|
let mut buf = BytesMut::new(out);
|
2021-11-28 12:36:12 +00:00
|
|
|
|
2022-01-02 11:02:44 +00:00
|
|
|
let header = Header { width, height, channels: N as u8, colorspace, ..Header::default() };
|
2022-01-02 17:16:05 +00:00
|
|
|
buf = buf.write_many(&header.encode());
|
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);
|
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 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 17:16:05 +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-02 17:16:05 +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
|
|
|
}
|
|
|
|
let index_pos = px.hash_index();
|
2022-01-02 11:02:44 +00:00
|
|
|
let index_px = &mut index[index_pos as usize];
|
2021-12-30 15:01:16 +00:00
|
|
|
let px_rgba = px.as_rgba(0xff);
|
|
|
|
if *index_px == px_rgba {
|
2022-01-02 17:16:05 +00:00
|
|
|
buf = buf.write_one(QOI_OP_INDEX | index_pos);
|
2021-11-28 12:36:12 +00:00
|
|
|
} else {
|
2021-12-30 15:01:16 +00:00
|
|
|
*index_px = px_rgba;
|
2022-01-02 17:16:05 +00:00
|
|
|
buf = px.encode_into(px_prev, buf);
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
|
|
|
px_prev = px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 17:16:05 +00:00
|
|
|
buf = buf.write_many(&QOI_PADDING);
|
2021-12-30 15:01:16 +00:00
|
|
|
Ok(out_size.saturating_sub(buf.len()))
|
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]
|
2021-12-31 10:34:27 +00:00
|
|
|
pub fn qoi_encode_to_buf<O, D, C>(
|
|
|
|
mut out: O, data: D, width: u32, height: u32, channels: u8, colorspace: C,
|
|
|
|
) -> Result<usize>
|
|
|
|
where
|
|
|
|
O: AsMut<[u8]>,
|
|
|
|
D: AsRef<[u8]>,
|
|
|
|
C: TryInto<ColorSpace>,
|
|
|
|
Error: From<C::Error>,
|
|
|
|
{
|
2021-12-29 19:41:39 +00:00
|
|
|
let out = out.as_mut();
|
|
|
|
let data = data.as_ref();
|
2021-12-31 10:34:27 +00:00
|
|
|
let colorspace = colorspace.try_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-31 10:34:27 +00:00
|
|
|
pub fn qoi_encode_to_vec<D, C>(
|
|
|
|
data: D, width: u32, height: u32, channels: u8, colorspace: C,
|
|
|
|
) -> Result<Vec<u8>>
|
|
|
|
where
|
|
|
|
D: AsRef<[u8]>,
|
|
|
|
C: TryInto<ColorSpace>,
|
|
|
|
Error: From<C::Error>,
|
|
|
|
{
|
2021-12-30 15:01:16 +00:00
|
|
|
let size = encode_size_required(width, height, channels);
|
|
|
|
let mut out = vec![0; size]; // note: we could save time here but that won't be safe anymore
|
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
|
|
|
}
|