11//! All IO functionality needed for TIFF decoding
2-
3- #[ cfg( feature = "fax" ) ]
4- use std:: collections:: VecDeque ;
52use std:: io:: { self , BufRead , BufReader , Read , Seek , Take } ;
63
74/// Byte order of the TIFF file.
@@ -281,8 +278,7 @@ impl<R: Read> Read for PackBitsReader<R> {
281278#[ cfg( feature = "fax" ) ]
282279pub struct Group4Reader < R : Read > {
283280 decoder : fax:: decoder:: Group4Decoder < io:: Bytes < io:: Take < R > > > ,
284- bits : bitvec:: vec:: BitVec < u8 , bitvec:: prelude:: Msb0 > ,
285- byte_buf : VecDeque < u8 > ,
281+ line_buf : io:: Cursor < Vec < u8 > > ,
286282 height : u16 ,
287283 width : u16 ,
288284 y : u16 ,
@@ -303,8 +299,7 @@ impl<R: Read> Group4Reader<R> {
303299 reader. take ( compressed_length) . bytes ( ) ,
304300 width,
305301 ) ?,
306- bits : bitvec:: vec:: BitVec :: new ( ) ,
307- byte_buf : VecDeque :: new ( ) ,
302+ line_buf : io:: Cursor :: new ( Vec :: with_capacity ( width. into ( ) ) ) ,
308303 width : width,
309304 height : height,
310305 y : 0 ,
@@ -315,27 +310,47 @@ impl<R: Read> Group4Reader<R> {
315310#[ cfg( feature = "fax" ) ]
316311impl < R : Read > Read for Group4Reader < R > {
317312 fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
318- if self . byte_buf . is_empty ( ) && self . y < self . height {
319- let next = self
320- . decoder
321- . advance ( )
322- . map_err ( |e| std:: io:: Error :: new ( std :: io :: ErrorKind :: Other , e ) ) ?;
313+ // Either we have not read any line or we are at the end of a line.
314+ if self . line_buf . position ( ) as usize == self . line_buf . get_ref ( ) . len ( )
315+ && self . y < self . height
316+ {
317+ let next = self . decoder . advance ( ) . map_err ( std:: io:: Error :: other ) ?;
323318
324319 match next {
325320 fax:: decoder:: DecodeStatus :: End => ( ) ,
326321 fax:: decoder:: DecodeStatus :: Incomplete => {
327322 self . y += 1 ;
323+
324+ // We known `transitions` yields exactly `self.width` items (per doc).
328325 let transitions = fax:: decoder:: pels ( self . decoder . transition ( ) , self . width ) ;
329- self . bits . extend ( transitions. map ( |c| match c {
326+
327+ let buffer = self . line_buf . get_mut ( ) ;
328+ buffer. resize ( usize:: from ( self . width ) . div_ceil ( 8 ) , 0u8 ) ;
329+
330+ let target = & mut buffer[ ..] ;
331+
332+ let mut bits = transitions. map ( |c| match c {
330333 fax:: Color :: Black => true ,
331334 fax:: Color :: White => false ,
332- } ) ) ;
333- self . byte_buf . extend ( self . bits . as_raw_slice ( ) ) ;
334- self . bits . clear ( ) ;
335+ } ) ;
336+
337+ // Assemble bits in MSB as per our library representation for buffer.
338+ for byte in target {
339+ let mut val = 0 ;
340+
341+ for ( idx, bit) in bits. by_ref ( ) . take ( 8 ) . enumerate ( ) {
342+ val |= u8:: from ( bit) << ( 7 - idx % 8 ) ;
343+ }
344+
345+ * byte = val;
346+ }
347+
348+ self . line_buf . set_position ( 0 ) ;
335349 }
336350 }
337351 }
338- self . byte_buf . read ( buf)
352+
353+ self . line_buf . read ( buf)
339354 }
340355}
341356
0 commit comments