| 1 | 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#}; |
| 2 | |
| 3 | use craterust-analyzer cargo imagesize 0.13.0 crate/::{utilrust-analyzer cargo imagesize 0.13.0 util/::read_line_cappedrust-analyzer cargo imagesize 0.13.0 util/read_line_capped()., ImageResultrust-analyzer cargo imagesize 0.13.0 ImageResult#, ImageSizerust-analyzer cargo imagesize 0.13.0 ImageSize#}; |
| 4 | |
| 5 | pub fn sizerust-analyzer cargo imagesize 0.13.0 formats/hdr/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#> { |
| 6 | 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#(0))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 7 | |
| 8 | // Read the first line and check if it's a valid HDR format identifier |
| 9 | // Only read max of 11 characters which is max for longest valid header |
| 10 | let format_identifier = read_line_cappedrust-analyzer cargo imagesize 0.13.0 util/read_line_capped().(reader, 11)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 11 | |
| 12 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().format_identifier.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("#?RADIANCE") && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().format_identifier.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("#?RGBE") { |
| 13 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#( |
| 14 | 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#, "Invalid HDR format identifier").intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 15 | ); |
| 16 | } |
| 17 | |
| 18 | loop { |
| 19 | // Assuming no line will ever go above 256. Just a random guess at the moment. |
| 20 | // If a line goes over the capped length we will return InvalidData which I think |
| 21 | // is better than potentially reading a malicious file and exploding memory usage. |
| 22 | let line = read_line_cappedrust-analyzer cargo imagesize 0.13.0 util/read_line_capped().(reader, 256)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 23 | |
| 24 | if line.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().().is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 25 | continue; |
| 26 | } |
| 27 | |
| 28 | // HDR image dimensions can be stored in 8 different ways based on orientation |
| 29 | // Using EXIF orientation as a reference: |
| 30 | // https://web.archive.org/web/20220924095433/https://sirv.sirv.com/website/exif-orientation-values.jpg |
| 31 | // |
| 32 | // -Y N +X M => Standard orientation (EXIF 1) |
| 33 | // -Y N -X M => Flipped horizontally (EXIF 2) |
| 34 | // +Y N -X M => Flipped vertically and horizontally (EXIF 3) |
| 35 | // +Y N +X M => Flipped vertically (EXIF 4) |
| 36 | // +X M -Y N => Rotate 90 CCW and flip vertically (EXIF 5) |
| 37 | // -X M -Y N => Rotate 90 CCW (EXIF 6) |
| 38 | // -X M +Y N => Rotate 90 CW and flip vertically (EXIF 7) |
| 39 | // +X M +Y N => Rotate 90 CW (EXIF 8) |
| 40 | // |
| 41 | // For EXIF 1-4 we can treat the dimensions the same. Flipping horizontally/vertically does not change them. |
| 42 | // For EXIF 5-8 we need to swap width and height because the image was rotated 90/270 degrees. |
| 43 | // |
| 44 | // Because of the ordering and rotations I believe that means that lines that start with Y will always |
| 45 | // be read as `height` then `width` and ones that start with X will be read as `width` then `height, |
| 46 | // but since any line that starts with X is rotated 90 degrees they will be flipped. Essentially this |
| 47 | // means that no matter whether the line starts with X or Y, it will be read as height then width. |
| 48 | |
| 49 | // Extract width and height information |
| 50 | if line.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("-Y") |
| 51 | || line.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("+Y") |
| 52 | || line.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("-X") |
| 53 | || line.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("+X") |
| 54 | { |
| 55 | let dimensions: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&str> = line.split_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_whitespace().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 56 | if dimensions.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().4 { |
| 57 | return 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().( |
| 58 | 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#, |
| 59 | "Invalid HDR dimensions line", |
| 60 | ) |
| 61 | .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 62 | } |
| 63 | |
| 64 | let height_parsed = dimensions[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().1]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..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().(); |
| 65 | let width_parsed = dimensions[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().3]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..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().(); |
| 66 | |
| 67 | 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) { |
| 68 | 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. }); |
| 69 | } |
| 70 | |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | 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#, "HDR dimensions not found").intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 76 | } |
| 77 | |
| 78 | pub fn matchesrust-analyzer cargo imagesize 0.13.0 formats/hdr/matches().(header: &[u8]) -> bool { |
| 79 | let radiance_header = b"#?RADIANCE\n"; |
| 80 | let rgbe_header = b"#?RGBE\n"; |
| 81 | |
| 82 | header.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]starts_with().(radiance_header) || header.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]starts_with().(rgbe_header) |
| 83 | } |