@@ -260,6 +260,22 @@ fn reader_finished_already() -> ImageError {
260260 ImageError :: Parameter ( ParameterError :: from_kind ( ParameterErrorKind :: NoMoreData ) )
261261}
262262
263+ /// PNG images are big endian. For 16 bit per channel and larger types, the buffer may need
264+ /// to be reordered to native endianness per the contract of `read_image`. Assumes equal
265+ /// depth which is the only supported output from `png` with our options.
266+ fn big_endian_to_native_endian ( buf : & mut [ u8 ] , color : ColorType ) {
267+ let bytes_per_channel = color. bytes_per_pixel ( ) / color. channel_count ( ) ;
268+
269+ match bytes_per_channel {
270+ 1 => ( ) , // No reodering necessary for u8
271+ 2 => buf. as_chunks_mut :: < 2 > ( ) . 0 . iter_mut ( ) . for_each ( |c| {
272+ * c = u16:: from_be_bytes ( * c) . to_ne_bytes ( ) ;
273+ } ) ,
274+ // not emitted by png crate
275+ _ => unreachable ! ( ) ,
276+ }
277+ }
278+
263279impl < R : BufRead + Seek > ImageDecoder for PngDecoder < R > {
264280 fn prepare_image ( & mut self ) -> ImageResult < DecoderPreparedImage > {
265281 let reader = self . ensure_reader_and_header ( ) ?;
@@ -361,18 +377,7 @@ impl<R: BufRead + Seek> ImageDecoder for PngDecoder<R> {
361377 let original_color_type = Self :: color_type_info ( reader. info ( ) ) ;
362378 reader. next_frame ( buf) . map_err ( ImageError :: from_png) ?;
363379
364- // PNG images are big endian. For 16 bit per channel and larger types, the buffer may need
365- // to be reordered to native endianness per the contract of `read_image`. Assumes equal
366- // depth which is the only supported output from `png` with our options.
367- let bpc = layout. layout . color . bytes_per_pixel ( ) / layout. layout . color . channel_count ( ) ;
368-
369- match bpc {
370- 1 => ( ) , // No reodering necessary for u8
371- 2 => buf. as_chunks_mut :: < 2 > ( ) . 0 . iter_mut ( ) . for_each ( |c| {
372- * c = u16:: from_be_bytes ( * c) . to_ne_bytes ( ) ;
373- } ) ,
374- _ => unreachable ! ( ) ,
375- }
380+ big_endian_to_native_endian ( buf, layout. layout . color ) ;
376381
377382 Ok ( DecodedImageAttributes {
378383 original_color_type : Some ( original_color_type) ,
@@ -558,6 +563,8 @@ impl<R: BufRead + Seek> ApngDecoder<R> {
558563 // to also constrain the internal buffer allocations in the PNG crate
559564 reader. next_frame ( buffer) . map_err ( ImageError :: from_png) ?;
560565
566+ big_endian_to_native_endian ( buffer, color) ;
567+
561568 // Find out how to interpret the decoded frame.
562569 let info = reader. info ( ) ;
563570 let attributes = attributes_from_info ( info) ;
@@ -1142,4 +1149,27 @@ mod tests {
11421149 let gamma = PngDecoder :: new ( file) . gamma_value ( ) . unwrap ( ) ;
11431150 assert_eq ! ( gamma, None ) ;
11441151 }
1152+
1153+ #[ test]
1154+ fn apng_16_bits_per_channel ( ) {
1155+ let file = BufReader :: new ( std:: fs:: File :: open ( "tests/images/png/apng/rgba16.png" ) . unwrap ( ) ) ;
1156+ let decoder = PngDecoder :: new ( file) . apng ( ) . unwrap ( ) ;
1157+ let reader = crate :: ImageReader :: from_decoder ( Box :: new ( decoder) ) ;
1158+ let frames = reader. into_frames ( ) . collect_frames ( ) . unwrap ( ) ;
1159+ assert_eq ! ( frames. len( ) , 3 ) ;
1160+
1161+ let colors = [
1162+ * frames[ 0 ] . buffer ( ) . get_pixel ( 0 , 0 ) ,
1163+ * frames[ 1 ] . buffer ( ) . get_pixel ( 0 , 0 ) ,
1164+ * frames[ 2 ] . buffer ( ) . get_pixel ( 0 , 0 ) ,
1165+ ] ;
1166+ assert_eq ! (
1167+ colors,
1168+ [
1169+ Rgba ( [ 255 , 0 , 0 , 255 ] ) ,
1170+ Rgba ( [ 0 , 255 , 0 , 255 ] ) ,
1171+ Rgba ( [ 0 , 0 , 255 , 255 ] ) ,
1172+ ]
1173+ ) ;
1174+ }
11451175}
0 commit comments