Initial i8 support

This commit is contained in:
HagaiHargil committed 2020-12-13 11:37:56 +02:00
1 parent 332ab1dc4d
commit aa13e31894
3 files changed
+58 -2

No files matched your search

+28 -1
View File
@@ -32,6 +32,8 @@ pub enum DecodingResult {
F32(Vec<f32>),
/// A vector of 64 bit IEEE floats
F64(Vec<f64>),
/// A vector of 8 bit signed ints
I8(Vec<i8>),
/// A vector of 16 bit signed ints
I16(Vec<i16>),
}
@@ -85,6 +87,14 @@ impl DecodingResult {
}
}
fn new_i8(size: usize, limits: &Limits) -> TiffResult<DecodingResult> {
if size > limits.decoding_buffer_size / std::mem::size_of::<i8>() {
Err(TiffError::LimitsExceeded)
} else {
Ok(DecodingResult::I8(vec![0; size]))
}
}
fn new_i16(size: usize, limits: &Limits) -> TiffResult<DecodingResult> {
if size > limits.decoding_buffer_size / 2 {
Err(TiffError::LimitsExceeded)
@@ -101,6 +111,7 @@ impl DecodingResult {
DecodingResult::U64(ref mut buf) => DecodingBuffer::U64(&mut buf[start..]),
DecodingResult::F32(ref mut buf) => DecodingBuffer::F32(&mut buf[start..]),
DecodingResult::F64(ref mut buf) => DecodingBuffer::F64(&mut buf[start..]),
DecodingResult::I8(ref mut buf) => DecodingBuffer::I8(&mut buf[start..]),
DecodingResult::I16(ref mut buf) => DecodingBuffer::I16(&mut buf[start..]),
}
}
@@ -120,6 +131,8 @@ pub enum DecodingBuffer<'a> {
F32(&'a mut [f32]),
/// A slice of 64 bit IEEE floats
F64(&'a mut [f64]),
/// A slice of 8 bits signed ints
I8(&'a mut [i8]),
/// A slice of 16 bits signed ints
I16(&'a mut [i16]),
}
@@ -133,6 +146,7 @@ impl<'a> DecodingBuffer<'a> {
DecodingBuffer::U64(ref buf) => buf.len(),
DecodingBuffer::F32(ref buf) => buf.len(),
DecodingBuffer::F64(ref buf) => buf.len(),
DecodingBuffer::I8(ref buf) => buf.len(),
DecodingBuffer::I16(ref buf) => buf.len(),
}
}
@@ -145,6 +159,7 @@ impl<'a> DecodingBuffer<'a> {
DecodingBuffer::U64(_) => 8,
DecodingBuffer::F32(_) => 4,
DecodingBuffer::F64(_) => 8,
DecodingBuffer::I8(_) => 1,
DecodingBuffer::I16(_) => 2,
}
}
@@ -160,6 +175,7 @@ impl<'a> DecodingBuffer<'a> {
DecodingBuffer::U64(ref mut buf) => DecodingBuffer::U64(buf),
DecodingBuffer::F32(ref mut buf) => DecodingBuffer::F32(buf),
DecodingBuffer::F64(ref mut buf) => DecodingBuffer::F64(buf),
DecodingBuffer::I8(ref mut buf) => DecodingBuffer::I8(buf),
DecodingBuffer::I16(ref mut buf) => DecodingBuffer::I16(buf),
}
}
@@ -273,6 +289,13 @@ impl Wrapping for u64 {
}
}
impl Wrapping for i8 {
fn wrapping_add(&self, other: Self) -> Self {
i8::wrapping_add(*self, other)
}
}
impl Wrapping for i16 {
fn wrapping_add(&self, other: Self) -> Self {
i16::wrapping_add(*self, other)
@@ -342,6 +365,9 @@ fn rev_hpredict(image: DecodingBuffer, size: (u32, u32), color_type: ColorType)
TiffUnsupportedError::HorizontalPredictor(color_type),
));
}
DecodingBuffer::I8(buf) => {
rev_hpredict_nsamp(buf, size, samples)?;
}
DecodingBuffer::I16(buf) => {
rev_hpredict_nsamp(buf, size, samples)?;
}
@@ -1152,7 +1178,8 @@ impl<R: Read + Seek> Decoder<R> {
)),
},
SampleFormat::Int => match max_sample_bits {
n if n <= 16 => DecodingResult::new_i16(buffer_size, &self.limits),
n if n <= 8 => DecodingResult::new_i8(buffer_size, &self.limits),
n if (n > 8) & (n <= 16) => DecodingResult::new_i16(buffer_size, &self.limits),
n => Err(TiffError::UnsupportedError(
TiffUnsupportedError::UnsupportedBitsPerChannel(n),
)),
+29
View File
@@ -48,6 +48,35 @@ pub trait EndianReader: Read {
Ok(())
}
/// Reads an i8
#[inline(always)]
fn read_i8(&mut self) -> Result<i8, io::Error> {
let mut n = [0u8; 1];
self.read_exact(&mut n)?;
Ok(match self.byte_order() {
ByteOrder::LittleEndian => i8::from_le_bytes(n),
ByteOrder::BigEndian => i8::from_be_bytes(n),
})
}
#[inline(always)]
fn read_i8_into(&mut self, buffer: &mut [i8]) -> Result<(), io::Error> {
self.read_exact(bytecast::i8_as_ne_mut_bytes(buffer))?;
match self.byte_order() {
ByteOrder::LittleEndian => {
for n in buffer {
*n = i8::from_le(*n);
}
}
ByteOrder::BigEndian => {
for n in buffer {
*n = i8::from_be(*n);
}
}
}
Ok(())
}
/// Reads an i16
#[inline(always)]
fn read_i16(&mut self) -> Result<i16, io::Error> {