@@ -312,30 +312,53 @@ pub fn to_png_compatible_channels(channels: Channels) -> (Channels, png::ColorTy
312312 }
313313}
314314
315- pub fn read_png_u8 ( png_path : & Path ) -> Result < Image < u8 > , Box < dyn std:: error:: Error > > {
315+ pub fn read_png_u16 ( png_path : & Path ) -> Result < Image < u16 > , Box < dyn std:: error:: Error > > {
316316 let png_decoder = png:: Decoder :: new ( File :: open ( png_path) ?) ;
317317 let mut png_reader = png_decoder. read_info ( ) ?;
318318 let ( color, bits) = png_reader. output_color_type ( ) ;
319319
320- if bits != png:: BitDepth :: Eight {
321- return Err ( "Output PNG is not 8-bit, which shouldn't happen." . into ( ) ) ;
322- }
323320 let channels = match color {
324321 png:: ColorType :: Grayscale => Channels :: Grayscale ,
325322 png:: ColorType :: Rgb => Channels :: Rgb ,
326323 png:: ColorType :: Rgba => Channels :: Rgba ,
327324 _ => return Err ( "Unsupported PNG color type" . into ( ) ) ,
328325 } ;
329326
330- let mut png_image_data = vec ! [ 0 ; png_reader. output_buffer_size( ) ] ;
331- png_reader. next_frame ( & mut png_image_data) ?;
332- png_reader. finish ( ) ?;
327+ match bits {
328+ png:: BitDepth :: Sixteen => {
329+ let mut png_image_data: Vec < u16 > = vec ! [ 0 ; png_reader. output_buffer_size( ) / 2 ] ;
330+ png_reader. next_frame ( cast_slice_mut ( & mut png_image_data) ) ?;
331+ png_reader. finish ( ) ?;
332+
333+ png_image_data. iter_mut ( ) . for_each ( |v| * v = v. to_be ( ) ) ;
333334
334- Ok ( Image :: new (
335- png_image_data,
336- channels,
337- Size :: new ( png_reader. info ( ) . width , png_reader. info ( ) . height ) ,
338- ) )
335+ Ok ( Image :: new (
336+ png_image_data,
337+ channels,
338+ Size :: new ( png_reader. info ( ) . width , png_reader. info ( ) . height ) ,
339+ ) )
340+ }
341+ png:: BitDepth :: Eight => {
342+ let mut png_image_data = vec ! [ 0 ; png_reader. output_buffer_size( ) ] ;
343+ png_reader. next_frame ( & mut png_image_data) ?;
344+ png_reader. finish ( ) ?;
345+
346+ let image_u8 = Image :: new (
347+ png_image_data,
348+ channels,
349+ Size :: new ( png_reader. info ( ) . width , png_reader. info ( ) . height ) ,
350+ ) ;
351+
352+ Ok ( image_u8. to_u16 ( ) )
353+ }
354+ _ => Err ( "Output PNG is not 8/16-bit, which shouldn't happen." . into ( ) ) ,
355+ }
356+ }
357+ pub fn read_png_u8 ( png_path : & Path ) -> Result < Image < u8 > , Box < dyn std:: error:: Error > > {
358+ Ok ( read_png_u16 ( png_path) ?. to_u8 ( ) )
359+ }
360+ pub fn read_png_f32 ( png_path : & Path ) -> Result < Image < f32 > , Box < dyn std:: error:: Error > > {
361+ Ok ( read_png_u16 ( png_path) ?. to_f32 ( ) )
339362}
340363
341364pub fn write_simple_dds_header (
0 commit comments