Skip to content

Commit b98b2eb

Browse files
committed
Graphics get region and hidpi pixel averaging.
1 parent b6c28f3 commit b98b2eb

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
@@ -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
880917
pub 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

‎crates/processing_render/src/lib.rs‎

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

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

0 commit comments

Comments
 (0)