From 62a659eb03c516ffe1d528ac6887a7d7a9558449 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Mon, 29 Nov 2021 22:38:09 +0000 Subject: [PATCH] Change magic type to u32 (like in reference impl) --- src/consts.rs | 3 ++- src/error.rs | 2 +- src/header.rs | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/consts.rs b/src/consts.rs index 6cb53c2..ee13ee0 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -22,4 +22,5 @@ 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_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); diff --git a/src/error.rs b/src/error.rs index 98d2c5b..a5d7cf2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -10,7 +10,7 @@ pub enum Error { EmptyImage { width: u32, height: u32 }, BadEncodingDataSize { size: usize, expected: usize }, BadDecodingDataSize { size: usize }, - InvalidMagic { magic: [u8; 4] }, + InvalidMagic { magic: u32 }, // TODO: invalid colorspace } diff --git a/src/header.rs b/src/header.rs index 8bc6a71..9369b1b 100644 --- a/src/header.rs +++ b/src/header.rs @@ -3,7 +3,7 @@ use crate::consts::{QOI_HEADER_SIZE, QOI_MAGIC}; #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Header { - pub magic: [u8; 4], + pub magic: u32, pub width: u32, pub height: u32, pub channels: u8, @@ -40,7 +40,7 @@ impl Header { pub(crate) fn to_bytes(&self) -> [u8; 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[8..12].copy_from_slice(&u32_to_be(self.height)); out[12] = self.channels; @@ -50,7 +50,7 @@ impl Header { pub(crate) fn from_bytes(v: [u8; QOI_HEADER_SIZE]) -> Self { 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.height = u32_from_be(&v[8..12]); out.channels = v[12];