wasmcv

Crates.iowasmcv
lib.rswasmcv
version0.6.0
sourcesrc
created_at2024-09-16 09:34:21.506689
updated_at2024-10-16 18:43:37.668976
descriptionRust bindings for wasmCV computer vision interfaces based on WebAssembly.
homepage
repositoryhttps://github.com/wasmvision/wasmcv
max_upload_size
id1376168
size166,435
Ron Evans (deadprogram)

documentation

README

wasmCV

Rust bindings for wasmCV WebAssembly interfaces to computer vision systems.

See https://github.com/wasmvision/wasmcv for information about wasmCV.

Package

How to use

This example Rust module exports a process() function to the WASM host application. When the host calls the processor, it passes in the wasmCV image Mat to be processed. The wasmCV module then calls functions on that Mat which are handled by the host application, by calling OpenCV to actually perform the operations.

#![no_std]

extern crate core;
extern crate wee_alloc;
extern crate alloc;
extern crate wasmcv;

use alloc::string::String;
use alloc::string::ToString;
use wasmcv::wasm::cv;

#[no_mangle]
pub extern fn process(mat: cv::mat::Mat) -> cv::mat::Mat {
    println(&["Cols: ", &mat.cols().to_string(), " Rows: ", &mat.rows().to_string()].concat());

    return mat;
}

/// Print a message to the host [`_println`].
fn println(message: &String) {
    unsafe {
        let (ptr, len) = string_to_ptr(message);
        _println(ptr, len);
    }
}

#[link(wasm_import_module = "hosted")]
extern "C" {
    #[link_name = "println"]
    fn _println(ptr: u32, size: u32);
}

unsafe fn string_to_ptr(s: &String) -> (u32, u32) {
    return (s.as_ptr() as u32, s.len() as u32);
}

// Use `wee_alloc` as the global allocator...for now.
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

You can then compile this module using the Rust compiler.

cargo build --target wasm32-unknown-unknown --release

The wasm32-unknown-unknown target can be used with wasmCV to produce very lightweight guest modules when combined with no_std. The example above compiles to around 14k, including debug information.

-rwxrwxr-x 1 ron ron 14488 sep 12 14:23 processrs.wasm
Commit count: 124

cargo fmt