mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-23 02:44:24 +01:00
Native file: a distorted image with a malformed saved corner opens placed by its transform
A native file whose `image_quad` held an entry that was not a point among
its first four failed to open: `image_corners` read each entry as a
`Point` and returned the error. Such corners, and an `image_quad` that is
not a list, are now dropped as the file is read, and the image opens
placed by its transform, as a list of other than four points already did.
A point is `[x, y]` or `{"x": x, "y": y}`; values that are not taken are
skipped without being kept in memory.
Follows #8392.
This commit is contained in:
1 parent
063f550802
commit
525dea58f0
3 files changed
+218
-25
No files matched your search
@@ -474,6 +474,50 @@ mod tests {
|
||||
assert_eq!(read.image_quad(), Some(q));
|
||||
}
|
||||
|
||||
/// An entry among the first four that is not a point, or a field that is not a list, drops the
|
||||
/// corners and leaves the rest of the image read; the entries after a bad one are skipped.
|
||||
#[test]
|
||||
fn malformed_corners_a_file_holds_are_dropped_as_it_is_read() {
|
||||
let n = image(100, 100);
|
||||
let plain: Node = serde_json::from_value(serde_json::to_value(&n).unwrap()).unwrap();
|
||||
let bad_entries = [
|
||||
serde_json::json!([[40.0, 0.0], "not a point", [100.0, 100.0], [0.0, 100.0]]),
|
||||
serde_json::json!([[40.0, 0.0], [60.0, 0.0], [100.0, 100.0], {"x": 1}]),
|
||||
serde_json::json!([null, [60.0, 0.0], [100.0, 100.0], [0.0, 100.0]]),
|
||||
serde_json::json!([[40.0, 0.0], [60.0], [100.0, 100.0], [0.0, 100.0], [5.0, 5.0]]),
|
||||
serde_json::json!([[40.0, 0.0], [[1.0, 2.0], 3.0]]),
|
||||
serde_json::json!([[40.0, 0.0], [60.0, 0.0, 1.0], [100.0, 100.0], [0.0, 100.0]]),
|
||||
serde_json::json!([[40.0, 0.0], {"x": 60.0, "y": "0"}, [100.0, 100.0], [0.0, 100.0]]),
|
||||
];
|
||||
let bad_fields = [serde_json::json!("corners"), serde_json::json!(4), serde_json::json!(true), serde_json::json!({"a": [1.0, 2.0]})];
|
||||
for quad in bad_entries.into_iter().chain(bad_fields) {
|
||||
let mut v = serde_json::to_value(&n).unwrap();
|
||||
v["image_quad"] = quad.clone();
|
||||
// File text, so the fields after the corners are read from the same stream.
|
||||
let text = serde_json::to_string(&v).unwrap();
|
||||
let read: Node = serde_json::from_str(&text).unwrap_or_else(|e| panic!("{quad}: {e}"));
|
||||
assert!(read.extras().image_quad.is_none(), "{quad}");
|
||||
assert!(!read.is_distorted_image(), "{quad}");
|
||||
assert_eq!(read, plain, "{quad}");
|
||||
assert_eq!(read.geometric_bounds(), Some(Rect::new(0.0, 0.0, 100.0, 100.0)), "{quad}");
|
||||
}
|
||||
// A point with a repeated coordinate.
|
||||
let mut v = serde_json::to_value(&n).unwrap();
|
||||
v["image_quad"] = serde_json::json!([[40.0, 0.0], "SECOND", [100.0, 100.0], [0.0, 100.0]]);
|
||||
let text = serde_json::to_string(&v).unwrap().replace("\"SECOND\"", r#"{"x":60.0,"y":0.0,"y":1.0}"#);
|
||||
let read: Node = serde_json::from_str(&text).unwrap();
|
||||
assert_eq!(read, plain);
|
||||
// Both forms of a point, whole numbers and unknown fields read as corners, from text and
|
||||
// from a value.
|
||||
let mut v = serde_json::to_value(&n).unwrap();
|
||||
v["image_quad"] = serde_json::json!([[40, 0], {"y": 0.0, "x": 60.0, "z": [1, 2]}, [100.0, 100.0], {"x": 0, "y": 100}]);
|
||||
let q = pts([(40.0, 0.0), (60.0, 0.0), (100.0, 100.0), (0.0, 100.0)]);
|
||||
let read: Node = serde_json::from_str(&serde_json::to_string(&v).unwrap()).unwrap();
|
||||
assert_eq!(read.image_quad(), Some(q));
|
||||
let read: Node = serde_json::from_value(v).unwrap();
|
||||
assert_eq!(read.image_quad(), Some(q));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refused_corners_change_nothing_and_a_bad_quad_in_a_file_is_ignored() {
|
||||
let mut n = image(10, 10);
|
||||
|
||||
+172
-23
@@ -264,39 +264,188 @@ pub(super) struct NodeWire {
|
||||
}
|
||||
|
||||
/// A distorted image's corners as a file holds them: a list of exactly four points, else none (the
|
||||
/// image is then placed by its transform). At most five points of a longer list are kept while it
|
||||
/// is read.
|
||||
/// image is then placed by its transform). A point is `[x, y]` or `{"x": x, "y": y}`. A value that
|
||||
/// is not a list, or a list with an entry that is not a point, is none too. Values that are not
|
||||
/// taken are skipped as they are read, without being kept in memory, and at most four entries of a
|
||||
/// longer list are decoded.
|
||||
fn image_corners<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Option<Box<[vectorcraft_geom::Point; 4]>>, D::Error> {
|
||||
use serde::de::{Deserializer, Error, IgnoredAny, MapAccess, SeqAccess, Visitor};
|
||||
use std::fmt::{Formatter, Result as FmtResult};
|
||||
use vectorcraft_geom::Point;
|
||||
|
||||
/// Skips the rest of a list; true when it held another entry.
|
||||
fn rest_of_list<'de, A: SeqAccess<'de>>(mut seq: A) -> Result<bool, A::Error> {
|
||||
let mut more = false;
|
||||
while seq.next_element::<IgnoredAny>()?.is_some() {
|
||||
more = true;
|
||||
}
|
||||
Ok(more)
|
||||
}
|
||||
fn rest_of_map<'de, A: MapAccess<'de>>(mut map: A) -> Result<(), A::Error> {
|
||||
while map.next_entry::<IgnoredAny, IgnoredAny>()?.is_some() {}
|
||||
Ok(())
|
||||
}
|
||||
// The visitors below give none for a value of a kind they do not take.
|
||||
macro_rules! none_for {
|
||||
(scalars) => {
|
||||
fn visit_bool<E: Error>(self, _: bool) -> Result<Self::Value, E> {
|
||||
Ok(None)
|
||||
}
|
||||
fn visit_str<E: Error>(self, _: &str) -> Result<Self::Value, E> {
|
||||
Ok(None)
|
||||
}
|
||||
fn visit_unit<E: Error>(self) -> Result<Self::Value, E> {
|
||||
Ok(None)
|
||||
}
|
||||
fn visit_none<E: Error>(self) -> Result<Self::Value, E> {
|
||||
Ok(None)
|
||||
}
|
||||
};
|
||||
(numbers) => {
|
||||
fn visit_i64<E: Error>(self, _: i64) -> Result<Self::Value, E> {
|
||||
Ok(None)
|
||||
}
|
||||
fn visit_u64<E: Error>(self, _: u64) -> Result<Self::Value, E> {
|
||||
Ok(None)
|
||||
}
|
||||
fn visit_f64<E: Error>(self, _: f64) -> Result<Self::Value, E> {
|
||||
Ok(None)
|
||||
}
|
||||
};
|
||||
(maps $de:lifetime) => {
|
||||
fn visit_map<A: MapAccess<$de>>(self, map: A) -> Result<Self::Value, A::Error> {
|
||||
rest_of_map(map)?;
|
||||
Ok(None)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// A coordinate: a number, else none.
|
||||
struct Coord(Option<f64>);
|
||||
impl<'de> serde::Deserialize<'de> for Coord {
|
||||
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||
struct V;
|
||||
impl<'de> Visitor<'de> for V {
|
||||
type Value = Option<f64>;
|
||||
fn expecting(&self, f: &mut Formatter) -> FmtResult {
|
||||
f.write_str("a number")
|
||||
}
|
||||
fn visit_i64<E: Error>(self, v: i64) -> Result<Self::Value, E> {
|
||||
Ok(Some(v as f64))
|
||||
}
|
||||
fn visit_u64<E: Error>(self, v: u64) -> Result<Self::Value, E> {
|
||||
Ok(Some(v as f64))
|
||||
}
|
||||
fn visit_f64<E: Error>(self, v: f64) -> Result<Self::Value, E> {
|
||||
Ok(Some(v))
|
||||
}
|
||||
fn visit_seq<A: SeqAccess<'de>>(self, seq: A) -> Result<Self::Value, A::Error> {
|
||||
rest_of_list(seq)?;
|
||||
Ok(None)
|
||||
}
|
||||
none_for!(scalars);
|
||||
none_for!(maps 'de);
|
||||
}
|
||||
d.deserialize_any(V).map(Coord)
|
||||
}
|
||||
}
|
||||
|
||||
/// The name of a point's field.
|
||||
enum Key {
|
||||
X,
|
||||
Y,
|
||||
Other,
|
||||
}
|
||||
impl<'de> serde::Deserialize<'de> for Key {
|
||||
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||
struct V;
|
||||
impl Visitor<'_> for V {
|
||||
type Value = Key;
|
||||
fn expecting(&self, f: &mut Formatter) -> FmtResult {
|
||||
f.write_str("a field name")
|
||||
}
|
||||
fn visit_str<E: Error>(self, v: &str) -> Result<Key, E> {
|
||||
Ok(match v {
|
||||
"x" => Key::X,
|
||||
"y" => Key::Y,
|
||||
_ => Key::Other,
|
||||
})
|
||||
}
|
||||
}
|
||||
d.deserialize_identifier(V)
|
||||
}
|
||||
}
|
||||
|
||||
/// One entry of the list: a point, else none.
|
||||
struct Corner(Option<Point>);
|
||||
impl<'de> serde::Deserialize<'de> for Corner {
|
||||
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||
struct V;
|
||||
impl<'de> Visitor<'de> for V {
|
||||
type Value = Option<Point>;
|
||||
fn expecting(&self, f: &mut Formatter) -> FmtResult {
|
||||
f.write_str("a point")
|
||||
}
|
||||
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
|
||||
let x = seq.next_element::<Coord>()?.and_then(|c| c.0);
|
||||
let y = match x {
|
||||
Some(_) => seq.next_element::<Coord>()?.and_then(|c| c.0),
|
||||
None => None,
|
||||
};
|
||||
let more = rest_of_list(seq)?;
|
||||
Ok(x.zip(y).filter(|_| !more).map(|(x, y)| Point::new(x, y)))
|
||||
}
|
||||
fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
|
||||
let (mut x, mut y, mut bad) = (None, None, false);
|
||||
while let Some(key) = map.next_key::<Key>()? {
|
||||
let slot = match key {
|
||||
Key::X => &mut x,
|
||||
Key::Y => &mut y,
|
||||
Key::Other => {
|
||||
map.next_value::<IgnoredAny>()?;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let v = map.next_value::<Coord>()?.0;
|
||||
// A repeated or non-numeric coordinate makes the entry no point.
|
||||
bad |= slot.is_some() || v.is_none();
|
||||
*slot = slot.or(v);
|
||||
}
|
||||
Ok(x.zip(y).filter(|_| !bad).map(|(x, y)| Point::new(x, y)))
|
||||
}
|
||||
none_for!(scalars);
|
||||
none_for!(numbers);
|
||||
}
|
||||
d.deserialize_any(V).map(Corner)
|
||||
}
|
||||
}
|
||||
|
||||
struct Corners;
|
||||
impl<'de> serde::de::Visitor<'de> for Corners {
|
||||
impl<'de> Visitor<'de> for Corners {
|
||||
type Value = Option<Box<[Point; 4]>>;
|
||||
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
fn expecting(&self, f: &mut Formatter) -> FmtResult {
|
||||
f.write_str("a list of points")
|
||||
}
|
||||
fn visit_none<E: serde::de::Error>(self) -> Result<Self::Value, E> {
|
||||
Ok(None)
|
||||
fn visit_some<D: Deserializer<'de>>(self, d: D) -> Result<Self::Value, D::Error> {
|
||||
d.deserialize_any(Corners)
|
||||
}
|
||||
fn visit_unit<E: serde::de::Error>(self) -> Result<Self::Value, E> {
|
||||
Ok(None)
|
||||
}
|
||||
fn visit_some<D: serde::Deserializer<'de>>(self, d: D) -> Result<Self::Value, D::Error> {
|
||||
d.deserialize_seq(Corners)
|
||||
}
|
||||
fn visit_seq<A: serde::de::SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
|
||||
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
|
||||
let mut points = Vec::with_capacity(4);
|
||||
while points.len() < 4
|
||||
&& let Some(p) = seq.next_element::<Point>()?
|
||||
{
|
||||
points.push(p);
|
||||
let mut malformed = false;
|
||||
while points.len() < 4 && !malformed {
|
||||
match seq.next_element::<Corner>()? {
|
||||
Some(Corner(Some(p))) => points.push(p),
|
||||
Some(Corner(None)) => malformed = true,
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
// A fifth entry drops the field, so the rest is skipped without decoding it.
|
||||
let mut extra = false;
|
||||
while seq.next_element::<serde::de::IgnoredAny>()?.is_some() {
|
||||
extra = true;
|
||||
}
|
||||
Ok(if extra { None } else { <[Point; 4]>::try_from(points).ok().map(Box::new) })
|
||||
// A malformed or fifth entry drops the field, so the rest is skipped without decoding it.
|
||||
let extra = rest_of_list(seq)?;
|
||||
Ok(if extra || malformed { None } else { <[Point; 4]>::try_from(points).ok().map(Box::new) })
|
||||
}
|
||||
none_for!(scalars);
|
||||
none_for!(numbers);
|
||||
none_for!(maps 'de);
|
||||
}
|
||||
d.deserialize_option(Corners)
|
||||
}
|
||||
|
||||
+2
-2
@@ -7565,8 +7565,8 @@ allowed. In EMF each piece stores only the pixels inside its clip, with a pixel
|
||||
clip, so they write the image as the parallelogram nearest its corners and warn. Corners that would fold the image over
|
||||
itself, put three corners on one line, join two corners, cross the warp's horizon or lie too close together for a
|
||||
perspective map are an error, and nothing changes. A transform that squeezes the corners past what a perspective map
|
||||
can place leaves the image at the parallelogram nearest them. A file whose `image_quad` holds other than four points,
|
||||
or breaks these rules, opens with the image placed by its transform. A drag is one undo step, and a distort that leaves
|
||||
can place leaves the image at the parallelogram nearest them. A file whose `image_quad` is not a list, holds other than four points
|
||||
(an entry that is not a point included), or breaks these rules, opens with the image placed by its transform. A drag is one undo step, and a distort that leaves
|
||||
the corners where they are records none. Image info (`image.info`) gives a distorted image's resolution at its middle, and Link
|
||||
Info (`links.info`) gives its scale and rotation there.
|
||||
|
||||
|
||||
Reference in new issue
Block a user