initial impl

This commit is contained in:
Timofey Gelazoniya 2023-10-17 20:12:28 +03:00
parent 9891ddfa5b
commit d9d416a0fe
Signed by: zeldon
GPG Key ID: 047886915281DD2A
2 changed files with 54 additions and 2 deletions

View File

@ -7,6 +7,7 @@ edition = "2021"
crate-type = ["cdylib"]
[dependencies]
magick_rust = "0.19.1"
napi = "2.13.3"
napi-derive = "2.13.0"

View File

@ -1,8 +1,59 @@
#![deny(clippy::all)]
use magick_rust::{magick_wand_genesis, MagickWand};
use napi::{
bindgen_prelude::{AsyncTask, Buffer},
Task,
};
use napi_derive::napi;
use std::sync::Once;
static START: Once = Once::new();
struct LiquidRescale {
input: Buffer,
}
#[napi]
pub fn plus_100(input: u32) -> u32 {
input + 100
impl Task for LiquidRescale {
type Output = Buffer;
type JsValue = Buffer;
fn compute(&mut self) -> napi::Result<Self::Output> {
let buffer: Vec<u8> = self.input.clone().into();
let result = lib_liquid_rescale(&buffer, 200, 400).unwrap();
Ok(result.into())
}
fn resolve(&mut self, _env: napi::Env, output: Self::Output) -> napi::Result<Self::JsValue> {
Ok(output)
}
}
#[napi(js_name = "liquidRescaleImage")]
fn liquid_rescale(input_image: Buffer) -> AsyncTask<LiquidRescale> {
AsyncTask::new(LiquidRescale { input: input_image })
}
fn lib_liquid_rescale(input_image: &[u8], width: usize, height: usize) -> Result<Vec<u8>, String> {
START.call_once(|| {
magick_wand_genesis();
});
let wand = MagickWand::new();
if let Err(err) = wand.read_image_blob(input_image) {
return Err(format!("Failed to read image: {}", err));
}
if let Err(err) = wand.liquid_rescale_image(width, height, 1.0, 0.0) {
return Err(format!("Failed to perform liquid rescaling: {}", err));
}
let output_image = match wand.write_image_blob("JPEG") {
Ok(image) => image,
Err(err) => return Err(format!("Failed to write image: {}", err)),
};
Ok(output_image)
}