@@ -19,17 +19,15 @@ impl Clone for WebpEncoderFrame {
1919pub struct WebpEncoder {
2020 width : u32 ,
2121 height : u32 ,
22- options : EncoderOptions ,
2322 frame_rate : u16 ,
2423 frames : Vec < WebpEncoderFrame > ,
2524}
2625
2726impl WebpEncoder {
28- pub fn new ( width : u32 , height : u32 , options : Option < EncoderOptions > ) -> Self {
27+ pub fn new ( width : u32 , height : u32 ) -> Self {
2928 WebpEncoder {
3029 width,
3130 height,
32- options : options. unwrap_or_else ( EncoderOptions :: default) ,
3331 frame_rate : 30 ,
3432 frames : Vec :: new ( ) ,
3533 }
@@ -46,10 +44,10 @@ impl WebpEncoder {
4644 } ) ;
4745 }
4846
49- pub fn get_buffer ( & self ) -> Result < Vec < u8 > > {
50- let mut encoder =
51- Encoder :: new_with_options ( ( self . width , self . height ) , self . options . to_owned ( ) )
52- . map_err ( EncoderError :: EncoderError ) ?;
47+ pub fn get_buffer ( & self , options : Option < EncoderOptions > ) -> Result < Vec < u8 > > {
48+ let options = options . unwrap_or_else ( || EncoderOptions :: default ( ) ) ;
49+ let mut encoder = Encoder :: new_with_options ( ( self . width , self . height ) , options)
50+ . map_err ( EncoderError :: EncoderError ) ?;
5351 let mut timestamp: i32 = 0 ;
5452 for frame in & self . frames {
5553 encoder
@@ -63,8 +61,8 @@ impl WebpEncoder {
6361 . to_vec ( ) )
6462 }
6563
66- pub fn write_to_file ( & self , path : String ) -> Result < Vec < u8 > > {
67- let buffer = self . get_buffer ( ) ?;
64+ pub fn write_to_file ( & self , path : String , options : Option < EncoderOptions > ) -> Result < Vec < u8 > > {
65+ let buffer = self . get_buffer ( options ) ?;
6866 std:: fs:: write (
6967 match path. ends_with ( ".webp" ) {
7068 true => path,
@@ -85,15 +83,10 @@ impl WebpEncoder {
8583 self . height = height;
8684 }
8785
88- pub fn set_options ( & mut self , options : EncoderOptions ) {
89- self . options = options;
90- }
91-
9286 pub fn clone ( & self ) -> Self {
9387 WebpEncoder {
9488 width : self . width ,
9589 height : self . height ,
96- options : self . options . clone ( ) ,
9790 frame_rate : self . frame_rate ,
9891 frames : self . frames . clone ( ) ,
9992 }
@@ -110,6 +103,7 @@ pub struct JsWebpEncoderOptions {
110103
111104pub struct AsyncGetBuffer {
112105 encoder : WebpEncoder ,
106+ options : Option < EncoderOptions > ,
113107}
114108
115109#[ napi]
@@ -118,7 +112,7 @@ impl Task for AsyncGetBuffer {
118112 type JsValue = Buffer ;
119113
120114 fn compute ( & mut self ) -> Result < Self :: Output > {
121- self . encoder . get_buffer ( )
115+ self . encoder . get_buffer ( self . options . clone ( ) )
122116 }
123117
124118 fn resolve ( & mut self , _: Env , output : Self :: Output ) -> Result < Self :: JsValue > {
@@ -129,6 +123,7 @@ impl Task for AsyncGetBuffer {
129123pub struct AsyncWriteToFile {
130124 encoder : WebpEncoder ,
131125 path : String ,
126+ options : Option < EncoderOptions > ,
132127}
133128
134129#[ napi]
@@ -137,7 +132,8 @@ impl Task for AsyncWriteToFile {
137132 type JsValue = Buffer ;
138133
139134 fn compute ( & mut self ) -> Result < Self :: Output > {
140- self . encoder . write_to_file ( self . path . clone ( ) )
135+ self . encoder
136+ . write_to_file ( self . path . clone ( ) , self . options . clone ( ) )
141137 }
142138
143139 fn resolve ( & mut self , _: Env , output : Self :: Output ) -> Result < Self :: JsValue > {
@@ -153,28 +149,9 @@ pub struct JsWebpEncoder {
153149#[ napi]
154150impl JsWebpEncoder {
155151 #[ napi( constructor) ]
156- pub fn new (
157- width : u32 ,
158- height : u32 ,
159- #[ napi( ts_arg_type = "JsWebpEncoderOptions" ) ] options : Option < JsWebpEncoderOptions > ,
160- ) -> Self {
161- let options = options. map ( |options| EncoderOptions {
162- anim_params : AnimParams {
163- loop_count : options. loop_count . unwrap_or ( 0 ) ,
164- } ,
165- encoding_config : Some ( EncodingConfig {
166- encoding_type : if options. lossless . unwrap_or ( true ) {
167- webp_animation:: EncodingType :: Lossless
168- } else {
169- webp_animation:: EncodingType :: Lossy ( LossyEncodingConfig :: default ( ) )
170- } ,
171- quality : options. quality . unwrap_or ( 1 ) as f32 ,
172- method : options. method . unwrap_or ( 4 ) as usize ,
173- } ) ,
174- ..Default :: default ( )
175- } ) ;
152+ pub fn new ( width : u32 , height : u32 ) -> Self {
176153 JsWebpEncoder {
177- encoder : WebpEncoder :: new ( width, height, options ) ,
154+ encoder : WebpEncoder :: new ( width, height) ,
178155 }
179156 }
180157
@@ -193,28 +170,50 @@ impl JsWebpEncoder {
193170 }
194171
195172 #[ napi]
196- pub fn get_buffer ( & self ) -> AsyncTask < AsyncGetBuffer > {
173+ pub fn get_buffer (
174+ & self ,
175+ #[ napi( ts_arg_type = "JsWebpEncoderOptions" ) ] options : Option < JsWebpEncoderOptions > ,
176+ ) -> AsyncTask < AsyncGetBuffer > {
197177 AsyncTask :: new ( AsyncGetBuffer {
198178 encoder : self . encoder . clone ( ) ,
179+ options : options. map ( map_js_webp_encoder_options) ,
199180 } )
200181 }
201182
202183 #[ napi]
203- pub fn get_buffer_sync ( & self ) -> Result < Buffer > {
204- Ok ( self . encoder . get_buffer ( ) ?. into ( ) )
184+ pub fn get_buffer_sync (
185+ & self ,
186+ #[ napi( ts_arg_type = "JsWebpEncoderOptions" ) ] options : Option < JsWebpEncoderOptions > ,
187+ ) -> Result < Buffer > {
188+ Ok ( self
189+ . encoder
190+ . get_buffer ( options. map ( map_js_webp_encoder_options) ) ?
191+ . into ( ) )
205192 }
206193
207194 #[ napi]
208- pub fn write_to_file ( & self , path : String ) -> AsyncTask < AsyncWriteToFile > {
195+ pub fn write_to_file (
196+ & self ,
197+ path : String ,
198+ #[ napi( ts_arg_type = "JsWebpEncoderOptions" ) ] options : Option < JsWebpEncoderOptions > ,
199+ ) -> AsyncTask < AsyncWriteToFile > {
209200 AsyncTask :: new ( AsyncWriteToFile {
210201 encoder : self . encoder . clone ( ) ,
211202 path,
203+ options : options. map ( map_js_webp_encoder_options) ,
212204 } )
213205 }
214206
215207 #[ napi]
216- pub fn write_to_file_sync ( & self , path : String ) -> Result < Buffer > {
217- Ok ( self . encoder . write_to_file ( path) ?. into ( ) )
208+ pub fn write_to_file_sync (
209+ & self ,
210+ path : String ,
211+ #[ napi( ts_arg_type = "JsWebpEncoderOptions" ) ] options : Option < JsWebpEncoderOptions > ,
212+ ) -> Result < Buffer > {
213+ Ok ( self
214+ . encoder
215+ . write_to_file ( path, options. map ( map_js_webp_encoder_options) ) ?
216+ . into ( ) )
218217 }
219218
220219 #[ napi]
@@ -226,24 +225,23 @@ impl JsWebpEncoder {
226225 pub fn set_dimensions ( & mut self , width : u32 , height : u32 ) {
227226 self . encoder . set_dimensions ( width, height) ;
228227 }
228+ }
229229
230- #[ napi]
231- pub fn set_options ( & mut self , options : JsWebpEncoderOptions ) {
232- self . encoder . set_options ( EncoderOptions {
233- anim_params : AnimParams {
234- loop_count : options. loop_count . unwrap_or ( 0 ) ,
230+ fn map_js_webp_encoder_options ( options : JsWebpEncoderOptions ) -> EncoderOptions {
231+ EncoderOptions {
232+ anim_params : AnimParams {
233+ loop_count : options. loop_count . unwrap_or ( 0 ) ,
234+ } ,
235+ encoding_config : Some ( EncodingConfig {
236+ encoding_type : if options. lossless . unwrap_or ( true ) {
237+ webp_animation:: EncodingType :: Lossless
238+ } else {
239+ webp_animation:: EncodingType :: Lossy ( LossyEncodingConfig :: default ( ) )
235240 } ,
236- encoding_config : Some ( EncodingConfig {
237- encoding_type : if options. lossless . unwrap_or ( true ) {
238- webp_animation:: EncodingType :: Lossless
239- } else {
240- webp_animation:: EncodingType :: Lossy ( LossyEncodingConfig :: default ( ) )
241- } ,
242- quality : options. quality . unwrap_or ( 1 ) as f32 ,
243- method : options. method . unwrap_or ( 4 ) as usize ,
244- } ) ,
245- ..Default :: default ( )
246- } ) ;
241+ quality : options. quality . unwrap_or ( 1 ) as f32 ,
242+ method : options. method . unwrap_or ( 4 ) as usize ,
243+ } ) ,
244+ ..Default :: default ( )
247245 }
248246}
249247
0 commit comments