| 1 | |
| 2 | //! Write an exr image to a file. |
| 3 | //! |
| 4 | //! First, call `my_image.write()`. The resulting value can be customized, like this: |
| 5 | //! ```no_run |
| 6 | //! use exr::prelude::*; |
| 7 | //! # let my_image: FlatImage = unimplemented!(); |
| 8 | //! |
| 9 | //! my_image.write() |
| 10 | //! .on_progress(|progress| println!("progress: {:.1}", progress*100.0)) |
| 11 | //! .to_file("image.exr").unwrap(); |
| 12 | //! ``` |
| 13 | //! |
| 14 | |
| 15 | pub mod layersrust-analyzer cargo exr 1.74.0 image/write/layers/; |
| 16 | pub mod samplesrust-analyzer cargo exr 1.74.0 image/write/samples/; |
| 17 | pub mod channelsrust-analyzer cargo exr 1.74.0 image/write/channels/; |
| 18 | |
| 19 | |
| 20 | |
| 21 | use craterust-analyzer cargo exr 1.74.0 crate/::metarust-analyzer cargo exr 1.74.0 meta/::Headersrust-analyzer cargo exr 1.74.0 meta/Headers#; |
| 22 | use craterust-analyzer cargo exr 1.74.0 crate/::errorrust-analyzer cargo exr 1.74.0 error/::UnitResultrust-analyzer cargo exr 1.74.0 error/UnitResult#; |
| 23 | 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/::{Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#, BufWriterrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufwriter/BufWriter#}; |
| 24 | use craterust-analyzer cargo exr 1.74.0 crate/::iorust-analyzer cargo exr 1.74.0 io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#; |
| 25 | use craterust-analyzer cargo exr 1.74.0 crate/::imagerust-analyzer cargo exr 1.74.0 image/::{Imagerust-analyzer cargo exr 1.74.0 image/Image#, ignore_progressrust-analyzer cargo exr 1.74.0 image/ignore_progress()., SpecificChannelsrust-analyzer cargo exr 1.74.0 image/SpecificChannels#, IntoSamplerust-analyzer cargo exr 1.74.0 image/IntoSample#}; |
| 26 | use craterust-analyzer cargo exr 1.74.0 crate/::imagerust-analyzer cargo exr 1.74.0 image/::writerust-analyzer cargo exr 1.74.0 image/write/::layersrust-analyzer cargo exr 1.74.0 image/write/layers/::{WritableLayersrust-analyzer cargo exr 1.74.0 image/write/layers/WritableLayers#, LayersWriterrust-analyzer cargo exr 1.74.0 image/write/layers/LayersWriter#}; |
| 27 | use craterust-analyzer cargo exr 1.74.0 crate/::mathrust-analyzer cargo exr 1.74.0 math/::Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#; |
| 28 | use craterust-analyzer cargo exr 1.74.0 crate/::blockrust-analyzer cargo exr 1.74.0 block/::writerrust-analyzer cargo exr 1.74.0 block/writer/::ChunksWriterrust-analyzer cargo exr 1.74.0 block/writer/ChunksWriter#; |
| 29 | |
| 30 | /// An oversimplified function for "just write the damn file already" use cases. |
| 31 | /// Have a look at the examples to see how you can write an image with more flexibility (it's not that hard). |
| 32 | /// Use `write_rgb_file` if you do not need an alpha channel. |
| 33 | /// |
| 34 | /// Each of `R`, `G`, `B` and `A` can be either `f16`, `f32`, `u32`, or `Sample`. |
| 35 | // TODO explain pixel tuple f32,f16,u32 |
| 36 | pub fn write_rgba_filerust-analyzer cargo exr 1.74.0 image/write/write_rgba_file().<R,G,B,A>( |
| 37 | path: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>, width: usize, height: usize, |
| 38 | colors: impl Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# + Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(usize, usize) -> (R, G, B, A) |
| 39 | ) -> UnitResultrust-analyzer cargo exr 1.74.0 error/UnitResult# |
| 40 | where R: IntoSamplerust-analyzer cargo exr 1.74.0 image/IntoSample#, G: IntoSamplerust-analyzer cargo exr 1.74.0 image/IntoSample#, B: IntoSamplerust-analyzer cargo exr 1.74.0 image/IntoSample#, A: IntoSamplerust-analyzer cargo exr 1.74.0 image/IntoSample#, |
| 41 | { |
| 42 | let channels = SpecificChannelsrust-analyzer cargo exr 1.74.0 image/SpecificChannels#::rgbarust-analyzer cargo exr 1.74.0 image/impl#[`SpecificChannels<SampleStorage, (ChannelDescription, ChannelDescription, ChannelDescription, ChannelDescription)>`]rgba().(|Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(x,y)| colors(x,y)); |
| 43 | Imagerust-analyzer cargo exr 1.74.0 image/Image#::from_channelsrust-analyzer cargo exr 1.74.0 image/impl#[`Image<Layer<ChannelData>>`]from_channels().((width, height), channels).writerust-analyzer cargo exr 1.74.0 image/write/impl#[`&'img Image<WritableLayers>`][`WritableImage<'img, WritableLayers>`]write().().to_filerust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`]to_file().(path) |
| 44 | } |
| 45 | |
| 46 | /// An oversimplified function for "just write the damn file already" use cases. |
| 47 | /// Have a look at the examples to see how you can write an image with more flexibility (it's not that hard). |
| 48 | /// Use `write_rgb_file` if you do not need an alpha channel. |
| 49 | /// |
| 50 | /// Each of `R`, `G`, and `B` can be either `f16`, `f32`, `u32`, or `Sample`. |
| 51 | // TODO explain pixel tuple f32,f16,u32 |
| 52 | pub fn write_rgb_filerust-analyzer cargo exr 1.74.0 image/write/write_rgb_file().<R,G,B>( |
| 53 | path: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>, width: usize, height: usize, |
| 54 | colors: impl Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# + Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(usize, usize) -> (R, G, B) |
| 55 | ) -> UnitResultrust-analyzer cargo exr 1.74.0 error/UnitResult# |
| 56 | where R: IntoSamplerust-analyzer cargo exr 1.74.0 image/IntoSample#, G: IntoSamplerust-analyzer cargo exr 1.74.0 image/IntoSample#, B: IntoSamplerust-analyzer cargo exr 1.74.0 image/IntoSample# |
| 57 | { |
| 58 | let channels = SpecificChannelsrust-analyzer cargo exr 1.74.0 image/SpecificChannels#::rgbrust-analyzer cargo exr 1.74.0 image/impl#[`SpecificChannels<SampleStorage, (ChannelDescription, ChannelDescription, ChannelDescription)>`]rgb().(|Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(x,y)| colors(x,y)); |
| 59 | Imagerust-analyzer cargo exr 1.74.0 image/Image#::from_channelsrust-analyzer cargo exr 1.74.0 image/impl#[`Image<Layer<ChannelData>>`]from_channels().((width, height), channels).writerust-analyzer cargo exr 1.74.0 image/write/impl#[`&'img Image<WritableLayers>`][`WritableImage<'img, WritableLayers>`]write().().to_filerust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`]to_file().(path) |
| 60 | } |
| 61 | |
| 62 | |
| 63 | |
| 64 | /// Enables an image to be written to a file. Call `image.write()` where this trait is implemented. |
| 65 | pub trait WritableImagerust-analyzer cargo exr 1.74.0 image/write/WritableImage#<'img, WritableLayers>: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# { |
| 66 | |
| 67 | /// Create a temporary writer which can be configured and used to write the image to a file. |
| 68 | fn writerust-analyzer cargo exr 1.74.0 image/write/WritableImage#write().(self) -> WriteImageWithOptionsrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#<'img, WritableLayers, fn(f64)>; |
| 69 | } |
| 70 | |
| 71 | impl<'img, WritableLayers> WritableImagerust-analyzer cargo exr 1.74.0 image/write/WritableImage#<'img, WritableLayers> for &'img Imagerust-analyzer cargo exr 1.74.0 image/Image#<WritableLayers> { |
| 72 | fn writerust-analyzer cargo exr 1.74.0 image/write/impl#[`&'img Image<WritableLayers>`][`WritableImage<'img, WritableLayers>`]write().(self) -> WriteImageWithOptionsrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#<'img, WritableLayers, fn(f64)> { |
| 73 | WriteImageWithOptionsrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions# { |
| 74 | imagerust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#image.: self, |
| 75 | check_compatibilityrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#check_compatibility.: true, |
| 76 | |
| 77 | #[cfg(not(feature = "rayon"))] |
| 78 | parallelrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#parallel.: false, |
| 79 | |
| 80 | #[cfg(feature = "rayon")] |
| 81 | parallelrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#parallel.: true, |
| 82 | |
| 83 | on_progressrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#on_progress.: ignore_progressrust-analyzer cargo exr 1.74.0 image/ignore_progress(). |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// A temporary writer which can be configured and used to write an image to a file. |
| 89 | // temporary writer with options |
| 90 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, 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:)] |
| 91 | pub struct WriteImageWithOptionsrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#<'img, Layers, OnProgress> { |
| 92 | imagerust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#image.: &'img Imagerust-analyzer cargo exr 1.74.0 image/Image#<Layers>, |
| 93 | on_progressrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#on_progress.: OnProgress, |
| 94 | check_compatibilityrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#check_compatibility.: bool, |
| 95 | parallelrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#parallel.: bool, |
| 96 | } |
| 97 | |
| 98 | |
| 99 | impl<'img, L, F> WriteImageWithOptionsrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#<'img, L, F> |
| 100 | where L: WritableLayersrust-analyzer cargo exr 1.74.0 image/write/layers/WritableLayers#<'img>, F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(f64) |
| 101 | { |
| 102 | /// Generate file meta data for this image. The meta data structure is close to the data in the file. |
| 103 | pub fn infer_meta_datarust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`]infer_meta_data().(&self) -> Headersrust-analyzer cargo exr 1.74.0 meta/Headers# { // TODO this should perform all validity checks? and none after that? |
| 104 | self.imagerust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#image..layer_datarust-analyzer cargo exr 1.74.0 image/Image#layer_data..infer_headersrust-analyzer cargo exr 1.74.0 image/write/layers/WritableLayers#infer_headers().(&self.imagerust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#image..attributesrust-analyzer cargo exr 1.74.0 image/Image#attributes.) |
| 105 | } |
| 106 | |
| 107 | /// Do not compress multiple pixel blocks on multiple threads at once. |
| 108 | /// Might use less memory and synchronization, but will be slower in most situations. |
| 109 | pub fn non_parallelrust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`]non_parallel().(self) -> Selfrust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`] { Selfrust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`] { parallelrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#parallel.: false, ..self } } |
| 110 | |
| 111 | /// Skip some checks that ensure a file can be opened by other exr software. |
| 112 | /// For example, it is no longer checked that no two headers or two attributes have the same name, |
| 113 | /// which might be an expensive check for images with an exorbitant number of headers. |
| 114 | /// |
| 115 | /// If you write an uncompressed file and need maximum speed, it might save a millisecond to disable the checks, |
| 116 | /// if you know that your file is not invalid any ways. I do not recommend this though, |
| 117 | /// as the file might not be readably by any other exr library after that. |
| 118 | /// __You must care for not producing an invalid file yourself.__ |
| 119 | pub fn skip_compatibility_checksrust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`]skip_compatibility_checks().(self) -> Selfrust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`] { Selfrust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`] { check_compatibilityrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#check_compatibility.: false, ..self } } |
| 120 | |
| 121 | /// Specify a function to be called regularly throughout the writing process. |
| 122 | /// Replaces all previously specified progress functions in this reader. |
| 123 | pub fn on_progressrust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`]on_progress().<OnProgress>(self, on_progress: OnProgress) -> WriteImageWithOptionsrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#<'img, L, OnProgress> |
| 124 | where OnProgress: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(f64) |
| 125 | { |
| 126 | WriteImageWithOptionsrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions# { |
| 127 | on_progressrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#on_progress., |
| 128 | imagerust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#image.: self.imagerust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#image., |
| 129 | check_compatibilityrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#check_compatibility.: self.check_compatibilityrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#check_compatibility., |
| 130 | parallelrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#parallel.: self.parallelrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#parallel. |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /// Write the exr image to a file. |
| 135 | /// Use `to_unbuffered` instead, if you do not have a file. |
| 136 | /// If an error occurs, attempts to delete the partially written file. |
| 137 | #[inline] |
| 138 | #[must_use] |
| 139 | pub fn to_filerust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`]to_file().(self, path: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>) -> UnitResultrust-analyzer cargo exr 1.74.0 error/UnitResult# { |
| 140 | craterust-analyzer cargo exr 1.74.0 crate/::iorust-analyzer cargo exr 1.74.0 io/::attempt_delete_file_on_write_errorrust-analyzer cargo exr 1.74.0 io/attempt_delete_file_on_write_error().(path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(), move |write| |
| 141 | self.to_unbufferedrust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`]to_unbuffered().(write) |
| 142 | ) |
| 143 | } |
| 144 | |
| 145 | /// Buffer the writer and then write the exr image to it. |
| 146 | /// Use `to_buffered` instead, if your writer is an in-memory buffer. |
| 147 | /// Use `to_file` instead, if you have a file path. |
| 148 | /// If your writer cannot seek, you can write to an in-memory vector of bytes first, using `to_buffered`. |
| 149 | #[inline] |
| 150 | #[must_use] |
| 151 | pub fn to_unbufferedrust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`]to_unbuffered().(self, unbuffered: impl Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# + Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#) -> UnitResultrust-analyzer cargo exr 1.74.0 error/UnitResult# { |
| 152 | self.to_bufferedrust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`]to_buffered().(BufWriterrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufwriter/BufWriter#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufwriter/impl#[`BufWriter<W>`]new().(unbuffered)) |
| 153 | } |
| 154 | |
| 155 | /// Write the exr image to a writer. |
| 156 | /// Use `to_file` instead, if you have a file path. |
| 157 | /// Use `to_unbuffered` instead, if this is not an in-memory writer. |
| 158 | /// If your writer cannot seek, you can write to an in-memory vector of bytes first. |
| 159 | #[must_use] |
| 160 | pub fn to_bufferedrust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`]to_buffered().(self, write: impl Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# + Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#) -> UnitResultrust-analyzer cargo exr 1.74.0 error/UnitResult# { |
| 161 | let headers = self.infer_meta_datarust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`]infer_meta_data().(); |
| 162 | let layers = self.imagerust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#image..layer_datarust-analyzer cargo exr 1.74.0 image/Image#layer_data..create_writerrust-analyzer cargo exr 1.74.0 image/write/layers/WritableLayers#create_writer().(&headers); |
| 163 | |
| 164 | craterust-analyzer cargo exr 1.74.0 crate/::blockrust-analyzer cargo exr 1.74.0 block/::writerust-analyzer cargo exr 1.74.0 block/write().( |
| 165 | write, headers, self.check_compatibilityrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#check_compatibility., |
| 166 | move |meta, chunk_writer|{ |
| 167 | let blocks = meta.collect_ordered_block_datarust-analyzer cargo exr 1.74.0 meta/impl#[MetaData]collect_ordered_block_data().(|block_index| |
| 168 | layers.extract_uncompressed_blockrust-analyzer cargo exr 1.74.0 image/write/layers/LayersWriter#extract_uncompressed_block().(&meta.headersrust-analyzer cargo exr 1.74.0 meta/MetaData#headers., block_index) |
| 169 | ); |
| 170 | |
| 171 | let chunk_writer = chunk_writer.on_progressrust-analyzer cargo exr 1.74.0 block/writer/ChunksWriter#on_progress().(self.on_progressrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#on_progress.); |
| 172 | if self.parallelrust-analyzer cargo exr 1.74.0 image/write/WriteImageWithOptions#parallel. { |
| 173 | #[cfg(not(feature = "rayon"))] |
| 174 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(craterust-analyzer cargo exr 1.74.0 crate/::errorrust-analyzer cargo exr 1.74.0 error/::Errorrust-analyzer cargo exr 1.74.0 error/Error#::unsupported("parallel compression requires the rayon feature")); |
| 175 | |
| 176 | #[cfg(feature = "rayon")] |
| 177 | chunk_writer.compress_all_blocks_parallelrust-analyzer cargo exr 1.74.0 block/writer/ChunksWriter#compress_all_blocks_parallel().(&meta, blocks)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 178 | } |
| 179 | else { chunk_writer.compress_all_blocks_sequentialrust-analyzer cargo exr 1.74.0 block/writer/ChunksWriter#compress_all_blocks_sequential().(&meta, blocks)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; } |
| 180 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 181 | } |
| 182 | ) |
| 183 | } |
| 184 | } |
| 185 |