Debian Codegraph @

src/common.rs

raw symbols SCIP
1 use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#;
2 use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc 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/Vec#;
3
4 #[cfg(feature = "color_quant")]
5 use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::collectionsrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/::{BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#, BTreeSetrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/BTreeSet#};
6
7 /// Disposal method
8 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, 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:, 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:)]
9 #[repr(u8)]
10 pub enum DisposalMethodrust-analyzer cargo gif 0.14.2 common/DisposalMethod# {
11 /// `StreamingDecoder` is not required to take any action.
12 Anyrust-analyzer cargo gif 0.14.2 common/DisposalMethod#Any# = 0,
13 /// Do not dispose.
14 Keeprust-analyzer cargo gif 0.14.2 common/DisposalMethod#Keep# = 1,
15 /// Restore to background color.
16 Backgroundrust-analyzer cargo gif 0.14.2 common/DisposalMethod#Background# = 2,
17 /// Restore to previous.
18 Previousrust-analyzer cargo gif 0.14.2 common/DisposalMethod#Previous# = 3,
19 }
20
21 impl DisposalMethodrust-analyzer cargo gif 0.14.2 common/DisposalMethod# {
22 /// Converts `u8` to `Option<Self>`
23 #[must_use]
24 pub const fn from_u8rust-analyzer cargo gif 0.14.2 common/impl#[DisposalMethod]from_u8().(n: u8) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo gif 0.14.2 common/impl#[DisposalMethod]> {
25 match n {
26 0 => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo gif 0.14.2 common/impl#[DisposalMethod]::Anyrust-analyzer cargo gif 0.14.2 common/DisposalMethod#Any#),
27 1 => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo gif 0.14.2 common/impl#[DisposalMethod]::Keeprust-analyzer cargo gif 0.14.2 common/DisposalMethod#Keep#),
28 2 => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo gif 0.14.2 common/impl#[DisposalMethod]::Backgroundrust-analyzer cargo gif 0.14.2 common/DisposalMethod#Background#),
29 3 => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo gif 0.14.2 common/impl#[DisposalMethod]::Previousrust-analyzer cargo gif 0.14.2 common/DisposalMethod#Previous#),
30 _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
31 }
32 }
33 }
34
35 /// Known GIF block labels.
36 ///
37 /// Note that the block uniquely specifies the layout of bytes that follow and how they are
38 /// framed. For example, the header always has a fixed length but is followed by a variable amount
39 /// of additional data. An image descriptor may be followed by a local color table depending on
40 /// information read in it. Therefore, it doesn't make sense to continue parsing after encountering
41 /// an unknown block as the semantics of following bytes are unclear.
42 ///
43 /// The extension block provides a common framing for an arbitrary amount of application specific
44 /// data which may be ignored.
45 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, 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:, 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:)]
46 #[repr(u8)]
47 pub enum Blockrust-analyzer cargo gif 0.14.2 common/Block# {
48 /// Image block.
49 Imagerust-analyzer cargo gif 0.14.2 common/Block#Image# = 0x2C,
50 /// Extension block.
51 Extensionrust-analyzer cargo gif 0.14.2 common/Block#Extension# = 0x21,
52 /// Image trailer.
53 Trailerrust-analyzer cargo gif 0.14.2 common/Block#Trailer# = 0x3B,
54 }
55
56 impl Blockrust-analyzer cargo gif 0.14.2 common/Block# {
57 /// Converts `u8` to `Option<Self>`
58 #[must_use]
59 pub const fn from_u8rust-analyzer cargo gif 0.14.2 common/impl#[Block]from_u8().(n: u8) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo gif 0.14.2 common/impl#[Block]> {
60 match n {
61 0x2C => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo gif 0.14.2 common/impl#[Block]::Imagerust-analyzer cargo gif 0.14.2 common/Block#Image#),
62 0x21 => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo gif 0.14.2 common/impl#[Block]::Extensionrust-analyzer cargo gif 0.14.2 common/Block#Extension#),
63 0x3B => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo gif 0.14.2 common/impl#[Block]::Trailerrust-analyzer cargo gif 0.14.2 common/Block#Trailer#),
64 _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
65 }
66 }
67 }
68
69 /// A newtype wrapper around an arbitrary extension ID.
70 ///
71 /// An extension is some amount of byte data organized in sub-blocks so that one can skip over it
72 /// without knowing the semantics. Though technically you likely want to use a `Application`
73 /// extension, the library tries to stay flexible here.
74 ///
75 /// This allows us to customize the set of impls compared to a raw `u8`. It also clarifies the
76 /// intent and gives some inherent methods for interoperability with known extension types.
77 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, 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:)]
78 pub struct AnyExtensionrust-analyzer cargo gif 0.14.2 common/AnyExtension#(pub u8);
79
80 /// Known GIF extension labels.
81 ///
82 /// These are extensions which may be interpreted by the library and to which a specification with
83 /// the internal data layout is known.
84 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, 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:, 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:)]
85 #[repr(u8)]
86 pub enum Extensionrust-analyzer cargo gif 0.14.2 common/Extension# {
87 /// Plain Text extension.
88 ///
89 /// This instructs the decoder to render a text as characters in a grid of cells, in a
90 /// mono-spaced font of its choosing. This is seldom actually implemented and ignored by
91 /// ImageMagick. The color is always taken from the global table which further complicates any
92 /// use. No real information on the frame sequencing of this block is available in the
93 /// standard.
94 Textrust-analyzer cargo gif 0.14.2 common/Extension#Text# = 0x01,
95 /// Control extension.
96 Controlrust-analyzer cargo gif 0.14.2 common/Extension#Control# = 0xF9,
97 /// Comment extension.
98 Commentrust-analyzer cargo gif 0.14.2 common/Extension#Comment# = 0xFE,
99 /// Application extension.
100 ///
101 /// See [ImageMagick] for an idea of commonly recognized extensions.
102 ///
103 /// [ImageMagick]: https://github.com/ImageMagick/ImageMagick/blob/b0b58c6303195928060f55f9c3ca8233ab7f7733/coders/gif.c#L1128
104 Applicationrust-analyzer cargo gif 0.14.2 common/Extension#Application# = 0xFF,
105 }
106
107 impl AnyExtensionrust-analyzer cargo gif 0.14.2 common/AnyExtension# {
108 /// Decode the label as a known extension.
109 #[must_use]
110 pub const fn into_knownrust-analyzer cargo gif 0.14.2 common/impl#[AnyExtension]into_known().(self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Extensionrust-analyzer cargo gif 0.14.2 common/Extension#> {
111 Extensionrust-analyzer cargo gif 0.14.2 common/Extension#::from_u8rust-analyzer cargo gif 0.14.2 common/impl#[Extension]from_u8().(self.0rust-analyzer cargo gif 0.14.2 common/AnyExtension#0.)
112 }
113 }
114
115 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Extensionrust-analyzer cargo gif 0.14.2 common/Extension#> for AnyExtensionrust-analyzer cargo gif 0.14.2 common/AnyExtension# {
116 fn fromrust-analyzer cargo gif 0.14.2 common/impl#[AnyExtension][`From<Extension>`]from().(ext: Extensionrust-analyzer cargo gif 0.14.2 common/Extension#) -> Selfrust-analyzer cargo gif 0.14.2 common/impl#[AnyExtension][`From<Extension>`] {
117 Selfrust-analyzer cargo gif 0.14.2 common/impl#[AnyExtension][`From<Extension>`](ext as u8)
118 }
119 }
120
121 impl Extensionrust-analyzer cargo gif 0.14.2 common/Extension# {
122 /// Converts `u8` to a `Extension` if it is known.
123 #[must_use]
124 pub const fn from_u8rust-analyzer cargo gif 0.14.2 common/impl#[Extension]from_u8().(n: u8) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo gif 0.14.2 common/impl#[Extension]> {
125 match n {
126 0x01 => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo gif 0.14.2 common/impl#[Extension]::Textrust-analyzer cargo gif 0.14.2 common/Extension#Text#),
127 0xF9 => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo gif 0.14.2 common/impl#[Extension]::Controlrust-analyzer cargo gif 0.14.2 common/Extension#Control#),
128 0xFE => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo gif 0.14.2 common/impl#[Extension]::Commentrust-analyzer cargo gif 0.14.2 common/Extension#Comment#),
129 0xFF => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo gif 0.14.2 common/impl#[Extension]::Applicationrust-analyzer cargo gif 0.14.2 common/Extension#Application#),
130 _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
131 }
132 }
133 }
134
135 /// A GIF frame
136 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
137 pub struct Framerust-analyzer cargo gif 0.14.2 common/Frame#<'a> {
138 /// Frame delay in units of 10 ms.
139 pub delayrust-analyzer cargo gif 0.14.2 common/Frame#delay.: u16,
140 /// Disposal method.
141 pub disposerust-analyzer cargo gif 0.14.2 common/Frame#dispose.: DisposalMethodrust-analyzer cargo gif 0.14.2 common/DisposalMethod#,
142 /// Transparent index (if available).
143 pub transparentrust-analyzer cargo gif 0.14.2 common/Frame#transparent.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u8>,
144 /// True if the frame needs user input to be displayed.
145 pub needs_user_inputrust-analyzer cargo gif 0.14.2 common/Frame#needs_user_input.: bool,
146 /// Offset from the top border of the canvas.
147 pub toprust-analyzer cargo gif 0.14.2 common/Frame#top.: u16,
148 /// Offset from the left border of the canvas.
149 pub leftrust-analyzer cargo gif 0.14.2 common/Frame#left.: u16,
150 /// Width of the frame.
151 pub widthrust-analyzer cargo gif 0.14.2 common/Frame#width.: u16,
152 /// Height of the frame.
153 pub heightrust-analyzer cargo gif 0.14.2 common/Frame#height.: u16,
154 /// True if the image is interlaced.
155 pub interlacedrust-analyzer cargo gif 0.14.2 common/Frame#interlaced.: bool,
156 /// Frame local color palette if available.
157 pub paletterust-analyzer cargo gif 0.14.2 common/Frame#palette.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>>,
158 /// Buffer containing the image data.
159 /// Only indices unless configured differently.
160 pub bufferrust-analyzer cargo gif 0.14.2 common/Frame#buffer.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, [u8]>,
161 }
162
163 impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for Framerust-analyzer cargo gif 0.14.2 common/Frame#<'_> {
164 fn defaultrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'_>`][Default]default().() -> Selfrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'_>`][Default] {
165 Framerust-analyzer cargo gif 0.14.2 common/Frame# {
166 delayrust-analyzer cargo gif 0.14.2 common/Frame#delay.: 0,
167 disposerust-analyzer cargo gif 0.14.2 common/Frame#dispose.: DisposalMethodrust-analyzer cargo gif 0.14.2 common/DisposalMethod#::Keeprust-analyzer cargo gif 0.14.2 common/DisposalMethod#Keep#,
168 transparentrust-analyzer cargo gif 0.14.2 common/Frame#transparent.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
169 needs_user_inputrust-analyzer cargo gif 0.14.2 common/Frame#needs_user_input.: false,
170 toprust-analyzer cargo gif 0.14.2 common/Frame#top.: 0,
171 leftrust-analyzer cargo gif 0.14.2 common/Frame#left.: 0,
172 widthrust-analyzer cargo gif 0.14.2 common/Frame#width.: 0,
173 heightrust-analyzer cargo gif 0.14.2 common/Frame#height.: 0,
174 interlacedrust-analyzer cargo gif 0.14.2 common/Frame#interlaced.: false,
175 paletterust-analyzer cargo gif 0.14.2 common/Frame#palette.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
176 bufferrust-analyzer cargo gif 0.14.2 common/Frame#buffer.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(&[]),
177 }
178 }
179 }
180
181 impl Framerust-analyzer cargo gif 0.14.2 common/Frame#<'static> {
182 /// Creates a frame from pixels in RGBA format.
183 ///
184 /// This is a lossy method. The `gif` format does not support arbitrary alpha but only a 1-bit
185 /// transparency mask per pixel. Any non-zero alpha value will be interpreted as a fully opaque
186 /// pixel. Additionally, only 256 colors can appear in a single frame. The palette will be
187 /// reduced by the NeuQuant algorithm if necessary. Different frames have independent palettes.
188 ///
189 /// *Note: This method is not optimized for speed.*
190 ///
191 /// # Panics:
192 /// * If the length of pixels does not equal `width * height * 4`.
193 #[cfg(feature = "color_quant")]
194 #[track_caller]
195 pub fn from_rgbarust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`]from_rgba().(width: u16, height: u16, pixels: &mut [u8]) -> Selfrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`] {
196 Framerust-analyzer cargo gif 0.14.2 common/Frame#::from_rgba_speedrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`]from_rgba_speed().(width, height, pixels, 1)
197 }
198
199 /// Creates a frame from pixels in RGBA format.
200 ///
201 /// `speed` is a value in the range [1, 30].
202 /// The higher the value the faster it runs at the cost of image quality.
203 /// A `speed` of 10 is a good compromise between speed and quality.
204 ///
205 /// This is a lossy method. The `gif` format does not support arbitrary alpha but only a 1-bit
206 /// transparency mask per pixel. Any non-zero alpha value will be interpreted as a fully opaque
207 /// pixel. Additionally, only 256 colors can appear in a single frame. The palette will be
208 /// reduced by the NeuQuant algorithm if necessary. Different frames have independent palettes.
209 ///
210 /// # Panics:
211 /// * If the length of pixels does not equal `width * height * 4`.
212 /// * If `speed < 1` or `speed > 30`
213 #[cfg(feature = "color_quant")]
214 #[track_caller]
215 pub fn from_rgba_speedrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`]from_rgba_speed().(width: u16, height: u16, pixels: &mut [u8], speed: i32) -> Selfrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`] {
216 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(width as usize *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). height as usize *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). 4, pixels.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), "Too much or too little pixel data for the given width and height to create a GIF Frame");
217 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(
218 speed >= 1 && speed <= 30,
219 "speed needs to be in the range [1, 30]"
220 );
221 let mut transparent: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<[u8; 4]> = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
222 for pix in pixels.chunks_exact_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact_mut().(4) {
223 if pix[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().3]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 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 {
224 pix[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = 0xFF;
225 continue;
226 }
227
228 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#([r, g, b, a]) = transparent {
229 pix[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = r;
230 pix[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = g;
231 pix[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = b;
232 pix[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = a;
233 } else {
234 transparent = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#([pix[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()., pix[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()., pix[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()., pix[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().]);
235 }
236 }
237
238 // Attempt to build a palette of all colors. If we go over 256 colors,
239 // switch to the NeuQuant algorithm.
240 let mut colors: BTreeSetrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/BTreeSet#<(u8, u8, u8, u8)> = BTreeSetrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/BTreeSet#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/impl#[`BTreeSet<T>`]new().();
241 for pixel in pixels.chunks_exactrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact().(4) {
242 if colors.insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/impl#[`BTreeSet<T, A>`]insert().((pixel[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()., pixel[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()., pixel[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()., pixel[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)) && colors.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/impl#[`BTreeSet<T, A>`]len().() 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().256 {
243 // > 256 colours, let's use NeuQuant.
244 let nq = color_quantrust-analyzer cargo color_quant 1.1.0 crate/::NeuQuantrust-analyzer cargo color_quant 1.1.0 NeuQuant#::newrust-analyzer cargo color_quant 1.1.0 impl#[NeuQuant]new().(speed, 256, pixels);
245
246 return Framerust-analyzer cargo gif 0.14.2 common/Frame# {
247 widthrust-analyzer cargo gif 0.14.2 common/Frame#width.,
248 heightrust-analyzer cargo gif 0.14.2 common/Frame#height.,
249 bufferrust-analyzer cargo gif 0.14.2 common/Frame#buffer.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(
250 pixels
251 .chunks_exactrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact().(4)
252 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|pix| nq.index_ofrust-analyzer cargo color_quant 1.1.0 impl#[NeuQuant]index_of().(pix) as u8)
253 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(),
254 ),
255 paletterust-analyzer cargo gif 0.14.2 common/Frame#palette.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(nq.color_map_rgbrust-analyzer cargo color_quant 1.1.0 impl#[NeuQuant]color_map_rgb().()),
256 transparentrust-analyzer cargo gif 0.14.2 common/Frame#transparent.: transparent.maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|t| nq.index_ofrust-analyzer cargo color_quant 1.1.0 impl#[NeuQuant]index_of().(&t) as u8),
257 ..Framerust-analyzer cargo gif 0.14.2 common/Frame#::defaultrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'_>`][Default]default().()
258 };
259 }
260 }
261
262 // Palette size <= 256 elements, we can build an exact palette.
263 let mut colors_vec: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(u8, u8, u8, u8)> = colors.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/set/impl#[`BTreeSet<T, A>`][IntoIterator]into_iter().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
264 colors_vec.sort_unstablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]sort_unstable().();
265 let palette = colors_vec
266 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
267 .flat_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#flat_map().(|&(r, g, b, _a)| [r, g, b])
268 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
269 let colors_lookup: BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<(u8, u8, u8, u8), u8> =
270 colors_vec.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(0..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#255).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
271
272 let index_of = |pixel: &[u8]| {
273 colors_lookup
274 .getrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]get().(&(pixel[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()., pixel[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()., pixel[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()., pixel[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().))
275 .copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<&T>`]copied().()
276 .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(0)
277 };
278
279 Framerust-analyzer cargo gif 0.14.2 common/Frame# {
280 widthrust-analyzer cargo gif 0.14.2 common/Frame#width.,
281 heightrust-analyzer cargo gif 0.14.2 common/Frame#height.,
282 bufferrust-analyzer cargo gif 0.14.2 common/Frame#buffer.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(pixels.chunks_exactrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact().(4).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(index_of).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()),
283 paletterust-analyzer cargo gif 0.14.2 common/Frame#palette.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(palette),
284 transparentrust-analyzer cargo gif 0.14.2 common/Frame#transparent.: transparent.maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|t| index_of(&t)),
285 ..Framerust-analyzer cargo gif 0.14.2 common/Frame#::defaultrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'_>`][Default]default().()
286 }
287 }
288
289 /// Creates a frame from pixels in LumaAlpha format (grayscale pixels with transparency).
290 ///
291 /// This is a lossy method. The `gif` format does not support arbitrary alpha but only a 1-bit
292 /// transparency mask per pixel. Any non-zero alpha value will be interpreted as a fully opaque
293 /// pixel. The least used color will be used to indicate alpha and replace with the closest other color
294 /// in the image. Different frames have independent palettes.
295 ///
296 /// # Panics:
297 /// * If the length of pixels does not equal `width * height * 2`.
298 pub fn from_grayscale_with_alpharust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`]from_grayscale_with_alpha().(width: u16, height: u16, pixels: &[u8]) -> Selfrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`] {
299 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(width as usize *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). height as usize *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). 2, pixels.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), "Too much or too little pixel data for the given width and height to create a GIF Frame");
300
301 // Input is in LumaA format.
302 // Count the occurrences of all the colors, then pick the least common color as alpha.
303 let mut num_transparent_pixels: u32 = 0;
304 let mut color_frequencies: [u32; 256] = [0; 256];
305 for pixel in pixels.chunks_exactrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact().(2) {
306 let color = pixel[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().;
307 let alpha = pixel[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().;
308 // do not count colors in fully transparent pixels
309 if alpha 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().0 {
310 num_transparent_pixels rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().1;
311 } else {
312 color_frequencies[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().color as usize]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/arith/impl#[u32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().1;
313 }
314 }
315
316 let grayscale_palette: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = (0..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#255).flat_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#flat_map().(|i| [i, i, i]).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
317
318 // If there were no fully transparent pixels, do not allocate a color to transparency in the GIF
319 // and return immediately with the generic grayscale palette
320 if num_transparent_pixels rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().0 {
321 let stripped_alpha: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = pixels.chunks_exactrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact().(2).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|pixel| pixel[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().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
322 return Framerust-analyzer cargo gif 0.14.2 common/Frame# {
323 widthrust-analyzer cargo gif 0.14.2 common/Frame#width.,
324 heightrust-analyzer cargo gif 0.14.2 common/Frame#height.,
325 bufferrust-analyzer cargo gif 0.14.2 common/Frame#buffer.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(stripped_alpha),
326 paletterust-analyzer cargo gif 0.14.2 common/Frame#palette.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(grayscale_palette),
327 transparentrust-analyzer cargo gif 0.14.2 common/Frame#transparent.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
328 ..Framerust-analyzer cargo gif 0.14.2 common/Frame#::defaultrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'_>`][Default]default().()
329 };
330 }
331
332 // Choose the color that will be our alpha color
333 let least_used_color = color_frequencies
334 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
335 .enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().()
336 .min_by_keyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#min_by_key().(|(_, &value)| value)
337 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|(index, _)| index as u8)
338 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("input slice is empty");
339
340 // pick the less used color out of the neighbours as the replacement color
341 let replacement_color = if least_used_color 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().255 {
342 254
343 } else if least_used_color 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().0 {
344 1
345 } else if color_frequencies[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().(least_used_color 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) as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().
346 <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]lt().color_frequencies[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().(least_used_color rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().1) as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().
347 {
348 least_used_color 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
349 } else {
350 least_used_color rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().1
351 };
352
353 // Strip alpha and replace fully transparent pixels with the chosen color
354 let paletted: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = pixels
355 .chunks_exactrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact().(2)
356 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|pixel| {
357 let color = pixel[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().;
358 let alpha = pixel[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().;
359 if alpha 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().0 {
360 least_used_color
361 } else if color 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().least_used_color {
362 replacement_color
363 } else {
364 color
365 }
366 })
367 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
368
369 Framerust-analyzer cargo gif 0.14.2 common/Frame# {
370 widthrust-analyzer cargo gif 0.14.2 common/Frame#width.,
371 heightrust-analyzer cargo gif 0.14.2 common/Frame#height.,
372 bufferrust-analyzer cargo gif 0.14.2 common/Frame#buffer.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(paletted),
373 paletterust-analyzer cargo gif 0.14.2 common/Frame#palette.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(grayscale_palette),
374 transparentrust-analyzer cargo gif 0.14.2 common/Frame#transparent.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(least_used_color),
375 ..Framerust-analyzer cargo gif 0.14.2 common/Frame#::defaultrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'_>`][Default]default().()
376 }
377 }
378
379 /// Creates a frame from a palette and indexed pixels.
380 ///
381 /// # Panics:
382 /// * If the length of pixels does not equal `width * height`.
383 /// * If the length of palette > `256 * 3`.
384 #[track_caller]
385 pub fn from_palette_pixelsrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`]from_palette_pixels().(
386 width: u16,
387 height: u16,
388 pixels: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>>,
389 palette: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>>,
390 transparent: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u8>,
391 ) -> Selfrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`] {
392 let pixels = pixels.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().();
393 let palette = palette.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().();
394 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
395 width as usize *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). height as usize,
396 pixels.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(),
397 "Too many or too little pixels for the given width and height to create a GIF Frame"
398 );
399 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(
400 palette.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() <= 256 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). 3,
401 "Too many palette values to create a GIF Frame"
402 );
403
404 Framerust-analyzer cargo gif 0.14.2 common/Frame# {
405 widthrust-analyzer cargo gif 0.14.2 common/Frame#width.,
406 heightrust-analyzer cargo gif 0.14.2 common/Frame#height.,
407 bufferrust-analyzer cargo gif 0.14.2 common/Frame#buffer.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(pixels),
408 paletterust-analyzer cargo gif 0.14.2 common/Frame#palette.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(palette),
409 transparentrust-analyzer cargo gif 0.14.2 common/Frame#transparent.,
410 ..Framerust-analyzer cargo gif 0.14.2 common/Frame#::defaultrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'_>`][Default]default().()
411 }
412 }
413
414 /// Creates a frame from indexed pixels in the global palette.
415 ///
416 /// # Panics:
417 /// * If the length of pixels does not equal `width * height`.
418 #[track_caller]
419 pub fn from_indexed_pixelsrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`]from_indexed_pixels().(
420 width: u16,
421 height: u16,
422 pixels: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>>,
423 transparent: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u8>,
424 ) -> Selfrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`] {
425 let pixels = pixels.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().();
426 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
427 width as usize *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). height as usize,
428 pixels.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(),
429 "Too many or too little pixels for the given width and height to create a GIF Frame"
430 );
431
432 Framerust-analyzer cargo gif 0.14.2 common/Frame# {
433 widthrust-analyzer cargo gif 0.14.2 common/Frame#width.,
434 heightrust-analyzer cargo gif 0.14.2 common/Frame#height.,
435 bufferrust-analyzer cargo gif 0.14.2 common/Frame#buffer.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(pixels),
436 paletterust-analyzer cargo gif 0.14.2 common/Frame#palette.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
437 transparentrust-analyzer cargo gif 0.14.2 common/Frame#transparent.,
438 ..Framerust-analyzer cargo gif 0.14.2 common/Frame#::defaultrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'_>`][Default]default().()
439 }
440 }
441
442 /// Creates a frame from pixels in RGB format.
443 ///
444 /// This is a lossy method. In the `gif` format only 256 colors can appear in a single frame.
445 /// The palette will be reduced by the NeuQuant algorithm if necessary. Different frames have
446 /// independent palettes.
447 ///
448 /// *Note: This method is not optimized for speed.*
449 ///
450 /// # Panics:
451 /// * If the length of pixels does not equal `width * height * 3`.
452 #[cfg(feature = "color_quant")]
453 #[must_use]
454 #[track_caller]
455 pub fn from_rgbrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`]from_rgb().(width: u16, height: u16, pixels: &[u8]) -> Selfrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`] {
456 Framerust-analyzer cargo gif 0.14.2 common/Frame#::from_rgb_speedrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`]from_rgb_speed().(width, height, pixels, 1)
457 }
458
459 /// Creates a frame from pixels in RGB format.
460 ///
461 /// `speed` is a value in the range [1, 30].
462 ///
463 /// This is a lossy method. In the `gif` format only 256 colors can appear in a single frame.
464 /// The palette will be reduced by the NeuQuant algorithm if necessary. Different frames have
465 /// independent palettes.
466 ///
467 /// The higher the value the faster it runs at the cost of image quality.
468 /// A `speed` of 10 is a good compromise between speed and quality.
469 ///
470 /// # Panics:
471 /// * If the length of pixels does not equal `width * height * 3`.
472 /// * If `speed < 1` or `speed > 30`
473 #[cfg(feature = "color_quant")]
474 #[must_use]
475 #[track_caller]
476 pub fn from_rgb_speedrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`]from_rgb_speed().(width: u16, height: u16, pixels: &[u8], speed: i32) -> Selfrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`] {
477 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(width as usize *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). height as usize *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). 3, pixels.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), "Too much or too little pixel data for the given width and height to create a GIF Frame");
478 let mut vec: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
479 vec.try_reserve_exactrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]try_reserve_exact().(pixels.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][`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().width as usize 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().height as usize)
480 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("OOM");
481 for v in pixels.chunks_exactrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact().(3) {
482 vec.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(&[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()., 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()., 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()., 0xFF]);
483 }
484 Framerust-analyzer cargo gif 0.14.2 common/Frame#::from_rgba_speedrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`]from_rgba_speed().(width, height, &mut vec, speed)
485 }
486
487 /// Leaves empty buffer and empty palette behind
488 #[inline]
489 pub(craterust-analyzer cargo gif 0.14.2 crate/) fn takerust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`]take().(&mut self) -> Selfrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`] {
490 Framerust-analyzer cargo gif 0.14.2 common/Frame# {
491 delayrust-analyzer cargo gif 0.14.2 common/Frame#delay.: self.delayrust-analyzer cargo gif 0.14.2 common/Frame#delay.,
492 disposerust-analyzer cargo gif 0.14.2 common/Frame#dispose.: self.disposerust-analyzer cargo gif 0.14.2 common/Frame#dispose.,
493 transparentrust-analyzer cargo gif 0.14.2 common/Frame#transparent.: self.transparentrust-analyzer cargo gif 0.14.2 common/Frame#transparent.,
494 needs_user_inputrust-analyzer cargo gif 0.14.2 common/Frame#needs_user_input.: self.needs_user_inputrust-analyzer cargo gif 0.14.2 common/Frame#needs_user_input.,
495 toprust-analyzer cargo gif 0.14.2 common/Frame#top.: self.toprust-analyzer cargo gif 0.14.2 common/Frame#top.,
496 leftrust-analyzer cargo gif 0.14.2 common/Frame#left.: self.leftrust-analyzer cargo gif 0.14.2 common/Frame#left.,
497 widthrust-analyzer cargo gif 0.14.2 common/Frame#width.: self.widthrust-analyzer cargo gif 0.14.2 common/Frame#width.,
498 heightrust-analyzer cargo gif 0.14.2 common/Frame#height.: self.heightrust-analyzer cargo gif 0.14.2 common/Frame#height.,
499 interlacedrust-analyzer cargo gif 0.14.2 common/Frame#interlaced.: self.interlacedrust-analyzer cargo gif 0.14.2 common/Frame#interlaced.,
500 paletterust-analyzer cargo gif 0.14.2 common/Frame#palette.: corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/take().(&mut self.paletterust-analyzer cargo gif 0.14.2 common/Frame#palette.),
501 bufferrust-analyzer cargo gif 0.14.2 common/Frame#buffer.: corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::replacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/replace().(&mut self.bufferrust-analyzer cargo gif 0.14.2 common/Frame#buffer., Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(&[])),
502 }
503 }
504 }
505
506 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
507 #[cfg(feature = "color_quant")]
508 // Creating the `colors_lookup` hashmap in Frame::from_rgba_speed panics due to
509 // overflow while bypassing NeuQuant and zipping a RangeFrom with 256 colors.
510 // Changing .zip(0_u8..) to .zip(0_u8..=255) fixes this issue.
511 fn rgba_speed_avoid_panic_256_colorsrust-analyzer cargo gif 0.14.2 common/rgba_speed_avoid_panic_256_colors().() {
512 let side = 16;
513 let pixel_data: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = (0..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#255).flat_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#flat_map().(|a| [a, a, a]).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
514 let _ = Framerust-analyzer cargo gif 0.14.2 common/Frame#::from_rgbrust-analyzer cargo gif 0.14.2 common/impl#[`Frame<'static>`]from_rgb().(side, side, &pixel_data);
515 }