@@ -124,23 +124,6 @@ fn normalize_src_rect(entity: Entity, rect: Option<[f32; 4]>) -> PyResult<[f32;
124124 }
125125}
126126
127- /// Resolve a composite source (`Image`, `Webcam`, or `Graphics`) to a
128- /// texture-bearing entity, plus the graphics entity that must be flushed first
129- /// when the source is a graphics (so its latest content is what gets sampled).
130- /// A `Graphics` source uses its render-target surface entity, which carries an
131- /// `Image` component for offscreen graphics.
132- fn resolve_composite_source ( src : & Bound < ' _ , PyAny > ) -> PyResult < ( Entity , Option < Entity > ) > {
133- if let Ok ( img) = src. extract :: < ImageRef > ( ) {
134- return Ok ( ( img. entity , None ) ) ;
135- }
136- if let Ok ( g) = src. extract :: < PyRef < Graphics > > ( ) {
137- return Ok ( ( g. surface . entity , Some ( g. entity ) ) ) ;
138- }
139- Err ( PyValueError :: new_err (
140- "composite source must be an Image, Webcam, or Graphics" ,
141- ) )
142- }
143-
144127/// Run the built-in composite filter: blend the `src` texture into the `dst`
145128/// graphics target using `mode`, with normalized src/dst rects. This is the
146129/// shader-based composite pass shared by `copy`/`blend`/`mask`.
@@ -446,6 +429,36 @@ impl Light {
446429 let v = extract_vec3 ( args) ?;
447430 transform_look_at ( self . entity , v) . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "{e}" ) ) )
448431 }
432+
433+ #[ pyo3( signature = ( * args) ) ]
434+ pub fn translate ( & self , args : & Bound < ' _ , PyTuple > ) -> PyResult < ( ) > {
435+ let v = extract_vec3 ( args) ?;
436+ transform_translate ( self . entity , v) . map_err ( rt_err)
437+ }
438+
439+ pub fn rotate_x ( & self , angle : f32 ) -> PyResult < ( ) > {
440+ transform_rotate_x ( self . entity , angle) . map_err ( rt_err)
441+ }
442+
443+ pub fn rotate_y ( & self , angle : f32 ) -> PyResult < ( ) > {
444+ transform_rotate_y ( self . entity , angle) . map_err ( rt_err)
445+ }
446+
447+ pub fn rotate_z ( & self , angle : f32 ) -> PyResult < ( ) > {
448+ transform_rotate_z ( self . entity , angle) . map_err ( rt_err)
449+ }
450+
451+ /// Rotate by `angle` radians around `axis`.
452+ #[ pyo3( signature = ( angle, * axis) ) ]
453+ pub fn rotate_axis ( & self , angle : f32 , axis : & Bound < ' _ , PyTuple > ) -> PyResult < ( ) > {
454+ let axis = extract_vec3 ( axis) ?;
455+ transform_rotate_axis ( self . entity , angle, axis) . map_err ( rt_err)
456+ }
457+
458+ /// Back to the origin with no rotation.
459+ pub fn reset ( & self ) -> PyResult < ( ) > {
460+ transform_reset ( self . entity ) . map_err ( rt_err)
461+ }
449462}
450463
451464impl Light {
@@ -565,16 +578,33 @@ impl Image {
565578 }
566579}
567580
581+ /// Anything that can be sampled as an image: an `Image`, a `Webcam`, or an
582+ /// offscreen `Graphics`.
568583pub ( crate ) struct ImageRef {
569- pub entity : Entity ,
584+ entity : Entity ,
585+ graphics : Option < Entity > ,
586+ }
587+
588+ impl ImageRef {
589+ /// The entity carrying the image. A `Graphics` source with pending draws is
590+ /// flushed first, so what it has drawn so far is what gets sampled.
591+ pub ( crate ) fn texture ( & self ) -> PyResult < Entity > {
592+ if let Some ( graphics) = self . graphics {
593+ graphics_flush_pending ( graphics) . map_err ( rt_err) ?;
594+ }
595+ Ok ( self . entity )
596+ }
570597}
571598
572599impl < ' a , ' py > FromPyObject < ' a , ' py > for ImageRef {
573600 type Error = PyErr ;
574601
575602 fn extract ( ob : pyo3:: Borrowed < ' a , ' py , PyAny > ) -> PyResult < Self > {
576603 if let Ok ( img) = ob. extract :: < PyRef < Image > > ( ) {
577- return Ok ( ImageRef { entity : img. entity } ) ;
604+ return Ok ( ImageRef {
605+ entity : img. entity ,
606+ graphics : None ,
607+ } ) ;
578608 }
579609 // an offscreen Graphics renders into an image; a window has no texture to sample
580610 if let Ok ( g) = ob. extract :: < PyRef < Graphics > > ( ) {
@@ -585,12 +615,14 @@ impl<'a, 'py> FromPyObject<'a, 'py> for ImageRef {
585615 }
586616 return Ok ( ImageRef {
587617 entity : g. surface . entity ,
618+ graphics : Some ( g. entity ) ,
588619 } ) ;
589620 }
590621 #[ cfg( feature = "webcam" ) ]
591622 if let Ok ( cam) = ob. extract :: < PyRef < crate :: webcam:: Webcam > > ( ) {
592623 return Ok ( ImageRef {
593624 entity : cam. image_entity ( ) ?,
625+ graphics : None ,
594626 } ) ;
595627 }
596628 Err ( pyo3:: exceptions:: PyTypeError :: new_err (
@@ -629,6 +661,26 @@ impl Image {
629661 . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "{e}" ) ) )
630662 }
631663
664+ /// Resize to `width` x `height` pixels. A 0 for either keeps the aspect ratio.
665+ pub fn resize ( & self , width : u32 , height : u32 ) -> PyResult < ( ) > {
666+ let ( w, h) = image_size ( self . entity ) . map_err ( rt_err) ?;
667+ let ( width, height) = match ( width, height) {
668+ ( 0 , 0 ) => return Err ( PyValueError :: new_err ( "resize() needs a non-zero size" ) ) ,
669+ ( 0 , height) => ( ( w as f32 * height as f32 / h as f32 ) . round ( ) as u32 , height) ,
670+ ( width, 0 ) => ( width, ( h as f32 * width as f32 / w as f32 ) . round ( ) as u32 ) ,
671+ size => size,
672+ } ;
673+ image_resize (
674+ self . entity ,
675+ Extent3d {
676+ width : width. max ( 1 ) ,
677+ height : height. max ( 1 ) ,
678+ depth_or_array_layers : 1 ,
679+ } ,
680+ )
681+ . map_err ( rt_err)
682+ }
683+
632684 /// Image width in pixels.
633685 #[ getter]
634686 fn width ( & self ) -> PyResult < u32 > {
@@ -1258,8 +1310,19 @@ impl Graphics {
12581310 . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "{e}" ) ) )
12591311 }
12601312
1313+ /// A color (same arguments as `fill()`), or an `Image`, `Graphics` or
1314+ /// `Webcam` stretched over the canvas.
12611315 #[ pyo3( signature = ( * args) ) ]
12621316 pub fn background ( & self , args : & Bound < ' _ , PyTuple > ) -> PyResult < ( ) > {
1317+ if args. len ( ) == 1
1318+ && let Ok ( image) = args. get_item ( 0 ) ?. extract :: < ImageRef > ( )
1319+ {
1320+ return graphics_record_command (
1321+ self . entity ,
1322+ DrawCommand :: BackgroundImage ( image. texture ( ) ?) ,
1323+ )
1324+ . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "{e}" ) ) ) ;
1325+ }
12631326 let color = extract_color_with_mode (
12641327 args,
12651328 & graphics_get_color_mode ( self . entity )
@@ -1269,11 +1332,6 @@ impl Graphics {
12691332 . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "{e}" ) ) )
12701333 }
12711334
1272- pub fn background_image ( & self , image : & Image ) -> PyResult < ( ) > {
1273- graphics_record_command ( self . entity , DrawCommand :: BackgroundImage ( image. entity ) )
1274- . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "{e}" ) ) )
1275- }
1276-
12771335 pub fn clear ( & self ) -> PyResult < ( ) > {
12781336 graphics_record_command (
12791337 self . entity ,
@@ -1951,7 +2009,7 @@ impl Graphics {
19512009 graphics_record_command (
19522010 self . entity ,
19532011 DrawCommand :: Image {
1954- entity : source. entity ,
2012+ entity : source. texture ( ) ? ,
19552013 dx,
19562014 dy,
19572015 d_width,
@@ -2340,7 +2398,7 @@ impl Graphics {
23402398 }
23412399
23422400 pub fn texture ( & self , source : ImageRef ) -> PyResult < ( ) > {
2343- graphics_record_command ( self . entity , DrawCommand :: Texture ( source. entity ) )
2401+ graphics_record_command ( self . entity , DrawCommand :: Texture ( source. texture ( ) ? ) )
23442402 . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "{e}" ) ) )
23452403 }
23462404
@@ -2473,10 +2531,7 @@ impl Graphics {
24732531 MULTIPLY, SCREEN, REPLACE)",
24742532 )
24752533 } ) ?;
2476- let ( src_entity, flush) = resolve_composite_source ( src) ?;
2477- if let Some ( g) = flush {
2478- graphics_flush ( g) . map_err ( rt_err) ?;
2479- }
2534+ let src_entity = src. extract :: < ImageRef > ( ) ?. texture ( ) ?;
24802535 let sr = normalize_src_rect ( src_entity, src_rect) ?;
24812536 let dr = normalize_rect ( dst_rect, self . width as f32 , self . height as f32 ) ;
24822537 composite_apply ( self . entity , src_entity, code, sr, dr, opacity)
@@ -2492,10 +2547,7 @@ impl Graphics {
24922547 src_rect : Option < [ f32 ; 4 ] > ,
24932548 dst_rect : Option < [ f32 ; 4 ] > ,
24942549 ) -> PyResult < ( ) > {
2495- let ( src_entity, flush) = resolve_composite_source ( src) ?;
2496- if let Some ( g) = flush {
2497- graphics_flush ( g) . map_err ( rt_err) ?;
2498- }
2550+ let src_entity = src. extract :: < ImageRef > ( ) ?. texture ( ) ?;
24992551 let sr = normalize_src_rect ( src_entity, src_rect) ?;
25002552 let dr = normalize_rect ( dst_rect, self . width as f32 , self . height as f32 ) ;
25012553 composite_apply ( self . entity , src_entity, COMPOSITE_MODE_REPLACE , sr, dr, 1.0 )
@@ -2519,10 +2571,7 @@ impl Graphics {
25192571 /// Applies a mask source's brightness as this graphics' alpha channel
25202572 /// (Processing `mask`). `mask` may be an `Image`, webcam, or `Graphics`.
25212573 pub fn mask ( & self , mask : & Bound < ' _ , PyAny > ) -> PyResult < ( ) > {
2522- let ( src_entity, flush) = resolve_composite_source ( mask) ?;
2523- if let Some ( g) = flush {
2524- graphics_flush ( g) . map_err ( rt_err) ?;
2525- }
2574+ let src_entity = mask. extract :: < ImageRef > ( ) ?. texture ( ) ?;
25262575 composite_apply (
25272576 self . entity ,
25282577 src_entity,
0 commit comments