@@ -876,6 +876,43 @@ fn pixel_scale(app: &App, entity: Entity) -> Result<(u32, u32, f64, f64)> {
876876 ) )
877877}
878878
879+ /// The color of logical pixel (`lx`, `ly`), an alpha-weighted average of its physical pixels.
880+ fn logical_pixel (
881+ data : & [ u8 ] ,
882+ format : TextureFormat ,
883+ ( pw, ph) : ( usize , usize ) ,
884+ ( sx, sy) : ( f64 , f64 ) ,
885+ lx : i64 ,
886+ ly : i64 ,
887+ ) -> Result < LinearRgba > {
888+ let px_size = pixel_size ( format) ?;
889+ let px0 = ( ( lx as f64 * sx) as usize ) . min ( pw - 1 ) ;
890+ let py0 = ( ( ly as f64 * sy) as usize ) . min ( ph - 1 ) ;
891+ let px1 = ( ( ( lx + 1 ) as f64 * sx) as usize ) . clamp ( px0 + 1 , pw) ;
892+ let py1 = ( ( ( ly + 1 ) as f64 * sy) as usize ) . clamp ( py0 + 1 , ph) ;
893+ let mut sum = [ 0.0f32 ; 4 ] ;
894+ for py in py0..py1 {
895+ for px in px0..px1 {
896+ let at = ( py * pw + px) * px_size;
897+ let p = crate :: image:: decode_pixel ( & data[ at..at + px_size] , format) ?;
898+ sum[ 0 ] += p. red * p. alpha ;
899+ sum[ 1 ] += p. green * p. alpha ;
900+ sum[ 2 ] += p. blue * p. alpha ;
901+ sum[ 3 ] += p. alpha ;
902+ }
903+ }
904+ if sum[ 3 ] <= 0.0 {
905+ return Ok ( LinearRgba :: NONE ) ;
906+ }
907+ let count = ( ( px1 - px0) * ( py1 - py0) ) as f32 ;
908+ Ok ( LinearRgba :: new (
909+ sum[ 0 ] / sum[ 3 ] ,
910+ sum[ 1 ] / sum[ 3 ] ,
911+ sum[ 2 ] / sum[ 3 ] ,
912+ sum[ 3 ] / count,
913+ ) )
914+ }
915+
879916/// The pixel at logical (`x`, `y`); `None` outside the canvas
880917pub fn get_pixel ( app : & mut App , entity : Entity , x : i64 , y : i64 ) -> Result < Option < LinearRgba > > {
881918 let ( lw, lh, sx, sy) = pixel_scale ( app, entity) ?;
@@ -890,11 +927,60 @@ pub fn get_pixel(app: &mut App, entity: Entity, x: i64, y: i64) -> Result<Option
890927 let Some ( data) = graphics. pixels . as_deref ( ) else {
891928 return Ok ( None ) ;
892929 } ;
893- let px_size = pixel_size ( graphics. texture_format ) ?;
894- let px = ( ( x as f64 * sx) as usize ) . min ( graphics. size . width as usize - 1 ) ;
895- let py = ( ( y as f64 * sy) as usize ) . min ( graphics. size . height as usize - 1 ) ;
896- let at = ( py * graphics. size . width as usize + px) * px_size;
897- crate :: image:: decode_pixel ( & data[ at..at + px_size] , graphics. texture_format ) . map ( Some )
930+ let physical = ( graphics. size . width as usize , graphics. size . height as usize ) ;
931+ logical_pixel ( data, graphics. texture_format , physical, ( sx, sy) , x, y) . map ( Some )
932+ }
933+
934+ /// A new `width` x `height` image of the logical block at (`x`, `y`).
935+ pub fn get_region (
936+ app : & mut App ,
937+ entity : Entity ,
938+ x : i64 ,
939+ y : i64 ,
940+ width : u32 ,
941+ height : u32 ,
942+ ) -> Result < Entity > {
943+ if width == 0 || height == 0 {
944+ return Err ( ProcessingError :: InvalidArgument (
945+ "a sub-image needs a width and height above zero" . to_string ( ) ,
946+ ) ) ;
947+ }
948+ let ( lw, lh, sx, sy) = pixel_scale ( app, entity) ?;
949+ ensure_pixels ( app, entity) ?;
950+ let graphics = app
951+ . world ( )
952+ . get :: < Graphics > ( entity)
953+ . ok_or ( ProcessingError :: GraphicsNotFound ) ?;
954+ let data = graphics
955+ . pixels
956+ . as_deref ( )
957+ . ok_or ( ProcessingError :: GraphicsNotFound ) ?;
958+ let format = graphics. texture_format ;
959+ let ( pw, ph) = ( graphics. size . width as usize , graphics. size . height as usize ) ;
960+
961+ let mut pixels = Vec :: with_capacity ( width as usize * height as usize ) ;
962+ for row in 0 ..height as i64 {
963+ for col in 0 ..width as i64 {
964+ let ( lx, ly) = ( x + col, y + row) ;
965+ if lx < 0 || ly < 0 || lx >= lw as i64 || ly >= lh as i64 {
966+ pixels. push ( LinearRgba :: NONE ) ;
967+ continue ;
968+ }
969+ pixels. push ( logical_pixel ( data, format, ( pw, ph) , ( sx, sy) , lx, ly) ?) ;
970+ }
971+ }
972+
973+ let format = TextureFormat :: Rgba8UnormSrgb ;
974+ let bytes = pixels_to_bytes ( & pixels, format) ?;
975+ let size = Extent3d {
976+ width,
977+ height,
978+ depth_or_array_layers : 1 ,
979+ } ;
980+ Ok ( app
981+ . world_mut ( )
982+ . run_system_cached_with ( crate :: image:: create, ( size, bytes, format) )
983+ . unwrap ( ) )
898984}
899985
900986/// Sets the pixel at logical (`x`, `y`), which covers a block of physical pixels
0 commit comments