Skip to content

Make images smaller using best-in-class codecs, right in the browser.

JavaScript

Switch to dssim_core

Surma8c56d16aea08
diff --git a/codecs/visdif/Cargo.lock b/codecs/visdif/Cargo.lock
index 5052151c..3e097a56 100644
--- a/codecs/visdif/Cargo.lock
+++ b/codecs/visdif/Cargo.lock
@@ -1,11 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
-[[package]]
-name = "adler"
-version = "0.2.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e"
-
[[package]]
name = "autocfg"
version = "1.0.1"
@@ -40,15 +34,6 @@ dependencies = [
"wasm-bindgen",
]
-[[package]]
-name = "crc32fast"
-version = "1.2.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1"
-dependencies = [
- "cfg-if",
-]
-
[[package]]
name = "crossbeam-deque"
version = "0.7.3"
@@ -98,13 +83,13 @@ dependencies = [
]
[[package]]
-name = "dssim"
-version = "2.11.2"
+name = "dssim-core"
+version = "2.11.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "462c6cf1cb98d8f7dc5f7a5878e9f05e6be51086370f7f162e446a62ebfab0cb"
dependencies = [
- "getopts",
"imgref",
"itertools",
- "lodepng",
"rayon",
"rgb",
]
@@ -115,33 +100,12 @@ version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd56b59865bce947ac5958779cfa508f6c3b9497cc762b7e24a12d11ccde2c4f"
-[[package]]
-name = "flate2"
-version = "1.0.17"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "766d0e77a2c1502169d4a93ff3b8c15a71fd946cd0126309752104e5f3c46d94"
-dependencies = [
- "cfg-if",
- "crc32fast",
- "libc",
- "miniz_oxide",
-]
-
[[package]]
name = "futures"
version = "0.1.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b980f2816d6ee8673b6517b52cb0e808a180efc92e5c19d02cdda79066703ef"
-[[package]]
-name = "getopts"
-version = "0.2.21"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5"
-dependencies = [
- "unicode-width",
-]
-
[[package]]
name = "hermit-abi"
version = "0.1.15"
@@ -187,17 +151,6 @@ version = "0.2.72"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9f8082297d534141b30c8d39e9b1773713ab50fdbe4ff30f750d063b3bfd701"
-[[package]]
-name = "lodepng"
-version = "3.2.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1d6face0280146377ad4959c588399014554f2875e953fdec135655ca769e1da"
-dependencies = [
- "flate2",
- "libc",
- "rgb",
-]
-
[[package]]
name = "log"
version = "0.4.8"
@@ -228,15 +181,6 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3"
-[[package]]
-name = "miniz_oxide"
-version = "0.4.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4d7559a8a40d0f97e1edea3220f698f78b1c5ab67532e49f68fde3910323b722"
-dependencies = [
- "adler",
-]
-
[[package]]
name = "num_cpus"
version = "1.13.0"
@@ -335,7 +279,9 @@ version = "0.1.0"
dependencies = [
"cfg-if",
"console_error_panic_hook",
- "dssim",
+ "dssim-core",
+ "imgref",
+ "rgb",
"wasm-bindgen",
"wasm-bindgen-test",
"wee_alloc",
@@ -352,12 +298,6 @@ dependencies = [
"unicode-xid 0.2.1",
]
-[[package]]
-name = "unicode-width"
-version = "0.1.8"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
-
[[package]]
name = "unicode-xid"
version = "0.1.0"
diff --git a/codecs/visdif/Cargo.toml b/codecs/visdif/Cargo.toml
index aa818570..ea0424f0 100644
--- a/codecs/visdif/Cargo.toml
+++ b/codecs/visdif/Cargo.toml
@@ -2,6 +2,7 @@
name = "squoosh-visdiff"
version = "0.1.0"
authors = ["Surma <[email protected]>"]
+edition = "2018"
publish = false
[lib]
@@ -14,6 +15,9 @@ default = ["console_error_panic_hook", "wee_alloc"]
[dependencies]
cfg-if = "0.1.2"
wasm-bindgen = "0.2.38"
+dssim-core = "2.11.3"
+imgref = "1.7.0"
+rgb = "0.8.24"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
@@ -27,8 +31,8 @@ console_error_panic_hook = { version = "0.1.1", optional = true }
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.2", optional = true }
-[dependencies.dssim]
-path = "/Users/surma/src/github.com/kornelski/dssim"
+#[dependencies.dssim]
+#path = "/Users/surma/src/github.com/kornelski/dssim"
#dssim = "2.11.2"
diff --git a/codecs/visdif/src/lib.rs b/codecs/visdif/src/lib.rs
index 2bb41ecf..57d64875 100644
--- a/codecs/visdif/src/lib.rs
+++ b/codecs/visdif/src/lib.rs
@@ -1,9 +1,11 @@
-mod utils;
-
use cfg_if::cfg_if;
use wasm_bindgen::prelude::*;
-use dssim::ToRGBAPLU;
+use dssim_core::{Dssim, ToRGBAPLU};
+use imgref;
+use rgb::FromSlice;
+
+mod utils;
cfg_if! {
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
@@ -17,13 +19,28 @@ cfg_if! {
#[wasm_bindgen]
#[no_mangle]
-pub fn ssim(
- image_a: Vec<u8>,
- image_b: Vec<u8>
-) -> f64 {
- let a = image_a.as_slice().to_rgbaplu();
- let b = image_b.as_slice().to_rgbaplu();
+pub fn ssim(data_a: Vec<u8>, data_b: Vec<u8>, width: usize, height: usize) -> f64 {
+ let dssim = Dssim::new();
+
+ // FIXME: Creating new Dssim every time is wasteful. In the context
+ // of the CLI, one image is highly likely to remain static (i.e. the
+ // reference image). dssim_core has a `set_save_ssim_maps`, that should
+ // make subsequent comparisons a lot cheaper.
+
+ let image_a = imgref::Img::new(data_a.as_slice().as_rgba().to_rgbaplu(), width, height);
+ let a = match dssim.create_image(&image_a) {
+ Some(v) => v,
+ _ => return -1.0, // FIXME: Use something more idiomatic
+ };
+
+ let image_b = imgref::Img::new(data_b.as_slice().as_rgba().to_rgbaplu(), width, height);
+ let b = match dssim.create_image(&image_b) {
+ Some(v) => v,
+ _ => return -1.0, // FIXME: Use something more idiomatic
+ };
+
+ //let b = (image_b.as_slice().to_rgbaplu();
// TODO: Rearchitect entire thing to enable `set_save_ssim_maps`
// accrossm multiple SSIM calculations.
- dssim::Dssim::new().compare(a, b)
+ dssim.compare(&a, &b).0.into()
}