Skip to content

Commit d13877d

Browse files
committed
Graphics get region and hidpi pixel averaging.
1 parent 471b60f commit d13877d

4 files changed

Lines changed: 139 additions & 15 deletions

File tree

‎crates/processing_pyo3/src/graphics.rs‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,13 +1196,34 @@ impl Graphics {
11961196
write_png_file(filename, raw.width, raw.height, &rgba)
11971197
}
11981198

1199-
/// Read a single pixel as a `Color` (Processing `get`).
1200-
pub fn get(&self, x: i64, y: i64) -> PyResult<crate::color::PyColor> {
1201-
let p = graphics_get_pixel(self.entity, x, y)
1202-
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
1203-
Ok(crate::color::PyColor(bevy::prelude::Color::Srgba(
1204-
p.map_or(Srgba::NONE, pixel_to_srgba),
1205-
)))
1199+
/// `get(x, y)` reads one pixel as a `Color`; `get(x, y, w, h)` copies a block into a new `Image`.
1200+
#[pyo3(signature = (x, y, w=None, h=None))]
1201+
pub fn get(
1202+
&self,
1203+
py: Python<'_>,
1204+
x: i64,
1205+
y: i64,
1206+
w: Option<u32>,
1207+
h: Option<u32>,
1208+
) -> PyResult<Py<PyAny>> {
1209+
match (w, h) {
1210+
(Some(w), Some(h)) => {
1211+
let entity = graphics_get_region(self.entity, x, y, w, h)
1212+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
1213+
Ok(Py::new(py, Image::wrap(entity))?.into_any())
1214+
}
1215+
(None, None) => {
1216+
let p = graphics_get_pixel(self.entity, x, y)
1217+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
1218+
let color = crate::color::PyColor(bevy::prelude::Color::Srgba(
1219+
p.map_or(Srgba::NONE, pixel_to_srgba),
1220+
));
1221+
Ok(Py::new(py, color)?.into_any())
1222+
}
1223+
_ => Err(pyo3::exceptions::PyTypeError::new_err(
1224+
"get() takes x, y or x, y, w, h",
1225+
)),
1226+
}
12061227
}
12071228

12081229
/// Write a single pixel (Processing `set`); outside the canvas it is dropped.

‎crates/processing_pyo3/src/lib.rs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,9 +2733,15 @@ pub mod processing_py {
27332733
}
27342734

27352735
#[pyfunction]
2736-
#[pyo3(pass_module)]
2737-
fn get(module: &Bound<'_, PyModule>, x: i64, y: i64) -> PyResult<PyColor> {
2738-
graphics!(module).get(x, y)
2736+
#[pyo3(pass_module, signature = (x, y, w=None, h=None))]
2737+
fn get(
2738+
module: &Bound<'_, PyModule>,
2739+
x: i64,
2740+
y: i64,
2741+
w: Option<u32>,
2742+
h: Option<u32>,
2743+
) -> PyResult<Py<PyAny>> {
2744+
graphics!(module).get(module.py(), x, y, w, h)
27392745
}
27402746

27412747
#[pyfunction]

‎crates/processing_render/src/graphics.rs‎

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
879916
pub 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

‎crates/processing_render/src/lib.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,17 @@ pub fn graphics_update(graphics_entity: Entity, pixels: &[LinearRgba]) -> error:
692692
})
693693
}
694694

695+
/// A new image of the logical block at (`x`, `y`) of the surface.
696+
pub fn graphics_get_region(
697+
graphics_entity: Entity,
698+
x: i64,
699+
y: i64,
700+
width: u32,
701+
height: u32,
702+
) -> error::Result<Entity> {
703+
app_mut(|app| graphics::get_region(app, graphics_entity, x, y, width, height))
704+
}
705+
695706
/// Update a region of the graphics surface with new pixel data.
696707
/// The pixel at logical (`x`, `y`); `None` outside the surface, where Processing's `get` is
697708
/// transparent black

0 commit comments

Comments
 (0)