1
2pub mod read;
27pub mod write;
28pub mod crop;
29pub mod pixel_vec;
30pub mod recursive;
31use crate::meta::header::{ImageAttributes, LayerAttributes};
35use crate::meta::attribute::{Text, LineOrder};
36use half::f16;
37use crate::math::{Vec2, RoundingMode};
38use crate::compression::Compression;
39use smallvec::{SmallVec};
40use crate::error::Error;
41
42pub(crate) fn ignore_progress(_progress: f64){}
44
45pub type AnyImage = Image<Layers<AnyChannels<Levels<FlatSamples>>>>;
48
49pub type FlatImage = Image<Layers<AnyChannels<FlatSamples>>>;
52
53pub type PixelLayersImage<Storage, Channels> = Image<Layers<SpecificChannels<Storage, Channels>>>;
55
56pub type PixelImage<Storage, Channels> = Image<Layer<SpecificChannels<Storage, Channels>>>;
58
59pub type RgbaLayersImage<Storage> = PixelLayersImage<Storage, RgbaChannels>;
61
62pub type RgbaImage<Storage> = PixelImage<Storage, RgbaChannels>;
64
65pub type RgbaChannels = (ChannelDescription, ChannelDescription, ChannelDescription, Option<ChannelDescription>);
68
69pub type RgbChannels = (ChannelDescription, ChannelDescription, ChannelDescription);
71
72#[derive(Debug, Clone, PartialEq)]
75pub struct Image<Layers> {
76
77 pub attributes: ImageAttributes,
82
83 pub layer_data: Layers,
86}
87
88pub type Layers<Channels> = SmallVec<[Layer<Channels>; 2]>;
90
91#[derive(Debug, Clone, PartialEq)]
94pub struct Layer<Channels> {
95
96 pub channel_data: Channels,
98
99 pub attributes: LayerAttributes,
104
105 pub size: Vec2<usize>,
108
109 pub encoding: Encoding
111}
112
113#[derive(Copy, Clone, Debug, PartialEq)]
115pub struct Encoding {
116
117 pub compression: Compression,
120
121 pub blocks: Blocks,
125
126 pub line_order: LineOrder,
130}
131
132#[derive(Copy, Clone, Debug, PartialEq, Eq)]
134pub enum Blocks {
135
136 ScanLines,
139
140 Tiles (Vec2<usize>)
147}
148
149
150#[derive(Debug, Clone, PartialEq, Eq)]
154pub struct SpecificChannels<Pixels, ChannelsDescription> {
155
156 pub channels: ChannelsDescription, pub pixels: Pixels, }
164
165
166#[derive(Debug, Clone, PartialEq)]
169pub struct AnyChannels<Samples> {
170
171 pub list: SmallVec<[AnyChannel<Samples>; 4]>
174}
175
176#[derive(Debug, Clone, PartialEq)]
179pub struct AnyChannel<Samples> {
180
181 pub name: Text,
183
184 pub sample_data: Samples,
187
188 pub quantize_linearly: bool,
194
195 pub sampling: Vec2<usize>,
201}
202
203#[derive(Debug, Clone, PartialEq, Eq)]
206pub enum Levels<Samples> {
207
208 Singular(Samples),
211
212 Mip
214 {
215 rounding_mode: RoundingMode,
217
218 level_data: LevelMaps<Samples>
220 },
221
222 Rip
224 {
225 rounding_mode: RoundingMode,
227
228 level_data: RipMaps<Samples>
230 },
231}
232
233pub type LevelMaps<Samples> = Vec<Samples>;
236
237#[derive(Debug, Clone, PartialEq, Eq)]
242pub struct RipMaps<Samples> {
243
244 pub map_data: LevelMaps<Samples>,
246
247 pub level_count: Vec2<usize>,
249}
250
251
252#[derive(Clone, PartialEq)] pub enum FlatSamples {
269
270 F16(Vec<f16>),
272
273 F32(Vec<f32>),
275
276 U32(Vec<u32>),
278}
279
280
281use crate::block::samples::*;
289use crate::meta::attribute::*;
290use crate::error::Result;
291use crate::block::samples::Sample;
292use crate::image::write::channels::*;
293use crate::image::write::layers::WritableLayers;
294use crate::image::write::samples::{WritableSamples};
295use crate::meta::{mip_map_levels, rip_map_levels};
296use crate::io::Data;
297use crate::image::recursive::{NoneMore, Recursive, IntoRecursive};
298use std::marker::PhantomData;
299use std::ops::Not;
300use crate::image::validate_results::{ValidationOptions};
301
302
303impl<Channels> Layer<Channels> {
304 pub fn absolute_bounds(&self) -> IntegerBounds {
306 IntegerBounds::new(self.attributes.layer_position, self.size)
307 }
308}
309
310
311impl<SampleStorage, Channels> SpecificChannels<SampleStorage, Channels> {
312 pub fn new(channels: Channels, source_samples: SampleStorage) -> Self
316 where
317 SampleStorage: GetPixel,
318 SampleStorage::Pixel: IntoRecursive,
319 Channels: Sync + Clone + IntoRecursive,
320 <Channels as IntoRecursive>::Recursive: WritableChannelsDescription<<SampleStorage::Pixel as IntoRecursive>::Recursive>,
321 {
322 SpecificChannels { channels, pixels: source_samples }
323 }
324}
325
326pub trait IntoSample: IntoNativeSample {
329
330 const PREFERRED_SAMPLE_TYPE: SampleType;
332}
333
334impl IntoSample for f16 { const PREFERRED_SAMPLE_TYPE: SampleType = SampleType::F16; }
335impl IntoSample for f32 { const PREFERRED_SAMPLE_TYPE: SampleType = SampleType::F32; }
336impl IntoSample for u32 { const PREFERRED_SAMPLE_TYPE: SampleType = SampleType::U32; }
337
338#[derive(Debug)]
342pub struct SpecificChannelsBuilder<RecursiveChannels, RecursivePixel> {
343 channels: RecursiveChannels,
344 px: PhantomData<RecursivePixel>
345}
346
347pub trait CheckDuplicates {
350
351 fn already_contains(&self, name: &Text) -> bool;
353}
354
355impl CheckDuplicates for NoneMore {
356 fn already_contains(&self, _: &Text) -> bool { false }
357}
358
359impl<Inner: CheckDuplicates> CheckDuplicates for Recursive<Inner, ChannelDescription> {
360 fn already_contains(&self, name: &Text) -> bool {
361 &self.value.name == name || self.inner.already_contains(name)
362 }
363}
364
365impl SpecificChannels<(),()>
366{
367 pub fn build() -> SpecificChannelsBuilder<NoneMore, NoneMore> {
371 SpecificChannelsBuilder { channels: NoneMore, px: Default::default() }
372 }
373}
374
375impl<RecursiveChannels: CheckDuplicates, RecursivePixel> SpecificChannelsBuilder<RecursiveChannels, RecursivePixel>
376{
377 pub fn with_channel<Sample: IntoSample>(self, name: impl Into<Text>)
385 -> SpecificChannelsBuilder<Recursive<RecursiveChannels, ChannelDescription>, Recursive<RecursivePixel, Sample>>
386 {
387 self.with_channel_details::<Sample>(ChannelDescription::named(name, Sample::PREFERRED_SAMPLE_TYPE))
388 }
389
390 pub fn with_channel_details<Sample: Into<Sample>>(self, channel: ChannelDescription)
396 -> SpecificChannelsBuilder<Recursive<RecursiveChannels, ChannelDescription>, Recursive<RecursivePixel, Sample>>
397 {
398 assert!(self.channels.already_contains(&channel.name).not(), "channel name `{}` is duplicate", channel.name);
400
401 SpecificChannelsBuilder {
402 channels: Recursive::new(self.channels, channel),
403 px: PhantomData::default()
404 }
405 }
406
407 pub fn with_pixels<Pixels>(self, get_pixel: Pixels) -> SpecificChannels<Pixels, RecursiveChannels>
414 where Pixels: GetPixel, <Pixels as GetPixel>::Pixel: IntoRecursive<Recursive=RecursivePixel>,
415 {
416 SpecificChannels {
417 channels: self.channels,
418 pixels: get_pixel
419 }
420 }
421
422 pub fn with_pixel_fn<Pixel, Pixels>(self, get_pixel: Pixels) -> SpecificChannels<Pixels, RecursiveChannels>
431 where Pixels: Sync + Fn(Vec2<usize>) -> Pixel, Pixel: IntoRecursive<Recursive=RecursivePixel>,
432 {
433 SpecificChannels {
434 channels: self.channels,
435 pixels: get_pixel
436 }
437 }
438}
439
440impl<SampleStorage> SpecificChannels<
441 SampleStorage, (ChannelDescription, ChannelDescription, ChannelDescription, ChannelDescription)
442>
443{
444
445 pub fn rgba<R, G, B, A>(source_samples: SampleStorage) -> Self
450 where R: IntoSample, G: IntoSample,
451 B: IntoSample, A: IntoSample,
452 SampleStorage: GetPixel<Pixel=(R, G, B, A)>
453 {
454 SpecificChannels {
455 channels: (
456 ChannelDescription::named("R", R::PREFERRED_SAMPLE_TYPE),
457 ChannelDescription::named("G", G::PREFERRED_SAMPLE_TYPE),
458 ChannelDescription::named("B", B::PREFERRED_SAMPLE_TYPE),
459 ChannelDescription::named("A", A::PREFERRED_SAMPLE_TYPE),
460 ),
461 pixels: source_samples
462 }
463 }
464}
465
466impl<SampleStorage> SpecificChannels<
467 SampleStorage, (ChannelDescription, ChannelDescription, ChannelDescription)
468>
469{
470
471 pub fn rgb<R, G, B>(source_samples: SampleStorage) -> Self
476 where R: IntoSample, G: IntoSample, B: IntoSample,
477 SampleStorage: GetPixel<Pixel=(R, G, B)>
478 {
479 SpecificChannels {
480 channels: (
481 ChannelDescription::named("R", R::PREFERRED_SAMPLE_TYPE),
482 ChannelDescription::named("G", G::PREFERRED_SAMPLE_TYPE),
483 ChannelDescription::named("B", B::PREFERRED_SAMPLE_TYPE),
484 ),
485 pixels: source_samples
486 }
487 }
488}
489
490
491pub type FlatSamplesPixel = SmallVec<[Sample; 8]>;
494
495impl Layer<AnyChannels<FlatSamples>> {
497
498 pub fn sample_vec_at(&self, position: Vec2<usize>) -> FlatSamplesPixel {
500 self.samples_at(position).collect()
501 }
502
503 pub fn samples_at(&self, position: Vec2<usize>) -> FlatSampleIterator<'_> {
505 FlatSampleIterator {
506 layer: self,
507 channel_index: 0,
508 position
509 }
510 }
511}
512
513#[derive(Debug, Copy, Clone, PartialEq)]
515pub struct FlatSampleIterator<'s> {
516 layer: &'s Layer<AnyChannels<FlatSamples>>,
517 channel_index: usize,
518 position: Vec2<usize>,
519}
520
521impl Iterator for FlatSampleIterator<'_> {
522 type Item = Sample;
523
524 fn next(&mut self) -> Option<Self::Item> {
525 if self.channel_index < self.layer.channel_data.list.len() {
526 let channel = &self.layer.channel_data.list[self.channel_index];
527 let sample = channel.sample_data.value_by_flat_index(self.position.flat_index_for_size(self.layer.size));
528 self.channel_index += 1;
529 Some(sample)
530 }
531 else { None }
532 }
533
534 fn nth(&mut self, pos: usize) -> Option<Self::Item> {
535 self.channel_index += pos;
536 self.next()
537 }
538
539 fn size_hint(&self) -> (usize, Option<usize>) {
540 let remaining = self.layer.channel_data.list.len().saturating_sub(self.channel_index);
541 (remaining, Some(remaining))
542 }
543}
544
545impl ExactSizeIterator for FlatSampleIterator<'_> {}
546
547impl<SampleData> AnyChannels<SampleData>{
548
549 pub fn sort(mut list: SmallVec<[AnyChannel<SampleData>; 4]>) -> Self {
551 list.sort_unstable_by_key(|channel| channel.name.clone()); Self { list }
553 }
554}
555
556impl<LevelSamples> Levels<LevelSamples> {
558
559 pub fn get_level(&self, level: Vec2<usize>) -> Result<&LevelSamples> {
561 match self {
562 Levels::Singular(block) => {
563 debug_assert_eq!(level, Vec2(0,0), "singular image cannot write leveled blocks bug");
564 Ok(block)
565 },
566
567 Levels::Mip { level_data, .. } => {
568 debug_assert_eq!(level.x(), level.y(), "mip map levels must be equal on x and y bug");
569 level_data.get(level.x()).ok_or(Error::invalid("block mip level index"))
570 },
571
572 Levels::Rip { level_data, .. } => {
573 level_data.get_by_level(level).ok_or(Error::invalid("block rip level index"))
574 }
575 }
576 }
577
578 pub fn get_level_mut(&mut self, level: Vec2<usize>) -> Result<&mut LevelSamples> {
581 match self {
582 Levels::Singular(ref mut block) => {
583 debug_assert_eq!(level, Vec2(0,0), "singular image cannot write leveled blocks bug");
584 Ok(block)
585 },
586
587 Levels::Mip { level_data, .. } => {
588 debug_assert_eq!(level.x(), level.y(), "mip map levels must be equal on x and y bug");
589 level_data.get_mut(level.x()).ok_or(Error::invalid("block mip level index"))
590 },
591
592 Levels::Rip { level_data, .. } => {
593 level_data.get_by_level_mut(level).ok_or(Error::invalid("block rip level index"))
594 }
595 }
596 }
597
598 pub fn levels_as_slice(&self) -> &[LevelSamples] {
600 match self {
601 Levels::Singular(data) => std::slice::from_ref(data),
602 Levels::Mip { level_data, .. } => level_data,
603 Levels::Rip { level_data, .. } => &level_data.map_data,
604 }
605 }
606
607 pub fn levels_as_slice_mut(&mut self) -> &mut [LevelSamples] {
609 match self {
610 Levels::Singular(data) => std::slice::from_mut(data),
611 Levels::Mip { level_data, .. } => level_data,
612 Levels::Rip { level_data, .. } => &mut level_data.map_data,
613 }
614 }
615
616 pub fn level_mode(&self) -> LevelMode {
628 match self {
629 Levels::Singular(_) => LevelMode::Singular,
630 Levels::Mip { .. } => LevelMode::MipMap,
631 Levels::Rip { .. } => LevelMode::RipMap,
632 }
633 }
634}
635
636impl<Samples> RipMaps<Samples> {
637
638 pub fn get_level_index(&self, level: Vec2<usize>) -> usize {
640 level.flat_index_for_size(self.level_count)
641 }
642
643 pub fn get_by_level(&self, level: Vec2<usize>) -> Option<&Samples> {
645 self.map_data.get(self.get_level_index(level))
646 }
647
648 pub fn get_by_level_mut(&mut self, level: Vec2<usize>) -> Option<&mut Samples> {
650 let index = self.get_level_index(level);
651 self.map_data.get_mut(index)
652 }
653}
654
655impl FlatSamples {
656
657 pub fn len(&self) -> usize {
660 match self {
661 FlatSamples::F16(vec) => vec.len(),
662 FlatSamples::F32(vec) => vec.len(),
663 FlatSamples::U32(vec) => vec.len(),
664 }
665 }
666
667 pub fn values_as_f32<'s>(&'s self) -> impl 's + Iterator<Item = f32> {
671 self.values().map(|sample| sample.to_f32())
672 }
673
674 pub fn values<'s>(&'s self) -> impl 's + Iterator<Item = Sample> {
678 (0..self.len()).map(move |index| self.value_by_flat_index(index))
679 }
680
681 pub fn value_by_flat_index(&self, index: usize) -> Sample {
685 match self {
686 FlatSamples::F16(vec) => Sample::F16(vec[index]),
687 FlatSamples::F32(vec) => Sample::F32(vec[index]),
688 FlatSamples::U32(vec) => Sample::U32(vec[index]),
689 }
690 }
691}
692
693
694impl<'s, ChannelData:'s> Layer<ChannelData> {
695
696 pub fn new(
699 dimensions: impl Into<Vec2<usize>>,
700 attributes: LayerAttributes,
701 encoding: Encoding,
702 channels: ChannelData
703 ) -> Self
704 where ChannelData: WritableChannels<'s>
705 {
706 Layer { channel_data: channels, attributes, size: dimensions.into(), encoding }
707 }
708
709 pub fn levels_with_resolution<'l, L>(&self, levels: &'l Levels<L>) -> Box<dyn 'l + Iterator<Item=(&'l L, Vec2<usize>)>> {
712 match levels {
713 Levels::Singular(level) => Box::new(std::iter::once((level, self.size))),
714
715 Levels::Mip { rounding_mode, level_data } => Box::new(level_data.iter().zip(
716 mip_map_levels(*rounding_mode, self.size)
717 .map(|(_index, size)| size)
718 )),
719
720 Levels::Rip { rounding_mode, level_data } => Box::new(level_data.map_data.iter().zip(
721 rip_map_levels(*rounding_mode, self.size)
722 .map(|(_index, size)| size)
723 )),
724 }
725 }
726}
727
728impl Encoding {
729
730 pub const UNCOMPRESSED: Encoding = Encoding {
733 compression: Compression::Uncompressed,
734 blocks: Blocks::ScanLines, line_order: LineOrder::Increasing };
737
738 pub const FAST_LOSSLESS: Encoding = Encoding {
741 compression: Compression::RLE,
742 blocks: Blocks::Tiles(Vec2(64, 64)), line_order: LineOrder::Unspecified
744 };
745
746 pub const SMALL_LOSSLESS: Encoding = Encoding {
748 compression: Compression::ZIP16,
749 blocks: Blocks::ScanLines, line_order: LineOrder::Increasing
751 };
752
753 pub const SMALL_FAST_LOSSLESS: Encoding = Encoding {
755 compression: Compression::PIZ,
756 blocks: Blocks::Tiles(Vec2(256, 256)),
757 line_order: LineOrder::Unspecified
758 };
759}
760
761impl Default for Encoding {
762 fn default() -> Self { Encoding::FAST_LOSSLESS }
763}
764
765impl<'s, LayerData: 's> Image<LayerData> where LayerData: WritableLayers<'s> {
766 pub fn new(image_attributes: ImageAttributes, layer_data: LayerData) -> Self {
768 Image { attributes: image_attributes, layer_data }
769 }
770}
771
772impl<'s, Channels: 's> Image<Layers<Channels>> where Channels: WritableChannels<'s> {
774 pub fn from_layers(image_attributes: ImageAttributes, layer_data: impl Into<Layers<Channels>>) -> Self {
776 Self::new(image_attributes, layer_data.into())
777 }
778}
779
780
781impl<'s, ChannelData:'s> Image<Layer<ChannelData>> where ChannelData: WritableChannels<'s> {
782
783 pub fn from_layer(layer: Layer<ChannelData>) -> Self {
785 let bounds = IntegerBounds::new(layer.attributes.layer_position, layer.size);
786 Self::new(ImageAttributes::new(bounds), layer)
787 }
788
789 pub fn from_encoded_channels(size: impl Into<Vec2<usize>>, encoding: Encoding, channels: ChannelData) -> Self {
791 Self::from_layer(Layer::new(size, LayerAttributes::default(), encoding, channels))
793 }
794
795 pub fn from_channels(size: impl Into<Vec2<usize>>, channels: ChannelData) -> Self {
797 Self::from_encoded_channels(size, Encoding::default(), channels)
798 }
799}
800
801
802impl Image<NoneMore> {
803
804 pub fn empty(attributes: ImageAttributes) -> Self { Self { attributes, layer_data: NoneMore } }
807}
808
809impl<'s, InnerLayers: 's> Image<InnerLayers> where
810 InnerLayers: WritableLayers<'s>,
811{
812 pub fn with_layer<NewChannels>(self, layer: Layer<NewChannels>)
815 -> Image<Recursive<InnerLayers, Layer<NewChannels>>>
816 where NewChannels: 's + WritableChannels<'s>
817 {
818 Image {
819 attributes: self.attributes,
820 layer_data: Recursive::new(self.layer_data, layer)
821 }
822 }
823}
824
825
826impl<'s, SampleData: 's> AnyChannel<SampleData> {
827
828 pub fn new(name: impl Into<Text>, sample_data: SampleData) -> Self where SampleData: WritableSamples<'s> {
835 let name: Text = name.into();
836
837 AnyChannel {
838 quantize_linearly: ChannelDescription::guess_quantization_linearity(&name),
839 name, sample_data,
840 sampling: Vec2(1, 1),
841 }
842 }
843
844 }
851
852impl std::fmt::Debug for FlatSamples {
853 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
854 if self.len() <= 6 {
855 match self {
856 FlatSamples::F16(vec) => vec.fmt(formatter),
857 FlatSamples::F32(vec) => vec.fmt(formatter),
858 FlatSamples::U32(vec) => vec.fmt(formatter),
859 }
860 }
861 else {
862 match self {
863 FlatSamples::F16(vec) => write!(formatter, "[f16; {}]", vec.len()),
864 FlatSamples::F32(vec) => write!(formatter, "[f32; {}]", vec.len()),
865 FlatSamples::U32(vec) => write!(formatter, "[u32; {}]", vec.len()),
866 }
867 }
868 }
869}
870
871
872
873pub mod validate_results {
877 use crate::prelude::*;
878 use smallvec::Array;
879 use crate::prelude::recursive::*;
880 use crate::image::write::samples::WritableSamples;
881 use std::ops::Not;
882 use crate::block::samples::IntoNativeSample;
883
884
885 pub trait ValidateResult {
888
889 fn assert_equals_result(&self, result: &Self) {
900 self.validate_result(result, ValidationOptions::default(), || String::new()).unwrap();
901 }
902
903 fn validate_result(
912 &self, lossy_result: &Self,
913 options: ValidationOptions,
914 context: impl Fn() -> String
917 ) -> ValidationResult;
918 }
919
920 #[derive(Default, Debug, Eq, PartialEq, Hash, Copy, Clone)]
922 pub struct ValidationOptions {
923 allow_lossy: bool,
924 nan_converted_to_zero: bool,
925 }
926
927 pub type ValidationResult = std::result::Result<(), String>;
929
930
931 impl<C> ValidateResult for Image<C> where C: ValidateResult {
932 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
933 if self.attributes != other.attributes { Err(location() + "| image > attributes") }
934 else { self.layer_data.validate_result(&other.layer_data, options, || location() + "| image > layer data") }
935 }
936 }
937
938 impl<S> ValidateResult for Layer<AnyChannels<S>>
939 where AnyChannel<S>: ValidateResult, S: for<'a> WritableSamples<'a>
940 {
941 fn validate_result(&self, other: &Self, _overridden: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
942 let location = || format!("{} (layer `{:?}`)", location(), self.attributes.layer_name);
943 if self.attributes != other.attributes { Err(location() + " > attributes") }
944 else if self.encoding != other.encoding { Err(location() + " > encoding") }
945 else if self.size != other.size { Err(location() + " > size") }
946 else if self.channel_data.list.len() != other.channel_data.list.len() { Err(location() + " > channel count") }
947 else {
948 for (own_chan, other_chan) in self.channel_data.list.iter().zip(other.channel_data.list.iter()) {
949 own_chan.validate_result(
950 other_chan,
951
952 ValidationOptions {
953 allow_lossy: other.encoding.compression
955 .is_lossless_for(other_chan.sample_data.sample_type()).not(),
956
957 nan_converted_to_zero: other.encoding.compression.supports_nan().not()
959 },
960
961 || format!("{} > channel `{}`", location(), own_chan.name)
962 )?;
963 }
964 Ok(())
965 }
966 }
967 }
968
969 impl<Px, Desc> ValidateResult for Layer<SpecificChannels<Px, Desc>>
970 where SpecificChannels<Px, Desc>: ValidateResult
971 {
972 fn validate_result(&self, other: &Self, _overridden: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
975 let location = || format!("{} (layer `{:?}`)", location(), self.attributes.layer_name);
976
977 if self.attributes != other.attributes { Err(location() + " > attributes") }
979 else if self.encoding != other.encoding { Err(location() + " > encoding") }
980 else if self.size != other.size { Err(location() + " > size") }
981 else {
982 let options = ValidationOptions {
983 allow_lossy: other.encoding.compression.may_loose_data(),nan_converted_to_zero: other.encoding.compression.supports_nan().not()
989 };
990
991 self.channel_data.validate_result(&other.channel_data, options, || location() + " > channel_data")?;
992 Ok(())
993 }
994 }
995 }
996
997 impl<S> ValidateResult for AnyChannels<S> where S: ValidateResult {
998 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
999 self.list.validate_result(&other.list, options, location)
1000 }
1001 }
1002
1003 impl<S> ValidateResult for AnyChannel<S> where S: ValidateResult {
1004 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
1005 if self.name != other.name { Err(location() + " > name") }
1006 else if self.quantize_linearly != other.quantize_linearly { Err(location() + " > quantize_linearly") }
1007 else if self.sampling != other.sampling { Err(location() + " > sampling") }
1008 else {
1009 self.sample_data.validate_result(&other.sample_data, options, || location() + " > sample_data")
1010 }
1011 }
1012 }
1013
1014 impl<Pxs, Chans> ValidateResult for SpecificChannels<Pxs, Chans> where Pxs: ValidateResult, Chans: Eq {
1015 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
1016 if self.channels != other.channels { Err(location() + " > specific channels") }
1017 else { self.pixels.validate_result(&other.pixels, options, || location() + " > specific pixels") }
1018 }
1019 }
1020
1021 impl<S> ValidateResult for Levels<S> where S: ValidateResult {
1022 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
1023 self.levels_as_slice().validate_result(&other.levels_as_slice(), options, || location() + " > levels")
1024 }
1025 }
1026
1027 impl ValidateResult for FlatSamples {
1028 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
1029 use FlatSamples::*;
1030 match (self, other) {
1031 (F16(values), F16(other_values)) => values.as_slice().validate_result(&other_values.as_slice(), options, ||location() + " > f16 samples"),
1032 (F32(values), F32(other_values)) => values.as_slice().validate_result(&other_values.as_slice(), options, ||location() + " > f32 samples"),
1033 (U32(values), U32(other_values)) => values.as_slice().validate_result(&other_values.as_slice(), options, ||location() + " > u32 samples"),
1034 (own, other) => Err(format!("{}: samples type mismatch. expected {:?}, found {:?}", location(), own.sample_type(), other.sample_type()))
1035 }
1036 }
1037 }
1038
1039 impl<T> ValidateResult for &[T] where T: ValidateResult {
1040 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
1041 if self.len() != other.len() { Err(location() + " count") }
1042 else {
1043 for (index, (slf, other)) in self.iter().zip(other.iter()).enumerate() {
1044 slf.validate_result(other, options, ||format!("{} element [{}] of {}", location(), index, self.len()))?;
1045 }
1046 Ok(())
1047 }
1048 }
1049 }
1050
1051 impl<A: Array> ValidateResult for SmallVec<A> where A::Item: ValidateResult {
1052 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
1053 self.as_slice().validate_result(&other.as_slice(), options, location)
1054 }
1055 }
1056
1057 impl<A> ValidateResult for Vec<A> where A: ValidateResult {
1058 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
1059 self.as_slice().validate_result(&other.as_slice(), options, location)
1060 }
1061 }
1062
1063 impl<A,B,C,D> ValidateResult for (A, B, C, D) where A: Clone+ ValidateResult, B: Clone+ ValidateResult, C: Clone+ ValidateResult, D: Clone+ ValidateResult {
1064 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
1065 self.clone().into_recursive().validate_result(&other.clone().into_recursive(), options, location)
1066 }
1067 }
1068
1069 impl<A,B,C> ValidateResult for (A, B, C) where A: Clone+ ValidateResult, B: Clone+ ValidateResult, C: Clone+ ValidateResult {
1070 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
1071 self.clone().into_recursive().validate_result(&other.clone().into_recursive(), options, location)
1072 }
1073 }
1074
1075 impl ValidateResult for NoneMore {
1089 fn validate_result(&self, _: &Self, _: ValidationOptions, _: impl Fn()->String) -> ValidationResult { Ok(()) }
1090 }
1091
1092 impl<Inner, T> ValidateResult for Recursive<Inner, T> where Inner: ValidateResult, T: ValidateResult {
1093 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
1094 self.value.validate_result(&other.value, options, &location).and_then(|()|
1095 self.inner.validate_result(&other.inner, options, &location)
1096 )
1097 }
1098 }
1099
1100 impl<S> ValidateResult for Option<S> where S: ValidateResult {
1101 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
1102 match (self, other) {
1103 (None, None) => Ok(()),
1104 (Some(value), Some(other)) => value.validate_result(other, options, location),
1105 _ => Err(location() + ": option mismatch")
1106 }
1107 }
1108 }
1109
1110 impl ValidateResult for f32 {
1111 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
1112 if self == other || (self.is_nan() && other.is_nan()) || (options.nan_converted_to_zero && !self.is_normal() && *other == 0.0) {
1113 return Ok(());
1114 }
1115
1116 if options.allow_lossy {
1117 let epsilon = 0.06;
1118 let max_difference = 0.1;
1119
1120 let adaptive_threshold = epsilon * (self.abs() + other.abs());
1121 let tolerance = adaptive_threshold.max(max_difference);
1122 let difference = (self - other).abs();
1123
1124 return if difference <= tolerance { Ok(()) }
1125 else { Err(format!("{}: expected ~{}, found {} (adaptive tolerance {})", location(), self, other, tolerance)) };
1126 }
1127
1128 Err(format!("{}: expected exactly {}, found {}", location(), self, other))
1129 }
1130 }
1131
1132 impl ValidateResult for f16 {
1133 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
1134 if self.to_bits() == other.to_bits() { Ok(()) } else {
1135 self.to_f32().validate_result(&other.to_f32(), options, location)
1136 }
1137 }
1138 }
1139
1140 impl ValidateResult for u32 {
1141 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
1142 if self == other { Ok(()) } else { self.to_f32().validate_result(&other.to_f32(), options, location)
1144 }
1145 }
1146 }
1147
1148 impl ValidateResult for Sample {
1149 fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn()->String) -> ValidationResult {
1150 use Sample::*;
1151 match (self, other) {
1152 (F16(a), F16(b)) => a.validate_result(b, options, ||location() + " (f16)"),
1153 (F32(a), F32(b)) => a.validate_result(b, options, ||location() + " (f32)"),
1154 (U32(a), U32(b)) => a.validate_result(b, options, ||location() + " (u32)"),
1155 (_,_) => Err(location() + ": sample type mismatch")
1156 }
1157 }
1158 }
1159
1160
1161 #[cfg(test)]
1162 mod test_value_result {
1163 use std::f32::consts::*;
1164 use std::io::Cursor;
1165 use crate::image::pixel_vec::PixelVec;
1166 use crate::image::validate_results::{ValidateResult, ValidationOptions};
1167 use crate::meta::attribute::LineOrder::Increasing;
1168 use crate::image::{FlatSamples};
1169
1170 fn expect_valid<T>(original: &T, result: &T, allow_lossy: bool, nan_converted_to_zero: bool) where T: ValidateResult {
1171 original.validate_result(
1172 result,
1173 ValidationOptions { allow_lossy, nan_converted_to_zero },
1174 || String::new()
1175 ).unwrap();
1176 }
1177
1178 fn expect_invalid<T>(original: &T, result: &T, allow_lossy: bool, nan_converted_to_zero: bool) where T: ValidateResult {
1179 assert!(original.validate_result(
1180 result,
1181 ValidationOptions { allow_lossy, nan_converted_to_zero },
1182 || String::new()
1183 ).is_err());
1184 }
1185
1186 #[test]
1187 fn test_f32(){
1188 let original:&[f32] = &[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, -20.4, f32::NAN];
1189 let lossy:&[f32] = &[0.0, 0.2, 0.2, 0.3, 0.4, 0.5, -20.5, f32::NAN];
1190
1191 expect_valid(&original, &original, true, true);
1192 expect_valid(&original, &original, true, false);
1193 expect_valid(&original, &original, false, true);
1194 expect_valid(&original, &original, false, false);
1195
1196 expect_invalid(&original, &lossy, false, false);
1197 expect_valid(&original, &lossy, true, false);
1198
1199 expect_invalid(&original, &&original[..original.len()-2], true, true);
1200
1201 expect_valid(&1_000_f32, &1_001_f32, true, false);
1203 expect_invalid(&1_000_f32, &1_200_f32, true, false);
1204
1205 expect_valid(&10_000_f32, &10_100_f32, true, false);
1206 expect_invalid(&10_000_f32, &12_000_f32, true, false);
1207
1208 expect_valid(&33_120_f32, &30_120_f32, true, false);
1209 expect_invalid(&33_120_f32, &20_120_f32, true, false);
1210 }
1211
1212 #[test]
1213 fn test_nan(){
1214 let original:&[f32] = &[ 0.0, f32::NAN, f32::NAN ];
1215 let lossy:&[f32] = &[ 0.0, f32::NAN, 0.0 ];
1216
1217 expect_valid(&original, &lossy, true, true);
1218 expect_invalid(&lossy, &original, true, true);
1219
1220 expect_valid(&lossy, &lossy, true, true);
1221 expect_valid(&lossy, &lossy, false, true);
1222 }
1223
1224 #[test]
1225 fn test_error(){
1226
1227 fn print_error<T: ValidateResult>(original: &T, lossy: &T, allow_lossy: bool){
1228 let message = original
1229 .validate_result(
1230 &lossy,
1231 ValidationOptions { allow_lossy, .. Default::default() },
1232 || String::new() )
1234 .unwrap_err();
1235
1236 println!("message: {}", message);
1237 }
1238
1239 let original:&[f32] = &[ 0.0, f32::NAN, f32::NAN ];
1240 let lossy:&[f32] = &[ 0.0, f32::NAN, 0.0 ];
1241 print_error(&original, &lossy, false);
1242
1243 print_error(&2.0, &1.0, true);
1244 print_error(&2.0, &1.0, false);
1245
1246 print_error(&FlatSamples::F32(vec![0.1,0.1]), &FlatSamples::F32(vec![0.1,0.2]), false);
1247 print_error(&FlatSamples::U32(vec![0,0]), &FlatSamples::F32(vec![0.1,0.2]), false);
1248
1249 {
1250 let image = crate::prelude::read_all_data_from_file("tests/images/valid/openexr/MultiResolution/Kapaa.exr").unwrap();
1251
1252 let mut mutated = image.clone();
1253 let samples = mutated.layer_data.first_mut().unwrap()
1254 .channel_data.list.first_mut().unwrap().sample_data.levels_as_slice_mut().first_mut().unwrap();
1255
1256 match samples {
1257 FlatSamples::F16(vals) => vals[100] = vals[1],
1258 FlatSamples::F32(vals) => vals[100] = vals[1],
1259 FlatSamples::U32(vals) => vals[100] = vals[1],
1260 }
1261
1262 print_error(&image, &mutated, false);
1263 }
1264
1265 }
1267
1268 #[test]
1269 fn test_uncompressed(){
1270 use crate::prelude::*;
1271
1272 let original_pixels: [(f32,f32,f32); 4] = [
1273 (0.0, -1.1, PI),
1274 (0.0, -1.1, TAU),
1275 (0.0, -1.1, f32::EPSILON),
1276 (f32::NAN, 10000.1, -1024.009),
1277 ];
1278
1279 let mut file_bytes = Vec::new();
1280 let original_image = Image::from_encoded_channels(
1281 (2,2),
1282 Encoding {
1283 compression: Compression::Uncompressed,
1284 line_order: Increasing, .. Encoding::default()
1286 },
1287 SpecificChannels::rgb(PixelVec::new(Vec2(2,2), original_pixels.to_vec()))
1288 );
1289
1290 original_image.write().to_buffered(Cursor::new(&mut file_bytes)).unwrap();
1291
1292 let lossy_image = read().no_deep_data().largest_resolution_level()
1293 .rgb_channels(PixelVec::<(f32,f32,f32)>::constructor, PixelVec::set_pixel)
1294 .first_valid_layer().all_attributes().from_buffered(Cursor::new(&file_bytes)).unwrap();
1295
1296 original_image.assert_equals_result(&original_image);
1297 lossy_image.assert_equals_result(&lossy_image);
1298 original_image.assert_equals_result(&lossy_image);
1299 lossy_image.assert_equals_result(&original_image);
1300 }
1301
1302 #[test]
1303 fn test_compiles(){
1304 use crate::prelude::*;
1305
1306 fn accepts_validatable_value(_: &impl ValidateResult){}
1307
1308 let object: Levels<FlatSamples> = Levels::Singular(FlatSamples::F32(Vec::default()));
1309 accepts_validatable_value(&object);
1310
1311 let object: AnyChannels<Levels<FlatSamples>> = AnyChannels::sort(SmallVec::default());
1312 accepts_validatable_value(&object);
1313
1314 let layer: Layer<AnyChannels<Levels<FlatSamples>>> = Layer::new((0,0), Default::default(), Default::default(), object);
1315 accepts_validatable_value(&layer);
1316
1317 let layers: Layers<AnyChannels<Levels<FlatSamples>>> = Default::default();
1318 accepts_validatable_value(&layers);
1319
1320 let object: Image<Layer<AnyChannels<Levels<FlatSamples>>>> = Image::from_layer(layer);
1321 object.assert_equals_result(&object);
1322 }
1323 }
1324}
1325
1326