From a7f1895f9678600a798b53fe7be2c9f07346c9cb Mon Sep 17 00:00:00 2001 From: MeloAlright Date: Sat, 20 Apr 2024 23:04:25 +0800 Subject: [PATCH 1/4] Support :blur :grayscale :brighten :invert in v-image (#29) --- soft-skia-wasm/src/lib.rs | 12 +++++++++ soft-skia/Cargo.toml | 1 + soft-skia/src/shape.rs | 31 +++++++++++++++++++++++- vue-playground/src/App.vue | 11 +++++++++ vue-playground/src/code.ts | 11 +++++++++ vue-skia-framework/components/VImage.vue | 16 ++++++++++++ vue-skia-framework/plugin/index.ts | 4 +++ 7 files changed, 85 insertions(+), 1 deletion(-) diff --git a/soft-skia-wasm/src/lib.rs b/soft-skia-wasm/src/lib.rs index 59c6213c..803b7afe 100644 --- a/soft-skia-wasm/src/lib.rs +++ b/soft-skia-wasm/src/lib.rs @@ -92,6 +92,10 @@ pub struct WASMImageAttr { y: i32, width: u32, height: u32, + blur: Option, + grayscale: Option, + brighten: Option, + invert: Option, } #[derive(Serialize, Deserialize, Debug)] @@ -385,6 +389,10 @@ impl SoftSkiaWASM { image, width, height, + blur, + grayscale, + brighten, + invert, }) => self.0.set_shape_to_child( id, Shapes::I(Image { @@ -393,6 +401,10 @@ impl SoftSkiaWASM { y, width, height, + blur, + grayscale, + brighten, + invert, }), ), WASMShapesAttr::T(WASMTextAttr { diff --git a/soft-skia/Cargo.toml b/soft-skia/Cargo.toml index 4bd3a250..cd0a3e25 100644 --- a/soft-skia/Cargo.toml +++ b/soft-skia/Cargo.toml @@ -12,6 +12,7 @@ png = "0.17.5" tiny-skia = "0.10.0" base64 = "0.21.0" fontdue = "0.7.3" +image = "0.25.1" [dependencies.web-sys] version = "0.3" diff --git a/soft-skia/src/shape.rs b/soft-skia/src/shape.rs index c090842b..b6abe9d2 100644 --- a/soft-skia/src/shape.rs +++ b/soft-skia/src/shape.rs @@ -7,6 +7,8 @@ use fontdue::{ use std::iter::zip; pub use tiny_skia::{ColorU8, FillRule, Mask, Paint, PathBuilder, Pixmap, Stroke, Transform}; use tiny_skia::{LineCap, LineJoin, Path, PixmapPaint}; +use image::io::Reader as ImageReader; +use std::io::Cursor; #[derive(Debug)] pub enum Shapes { @@ -112,6 +114,10 @@ pub struct Image { pub y: i32, pub width: u32, pub height: u32, + pub blur: Option, + pub grayscale: Option, + pub brighten: Option, + pub invert: Option, } #[derive(Debug)] @@ -509,7 +515,30 @@ impl Shape for Image { fn draw(&self, pixmap: &mut Pixmap, context: &DrawContext) -> () { let u8_array = base64::decode(&self.image).expect("base64 decode failed"); - let p = Pixmap::decode_png(&u8_array).expect("decode png failed"); + let mut bytes: Vec = Vec::new(); + + let mut img = ImageReader::new(Cursor::new(&u8_array as &[u8])).with_guessed_format().unwrap().decode().unwrap(); + + if let Some(blur) = self.blur { + img = img.blur(blur); + } + if let Some(grayscale) = self.grayscale { + if grayscale { + img = img.grayscale(); + } + } + if let Some(brighten) = self.brighten { + img = img.brighten(brighten); + } + if let Some(invert) = self.invert { + if invert { + img.invert(); + } + } + + img.write_to(&mut Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap(); + + let p = Pixmap::decode_png(&bytes).expect("decode png failed"); let scale_x = self.width as f32 / p.width() as f32; let scale_y = self.height as f32 / p.height() as f32; pixmap.draw_pixmap( diff --git a/vue-playground/src/App.vue b/vue-playground/src/App.vue index 9fe3f5c3..4816f666 100644 --- a/vue-playground/src/App.vue +++ b/vue-playground/src/App.vue @@ -73,6 +73,17 @@ :style="'fill'" > + [98, 90], [138, 10], ]" :style="'fill'" :strokeWidth="1" :color="'rgba(200, 255, 0, 0.7)'" /> + , required: true, + }, + blur: { + type: Number as PropType, + required: false, + }, + grayscale: { + type: Boolean as PropType, + required: false, + }, + brighten: { + type: Number as PropType, + required: false, + }, + invert: { + type: Boolean as PropType, + required: false, } }, methods: { diff --git a/vue-skia-framework/plugin/index.ts b/vue-skia-framework/plugin/index.ts index 5b891f78..6113218c 100644 --- a/vue-skia-framework/plugin/index.ts +++ b/vue-skia-framework/plugin/index.ts @@ -170,6 +170,10 @@ const VSKNode = (name: string) => { y: attrs.y, width: attrs.width, height: attrs.height, + blur: attrs.blur, + grayscale: attrs.grayscale, + brighten: attrs.brighten, + invert: attrs.invert, }, }, }); From 79f483b1d826061a93cbacc5962bc9c7f66bb14c Mon Sep 17 00:00:00 2001 From: meloalright Date: Sun, 21 Apr 2024 01:57:45 +0800 Subject: [PATCH 2/4] fix: v-image props into v-sk-image --- vue-skia-framework/components/VImage.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vue-skia-framework/components/VImage.vue b/vue-skia-framework/components/VImage.vue index 0cabe962..56ad672e 100644 --- a/vue-skia-framework/components/VImage.vue +++ b/vue-skia-framework/components/VImage.vue @@ -1,5 +1,5 @@