/src/image/src/codecs/openexr.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! Decoding of OpenEXR (.exr) Images |
2 | | //! |
3 | | //! OpenEXR is an image format that is widely used, especially in VFX, |
4 | | //! because it supports lossless and lossy compression for float data. |
5 | | //! |
6 | | //! This decoder only supports RGB and RGBA images. |
7 | | //! If an image does not contain alpha information, |
8 | | //! it is defaulted to `1.0` (no transparency). |
9 | | //! |
10 | | //! # Related Links |
11 | | //! * <https://www.openexr.com/documentation.html> - The OpenEXR reference. |
12 | | //! |
13 | | //! |
14 | | //! Current limitations (July 2021): |
15 | | //! - only pixel type `Rgba32F` and `Rgba16F` are supported |
16 | | //! - only non-deep rgb/rgba files supported, no conversion from/to YCbCr or similar |
17 | | //! - only the first non-deep rgb layer is used |
18 | | //! - only the largest mip map level is used |
19 | | //! - pixels outside display window are lost |
20 | | //! - meta data is lost |
21 | | //! - dwaa/dwab compressed images not supported yet by the exr library |
22 | | //! - (chroma) subsampling not supported yet by the exr library |
23 | | use exr::prelude::*; |
24 | | |
25 | | use crate::error::{DecodingError, EncodingError, ImageFormatHint}; |
26 | | use crate::{ |
27 | | ColorType, ExtendedColorType, ImageDecoder, ImageEncoder, ImageError, ImageFormat, ImageResult, |
28 | | }; |
29 | | |
30 | | use std::io::{BufRead, Seek, Write}; |
31 | | |
32 | | /// An OpenEXR decoder. Immediately reads the meta data from the file. |
33 | | #[derive(Debug)] |
34 | | pub struct OpenExrDecoder<R> { |
35 | | exr_reader: exr::block::reader::Reader<R>, |
36 | | |
37 | | // select a header that is rgb and not deep |
38 | | header_index: usize, |
39 | | |
40 | | // decode either rgb or rgba. |
41 | | // can be specified to include or discard alpha channels. |
42 | | // if none, the alpha channel will only be allocated where the file contains data for it. |
43 | | alpha_preference: Option<bool>, |
44 | | |
45 | | alpha_present_in_file: bool, |
46 | | } |
47 | | |
48 | | impl<R: BufRead + Seek> OpenExrDecoder<R> { |
49 | | /// Create a decoder. Consumes the first few bytes of the source to extract image dimensions. |
50 | | /// Assumes the reader is buffered. In most cases, |
51 | | /// you should wrap your reader in a `BufReader` for best performance. |
52 | | /// Loads an alpha channel if the file has alpha samples. |
53 | | /// Use `with_alpha_preference` if you want to load or not load alpha unconditionally. |
54 | 0 | pub fn new(source: R) -> ImageResult<Self> { |
55 | 0 | Self::with_alpha_preference(source, None) |
56 | 0 | } Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::new Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<_>>::new Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::new Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::new Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::new Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::new Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::new Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::new Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::new Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::new Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::new Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::new |
57 | | |
58 | | /// Create a decoder. Consumes the first few bytes of the source to extract image dimensions. |
59 | | /// Assumes the reader is buffered. In most cases, |
60 | | /// you should wrap your reader in a `BufReader` for best performance. |
61 | | /// If alpha preference is specified, an alpha channel will |
62 | | /// always be present or always be not present in the returned image. |
63 | | /// If alpha preference is none, the alpha channel will only be returned if it is found in the file. |
64 | 0 | pub fn with_alpha_preference(source: R, alpha_preference: Option<bool>) -> ImageResult<Self> { |
65 | | // read meta data, then wait for further instructions, keeping the file open and ready |
66 | 0 | let exr_reader = exr::block::read(source, false).map_err(to_image_err)?; |
67 | | |
68 | 0 | let header_index = exr_reader |
69 | 0 | .headers() |
70 | 0 | .iter() |
71 | 0 | .position(|header| { |
72 | 0 | // check if r/g/b exists in the channels |
73 | 0 | let has_rgb = ["R", "G", "B"] |
74 | 0 | .iter() |
75 | 0 | .all(|&required| // alpha will be optional |
76 | 0 | header.channels.find_index_of_channel(&Text::from(required)).is_some()); Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0}::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<_>>::with_alpha_preference::{closure#0}::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0}::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0}::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0}::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0}::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0}::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0}::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0}::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0}::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0}::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0}::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<alloc::vec::Vec<u8>>>>::with_alpha_preference::{closure#0}::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0}::{closure#0} |
77 | 0 |
|
78 | 0 | // we currently dont support deep images, or images with other color spaces than rgb |
79 | 0 | !header.deep && has_rgb |
80 | 0 | }) Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<_>>::with_alpha_preference::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<alloc::vec::Vec<u8>>>>::with_alpha_preference::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#0} |
81 | 0 | .ok_or_else(|| { |
82 | 0 | ImageError::Decoding(DecodingError::new( |
83 | 0 | ImageFormatHint::Exact(ImageFormat::OpenExr), |
84 | 0 | "image does not contain non-deep rgb channels", |
85 | 0 | )) |
86 | 0 | })?; Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<_>>::with_alpha_preference::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<alloc::vec::Vec<u8>>>>::with_alpha_preference::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference::{closure#1} |
87 | | |
88 | 0 | let has_alpha = exr_reader.headers()[header_index] |
89 | 0 | .channels |
90 | 0 | .find_index_of_channel(&Text::from("A")) |
91 | 0 | .is_some(); |
92 | 0 |
|
93 | 0 | Ok(Self { |
94 | 0 | alpha_preference, |
95 | 0 | exr_reader, |
96 | 0 | header_index, |
97 | 0 | alpha_present_in_file: has_alpha, |
98 | 0 | }) |
99 | 0 | } Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<_>>::with_alpha_preference Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<alloc::vec::Vec<u8>>>>::with_alpha_preference Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::with_alpha_preference |
100 | | |
101 | | // does not leak exrs-specific meta data into public api, just does it for this module |
102 | 0 | fn selected_exr_header(&self) -> &exr::meta::header::Header { |
103 | 0 | &self.exr_reader.meta_data().headers[self.header_index] |
104 | 0 | } Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::selected_exr_header Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<_>>::selected_exr_header Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::selected_exr_header Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::selected_exr_header Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::selected_exr_header Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::selected_exr_header Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::selected_exr_header Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::selected_exr_header Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::selected_exr_header Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::selected_exr_header Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::selected_exr_header Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::selected_exr_header Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<alloc::vec::Vec<u8>>>>::selected_exr_header Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>>>::selected_exr_header |
105 | | } |
106 | | |
107 | | impl<R: BufRead + Seek> ImageDecoder for OpenExrDecoder<R> { |
108 | 0 | fn dimensions(&self) -> (u32, u32) { |
109 | 0 | let size = self |
110 | 0 | .selected_exr_header() |
111 | 0 | .shared_attributes |
112 | 0 | .display_window |
113 | 0 | .size; |
114 | 0 | (size.width() as u32, size.height() as u32) |
115 | 0 | } Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::dimensions Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<_> as image::io::decoder::ImageDecoder>::dimensions Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::dimensions Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::dimensions Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::dimensions Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::dimensions Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::dimensions Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::dimensions Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::dimensions Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::dimensions Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::dimensions Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::dimensions Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<alloc::vec::Vec<u8>>> as image::io::decoder::ImageDecoder>::dimensions Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::dimensions |
116 | | |
117 | 0 | fn color_type(&self) -> ColorType { |
118 | 0 | let returns_alpha = self.alpha_preference.unwrap_or(self.alpha_present_in_file); |
119 | 0 | if returns_alpha { |
120 | 0 | ColorType::Rgba32F |
121 | | } else { |
122 | 0 | ColorType::Rgb32F |
123 | | } |
124 | 0 | } Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<_> as image::io::decoder::ImageDecoder>::color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<alloc::vec::Vec<u8>>> as image::io::decoder::ImageDecoder>::color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::color_type |
125 | | |
126 | 0 | fn original_color_type(&self) -> ExtendedColorType { |
127 | 0 | if self.alpha_present_in_file { |
128 | 0 | ExtendedColorType::Rgba32F |
129 | | } else { |
130 | 0 | ExtendedColorType::Rgb32F |
131 | | } |
132 | 0 | } Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::original_color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<_> as image::io::decoder::ImageDecoder>::original_color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::original_color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::original_color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::original_color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::original_color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::original_color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::original_color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::original_color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::original_color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::original_color_type Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::original_color_type |
133 | | |
134 | | // reads with or without alpha, depending on `self.alpha_preference` and `self.alpha_present_in_file` |
135 | 0 | fn read_image(self, unaligned_bytes: &mut [u8]) -> ImageResult<()> { |
136 | 0 | let _blocks_in_header = self.selected_exr_header().chunk_count as u64; |
137 | 0 | let channel_count = self.color_type().channel_count() as usize; |
138 | 0 |
|
139 | 0 | let display_window = self.selected_exr_header().shared_attributes.display_window; |
140 | 0 | let data_window_offset = |
141 | 0 | self.selected_exr_header().own_attributes.layer_position - display_window.position; |
142 | 0 |
|
143 | 0 | { |
144 | 0 | // check whether the buffer is large enough for the dimensions of the file |
145 | 0 | let (width, height) = self.dimensions(); |
146 | 0 | let bytes_per_pixel = self.color_type().bytes_per_pixel() as usize; |
147 | 0 | let expected_byte_count = (width as usize) |
148 | 0 | .checked_mul(height as usize) |
149 | 0 | .and_then(|size| size.checked_mul(bytes_per_pixel)); Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<_> as image::io::decoder::ImageDecoder>::read_image::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<alloc::vec::Vec<u8>>> as image::io::decoder::ImageDecoder>::read_image::{closure#0} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#0} |
150 | 0 |
|
151 | 0 | // if the width and height does not match the length of the bytes, the arguments are invalid |
152 | 0 | let has_invalid_size_or_overflowed = expected_byte_count |
153 | 0 | .map(|expected_byte_count| unaligned_bytes.len() != expected_byte_count) Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<_> as image::io::decoder::ImageDecoder>::read_image::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<alloc::vec::Vec<u8>>> as image::io::decoder::ImageDecoder>::read_image::{closure#1} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#1} |
154 | 0 | // otherwise, size calculation overflowed, is bigger than memory, |
155 | 0 | // therefore data is too small, so it is invalid. |
156 | 0 | .unwrap_or(true); |
157 | 0 |
|
158 | 0 | assert!( |
159 | 0 | !has_invalid_size_or_overflowed, |
160 | 0 | "byte buffer not large enough for the specified dimensions and f32 pixels" |
161 | | ); |
162 | | } |
163 | | |
164 | 0 | let result = read() |
165 | 0 | .no_deep_data() |
166 | 0 | .largest_resolution_level() |
167 | 0 | .rgba_channels( |
168 | 0 | move |_size, _channels| vec![0_f32; display_window.size.area() * channel_count], Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#2} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<_> as image::io::decoder::ImageDecoder>::read_image::{closure#2} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#2} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#2} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#2} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#2} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#2} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#2} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#2} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#2} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#2} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#2} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<alloc::vec::Vec<u8>>> as image::io::decoder::ImageDecoder>::read_image::{closure#2} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#2} |
169 | 0 | move |buffer, index_in_data_window, (r, g, b, a_or_1): (f32, f32, f32, f32)| { |
170 | 0 | let index_in_display_window = |
171 | 0 | index_in_data_window.to_i32() + data_window_offset; |
172 | 0 |
|
173 | 0 | // only keep pixels inside the data window |
174 | 0 | // TODO filter chunks based on this |
175 | 0 | if index_in_display_window.x() >= 0 |
176 | 0 | && index_in_display_window.y() >= 0 |
177 | 0 | && index_in_display_window.x() < display_window.size.width() as i32 |
178 | 0 | && index_in_display_window.y() < display_window.size.height() as i32 |
179 | 0 | { |
180 | 0 | let index_in_display_window = |
181 | 0 | index_in_display_window.to_usize("index bug").unwrap(); |
182 | 0 | let first_f32_index = |
183 | 0 | index_in_display_window.flat_index_for_size(display_window.size); |
184 | 0 |
|
185 | 0 | buffer[first_f32_index * channel_count |
186 | 0 | ..(first_f32_index + 1) * channel_count] |
187 | 0 | .copy_from_slice(&[r, g, b, a_or_1][0..channel_count]); |
188 | 0 |
|
189 | 0 | // TODO white point chromaticities + srgb/linear conversion? |
190 | 0 | } |
191 | 0 | }, Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#3} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<_> as image::io::decoder::ImageDecoder>::read_image::{closure#3} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#3} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#3} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#3} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#3} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#3} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#3} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#3} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#3} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#3} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#3} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<alloc::vec::Vec<u8>>> as image::io::decoder::ImageDecoder>::read_image::{closure#3} Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image::{closure#3} |
192 | 0 | ) |
193 | 0 | .first_valid_layer() // TODO select exact layer by self.header_index? |
194 | 0 | .all_attributes() |
195 | 0 | .from_chunks(self.exr_reader) |
196 | 0 | .map_err(to_image_err)?; |
197 | | |
198 | | // TODO this copy is strictly not necessary, but the exr api is a little too simple for reading into a borrowed target slice |
199 | | |
200 | | // this cast is safe and works with any alignment, as bytes are copied, and not f32 values. |
201 | | // note: buffer slice length is checked in the beginning of this function and will be correct at this point |
202 | 0 | unaligned_bytes.copy_from_slice(bytemuck::cast_slice( |
203 | 0 | result.layer_data.channel_data.pixels.as_slice(), |
204 | 0 | )); |
205 | 0 | Ok(()) |
206 | 0 | } Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<_> as image::io::decoder::ImageDecoder>::read_image Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<alloc::vec::Vec<u8>>> as image::io::decoder::ImageDecoder>::read_image Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image |
207 | | |
208 | 0 | fn read_image_boxed(self: Box<Self>, buf: &mut [u8]) -> ImageResult<()> { |
209 | 0 | (*self).read_image(buf) |
210 | 0 | } Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image_boxed Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<_> as image::io::decoder::ImageDecoder>::read_image_boxed Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image_boxed Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image_boxed Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image_boxed Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image_boxed Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image_boxed Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image_boxed Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image_boxed Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image_boxed Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image_boxed Unexecuted instantiation: <image::codecs::openexr::OpenExrDecoder<std::io::cursor::Cursor<&[u8]>> as image::io::decoder::ImageDecoder>::read_image_boxed |
211 | | } |
212 | | |
213 | | /// Write a raw byte buffer of pixels, |
214 | | /// returning an Error if it has an invalid length. |
215 | | /// |
216 | | /// Assumes the writer is buffered. In most cases, |
217 | | /// you should wrap your writer in a `BufWriter` for best performance. |
218 | | // private. access via `OpenExrEncoder` |
219 | 0 | fn write_buffer( |
220 | 0 | mut buffered_write: impl Write + Seek, |
221 | 0 | unaligned_bytes: &[u8], |
222 | 0 | width: u32, |
223 | 0 | height: u32, |
224 | 0 | color_type: ExtendedColorType, |
225 | 0 | ) -> ImageResult<()> { |
226 | 0 | let width = width as usize; |
227 | 0 | let height = height as usize; |
228 | 0 | let bytes_per_pixel = color_type.bits_per_pixel() as usize / 8; |
229 | 0 |
|
230 | 0 | match color_type { |
231 | | ExtendedColorType::Rgb32F => { |
232 | 0 | Image // TODO compression method zip?? |
233 | 0 | ::from_channels( |
234 | 0 | (width, height), |
235 | 0 | SpecificChannels::rgb(|pixel: Vec2<usize>| { |
236 | 0 | let pixel_index = pixel.flat_index_for_size(Vec2(width, height)); |
237 | 0 | let start_byte = pixel_index * bytes_per_pixel; |
238 | 0 |
|
239 | 0 | let [r, g, b]: [f32; 3] = bytemuck::pod_read_unaligned( |
240 | 0 | &unaligned_bytes[start_byte..start_byte + bytes_per_pixel], |
241 | 0 | ); |
242 | 0 |
|
243 | 0 | (r, g, b) |
244 | 0 | }), Unexecuted instantiation: image::codecs::openexr::write_buffer::<_>::{closure#0} Unexecuted instantiation: image::codecs::openexr::write_buffer::<&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>>>::{closure#0} Unexecuted instantiation: image::codecs::openexr::write_buffer::<std::io::cursor::Cursor<&mut alloc::vec::Vec<u8>>>::{closure#0} |
245 | 0 | ) |
246 | 0 | .write() |
247 | 0 | // .on_progress(|progress| todo!()) |
248 | 0 | .to_buffered(&mut buffered_write) |
249 | 0 | .map_err(to_image_err)?; |
250 | | } |
251 | | |
252 | | ExtendedColorType::Rgba32F => { |
253 | 0 | Image // TODO compression method zip?? |
254 | 0 | ::from_channels( |
255 | 0 | (width, height), |
256 | 0 | SpecificChannels::rgba(|pixel: Vec2<usize>| { |
257 | 0 | let pixel_index = pixel.flat_index_for_size(Vec2(width, height)); |
258 | 0 | let start_byte = pixel_index * bytes_per_pixel; |
259 | 0 |
|
260 | 0 | let [r, g, b, a]: [f32; 4] = bytemuck::pod_read_unaligned( |
261 | 0 | &unaligned_bytes[start_byte..start_byte + bytes_per_pixel], |
262 | 0 | ); |
263 | 0 |
|
264 | 0 | (r, g, b, a) |
265 | 0 | }), Unexecuted instantiation: image::codecs::openexr::write_buffer::<_>::{closure#1} Unexecuted instantiation: image::codecs::openexr::write_buffer::<&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>>>::{closure#1} Unexecuted instantiation: image::codecs::openexr::write_buffer::<std::io::cursor::Cursor<&mut alloc::vec::Vec<u8>>>::{closure#1} |
266 | 0 | ) |
267 | 0 | .write() |
268 | 0 | // .on_progress(|progress| todo!()) |
269 | 0 | .to_buffered(&mut buffered_write) |
270 | 0 | .map_err(to_image_err)?; |
271 | | } |
272 | | |
273 | | // TODO other color types and channel types |
274 | 0 | unsupported_color_type => { |
275 | 0 | return Err(ImageError::Encoding(EncodingError::new( |
276 | 0 | ImageFormatHint::Exact(ImageFormat::OpenExr), |
277 | 0 | format!("writing color type {unsupported_color_type:?} not yet supported"), |
278 | 0 | ))) |
279 | | } |
280 | | } |
281 | | |
282 | 0 | Ok(()) |
283 | 0 | } Unexecuted instantiation: image::codecs::openexr::write_buffer::<_> Unexecuted instantiation: image::codecs::openexr::write_buffer::<&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>>> Unexecuted instantiation: image::codecs::openexr::write_buffer::<std::io::cursor::Cursor<&mut alloc::vec::Vec<u8>>> |
284 | | |
285 | | // TODO is this struct and trait actually used anywhere? |
286 | | /// A thin wrapper that implements `ImageEncoder` for OpenEXR images. Will behave like `image::codecs::openexr::write_buffer`. |
287 | | #[derive(Debug)] |
288 | | pub struct OpenExrEncoder<W>(W); |
289 | | |
290 | | impl<W> OpenExrEncoder<W> { |
291 | | /// Create an `ImageEncoder`. Does not write anything yet. Writing later will behave like `image::codecs::openexr::write_buffer`. |
292 | | // use constructor, not public field, for future backwards-compatibility |
293 | 0 | pub fn new(write: W) -> Self { |
294 | 0 | Self(write) |
295 | 0 | } Unexecuted instantiation: <image::codecs::openexr::OpenExrEncoder<_>>::new Unexecuted instantiation: <image::codecs::openexr::OpenExrEncoder<&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>>>>::new Unexecuted instantiation: <image::codecs::openexr::OpenExrEncoder<std::io::cursor::Cursor<&mut alloc::vec::Vec<u8>>>>::new |
296 | | } |
297 | | |
298 | | impl<W> ImageEncoder for OpenExrEncoder<W> |
299 | | where |
300 | | W: Write + Seek, |
301 | | { |
302 | | /// Writes the complete image. |
303 | | /// |
304 | | /// Assumes the writer is buffered. In most cases, you should wrap your writer in a `BufWriter` |
305 | | /// for best performance. |
306 | | #[track_caller] |
307 | 0 | fn write_image( |
308 | 0 | self, |
309 | 0 | buf: &[u8], |
310 | 0 | width: u32, |
311 | 0 | height: u32, |
312 | 0 | color_type: ExtendedColorType, |
313 | 0 | ) -> ImageResult<()> { |
314 | 0 | let expected_buffer_len = color_type.buffer_size(width, height); |
315 | 0 | assert_eq!( |
316 | 0 | expected_buffer_len, |
317 | 0 | buf.len() as u64, |
318 | 0 | "Invalid buffer length: expected {expected_buffer_len} got {} for {width}x{height} image", |
319 | 0 | buf.len(), |
320 | | ); |
321 | | |
322 | 0 | write_buffer(self.0, buf, width, height, color_type) |
323 | 0 | } Unexecuted instantiation: <image::codecs::openexr::OpenExrEncoder<_> as image::io::encoder::ImageEncoder>::write_image Unexecuted instantiation: <image::codecs::openexr::OpenExrEncoder<&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>>> as image::io::encoder::ImageEncoder>::write_image Unexecuted instantiation: <image::codecs::openexr::OpenExrEncoder<std::io::cursor::Cursor<&mut alloc::vec::Vec<u8>>> as image::io::encoder::ImageEncoder>::write_image |
324 | | } |
325 | | |
326 | 0 | fn to_image_err(exr_error: Error) -> ImageError { |
327 | 0 | ImageError::Decoding(DecodingError::new( |
328 | 0 | ImageFormatHint::Exact(ImageFormat::OpenExr), |
329 | 0 | exr_error.to_string(), |
330 | 0 | )) |
331 | 0 | } |
332 | | |
333 | | #[cfg(test)] |
334 | | mod test { |
335 | | use super::*; |
336 | | |
337 | | use std::fs::File; |
338 | | use std::io::{BufReader, Cursor}; |
339 | | use std::path::{Path, PathBuf}; |
340 | | |
341 | | use crate::error::{LimitError, LimitErrorKind}; |
342 | | use crate::images::buffer::{Rgb32FImage, Rgba32FImage}; |
343 | | use crate::io::free_functions::decoder_to_vec; |
344 | | use crate::{DynamicImage, ImageBuffer, Rgb, Rgba}; |
345 | | |
346 | | const BASE_PATH: &[&str] = &[".", "tests", "images", "exr"]; |
347 | | |
348 | | /// Write an `Rgb32FImage`. |
349 | | /// Assumes the writer is buffered. In most cases, |
350 | | /// you should wrap your writer in a `BufWriter` for best performance. |
351 | | fn write_rgb_image(write: impl Write + Seek, image: &Rgb32FImage) -> ImageResult<()> { |
352 | | write_buffer( |
353 | | write, |
354 | | bytemuck::cast_slice(image.as_raw().as_slice()), |
355 | | image.width(), |
356 | | image.height(), |
357 | | ExtendedColorType::Rgb32F, |
358 | | ) |
359 | | } |
360 | | |
361 | | /// Write an `Rgba32FImage`. |
362 | | /// Assumes the writer is buffered. In most cases, |
363 | | /// you should wrap your writer in a `BufWriter` for best performance. |
364 | | fn write_rgba_image(write: impl Write + Seek, image: &Rgba32FImage) -> ImageResult<()> { |
365 | | write_buffer( |
366 | | write, |
367 | | bytemuck::cast_slice(image.as_raw().as_slice()), |
368 | | image.width(), |
369 | | image.height(), |
370 | | ExtendedColorType::Rgba32F, |
371 | | ) |
372 | | } |
373 | | |
374 | | /// Read the file from the specified path into an `Rgba32FImage`. |
375 | | fn read_as_rgba_image_from_file(path: impl AsRef<Path>) -> ImageResult<Rgba32FImage> { |
376 | | read_as_rgba_image(BufReader::new(File::open(path)?)) |
377 | | } |
378 | | |
379 | | /// Read the file from the specified path into an `Rgb32FImage`. |
380 | | fn read_as_rgb_image_from_file(path: impl AsRef<Path>) -> ImageResult<Rgb32FImage> { |
381 | | read_as_rgb_image(BufReader::new(File::open(path)?)) |
382 | | } |
383 | | |
384 | | /// Read the file from the specified path into an `Rgb32FImage`. |
385 | | fn read_as_rgb_image(read: impl BufRead + Seek) -> ImageResult<Rgb32FImage> { |
386 | | let decoder = OpenExrDecoder::with_alpha_preference(read, Some(false))?; |
387 | | let (width, height) = decoder.dimensions(); |
388 | | let buffer: Vec<f32> = decoder_to_vec(decoder)?; |
389 | | |
390 | | ImageBuffer::from_raw(width, height, buffer) |
391 | | // this should be the only reason for the "from raw" call to fail, |
392 | | // even though such a large allocation would probably cause an error much earlier |
393 | | .ok_or_else(|| { |
394 | | ImageError::Limits(LimitError::from_kind(LimitErrorKind::InsufficientMemory)) |
395 | | }) |
396 | | } |
397 | | |
398 | | /// Read the file from the specified path into an `Rgba32FImage`. |
399 | | fn read_as_rgba_image(read: impl BufRead + Seek) -> ImageResult<Rgba32FImage> { |
400 | | let decoder = OpenExrDecoder::with_alpha_preference(read, Some(true))?; |
401 | | let (width, height) = decoder.dimensions(); |
402 | | let buffer: Vec<f32> = decoder_to_vec(decoder)?; |
403 | | |
404 | | ImageBuffer::from_raw(width, height, buffer) |
405 | | // this should be the only reason for the "from raw" call to fail, |
406 | | // even though such a large allocation would probably cause an error much earlier |
407 | | .ok_or_else(|| { |
408 | | ImageError::Limits(LimitError::from_kind(LimitErrorKind::InsufficientMemory)) |
409 | | }) |
410 | | } |
411 | | |
412 | | #[test] |
413 | | fn compare_exr_hdr() { |
414 | | if cfg!(not(feature = "hdr")) { |
415 | | eprintln!("warning: to run all the openexr tests, activate the hdr feature flag"); |
416 | | } |
417 | | |
418 | | #[cfg(feature = "hdr")] |
419 | | { |
420 | | use crate::codecs::hdr::HdrDecoder; |
421 | | |
422 | | let folder = BASE_PATH.iter().collect::<PathBuf>(); |
423 | | let reference_path = folder.clone().join("overexposed gradient.hdr"); |
424 | | let exr_path = folder |
425 | | .clone() |
426 | | .join("overexposed gradient - data window equals display window.exr"); |
427 | | |
428 | | let hdr_decoder = |
429 | | HdrDecoder::new(BufReader::new(File::open(reference_path).unwrap())).unwrap(); |
430 | | let hdr: Rgb32FImage = match DynamicImage::from_decoder(hdr_decoder).unwrap() { |
431 | | DynamicImage::ImageRgb32F(image) => image, |
432 | | _ => panic!("expected rgb32f image"), |
433 | | }; |
434 | | |
435 | | let exr_pixels: Rgb32FImage = read_as_rgb_image_from_file(exr_path).unwrap(); |
436 | | assert_eq!(exr_pixels.dimensions(), hdr.dimensions()); |
437 | | |
438 | | for (expected, found) in hdr.pixels().zip(exr_pixels.pixels()) { |
439 | | for (expected, found) in expected.0.iter().zip(found.0.iter()) { |
440 | | // the large tolerance seems to be caused by |
441 | | // the RGBE u8x4 pixel quantization of the hdr image format |
442 | | assert!( |
443 | | (expected - found).abs() < 0.1, |
444 | | "expected {expected}, found {found}" |
445 | | ); |
446 | | } |
447 | | } |
448 | | } |
449 | | } |
450 | | |
451 | | #[test] |
452 | | fn roundtrip_rgba() { |
453 | | let mut next_random = vec![1.0, 0.0, -1.0, -3.15, 27.0, 11.0, 31.0] |
454 | | .into_iter() |
455 | | .cycle(); |
456 | | let mut next_random = move || next_random.next().unwrap(); |
457 | | |
458 | | let generated_image: Rgba32FImage = ImageBuffer::from_fn(9, 31, |_x, _y| { |
459 | | Rgba([next_random(), next_random(), next_random(), next_random()]) |
460 | | }); |
461 | | |
462 | | let mut bytes = vec![]; |
463 | | write_rgba_image(Cursor::new(&mut bytes), &generated_image).unwrap(); |
464 | | let decoded_image = read_as_rgba_image(Cursor::new(bytes)).unwrap(); |
465 | | |
466 | | debug_assert_eq!(generated_image, decoded_image); |
467 | | } |
468 | | |
469 | | #[test] |
470 | | fn roundtrip_rgb() { |
471 | | let mut next_random = vec![1.0, 0.0, -1.0, -3.15, 27.0, 11.0, 31.0] |
472 | | .into_iter() |
473 | | .cycle(); |
474 | | let mut next_random = move || next_random.next().unwrap(); |
475 | | |
476 | | let generated_image: Rgb32FImage = ImageBuffer::from_fn(9, 31, |_x, _y| { |
477 | | Rgb([next_random(), next_random(), next_random()]) |
478 | | }); |
479 | | |
480 | | let mut bytes = vec![]; |
481 | | write_rgb_image(Cursor::new(&mut bytes), &generated_image).unwrap(); |
482 | | let decoded_image = read_as_rgb_image(Cursor::new(bytes)).unwrap(); |
483 | | |
484 | | debug_assert_eq!(generated_image, decoded_image); |
485 | | } |
486 | | |
487 | | #[test] |
488 | | fn compare_rgba_rgb() { |
489 | | let exr_path = BASE_PATH |
490 | | .iter() |
491 | | .collect::<PathBuf>() |
492 | | .join("overexposed gradient - data window equals display window.exr"); |
493 | | |
494 | | let rgb: Rgb32FImage = read_as_rgb_image_from_file(&exr_path).unwrap(); |
495 | | let rgba: Rgba32FImage = read_as_rgba_image_from_file(&exr_path).unwrap(); |
496 | | |
497 | | assert_eq!(rgba.dimensions(), rgb.dimensions()); |
498 | | |
499 | | for (Rgb(rgb), Rgba(rgba)) in rgb.pixels().zip(rgba.pixels()) { |
500 | | assert_eq!(rgb, &rgba[..3]); |
501 | | } |
502 | | } |
503 | | |
504 | | #[test] |
505 | | fn compare_cropped() { |
506 | | // like in photoshop, exr images may have layers placed anywhere in a canvas. |
507 | | // we don't want to load the pixels from the layer, but we want to load the pixels from the canvas. |
508 | | // a layer might be smaller than the canvas, in that case the canvas should be transparent black |
509 | | // where no layer was covering it. a layer might also be larger than the canvas, |
510 | | // these pixels should be discarded. |
511 | | // |
512 | | // in this test we want to make sure that an |
513 | | // auto-cropped image will be reproduced to the original. |
514 | | |
515 | | let exr_path = BASE_PATH.iter().collect::<PathBuf>(); |
516 | | let original = exr_path.clone().join("cropping - uncropped original.exr"); |
517 | | let cropped = exr_path |
518 | | .clone() |
519 | | .join("cropping - data window differs display window.exr"); |
520 | | |
521 | | // smoke-check that the exr files are actually not the same |
522 | | { |
523 | | let original_exr = read_first_flat_layer_from_file(&original).unwrap(); |
524 | | let cropped_exr = read_first_flat_layer_from_file(&cropped).unwrap(); |
525 | | assert_eq!( |
526 | | original_exr.attributes.display_window, |
527 | | cropped_exr.attributes.display_window |
528 | | ); |
529 | | assert_ne!( |
530 | | original_exr.layer_data.attributes.layer_position, |
531 | | cropped_exr.layer_data.attributes.layer_position |
532 | | ); |
533 | | assert_ne!(original_exr.layer_data.size, cropped_exr.layer_data.size); |
534 | | } |
535 | | |
536 | | // check that they result in the same image |
537 | | let original: Rgba32FImage = read_as_rgba_image_from_file(&original).unwrap(); |
538 | | let cropped: Rgba32FImage = read_as_rgba_image_from_file(&cropped).unwrap(); |
539 | | assert_eq!(original.dimensions(), cropped.dimensions()); |
540 | | |
541 | | // the following is not a simple assert_eq, as in case of an error, |
542 | | // the whole image would be printed to the console, which takes forever |
543 | | assert!(original.pixels().zip(cropped.pixels()).all(|(a, b)| a == b)); |
544 | | } |
545 | | } |