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