Add unique stream-end marker (7 zeros and one 1)

This commit is contained in:
Ivan Smirnov 2021-12-29 16:07:54 +03:00
parent 4d0d760f92
commit 6640bc571a
3 changed files with 10 additions and 8 deletions

View file

@ -20,7 +20,9 @@ pub const QOI_MASK_3: u8 = 0xe0; // (111)00000
pub const QOI_MASK_4: u8 = 0xf0; // (1111)0000
pub const QOI_HEADER_SIZE: usize = 14;
pub const QOI_PADDING: usize = 4;
pub const QOI_PADDING: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0x01]; // 7 zeros and one 0x01 marker
pub const QOI_PADDING_SIZE: usize = 8;
pub const QOI_MAGIC: u32 =
(b'q' as u32) << 24 | (b'o' as u32) << 16 | (b'i' as u32) << 8 | (b'f' as u32);

View file

@ -1,6 +1,6 @@
use std::mem;
use crate::consts::{QOI_HEADER_SIZE, QOI_INDEX, QOI_PADDING};
use crate::consts::{QOI_HEADER_SIZE, QOI_INDEX, QOI_PADDING, QOI_PADDING_SIZE};
use crate::error::{Error, Result};
use crate::header::Header;
use crate::pixel::{Pixel, SupportedChannels};
@ -35,10 +35,10 @@ pub fn qoi_decode_impl<const N: usize>(data: &[u8], n_pixels: usize) -> Result<V
where
Pixel<N>: SupportedChannels,
{
if unlikely(data.len() < QOI_HEADER_SIZE + QOI_PADDING) {
if unlikely(data.len() < QOI_HEADER_SIZE + QOI_PADDING_SIZE) {
return Err(Error::InputBufferTooSmall {
size: data.len(),
required: QOI_HEADER_SIZE + QOI_PADDING,
required: QOI_HEADER_SIZE + QOI_PADDING_SIZE,
});
}
@ -47,7 +47,7 @@ where
// Safety: we have just allocated enough memory to set the length without problems
pixels.set_len(n_pixels);
}
let encoded_data_size = data.len() - QOI_HEADER_SIZE - QOI_PADDING;
let encoded_data_size = data.len() - QOI_HEADER_SIZE - QOI_PADDING_SIZE;
let mut buf = unsafe {
// Safety: we will check within the loop that there are no reads outside the slice
ReadBuf::new(data.as_ptr().add(QOI_HEADER_SIZE), encoded_data_size)

View file

@ -3,7 +3,7 @@ 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_PIXELS_MAX, QOI_RUN_16, QOI_RUN_8,
QOI_PADDING_SIZE, QOI_PIXELS_MAX, QOI_RUN_16, QOI_RUN_8,
};
use crate::error::{Error, Result};
use crate::header::Header;
@ -220,7 +220,7 @@ where
}
}
buf.write([0; QOI_PADDING]);
buf.write(QOI_PADDING);
Ok(buf.len())
}
@ -253,7 +253,7 @@ pub fn encode_to_vec_impl<const CANONICAL: bool>(
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);
QOI_HEADER_SIZE + n_pixels.saturating_mul(usize::from(channels)) + n_pixels + QOI_PADDING
QOI_HEADER_SIZE + n_pixels.saturating_mul(usize::from(channels)) + n_pixels + QOI_PADDING_SIZE
}
#[inline]