@@ -261,6 +261,26 @@ fn rgba(raw: &[u8], width: usize, height: usize, stride: usize, format: usize) -
261261 Ok ( output)
262262}
263263
264+ /// The desktop app rejects larger capture PNGs (`linuxCaptureSession.ts`).
265+ const MAX_PNG_BYTES : usize = 32 * 1024 * 1024 ;
266+
267+ /// Level 6 dominated capture time, so encode at the much faster level 2 and fall back
268+ /// to level 6 only when level 2's slightly larger output would be rejected.
269+ fn encode_png ( rgba : & [ u8 ] , width : u32 , height : u32 , limit : usize ) -> Result < Vec < u8 > > {
270+ for level in [ 2 , 6 ] {
271+ let mut png = Vec :: new ( ) ;
272+ let mut encoder = png:: Encoder :: new ( & mut png, width, height) ;
273+ encoder. set_color ( png:: ColorType :: Rgba ) ;
274+ encoder. set_depth ( png:: BitDepth :: Eight ) ;
275+ encoder. set_deflate_compression ( png:: DeflateCompression :: Level ( level) ) ;
276+ encoder. write_header ( ) ?. write_image_data ( rgba) ?;
277+ if png. len ( ) <= limit {
278+ return Ok ( png) ;
279+ }
280+ }
281+ Err ( "Screenshot too large" . into ( ) )
282+ }
283+
264284fn capture ( connection : & Connection , directory : & Path ) -> Result < Option < Window > > {
265285 // Lack of scripting only loses identity/text; never guess which app was captured.
266286 let before = window ( connection, directory, None ) . ok ( ) . flatten ( ) ;
@@ -299,16 +319,12 @@ fn capture(connection: &Connection, directory: &Path) -> Result<Option<Window>>
299319 let mut raw = vec ! [ 0 ; bytes] ;
300320 reader. read_exact ( & mut raw) ?;
301321 let pixels = rgba ( & raw, width, height, stride, uint ( & metadata, "format" ) ?) ?;
302- let image = OpenOptions :: new ( )
322+ let png = encode_png ( & pixels, width as u32 , height as u32 , MAX_PNG_BYTES ) ?;
323+ OpenOptions :: new ( )
303324 . write ( true )
304325 . create_new ( true )
305- . open ( directory. join ( "capture.png" ) ) ?;
306- let mut encoder = png:: Encoder :: new ( image, width as u32 , height as u32 ) ;
307- encoder. set_color ( png:: ColorType :: Rgba ) ;
308- encoder. set_depth ( png:: BitDepth :: Eight ) ;
309- let mut image = encoder. write_header ( ) ?;
310- image. write_image_data ( & pixels) ?;
311- image. finish ( ) ?;
326+ . open ( directory. join ( "capture.png" ) ) ?
327+ . write_all ( & png) ?;
312328 let identity = before. filter ( |before| {
313329 window ( connection, directory, Some ( & before. id ) )
314330 . ok ( )
@@ -453,6 +469,28 @@ mod tests {
453469 }
454470 }
455471
472+ #[ test]
473+ fn falls_back_to_smaller_png_when_fast_encode_exceeds_limit ( ) {
474+ let ( width, height) = ( 256 , 256 ) ;
475+ let rgba: Vec < u8 > = ( 0 ..width * height * 4 )
476+ . map ( |i| ( ( ( i / 4 ) % 251 ) ^ ( i % 7 ) ) as u8 )
477+ . collect ( ) ;
478+ let decode = |png : & [ u8 ] | {
479+ let mut reader = png:: Decoder :: new ( std:: io:: Cursor :: new ( png) )
480+ . read_info ( )
481+ . unwrap ( ) ;
482+ let mut pixels = vec ! [ 0 ; reader. output_buffer_size( ) . unwrap( ) ] ;
483+ reader. next_frame ( & mut pixels) . unwrap ( ) ;
484+ pixels
485+ } ;
486+ let fast = encode_png ( & rgba, width, height, usize:: MAX ) . unwrap ( ) ;
487+ let fallback = encode_png ( & rgba, width, height, fast. len ( ) - 1 ) . unwrap ( ) ;
488+ assert ! ( fallback. len( ) < fast. len( ) ) ;
489+ assert_eq ! ( decode( & fast) , rgba) ;
490+ assert_eq ! ( decode( & fallback) , rgba) ;
491+ assert ! ( encode_png( & rgba, width, height, fallback. len( ) - 1 ) . is_err( ) ) ;
492+ }
493+
456494 #[ test]
457495 fn decodes_native_argb_and_row_padding ( ) {
458496 let raw = [ 0xff112233_u32 . to_ne_bytes ( ) , [ 0 ; 4 ] ] . concat ( ) ;
0 commit comments