|
| 1 | +use crate::{ |
| 2 | + error::{AsyncTiffError, AsyncTiffResult}, |
| 3 | + tiff::tags::SampleFormat, |
| 4 | +}; |
| 5 | + |
| 6 | +/// Result of a decoding process |
| 7 | +#[derive(Debug)] |
| 8 | +#[non_exhaustive] |
| 9 | +pub enum DecodingResult { |
| 10 | + /// A vector of unsigned bytes |
| 11 | + U8(Vec<u8>), |
| 12 | + /// A vector of unsigned words |
| 13 | + U16(Vec<u16>), |
| 14 | + /// A vector of 32 bit unsigned ints |
| 15 | + U32(Vec<u32>), |
| 16 | + /// A vector of 64 bit unsigned ints |
| 17 | + U64(Vec<u64>), |
| 18 | + /// A vector of 32 bit IEEE floats |
| 19 | + F32(Vec<f32>), |
| 20 | + /// A vector of 64 bit IEEE floats |
| 21 | + F64(Vec<f64>), |
| 22 | + /// A vector of 8 bit signed ints |
| 23 | + I8(Vec<i8>), |
| 24 | + /// A vector of 16 bit signed ints |
| 25 | + I16(Vec<i16>), |
| 26 | + /// A vector of 32 bit signed ints |
| 27 | + I32(Vec<i32>), |
| 28 | + /// A vector of 64 bit signed ints |
| 29 | + I64(Vec<i64>), |
| 30 | +} |
| 31 | + |
| 32 | +impl DecodingResult { |
| 33 | + pub fn new( |
| 34 | + sample_format: SampleFormat, |
| 35 | + bits_per_sample: u16, |
| 36 | + total_samples: usize, |
| 37 | + ) -> AsyncTiffResult<Self> { |
| 38 | + match sample_format { |
| 39 | + SampleFormat::Uint => match bits_per_sample { |
| 40 | + 8 => Ok(Self::U8(vec![0u8; total_samples])), |
| 41 | + 16 => Ok(Self::U16(vec![0u16; total_samples])), |
| 42 | + 32 => Ok(Self::U32(vec![0u32; total_samples])), |
| 43 | + 64 => Ok(Self::U64(vec![0u64; total_samples])), |
| 44 | + _ => Err(AsyncTiffError::General(format!( |
| 45 | + "cannot create DecodingResult for {bits_per_sample:?} bits per samples" |
| 46 | + ))), |
| 47 | + }, |
| 48 | + SampleFormat::Int => match bits_per_sample { |
| 49 | + 8 => Ok(Self::I8(vec![0i8; total_samples])), |
| 50 | + 16 => Ok(Self::I16(vec![0i16; total_samples])), |
| 51 | + 32 => Ok(Self::I32(vec![0i32; total_samples])), |
| 52 | + 64 => Ok(Self::I64(vec![0i64; total_samples])), |
| 53 | + _ => Err(AsyncTiffError::General(format!( |
| 54 | + "cannot create DecodingResult for {bits_per_sample:?} bits per samples" |
| 55 | + ))), |
| 56 | + }, |
| 57 | + SampleFormat::IEEEFP => match bits_per_sample { |
| 58 | + 32 => Ok(Self::F32(vec![0f32; total_samples])), |
| 59 | + 64 => Ok(Self::F64(vec![0f64; total_samples])), |
| 60 | + _ => Err(AsyncTiffError::General(format!( |
| 61 | + "cannot create float DecodingResult for {bits_per_sample:?} bits per samples" |
| 62 | + ))), |
| 63 | + }, |
| 64 | + format => Err(AsyncTiffError::General(format!( |
| 65 | + "Unsupported sample format {format:?}" |
| 66 | + ))), |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + pub fn u8_buf_mut(&mut self) -> &mut [u8] { |
| 71 | + match *self { |
| 72 | + DecodingResult::U8(ref mut buf) => bytemuck::cast_slice_mut(&mut buf[..]), |
| 73 | + DecodingResult::U16(ref mut buf) => bytemuck::cast_slice_mut(&mut buf[..]), |
| 74 | + DecodingResult::U32(ref mut buf) => bytemuck::cast_slice_mut(&mut buf[..]), |
| 75 | + DecodingResult::U64(ref mut buf) => bytemuck::cast_slice_mut(&mut buf[..]), |
| 76 | + DecodingResult::F32(ref mut buf) => bytemuck::cast_slice_mut(&mut buf[..]), |
| 77 | + DecodingResult::F64(ref mut buf) => bytemuck::cast_slice_mut(&mut buf[..]), |
| 78 | + DecodingResult::I8(ref mut buf) => bytemuck::cast_slice_mut(&mut buf[..]), |
| 79 | + DecodingResult::I16(ref mut buf) => bytemuck::cast_slice_mut(&mut buf[..]), |
| 80 | + DecodingResult::I32(ref mut buf) => bytemuck::cast_slice_mut(&mut buf[..]), |
| 81 | + DecodingResult::I64(ref mut buf) => bytemuck::cast_slice_mut(&mut buf[..]), |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + pub fn u8_buf(&self) -> &[u8] { |
| 86 | + match *self { |
| 87 | + DecodingResult::U8(ref buf) => bytemuck::cast_slice(&buf[..]), |
| 88 | + DecodingResult::U16(ref buf) => bytemuck::cast_slice(&buf[..]), |
| 89 | + DecodingResult::U32(ref buf) => bytemuck::cast_slice(&buf[..]), |
| 90 | + DecodingResult::U64(ref buf) => bytemuck::cast_slice(&buf[..]), |
| 91 | + DecodingResult::F32(ref buf) => bytemuck::cast_slice(&buf[..]), |
| 92 | + DecodingResult::F64(ref buf) => bytemuck::cast_slice(&buf[..]), |
| 93 | + DecodingResult::I8(ref buf) => bytemuck::cast_slice(&buf[..]), |
| 94 | + DecodingResult::I16(ref buf) => bytemuck::cast_slice(&buf[..]), |
| 95 | + DecodingResult::I32(ref buf) => bytemuck::cast_slice(&buf[..]), |
| 96 | + DecodingResult::I64(ref buf) => bytemuck::cast_slice(&buf[..]), |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + pub fn len(&self) -> usize { |
| 101 | + match self { |
| 102 | + DecodingResult::U8(v) => v.len(), |
| 103 | + DecodingResult::U16(v) => v.len(), |
| 104 | + DecodingResult::U32(v) => v.len(), |
| 105 | + DecodingResult::U64(v) => v.len(), |
| 106 | + DecodingResult::F32(v) => v.len(), |
| 107 | + DecodingResult::F64(v) => v.len(), |
| 108 | + DecodingResult::I8(v) => v.len(), |
| 109 | + DecodingResult::I16(v) => v.len(), |
| 110 | + DecodingResult::I32(v) => v.len(), |
| 111 | + DecodingResult::I64(v) => v.len(), |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + pub fn is_empty(&self) -> bool { |
| 116 | + match self { |
| 117 | + DecodingResult::U8(v) => v.is_empty(), |
| 118 | + DecodingResult::U16(v) => v.is_empty(), |
| 119 | + DecodingResult::U32(v) => v.is_empty(), |
| 120 | + DecodingResult::U64(v) => v.is_empty(), |
| 121 | + DecodingResult::F32(v) => v.is_empty(), |
| 122 | + DecodingResult::F64(v) => v.is_empty(), |
| 123 | + DecodingResult::I8(v) => v.is_empty(), |
| 124 | + DecodingResult::I16(v) => v.is_empty(), |
| 125 | + DecodingResult::I32(v) => v.is_empty(), |
| 126 | + DecodingResult::I64(v) => v.is_empty(), |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments