Skip to content

Commit

Permalink
refine
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb committed Mar 17, 2024
1 parent 7e10231 commit ad193dc
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
105 changes: 104 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,110 @@ The code for `Node.js` and `Deno` is generated using `wit-bindgen` and `jco`.
| --------------- |---------------| -------------------- |
| ![](https://github.com/bokuweb/pixelmatch-rs/raw/main/fixtures/001a.png) | ![](https://github.com/bokuweb/pixelmatch-rs/raw/main/fixtures/001b.png) |![](https://github.com/bokuweb/pixelmatch-rs/raw/main/assets/diff1.png)|

## Generate JS code from wasm component.
## JavaScript


### installation

```
npm install @bokuweb/image-diff-wasm
```

### examples

```js
import { readFile } from "node:fs/promises";
import { diff } from "@bokuweb/image-diff-wasm";

const imga = await readFile(PATH_TO_IMAGE_A);
const imgb = await readFile(PATH_TO_IMAGE_B);

const result = diff(imga, imgb, { enableAntiAlias: true, threshold: 0.01 });
```

### API

#### diff(imga: Uint8Array, imgb: Uint8Array, opts: Opts): Output;

The diff function is designed to compare two images and identify their differences.
It takes two image buffers as input and returns an `Output` object containing information about the differences.

##### Input

- `imga`: Uint8Array: The first image buffer.
- `imgb`: Uint8Array: The second image buffer.
- `opts`: Opts: Options object for the function.

```Typescript
export interface Opts {
threshold?: number,
includeAntiAlias?: boolean,
}
```

- `threshold`: Matching threshold, ranges from 0 to 1. Smaller values make the comparison more sensitive. 0.1 by default.
- `includeAntiAlias`: The flag of antialias. If omitted false.

##### Output

```Typescript
export interface Output {
diffCount: number,
diffImage: Uint8Array,
width: number,
height: number,
}
```

- `diffCount`: The number of pixels that differ between the two images.
- `diffImage`: The buffer of the difference image in `WebP` format.
- `width`: The width of the difference image.
- `height`: The height of the difference image.

##### Error

The function may throw following values as `ComponentError`.

```Typescript
export type Error = ErrorDecode | ErrorEncode;
export interface ErrorDecode {
tag: 'decode',
val: string,
}
export interface ErrorEncode {
tag: 'encode',
val: string,
}
```

## Rust

### examples

```Rust
pub fn main() {
let imga = std::fs::read("../fixtures/sample0.webp").unwrap();
let imgb = std::fs::read("../fixtures/sample1.webp").unwrap();

let _result = diff(
imga,
imgb,
&DiffOption {
threshold: Some(0.01),
include_anti_alias: Some(true),
},
)
.unwrap();
}
```

``` sh
cargo run --example compare
```

## Wasm

### Generate JS code from wasm component.

```sh
AR=llvm-ar CFLAGS='--sysroot ../wasi-sdk/share/wasi-sysroot' cargo wasi build --release
Expand Down
5 changes: 4 additions & 1 deletion wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ wit_bindgen::generate!({
}
});

struct ImageDiff;
// re-export
pub use image_diff_rs::diff;

pub struct ImageDiff;

impl From<ImageDiffError> for bokuweb::image_diff::types::Error {
fn from(value: ImageDiffError) -> Self {
Expand Down

0 comments on commit ad193dc

Please sign in to comment.