mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 09:36:45 +00:00
Merge main into fix/transform-copies-raster-effects-groups
This commit is contained in:
@@ -42,13 +42,7 @@ pub(crate) fn blob_pixmap(blob: &vectorcraft_doc::ImageBlob, touch: bool) -> Opt
|
||||
// Decoded outside the lock: other renderers carry on meanwhile. The inks and the pixels
|
||||
// shown from them are admitted together, as other images are, before either is made:
|
||||
// without room the image is read as other images are (which reduces it).
|
||||
let room = || {
|
||||
image::ImageReader::new(std::io::Cursor::new(blob.bytes.as_slice()))
|
||||
.with_guessed_format()
|
||||
.ok()
|
||||
.and_then(|r| r.into_dimensions().ok())
|
||||
.is_some_and(|(w, h)| paint::within_allowance(w, h.saturating_mul(2)))
|
||||
};
|
||||
let room = || blob.header_size().is_some_and(|(w, h)| paint::within_allowance(w, h.saturating_mul(2)));
|
||||
let pm = match lut.filter(|_| room()).and_then(|lut| ink::cmyk_pixmap(blob, &lut)) {
|
||||
Some(pm) => pm,
|
||||
None => match paint::decode_pixmap_or_why(&blob.bytes) {
|
||||
|
||||
@@ -186,11 +186,16 @@ pub fn on_white(p: &[u8; 4]) -> [u8; 3] {
|
||||
|
||||
/// Lossless WebP.
|
||||
pub(crate) fn webp(img: &Rendered) -> Result<Vec<u8>, String> {
|
||||
let mut buf = Vec::new();
|
||||
let enc = image::codecs::webp::WebPEncoder::new_lossless(&mut buf);
|
||||
image::ImageEncoder::write_image(enc, &img.to_straight(), img.width, img.height, image::ExtendedColorType::Rgba8)
|
||||
.map_err(|e| format!("WebP encoding failed: {e}"))?;
|
||||
Ok(buf)
|
||||
let src = craft_image_codecs::Image::from_raw(
|
||||
img.width,
|
||||
img.height,
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U8,
|
||||
img.to_straight(),
|
||||
)
|
||||
.map_err(|e| format!("WebP encoding failed: {e}"))?;
|
||||
let opts = craft_image_codecs::EncodeOptions { webp_lossless: true, ..craft_image_codecs::EncodeOptions::default() };
|
||||
craft_image_codecs::encode(&src, craft_image_codecs::Format::WebP, &opts).map_err(|e| format!("WebP encoding failed: {e}"))
|
||||
}
|
||||
|
||||
/// Bounds of the art an export draws: visible objects off template layers (guides left out),
|
||||
|
||||
@@ -1,33 +1,21 @@
|
||||
//! GIF export: a palette image ([`super::quantize`]) as a one-frame GIF89a with its transparent
|
||||
//! entry, optionally interlaced (rows in four passes, so the image builds up while it loads).
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use super::quantize::Indexed;
|
||||
|
||||
/// Encode `img` as a GIF file.
|
||||
pub fn encode(img: &Indexed, interlaced: bool) -> Result<Vec<u8>, String> {
|
||||
let fail = |e: gif::EncodingError| format!("GIF encoding failed: {e}");
|
||||
let (Ok(w), Ok(h)) = (u16::try_from(img.width), u16::try_from(img.height)) else {
|
||||
if img.width > u32::from(u16::MAX) || img.height > u32::from(u16::MAX) {
|
||||
return Err(format!("{} × {} pixels is too large for GIF (at most 65535 pixels a side): lower the resolution", img.width, img.height));
|
||||
};
|
||||
if w == 0 || h == 0 || img.indices.len() != w as usize * h as usize || img.palette.is_empty() || img.palette.len() > 256 {
|
||||
}
|
||||
if img.width == 0
|
||||
|| img.height == 0
|
||||
|| img.indices.len() != img.width as usize * img.height as usize
|
||||
|| img.palette.is_empty()
|
||||
|| img.palette.len() > 256
|
||||
{
|
||||
return Err("GIF encoding failed: the palette image is malformed".into());
|
||||
}
|
||||
let buffer = if interlaced { Cow::Owned(interlace(&img.indices, w as usize)) } else { Cow::Borrowed(img.indices.as_slice()) };
|
||||
let mut enc = gif::Encoder::new(Vec::new(), w, h, &img.palette.concat()).map_err(fail)?;
|
||||
let frame = gif::Frame { width: w, height: h, buffer, transparent: img.transparent, interlaced, ..gif::Frame::default() };
|
||||
enc.write_frame(&frame).map_err(fail)?;
|
||||
enc.into_inner().map_err(fail)
|
||||
}
|
||||
|
||||
/// The rows of a `width`-wide image in GIF's interlaced order: every 8th row from 0, every 8th
|
||||
/// from 4, every 4th from 2, then every 2nd from 1.
|
||||
fn interlace(px: &[u8], width: usize) -> Vec<u8> {
|
||||
let rows: Vec<&[u8]> = px.chunks(width.max(1)).collect();
|
||||
[(0, 8), (4, 8), (2, 4), (1, 2)]
|
||||
.into_iter()
|
||||
.flat_map(|(start, step)| rows.iter().skip(start).step_by(step))
|
||||
.flat_map(|r| r.iter().copied())
|
||||
.collect()
|
||||
craft_gif::write::encode_indexed(img.width, img.height, &img.palette, &img.indices, img.transparent, interlaced)
|
||||
.map_err(|e| format!("GIF encoding failed: {e}"))
|
||||
}
|
||||
|
||||
@@ -32,7 +32,25 @@ fn noise(w: u32, h: u32, opaque: bool) -> Vec<u8> {
|
||||
}
|
||||
|
||||
fn decode(png: &[u8]) -> Vec<u8> {
|
||||
image::load_from_memory_with_format(png, image::ImageFormat::Png).expect("decodes").to_rgba8().into_raw()
|
||||
craft_image_codecs::decode_as(craft_image_codecs::Format::Png, png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.expect("decodes")
|
||||
.to_rgba8()
|
||||
.into_raw()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -73,7 +91,27 @@ fn png_refuses_a_buffer_of_the_wrong_size() {
|
||||
#[test]
|
||||
fn gray_and_bilevel_images_store_gray() {
|
||||
let header = |file: &[u8]| (file[24], file[25]);
|
||||
let gray = |file: &[u8]| image::load_from_memory(file).unwrap().to_luma_alpha8().into_raw();
|
||||
let gray = |file: &[u8]| {
|
||||
craft_image_codecs::decode(file)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_luma_alpha8()
|
||||
.into_raw()
|
||||
};
|
||||
// 3 × 2, premultiplied: black, red, light grey; white, mid grey, almost black.
|
||||
let mut img = Rendered {
|
||||
width: 3,
|
||||
@@ -116,7 +154,24 @@ fn circle_doc() -> Document {
|
||||
|
||||
fn export(d: &Document, format: RasterFormat, o: &RasterExportOptions) -> image::RgbaImage {
|
||||
let bytes = Renderer::new().export_region(d, d.artboards[0].rect, format, o).unwrap();
|
||||
image::load_from_memory(&bytes).unwrap().to_rgba8()
|
||||
craft_image_codecs::decode(&bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -175,7 +230,24 @@ fn translucent_edges_over_opaque_art_stay_opaque() {
|
||||
(&d, RasterExportOptions { ppi: 72.0 * 0.3515625, ..Default::default() }),
|
||||
] {
|
||||
let png = Renderer::new().export_region(doc, doc.artboards[0].rect, RasterFormat::Png, &o).unwrap();
|
||||
let img = image::load_from_memory(&png).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
let below: Vec<_> = img.pixels().filter(|p| p[3] < 255).collect();
|
||||
assert!(below.is_empty(), "ppi {}: {} pixels below alpha 255, e.g. {:?}", o.ppi, below.len(), below.first());
|
||||
assert_eq!(png[25], 2, "IHDR colour type: RGB");
|
||||
|
||||
@@ -23,8 +23,25 @@ pub(super) fn art(w: u32, h: u32) -> Vec<u8> {
|
||||
px
|
||||
}
|
||||
|
||||
pub(super) fn decode(file: &[u8], format: image::ImageFormat) -> image::RgbaImage {
|
||||
image::load_from_memory_with_format(file, format).expect("decodes").to_rgba8()
|
||||
pub(super) fn decode(file: &[u8], format: craft_image_codecs::Format) -> image::RgbaImage {
|
||||
craft_image_codecs::decode_as(format, file)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.expect("decodes")
|
||||
.to_rgba8()
|
||||
}
|
||||
|
||||
fn bmp(px: &[u8], w: u32, h: u32, o: &BmpOptions) -> Vec<u8> {
|
||||
@@ -41,7 +58,7 @@ fn every_depth_writes_its_bit_count_and_decodes() {
|
||||
assert_eq!(u32_at(&file, 2) as usize, file.len(), "file size");
|
||||
assert_eq!(u16_at(&file, 28), u16::from(depth), "bit count");
|
||||
assert_eq!(u32_at(&file, 38), pixels_per_metre(150.0), "resolution");
|
||||
let img = decode(&file, image::ImageFormat::Bmp);
|
||||
let img = decode(&file, craft_image_codecs::Format::Bmp);
|
||||
assert_eq!(img.dimensions(), (w, h));
|
||||
let got = img.get_pixel(5, 3).0;
|
||||
let want = on_white(px[(3 * w as usize + 5) * 4..][..4].try_into().unwrap());
|
||||
@@ -71,13 +88,13 @@ fn rle8_and_rle4_decode_like_uncompressed() {
|
||||
assert_eq!(u32_at(&rle, 30), if depth == 8 { 1 } else { 2 }, "BI_RLE8 / BI_RLE4");
|
||||
assert_eq!(u32_at(&rle, 34) as usize, rle.len() - u32_at(&rle, 10) as usize, "the compressed size");
|
||||
assert!(rle.len() < plain.len(), "{} vs {}", rle.len(), plain.len());
|
||||
assert_eq!(decode(&rle, image::ImageFormat::Bmp), decode(&plain, image::ImageFormat::Bmp), "depth {depth}");
|
||||
assert_eq!(decode(&rle, craft_image_codecs::Format::Bmp), decode(&plain, craft_image_codecs::Format::Bmp), "depth {depth}");
|
||||
}
|
||||
// Rows of many different pixels go in absolute mode.
|
||||
let noisy: Vec<u8> = (0..40u32 * 4).flat_map(|i| [(i * 37 % 256) as u8, (i * 91 % 256) as u8, (i * 13 % 256) as u8, 255]).collect();
|
||||
let plain = bmp(&noisy, 40, 4, &BmpOptions { depth: 8, ..Default::default() });
|
||||
let rle = bmp(&noisy, 40, 4, &BmpOptions { depth: 8, rle: true, ..Default::default() });
|
||||
assert_eq!(decode(&rle, image::ImageFormat::Bmp), decode(&plain, image::ImageFormat::Bmp));
|
||||
assert_eq!(decode(&rle, craft_image_codecs::Format::Bmp), decode(&plain, craft_image_codecs::Format::Bmp));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -91,7 +108,7 @@ fn flipped_rows_have_a_negative_height() {
|
||||
let offset = u32_at(&down, 10) as usize;
|
||||
// Top-down: the first row stored is the image's first (its transparent pixel, on white).
|
||||
assert_eq!(&down[offset..offset + 3], &[255, 255, 255]);
|
||||
assert_eq!(decode(&up, image::ImageFormat::Bmp), decode(&down, image::ImageFormat::Bmp));
|
||||
assert_eq!(decode(&up, craft_image_codecs::Format::Bmp), decode(&down, craft_image_codecs::Format::Bmp));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -106,7 +123,7 @@ fn os2_bitmaps_have_a_core_header() {
|
||||
let entries = if depth <= 8 { 1 << depth } else { 0 };
|
||||
assert_eq!(u32_at(&file, 10), 14 + 12 + entries * 3);
|
||||
let windows = bmp(&px, w, h, &BmpOptions { depth, ..Default::default() });
|
||||
assert_eq!(decode(&file, image::ImageFormat::Bmp), decode(&windows, image::ImageFormat::Bmp), "depth {depth}");
|
||||
assert_eq!(decode(&file, craft_image_codecs::Format::Bmp), decode(&windows, craft_image_codecs::Format::Bmp), "depth {depth}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,7 +132,7 @@ fn greys_and_impossible_combinations() {
|
||||
let (w, h) = (8, 2);
|
||||
let px = art(w, h);
|
||||
for depth in [8, 24] {
|
||||
let img = decode(&bmp(&px, w, h, &BmpOptions { depth, gray: true, ..Default::default() }), image::ImageFormat::Bmp);
|
||||
let img = decode(&bmp(&px, w, h, &BmpOptions { depth, gray: true, ..Default::default() }), craft_image_codecs::Format::Bmp);
|
||||
assert!(img.pixels().all(|p| p[0] == p[1] && p[1] == p[2]), "depth {depth}: greys");
|
||||
}
|
||||
let bad = [
|
||||
|
||||
@@ -108,7 +108,23 @@ fn methods_write_baseline_or_progressive_frames() {
|
||||
assert_eq!(frame(&p3).0, 0xC2, "SOF2");
|
||||
assert!(sos(&p3) > 1 && sos(&p5) > sos(&p3), "more scans: {} {}", sos(&p3), sos(&p5));
|
||||
for f in [&base, &p3, &p5] {
|
||||
let back = image::load_from_memory(f).unwrap();
|
||||
let back = craft_image_codecs::decode(f)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!((back.width(), back.height()), (40, 30));
|
||||
}
|
||||
}
|
||||
@@ -184,7 +200,24 @@ fn cmyk_exports_keep_the_inks_of_cmyk_colours() {
|
||||
assert!(red[1] > 200 && red[2] > 200 && red[0] < 40, "RGB red separates to magenta and yellow: {red:?}");
|
||||
let file = Renderer::new().export_region(&d, d.artboards[0].rect, RasterFormat::Jpeg, &o).unwrap();
|
||||
assert_eq!(frame(&file).1, 4, "4 components");
|
||||
let back = image::load_from_memory(&file).unwrap().to_rgb8();
|
||||
let back = craft_image_codecs::decode(&file)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgb8();
|
||||
assert!(back.get_pixel(3, 5).0.iter().all(|v| *v < 60), "black reads back dark: {:?}", back.get_pixel(3, 5));
|
||||
let r = back.get_pixel(15, 5).0;
|
||||
assert!(r[0] > 180 && r[1] < 90, "red reads back red: {r:?}");
|
||||
@@ -196,7 +229,24 @@ fn gray_exports_keep_the_lightness() {
|
||||
let o = RasterExportOptions { jpeg: JpegOptions { color_model: ColorModel::Gray, ..Default::default() }, ..Default::default() };
|
||||
let file = Renderer::new().export_region(&d, d.artboards[0].rect, RasterFormat::Jpeg, &o).unwrap();
|
||||
assert_eq!(frame(&file).1, 1);
|
||||
let back = image::load_from_memory(&file).unwrap().to_luma8();
|
||||
let back = craft_image_codecs::decode(&file)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_luma8();
|
||||
let red = back.get_pixel(15, 5)[0];
|
||||
// sRGB red's relative luminance 0.2126, encoded: about 127.
|
||||
assert!((115..=140).contains(&red), "{red}");
|
||||
|
||||
@@ -141,7 +141,24 @@ fn transparent_pixels_get_their_own_entry() {
|
||||
|
||||
let file = encode_indexed(&ix, &PngOptions::default()).unwrap();
|
||||
assert_eq!(chunk(&file, b"tRNS").unwrap(), [0]);
|
||||
let back = image::load_from_memory(&file).unwrap().to_rgba8();
|
||||
let back = craft_image_codecs::decode(&file)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(back.get_pixel(0, 0).0, [255, 0, 0, 255]);
|
||||
assert_eq!(back.get_pixel(1, 0)[3], 0);
|
||||
}
|
||||
@@ -154,7 +171,24 @@ fn png8_decodes_at_every_bit_depth_and_interlaced() {
|
||||
for interlaced in [false, true] {
|
||||
let file = encode_indexed(&ix, &PngOptions { ppi: Some(144.0), interlaced }).unwrap();
|
||||
assert_eq!((file[24], file[25], file[28]), (depth, 3, u8::from(interlaced)));
|
||||
let back = image::load_from_memory(&file).unwrap().to_rgb8();
|
||||
let back = craft_image_codecs::decode(&file)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgb8();
|
||||
let want: Vec<u8> = colours(&ix).into_iter().flatten().collect();
|
||||
assert_eq!(back.into_raw(), want, "{colors} colours, interlaced {interlaced}");
|
||||
assert!(chunk(&file, b"pHYs").is_some());
|
||||
@@ -169,7 +203,24 @@ fn gif_decodes() {
|
||||
for interlaced in [false, true] {
|
||||
let file = gif::encode(&ix, interlaced).unwrap();
|
||||
assert_eq!(&file[..6], b"GIF89a");
|
||||
let back = image::load_from_memory_with_format(&file, image::ImageFormat::Gif).unwrap().to_rgba8();
|
||||
let back = craft_image_codecs::decode_as(craft_image_codecs::Format::Gif, &file)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(back.dimensions(), (21, 17));
|
||||
for (i, p) in back.pixels().enumerate() {
|
||||
match ix.indices[i] {
|
||||
|
||||
@@ -14,7 +14,7 @@ fn targa_depths_alpha_and_footer() {
|
||||
assert_eq!((u16_at(&file, 12), u16_at(&file, 14)), (7, 3));
|
||||
assert_eq!(file.len(), 18 + (w * h) as usize * usize::from(depth / 8) + 26);
|
||||
assert!(file.ends_with(b"TRUEVISION-XFILE.\0"));
|
||||
let img = decode(&file, image::ImageFormat::Tga);
|
||||
let img = decode(&file, craft_image_codecs::Format::Tga);
|
||||
assert_eq!(img.dimensions(), (w, h));
|
||||
let want = on_white(px[(2 * w as usize + 3) * 4..][..4].try_into().unwrap());
|
||||
let got = img.get_pixel(3, 2).0;
|
||||
|
||||
@@ -202,3 +202,33 @@ fn packbits_round_trips() {
|
||||
assert_eq!(out, data);
|
||||
}
|
||||
}
|
||||
|
||||
/// TIFF's LZW strips through craft-stream-codecs (#778) read back as the strip they were made of
|
||||
/// with weezl, the writer before them, and take no more bytes than weezl's: the same bytes until
|
||||
/// the code table first fills, after which the two clear it at different codes.
|
||||
#[test]
|
||||
fn lzw_strips_read_back_as_weezl_reads_them() {
|
||||
let mut seed = 0x9e37_79b9_7f4a_7c15u64;
|
||||
let mut noise = |n: usize, spread: u64| {
|
||||
(0..n)
|
||||
.map(|_| {
|
||||
seed ^= seed << 13;
|
||||
seed ^= seed >> 7;
|
||||
seed ^= seed << 17;
|
||||
(seed % spread) as u8
|
||||
})
|
||||
.collect::<Vec<u8>>()
|
||||
};
|
||||
let strips =
|
||||
[vec![], vec![7], vec![0; 70_000], noise(8_192, 256), noise(65_536, 4), noise(200_000, 256), (0..=255).cycle().take(30_000).collect()];
|
||||
for strip in &strips {
|
||||
let weezl = weezl::encode::Encoder::with_tiff_size_switch(weezl::BitOrder::Msb, 8).encode(strip).unwrap();
|
||||
let craft = craft_stream_codecs::lzw::encode(strip, true);
|
||||
let back = weezl::decode::Decoder::with_tiff_size_switch(weezl::BitOrder::Msb, 8).decode(&craft).unwrap();
|
||||
assert!(back == *strip, "{} bytes read back", strip.len());
|
||||
assert!(craft.len() <= weezl.len(), "{} bytes: {} against {}", strip.len(), craft.len(), weezl.len());
|
||||
// Up to the first table reset (4,093 codes of at most 12 bits) the codes are the same.
|
||||
let same = craft.iter().zip(&weezl).take_while(|(a, b)| a == b).count();
|
||||
assert!(same >= weezl.len().min(4_300), "{} bytes: the same for {same}", strip.len());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,15 +95,75 @@ fn comments_are_valid_files() {
|
||||
let ix = quantize(&image(5, 4, |x, _| [x as u8 * 40, 0, 0]), 5, 4, &PaletteOptions::default());
|
||||
let file = gif_comment(gif::encode(&ix, false).unwrap(), &"Copyright: Someone ".repeat(20));
|
||||
assert!(file.windows(2).any(|w| w == [0x21, 0xFE]));
|
||||
assert_eq!(image::load_from_memory(&file).unwrap().width(), 5);
|
||||
assert_eq!(
|
||||
craft_image_codecs::decode(&file)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.width(),
|
||||
5
|
||||
);
|
||||
let px = vec![200u8; 5 * 4 * 3];
|
||||
let jpg = jpeg::encode(&px, 5, 4, 80, Some(72.0), &jpeg::JpegOptions::default()).unwrap();
|
||||
let with = jpeg_comment(jpg.clone(), "Copyright: Someone");
|
||||
assert_eq!(with.len(), jpg.len() + 4 + 18);
|
||||
assert!(with.windows(2).any(|w| w == [0xFF, 0xFE]));
|
||||
assert_eq!(image::load_from_memory(&with).unwrap().height(), 4);
|
||||
assert_eq!(
|
||||
craft_image_codecs::decode(&with)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.height(),
|
||||
4
|
||||
);
|
||||
assert_eq!(jpeg_comment(vec![1, 2, 3], "x"), vec![1, 2, 3], "not a JPEG");
|
||||
let png = png::with_srgb(png::encode(&[0, 0, 0, 255], 1, 1, &png::PngOptions::default()).unwrap());
|
||||
assert!(png.windows(4).any(|w| w == b"sRGB"));
|
||||
assert_eq!(image::load_from_memory(&png).unwrap().width(), 1);
|
||||
assert_eq!(
|
||||
craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.width(),
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
@@ -192,12 +192,7 @@ pub fn write(img: &Image, l: &Layout) -> Result<Vec<u8>, String> {
|
||||
offsets.push(strips.len());
|
||||
match l.compression {
|
||||
Compression::None => strips.extend_from_slice(strip),
|
||||
Compression::Lzw => {
|
||||
let lzw = weezl::encode::Encoder::with_tiff_size_switch(weezl::BitOrder::Msb, 8)
|
||||
.encode(strip)
|
||||
.map_err(|e| format!("TIFF encoding failed: {e}"))?;
|
||||
strips.extend(lzw);
|
||||
}
|
||||
Compression::Lzw => strips.extend(craft_stream_codecs::lzw::encode(strip, true)),
|
||||
Compression::PackBits => strip.chunks(row).for_each(|r| packbits(r, &mut strips)),
|
||||
}
|
||||
// Strips start on a word boundary.
|
||||
|
||||
@@ -350,11 +350,16 @@ impl Rendered {
|
||||
/// Encode as PNG without metadata (thumbnails, screenshots, embedded rasters; exports use
|
||||
/// [`encode`]).
|
||||
pub fn to_png(&self) -> Result<Vec<u8>, String> {
|
||||
let mut buf = Vec::new();
|
||||
let img = image::RgbaImage::from_raw(self.width, self.height, self.to_straight())
|
||||
.ok_or("PNG encoding failed: pixel buffer doesn't match the image size")?;
|
||||
img.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png).map_err(|e| format!("PNG encoding failed: {e}"))?;
|
||||
Ok(buf)
|
||||
let img = craft_image_codecs::Image::from_raw(
|
||||
self.width,
|
||||
self.height,
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U8,
|
||||
self.to_straight(),
|
||||
)
|
||||
.map_err(|e| format!("PNG encoding failed: {e}"))?;
|
||||
craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map_err(|e| format!("PNG encoding failed: {e}"))
|
||||
}
|
||||
/// Each pixel's grey level and alpha: the Rec. 709 luma of its straight colour, with `bilevel`
|
||||
/// black below 128 and white from there up. A clear pixel is white: no ink where a fill
|
||||
|
||||
+140
-37
@@ -157,30 +157,72 @@ pub fn image_decodes() -> u64 {
|
||||
|
||||
/// [`decode_in_room`]'s image or error.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn decode_within_allowance(bytes: &[u8]) -> image::ImageResult<image::DynamicImage> {
|
||||
pub(crate) fn decode_within_allowance(bytes: &[u8]) -> Result<image::DynamicImage, DecodeError> {
|
||||
decode_in_room(bytes).map_err(|(e, _)| e)
|
||||
}
|
||||
|
||||
/// Why [`decode_in_room`] gave no image.
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum DecodeError {
|
||||
/// The image takes more than the decode allowance, or than the room the images being painted
|
||||
/// leave: it may be read at [`reduced_size`].
|
||||
Limit(String),
|
||||
/// Its pixels can't be read.
|
||||
Unreadable(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for DecodeError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
DecodeError::Limit(why) => write!(f, "the image is too large to decode: {why}"),
|
||||
DecodeError::Unreadable(why) => write!(f, "the image's pixels can't be read: {why}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The formats the renderer draws, told from their bytes: those the image crate read for it
|
||||
/// before craft-image-codecs did (#778). TGA has no signature to tell it by and was never drawn.
|
||||
fn drawn_format(bytes: &[u8]) -> Option<craft_image_codecs::Format> {
|
||||
use craft_image_codecs::Format as F;
|
||||
craft_image_codecs::detect(bytes).filter(|f| matches!(f, F::Png | F::Jpeg | F::Gif | F::WebP | F::Tiff | F::Bmp))
|
||||
}
|
||||
|
||||
/// `bytes` decoded as `format` within `allowance` bytes: the pixels as stored, not turned upright,
|
||||
/// as the image crate gave them.
|
||||
fn decode_as(format: craft_image_codecs::Format, bytes: &[u8], allowance: u64) -> Result<image::DynamicImage, craft_image_codecs::CodecError> {
|
||||
vectorcraft_doc::decode::decode_image(format, bytes, allowance).map(|(img, _)| img)
|
||||
}
|
||||
|
||||
/// `bytes` decoded within [`MAX_DECODE_ALLOC`], counted as RGBA: the image's size is read from its
|
||||
/// header first, and an image of more than that as RGBA gets a limit error before anything is
|
||||
/// decoded (#1717). A failure comes with why the image gave no pixels ([`room_for`]).
|
||||
fn decode_in_room(bytes: &[u8]) -> Result<image::DynamicImage, (image::ImageError, Undecoded)> {
|
||||
fn decode_in_room(bytes: &[u8]) -> Result<image::DynamicImage, (DecodeError, Undecoded)> {
|
||||
let allowance = allowance();
|
||||
let unreadable = |e| (e, Undecoded::Unreadable);
|
||||
let reader = || -> image::ImageResult<_> {
|
||||
let mut r = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format()?;
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_alloc = Some(allowance);
|
||||
r.limits(limits);
|
||||
Ok(r)
|
||||
let unreadable = |e: String| (DecodeError::Unreadable(e), Undecoded::Unreadable);
|
||||
let format = drawn_format(bytes).ok_or_else(|| unreadable("not an image the renderer draws".into()))?;
|
||||
let (w, h) = match vectorcraft_doc::header_size(bytes) {
|
||||
Some(size) => size,
|
||||
None => {
|
||||
return Err(match craft_image_codecs::probe_size(bytes) {
|
||||
Err(craft_image_codecs::CodecError::LimitExceeded(e)) => (DecodeError::Limit(e), Undecoded::Unreadable),
|
||||
Err(e) => unreadable(e.to_string()),
|
||||
Ok(_) => unreadable("the image's size can't be read".into()),
|
||||
});
|
||||
}
|
||||
};
|
||||
let (w, h) = reader().and_then(|r| r.into_dimensions()).map_err(unreadable)?;
|
||||
let bytes = (u64::from(w) * u64::from(h)).saturating_mul(4);
|
||||
let rgba = (u64::from(w) * u64::from(h)).saturating_mul(4);
|
||||
let pinned = crate::images::store().pinned_bytes() as u64;
|
||||
if let Err(why) = room_for(bytes, pinned) {
|
||||
return Err((image::ImageError::Limits(image::error::LimitError::from_kind(image::error::LimitErrorKind::InsufficientMemory)), why));
|
||||
if let Err(why) = room_for(rgba, pinned) {
|
||||
return Err((DecodeError::Limit(format!("{w} × {h} pixels take {rgba} bytes as RGBA")), why));
|
||||
}
|
||||
reader().and_then(|r| r.decode()).map_err(unreadable)
|
||||
// Within the allowance as RGBA and as decoded, a limit the decoder meets is a file that can't
|
||||
// hold the pixels its header declares: they can't be read.
|
||||
let samples_fit = vectorcraft_doc::decode::decoded_layout(bytes, format)
|
||||
.is_some_and(|(_, _, per_pixel)| (u64::from(w) * u64::from(h)).saturating_mul(per_pixel) <= allowance);
|
||||
decode_as(format, bytes, allowance).map_err(|e| match e {
|
||||
craft_image_codecs::CodecError::LimitExceeded(m) if !samples_fit => (DecodeError::Limit(m), Undecoded::Unreadable),
|
||||
e => unreadable(e.to_string()),
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether an image of `rgba` bytes may be decoded while the images being painted hold `pinned`
|
||||
@@ -240,40 +282,33 @@ struct Reduction {
|
||||
|
||||
/// [`reduced_size`]'s reading of `bytes` within `allowance`.
|
||||
fn reduction(bytes: &[u8], allowance: u64) -> Option<Reduction> {
|
||||
use image::ImageDecoder as _;
|
||||
let mut reader = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().ok()?;
|
||||
let format = reader.format()?;
|
||||
if !matches!(format, image::ImageFormat::Png | image::ImageFormat::Jpeg | image::ImageFormat::Gif | image::ImageFormat::WebP) {
|
||||
return None;
|
||||
}
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_alloc = Some(allowance);
|
||||
reader.limits(limits);
|
||||
let d = reader.into_decoder().ok()?;
|
||||
let (w, h) = d.dimensions();
|
||||
use craft_image_codecs::Format as F;
|
||||
let format = drawn_format(bytes).filter(|f| matches!(f, F::Png | F::Jpeg | F::Gif | F::WebP))?;
|
||||
let (w, h, per_pixel) = vectorcraft_doc::decode::decoded_layout(bytes, format)?;
|
||||
let pixels = u64::from(w) * u64::from(h);
|
||||
let total_bytes = pixels.saturating_mul(per_pixel);
|
||||
// An image of more pixels than the allowance has bytes (four times what it holds as RGBA) is
|
||||
// not read.
|
||||
if pixels == 0 || pixels > allowance || (pixels.saturating_mul(4) <= allowance && d.total_bytes() <= allowance) {
|
||||
if pixels == 0 || pixels > allowance || (pixels.saturating_mul(4) <= allowance && total_bytes <= allowance) {
|
||||
return None;
|
||||
}
|
||||
let to = fitted_dims(w, h, allowance)?;
|
||||
// A PNG that isn't interlaced or animated (and upright in pre-pixel EXIF) is read a row at a time when the reader's buffers fit the allowance.
|
||||
let row_scratch = if format == image::ImageFormat::Png {
|
||||
let row_scratch = if format == F::Png {
|
||||
let streamed = craft_image_codecs::PngDecodeRows::new_streaming(bytes, &png_streaming_limits(allowance))
|
||||
.ok()
|
||||
.filter(|dec| dec.streamable())
|
||||
.and_then(|dec| png_row_scratch(&dec.header()));
|
||||
streamed.or_else(|| png_row_scratch_from_header(bytes, w, h, d.total_bytes())).filter(|&s| s <= allowance)
|
||||
streamed.or_else(|| png_row_scratch_from_header(bytes, w, h, total_bytes)).filter(|&s| s <= allowance)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let rows = row_scratch.is_some();
|
||||
let scratch = row_scratch.unwrap_or_else(|| {
|
||||
let band = u64::from(BAND) * u64::from(w) * (u64::from(d.color_type().bytes_per_pixel()) + 4);
|
||||
d.total_bytes().saturating_add(band)
|
||||
let band = u64::from(BAND) * u64::from(w) * (per_pixel + 4);
|
||||
total_bytes.saturating_add(band)
|
||||
});
|
||||
(rows || d.total_bytes() <= allowance).then_some(Reduction { size: (w, h), to, rows, scratch })
|
||||
(rows || total_bytes <= allowance).then_some(Reduction { size: (w, h), to, rows, scratch })
|
||||
}
|
||||
|
||||
/// Streaming retains rows, not the declared full image. Preserve the renderer's source-pixel
|
||||
@@ -439,11 +474,7 @@ fn shrunk_with_png_limits(bytes: &[u8], r: &Reduction, png_limits: &craft_image_
|
||||
.ok()?;
|
||||
}
|
||||
} else {
|
||||
let mut reader = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().ok()?;
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_alloc = Some(allowance());
|
||||
reader.limits(limits);
|
||||
let img = reader.decode().ok()?;
|
||||
let img = decode_as(drawn_format(bytes)?, bytes, allowance()).ok()?;
|
||||
let (w, h) = (img.width(), img.height());
|
||||
for y in (0..h).step_by(BAND as usize) {
|
||||
let band = img.crop_imm(0, y, w, BAND.min(h - y)).to_rgba8();
|
||||
@@ -563,7 +594,7 @@ fn decode_or_reduction(bytes: &[u8]) -> Result<image::DynamicImage, Result<Reduc
|
||||
DECODES.with(|n| n.set(n.get() + 1));
|
||||
match decode_in_room(bytes) {
|
||||
Ok(img) => Ok(img),
|
||||
Err((image::ImageError::Limits(_), Undecoded::Unreadable)) => Err(reduction(bytes, allowance()).ok_or(Undecoded::Unreadable)),
|
||||
Err((DecodeError::Limit(_), Undecoded::Unreadable)) => Err(reduction(bytes, allowance()).ok_or(Undecoded::Unreadable)),
|
||||
Err((_, why)) => Err(Err(why)),
|
||||
}
|
||||
}
|
||||
@@ -730,6 +761,78 @@ mod tests {
|
||||
assert!(red_at_its_coverage(&reduced));
|
||||
}
|
||||
|
||||
/// The renderer's decoding through craft-image-codecs gives the pixels the image crate gave it
|
||||
/// (#778): the same layout and samples for PNGs of each color type and depth, GIF, WebP, TIFF and
|
||||
/// BMP, and JPEG samples within two steps (the two decoders' IDCTs round differently). The EXIF
|
||||
/// orientation is left to the caller, as before.
|
||||
#[test]
|
||||
fn decoded_pixels_are_those_the_image_crate_gave() {
|
||||
use image::{DynamicImage as D, ImageFormat as F};
|
||||
let rgba = image::RgbaImage::from_fn(13, 7, |x, y| image::Rgba([(x * 19) as u8, (y * 37) as u8, ((x + y) * 11) as u8, (x * y * 5) as u8]));
|
||||
let rgba16 = image::ImageBuffer::<image::Rgba<u16>, _>::from_fn(13, 7, |x, y| {
|
||||
image::Rgba([(x * 4_999) as u16, (y * 9_001) as u16, 300, 65_535 - (x * y) as u16])
|
||||
});
|
||||
let images = [
|
||||
D::ImageRgba8(rgba.clone()),
|
||||
D::ImageRgb8(D::ImageRgba8(rgba.clone()).to_rgb8()),
|
||||
D::ImageLuma8(D::ImageRgba8(rgba.clone()).to_luma8()),
|
||||
D::ImageLumaA8(D::ImageRgba8(rgba.clone()).to_luma_alpha8()),
|
||||
D::ImageRgba16(rgba16.clone()),
|
||||
D::ImageRgb16(D::ImageRgba16(rgba16.clone()).to_rgb16()),
|
||||
D::ImageLuma16(D::ImageRgba16(rgba16.clone()).to_luma16()),
|
||||
D::ImageLumaA16(D::ImageRgba16(rgba16).to_luma_alpha16()),
|
||||
];
|
||||
let encoded = |img: &D, f: F| {
|
||||
let mut out = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), f).ok().map(|()| out)
|
||||
};
|
||||
let mut files: Vec<(String, Vec<u8>)> = Vec::new();
|
||||
for img in &images {
|
||||
for f in [F::Png, F::Tiff, F::Bmp, F::Gif, F::WebP, F::Jpeg] {
|
||||
if let Some(bytes) = encoded(img, f) {
|
||||
files.push((format!("{f:?} {:?}", img.color()), bytes));
|
||||
}
|
||||
}
|
||||
}
|
||||
// A palette PNG with a transparent entry and a 1-bit gray PNG, as other writers make them.
|
||||
let png = |w: u32, h: u32, depth: u8, color: u8, extra: &[&[u8]], rows: &[u8]| {
|
||||
let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default());
|
||||
std::io::Write::write_all(&mut z, rows).unwrap();
|
||||
let idat = [&b"IDAT"[..], &z.finish().unwrap()].concat();
|
||||
let ihdr = [&b"IHDR"[..], &w.to_be_bytes(), &h.to_be_bytes(), &[depth, color, 0, 0, 0]].concat();
|
||||
let mut out = b"\x89PNG\r\n\x1a\n".to_vec();
|
||||
for chunk in [&ihdr[..]].into_iter().chain(extra.iter().copied()).chain([&idat[..], b"IEND"]) {
|
||||
out.extend((chunk.len() as u32 - 4).to_be_bytes());
|
||||
out.extend(chunk);
|
||||
let mut crc = flate2::Crc::new();
|
||||
crc.update(chunk);
|
||||
out.extend(crc.sum().to_be_bytes());
|
||||
}
|
||||
out
|
||||
};
|
||||
let plte: &[u8] = b"PLTE\xff\0\0\0\xff\0\0\0\xff";
|
||||
files.push(("indexed PNG".into(), png(4, 2, 8, 3, &[plte, b"tRNS\xff\0"], &[0, 0, 1, 2, 1, 0, 2, 1, 0, 0])));
|
||||
files.push(("indexed PNG, opaque".into(), png(4, 2, 8, 3, &[plte], &[0, 0, 1, 2, 1, 0, 2, 1, 0, 0])));
|
||||
files.push(("1-bit PNG".into(), png(10, 2, 1, 0, &[], &[0, 0b1010_1100, 0b0100_0000, 0, 0b0011_1111, 0b1100_0000])));
|
||||
assert!(files.len() > 20, "{}", files.len());
|
||||
for (what, bytes) in &files {
|
||||
let want = image::load_from_memory(bytes).unwrap();
|
||||
let got = decode_in_room(bytes).map_err(|(e, _)| e).unwrap();
|
||||
let jpeg = what.starts_with("Jpeg");
|
||||
if !jpeg {
|
||||
assert_eq!(got.color(), want.color(), "{what}");
|
||||
}
|
||||
let (g, w) = (got.to_rgba16(), want.to_rgba16());
|
||||
assert_eq!(g.dimensions(), w.dimensions(), "{what}");
|
||||
let tolerance = if jpeg { 2 * 257 } else { 0 };
|
||||
let worst = g.as_raw().iter().zip(w.as_raw()).map(|(a, b)| a.abs_diff(*b)).max().unwrap_or(0);
|
||||
assert!(worst <= tolerance, "{what}: samples {worst} apart");
|
||||
// Read past the allowance at reduced size, the same image is read whole first.
|
||||
let whole = shrunk(bytes, &Reduction { size: w.dimensions(), to: w.dimensions(), rows: false, scratch: 0 }).unwrap();
|
||||
assert_eq!((u32::from(whole.width()), u32::from(whole.height())), w.dimensions(), "{what}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Opaque red beside clear black, every other column of a `w` × 1 image.
|
||||
fn striped(w: u32) -> image::RgbaImage {
|
||||
image::RgbaImage::from_fn(w, 1, |x, _| if x % 2 == 0 { image::Rgba([200, 30, 30, 255]) } else { image::Rgba([0, 0, 0, 0]) })
|
||||
|
||||
@@ -1686,16 +1686,18 @@ fn tiff_header(w: u32, h: u32) -> Vec<u8> {
|
||||
#[test]
|
||||
fn an_image_too_large_as_rgba_is_not_decoded() {
|
||||
let over = paint::decode_within_allowance(&png_header(23_171, 23_170));
|
||||
assert!(matches!(over, Err(image::ImageError::Limits(_))), "{over:?}");
|
||||
assert!(matches!(over, Err(paint::DecodeError::Limit(_))), "{over:?}");
|
||||
assert!(over.unwrap_err().to_string().starts_with("the image is too large to decode: "));
|
||||
let failed = decode_failures();
|
||||
assert!(paint::decode_pixmap(&png_header(23_171, 23_170)).is_none());
|
||||
assert!(decode_failures() > failed);
|
||||
// Within the allowance: decoded, and its missing rows can't be read.
|
||||
let within = paint::decode_within_allowance(&png_header(23_170, 23_170));
|
||||
assert!(matches!(within, Err(image::ImageError::Decoding(_))), "{within:?}");
|
||||
assert!(matches!(within, Err(paint::DecodeError::Unreadable(_))), "{within:?}");
|
||||
assert!(within.unwrap_err().to_string().starts_with("the image's pixels can't be read: "));
|
||||
// A header whose size as RGBA does not fit in 64 bits gets the same limit error.
|
||||
let huge = paint::decode_within_allowance(&tiff_header(1 << 31, 1 << 31));
|
||||
assert!(matches!(huge, Err(image::ImageError::Limits(_))), "{huge:?}");
|
||||
assert!(matches!(huge, Err(paint::DecodeError::Limit(_))), "{huge:?}");
|
||||
}
|
||||
|
||||
/// A PNG of `w` × `h` noisy pixels cut off halfway through its bytes: its header reads, and its
|
||||
|
||||
@@ -47,7 +47,41 @@ fn red_square(x: f64, y: f64) -> Node {
|
||||
fn red_image() -> (String, ImageBlob) {
|
||||
let img = image::RgbaImage::from_pixel(30, 30, image::Rgba([255, 0, 0, 255]));
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
("red".into(), ImageBlob::new("image/png", png))
|
||||
}
|
||||
|
||||
|
||||
@@ -547,7 +547,41 @@ fn traced(view: &str) -> (Document, NodeId, NodeId, NodeId) {
|
||||
let mut d = Document::new(W as f64, H as f64);
|
||||
let mut png = vec![];
|
||||
let red = image::RgbaImage::from_pixel(1, 1, image::Rgba([255, 0, 0, 255]));
|
||||
red.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = red.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
d.images.insert("red".into(), vectorcraft_doc::ImageBlob::new("image/png", png));
|
||||
let source = red_image(&mut d, 10.0);
|
||||
let a = rect(&mut d, Rect::new(60.0, 10.0, 70.0, 20.0), Color::rgb(0.0, 0.5, 0.0));
|
||||
|
||||
@@ -211,7 +211,41 @@ fn placed_images_are_separated_not_blackened() {
|
||||
let mut d = doc(ColorMode::Cmyk, None, CYAN, Blend::Normal, 0.5);
|
||||
let img = image::RgbaImage::from_pixel(4, 4, image::Rgba([255, 0, 0, 255]));
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
d.images.insert("red".into(), ImageBlob::new("image/png", png));
|
||||
let n = Node::new(
|
||||
d.alloc_id(),
|
||||
@@ -255,7 +289,41 @@ fn rich_black_shows_black_ink_alone_as_rgb_black() {
|
||||
let mut d = Document::new_with_mode(20.0, 20.0, ColorMode::Cmyk);
|
||||
let img = image::RgbaImage::from_pixel(20, 20, image::Rgba([255, 0, 0, 255]));
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
d.images.insert("red".into(), ImageBlob::new("image/png", png));
|
||||
let im = ImageObject { key: "red".into(), width: 20, height: 20, xf: Affine::IDENTITY, link: None, placement: Default::default(), pixels: None };
|
||||
let n = Node::new(d.alloc_id(), NodeKind::Image(Box::new(im)));
|
||||
|
||||
@@ -28,7 +28,41 @@ fn bilevel_tiff() -> Vec<u8> {
|
||||
fn gray_png() -> Vec<u8> {
|
||||
let img = image::GrayImage::from_raw(2, 1, vec![0, 255]).unwrap();
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
png
|
||||
}
|
||||
|
||||
@@ -81,7 +115,41 @@ fn gray_and_1_bit_images_draw_in_their_spot_color_with_white_transparent() {
|
||||
fn shrunk_copies_are_kept_per_color() {
|
||||
let black = image::GrayImage::from_raw(64, 64, vec![0; 64 * 64]).unwrap();
|
||||
let mut png = vec![];
|
||||
black.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = black.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let blob = ImageBlob::new("image/png", png);
|
||||
let d = doc_of(blob.clone(), Some(Paint::solid(Color::rgb(0.0, 0.0, 1.0))), (64, 64), 1.0);
|
||||
let plain = doc_of(blob, None, (64, 64), 1.0);
|
||||
@@ -102,7 +170,41 @@ fn shrunk_copies_are_kept_per_color() {
|
||||
fn kept_images_only_draw_the_colorized_pixels() {
|
||||
let black = image::GrayImage::from_raw(48, 48, vec![0; 48 * 48]).unwrap();
|
||||
let mut png = vec![];
|
||||
black.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = black.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let d = doc_of(ImageBlob::new("image/png", png), Some(Paint::solid(Color::rgb(0.0, 0.0, 1.0))), (48, 48), 1.0);
|
||||
let view = Affine::scale(0.25);
|
||||
let mut r = Renderer::new();
|
||||
@@ -135,7 +237,41 @@ fn ink_planes_keep_the_colors_own_inks() {
|
||||
fn colorize_dims_with_the_grays() {
|
||||
let gray = image::GrayImage::from_raw(2, 1, vec![128, 255]).unwrap();
|
||||
let mut png = vec![];
|
||||
gray.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = gray.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let d = doc(ImageBlob::new("image/png", png), Some(Paint::solid(Color::rgb(0.0, 0.0, 1.0))));
|
||||
let px = Renderer::new().render(&d, 40, 20, Affine::IDENTITY, &RenderOptions { smooth_images: false, ..Default::default() }).pixel(10, 10);
|
||||
// Half blue over the green band.
|
||||
@@ -346,7 +482,41 @@ fn image_and_fills(mode: vectorcraft_doc::ColorMode, blob: ImageBlob, (width, he
|
||||
fn rgb_png(width: u32, height: u32, pixels: &[[u8; 3]]) -> Vec<u8> {
|
||||
let img = image::RgbImage::from_raw(width, height, pixels.concat()).unwrap();
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
png
|
||||
}
|
||||
|
||||
|
||||
@@ -155,7 +155,41 @@ fn group_geometry_effects_move_the_members_as_one_piece() {
|
||||
fn group_geometry_effects_move_and_copy_images_too() {
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_pixel(4, 4, image::Rgba([255, 0, 0, 255])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(4, 4, image::Rgba([255, 0, 0, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
d.images.insert("red".into(), ImageBlob::new("image/png", png));
|
||||
let b = rect(&mut d, Rect::new(0.0, 0.0, 20.0, 20.0), solid(0.0, 0.0, 1.0));
|
||||
let im = ImageObject {
|
||||
|
||||
@@ -12,7 +12,41 @@ fn doc(layer: impl FnOnce(&mut NodeKind)) -> Document {
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
let img = image::RgbaImage::from_pixel(30, 30, image::Rgba([255, 0, 0, 255]));
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
d.images.insert("red".into(), ImageBlob::new("image/png", png));
|
||||
let l = d.layers[0].id;
|
||||
let red = Appearance::basic(Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||||
|
||||
@@ -18,7 +18,41 @@ fn png(w: u32, h: u32, seed: u8) -> Vec<u8> {
|
||||
image::Rgba([v ^ seed, v.wrapping_mul(3), seed, 255])
|
||||
});
|
||||
let mut out = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -2536,7 +2570,41 @@ fn a_reduced_image_on_the_ink_planes_shows_its_average() {
|
||||
let _fonts = fonts_steady();
|
||||
let lines = image::RgbaImage::from_fn(64, 64, |x, _| image::Rgba(if x % 2 == 0 { [0, 0, 0, 255] } else { [255; 4] }));
|
||||
let mut bytes = vec![];
|
||||
lines.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = lines.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| bytes.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let mut d = cmyk_doc_with("lines", ImageBlob::new("image/png", bytes), 64, 64);
|
||||
// Transparency beside the image: the document blends in CMYK, drawing on its ink planes.
|
||||
let shape = vectorcraft_geom::shapes::rectangle(Rect::new(100.0, 0.0, 110.0, 10.0));
|
||||
@@ -2560,7 +2628,41 @@ fn a_reduced_image_on_the_ink_planes_shows_its_average() {
|
||||
fn varied_png(w: u32, h: u32, seed: u8) -> Vec<u8> {
|
||||
let img = image::RgbaImage::from_fn(w, h, |x, y| image::Rgba([(x * 5 + y) as u8, (x / 3 + y * 7) as u8, (x ^ y) as u8 ^ seed, 255]));
|
||||
let mut out = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,41 @@ use vectorcraft_geom::Point;
|
||||
fn png(alpha: u8) -> Vec<u8> {
|
||||
let img = image::RgbaImage::from_pixel(4, 4, image::Rgba([255, 0, 0, alpha]));
|
||||
let mut out = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
|
||||
@@ -306,7 +306,41 @@ fn image_doc(effects: Vec<Effect>) -> Document {
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
let img = image::RgbaImage::from_pixel(30, 30, image::Rgba([255, 0, 0, 255]));
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
d.images.insert("red".into(), ImageBlob::new("image/png", png));
|
||||
let mut n = Node::new(
|
||||
NodeId(0),
|
||||
@@ -786,7 +820,41 @@ fn an_images_outline_object_stroke_follows_new_pixels_under_the_same_key() {
|
||||
image::Rgba([255, 0, 0, if r <= 8.0 { 255 } else { 0 }])
|
||||
});
|
||||
let mut png = vec![];
|
||||
disc.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = disc.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
d.images.insert("red".into(), ImageBlob::new("image/png", png));
|
||||
let img = r.render(&d, 100, 100, Affine::IDENTITY, &opts);
|
||||
assert_eq!((lum(img.pixel(19, 35)), lum(img.pixel(26, 35))), (WHITE, 0));
|
||||
|
||||
@@ -374,7 +374,41 @@ fn a_placed_document_kept_as_its_preview_draws_the_preview() {
|
||||
// Only the preview (a 20×10 green PNG) is there, and the file can't be read here.
|
||||
let img = image::RgbaImage::from_pixel(20, 10, image::Rgba([0, 200, 0, 255]));
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let mut blob = ImageBlob::new("image/png", png);
|
||||
blob.proxy = Some(blob.bytes.clone());
|
||||
d.images.insert("preview-1".into(), blob);
|
||||
|
||||
@@ -7,7 +7,41 @@ use crate::*;
|
||||
fn doc_with_red_image() -> Document {
|
||||
let mut d = Document::new(40.0, 40.0);
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_pixel(4, 4, image::Rgba([255, 0, 0, 255])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(4, 4, image::Rgba([255, 0, 0, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
d.images.insert("red".into(), ImageBlob::new("image/png", png));
|
||||
let (id, layer) = (d.alloc_id(), d.layers[0].id);
|
||||
let im = ImageObject { key: "red".into(), width: 40, height: 40, xf: Affine::IDENTITY, link: None, placement: Default::default(), pixels: None };
|
||||
|
||||
@@ -115,7 +115,41 @@ fn rasters_cropped_to_the_view_follow_a_pan_and_a_zoom() {
|
||||
fn large_image(effects: &[(&str, serde_json::Value)]) -> Document {
|
||||
let img = image::RgbaImage::from_fn(300, 200, |x, y| image::Rgba([(x * 7) as u8, (y * 13) as u8, ((x + y) * 3) as u8, 255]));
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let mut d = Document::new(6000.0, 4000.0);
|
||||
d.images.insert("viewraster-pan".into(), ImageBlob::new("image/png", png));
|
||||
let im = ImageObject {
|
||||
|
||||
Reference in New Issue
Block a user