| 1 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#; |
| 2 | |
| 3 | 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#}; |
| 4 | use craterust-analyzer cargo qoi 0.4.1 crate/::utilsrust-analyzer cargo qoi 0.4.1 utils/::unlikelyrust-analyzer cargo qoi 0.4.1 utils/unlikely().; |
| 5 | |
| 6 | /// Image color space. |
| 7 | /// |
| 8 | /// Note: the color space is purely informative. Although it is saved to the |
| 9 | /// file header, it does not affect encoding/decoding in any way. |
| 10 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, 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:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:)] |
| 11 | #[repr(u8)] |
| 12 | pub enum ColorSpacerust-analyzer cargo qoi 0.4.1 types/ColorSpace# { |
| 13 | /// sRGB with linear alpha |
| 14 | Srgbrust-analyzer cargo qoi 0.4.1 types/ColorSpace#Srgb# = 0, |
| 15 | /// All channels are linear |
| 16 | Linearrust-analyzer cargo qoi 0.4.1 types/ColorSpace#Linear# = 1, |
| 17 | } |
| 18 | |
| 19 | impl ColorSpacerust-analyzer cargo qoi 0.4.1 types/ColorSpace# { |
| 20 | /// Returns true if the color space is sRGB with linear alpha. |
| 21 | pub const fn is_srgbrust-analyzer cargo qoi 0.4.1 types/impl#[ColorSpace]is_srgb().(self) -> bool { |
| 22 | matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(self, Selfrust-analyzer cargo qoi 0.4.1 types/impl#[ColorSpace]::Srgbrust-analyzer cargo qoi 0.4.1 types/ColorSpace#Srgb#) |
| 23 | } |
| 24 | |
| 25 | /// Returns true is all channels are linear. |
| 26 | pub const fn is_linearrust-analyzer cargo qoi 0.4.1 types/impl#[ColorSpace]is_linear().(self) -> bool { |
| 27 | matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(self, Selfrust-analyzer cargo qoi 0.4.1 types/impl#[ColorSpace]::Linearrust-analyzer cargo qoi 0.4.1 types/ColorSpace#Linear#) |
| 28 | } |
| 29 | |
| 30 | /// Converts to an integer (0 if sRGB, 1 if all linear). |
| 31 | pub const fn as_u8rust-analyzer cargo qoi 0.4.1 types/impl#[ColorSpace]as_u8().(self) -> u8 { |
| 32 | self as u8 |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for ColorSpacerust-analyzer cargo qoi 0.4.1 types/ColorSpace# { |
| 37 | fn defaultrust-analyzer cargo qoi 0.4.1 types/impl#[ColorSpace][Default]default().() -> Selfrust-analyzer cargo qoi 0.4.1 types/impl#[ColorSpace][Default] { |
| 38 | Selfrust-analyzer cargo qoi 0.4.1 types/impl#[ColorSpace][Default]::Srgbrust-analyzer cargo qoi 0.4.1 types/ColorSpace#Srgb# |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<ColorSpacerust-analyzer cargo qoi 0.4.1 types/ColorSpace#> for u8 { |
| 43 | #[inline] |
| 44 | fn fromrust-analyzer cargo qoi 0.4.1 types/impl#[u8][`From<ColorSpace>`]from().(colorspace: ColorSpacerust-analyzer cargo qoi 0.4.1 types/ColorSpace#) -> Selfrust-analyzer cargo qoi 0.4.1 types/impl#[u8][`From<ColorSpace>`] { |
| 45 | colorspace as Selfrust-analyzer cargo qoi 0.4.1 types/impl#[u8][`From<ColorSpace>`] |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | impl TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<u8> for ColorSpacerust-analyzer cargo qoi 0.4.1 types/ColorSpace# { |
| 50 | type Errorrust-analyzer cargo qoi 0.4.1 types/impl#[ColorSpace][`TryFrom<u8>`]Error# = Errorrust-analyzer cargo qoi 0.4.1 error/Error#; |
| 51 | |
| 52 | #[inline] |
| 53 | fn try_fromrust-analyzer cargo qoi 0.4.1 types/impl#[ColorSpace][`TryFrom<u8>`]try_from().(colorspace: u8) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<Selfrust-analyzer cargo qoi 0.4.1 types/impl#[ColorSpace][`TryFrom<u8>`]> { |
| 54 | if unlikelyrust-analyzer cargo qoi 0.4.1 utils/unlikely().(colorspace 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().1 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().1) { |
| 55 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo qoi 0.4.1 error/Error#::InvalidColorSpacerust-analyzer cargo qoi 0.4.1 error/Error#InvalidColorSpace# { colorspacerust-analyzer cargo qoi 0.4.1 error/Error#InvalidColorSpace#colorspace. }) |
| 56 | } else { |
| 57 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(if colorspace 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 { Selfrust-analyzer cargo qoi 0.4.1 types/impl#[ColorSpace][`TryFrom<u8>`]::Srgbrust-analyzer cargo qoi 0.4.1 types/ColorSpace#Srgb# } else { Selfrust-analyzer cargo qoi 0.4.1 types/impl#[ColorSpace][`TryFrom<u8>`]::Linearrust-analyzer cargo qoi 0.4.1 types/ColorSpace#Linear# }) |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /// Number of 8-bit channels in a pixel. |
| 63 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, 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:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:)] |
| 64 | #[repr(u8)] |
| 65 | pub enum Channelsrust-analyzer cargo qoi 0.4.1 types/Channels# { |
| 66 | /// Three 8-bit channels (RGB) |
| 67 | Rgbrust-analyzer cargo qoi 0.4.1 types/Channels#Rgb# = 3, |
| 68 | /// Four 8-bit channels (RGBA) |
| 69 | Rgbarust-analyzer cargo qoi 0.4.1 types/Channels#Rgba# = 4, |
| 70 | } |
| 71 | |
| 72 | impl Channelsrust-analyzer cargo qoi 0.4.1 types/Channels# { |
| 73 | /// Returns true if there are 3 channels (RGB). |
| 74 | pub const fn is_rgbrust-analyzer cargo qoi 0.4.1 types/impl#[Channels]is_rgb().(self) -> bool { |
| 75 | matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(self, Selfrust-analyzer cargo qoi 0.4.1 types/impl#[Channels]::Rgbrust-analyzer cargo qoi 0.4.1 types/Channels#Rgb#) |
| 76 | } |
| 77 | |
| 78 | /// Returns true if there are 4 channels (RGBA). |
| 79 | pub const fn is_rgbarust-analyzer cargo qoi 0.4.1 types/impl#[Channels]is_rgba().(self) -> bool { |
| 80 | matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(self, Selfrust-analyzer cargo qoi 0.4.1 types/impl#[Channels]::Rgbarust-analyzer cargo qoi 0.4.1 types/Channels#Rgba#) |
| 81 | } |
| 82 | |
| 83 | /// Converts to an integer (3 if RGB, 4 if RGBA). |
| 84 | pub const fn as_u8rust-analyzer cargo qoi 0.4.1 types/impl#[Channels]as_u8().(self) -> u8 { |
| 85 | self as u8 |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for Channelsrust-analyzer cargo qoi 0.4.1 types/Channels# { |
| 90 | fn defaultrust-analyzer cargo qoi 0.4.1 types/impl#[Channels][Default]default().() -> Selfrust-analyzer cargo qoi 0.4.1 types/impl#[Channels][Default] { |
| 91 | Selfrust-analyzer cargo qoi 0.4.1 types/impl#[Channels][Default]::Rgbrust-analyzer cargo qoi 0.4.1 types/Channels#Rgb# |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Channelsrust-analyzer cargo qoi 0.4.1 types/Channels#> for u8 { |
| 96 | #[inline] |
| 97 | fn fromrust-analyzer cargo qoi 0.4.1 types/impl#[u8][`From<Channels>`]from().(channels: Channelsrust-analyzer cargo qoi 0.4.1 types/Channels#) -> Selfrust-analyzer cargo qoi 0.4.1 types/impl#[u8][`From<Channels>`] { |
| 98 | channels as Selfrust-analyzer cargo qoi 0.4.1 types/impl#[u8][`From<Channels>`] |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | impl TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<u8> for Channelsrust-analyzer cargo qoi 0.4.1 types/Channels# { |
| 103 | type Errorrust-analyzer cargo qoi 0.4.1 types/impl#[Channels][`TryFrom<u8>`]Error# = Errorrust-analyzer cargo qoi 0.4.1 error/Error#; |
| 104 | |
| 105 | #[inline] |
| 106 | fn try_fromrust-analyzer cargo qoi 0.4.1 types/impl#[Channels][`TryFrom<u8>`]try_from().(channels: u8) -> Resultrust-analyzer cargo qoi 0.4.1 error/Result#<Selfrust-analyzer cargo qoi 0.4.1 types/impl#[Channels][`TryFrom<u8>`]> { |
| 107 | if unlikelyrust-analyzer cargo qoi 0.4.1 utils/unlikely().(channels 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().3 && channels 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().4) { |
| 108 | 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. }) |
| 109 | } else { |
| 110 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(if channels 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().3 { Selfrust-analyzer cargo qoi 0.4.1 types/impl#[Channels][`TryFrom<u8>`]::Rgbrust-analyzer cargo qoi 0.4.1 types/Channels#Rgb# } else { Selfrust-analyzer cargo qoi 0.4.1 types/impl#[Channels][`TryFrom<u8>`]::Rgbarust-analyzer cargo qoi 0.4.1 types/Channels#Rgba# }) |
| 111 | } |
| 112 | } |
| 113 | } |