From 345aff0629117ca4dae318157d530b3fd878e793 Mon Sep 17 00:00:00 2001 From: zroug <37004975+zroug@users.noreply.github.com> Date: Mon, 27 Aug 2018 23:05:16 +0200 Subject: [PATCH] Greatly improved performance for 16 bit images --- src/decoder/mod.rs | 10 ++++------ src/decoder/stream.rs | 8 ++++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index 2c7bdcd..48580b4 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -427,15 +427,13 @@ impl Decoder { } (ColorType::RGBA(16), DecodingBuffer::U16(ref mut buffer)) | (ColorType:: RGB(16), DecodingBuffer::U16(ref mut buffer)) => { - for datum in buffer[..bytes/2].iter_mut() { - *datum = try!(reader.read_u16()) - } + try!(reader.read_u16_into(&mut buffer[..bytes/2])); bytes/2 } (ColorType::Gray(16), DecodingBuffer::U16(ref mut buffer)) => { - for datum in buffer[..bytes/2].iter_mut() { - *datum = try!(reader.read_u16()); - if self.photometric_interpretation == PhotometricInterpretation::WhiteIsZero { + try!(reader.read_u16_into(&mut buffer[..bytes/2])); + if self.photometric_interpretation == PhotometricInterpretation::WhiteIsZero { + for datum in buffer[..bytes/2].iter_mut() { *datum = 0xffff - *datum } } diff --git a/src/decoder/stream.rs b/src/decoder/stream.rs index 090a833..c80b84e 100644 --- a/src/decoder/stream.rs +++ b/src/decoder/stream.rs @@ -28,6 +28,14 @@ pub trait EndianReader: Read { } } + #[inline(always)] + fn read_u16_into(&mut self, buffer: &mut [u16]) -> Result<(), io::Error> { + match self.byte_order() { + ByteOrder::LittleEndian => ::read_u16_into::(self, buffer), + ByteOrder::BigEndian => ::read_u16_into::(self, buffer) + } + } + /// Reads an u32 #[inline(always)] fn read_u32(&mut self) -> Result {