@@ -115,6 +115,66 @@ pub fn to_color_image(img: &Image, grayscale: bool) -> ColorImage {
115115 ColorImage :: new ( [ w, h] , px)
116116}
117117
118+ /// The largest texture side the renderer (GPU or software fallback) takes; egui panics on
119+ /// a bigger texture.
120+ pub fn max_side ( ctx : & egui:: Context ) -> u32 {
121+ ctx. input ( |i| i. max_texture_side ) . clamp ( 1 , u32:: MAX as usize ) as u32
122+ }
123+
124+ /// `(w, h)` scaled down, keeping its shape, so neither side exceeds `max` (and none is 0).
125+ /// Render sizes go through this so a huge display, DPI or slide shape doesn't render a
126+ /// picture the GPU can't hold.
127+ pub fn fit_size ( ( w, h) : ( u32 , u32 ) , max : u32 ) -> ( u32 , u32 ) {
128+ let ( w, h, max) = ( w. max ( 1 ) , h. max ( 1 ) , max. max ( 1 ) ) ;
129+ let big = w. max ( h) ;
130+ if big <= max {
131+ return ( w, h) ;
132+ }
133+ let k = max as f64 / big as f64 ;
134+ ( ( ( w as f64 * k) . floor ( ) as u32 ) . clamp ( 1 , max) , ( ( h as f64 * k) . floor ( ) as u32 ) . clamp ( 1 , max) )
135+ }
136+
137+ /// `img` shrunk by whole-pixel box averaging until both sides fit `max`.
138+ pub fn shrink_to ( img : ColorImage , max : u32 ) -> ColorImage {
139+ let [ w, h] = img. size ;
140+ let max = max. max ( 1 ) as usize ;
141+ if w <= max && h <= max {
142+ return img;
143+ }
144+ let f = w. max ( h) . div_ceil ( max) ;
145+ let ( nw, nh) = ( w. div_ceil ( f) . max ( 1 ) , h. div_ceil ( f) . max ( 1 ) ) ;
146+ let mut px = Vec :: with_capacity ( nw * nh) ;
147+ for y in 0 ..nh {
148+ for x in 0 ..nw {
149+ let mut acc = [ 0u32 ; 4 ] ;
150+ let mut n = 0u32 ;
151+ for sy in y * f..( ( y + 1 ) * f) . min ( h) {
152+ for sx in x * f..( ( x + 1 ) * f) . min ( w) {
153+ if let Some ( c) = img. pixels . get ( sy * w + sx) {
154+ for ( a, v) in acc. iter_mut ( ) . zip ( c. to_array ( ) ) {
155+ * a += v as u32 ;
156+ }
157+ n += 1 ;
158+ }
159+ }
160+ }
161+ let n = n. max ( 1 ) ;
162+ px. push ( egui:: Color32 :: from_rgba_premultiplied ( ( acc[ 0 ] / n) as u8 , ( acc[ 1 ] / n) as u8 , ( acc[ 2 ] / n) as u8 , ( acc[ 3 ] / n) as u8 ) ) ;
163+ }
164+ }
165+ ColorImage :: new ( [ nw, nh] , px)
166+ }
167+
168+ /// Upload a texture, shrunk to the renderer's limit if need be.
169+ pub fn upload ( ctx : & egui:: Context , name : impl Into < String > , img : ColorImage ) -> TextureHandle {
170+ ctx. load_texture ( name, shrink_to ( img, max_side ( ctx) ) , TextureOptions :: LINEAR )
171+ }
172+
173+ /// Replace a texture's picture, shrunk to the renderer's limit if need be.
174+ pub fn replace ( ctx : & egui:: Context , tex : & mut TextureHandle , img : ColorImage ) {
175+ tex. set ( shrink_to ( img, max_side ( ctx) ) , TextureOptions :: LINEAR ) ;
176+ }
177+
118178fn master_of ( p : & Presentation , slide : & Slide ) -> Option < Arc < Master > > {
119179 p. masters . iter ( ) . find ( |m| m. layout ( slide. layout ) . is_some ( ) ) . cloned ( )
120180}
@@ -165,7 +225,7 @@ impl Textures {
165225 _ => estimate,
166226 } ;
167227 // The GPU's (or software fallback's) texture limit: a larger canvas texture panics in egui.
168- let max_side = ctx . input ( |i| i . max_texture_side ) . max ( 1 ) as u32 ;
228+ let max_side = max_side ( ctx ) ;
169229 let want = fit_texture ( deckcraft_render:: pasteboard_region ( size, scale, reach, visible) , visible, max_side) ;
170230 let key = Key {
171231 slide : Arc :: as_ptr ( & slide) as usize ,
@@ -204,10 +264,10 @@ impl Textures {
204264 let ci = to_color_image ( & img, grayscale) ;
205265 let tex = match self . canvas . take ( ) {
206266 Some ( mut e) => {
207- e. tex . set ( ci , TextureOptions :: LINEAR ) ;
267+ replace ( ctx , & mut e. tex , ci ) ;
208268 e. tex
209269 }
210- None => ctx . load_texture ( "slide-canvas" , ci, TextureOptions :: LINEAR ) ,
270+ None => upload ( ctx , "slide-canvas" , ci) ,
211271 } ;
212272 self . canvas = Some ( Entry { key, tex : tex. clone ( ) , region, _slide : slide, _master : master, stamp : 0 } ) ;
213273 Some ( ( tex, region) )
@@ -253,9 +313,10 @@ impl Textures {
253313 return self . thumbs . get ( & key. slide ) . map ( |e| e. tex . clone ( ) ) ;
254314 }
255315 * budget -= 1 ;
256- let scale = width_px as f64 / p. slide_size . width . max ( 1.0 ) ;
257- let img = deckcraft_render:: render_slide ( p, index, & RenderOpts { scale, size : Some ( ( width_px, h) ) , threads : 0 , ..Default :: default ( ) } ) ;
258- let tex = ctx. load_texture ( format ! ( "thumb-{index}" ) , to_color_image ( & img, false ) , TextureOptions :: LINEAR ) ;
316+ let ( tw, th) = fit_size ( ( width_px, h) , max_side ( ctx) ) ;
317+ let scale = tw as f64 / p. slide_size . width . max ( 1.0 ) ;
318+ let img = deckcraft_render:: render_slide ( p, index, & RenderOpts { scale, size : Some ( ( tw, th) ) , threads : 0 , ..Default :: default ( ) } ) ;
319+ let tex = upload ( ctx, format ! ( "thumb-{index}" ) , to_color_image ( & img, false ) ) ;
259320 // Drop the old entry for this slide address; entries for slides that no longer exist age out.
260321 let region = key. want ;
261322 self . thumbs . insert ( key. slide , Entry { key, tex : tex. clone ( ) , region, _slide : slide, _master : master, stamp : clock } ) ;
@@ -298,4 +359,26 @@ mod fit_tests {
298359 assert_eq ! ( ( f. x, f. w) , ( -100 + 5000 - 2048 , 2048 ) ) ;
299360 assert_eq ! ( fit_texture( r, r, 8192 ) , r) ;
300361 }
362+
363+ #[ test]
364+ fn render_sizes_and_pictures_shrink_to_the_limit_keeping_their_shape ( ) {
365+ assert_eq ! ( fit_size( ( 195 , 10920 ) , 2048 ) , ( 36 , 2048 ) ) ;
366+ assert_eq ! ( fit_size( ( 3000 , 1500 ) , 2048 ) , ( 2048 , 1024 ) ) ;
367+ assert_eq ! ( fit_size( ( 800 , 600 ) , 2048 ) , ( 800 , 600 ) ) ;
368+ assert_eq ! ( fit_size( ( 0 , 0 ) , 0 ) , ( 1 , 1 ) ) ;
369+ // A 5000 × 3 picture: half red, half blue, averaged down to 1667 × 1.
370+ let img = ColorImage :: new (
371+ [ 5000 , 3 ] ,
372+ ( 0 ..3 )
373+ . flat_map ( |y| ( 0 ..5000 ) . map ( move |x| ( x, y) ) )
374+ . map ( |( x, _) | if x < 2500 { egui:: Color32 :: RED } else { egui:: Color32 :: BLUE } )
375+ . collect ( ) ,
376+ ) ;
377+ let small = shrink_to ( img, 2048 ) ;
378+ assert_eq ! ( small. size, [ 1667 , 1 ] ) ;
379+ assert_eq ! ( small. pixels[ 0 ] , egui:: Color32 :: RED ) ;
380+ assert_eq ! ( small. pixels[ 1666 ] , egui:: Color32 :: BLUE ) ;
381+ let ok = ColorImage :: filled ( [ 10 , 10 ] , egui:: Color32 :: WHITE ) ;
382+ assert_eq ! ( shrink_to( ok. clone( ) , 2048 ) . size, ok. size) ;
383+ }
301384}
0 commit comments