Change magic type to u32 (like in reference impl)

This commit is contained in:
Ivan Smirnov 2021-11-29 22:38:09 +00:00
parent f19c0ee641
commit 62a659eb03
3 changed files with 6 additions and 5 deletions

View file

@ -22,4 +22,5 @@ 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: usize = 4;
pub const QOI_MAGIC: [u8; 4] = [b'q', b'o', b'i', b'f']; 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

@ -10,7 +10,7 @@ pub enum Error {
EmptyImage { width: u32, height: u32 }, EmptyImage { width: u32, height: u32 },
BadEncodingDataSize { size: usize, expected: usize }, BadEncodingDataSize { size: usize, expected: usize },
BadDecodingDataSize { size: usize }, BadDecodingDataSize { size: usize },
InvalidMagic { magic: [u8; 4] }, InvalidMagic { magic: u32 },
// TODO: invalid colorspace // TODO: invalid colorspace
} }

View file

@ -3,7 +3,7 @@ use crate::consts::{QOI_HEADER_SIZE, QOI_MAGIC};
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Header { pub struct Header {
pub magic: [u8; 4], pub magic: u32,
pub width: u32, pub width: u32,
pub height: u32, pub height: u32,
pub channels: u8, pub channels: u8,
@ -40,7 +40,7 @@ impl Header {
pub(crate) fn to_bytes(&self) -> [u8; QOI_HEADER_SIZE] { pub(crate) fn to_bytes(&self) -> [u8; QOI_HEADER_SIZE] {
let mut out = [0; QOI_HEADER_SIZE]; let mut out = [0; QOI_HEADER_SIZE];
out[..4].copy_from_slice(&self.magic); out[..4].copy_from_slice(&u32_to_be(self.magic));
out[4..8].copy_from_slice(&u32_to_be(self.width)); out[4..8].copy_from_slice(&u32_to_be(self.width));
out[8..12].copy_from_slice(&u32_to_be(self.height)); out[8..12].copy_from_slice(&u32_to_be(self.height));
out[12] = self.channels; out[12] = self.channels;
@ -50,7 +50,7 @@ impl Header {
pub(crate) fn from_bytes(v: [u8; QOI_HEADER_SIZE]) -> Self { pub(crate) fn from_bytes(v: [u8; QOI_HEADER_SIZE]) -> Self {
let mut out = Self::default(); let mut out = Self::default();
out.magic.copy_from_slice(&v[..4]); out.magic = u32_from_be(&v[..4]);
out.width = u32_from_be(&v[4..8]); out.width = u32_from_be(&v[4..8]);
out.height = u32_from_be(&v[8..12]); out.height = u32_from_be(&v[8..12]);
out.channels = v[12]; out.channels = v[12];