/rust/registry/src/index.crates.io-1949cf8c6b5b557f/qoi-0.4.1/src/header.rs
Line | Count | Source |
1 | | use core::convert::TryInto; |
2 | | |
3 | | use bytemuck::cast_slice; |
4 | | |
5 | | use crate::consts::{QOI_HEADER_SIZE, QOI_MAGIC, QOI_PIXELS_MAX}; |
6 | | use crate::encode_max_len; |
7 | | use crate::error::{Error, Result}; |
8 | | use crate::types::{Channels, ColorSpace}; |
9 | | use crate::utils::unlikely; |
10 | | |
11 | | /// Image header: dimensions, channels, color space. |
12 | | /// |
13 | | /// ### Notes |
14 | | /// A valid image header must satisfy the following conditions: |
15 | | /// * Both width and height must be non-zero. |
16 | | /// * Maximum number of pixels is 400Mp (=4e8 pixels). |
17 | | #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] |
18 | | pub struct Header { |
19 | | /// Image width in pixels |
20 | | pub width: u32, |
21 | | /// Image height in pixels |
22 | | pub height: u32, |
23 | | /// Number of 8-bit channels per pixel |
24 | | pub channels: Channels, |
25 | | /// Color space (informative field, doesn't affect encoding) |
26 | | pub colorspace: ColorSpace, |
27 | | } |
28 | | |
29 | | impl Default for Header { |
30 | | #[inline] |
31 | 0 | fn default() -> Self { |
32 | 0 | Self { |
33 | 0 | width: 1, |
34 | 0 | height: 1, |
35 | 0 | channels: Channels::default(), |
36 | 0 | colorspace: ColorSpace::default(), |
37 | 0 | } |
38 | 0 | } |
39 | | } |
40 | | |
41 | | impl Header { |
42 | | /// Creates a new header and validates image dimensions. |
43 | | #[inline] |
44 | 0 | pub const fn try_new( |
45 | 0 | width: u32, height: u32, channels: Channels, colorspace: ColorSpace, |
46 | 0 | ) -> Result<Self> { |
47 | 0 | let n_pixels = (width as usize).saturating_mul(height as usize); |
48 | 0 | if unlikely(n_pixels == 0 || n_pixels > QOI_PIXELS_MAX) { |
49 | 0 | return Err(Error::InvalidImageDimensions { width, height }); |
50 | 0 | } |
51 | 0 | Ok(Self { width, height, channels, colorspace }) |
52 | 0 | } Unexecuted instantiation: <qoi::header::Header>::try_new Unexecuted instantiation: <qoi::header::Header>::try_new Unexecuted instantiation: <qoi::header::Header>::try_new Unexecuted instantiation: <qoi::header::Header>::try_new Unexecuted instantiation: <qoi::header::Header>::try_new Unexecuted instantiation: <qoi::header::Header>::try_new Unexecuted instantiation: <qoi::header::Header>::try_new Unexecuted instantiation: <qoi::header::Header>::try_new Unexecuted instantiation: <qoi::header::Header>::try_new Unexecuted instantiation: <qoi::header::Header>::try_new Unexecuted instantiation: <qoi::header::Header>::try_new Unexecuted instantiation: <qoi::header::Header>::try_new |
53 | | |
54 | | /// Creates a new header with modified channels. |
55 | | #[inline] |
56 | 0 | pub const fn with_channels(mut self, channels: Channels) -> Self { |
57 | 0 | self.channels = channels; |
58 | 0 | self |
59 | 0 | } |
60 | | |
61 | | /// Creates a new header with modified color space. |
62 | | #[inline] |
63 | 0 | pub const fn with_colorspace(mut self, colorspace: ColorSpace) -> Self { |
64 | 0 | self.colorspace = colorspace; |
65 | 0 | self |
66 | 0 | } |
67 | | |
68 | | /// Serializes the header into a bytes array. |
69 | | #[inline] |
70 | 0 | pub(crate) fn encode(&self) -> [u8; QOI_HEADER_SIZE] { |
71 | 0 | let mut out = [0; QOI_HEADER_SIZE]; |
72 | 0 | out[..4].copy_from_slice(&QOI_MAGIC.to_be_bytes()); |
73 | 0 | out[4..8].copy_from_slice(&self.width.to_be_bytes()); |
74 | 0 | out[8..12].copy_from_slice(&self.height.to_be_bytes()); |
75 | 0 | out[12] = self.channels.into(); |
76 | 0 | out[13] = self.colorspace.into(); |
77 | 0 | out |
78 | 0 | } Unexecuted instantiation: <qoi::header::Header>::encode Unexecuted instantiation: <qoi::header::Header>::encode |
79 | | |
80 | | /// Deserializes the header from a byte array. |
81 | | #[inline] |
82 | 0 | pub(crate) fn decode(data: impl AsRef<[u8]>) -> Result<Self> { |
83 | 0 | let data = data.as_ref(); |
84 | 0 | if unlikely(data.len() < QOI_HEADER_SIZE) { |
85 | 0 | return Err(Error::UnexpectedBufferEnd); |
86 | 0 | } |
87 | 0 | let v = cast_slice::<_, [u8; 4]>(&data[..12]); |
88 | 0 | let magic = u32::from_be_bytes(v[0]); |
89 | 0 | let width = u32::from_be_bytes(v[1]); |
90 | 0 | let height = u32::from_be_bytes(v[2]); |
91 | 0 | let channels = data[12].try_into()?; |
92 | 0 | let colorspace = data[13].try_into()?; |
93 | 0 | if unlikely(magic != QOI_MAGIC) { |
94 | 0 | return Err(Error::InvalidMagic { magic }); |
95 | 0 | } |
96 | 0 | Self::try_new(width, height, channels, colorspace) |
97 | 0 | } Unexecuted instantiation: <qoi::header::Header>::decode::<[u8; 14]> Unexecuted instantiation: <qoi::header::Header>::decode::<_> Unexecuted instantiation: <qoi::header::Header>::decode::<[u8; 14]> Unexecuted instantiation: <qoi::header::Header>::decode::<[u8; 14]> Unexecuted instantiation: <qoi::header::Header>::decode::<[u8; 14]> Unexecuted instantiation: <qoi::header::Header>::decode::<[u8; 14]> Unexecuted instantiation: <qoi::header::Header>::decode::<[u8; 14]> Unexecuted instantiation: <qoi::header::Header>::decode::<[u8; 14]> Unexecuted instantiation: <qoi::header::Header>::decode::<[u8; 14]> Unexecuted instantiation: <qoi::header::Header>::decode::<[u8; 14]> Unexecuted instantiation: <qoi::header::Header>::decode::<[u8; 14]> Unexecuted instantiation: <qoi::header::Header>::decode::<[u8; 14]> |
98 | | |
99 | | /// Returns a number of pixels in the image. |
100 | | #[inline] |
101 | 0 | pub const fn n_pixels(&self) -> usize { |
102 | 0 | (self.width as usize).saturating_mul(self.height as usize) |
103 | 0 | } Unexecuted instantiation: <qoi::header::Header>::n_pixels Unexecuted instantiation: <qoi::header::Header>::n_pixels Unexecuted instantiation: <qoi::header::Header>::n_pixels Unexecuted instantiation: <qoi::header::Header>::n_pixels Unexecuted instantiation: <qoi::header::Header>::n_pixels Unexecuted instantiation: <qoi::header::Header>::n_pixels Unexecuted instantiation: <qoi::header::Header>::n_pixels Unexecuted instantiation: <qoi::header::Header>::n_pixels Unexecuted instantiation: <qoi::header::Header>::n_pixels Unexecuted instantiation: <qoi::header::Header>::n_pixels Unexecuted instantiation: <qoi::header::Header>::n_pixels Unexecuted instantiation: <qoi::header::Header>::n_pixels |
104 | | |
105 | | /// Returns the total number of bytes in the raw pixel array. |
106 | | /// |
107 | | /// This may come useful when pre-allocating a buffer to decode the image into. |
108 | | #[inline] |
109 | 0 | pub const fn n_bytes(&self) -> usize { |
110 | 0 | self.n_pixels() * self.channels.as_u8() as usize |
111 | 0 | } |
112 | | |
113 | | /// The maximum number of bytes the encoded image will take. |
114 | | /// |
115 | | /// Can be used to pre-allocate the buffer to encode the image into. |
116 | | #[inline] |
117 | 0 | pub fn encode_max_len(&self) -> usize { |
118 | 0 | encode_max_len(self.width, self.height, self.channels) |
119 | 0 | } Unexecuted instantiation: <qoi::header::Header>::encode_max_len Unexecuted instantiation: <qoi::header::Header>::encode_max_len |
120 | | } |