Skip to content

Commit 2d35f0c

Browse files
committed
made floating point predictor(s) use
1 parent dc46f8a commit 2d35f0c

File tree

5 files changed

+161
-3
lines changed

5 files changed

+161
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 5
6+
}

Untitled.ipynb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "187138db-40ea-47d6-8c9a-892794d59fea",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": []
10+
}
11+
],
12+
"metadata": {
13+
"kernelspec": {
14+
"display_name": "",
15+
"name": ""
16+
},
17+
"language_info": {
18+
"name": ""
19+
}
20+
},
21+
"nbformat": 4,
22+
"nbformat_minor": 5
23+
}

a.out

15.8 KB
Binary file not shown.

src/decoding_result.rs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
}

src/predictor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ pub fn rev_predict_f16(input: &mut [u8], output: &mut [u8], samples: usize) {
325325
input[i] = input[i].wrapping_add(input[i - samples]);
326326
}
327327
// reverse byte shuffle and fix endianness
328-
for (i, chunk) in output.chunks_mut(2).enumerate() {
328+
for (i, chunk) in output.chunks_exact_mut(2).enumerate() {
329329
chunk.copy_from_slice(&u16::to_ne_bytes(
330330
// convert to native-endian
331331
// floating predictor is be-like
@@ -345,7 +345,7 @@ pub fn rev_predict_f32(input: &mut [u8], output: &mut [u8], samples: usize) {
345345
input[i] = input[i].wrapping_add(input[i - samples]);
346346
}
347347
// reverse byte shuffle and fix endianness
348-
for (i, chunk) in output.chunks_mut(4).enumerate() {
348+
for (i, chunk) in output.chunks_exact_mut(4).enumerate() {
349349
chunk.copy_from_slice(
350350
// convert to native-endian
351351
&u32::to_ne_bytes(
@@ -371,7 +371,7 @@ pub fn rev_predict_f64(input: &mut [u8], output: &mut [u8], samples: usize) {
371371
input[i] = input[i].wrapping_add(input[i - samples]);
372372
}
373373

374-
for (i, chunk) in output.chunks_mut(8).enumerate() {
374+
for (i, chunk) in output.chunks_exact_mut(8).enumerate() {
375375
chunk.copy_from_slice(
376376
// convert to native-endian
377377
&u64::to_ne_bytes(

0 commit comments

Comments
 (0)