@@ -17,7 +17,7 @@ use crate::parser::{
1717 IccChunk , ScanInfo ,
1818} ;
1919use crate :: upsampler:: Upsampler ;
20- use crate :: worker:: { PlatformWorker , RowData , Worker } ;
20+ use crate :: worker:: { PreferWorkerKind , RowData , Worker , with_worker } ;
2121
2222pub const MAX_COMPONENTS : usize = 4 ;
2323
@@ -191,7 +191,9 @@ impl<R: Read> Decoder<R> {
191191 ///
192192 /// If successful, the metadata can be obtained using the `info` method.
193193 pub fn read_info ( & mut self ) -> Result < ( ) > {
194- self . decode_internal ( true ) . map ( |_| ( ) )
194+ with_worker ( PreferWorkerKind :: Multithreaded , |worker| {
195+ self . decode_internal ( true , worker)
196+ } ) . map ( |_| ( ) )
195197 }
196198
197199 /// Configure the decoder to scale the image during decoding.
@@ -219,10 +221,16 @@ impl<R: Read> Decoder<R> {
219221
220222 /// Decodes the image and returns the decoded pixels if successful.
221223 pub fn decode ( & mut self ) -> Result < Vec < u8 > > {
222- self . decode_internal ( false )
224+ with_worker ( PreferWorkerKind :: Multithreaded , |worker| {
225+ self . decode_internal ( false , worker)
226+ } )
223227 }
224228
225- fn decode_internal ( & mut self , stop_after_metadata : bool ) -> Result < Vec < u8 > > {
229+ fn decode_internal (
230+ & mut self ,
231+ stop_after_metadata : bool ,
232+ worker : & mut dyn Worker ,
233+ ) -> Result < Vec < u8 > > {
226234 if stop_after_metadata && self . frame . is_some ( ) {
227235 // The metadata has already been read.
228236 return Ok ( Vec :: new ( ) ) ;
@@ -237,7 +245,6 @@ impl<R: Read> Decoder<R> {
237245
238246 let mut previous_marker = Marker :: SOI ;
239247 let mut pending_marker = None ;
240- let mut worker = None ;
241248 let mut scans_processed = 0 ;
242249 let mut planes = vec ! [
243250 Vec :: <u8 >:: new( ) ;
@@ -318,9 +325,6 @@ impl<R: Read> Decoder<R> {
318325 if self . frame . is_none ( ) {
319326 return Err ( Error :: Format ( "scan encountered before frame" . to_owned ( ) ) ) ;
320327 }
321- if worker. is_none ( ) {
322- worker = Some ( PlatformWorker :: new ( ) ?) ;
323- }
324328
325329 let frame = self . frame . clone ( ) . unwrap ( ) ;
326330 let scan = parse_sos ( & mut self . reader , & frame) ?;
@@ -383,7 +387,7 @@ impl<R: Read> Decoder<R> {
383387 }
384388
385389 let ( marker, data) =
386- self . decode_scan ( & frame, & scan, worker. as_mut ( ) . unwrap ( ) , & finished) ?;
390+ self . decode_scan ( & frame, & scan, worker, & finished) ?;
387391
388392 if let Some ( data) = data {
389393 for ( i, plane) in data
@@ -545,10 +549,6 @@ impl<R: Read> Decoder<R> {
545549 } ;
546550
547551 // Get the worker prepared
548- if worker. is_none ( ) {
549- worker = Some ( PlatformWorker :: new ( ) ?) ;
550- }
551- let worker = worker. as_mut ( ) . unwrap ( ) ;
552552 let row_data = RowData {
553553 index : i,
554554 component : component. clone ( ) ,
@@ -560,14 +560,17 @@ impl<R: Read> Decoder<R> {
560560 let coefficients_per_mcu_row = usize:: from ( component. block_size . width )
561561 * usize:: from ( component. vertical_sampling_factor )
562562 * 64 ;
563- for mcu_y in 0 ..frame. mcu_size . height {
564- let row_coefficients = {
563+
564+ let mut tasks = ( 0 ..frame. mcu_size . height )
565+ . map ( |mcu_y| {
565566 let offset = usize:: from ( mcu_y) * coefficients_per_mcu_row;
566- self . coefficients [ i] [ offset..offset + coefficients_per_mcu_row] . to_vec ( )
567- } ;
567+ let row_coefficients = self . coefficients [ i] [ offset..offset + coefficients_per_mcu_row] . to_vec ( ) ;
568+ ( i, row_coefficients)
569+ } ) ;
568570
569- worker. append_row ( ( i, row_coefficients) ) ?;
570- }
571+ // FIXME: additional potential work stealing opportunities for rayon case if we
572+ // also internally can parallelize over components.
573+ worker. append_rows ( & mut tasks) ?;
571574 planes[ i] = worker. get_result ( i) ?;
572575 }
573576 }
@@ -616,7 +619,7 @@ impl<R: Read> Decoder<R> {
616619 & mut self ,
617620 frame : & FrameInfo ,
618621 scan : & ScanInfo ,
619- worker : & mut PlatformWorker ,
622+ worker : & mut dyn Worker ,
620623 finished : & [ bool ; MAX_COMPONENTS ] ,
621624 ) -> Result < ( Option < Marker > , Option < Vec < Vec < u8 > > > ) > {
622625 assert ! ( scan. component_indices. len( ) <= MAX_COMPONENTS ) ;
@@ -871,6 +874,8 @@ impl<R: Read> Decoder<R> {
871874 )
872875 } ;
873876
877+ // FIXME: additional potential work stealing opportunities for rayon case if we
878+ // also internally can parallelize over components.
874879 worker. append_row ( ( i, row_coefficients) ) ?;
875880 }
876881 }
0 commit comments