Debian Codegraph @

src/header.rs

raw symbols SCIP
1 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::TryIntorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryInto#;
2
3 use bytemuckrust-analyzer cargo bytemuck 1.25.2 crate/::cast_slicerust-analyzer cargo bytemuck 1.25.2 cast_slice().;
4
5 use craterust-analyzer cargo qoi 0.4.1 crate/::constsrust-analyzer cargo qoi 0.4.1 consts/::{QOI_HEADER_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_HEADER_SIZE., QOI_MAGICrust-analyzer cargo qoi 0.4.1 consts/QOI_MAGIC., QOI_PIXELS_MAXrust-analyzer cargo qoi 0.4.1 consts/QOI_PIXELS_MAX.};
6 use craterust-analyzer cargo qoi 0.4.1 crate/::encode_max_lenrust-analyzer cargo qoi 0.4.1 encode/encode_max_len().;
7 use craterust-analyzer cargo qoi 0.4.1 crate/::errorrust-analyzer cargo qoi 0.4.1 error/::{Errorrust-analyzer cargo qoi 0.4.1 error/Error#, Resultrust-analyzer cargo qoi 0.4.1 error/Result#};
8 use craterust-analyzer cargo qoi 0.4.1 crate/::typesrust-analyzer cargo qoi 0.4.1 types/::{Channelsrust-analyzer cargo qoi 0.4.1 types/Channels#, ColorSpacerust-analyzer cargo qoi 0.4.1 types/ColorSpace#};
9 use craterust-analyzer cargo qoi 0.4.1 crate/::utilsrust-analyzer cargo qoi 0.4.1 utils/::unlikelyrust-analyzer cargo qoi 0.4.1 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 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)]
18 pub struct Headerrust-analyzer cargo qoi 0.4.1 header/Header# {
19 /// Image width in pixels
20 pub widthrust-analyzer cargo qoi 0.4.1 header/Header#width.: u32,
21 /// Image height in pixels
22 pub heightrust-analyzer cargo qoi 0.4.1 header/Header#height.: u32,
23 /// Number of 8-bit channels per pixel
24 pub channelsrust-analyzer cargo qoi 0.4.1 header/Header#channels.: Channelsrust-analyzer cargo qoi 0.4.1 types/Channels#,
25 /// Color space (informative field, doesn't affect encoding)
26 pub colorspacerust-analyzer cargo qoi 0.4.1 header/Header#colorspace.: ColorSpacerust-analyzer cargo qoi 0.4.1 types/ColorSpace#,
27 }
28
29 impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for Headerrust-analyzer cargo qoi 0.4.1 header/Header# {
30 #[inline]
31 fn defaultrust-analyzer cargo qoi 0.4.1 header/impl#[Header][Default]default().() -> Selfrust-analyzer cargo qoi 0.4.1 header/impl#[Header][Default] {
32 Selfrust-analyzer cargo qoi 0.4.1 header/impl#[Header][Default] {
33 widthrust-analyzer cargo qoi 0.4.1 header/Header#width.: 1,
34 heightrust-analyzer cargo qoi 0.4.1 header/Header#height.: 1,
35 channelsrust-analyzer cargo qoi 0.4.1 header/Header#channels.: Channelsrust-analyzer cargo qoi 0.4.1 types/Channels#::defaultrust-analyzer cargo qoi 0.4.1 types/impl#[Channels][Default]default().(),
36 colorspacerust-analyzer cargo qoi 0.4.1 header/Header#colorspace.: ColorSpacerust-analyzer cargo qoi 0.4.1 types/ColorSpace#::defaultrust-analyzer cargo qoi 0.4.1 types/impl#[ColorSpace][Default]default().(),
37 }
38 }
39 }
40
41 impl Headerrust-analyzer cargo qoi 0.4.1 header/Header# {
42 /// Creates a new header and validates image dimensions.
43 #[inline]
44 pub const fn try_newrust-analyzer cargo qoi 0.4.1 header/impl#[Header]try_new().(
45 width: u32, height: u32, channels: Channelsrust-analyzer cargo qoi 0.4.1 types/Channels#, colorspace: ColorSpacerust-analyzer cargo qoi 0.4.1 types/ColorSpace#,
46 ) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<Selfrust-analyzer cargo qoi 0.4.1 header/impl#[Header]> {
47 let n_pixels = (width as usize).saturating_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_mul().(height as usize);
48 if unlikelyrust-analyzer cargo qoi 0.4.1 utils/unlikely().(n_pixels rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 || n_pixels rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().QOI_PIXELS_MAXrust-analyzer cargo qoi 0.4.1 consts/QOI_PIXELS_MAX.) {
49 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo qoi 0.4.1 error/Error#::InvalidImageDimensionsrust-analyzer cargo qoi 0.4.1 error/Error#InvalidImageDimensions# { widthrust-analyzer cargo qoi 0.4.1 error/Error#InvalidImageDimensions#width., heightrust-analyzer cargo qoi 0.4.1 error/Error#InvalidImageDimensions#height. });
50 }
51 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo qoi 0.4.1 header/impl#[Header] { widthrust-analyzer cargo qoi 0.4.1 header/Header#width., heightrust-analyzer cargo qoi 0.4.1 header/Header#height., channelsrust-analyzer cargo qoi 0.4.1 header/Header#channels., colorspacerust-analyzer cargo qoi 0.4.1 header/Header#colorspace. })
52 }
53
54 /// Creates a new header with modified channels.
55 #[inline]
56 pub const fn with_channelsrust-analyzer cargo qoi 0.4.1 header/impl#[Header]with_channels().(mut self, channels: Channelsrust-analyzer cargo qoi 0.4.1 types/Channels#) -> Selfrust-analyzer cargo qoi 0.4.1 header/impl#[Header] {
57 self.channelsrust-analyzer cargo qoi 0.4.1 header/Header#channels. = channels;
58 self
59 }
60
61 /// Creates a new header with modified color space.
62 #[inline]
63 pub const fn with_colorspacerust-analyzer cargo qoi 0.4.1 header/impl#[Header]with_colorspace().(mut self, colorspace: ColorSpacerust-analyzer cargo qoi 0.4.1 types/ColorSpace#) -> Selfrust-analyzer cargo qoi 0.4.1 header/impl#[Header] {
64 self.colorspacerust-analyzer cargo qoi 0.4.1 header/Header#colorspace. = colorspace;
65 self
66 }
67
68 /// Serializes the header into a bytes array.
69 #[inline]
70 pub(craterust-analyzer cargo qoi 0.4.1 crate/) fn encoderust-analyzer cargo qoi 0.4.1 header/impl#[Header]encode().(&self) -> [u8; QOI_HEADER_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_HEADER_SIZE.] {
71 let mut out = [0; QOI_HEADER_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_HEADER_SIZE.];
72 out[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#4]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut()..copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&QOI_MAGICrust-analyzer cargo qoi 0.4.1 consts/QOI_MAGIC..to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]to_be_bytes().());
73 out[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().4..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#8]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut()..copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&self.widthrust-analyzer cargo qoi 0.4.1 header/Header#width..to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]to_be_bytes().());
74 out[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().8..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#12]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut()..copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&self.heightrust-analyzer cargo qoi 0.4.1 header/Header#height..to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]to_be_bytes().());
75 out[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().12]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut(). = self.channelsrust-analyzer cargo qoi 0.4.1 header/Header#channels..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
76 out[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().13]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut(). = self.colorspacerust-analyzer cargo qoi 0.4.1 header/Header#colorspace..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
77 out
78 }
79
80 /// Deserializes the header from a byte array.
81 #[inline]
82 pub(craterust-analyzer cargo qoi 0.4.1 crate/) fn decoderust-analyzer cargo qoi 0.4.1 header/impl#[Header]decode().(data: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<Selfrust-analyzer cargo qoi 0.4.1 header/impl#[Header]> {
83 let data = data.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().();
84 if unlikelyrust-analyzer cargo qoi 0.4.1 utils/unlikely().(data.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().QOI_HEADER_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_HEADER_SIZE.) {
85 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo qoi 0.4.1 error/Error#::UnexpectedBufferEndrust-analyzer cargo qoi 0.4.1 error/Error#UnexpectedBufferEnd#);
86 }
87 let v = cast_slicerust-analyzer cargo bytemuck 1.25.2 cast_slice().::<_, [u8; 4]>(&data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#12]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().);
88 let magic = u32::from_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]from_be_bytes().(v[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().);
89 let width = u32::from_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]from_be_bytes().(v[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().);
90 let height = u32::from_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]from_be_bytes().(v[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().);
91 let channels = data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().12]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
92 let colorspace = data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().13]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
93 if unlikelyrust-analyzer cargo qoi 0.4.1 utils/unlikely().(magic rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().QOI_MAGICrust-analyzer cargo qoi 0.4.1 consts/QOI_MAGIC.) {
94 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo qoi 0.4.1 error/Error#::InvalidMagicrust-analyzer cargo qoi 0.4.1 error/Error#InvalidMagic# { magicrust-analyzer cargo qoi 0.4.1 error/Error#InvalidMagic#magic. });
95 }
96 Selfrust-analyzer cargo qoi 0.4.1 header/impl#[Header]::try_newrust-analyzer cargo qoi 0.4.1 header/impl#[Header]try_new().(width, height, channels, colorspace)
97 }
98
99 /// Returns a number of pixels in the image.
100 #[inline]
101 pub const fn n_pixelsrust-analyzer cargo qoi 0.4.1 header/impl#[Header]n_pixels().(&self) -> usize {
102 (self.widthrust-analyzer cargo qoi 0.4.1 header/Header#width. as usize).saturating_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_mul().(self.heightrust-analyzer cargo qoi 0.4.1 header/Header#height. as usize)
103 }
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 pub const fn n_bytesrust-analyzer cargo qoi 0.4.1 header/impl#[Header]n_bytes().(&self) -> usize {
110 self.n_pixelsrust-analyzer cargo qoi 0.4.1 header/impl#[Header]n_pixels().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().self.channelsrust-analyzer cargo qoi 0.4.1 header/Header#channels..as_u8rust-analyzer cargo qoi 0.4.1 types/impl#[Channels]as_u8().() as usize
111 }
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 pub fn encode_max_lenrust-analyzer cargo qoi 0.4.1 header/impl#[Header]encode_max_len().(&self) -> usize {
118 encode_max_lenrust-analyzer cargo qoi 0.4.1 encode/encode_max_len().(self.widthrust-analyzer cargo qoi 0.4.1 header/Header#width., self.heightrust-analyzer cargo qoi 0.4.1 header/Header#height., self.channelsrust-analyzer cargo qoi 0.4.1 header/Header#channels.)
119 }
120 }