@@ -82,6 +82,21 @@ pub enum AdobeColorTransform {
82
82
YCCK ,
83
83
}
84
84
85
+ impl FrameInfo {
86
+ pub ( crate ) fn update_idct_size ( & mut self , idct_size : usize ) {
87
+ for component in & mut self . components {
88
+ component. dct_scale = idct_size;
89
+ }
90
+
91
+ update_component_sizes ( self . image_size , & mut self . components ) ;
92
+
93
+ self . output_size = Dimensions {
94
+ width : ( self . image_size . width as f32 * idct_size as f32 / 8.0 ) . ceil ( ) as u16 ,
95
+ height : ( self . image_size . height as f32 * idct_size as f32 / 8.0 ) . ceil ( ) as u16
96
+ } ;
97
+ }
98
+ }
99
+
85
100
fn read_length < R : Read > ( reader : & mut R , marker : Marker ) -> Result < usize > {
86
101
assert ! ( marker. has_length( ) ) ;
87
102
@@ -107,7 +122,7 @@ fn skip_bytes<R: Read>(reader: &mut R, length: usize) -> Result<()> {
107
122
}
108
123
109
124
// Section B.2.2
110
- pub fn parse_sof < R : Read > ( reader : & mut R , marker : Marker , requested_size : Option < Dimensions > ) -> Result < FrameInfo > {
125
+ pub fn parse_sof < R : Read > ( reader : & mut R , marker : Marker ) -> Result < FrameInfo > {
111
126
let length = read_length ( reader, marker) ?;
112
127
113
128
if length <= 6 {
@@ -159,10 +174,6 @@ pub fn parse_sof<R: Read>(reader: &mut R, marker: Marker, requested_size: Option
159
174
return Err ( Error :: Format ( "zero width in frame header" . to_owned ( ) ) ) ;
160
175
}
161
176
162
- let scale = if let Some ( req) = requested_size {
163
- crate :: idct:: choose_idct_size ( Dimensions { width, height } , req)
164
- } else { 8 } ;
165
-
166
177
let component_count = reader. read_u8 ( ) ?;
167
178
168
179
if component_count == 0 {
@@ -208,38 +219,45 @@ pub fn parse_sof<R: Read>(reader: &mut R, marker: Marker, requested_size: Option
208
219
horizontal_sampling_factor : horizontal_sampling_factor,
209
220
vertical_sampling_factor : vertical_sampling_factor,
210
221
quantization_table_index : quantization_table_index as usize ,
211
- dct_scale : scale ,
222
+ dct_scale : 8 ,
212
223
size : Dimensions { width : 0 , height : 0 } ,
213
224
block_size : Dimensions { width : 0 , height : 0 } ,
214
225
} ) ;
215
226
}
216
227
228
+ let mcu_size = update_component_sizes ( Dimensions { width, height } , & mut components) ;
229
+
230
+ Ok ( FrameInfo {
231
+ is_baseline : is_baseline,
232
+ is_differential : is_differential,
233
+ coding_process : coding_process,
234
+ entropy_coding : entropy_coding,
235
+ precision : precision,
236
+ image_size : Dimensions { width, height } ,
237
+ output_size : Dimensions { width, height } ,
238
+ mcu_size,
239
+ components : components,
240
+ } )
241
+ }
242
+
243
+ fn update_component_sizes ( size : Dimensions , components : & mut [ Component ] ) -> Dimensions {
217
244
let h_max = components. iter ( ) . map ( |c| c. horizontal_sampling_factor ) . max ( ) . unwrap ( ) ;
218
245
let v_max = components. iter ( ) . map ( |c| c. vertical_sampling_factor ) . max ( ) . unwrap ( ) ;
246
+
219
247
let mcu_size = Dimensions {
220
- width : ( width as f32 / ( h_max as f32 * 8.0 ) ) . ceil ( ) as u16 ,
221
- height : ( height as f32 / ( v_max as f32 * 8.0 ) ) . ceil ( ) as u16 ,
248
+ width : ( size . width as f32 / ( h_max as f32 * 8.0 ) ) . ceil ( ) as u16 ,
249
+ height : ( size . height as f32 / ( v_max as f32 * 8.0 ) ) . ceil ( ) as u16 ,
222
250
} ;
223
251
224
- for component in & mut components {
225
- component. size . width = ( width as f32 * component. horizontal_sampling_factor as f32 * component. dct_scale as f32 / ( h_max as f32 * 8.0 ) ) . ceil ( ) as u16 ;
226
- component. size . height = ( height as f32 * component. vertical_sampling_factor as f32 * component. dct_scale as f32 / ( v_max as f32 * 8.0 ) ) . ceil ( ) as u16 ;
252
+ for component in components {
253
+ component. size . width = ( size . width as f32 * component. horizontal_sampling_factor as f32 * component. dct_scale as f32 / ( h_max as f32 * 8.0 ) ) . ceil ( ) as u16 ;
254
+ component. size . height = ( size . height as f32 * component. vertical_sampling_factor as f32 * component. dct_scale as f32 / ( v_max as f32 * 8.0 ) ) . ceil ( ) as u16 ;
227
255
228
256
component. block_size . width = mcu_size. width * component. horizontal_sampling_factor as u16 ;
229
257
component. block_size . height = mcu_size. height * component. vertical_sampling_factor as u16 ;
230
258
}
231
259
232
- Ok ( FrameInfo {
233
- is_baseline : is_baseline,
234
- is_differential : is_differential,
235
- coding_process : coding_process,
236
- entropy_coding : entropy_coding,
237
- precision : precision,
238
- image_size : Dimensions { width : width, height : height} ,
239
- output_size : Dimensions { width : ( width as f32 * scale as f32 / 8.0 ) . ceil ( ) as u16 , height : ( height as f32 * scale as f32 / 8.0 ) . ceil ( ) as u16 } ,
240
- mcu_size : mcu_size,
241
- components : components,
242
- } )
260
+ mcu_size
243
261
}
244
262
245
263
// Section B.2.3
0 commit comments