11use crate :: error:: NokhwaError ;
22use crate :: frame_buffer:: FrameBuffer ;
33use crate :: image:: { DecodedImage , NonFloatScalarWidth } ;
4- use crate :: types:: { Resolution } ;
4+ use crate :: pixel_destination:: PixelDestination ;
5+ use crate :: types:: Resolution ;
6+ use bytemuck:: try_cast_slice_mut;
57pub use image:: { ImageBuffer , Pixel , Primitive } ;
68use std:: fmt:: Debug ;
7- use bytemuck:: try_cast_slice_mut;
8- use crate :: pixel_destination:: PixelDestination ;
99
1010pub trait Decoder {
1111 type Config : Clone + Debug + ConfigHasResolution ;
@@ -23,14 +23,25 @@ pub trait Decoder {
2323 destination_format : PixelDestination ,
2424 ) -> Result < Self :: OutputMeta , NokhwaError > ;
2525
26- fn decode_to_pixel_buffer < P : Pixel > ( & mut self , to_decode : FrameBuffer , mut buffer : impl AsMut < [ P :: Subpixel ] > ) -> Result < Self :: OutputMeta , NokhwaError >
26+ fn decode_to_pixel_buffer < P : Pixel > (
27+ & mut self ,
28+ to_decode : FrameBuffer ,
29+ mut buffer : impl AsMut < [ P :: Subpixel ] > ,
30+ ) -> Result < Self :: OutputMeta , NokhwaError >
2731 where
28- <P as Pixel >:: Subpixel : NonFloatScalarWidth
32+ <P as Pixel >:: Subpixel : NonFloatScalarWidth ,
2933 {
30- let Some ( destination) = PixelDestination :: get_by_pixel :: < P > ( ) else { return Err ( NokhwaError :: DecoderUnknownDestinationPixelFormat ( P :: COLOR_MODEL , P :: Subpixel :: WIDTH_BYTES ) ) } ;
34+ let Some ( destination) = PixelDestination :: get_by_pixel :: < P > ( ) else {
35+ return Err ( NokhwaError :: DecoderUnknownDestinationPixelFormat (
36+ P :: COLOR_MODEL ,
37+ P :: Subpixel :: WIDTH_BYTES ,
38+ ) ) ;
39+ } ;
3140
3241 if !Self :: supports_destination ( destination) {
33- return Err ( NokhwaError :: DecoderUnsupportedDestinationPixelFormat ( destination) )
42+ return Err ( NokhwaError :: DecoderUnsupportedDestinationPixelFormat (
43+ destination,
44+ ) ) ;
3445 }
3546
3647 let buffer = buffer. as_mut ( ) ;
@@ -52,28 +63,35 @@ pub trait Decoder {
5263 let mut out_buffer: Vec < P :: Subpixel > = vec ! [ P :: Subpixel :: DEFAULT_MIN_VALUE ; min_size_alloc] ;
5364 let meta = self . decode_to_pixel_buffer :: < P > ( to_decode, & mut out_buffer) ?;
5465 Ok ( DecodedImage :: new (
55- ImageBuffer :: from_vec (
56- resolution. width ( ) ,
57- resolution. height ( ) ,
58- out_buffer,
59- )
60- . ok_or ( NokhwaError :: Decoder (
61- "failed to convert into an image buffer" . to_string ( ) ,
62- ) ) ?,
66+ ImageBuffer :: from_vec ( resolution. width ( ) , resolution. height ( ) , out_buffer) . ok_or (
67+ NokhwaError :: Decoder ( "failed to convert into an image buffer" . to_string ( ) ) ,
68+ ) ?,
6369 meta,
6470 ) )
6571 }
66-
67- fn output_decoder_min_size_pixel < P > ( & self , resolution : Resolution ) -> Result < usize , NokhwaError > where
68- P : Pixel ,
69- <P as Pixel >:: Subpixel : NonFloatScalarWidth {
70- PixelDestination :: get_by_pixel :: < P > ( ) . map ( |dest| self . output_decoder_min_size ( resolution, dest) ) . ok_or ( NokhwaError :: DecoderUnknownDestinationPixelFormat ( P :: COLOR_MODEL , P :: Subpixel :: WIDTH_BYTES ) ) ?
7172
73+ fn output_decoder_min_size_pixel < P > ( & self , resolution : Resolution ) -> Result < usize , NokhwaError >
74+ where
75+ P : Pixel ,
76+ <P as Pixel >:: Subpixel : NonFloatScalarWidth ,
77+ {
78+ PixelDestination :: get_by_pixel :: < P > ( )
79+ . map ( |dest| self . output_decoder_min_size ( resolution, dest) )
80+ . ok_or ( NokhwaError :: DecoderUnknownDestinationPixelFormat (
81+ P :: COLOR_MODEL ,
82+ P :: Subpixel :: WIDTH_BYTES ,
83+ ) ) ?
7284 }
73-
74- fn output_decoder_min_size ( & self , resolution : Resolution , destination_format : PixelDestination ) -> Result < usize , NokhwaError > {
85+
86+ fn output_decoder_min_size (
87+ & self ,
88+ resolution : Resolution ,
89+ destination_format : PixelDestination ,
90+ ) -> Result < usize , NokhwaError > {
7591 if !Self :: supports_destination ( destination_format) {
76- return Err ( NokhwaError :: DecoderUnsupportedDestinationPixelFormat ( destination_format) )
92+ return Err ( NokhwaError :: DecoderUnsupportedDestinationPixelFormat (
93+ destination_format,
94+ ) ) ;
7795 }
7896
7997 let px_size = match destination_format {
@@ -83,7 +101,7 @@ pub trait Decoder {
83101 PixelDestination :: Rgba16 | PixelDestination :: Bgra16 => 4_u32 * 2_u32 ,
84102 PixelDestination :: Luma8 => 1_u32 ,
85103 PixelDestination :: LumaA8 | PixelDestination :: Luma16 => 2_u32 ,
86- } ;
104+ } ;
87105 let reso = resolution. width ( ) * resolution. height ( ) ;
88106 Ok ( ( reso as usize ) * ( px_size as usize ) )
89107 }
0 commit comments