| 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 | #[cfg(feature = "std")] |
| 4 | 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/::Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#; |
| 5 | |
| 6 | // TODO: can be removed once https://github.com/rust-lang/rust/issues/74985 is stable |
| 7 | use bytemuckrust-analyzer cargo bytemuck 1.25.2 crate/::{cast_slice_mutrust-analyzer cargo bytemuck 1.25.2 cast_slice_mut()., 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/::{ |
| 10 | QOI_HEADER_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_HEADER_SIZE., QOI_OP_DIFFrust-analyzer cargo qoi 0.4.1 consts/QOI_OP_DIFF., QOI_OP_INDEXrust-analyzer cargo qoi 0.4.1 consts/QOI_OP_INDEX., QOI_OP_LUMArust-analyzer cargo qoi 0.4.1 consts/QOI_OP_LUMA., QOI_OP_RGBrust-analyzer cargo qoi 0.4.1 consts/QOI_OP_RGB., QOI_OP_RGBArust-analyzer cargo qoi 0.4.1 consts/QOI_OP_RGBA., QOI_OP_RUNrust-analyzer cargo qoi 0.4.1 consts/QOI_OP_RUN., |
| 11 | QOI_PADDINGrust-analyzer cargo qoi 0.4.1 consts/QOI_PADDING., QOI_PADDING_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_PADDING_SIZE., |
| 12 | }; |
| 13 | 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#}; |
| 14 | 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#; |
| 15 | 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#}; |
| 16 | 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#; |
| 17 | use craterust-analyzer cargo qoi 0.4.1 crate/::utilsrust-analyzer cargo qoi 0.4.1 utils/::{coldrust-analyzer cargo qoi 0.4.1 utils/cold()., unlikelyrust-analyzer cargo qoi 0.4.1 utils/unlikely().}; |
| 18 | |
| 19 | const QOI_OP_INDEX_ENDrust-analyzer cargo qoi 0.4.1 decode/QOI_OP_INDEX_END.: u8 = 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().0x3f; |
| 20 | const QOI_OP_RUN_ENDrust-analyzer cargo qoi 0.4.1 decode/QOI_OP_RUN_END.: u8 = 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().0x3d; // <- note, 0x3d (not 0x3f) |
| 21 | const QOI_OP_DIFF_ENDrust-analyzer cargo qoi 0.4.1 decode/QOI_OP_DIFF_END.: u8 = QOI_OP_DIFFrust-analyzer cargo qoi 0.4.1 consts/QOI_OP_DIFF. 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().0x3f; |
| 22 | const QOI_OP_LUMA_ENDrust-analyzer cargo qoi 0.4.1 decode/QOI_OP_LUMA_END.: u8 = QOI_OP_LUMArust-analyzer cargo qoi 0.4.1 consts/QOI_OP_LUMA. 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().0x3f; |
| 23 | |
| 24 | #[inline] |
| 25 | fn decode_impl_slicerust-analyzer cargo qoi 0.4.1 decode/decode_impl_slice().<const N: usize, const RGBA: bool>(data: &[u8], out: &mut [u8]) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<usize> |
| 26 | where |
| 27 | Pixelrust-analyzer cargo qoi 0.4.1 pixel/Pixel#<N>: SupportedChannelsrust-analyzer cargo qoi 0.4.1 pixel/SupportedChannels#, |
| 28 | [u8; N]: Podrust-analyzer cargo bytemuck 1.25.2 pod/Pod#, |
| 29 | { |
| 30 | let mut pixels = cast_slice_mutrust-analyzer cargo bytemuck 1.25.2 cast_slice_mut().::<_, [u8; N]>(out); |
| 31 | let data_len = data.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 32 | let mut data = data; |
| 33 | |
| 34 | let mut index = [Pixelrust-analyzer cargo qoi 0.4.1 pixel/Pixel#::<4>::newrust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]new().(); 256]; |
| 35 | 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); |
| 36 | let mut px_rgba: Pixelrust-analyzer cargo qoi 0.4.1 pixel/Pixel#<4>; |
| 37 | |
| 38 | while let [px_out, ptail @ ..] = pixels { |
| 39 | pixels = ptail; |
| 40 | match data { |
| 41 | [b1 @ 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/range/RangeInclusive#QOI_OP_INDEX_ENDrust-analyzer cargo qoi 0.4.1 decode/QOI_OP_INDEX_END., dtail @ ..] => { |
| 42 | px_rgba = index[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().b1 as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().; |
| 43 | px.updaterust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]update().(px_rgba); |
| 44 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().px_out = px.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(); |
| 45 | data = dtail; |
| 46 | continue; |
| 47 | } |
| 48 | [QOI_OP_RGBrust-analyzer cargo qoi 0.4.1 consts/QOI_OP_RGB., r, g, b, dtail @ ..] => { |
| 49 | px.update_rgbrust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]update_rgb().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().r, *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().g, *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().b); |
| 50 | data = dtail; |
| 51 | } |
| 52 | [QOI_OP_RGBArust-analyzer cargo qoi 0.4.1 consts/QOI_OP_RGBA., r, g, b, a, dtail @ ..] if RGBA => { |
| 53 | px.update_rgbarust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]update_rgba().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().r, *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().g, *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().b, *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().a); |
| 54 | data = dtail; |
| 55 | } |
| 56 | [b1 @ 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/range/RangeInclusive#QOI_OP_RUN_ENDrust-analyzer cargo qoi 0.4.1 decode/QOI_OP_RUN_END., dtail @ ..] => { |
| 57 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().px_out = px.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(); |
| 58 | let run = ((b1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[`&u8`][`BitAnd<u8>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[`&u8`][`BitAnd<u8>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[`&u8`][`BitAnd<u8>`]bitand().0x3f) as usize).minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(pixels.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 59 | let (phead, ptail) = pixels.split_at_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]split_at_mut().(run); // can't panic |
| 60 | phead.fillrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]fill().(px.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 61 | pixels = ptail; |
| 62 | data = dtail; |
| 63 | continue; |
| 64 | } |
| 65 | [b1 @ QOI_OP_DIFFrust-analyzer cargo qoi 0.4.1 consts/QOI_OP_DIFF...=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#QOI_OP_DIFF_ENDrust-analyzer cargo qoi 0.4.1 decode/QOI_OP_DIFF_END., dtail @ ..] => { |
| 66 | px.update_diffrust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]update_diff().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().b1); |
| 67 | data = dtail; |
| 68 | } |
| 69 | [b1 @ QOI_OP_LUMArust-analyzer cargo qoi 0.4.1 consts/QOI_OP_LUMA...=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#QOI_OP_LUMA_ENDrust-analyzer cargo qoi 0.4.1 decode/QOI_OP_LUMA_END., b2, dtail @ ..] => { |
| 70 | px.update_lumarust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]update_luma().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().b1, *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().b2); |
| 71 | data = dtail; |
| 72 | } |
| 73 | _ => { |
| 74 | coldrust-analyzer cargo qoi 0.4.1 utils/cold().(); |
| 75 | 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_PADDING_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_PADDING_SIZE.) { |
| 76 | 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#); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | px_rgba = px.as_rgbarust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]as_rgba().(0xff); |
| 82 | index[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().px_rgba.hash_indexrust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]hash_index().() as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut(). = px_rgba; |
| 83 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().px_out = px.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(); |
| 84 | } |
| 85 | |
| 86 | 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_PADDING_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_PADDING_SIZE.) { |
| 87 | 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#); |
| 88 | } else if unlikelyrust-analyzer cargo qoi 0.4.1 utils/unlikely().(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#QOI_PADDING_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_PADDING_SIZE.]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 array/equality/impl#[`[T]`][`PartialEq<[U; N]>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/equality/impl#[`[T]`][`PartialEq<[U; N]>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/equality/impl#[`[T]`][`PartialEq<[U; N]>`]ne().QOI_PADDINGrust-analyzer cargo qoi 0.4.1 consts/QOI_PADDING.) { |
| 89 | 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#::InvalidPaddingrust-analyzer cargo qoi 0.4.1 error/Error#InvalidPadding#); |
| 90 | } |
| 91 | |
| 92 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(data_len.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(data.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()).saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(QOI_PADDING_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_PADDING_SIZE.)) |
| 93 | } |
| 94 | |
| 95 | #[inline] |
| 96 | fn decode_impl_slice_allrust-analyzer cargo qoi 0.4.1 decode/decode_impl_slice_all().( |
| 97 | data: &[u8], out: &mut [u8], channels: u8, src_channels: u8, |
| 98 | ) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<usize> { |
| 99 | match (channels, src_channels) { |
| 100 | (3, 3) => decode_impl_slicerust-analyzer cargo qoi 0.4.1 decode/decode_impl_slice().::<3, false>(data, out), |
| 101 | (3, 4) => decode_impl_slicerust-analyzer cargo qoi 0.4.1 decode/decode_impl_slice().::<3, true>(data, out), |
| 102 | (4, 3) => decode_impl_slicerust-analyzer cargo qoi 0.4.1 decode/decode_impl_slice().::<4, false>(data, out), |
| 103 | (4, 4) => decode_impl_slicerust-analyzer cargo qoi 0.4.1 decode/decode_impl_slice().::<4, true>(data, out), |
| 104 | _ => { |
| 105 | coldrust-analyzer cargo qoi 0.4.1 utils/cold().(); |
| 106 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo qoi 0.4.1 error/Error#::InvalidChannelsrust-analyzer cargo qoi 0.4.1 error/Error#InvalidChannels# { channelsrust-analyzer cargo qoi 0.4.1 error/Error#InvalidChannels#channels. }) |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// Decode the image into a pre-allocated buffer. |
| 112 | /// |
| 113 | /// Note: the resulting number of channels will match the header. In order to change |
| 114 | /// the number of channels, use [`Decoder::with_channels`]. |
| 115 | #[inline] |
| 116 | pub fn decode_to_bufrust-analyzer cargo qoi 0.4.1 decode/decode_to_buf().(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]>) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<Headerrust-analyzer cargo qoi 0.4.1 header/Header#> { |
| 117 | let mut decoder = Decoderrust-analyzer cargo qoi 0.4.1 decode/Decoder#::newrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<Bytes<'a>>`]new().(&data)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 118 | decoder.decode_to_bufrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]decode_to_buf().(buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 119 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().decoder.headerrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]header().()) |
| 120 | } |
| 121 | |
| 122 | /// Decode the image into a newly allocated vector. |
| 123 | /// |
| 124 | /// Note: the resulting number of channels will match the header. In order to change |
| 125 | /// the number of channels, use [`Decoder::with_channels`]. |
| 126 | #[cfg(any(feature = "std", feature = "alloc"))] |
| 127 | #[inline] |
| 128 | pub fn decode_to_vecrust-analyzer cargo qoi 0.4.1 decode/decode_to_vec().(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#<(Headerrust-analyzer cargo qoi 0.4.1 header/Header#, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>)> { |
| 129 | let mut decoder = Decoderrust-analyzer cargo qoi 0.4.1 decode/Decoder#::newrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<Bytes<'a>>`]new().(&data)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 130 | let out = decoder.decode_to_vecrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]decode_to_vec().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 131 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().decoder.headerrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]header().(), out)) |
| 132 | } |
| 133 | |
| 134 | /// Decode the image header from a slice of bytes. |
| 135 | #[inline] |
| 136 | pub fn decode_headerrust-analyzer cargo qoi 0.4.1 decode/decode_header().(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#<Headerrust-analyzer cargo qoi 0.4.1 header/Header#> { |
| 137 | Headerrust-analyzer cargo qoi 0.4.1 header/Header#::decoderust-analyzer cargo qoi 0.4.1 header/impl#[Header]decode().(data) |
| 138 | } |
| 139 | |
| 140 | #[cfg(any(feature = "std"))] |
| 141 | #[inline] |
| 142 | fn decode_impl_streamrust-analyzer cargo qoi 0.4.1 decode/decode_impl_stream().<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, const N: usize, const RGBA: bool>( |
| 143 | data: &mut R, out: &mut [u8], |
| 144 | ) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<()> |
| 145 | where |
| 146 | Pixelrust-analyzer cargo qoi 0.4.1 pixel/Pixel#<N>: SupportedChannelsrust-analyzer cargo qoi 0.4.1 pixel/SupportedChannels#, |
| 147 | [u8; N]: Podrust-analyzer cargo bytemuck 1.25.2 pod/Pod#, |
| 148 | { |
| 149 | let mut pixels = cast_slice_mutrust-analyzer cargo bytemuck 1.25.2 cast_slice_mut().::<_, [u8; N]>(out); |
| 150 | |
| 151 | let mut index = [Pixelrust-analyzer cargo qoi 0.4.1 pixel/Pixel#::<N>::newrust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]new().(); 256]; |
| 152 | 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); |
| 153 | |
| 154 | while let [px_out, ptail @ ..] = pixels { |
| 155 | pixels = ptail; |
| 156 | let mut p = [0]; |
| 157 | data.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut p)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 158 | let [b1] = p; |
| 159 | match b1 { |
| 160 | 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/range/RangeInclusive#QOI_OP_INDEX_ENDrust-analyzer cargo qoi 0.4.1 decode/QOI_OP_INDEX_END. => { |
| 161 | px = index[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().b1 as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().; |
| 162 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().px_out = px.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(); |
| 163 | continue; |
| 164 | } |
| 165 | QOI_OP_RGBrust-analyzer cargo qoi 0.4.1 consts/QOI_OP_RGB. => { |
| 166 | let mut p = [0; 3]; |
| 167 | data.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut p)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 168 | px.update_rgbrust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]update_rgb().(p[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., p[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., p[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().); |
| 169 | } |
| 170 | QOI_OP_RGBArust-analyzer cargo qoi 0.4.1 consts/QOI_OP_RGBA. if RGBA => { |
| 171 | let mut p = [0; 4]; |
| 172 | data.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut p)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 173 | px.update_rgbarust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]update_rgba().(p[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., p[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., p[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., p[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().); |
| 174 | } |
| 175 | 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/range/RangeInclusive#QOI_OP_RUN_ENDrust-analyzer cargo qoi 0.4.1 decode/QOI_OP_RUN_END. => { |
| 176 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().px_out = px.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(); |
| 177 | let run = ((b1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().0x3f) as usize).minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(pixels.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 178 | let (phead, ptail) = pixels.split_at_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]split_at_mut().(run); // can't panic |
| 179 | phead.fillrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]fill().(px.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 180 | pixels = ptail; |
| 181 | continue; |
| 182 | } |
| 183 | QOI_OP_DIFFrust-analyzer cargo qoi 0.4.1 consts/QOI_OP_DIFF...=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#QOI_OP_DIFF_ENDrust-analyzer cargo qoi 0.4.1 decode/QOI_OP_DIFF_END. => { |
| 184 | px.update_diffrust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]update_diff().(b1); |
| 185 | } |
| 186 | QOI_OP_LUMArust-analyzer cargo qoi 0.4.1 consts/QOI_OP_LUMA...=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#QOI_OP_LUMA_ENDrust-analyzer cargo qoi 0.4.1 decode/QOI_OP_LUMA_END. => { |
| 187 | let mut p = [0]; |
| 188 | data.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut p)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 189 | let [b2] = p; |
| 190 | px.update_lumarust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]update_luma().(b1, b2); |
| 191 | } |
| 192 | _ => { |
| 193 | coldrust-analyzer cargo qoi 0.4.1 utils/cold().(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | index[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().px.hash_indexrust-analyzer cargo qoi 0.4.1 pixel/impl#[`Pixel<N>`]hash_index().() as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut(). = px; |
| 198 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().px_out = px.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(); |
| 199 | } |
| 200 | |
| 201 | let mut p = [0_u8; QOI_PADDING_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_PADDING_SIZE.]; |
| 202 | data.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut p)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 203 | if unlikelyrust-analyzer cargo qoi 0.4.1 utils/unlikely().(p rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/equality/impl#[`[T; N]`][`PartialEq<[U; N]>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/equality/impl#[`[T; N]`][`PartialEq<[U; N]>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/equality/impl#[`[T; N]`][`PartialEq<[U; N]>`]ne().QOI_PADDINGrust-analyzer cargo qoi 0.4.1 consts/QOI_PADDING.) { |
| 204 | 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#::InvalidPaddingrust-analyzer cargo qoi 0.4.1 error/Error#InvalidPadding#); |
| 205 | } |
| 206 | |
| 207 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 208 | } |
| 209 | |
| 210 | #[cfg(feature = "std")] |
| 211 | #[inline] |
| 212 | fn decode_impl_stream_allrust-analyzer cargo qoi 0.4.1 decode/decode_impl_stream_all().<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#>( |
| 213 | data: &mut R, out: &mut [u8], channels: u8, src_channels: u8, |
| 214 | ) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<()> { |
| 215 | match (channels, src_channels) { |
| 216 | (3, 3) => decode_impl_streamrust-analyzer cargo qoi 0.4.1 decode/decode_impl_stream().::<_, 3, false>(data, out), |
| 217 | (3, 4) => decode_impl_streamrust-analyzer cargo qoi 0.4.1 decode/decode_impl_stream().::<_, 3, true>(data, out), |
| 218 | (4, 3) => decode_impl_streamrust-analyzer cargo qoi 0.4.1 decode/decode_impl_stream().::<_, 4, false>(data, out), |
| 219 | (4, 4) => decode_impl_streamrust-analyzer cargo qoi 0.4.1 decode/decode_impl_stream().::<_, 4, true>(data, out), |
| 220 | _ => { |
| 221 | coldrust-analyzer cargo qoi 0.4.1 utils/cold().(); |
| 222 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo qoi 0.4.1 error/Error#::InvalidChannelsrust-analyzer cargo qoi 0.4.1 error/Error#InvalidChannels# { channelsrust-analyzer cargo qoi 0.4.1 error/Error#InvalidChannels#channels. }) |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | #[doc(hidden)] |
| 228 | pub trait Readerrust-analyzer cargo qoi 0.4.1 decode/Reader#: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# { |
| 229 | fn decode_headerrust-analyzer cargo qoi 0.4.1 decode/Reader#decode_header().(&mut self) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<Headerrust-analyzer cargo qoi 0.4.1 header/Header#>; |
| 230 | fn decode_imagerust-analyzer cargo qoi 0.4.1 decode/Reader#decode_image().(&mut self, out: &mut [u8], channels: u8, src_channels: u8) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<()>; |
| 231 | } |
| 232 | |
| 233 | pub struct Bytesrust-analyzer cargo qoi 0.4.1 decode/Bytes#<'a>(&'a [u8]); |
| 234 | |
| 235 | impl<'a> Bytesrust-analyzer cargo qoi 0.4.1 decode/Bytes#<'a> { |
| 236 | #[inline] |
| 237 | pub const fn newrust-analyzer cargo qoi 0.4.1 decode/impl#[`Bytes<'a>`]new().(buf: &'a [u8]) -> Selfrust-analyzer cargo qoi 0.4.1 decode/impl#[`Bytes<'a>`] { |
| 238 | Selfrust-analyzer cargo qoi 0.4.1 decode/impl#[`Bytes<'a>`](buf) |
| 239 | } |
| 240 | |
| 241 | #[inline] |
| 242 | pub const fn as_slicerust-analyzer cargo qoi 0.4.1 decode/impl#[`Bytes<'a>`]as_slice().(&self) -> &[u8] { |
| 243 | self.0rust-analyzer cargo qoi 0.4.1 decode/Bytes#0. |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | impl<'a> Readerrust-analyzer cargo qoi 0.4.1 decode/Reader# for Bytesrust-analyzer cargo qoi 0.4.1 decode/Bytes#<'a> { |
| 248 | #[inline] |
| 249 | fn decode_headerrust-analyzer cargo qoi 0.4.1 decode/impl#[`Bytes<'a>`][Reader]decode_header().(&mut self) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<Headerrust-analyzer cargo qoi 0.4.1 header/Header#> { |
| 250 | let header = Headerrust-analyzer cargo qoi 0.4.1 header/Header#::decoderust-analyzer cargo qoi 0.4.1 header/impl#[Header]decode().(self.0rust-analyzer cargo qoi 0.4.1 decode/Bytes#0.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 251 | self.0rust-analyzer cargo qoi 0.4.1 decode/Bytes#0. = &self.0rust-analyzer cargo qoi 0.4.1 decode/Bytes#0.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().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/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; // can't panic |
| 252 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(header) |
| 253 | } |
| 254 | |
| 255 | #[inline] |
| 256 | fn decode_imagerust-analyzer cargo qoi 0.4.1 decode/impl#[`Bytes<'a>`][Reader]decode_image().(&mut self, out: &mut [u8], channels: u8, src_channels: u8) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<()> { |
| 257 | let n_read = decode_impl_slice_allrust-analyzer cargo qoi 0.4.1 decode/decode_impl_slice_all().(self.0rust-analyzer cargo qoi 0.4.1 decode/Bytes#0., out, channels, src_channels)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 258 | self.0rust-analyzer cargo qoi 0.4.1 decode/Bytes#0. = &self.0rust-analyzer cargo qoi 0.4.1 decode/Bytes#0.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().n_read..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 259 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | #[cfg(feature = "std")] |
| 264 | impl<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#> Readerrust-analyzer cargo qoi 0.4.1 decode/Reader# for R { |
| 265 | #[inline] |
| 266 | fn decode_headerrust-analyzer cargo qoi 0.4.1 decode/impl#[R][Reader]decode_header().(&mut self) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<Headerrust-analyzer cargo qoi 0.4.1 header/Header#> { |
| 267 | let mut b = [0; QOI_HEADER_SIZErust-analyzer cargo qoi 0.4.1 consts/QOI_HEADER_SIZE.]; |
| 268 | self.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut b)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 269 | Headerrust-analyzer cargo qoi 0.4.1 header/Header#::decoderust-analyzer cargo qoi 0.4.1 header/impl#[Header]decode().(b) |
| 270 | } |
| 271 | |
| 272 | #[inline] |
| 273 | fn decode_imagerust-analyzer cargo qoi 0.4.1 decode/impl#[R][Reader]decode_image().(&mut self, out: &mut [u8], channels: u8, src_channels: u8) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<()> { |
| 274 | decode_impl_stream_allrust-analyzer cargo qoi 0.4.1 decode/decode_impl_stream_all().(self, out, channels, src_channels) |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /// Decode QOI images from slices or from streams. |
| 279 | #[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:)] |
| 280 | pub struct Decoderrust-analyzer cargo qoi 0.4.1 decode/Decoder#<R> { |
| 281 | readerrust-analyzer cargo qoi 0.4.1 decode/Decoder#reader.: R, |
| 282 | headerrust-analyzer cargo qoi 0.4.1 decode/Decoder#header.: Headerrust-analyzer cargo qoi 0.4.1 header/Header#, |
| 283 | channelsrust-analyzer cargo qoi 0.4.1 decode/Decoder#channels.: Channelsrust-analyzer cargo qoi 0.4.1 types/Channels#, |
| 284 | } |
| 285 | |
| 286 | impl<'a> Decoderrust-analyzer cargo qoi 0.4.1 decode/Decoder#<Bytesrust-analyzer cargo qoi 0.4.1 decode/Bytes#<'a>> { |
| 287 | /// Creates a new decoder from a slice of bytes. |
| 288 | /// |
| 289 | /// The header will be decoded immediately upon construction. |
| 290 | /// |
| 291 | /// Note: this provides the most efficient decoding, but requires the source data to |
| 292 | /// be loaded in memory in order to decode it. In order to decode from a generic |
| 293 | /// stream, use [`Decoder::from_stream`] instead. |
| 294 | #[inline] |
| 295 | pub fn newrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<Bytes<'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#)) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<Selfrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<Bytes<'a>>`]> { |
| 296 | Selfrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<Bytes<'a>>`]::new_implrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]new_impl().(Bytesrust-analyzer cargo qoi 0.4.1 decode/Bytes#::newrust-analyzer cargo qoi 0.4.1 decode/impl#[`Bytes<'a>`]new().(data.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().())) |
| 297 | } |
| 298 | |
| 299 | /// Returns the undecoded tail of the input slice of bytes. |
| 300 | #[inline] |
| 301 | pub const fn datarust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<Bytes<'a>>`]data().(&self) -> &[u8] { |
| 302 | self.readerrust-analyzer cargo qoi 0.4.1 decode/Decoder#reader..as_slicerust-analyzer cargo qoi 0.4.1 decode/impl#[`Bytes<'a>`]as_slice().() |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | #[cfg(feature = "std")] |
| 307 | impl<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#> Decoderrust-analyzer cargo qoi 0.4.1 decode/Decoder#<R> { |
| 308 | /// Creates a new decoder from a generic reader that implements [`Read`](std::io::Read). |
| 309 | /// |
| 310 | /// The header will be decoded immediately upon construction. |
| 311 | /// |
| 312 | /// Note: while it's possible to pass a `&[u8]` slice here since it implements `Read`, it |
| 313 | /// would be more efficient to use a specialized constructor instead: [`Decoder::new`]. |
| 314 | #[inline] |
| 315 | pub fn from_streamrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]from_stream().(reader: R) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<Selfrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]> { |
| 316 | Selfrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]::new_implrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]new_impl().(reader) |
| 317 | } |
| 318 | |
| 319 | /// Returns an immutable reference to the underlying reader. |
| 320 | #[inline] |
| 321 | pub const fn readerrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]reader().(&self) -> &R { |
| 322 | &self.readerrust-analyzer cargo qoi 0.4.1 decode/Decoder#reader. |
| 323 | } |
| 324 | |
| 325 | /// Consumes the decoder and returns the underlying reader back. |
| 326 | #[inline] |
| 327 | #[allow(clippy::missing_const_for_fn)] |
| 328 | pub fn into_readerrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]into_reader().(self) -> R { |
| 329 | self.readerrust-analyzer cargo qoi 0.4.1 decode/Decoder#reader. |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | impl<R: Readerrust-analyzer cargo qoi 0.4.1 decode/Reader#> Decoderrust-analyzer cargo qoi 0.4.1 decode/Decoder#<R> { |
| 334 | #[inline] |
| 335 | fn new_implrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]new_impl().(mut reader: R) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<Selfrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]> { |
| 336 | let header = reader.decode_headerrust-analyzer cargo qoi 0.4.1 decode/Reader#decode_header().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 337 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`] { readerrust-analyzer cargo qoi 0.4.1 decode/Decoder#reader., headerrust-analyzer cargo qoi 0.4.1 decode/Decoder#header., channelsrust-analyzer cargo qoi 0.4.1 decode/Decoder#channels.: header.channelsrust-analyzer cargo qoi 0.4.1 header/Header#channels. }) |
| 338 | } |
| 339 | |
| 340 | /// Returns a new decoder with modified number of channels. |
| 341 | /// |
| 342 | /// By default, the number of channels in the decoded image will be equal |
| 343 | /// to whatever is specified in the header. However, it is also possible |
| 344 | /// to decode RGB into RGBA (in which case the alpha channel will be set |
| 345 | /// to 255), and vice versa (in which case the alpha channel will be ignored). |
| 346 | #[inline] |
| 347 | pub const fn with_channelsrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]with_channels().(mut self, channels: Channelsrust-analyzer cargo qoi 0.4.1 types/Channels#) -> Selfrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`] { |
| 348 | self.channelsrust-analyzer cargo qoi 0.4.1 decode/Decoder#channels. = channels; |
| 349 | self |
| 350 | } |
| 351 | |
| 352 | /// Returns the number of channels in the decoded image. |
| 353 | /// |
| 354 | /// Note: this may differ from the number of channels specified in the header. |
| 355 | #[inline] |
| 356 | pub const fn channelsrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]channels().(&self) -> Channelsrust-analyzer cargo qoi 0.4.1 types/Channels# { |
| 357 | self.channelsrust-analyzer cargo qoi 0.4.1 decode/Decoder#channels. |
| 358 | } |
| 359 | |
| 360 | /// Returns the decoded image header. |
| 361 | #[inline] |
| 362 | pub const fn headerrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]header().(&self) -> &Headerrust-analyzer cargo qoi 0.4.1 header/Header# { |
| 363 | &self.headerrust-analyzer cargo qoi 0.4.1 decode/Decoder#header. |
| 364 | } |
| 365 | |
| 366 | /// The number of bytes the decoded image will take. |
| 367 | /// |
| 368 | /// Can be used to pre-allocate the buffer to decode the image into. |
| 369 | #[inline] |
| 370 | pub const fn required_buf_lenrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]required_buf_len().(&self) -> usize { |
| 371 | self.headerrust-analyzer cargo qoi 0.4.1 decode/Decoder#header..n_pixelsrust-analyzer cargo qoi 0.4.1 header/impl#[Header]n_pixels().().saturating_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_mul().(self.channelsrust-analyzer cargo qoi 0.4.1 decode/Decoder#channels..as_u8rust-analyzer cargo qoi 0.4.1 types/impl#[Channels]as_u8().() as usize) |
| 372 | } |
| 373 | |
| 374 | /// Decodes the image to a pre-allocated buffer and returns the number of bytes written. |
| 375 | /// |
| 376 | /// The minimum size of the buffer can be found via [`Decoder::required_buf_len`]. |
| 377 | #[inline] |
| 378 | pub fn decode_to_bufrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]decode_to_buf().(&mut 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> { |
| 379 | let buf = buf.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#as_mut().(); |
| 380 | let size = self.required_buf_lenrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]required_buf_len().(); |
| 381 | 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) { |
| 382 | 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 }); |
| 383 | } |
| 384 | self.readerrust-analyzer cargo qoi 0.4.1 decode/Decoder#reader..decode_imagerust-analyzer cargo qoi 0.4.1 decode/Reader#decode_image().(buf, self.channelsrust-analyzer cargo qoi 0.4.1 decode/Decoder#channels..as_u8rust-analyzer cargo qoi 0.4.1 types/impl#[Channels]as_u8().(), self.headerrust-analyzer cargo qoi 0.4.1 decode/Decoder#header..channelsrust-analyzer cargo qoi 0.4.1 header/Header#channels..as_u8rust-analyzer cargo qoi 0.4.1 types/impl#[Channels]as_u8().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 385 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(size) |
| 386 | } |
| 387 | |
| 388 | /// Decodes the image into a newly allocated vector of bytes and returns it. |
| 389 | #[cfg(any(feature = "std", feature = "alloc"))] |
| 390 | #[inline] |
| 391 | pub fn decode_to_vecrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]decode_to_vec().(&mut 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>> { |
| 392 | let mut out = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; self.headerrust-analyzer cargo qoi 0.4.1 decode/Decoder#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(). self.channelsrust-analyzer cargo qoi 0.4.1 decode/Decoder#channels..as_u8rust-analyzer cargo qoi 0.4.1 types/impl#[Channels]as_u8().() as usize]; |
| 393 | let _ = self.decode_to_bufrust-analyzer cargo qoi 0.4.1 decode/impl#[`Decoder<R>`]decode_to_buf().(&mut out)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 394 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(out) |
| 395 | } |
| 396 | } |