| 1 | |
| 2 | #[macro_use] |
| 3 | extern crate smallvecrust-analyzer cargo exr 1.74.0 smallvec/; |
| 4 | extern crate randrust-analyzer cargo exr 1.74.0 rand/; |
| 5 | extern crate halfrust-analyzer cargo exr 1.74.0 half/; |
| 6 | |
| 7 | use randrust-analyzer cargo rand 0.8.5 crate/::Rngrust-analyzer cargo rand 0.8.5 rng/Rng#; |
| 8 | |
| 9 | // exr imports |
| 10 | extern crate exrrust-analyzer cargo exr 1.74.0 exr/; |
| 11 | |
| 12 | |
| 13 | /// Generate a noisy image and write it to a file, |
| 14 | /// also attaching some meta data. |
| 15 | fn mainrust-analyzer cargo exr 1.74.0 main().() { |
| 16 | use exrrust-analyzer cargo exr 1.74.0 crate/::preluderust-analyzer cargo exr 1.74.0 prelude/::*; |
| 17 | |
| 18 | fn generate_f16_vectorrust-analyzer cargo exr 1.74.0 generate_f16_vector().(size: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<usize>) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<f16rust-analyzer cargo half 2.6.0 binary16/f16#> { |
| 19 | let mut values = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ f16rust-analyzer cargo half 2.6.0 binary16/f16#::from_f32rust-analyzer cargo half 2.6.0 binary16/impl#[f16]from_f32().(0.5); size.arearust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]area().() ]; |
| 20 | |
| 21 | for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#(1024*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Mul<Self>`]mul().1024/rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Div<Self>`]div().3)/rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Div<Self>`]div().4 { |
| 22 | let index = randrust-analyzer cargo rand 0.8.5 crate/::thread_rngrust-analyzer cargo rand 0.8.5 rngs/thread/thread_rng().().gen_rangerust-analyzer cargo rand 0.8.5 rng/Rng#gen_range().(0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range# rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#values.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 23 | let value = 1.0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().randrust-analyzer cargo rand 0.8.5 crate/::randomrust-analyzer cargo rand 0.8.5 random().::<f32>() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Sub<Self>`]sub().1.0; |
| 24 | let value = if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().value.is_normalrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]is_normal().() || value rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().1000.0 { 1000.0 } else { value }; |
| 25 | values[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().index]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut(). = f16rust-analyzer cargo half 2.6.0 binary16/f16#::from_f32rust-analyzer cargo half 2.6.0 binary16/impl#[f16]from_f32().(value); |
| 26 | } |
| 27 | |
| 28 | values |
| 29 | } |
| 30 | |
| 31 | let size = (1024, 512); |
| 32 | |
| 33 | let r = AnyChannelrust-analyzer cargo exr 1.74.0 image/AnyChannel#::newrust-analyzer cargo exr 1.74.0 image/impl#[`AnyChannel<SampleData>`]new().( |
| 34 | "R", FlatSamplesrust-analyzer cargo exr 1.74.0 image/FlatSamples#::F16rust-analyzer cargo exr 1.74.0 image/FlatSamples#F16#(generate_f16_vectorrust-analyzer cargo exr 1.74.0 generate_f16_vector().(size.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())) |
| 35 | ); |
| 36 | |
| 37 | let g = AnyChannelrust-analyzer cargo exr 1.74.0 image/AnyChannel#::newrust-analyzer cargo exr 1.74.0 image/impl#[`AnyChannel<SampleData>`]new().( |
| 38 | "G", FlatSamplesrust-analyzer cargo exr 1.74.0 image/FlatSamples#::F16rust-analyzer cargo exr 1.74.0 image/FlatSamples#F16#(generate_f16_vectorrust-analyzer cargo exr 1.74.0 generate_f16_vector().(size.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())) |
| 39 | ); |
| 40 | |
| 41 | let b = AnyChannelrust-analyzer cargo exr 1.74.0 image/AnyChannel#::newrust-analyzer cargo exr 1.74.0 image/impl#[`AnyChannel<SampleData>`]new().( |
| 42 | "B", FlatSamplesrust-analyzer cargo exr 1.74.0 image/FlatSamples#::F32rust-analyzer cargo exr 1.74.0 image/FlatSamples#F32#(generate_f16_vectorrust-analyzer cargo exr 1.74.0 generate_f16_vector().(size.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()).into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(f16rust-analyzer cargo half 2.6.0 binary16/f16#::to_f32rust-analyzer cargo half 2.6.0 binary16/impl#[f16]to_f32().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()) |
| 43 | ); |
| 44 | |
| 45 | let a = AnyChannelrust-analyzer cargo exr 1.74.0 image/AnyChannel#::newrust-analyzer cargo exr 1.74.0 image/impl#[`AnyChannel<SampleData>`]new().( |
| 46 | "A", FlatSamplesrust-analyzer cargo exr 1.74.0 image/FlatSamples#::F32rust-analyzer cargo exr 1.74.0 image/FlatSamples#F32#(generate_f16_vectorrust-analyzer cargo exr 1.74.0 generate_f16_vector().(size.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()).into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(f16rust-analyzer cargo half 2.6.0 binary16/f16#::to_f32rust-analyzer cargo half 2.6.0 binary16/impl#[f16]to_f32().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()) |
| 47 | ); |
| 48 | |
| 49 | let mut layer_attributes = LayerAttributesrust-analyzer cargo exr 1.74.0 meta/header/LayerAttributes#::namedrust-analyzer cargo exr 1.74.0 meta/header/impl#[LayerAttributes]named().("test-image"); |
| 50 | layer_attributes.ownerrust-analyzer cargo exr 1.74.0 meta/header/LayerAttributes#owner. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Textrust-analyzer cargo exr 1.74.0 meta/attribute/Text#::fromrust-analyzer cargo exr 1.74.0 meta/attribute/impl#[Text][`From<&'s str>`]from().("It's you!")); |
| 51 | layer_attributes.commentsrust-analyzer cargo exr 1.74.0 meta/header/LayerAttributes#comments. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Textrust-analyzer cargo exr 1.74.0 meta/attribute/Text#::fromrust-analyzer cargo exr 1.74.0 meta/attribute/impl#[Text][`From<&'s str>`]from().("This image was procedurally generated")); |
| 52 | |
| 53 | let layer = Layerrust-analyzer cargo exr 1.74.0 image/Layer#::newrust-analyzer cargo exr 1.74.0 image/impl#[`Layer<ChannelData>`]new().( |
| 54 | size, |
| 55 | layer_attributes, |
| 56 | Encodingrust-analyzer cargo exr 1.74.0 image/Encoding#::defaultrust-analyzer cargo exr 1.74.0 image/impl#[Encoding][Default]default().(), |
| 57 | AnyChannelsrust-analyzer cargo exr 1.74.0 image/AnyChannels#::sortrust-analyzer cargo exr 1.74.0 image/impl#[`AnyChannels<SampleData>`]sort().(smallvecrust-analyzer cargo smallvec 1.15.1 smallvec!![ r, g, b, a ]), |
| 58 | ); |
| 59 | |
| 60 | // crop away transparent pixels from the border |
| 61 | let layer = layer |
| 62 | |
| 63 | // channel order is (a,b,g,r), as channels are already sorted |
| 64 | .crop_whererust-analyzer cargo exr 1.74.0 image/crop/impl#[T][`CropWhere<<T as InspectSample>::Sample>`]crop_where().(|samples| samples[rust-analyzer cargo smallvec 1.15.1 impl#[`SmallVec<A>`][`Index<I>`]index().0]rust-analyzer cargo smallvec 1.15.1 impl#[`SmallVec<A>`][`Index<I>`]index()..is_zerorust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]is_zero().()) |
| 65 | |
| 66 | // throw error if the image is 100% transparent pixels and should be removed |
| 67 | .or_none_if_emptyrust-analyzer cargo exr 1.74.0 image/crop/impl#[`CropResult<Cropped, Original>`]or_none_if_empty().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("image is empty and cannot be cropped"); |
| 68 | |
| 69 | let image = Imagerust-analyzer cargo exr 1.74.0 image/Image#::from_layerrust-analyzer cargo exr 1.74.0 image/impl#[`Image<Layer<ChannelData>>`]from_layer().(layer); |
| 70 | |
| 71 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("writing image {:#?}", image); |
| 72 | |
| 73 | image.writerust-analyzer cargo exr 1.74.0 image/write/impl#[`&'img Image<WritableLayers>`][`WritableImage<'img, WritableLayers>`]write().() |
| 74 | .on_progressrust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`]on_progress().(|progress| printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("progress: {:.1}", progress*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().100.0)) |
| 75 | .to_filerust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`]to_file().("noisy.exr").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 76 | |
| 77 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("created file noisy.exr"); |
| 78 | } |