diff --git a/src/consts.rs b/src/consts.rs index 204903b..d7a5f05 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -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); diff --git a/src/decode.rs b/src/decode.rs index 1fb0076..f06b561 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -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(data: &[u8], n_pixels: usize) -> Result: 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) diff --git a/src/encode.rs b/src/encode.rs index 93cff5f..e383d5f 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -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( 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]