1
- use alloc:: borrow:: ToOwned ;
2
- use alloc:: sync:: Arc ;
3
- use alloc:: vec:: Vec ;
4
- use alloc:: { format, vec} ;
5
- use core:: cmp;
6
- use core:: mem;
7
- use core:: ops:: Range ;
8
- use std:: convert:: TryInto ;
9
- use std:: io:: Read ;
10
- use crate :: read_u8;
11
1
use crate :: error:: { Error , Result , UnsupportedFeature } ;
12
2
use crate :: huffman:: { fill_default_mjpeg_tables, HuffmanDecoder , HuffmanTable } ;
13
3
use crate :: marker:: Marker ;
@@ -16,8 +6,18 @@ use crate::parser::{
16
6
AdobeColorTransform , AppData , CodingProcess , Component , Dimensions , EntropyCoding , FrameInfo ,
17
7
IccChunk , ScanInfo ,
18
8
} ;
9
+ use crate :: read_u8;
19
10
use crate :: upsampler:: Upsampler ;
20
- use crate :: worker:: { PreferWorkerKind , RowData , Worker , with_worker} ;
11
+ use crate :: worker:: { compute_image_parallel, with_worker, PreferWorkerKind , RowData , Worker } ;
12
+ use alloc:: borrow:: ToOwned ;
13
+ use alloc:: sync:: Arc ;
14
+ use alloc:: vec:: Vec ;
15
+ use alloc:: { format, vec} ;
16
+ use core:: cmp;
17
+ use core:: mem;
18
+ use core:: ops:: Range ;
19
+ use std:: convert:: TryInto ;
20
+ use std:: io:: Read ;
21
21
22
22
pub const MAX_COMPONENTS : usize = 4 ;
23
23
@@ -202,7 +202,8 @@ impl<R: Read> Decoder<R> {
202
202
pub fn read_info ( & mut self ) -> Result < ( ) > {
203
203
with_worker ( PreferWorkerKind :: Multithreaded , |worker| {
204
204
self . decode_internal ( true , worker)
205
- } ) . map ( |_| ( ) )
205
+ } )
206
+ . map ( |_| ( ) )
206
207
}
207
208
208
209
/// Configure the decoder to scale the image during decoding.
@@ -395,8 +396,7 @@ impl<R: Read> Decoder<R> {
395
396
}
396
397
}
397
398
398
- let ( marker, data) =
399
- self . decode_scan ( & frame, & scan, worker, & finished) ?;
399
+ let ( marker, data) = self . decode_scan ( & frame, & scan, worker, & finished) ?;
400
400
401
401
if let Some ( data) = data {
402
402
for ( i, plane) in data
@@ -542,12 +542,16 @@ impl<R: Read> Decoder<R> {
542
542
let frame = self . frame . as_ref ( ) . unwrap ( ) ;
543
543
544
544
if {
545
- let required_mem = frame. components . len ( )
545
+ let required_mem = frame
546
+ . components
547
+ . len ( )
546
548
. checked_mul ( frame. output_size . width . into ( ) )
547
549
. and_then ( |m| m. checked_mul ( frame. output_size . height . into ( ) ) ) ;
548
550
required_mem. map_or ( true , |m| self . decoding_buffer_size_limit < m)
549
551
} {
550
- return Err ( Error :: Format ( "size of decoded image exceeds maximum allowed size" . to_owned ( ) ) ) ;
552
+ return Err ( Error :: Format (
553
+ "size of decoded image exceeds maximum allowed size" . to_owned ( ) ,
554
+ ) ) ;
551
555
}
552
556
553
557
// If we're decoding a progressive jpeg and a component is unfinished, render what we've got
@@ -579,12 +583,12 @@ impl<R: Read> Decoder<R> {
579
583
* usize:: from ( component. vertical_sampling_factor )
580
584
* 64 ;
581
585
582
- let mut tasks = ( 0 ..frame. mcu_size . height )
583
- . map ( | mcu_y| {
584
- let offset = usize :: from ( mcu_y ) * coefficients_per_mcu_row ;
585
- let row_coefficients = self . coefficients [ i] [ offset..offset + coefficients_per_mcu_row] . to_vec ( ) ;
586
- ( i, row_coefficients)
587
- } ) ;
586
+ let mut tasks = ( 0 ..frame. mcu_size . height ) . map ( |mcu_y| {
587
+ let offset = usize :: from ( mcu_y) * coefficients_per_mcu_row ;
588
+ let row_coefficients =
589
+ self . coefficients [ i] [ offset..offset + coefficients_per_mcu_row] . to_vec ( ) ;
590
+ ( i, row_coefficients)
591
+ } ) ;
588
592
589
593
// FIXME: additional potential work stealing opportunities for rayon case if we
590
594
// also internally can parallelize over components.
@@ -825,7 +829,9 @@ impl<R: Read> Decoder<R> {
825
829
& mut mcu_row_coefficients[ i] [ block_offset..block_offset + 64 ]
826
830
} else {
827
831
& mut dummy_block[ ..64 ]
828
- } . try_into ( ) . unwrap ( ) ;
832
+ }
833
+ . try_into ( )
834
+ . unwrap ( ) ;
829
835
830
836
if scan. successive_approximation_high == 0 {
831
837
decode_block (
@@ -1174,65 +1180,7 @@ fn compute_image(
1174
1180
}
1175
1181
}
1176
1182
1177
- #[ cfg( feature = "rayon" ) ]
1178
- fn compute_image_parallel (
1179
- components : & [ Component ] ,
1180
- data : Vec < Vec < u8 > > ,
1181
- output_size : Dimensions ,
1182
- is_jfif : bool ,
1183
- color_transform : Option < AdobeColorTransform > ,
1184
- ) -> Result < Vec < u8 > > {
1185
- use rayon:: prelude:: * ;
1186
-
1187
- let color_convert_func = choose_color_convert_func ( components. len ( ) , is_jfif, color_transform) ?;
1188
- let upsampler = Upsampler :: new ( components, output_size. width , output_size. height ) ?;
1189
- let line_size = output_size. width as usize * components. len ( ) ;
1190
- let mut image = vec ! [ 0u8 ; line_size * output_size. height as usize ] ;
1191
-
1192
- image
1193
- . par_chunks_mut ( line_size)
1194
- . with_max_len ( 1 )
1195
- . enumerate ( )
1196
- . for_each ( |( row, line) | {
1197
- upsampler. upsample_and_interleave_row (
1198
- & data,
1199
- row,
1200
- output_size. width as usize ,
1201
- line,
1202
- color_convert_func,
1203
- ) ;
1204
- } ) ;
1205
-
1206
- Ok ( image)
1207
- }
1208
-
1209
- #[ cfg( not( feature = "rayon" ) ) ]
1210
- fn compute_image_parallel (
1211
- components : & [ Component ] ,
1212
- data : Vec < Vec < u8 > > ,
1213
- output_size : Dimensions ,
1214
- is_jfif : bool ,
1215
- color_transform : Option < AdobeColorTransform > ,
1216
- ) -> Result < Vec < u8 > > {
1217
- let color_convert_func = choose_color_convert_func ( components. len ( ) , is_jfif, color_transform) ?;
1218
- let upsampler = Upsampler :: new ( components, output_size. width , output_size. height ) ?;
1219
- let line_size = output_size. width as usize * components. len ( ) ;
1220
- let mut image = vec ! [ 0u8 ; line_size * output_size. height as usize ] ;
1221
-
1222
- for ( row, line) in image. chunks_mut ( line_size) . enumerate ( ) {
1223
- upsampler. upsample_and_interleave_row (
1224
- & data,
1225
- row,
1226
- output_size. width as usize ,
1227
- line,
1228
- color_convert_func,
1229
- ) ;
1230
- }
1231
-
1232
- Ok ( image)
1233
- }
1234
-
1235
- fn choose_color_convert_func (
1183
+ pub ( crate ) fn choose_color_convert_func (
1236
1184
component_count : usize ,
1237
1185
_is_jfif : bool ,
1238
1186
color_transform : Option < AdobeColorTransform > ,
@@ -1255,7 +1203,7 @@ fn choose_color_convert_func(
1255
1203
None => {
1256
1204
// Assume CMYK because no APP14 marker was found
1257
1205
Ok ( color_convert_line_cmyk)
1258
- } ,
1206
+ }
1259
1207
}
1260
1208
}
1261
1209
_ => panic ! ( ) ,
0 commit comments