| 1 | //! How to read samples (a grid of `f32`, `f16` or `u32` values). |
| 2 | |
| 3 | use craterust-analyzer cargo exr 1.74.0 crate/::imagerust-analyzer cargo exr 1.74.0 image/::*; |
| 4 | use craterust-analyzer cargo exr 1.74.0 crate/::metarust-analyzer cargo exr 1.74.0 meta/::headerrust-analyzer cargo exr 1.74.0 meta/header/::{Headerrust-analyzer cargo exr 1.74.0 meta/header/Header#}; |
| 5 | use craterust-analyzer cargo exr 1.74.0 crate/::errorrust-analyzer cargo exr 1.74.0 error/::{Resultrust-analyzer cargo exr 1.74.0 error/Result#, UnitResultrust-analyzer cargo exr 1.74.0 error/UnitResult#}; |
| 6 | use craterust-analyzer cargo exr 1.74.0 crate/::blockrust-analyzer cargo exr 1.74.0 block/::linesrust-analyzer cargo exr 1.74.0 block/lines/::LineRefrust-analyzer cargo exr 1.74.0 block/lines/LineRef#; |
| 7 | 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#; |
| 8 | use craterust-analyzer cargo exr 1.74.0 crate/::metarust-analyzer cargo exr 1.74.0 meta/::attributerust-analyzer cargo exr 1.74.0 meta/attribute/::{ChannelDescriptionrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#, SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#}; |
| 9 | use craterust-analyzer cargo exr 1.74.0 crate/::imagerust-analyzer cargo exr 1.74.0 image/::readrust-analyzer cargo exr 1.74.0 image/read/::any_channelsrust-analyzer cargo exr 1.74.0 image/read/any_channels/::{SamplesReaderrust-analyzer cargo exr 1.74.0 image/read/any_channels/SamplesReader#, ReadSamplesrust-analyzer cargo exr 1.74.0 image/read/any_channels/ReadSamples#}; |
| 10 | use craterust-analyzer cargo exr 1.74.0 crate/::imagerust-analyzer cargo exr 1.74.0 image/::readrust-analyzer cargo exr 1.74.0 image/read/::levelsrust-analyzer cargo exr 1.74.0 image/read/levels/::{ReadSamplesLevelrust-analyzer cargo exr 1.74.0 image/read/levels/ReadSamplesLevel#, ReadAllLevelsrust-analyzer cargo exr 1.74.0 image/read/levels/ReadAllLevels#, ReadLargestLevelrust-analyzer cargo exr 1.74.0 image/read/levels/ReadLargestLevel#}; |
| 11 | use craterust-analyzer cargo exr 1.74.0 crate/::blockrust-analyzer cargo exr 1.74.0 block/::chunkrust-analyzer cargo exr 1.74.0 block/chunk/::TileCoordinatesrust-analyzer cargo exr 1.74.0 block/chunk/TileCoordinates#; |
| 12 | // use crate::image::read::layers::ReadChannels; |
| 13 | |
| 14 | /// Specify to read only flat samples and no "deep data" |
| 15 | // FIXME do not throw error on deep data but just skip it! |
| 16 | #[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:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 17 | pub struct ReadFlatSamplesrust-analyzer cargo exr 1.74.0 image/read/samples/ReadFlatSamples#; |
| 18 | // pub struct ReadAnySamples; |
| 19 | |
| 20 | impl ReadFlatSamplesrust-analyzer cargo exr 1.74.0 image/read/samples/ReadFlatSamples# { |
| 21 | |
| 22 | // TODO |
| 23 | // e. g. `let sum = reader.any_channels_with(|sample, sum| sum += sample)` |
| 24 | // pub fn any_channels_with <S> (self, storage: S) -> { } |
| 25 | |
| 26 | /// Specify to read only the highest resolution level, skipping all smaller variations. |
| 27 | pub fn largest_resolution_levelrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[ReadFlatSamples]largest_resolution_level().(self) -> ReadLargestLevelrust-analyzer cargo exr 1.74.0 image/read/levels/ReadLargestLevel#<Selfrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[ReadFlatSamples]> { ReadLargestLevelrust-analyzer cargo exr 1.74.0 image/read/levels/ReadLargestLevel# { read_samplesrust-analyzer cargo exr 1.74.0 image/read/levels/ReadLargestLevel#read_samples.: self } } |
| 28 | |
| 29 | /// Specify to read all contained resolution levels from the image, if any. |
| 30 | pub fn all_resolution_levelsrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[ReadFlatSamples]all_resolution_levels().(self) -> ReadAllLevelsrust-analyzer cargo exr 1.74.0 image/read/levels/ReadAllLevels#<Selfrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[ReadFlatSamples]> { ReadAllLevelsrust-analyzer cargo exr 1.74.0 image/read/levels/ReadAllLevels# { read_samplesrust-analyzer cargo exr 1.74.0 image/read/levels/ReadAllLevels#read_samples.: self } } |
| 31 | |
| 32 | // TODO pub fn specific_resolution_level<F: Fn(&[Vec2<usize>])->usize >(self, select_level: F) -> ReadLevelBy<Self> { ReadAllLevels { read_samples: self } } |
| 33 | } |
| 34 | |
| 35 | |
| 36 | /*pub struct AnySamplesReader { TODO |
| 37 | resolution: Vec2<usize>, |
| 38 | samples: DeepAndFlatSamples |
| 39 | }*/ |
| 40 | |
| 41 | /// Processes pixel blocks from a file and accumulates them into a grid of samples, for example "Red" or "Alpha". |
| 42 | #[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:)] |
| 43 | pub struct FlatSamplesReaderrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader# { |
| 44 | levelrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader#level.: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<usize>, |
| 45 | resolutionrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader#resolution.: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<usize>, |
| 46 | samplesrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader#samples.: FlatSamplesrust-analyzer cargo exr 1.74.0 image/FlatSamples# |
| 47 | } |
| 48 | |
| 49 | |
| 50 | // only used when samples is directly inside a channel, without levels |
| 51 | impl ReadSamplesrust-analyzer cargo exr 1.74.0 image/read/any_channels/ReadSamples# for ReadFlatSamplesrust-analyzer cargo exr 1.74.0 image/read/samples/ReadFlatSamples# { |
| 52 | type Readerrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[ReadFlatSamples][ReadSamples]Reader# = FlatSamplesReaderrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader#; |
| 53 | |
| 54 | fn create_sample_readerrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[ReadFlatSamples][ReadSamples]create_sample_reader().(&self, header: &Headerrust-analyzer cargo exr 1.74.0 meta/header/Header#, channel: &ChannelDescriptionrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#) -> Resultrust-analyzer cargo exr 1.74.0 error/Result#<Selfrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[ReadFlatSamples][ReadSamples]::Readerrust-analyzer cargo exr 1.74.0 image/read/any_channels/ReadSamples#Reader#> { |
| 55 | self.create_samples_level_readerrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[ReadFlatSamples][ReadSamplesLevel]create_samples_level_reader().(header, channel, Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(0, 0), header.layer_sizerust-analyzer cargo exr 1.74.0 meta/header/Header#layer_size.) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl ReadSamplesLevelrust-analyzer cargo exr 1.74.0 image/read/levels/ReadSamplesLevel# for ReadFlatSamplesrust-analyzer cargo exr 1.74.0 image/read/samples/ReadFlatSamples# { |
| 60 | type Readerrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[ReadFlatSamples][ReadSamplesLevel]Reader# = FlatSamplesReaderrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader#; |
| 61 | |
| 62 | fn create_samples_level_readerrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[ReadFlatSamples][ReadSamplesLevel]create_samples_level_reader().(&self, _header: &Headerrust-analyzer cargo exr 1.74.0 meta/header/Header#, channel: &ChannelDescriptionrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#, level: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<usize>, resolution: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<usize>) -> Resultrust-analyzer cargo exr 1.74.0 error/Result#<Selfrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[ReadFlatSamples][ReadSamplesLevel]::Readerrust-analyzer cargo exr 1.74.0 image/read/levels/ReadSamplesLevel#Reader#> { |
| 63 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(FlatSamplesReaderrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader# { |
| 64 | levelrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader#level., resolutionrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader#resolution., // TODO sampling |
| 65 | samplesrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader#samples.: match channel.sample_typerust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#sample_type. { |
| 66 | SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F16rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F16# => FlatSamplesrust-analyzer cargo exr 1.74.0 image/FlatSamples#::F16rust-analyzer cargo exr 1.74.0 image/FlatSamples#F16#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![f16rust-analyzer cargo half 2.6.0 binary16/f16#::ZEROrust-analyzer cargo half 2.6.0 binary16/impl#[f16]ZERO.; resolution.arearust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]area().()]), |
| 67 | SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F32# => FlatSamplesrust-analyzer cargo exr 1.74.0 image/FlatSamples#::F32rust-analyzer cargo exr 1.74.0 image/FlatSamples#F32#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0.0; resolution.arearust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]area().()]), |
| 68 | SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::U32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#U32# => FlatSamplesrust-analyzer cargo exr 1.74.0 image/FlatSamples#::U32rust-analyzer cargo exr 1.74.0 image/FlatSamples#U32#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; resolution.arearust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]area().()]), |
| 69 | } |
| 70 | }) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | impl SamplesReaderrust-analyzer cargo exr 1.74.0 image/read/any_channels/SamplesReader# for FlatSamplesReaderrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader# { |
| 76 | type Samplesrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[FlatSamplesReader][SamplesReader]Samples# = FlatSamplesrust-analyzer cargo exr 1.74.0 image/FlatSamples#; |
| 77 | |
| 78 | fn filter_blockrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[FlatSamplesReader][SamplesReader]filter_block().(&self, tile: TileCoordinatesrust-analyzer cargo exr 1.74.0 block/chunk/TileCoordinates#) -> bool { |
| 79 | tile.level_indexrust-analyzer cargo exr 1.74.0 block/chunk/TileCoordinates#level_index. rust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`PartialEq<Self>`]eq().==rust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`PartialEq<Self>`]eq(). rust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`PartialEq<Self>`]eq().self.levelrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader#level. |
| 80 | } |
| 81 | |
| 82 | fn read_linerust-analyzer cargo exr 1.74.0 image/read/samples/impl#[FlatSamplesReader][SamplesReader]read_line().(&mut self, line: LineRefrust-analyzer cargo exr 1.74.0 block/lines/LineRef#<'_>) -> UnitResultrust-analyzer cargo exr 1.74.0 error/UnitResult# { |
| 83 | let index = line.locationrust-analyzer cargo exr 1.74.0 block/lines/LineSlice#location.; |
| 84 | let resolution = self.resolutionrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader#resolution.; |
| 85 | |
| 86 | // the index is generated by ourselves and must always be correct |
| 87 | debug_assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert_eq!!(index.levelrust-analyzer cargo exr 1.74.0 block/lines/LineIndex#level., self.levelrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader#level., "line should have been filtered"); |
| 88 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(index.positionrust-analyzer cargo exr 1.74.0 block/lines/LineIndex#position..xrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]x().() +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). index.sample_countrust-analyzer cargo exr 1.74.0 block/lines/LineIndex#sample_count. <= resolution.widthrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]width().(), "line index calculation bug"); |
| 89 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(index.positionrust-analyzer cargo exr 1.74.0 block/lines/LineIndex#position..yrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]y().() <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). resolution.heightrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]height().(), "line index calculation bug"); |
| 90 | debug_assert_nerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert_ne!!(resolution.0rust-analyzer cargo exr 1.74.0 math/Vec2#0., 0, "sample size bug"); |
| 91 | |
| 92 | let start_index = index.positionrust-analyzer cargo exr 1.74.0 block/lines/LineIndex#position..yrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]y().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().resolution.widthrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]width().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().index.positionrust-analyzer cargo exr 1.74.0 block/lines/LineIndex#position..xrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]x().(); |
| 93 | let end_index = start_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().index.sample_countrust-analyzer cargo exr 1.74.0 block/lines/LineIndex#sample_count.; |
| 94 | |
| 95 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!( |
| 96 | start_index <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). end_index && end_index <= self.samplesrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader#samples..lenrust-analyzer cargo exr 1.74.0 image/impl#[FlatSamples]len().(), |
| 97 | "for resolution {:?}, this is an invalid line: {:?}", |
| 98 | self.resolutionrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader#resolution., line.locationrust-analyzer cargo exr 1.74.0 block/lines/LineSlice#location. |
| 99 | ); |
| 100 | |
| 101 | match &mut self.samplesrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader#samples. { |
| 102 | FlatSamplesrust-analyzer cargo exr 1.74.0 image/FlatSamples#::F16rust-analyzer cargo exr 1.74.0 image/FlatSamples#F16#(samples) => |
| 103 | line.read_samples_into_slicerust-analyzer cargo exr 1.74.0 block/lines/impl#[`LineSlice<&[u8]>`]read_samples_into_slice().(&mut samples[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().start_index 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#end_index]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().) |
| 104 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("writing line bytes failed"), |
| 105 | |
| 106 | FlatSamplesrust-analyzer cargo exr 1.74.0 image/FlatSamples#::F32rust-analyzer cargo exr 1.74.0 image/FlatSamples#F32#(samples) => |
| 107 | line.read_samples_into_slicerust-analyzer cargo exr 1.74.0 block/lines/impl#[`LineSlice<&[u8]>`]read_samples_into_slice().(&mut samples[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().start_index 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#end_index]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().) |
| 108 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("writing line bytes failed"), |
| 109 | |
| 110 | FlatSamplesrust-analyzer cargo exr 1.74.0 image/FlatSamples#::U32rust-analyzer cargo exr 1.74.0 image/FlatSamples#U32#(samples) => |
| 111 | line.read_samples_into_slicerust-analyzer cargo exr 1.74.0 block/lines/impl#[`LineSlice<&[u8]>`]read_samples_into_slice().(&mut samples[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().start_index 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#end_index]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().) |
| 112 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("writing line bytes failed"), |
| 113 | } |
| 114 | |
| 115 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 116 | } |
| 117 | |
| 118 | fn into_samplesrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[FlatSamplesReader][SamplesReader]into_samples().(self) -> FlatSamplesrust-analyzer cargo exr 1.74.0 image/FlatSamples# { |
| 119 | self.samplesrust-analyzer cargo exr 1.74.0 image/read/samples/FlatSamplesReader#samples. |
| 120 | } |
| 121 | } |
| 122 |