From f1fbf97afa1227530c8282b9de9a2001a173e75d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E4=B8=9D?= Date: Fri, 24 Jun 2022 13:56:44 +0800 Subject: [PATCH] feat: add .pixel() API --- example/index.js | 5 ++++- js-binding.d.ts | 2 ++ src/lib.rs | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/example/index.js b/example/index.js index 9a41ba0f..16ec0fc8 100644 --- a/example/index.js +++ b/example/index.js @@ -28,7 +28,10 @@ async function main() { const pngBuffer = pngData.asPng() console.info('Original SVG Size:', `${resvg.width} x ${resvg.height}`) - console.info('Output PNG Size :', `${pngData.width} x ${pngData.height}`) + // console.info('Output PNG Size :', `${pngData.width} x ${pngData.height}`) + // console.info('pixmap :', `${pngData.pixel}`) + + console.info(typeof pngData.pixel) console.info('✨ Done in', performance.now() - t, 'ms') await promises.writeFile(join(__dirname, './text-out.png'), pngBuffer) diff --git a/js-binding.d.ts b/js-binding.d.ts index 1e21cb98..21aac1e5 100644 --- a/js-binding.d.ts +++ b/js-binding.d.ts @@ -43,6 +43,8 @@ export class Resvg { export class RenderedImage { /** Write the image data to Buffer */ asPng(): Buffer + /** Get Pixmap */ + get pixel(): Buffer /** Get the PNG width */ get width(): number /** Get the PNG height */ diff --git a/src/lib.rs b/src/lib.rs index 205b1d6b..8490fe53 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,6 +107,20 @@ impl RenderedImage { Ok(buffer.as_slice().into()) } + /// Get pixel data + #[cfg(target_arch = "wasm32")] + pub fn pixel(&self) -> js_sys::Uint8Array { + self.pix.data().into() + } + + /// Get pixel data + #[cfg(not(target_arch = "wasm32"))] + #[napi(getter)] + pub fn pixel(&self) -> Result { + let buffer = self.pix.data(); + Ok(buffer.into()) + } + #[cfg(not(target_arch = "wasm32"))] #[napi(getter)] /// Get the PNG width