| 1 | use craterust-analyzer cargo imagesize 0.13.0 crate/::utilrust-analyzer cargo imagesize 0.13.0 util/::*; |
| 2 | use craterust-analyzer cargo imagesize 0.13.0 crate/::{ImageResultrust-analyzer cargo imagesize 0.13.0 ImageResult#, ImageSizerust-analyzer cargo imagesize 0.13.0 ImageSize#}; |
| 3 | |
| 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/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#, Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#, SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#}; |
| 5 | |
| 6 | pub fn sizerust-analyzer cargo imagesize 0.13.0 formats/pnm/size().<R: BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead# + Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#>(reader: &mut R) -> ImageResultrust-analyzer cargo imagesize 0.13.0 ImageResult#<ImageSizerust-analyzer cargo imagesize 0.13.0 ImageSize#> { |
| 7 | reader.seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#seek().(SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Startrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Start#(2))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 8 | |
| 9 | // We try to loop until we find a line that does not start with a comment |
| 10 | // or is empty. After that, we should expect width and height back to back |
| 11 | // separated by an arbitrary amount of whitespace. |
| 12 | loop { |
| 13 | // Lines can be arbitrarily long, but 1k is a good enough cap I think. |
| 14 | // Anything higher and I blame whoever made the file. |
| 15 | let line = read_until_whitespacerust-analyzer cargo imagesize 0.13.0 util/read_until_whitespace().(reader, 1024)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 16 | let trimmed_line = line.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().(); |
| 17 | |
| 18 | // If it's a comment, skip until newline |
| 19 | if trimmed_line.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().('#') { |
| 20 | read_until_cappedrust-analyzer cargo imagesize 0.13.0 util/read_until_capped().(reader, b'\n', 1024)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 21 | continue; |
| 22 | } |
| 23 | |
| 24 | // If it's just empty skip |
| 25 | if trimmed_line.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 26 | continue; |
| 27 | } |
| 28 | |
| 29 | // The first thing we read that isn't empty or a comment should be the width |
| 30 | let raw_width = line; |
| 31 | |
| 32 | // Read in the next non-whitespace section as the height |
| 33 | let line = read_until_whitespacerust-analyzer cargo imagesize 0.13.0 util/read_until_whitespace().(reader, 1024)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 34 | let raw_height = line.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().(); |
| 35 | |
| 36 | // Try to parse the width and height |
| 37 | let width_parsed = raw_width.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<usize>().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().(); |
| 38 | let height_parsed = raw_height.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<usize>().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().(); |
| 39 | |
| 40 | // If successful return it |
| 41 | if let (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(width), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(height)) = (width_parsed, height_parsed) { |
| 42 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ImageSizerust-analyzer cargo imagesize 0.13.0 ImageSize# { widthrust-analyzer cargo imagesize 0.13.0 ImageSize#width., heightrust-analyzer cargo imagesize 0.13.0 ImageSize#height. }); |
| 43 | } |
| 44 | |
| 45 | // If no successful then assume that it cannot be read |
| 46 | // If this happens we need to gather test files for those cases |
| 47 | break; |
| 48 | } |
| 49 | |
| 50 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#, "PNM dimensions not found").intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 51 | } |
| 52 | |
| 53 | pub fn matchesrust-analyzer cargo imagesize 0.13.0 formats/pnm/matches().(header: &[u8]) -> bool { |
| 54 | if header[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(). 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().b'P' { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | // We only support P1 to P6. Currently ignoring P7, PF, PFM |
| 59 | if header[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(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]lt().b'1' || header[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(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]gt().b'6' { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | true |
| 64 | } |