Skip to content

armgit / klad

Public Imported from GitHub
0

A drawing app built for agent/human collaboration

Rust

Posed fills blend between pixels like transformed ones

Indicode BV0aeb082c8744
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dd67094..f31c3e9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,7 @@
- Fixed opening a drawing on the Mac: **Choose a .klad file...** in the start window (it said folder) and **File > Open…** could not pick a `.klad` drawing, which Finder shows as one file. The open panel now takes a `.klad` drawing; other files are greyed out.
- Fixed shade and rim on posed layers. The shade and rim fills that `klad light` makes keep the part of the shape outside a cut, and a pose turned the fill with its bone but left that cut where it was in the rest drawing, so in posed frames and their in-betweens the rim grew into a wide crescent and the shade slid away. Posing now moves the cut with the fill, in `klad apply` poses, in-betweens and the editor alike; a frame posed before this comes right when you pose it again.
- Fixed cuts in in-betweens. A fill or stroke cut by a lasso kept its cut where it was in the first key when an in-between moved it: flood fills between keys, and marks that ride a line (marks drawn by hand between keys and marks found in one key only). The cut now moves with the mark.
+- Fixed hard edges on posed fills. A pose sampled each fill at the nearest pixel, so a turned fill got a stepped edge and thin bands along it broke into dots; it now blends between pixels as `klad transform` does, and a pose and a transform of the same turn give the same picture. Frames posed before this keep their old edge until they are posed again.
## 0.2.0 (2026-11-12)
diff --git a/crates/core/src/fill.rs b/crates/core/src/fill.rs
index 241805f..534d9b1 100644
--- a/crates/core/src/fill.rs
+++ b/crates/core/src/fill.rs
@@ -122,6 +122,19 @@ impl Coverage {
self.alpha[((y - self.y) * self.width + (x - self.x)) as usize]
}
+ pub fn between(&self, x: f32, y: f32) -> f32 {
+ let at = |x: i64, y: i64| match x >= 0 && y >= 0 {
+ true => self.at(x as u32, y as u32) as f32,
+ false => 0.0,
+ };
+ let (fx, fy) = (x - 0.5, y - 0.5);
+ let (ix, iy) = (fx.floor() as i64, fy.floor() as i64);
+ let (tx, ty) = (fx - ix as f32, fy - iy as f32);
+ let top = at(ix, iy) * (1.0 - tx) + at(ix + 1, iy) * tx;
+ let bottom = at(ix, iy + 1) * (1.0 - tx) + at(ix + 1, iy + 1) * tx;
+ top * (1.0 - ty) + bottom * ty
+ }
+
pub fn bounds(&self) -> Option<[f32; 4]> {
(!self.is_empty()).then(|| {
[
diff --git a/crates/core/src/markedit.rs b/crates/core/src/markedit.rs
index 0290214..016f332 100644
--- a/crates/core/src/markedit.rs
+++ b/crates/core/src/markedit.rs
@@ -443,12 +443,7 @@ fn resampled(c: &Coverage, a: Affine, hard: bool) -> Coverage {
false => 0,
};
}
- let (fx, fy) = (sx - 0.5, sy - 0.5);
- let (ix, iy) = (fx.floor() as i64, fy.floor() as i64);
- let (tx, ty) = (fx - ix as f32, fy - iy as f32);
- let top = at(ix, iy) * (1.0 - tx) + at(ix + 1, iy) * tx;
- let bottom = at(ix, iy + 1) * (1.0 - tx) + at(ix + 1, iy + 1) * tx;
- (top * (1.0 - ty) + bottom * ty).round().clamp(0.0, 255.0) as u8
+ c.between(sx, sy).round().clamp(0.0, 255.0) as u8
})
.collect();
let mut out = Coverage::from_mask(w, h, &mask);
diff --git a/crates/core/src/rig.rs b/crates/core/src/rig.rs
index 8c7a2f4..fe69af8 100644
--- a/crates/core/src/rig.rs
+++ b/crates/core/src/rig.rs
@@ -843,13 +843,7 @@ impl<'a> Rig<'a> {
.collect();
let (w, h) = (canvas.0 as usize, canvas.1 as usize);
let mut mask = vec![0u8; w * h];
- let sample = |p: [f32; 2]| -> u8 {
- let (x, y) = (p[0].floor(), p[1].floor());
- match x >= 0.0 && y >= 0.0 {
- true => c.at(x as u32, y as u32),
- false => 0,
- }
- };
+ let sample = |p: [f32; 2]| -> u8 { c.between(p[0], p[1]).round().clamp(0.0, 255.0) as u8 };
(0..rows - 1).for_each(|j| {
(0..cols - 1).for_each(|i| {
let corner = |di: u32, dj: u32| ((i + di, j + dj), posed[((j + dj) * cols + i + di) as usize]);
@@ -1694,4 +1688,47 @@ mod tests {
);
});
}
+
+ #[test]
+ fn a_posed_fill_keeps_the_soft_edge_of_the_same_turn_by_transform() {
+ use crate::fill::Fill;
+ let bones = leg();
+ let alpha: Vec<u8> = (0..60u32)
+ .flat_map(|y| (0..60u32).map(move |x| (x, y)))
+ .map(|(x, y)| {
+ let d = (x as f32 + 0.5 - 30.0).hypot(y as f32 + 0.5 - 30.0) - 20.0;
+ ((0.5 - d) * 255.0).round().clamp(0.0, 255.0) as u8
+ })
+ .collect();
+ let disc = Fill::new(
+ 1,
+ [0.0, 0.0, 0.0, 1.0],
+ FillSource::Polygon(vec![[70.0, 120.0], [130.0, 120.0], [130.0, 180.0]]),
+ Coverage {
+ x: 70,
+ y: 120,
+ width: 60,
+ height: 60,
+ alpha,
+ },
+ );
+ let weights = Weights::auto(&bones, (200, 400));
+ let mut turned = Pose::default();
+ turned.angles.insert("thigh".into(), 10.0);
+ let rig = Rig::new(&bones, &[], &weights, &turned).following(Some("thigh"));
+ let posed = rig.pose(&[Mark::from(disc.clone())], (200, 400));
+ let (o, ex, ey) = (rig.moved([0.0, 0.0]), rig.moved([1.0, 0.0]), rig.moved([0.0, 1.0]));
+ let mut by_hand = disc;
+ crate::markedit::transform_fill(
+ &mut by_hand,
+ [ex[0] - o[0], ey[0] - o[0], ex[1] - o[1], ey[1] - o[1], o[0], o[1]],
+ );
+ let (a, b) = (&posed[0].as_fill().unwrap().coverage, &by_hand.coverage);
+ let worst = (0..400u32)
+ .flat_map(|y| (0..200u32).map(move |x| (x, y)))
+ .map(|(x, y)| a.at(x, y).abs_diff(b.at(x, y)))
+ .max()
+ .unwrap_or(0);
+ assert!(worst <= 2, "the posed edge is {worst} off the turned one");
+ }
}