Debian Codegraph @

src/encode.rs

raw symbols SCIP
1 #[cfg(any(feature = "std", feature = "alloc"))]
2 use allocrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#};
3 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/::TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#;
4 #[cfg(feature = "std")]
5 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#;
6
7 use bytemuckrust-analyzer cargo bytemuck 1.25.2 crate/::Podrust-analyzer cargo bytemuck 1.25.2 pod/Pod#;
8
9 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_OP_INDEXrust-analyzer cargo qoi 0.4.1 consts/QOI_OP_INDEX., QOI_OP_RUNrust-analyzer cargo qoi 0.4.1 consts/QOI_OP_RUN., QOI_PADDINGrust-analyzer cargo qoi 0.4.1 consts/QOI_PADDING., QOI_PADDING_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_PADDING_SIZE.};
10 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#};
11 use craterust-analyzer cargo qoi 0.4.1 crate/::headerrust-analyzer cargo qoi 0.4.1 header/::Headerrust-analyzer cargo qoi 0.4.1 header/Header#;
12 use craterust-analyzer cargo qoi 0.4.1 crate/::pixelrust-analyzer cargo qoi 0.4.1 pixel/::{Pixelrust-analyzer cargo qoi 0.4.1 pixel/Pixel#, SupportedChannelsrust-analyzer cargo qoi 0.4.1 pixel/SupportedChannels#};
13 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#};
14 #[cfg(feature = "std")]
15 use craterust-analyzer cargo qoi 0.4.1 crate/::utilsrust-analyzer cargo qoi 0.4.1 utils/::GenericWriterrust-analyzer cargo qoi 0.4.1 utils/GenericWriter#;
16 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()., BytesMutrust-analyzer cargo qoi 0.4.1 utils/BytesMut#, Writerrust-analyzer cargo qoi 0.4.1 utils/Writer#};
17
18 #[allow(clippy::cast_possible_truncation, unused_assignments, unused_variables)]
19 fn encode_implrust-analyzer cargo qoi 0.4.1 encode/encode_impl().<W: Writerrust-analyzer cargo qoi 0.4.1 utils/Writer#, const N: usize>(mut buf: W, data: &[u8]) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<usize>
20 where
21 Pixelrust-analyzer cargo qoi 0.4.1 pixel/Pixel#<N>: SupportedChannelsrust-analyzer cargo qoi 0.4.1 pixel/SupportedChannels#,
22 [u8; N]: Podrust-analyzer cargo bytemuck 1.25.2 pod/Pod#,
23 {
24 let cap = buf.capacityrust-analyzer cargo qoi 0.4.1 utils/Writer#capacity().();
25
26 let mut index = [Pixelrust-analyzer cargo qoi 0.4.1 pixel/Pixel#::newrust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]new().(); 256];
27 let mut px_prev = Pixelrust-analyzer cargo qoi 0.4.1 pixel/Pixel#::newrust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]new().().with_arust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]with_a().(0xff);
28 let mut hash_prev = px_prev.hash_indexrust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]hash_index().();
29 let mut run = 0_u8;
30 let mut px = Pixelrust-analyzer cargo qoi 0.4.1 pixel/Pixel#::<N>::newrust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]new().().with_arust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]with_a().(0xff);
31 let mut index_allowed = false;
32
33 let n_pixels = 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 ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().N;
34
35 for (i, chunk) in data.chunks_exactrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact().(N).enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() {
36 px.readrust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]read().(chunk);
37 if px rust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`][`PartialEq<Self>`]eq().==rust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`][`PartialEq<Self>`]eq(). rust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`][`PartialEq<Self>`]eq().px_prev {
38 run rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`AddAssign<Self>`]add_assign().1;
39 if run rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().62 || unlikelyrust-analyzer cargo qoi 0.4.1 utils/unlikely().(i 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().n_pixels rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1) {
40 buf = buf.write_onerust-analyzer cargo qoi 0.4.1 utils/Writer#write_one().(QOI_OP_RUNrust-analyzer cargo qoi 0.4.1 consts/QOI_OP_RUN. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOr<Self>`]bitor().(run rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().1))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
41 run = 0;
42 }
43 } else {
44 if run rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().0 {
45 #[cfg(not(feature = "reference"))]
46 {
47 // credits for the original idea: @zakarumych (had to be fixed though)
48 buf = buf.write_one(if run == 1 && index_allowed {
49 QOI_OP_INDEXrust-analyzer cargo qoi 0.4.1 consts/QOI_OP_INDEX. | hash_prev
50 } else {
51 QOI_OP_RUNrust-analyzer cargo qoi 0.4.1 consts/QOI_OP_RUN. | (run - 1)
52 })?;
53 }
54 #[cfg(feature = "reference")]
55 {
56 buf = buf.write_onerust-analyzer cargo qoi 0.4.1 utils/Writer#write_one().(QOI_OP_RUNrust-analyzer cargo qoi 0.4.1 consts/QOI_OP_RUN. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOr<Self>`]bitor().(run rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().1))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
57 }
58 run = 0;
59 }
60 index_allowed = true;
61 let px_rgba = px.as_rgbarust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]as_rgba().(0xff);
62 hash_prev = px_rgba.hash_indexrust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]hash_index().();
63 let index_px = &mut index[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().hash_prev as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().;
64 if *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().index_px rust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`][`PartialEq<Self>`]eq().==rust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`][`PartialEq<Self>`]eq(). rust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`][`PartialEq<Self>`]eq().px_rgba {
65 buf = buf.write_onerust-analyzer cargo qoi 0.4.1 utils/Writer#write_one().(QOI_OP_INDEXrust-analyzer cargo qoi 0.4.1 consts/QOI_OP_INDEX. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOr<Self>`]bitor().hash_prev)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
66 } else {
67 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().index_px = px_rgba;
68 buf = px.encode_intorust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]encode_into().(px_prev, buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
69 }
70 px_prev = px;
71 }
72 }
73
74 buf = buf.write_manyrust-analyzer cargo qoi 0.4.1 utils/Writer#write_many().(&QOI_PADDINGrust-analyzer cargo qoi 0.4.1 consts/QOI_PADDING.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
75 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(cap.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(buf.capacityrust-analyzer cargo qoi 0.4.1 utils/Writer#capacity().()))
76 }
77
78 #[inline]
79 fn encode_impl_allrust-analyzer cargo qoi 0.4.1 encode/encode_impl_all().<W: Writerrust-analyzer cargo qoi 0.4.1 utils/Writer#>(out: W, data: &[u8], channels: Channelsrust-analyzer cargo qoi 0.4.1 types/Channels#) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<usize> {
80 match channels {
81 Channelsrust-analyzer cargo qoi 0.4.1 types/Channels#::Rgbrust-analyzer cargo qoi 0.4.1 types/Channels#Rgb# => encode_implrust-analyzer cargo qoi 0.4.1 encode/encode_impl().::<_, 3>(out, data),
82 Channelsrust-analyzer cargo qoi 0.4.1 types/Channels#::Rgbarust-analyzer cargo qoi 0.4.1 types/Channels#Rgba# => encode_implrust-analyzer cargo qoi 0.4.1 encode/encode_impl().::<_, 4>(out, data),
83 }
84 }
85
86 /// The maximum number of bytes the encoded image will take.
87 ///
88 /// Can be used to pre-allocate the buffer to encode the image into.
89 #[inline]
90 pub fn encode_max_lenrust-analyzer cargo qoi 0.4.1 encode/encode_max_len().(width: u32, height: u32, channels: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<u8>) -> usize {
91 let (width, height) = (width as usize, height as usize);
92 let n_pixels = width.saturating_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_mul().(height);
93 QOI_HEADER_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_HEADER_SIZE.
94 +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().n_pixels.saturating_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_mul().(channels.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().() as usize)
95 +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().n_pixels
96 +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().QOI_PADDING_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_PADDING_SIZE.
97 }
98
99 /// Encode the image into a pre-allocated buffer.
100 ///
101 /// Returns the total number of bytes written.
102 #[inline]
103 pub fn encode_to_bufrust-analyzer cargo qoi 0.4.1 encode/encode_to_buf().(
104 buf: impl AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<[u8]>, data: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>, width: u32, height: u32,
105 ) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<usize> {
106 Encoderrust-analyzer cargo qoi 0.4.1 encode/Encoder#::newrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`]new().(&data, width, height)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..encode_to_bufrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`]encode_to_buf().(buf)
107 }
108
109 /// Encode the image into a newly allocated vector.
110 #[cfg(any(feature = "alloc", feature = "std"))]
111 #[inline]
112 pub fn encode_to_vecrust-analyzer cargo qoi 0.4.1 encode/encode_to_vec().(data: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>, width: u32, height: u32) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>> {
113 Encoderrust-analyzer cargo qoi 0.4.1 encode/Encoder#::newrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`]new().(&data, width, height)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..encode_to_vecrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`]encode_to_vec().()
114 }
115
116 /// Encode QOI images into buffers or into streams.
117 pub struct Encoderrust-analyzer cargo qoi 0.4.1 encode/Encoder#<'a> {
118 datarust-analyzer cargo qoi 0.4.1 encode/Encoder#data.: &'a [u8],
119 headerrust-analyzer cargo qoi 0.4.1 encode/Encoder#header.: Headerrust-analyzer cargo qoi 0.4.1 header/Header#,
120 }
121
122 impl<'a> Encoderrust-analyzer cargo qoi 0.4.1 encode/Encoder#<'a> {
123 /// Creates a new encoder from a given array of pixel data and image dimensions.
124 ///
125 /// The number of channels will be inferred automatically (the valid values
126 /// are 3 or 4). The color space will be set to sRGB by default.
127 #[inline]
128 #[allow(clippy::cast_possible_truncation)]
129 pub fn newrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`]new().(data: &'a (impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]> + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#), width: u32, height: u32) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<Selfrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`]> {
130 let data = data.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().();
131 let mut header =
132 Headerrust-analyzer cargo qoi 0.4.1 header/Header#::try_newrust-analyzer cargo qoi 0.4.1 header/impl#[Header]try_new().(width, height, Channelsrust-analyzer cargo qoi 0.4.1 types/Channels#::defaultrust-analyzer cargo qoi 0.4.1 types/impl#[Channels][Default]default().(), ColorSpacerust-analyzer cargo qoi 0.4.1 types/ColorSpace#::defaultrust-analyzer cargo qoi 0.4.1 types/impl#[ColorSpace][Default]default().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
133 let size = data.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().();
134 let n_channels = size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().header.n_pixelsrust-analyzer cargo qoi 0.4.1 header/impl#[Header]n_pixels().();
135 if header.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().n_channels rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().size {
136 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#::InvalidImageLengthrust-analyzer cargo qoi 0.4.1 error/Error#InvalidImageLength# { sizerust-analyzer cargo qoi 0.4.1 error/Error#InvalidImageLength#size., widthrust-analyzer cargo qoi 0.4.1 error/Error#InvalidImageLength#width., heightrust-analyzer cargo qoi 0.4.1 error/Error#InvalidImageLength#height. });
137 }
138 header.channelsrust-analyzer cargo qoi 0.4.1 header/Header#channels. = Channelsrust-analyzer cargo qoi 0.4.1 types/Channels#::try_fromrust-analyzer cargo qoi 0.4.1 types/impl#[Channels][`TryFrom<u8>`]try_from().(n_channels.minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(0xff) as u8)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
139 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`] { datarust-analyzer cargo qoi 0.4.1 encode/Encoder#data., headerrust-analyzer cargo qoi 0.4.1 encode/Encoder#header. })
140 }
141
142 /// Returns a new encoder with modified color space.
143 ///
144 /// Note: the color space doesn't affect encoding or decoding in any way, it's
145 /// a purely informative field that's stored in the image header.
146 #[inline]
147 pub const fn with_colorspacerust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`]with_colorspace().(mut self, colorspace: ColorSpacerust-analyzer cargo qoi 0.4.1 types/ColorSpace#) -> Selfrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`] {
148 self.headerrust-analyzer cargo qoi 0.4.1 encode/Encoder#header. = self.headerrust-analyzer cargo qoi 0.4.1 encode/Encoder#header..with_colorspacerust-analyzer cargo qoi 0.4.1 header/impl#[Header]with_colorspace().(colorspace);
149 self
150 }
151
152 /// Returns the inferred number of channels.
153 #[inline]
154 pub const fn channelsrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`]channels().(&self) -> Channelsrust-analyzer cargo qoi 0.4.1 types/Channels# {
155 self.headerrust-analyzer cargo qoi 0.4.1 encode/Encoder#header..channelsrust-analyzer cargo qoi 0.4.1 header/Header#channels.
156 }
157
158 /// Returns the header that will be stored in the encoded image.
159 #[inline]
160 pub const fn headerrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`]header().(&self) -> &Headerrust-analyzer cargo qoi 0.4.1 header/Header# {
161 &self.headerrust-analyzer cargo qoi 0.4.1 encode/Encoder#header.
162 }
163
164 /// The maximum number of bytes the encoded image will take.
165 ///
166 /// Can be used to pre-allocate the buffer to encode the image into.
167 #[inline]
168 pub fn required_buf_lenrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`]required_buf_len().(&self) -> usize {
169 self.headerrust-analyzer cargo qoi 0.4.1 encode/Encoder#header..encode_max_lenrust-analyzer cargo qoi 0.4.1 header/impl#[Header]encode_max_len().()
170 }
171
172 /// Encodes the image to a pre-allocated buffer and returns the number of bytes written.
173 ///
174 /// The minimum size of the buffer can be found via [`Encoder::required_buf_len`].
175 #[inline]
176 pub fn encode_to_bufrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`]encode_to_buf().(&self, mut buf: impl AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<[u8]>) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<usize> {
177 let buf = buf.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#as_mut().();
178 let size_required = self.required_buf_lenrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`]required_buf_len().();
179 if unlikelyrust-analyzer cargo qoi 0.4.1 utils/unlikely().(buf.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().size_required) {
180 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#::OutputBufferTooSmallrust-analyzer cargo qoi 0.4.1 error/Error#OutputBufferTooSmall# { sizerust-analyzer cargo qoi 0.4.1 error/Error#OutputBufferTooSmall#size.: buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), requiredrust-analyzer cargo qoi 0.4.1 error/Error#OutputBufferTooSmall#required.: size_required });
181 }
182 let (head, tail) = buf.split_at_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]split_at_mut().(QOI_HEADER_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_HEADER_SIZE.); // can't panic
183 head.copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&self.headerrust-analyzer cargo qoi 0.4.1 encode/Encoder#header..encoderust-analyzer cargo qoi 0.4.1 header/impl#[Header]encode().());
184 let n_written = encode_impl_allrust-analyzer cargo qoi 0.4.1 encode/encode_impl_all().(BytesMutrust-analyzer cargo qoi 0.4.1 utils/BytesMut#::newrust-analyzer cargo qoi 0.4.1 utils/impl#[`BytesMut<'a>`]new().(tail), self.datarust-analyzer cargo qoi 0.4.1 encode/Encoder#data., self.headerrust-analyzer cargo qoi 0.4.1 encode/Encoder#header..channelsrust-analyzer cargo qoi 0.4.1 header/Header#channels.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
185 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(QOI_HEADER_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_HEADER_SIZE. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().n_written)
186 }
187
188 /// Encodes the image into a newly allocated vector of bytes and returns it.
189 #[cfg(any(feature = "alloc", feature = "std"))]
190 #[inline]
191 pub fn encode_to_vecrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`]encode_to_vec().(&self) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>> {
192 let mut out = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0_u8; self.required_buf_lenrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`]required_buf_len().()];
193 let size = self.encode_to_bufrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`]encode_to_buf().(&mut out)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
194 out.truncaterust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]truncate().(size);
195 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(out)
196 }
197
198 /// Encodes the image directly to a generic writer that implements [`Write`](std::io::Write).
199 ///
200 /// Note: while it's possible to pass a `&mut [u8]` slice here since it implements `Write`,
201 /// it would more effficient to use a specialized method instead: [`Encoder::encode_to_buf`].
202 #[cfg(feature = "std")]
203 #[inline]
204 pub fn encode_to_streamrust-analyzer cargo qoi 0.4.1 encode/impl#[`Encoder<'a>`]encode_to_stream().<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#>(&self, writer: &mut W) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<usize> {
205 writer.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(&self.headerrust-analyzer cargo qoi 0.4.1 encode/Encoder#header..encoderust-analyzer cargo qoi 0.4.1 header/impl#[Header]encode().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
206 let n_written =
207 encode_impl_allrust-analyzer cargo qoi 0.4.1 encode/encode_impl_all().(GenericWriterrust-analyzer cargo qoi 0.4.1 utils/GenericWriter#::newrust-analyzer cargo qoi 0.4.1 utils/impl#[`GenericWriter<W>`]new().(writer), self.datarust-analyzer cargo qoi 0.4.1 encode/Encoder#data., self.headerrust-analyzer cargo qoi 0.4.1 encode/Encoder#header..channelsrust-analyzer cargo qoi 0.4.1 header/Header#channels.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
208 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(n_written rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().QOI_HEADER_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_HEADER_SIZE.)
209 }
210 }