Add unique stream-end marker (7 zeros and one 1)
This commit is contained in:
parent
4d0d760f92
commit
6640bc571a
3 changed files with 10 additions and 8 deletions
|
@ -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_MASK_4: u8 = 0xf0; // (1111)0000
|
||||||
|
|
||||||
pub const QOI_HEADER_SIZE: usize = 14;
|
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 =
|
pub const QOI_MAGIC: u32 =
|
||||||
(b'q' as u32) << 24 | (b'o' as u32) << 16 | (b'i' as u32) << 8 | (b'f' as u32);
|
(b'q' as u32) << 24 | (b'o' as u32) << 16 | (b'i' as u32) << 8 | (b'f' as u32);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::mem;
|
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::error::{Error, Result};
|
||||||
use crate::header::Header;
|
use crate::header::Header;
|
||||||
use crate::pixel::{Pixel, SupportedChannels};
|
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
|
where
|
||||||
Pixel<N>: SupportedChannels,
|
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 {
|
return Err(Error::InputBufferTooSmall {
|
||||||
size: data.len(),
|
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
|
// Safety: we have just allocated enough memory to set the length without problems
|
||||||
pixels.set_len(n_pixels);
|
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 {
|
let mut buf = unsafe {
|
||||||
// Safety: we will check within the loop that there are no reads outside the slice
|
// 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)
|
ReadBuf::new(data.as_ptr().add(QOI_HEADER_SIZE), encoded_data_size)
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::slice;
|
||||||
use crate::colorspace::ColorSpace;
|
use crate::colorspace::ColorSpace;
|
||||||
use crate::consts::{
|
use crate::consts::{
|
||||||
QOI_COLOR, QOI_DIFF_16, QOI_DIFF_24, QOI_DIFF_8, QOI_HEADER_SIZE, QOI_INDEX, QOI_PADDING,
|
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::error::{Error, Result};
|
||||||
use crate::header::Header;
|
use crate::header::Header;
|
||||||
|
@ -220,7 +220,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.write([0; QOI_PADDING]);
|
buf.write(QOI_PADDING);
|
||||||
Ok(buf.len())
|
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 {
|
pub fn encode_size_required(width: u32, height: u32, channels: u8) -> usize {
|
||||||
let (width, height) = (width as usize, height as usize);
|
let (width, height) = (width as usize, height as usize);
|
||||||
let n_pixels = width.saturating_mul(height);
|
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]
|
#[inline]
|
||||||
|
|
Loading…
Reference in a new issue