Add MemoizeImpure node and cache image base64 in graph (#1595)

* Cache base64 representation of images when converting to graphic group

* Fix build

* Fix build again

* Actually fix it this time

---------

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Dennis Kobert 2024-02-03 23:06:17 +01:00
parent 8fa46ba63a
commit 349ec5da72
No known key found for this signature in database
GPG key ID: 5C4243878B881A5C
12 changed files with 127 additions and 13 deletions

View file

@ -88,6 +88,7 @@ async fn map_gpu<'a: 'input>(image: ImageFrame<Color>, node: DocumentNode, edito
data: image.image.data.iter().map(|c| quantization::quantize_color(*c, quantization)).collect(),
width: image.image.width,
height: image.image.height,
base64_string: None,
},
transform: image.transform,
alpha_blending: image.alpha_blending,
@ -140,6 +141,7 @@ async fn map_gpu<'a: 'input>(image: ImageFrame<Color>, node: DocumentNode, edito
data: colors,
width: image.image.width,
height: image.image.height,
..Default::default()
},
transform: image.transform,
alpha_blending: image.alpha_blending,
@ -586,6 +588,7 @@ async fn blend_gpu_image(foreground: ImageFrame<Color>, background: ImageFrame<C
data: colors,
width: background.image.width,
height: background.image.height,
..Default::default()
},
transform: background.transform,
alpha_blending: background.alpha_blending,

View file

@ -74,6 +74,7 @@ mod test {
width: 100,
height: 100,
data: vec![Color::from_rgbaf32(0.0, 0.0, 0.0, 1.0).unwrap(); 10000],
base64_string: None,
},
..Default::default()
}),

View file

@ -446,7 +446,7 @@ async fn imaginate_maybe_fail<'a, P: Pixel, F: Fn(ImaginateStatus)>(
fn image_to_base64<P: Pixel>(image: Image<P>) -> Result<String, Error> {
use base64::prelude::*;
let Image { width, height, data } = image;
let Image { width, height, data, .. } = image;
fn cast_with_f32<S: Pixel, D: image::Pixel<Subpixel = f32>>(data: Vec<S>, width: u32, height: u32) -> Result<DynamicImage, Error>
where
@ -485,7 +485,12 @@ fn base64_to_image<D: AsRef<[u8]>, P: Pixel>(base64_data: D) -> Result<Image<P>,
_ => return Err(Error::UnsupportedPixelType(core::any::type_name::<P>())),
};
Ok(Image { data: result_data, width, height })
Ok(Image {
data: result_data,
width,
height,
base64_string: None,
})
}
pub fn pick_safe_imaginate_resolution((width, height): (f64, f64)) -> (u64, u64) {

View file

@ -111,6 +111,7 @@ fn sample(footprint: Footprint, image_frame: ImageFrame<Color>) -> ImageFrame<Co
width: new_width,
height: new_height,
data: vec,
base64_string: None,
};
// we need to adjust the offset if we truncate the offset calculation
@ -750,7 +751,12 @@ fn mandelbrot_node(footprint: Footprint) -> ImageFrame<Color> {
}
}
ImageFrame {
image: Image { width, height, data },
image: Image {
width,
height,
data,
..Default::default()
},
transform: DAffine2::from_translation(offset) * DAffine2::from_scale(size),
..Default::default()
}

View file

@ -277,6 +277,7 @@ fn decode_image_node<'a: 'input>(data: Arc<[u8]>) -> ImageFrame<Color> {
data: image.chunks(4).map(|pixel| Color::from_unassociated_alpha(pixel[0], pixel[1], pixel[2], pixel[3])).collect(),
width: image.width(),
height: image.height(),
..Default::default()
},
..Default::default()
};