mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 06:50:57 +00:00
.ai import: boxes of differing patches on an image-only page use the pixel size of the comparison raster
The comparison of an image-only page draws at four times the comparison scale and resizes the result to the comparison size. The two sizes round differently along the shorter side, so a pixel is not 1/scale points wide. A box at the far edge of a large page moved by up to 0.625/scale points and could lie outside the object that holds it, so the import used the PDF part instead of the layers. The boxes now use the size the raster was drawn at.
This commit is contained in:
@@ -200,4 +200,66 @@ mod tests {
|
||||
let shifted = Rect::new(0.0, 0.0, 500.0, 500.0);
|
||||
assert!(view.difference(&vector, shifted, false).any > MAX_DIFFERENCE, "wrong framing remains an error");
|
||||
}
|
||||
|
||||
/// The boxes `solid_regions` gives for a page of images are in the page's points: the
|
||||
/// comparison is drawn at a multiple of the scale and resized, and the two sizes round
|
||||
/// differently on the short side, so a box at the far edge does not drift out of the object
|
||||
/// that makes it.
|
||||
#[test]
|
||||
fn solid_regions_of_a_raster_page_lie_in_the_object_that_differs() {
|
||||
for bottom in (884..=900).map(f64::from) {
|
||||
let object = Rect::new(1000.0, 0.0, 3000.0, bottom);
|
||||
for r in raster_page_regions(object, 4000.0, 1006.0) {
|
||||
assert!(
|
||||
r.x0 >= object.x0 - 1.0 && r.y0 >= object.y0 - 1.0 && r.x1 <= object.x1 + 1.0 && r.y1 <= object.y1 + 1.0,
|
||||
"{r:?} lies outside {object:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The boxes `solid_regions` gives for a `width` × `height` page that is one image of a black
|
||||
/// `object`, compared with layers that hold nothing.
|
||||
fn raster_page_regions(object: Rect, width: f64, height: f64) -> Vec<Rect> {
|
||||
use vectorcraft_color::{Color, Paint};
|
||||
use vectorcraft_doc::{Appearance, ImageBlob, ImageObject};
|
||||
let mut vector = Document::new(width, height);
|
||||
let layer = vector.layers[0].id;
|
||||
let n = Node::path(
|
||||
vector.alloc_id(),
|
||||
vectorcraft_geom::shapes::rectangle(object),
|
||||
Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0),
|
||||
);
|
||||
vector.insert(Some(layer), usize::MAX, n).unwrap();
|
||||
let rect = vector.artboards[0].rect;
|
||||
let source = vectorcraft_render::Renderer::new().render_region(&vector, rect, 1.0, true);
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_raw(source.width, source.height, source.to_straight())
|
||||
.unwrap()
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
let blob = ImageBlob::new("image/png", png);
|
||||
let key = blob.content_key();
|
||||
let mut page = Document::new(width, height);
|
||||
page.images.insert(key.clone(), blob);
|
||||
let image = Node::new(
|
||||
page.alloc_id(),
|
||||
NodeKind::Image(Box::new(ImageObject {
|
||||
key,
|
||||
width: source.width,
|
||||
height: source.height,
|
||||
xf: Affine::IDENTITY,
|
||||
link: None,
|
||||
placement: Default::default(),
|
||||
pixels: None,
|
||||
})),
|
||||
);
|
||||
page.insert(Some(page.layers[0].id), usize::MAX, image).unwrap();
|
||||
assert_eq!(comparison_samples(&page), 4);
|
||||
let frame = Frame { page: &page, page_rect: rect, rect, whole: false, pages: vec![(rect, Affine::IDENTITY)] };
|
||||
let mut view = PageView::new(&frame);
|
||||
let (_, regions) = view.solid_regions(&Document::new(width, height), rect, false).unwrap();
|
||||
assert!(!regions.is_empty());
|
||||
regions
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,6 +231,11 @@ impl PageView {
|
||||
if solid.len() != w * h {
|
||||
return None;
|
||||
}
|
||||
// A pixel's size in points: [`comparison_render`] draws at `scale * samples`, rounds that
|
||||
// raster to whole pixels and resizes it to `w` × `h`, so the two sizes can round differently.
|
||||
let k = f64::from(self.samples);
|
||||
let drawn = vectorcraft_render::raster_size(rb, self.scale * k).map_or((w as f64, h as f64), |(dw, dh)| (f64::from(dw), f64::from(dh)));
|
||||
let pitch = (drawn.0 / (w as f64 * k * self.scale), drawn.1 / (h as f64 * k * self.scale));
|
||||
let mut regions = vec![];
|
||||
let mut stack = vec![];
|
||||
for start in 0..solid.len() {
|
||||
@@ -255,8 +260,7 @@ impl PageView {
|
||||
}
|
||||
}
|
||||
}
|
||||
let s = self.scale;
|
||||
let at = |i: usize, j: usize| (rb.x0 + i as f64 / s, rb.y0 + j as f64 / s);
|
||||
let at = |i: usize, j: usize| (rb.x0 + i as f64 * pitch.0, rb.y0 + j as f64 * pitch.1);
|
||||
let ((x0, y0), (x1, y1)) = (at(i0, j0), at(i1 + 1, j1 + 1));
|
||||
regions.push(Rect::new(x0, y0, x1, y1));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user