Make images smaller using best-in-class codecs, right in the browser.
JavaScript
Trying dssim
Surma773208d76ec2
diff --git a/codecs/visdif/.gitignore b/codecs/visdif/.gitignorenew file mode 100644index 00000000..785da573--- /dev/null+++ b/codecs/visdif/.gitignore@@ -0,0 +1,4 @@+**/*.rs.bk+target+bin/+pkg/README.mddiff --git a/codecs/visdif/Cargo.lock b/codecs/visdif/Cargo.locknew file mode 100644index 00000000..5052151c--- /dev/null+++ b/codecs/visdif/Cargo.lock@@ -0,0 +1,507 @@+# 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"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"++[[package]]+name = "bumpalo"+version = "3.4.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820"++[[package]]+name = "bytemuck"+version = "1.3.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "db7a1029718df60331e557c9e83a55523c955e5dd2a7bfeffad6bbd50b538ae9"++[[package]]+name = "cfg-if"+version = "0.1.10"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"++[[package]]+name = "console_error_panic_hook"+version = "0.1.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "b8d976903543e0c48546a91908f21588a680a8c8f984df9a5d69feccb2b2a211"+dependencies = [+ "cfg-if",+ "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"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285"+dependencies = [+ "crossbeam-epoch",+ "crossbeam-utils",+ "maybe-uninit",+]++[[package]]+name = "crossbeam-epoch"+version = "0.8.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace"+dependencies = [+ "autocfg",+ "cfg-if",+ "crossbeam-utils",+ "lazy_static",+ "maybe-uninit",+ "memoffset",+ "scopeguard",+]++[[package]]+name = "crossbeam-queue"+version = "0.2.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570"+dependencies = [+ "cfg-if",+ "crossbeam-utils",+ "maybe-uninit",+]++[[package]]+name = "crossbeam-utils"+version = "0.7.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"+dependencies = [+ "autocfg",+ "cfg-if",+ "lazy_static",+]++[[package]]+name = "dssim"+version = "2.11.2"+dependencies = [+ "getopts",+ "imgref",+ "itertools",+ "lodepng",+ "rayon",+ "rgb",+]++[[package]]+name = "either"+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"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9"+dependencies = [+ "libc",+]++[[package]]+name = "imgref"+version = "1.7.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d8767843be7909b5b3ffd9b52cdc042882b94e2cfe0c00abc4ce1c301cb1fcae"++[[package]]+name = "itertools"+version = "0.9.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b"+dependencies = [+ "either",+]++[[package]]+name = "js-sys"+version = "0.3.41"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c4b9172132a62451e56142bff9afc91c8e4a4500aa5b847da36815b63bfda916"+dependencies = [+ "wasm-bindgen",+]++[[package]]+name = "lazy_static"+version = "1.4.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"++[[package]]+name = "libc"+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"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"+dependencies = [+ "cfg-if",+]++[[package]]+name = "maybe-uninit"+version = "2.0.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00"++[[package]]+name = "memoffset"+version = "0.5.5"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c198b026e1bbf08a937e94c6c60f9ec4a2267f5b0d2eec9c1b21b061ce2be55f"+dependencies = [+ "autocfg",+]++[[package]]+name = "memory_units"+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"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"+dependencies = [+ "hermit-abi",+ "libc",+]++[[package]]+name = "proc-macro2"+version = "0.4.30"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"+dependencies = [+ "unicode-xid 0.1.0",+]++[[package]]+name = "proc-macro2"+version = "1.0.18"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "beae6331a816b1f65d04c45b078fd8e6c93e8071771f41b8163255bbd8d7c8fa"+dependencies = [+ "unicode-xid 0.2.1",+]++[[package]]+name = "quote"+version = "0.6.13"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1"+dependencies = [+ "proc-macro2 0.4.30",+]++[[package]]+name = "quote"+version = "1.0.7"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"+dependencies = [+ "proc-macro2 1.0.18",+]++[[package]]+name = "rayon"+version = "1.3.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "62f02856753d04e03e26929f820d0a0a337ebe71f849801eea335d464b349080"+dependencies = [+ "autocfg",+ "crossbeam-deque",+ "either",+ "rayon-core",+]++[[package]]+name = "rayon-core"+version = "1.7.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e92e15d89083484e11353891f1af602cc661426deb9564c298b270c726973280"+dependencies = [+ "crossbeam-deque",+ "crossbeam-queue",+ "crossbeam-utils",+ "lazy_static",+ "num_cpus",+]++[[package]]+name = "rgb"+version = "0.8.24"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "7466cad0eb3303798229ffab23bb8f598d185c71f3dfa17cd751d440e375782a"+dependencies = [+ "bytemuck",+]++[[package]]+name = "scoped-tls"+version = "1.0.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2"++[[package]]+name = "scopeguard"+version = "1.1.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"++[[package]]+name = "squoosh-visdiff"+version = "0.1.0"+dependencies = [+ "cfg-if",+ "console_error_panic_hook",+ "dssim",+ "wasm-bindgen",+ "wasm-bindgen-test",+ "wee_alloc",+]++[[package]]+name = "syn"+version = "1.0.34"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "936cae2873c940d92e697597c5eee105fb570cd5689c695806f672883653349b"+dependencies = [+ "proc-macro2 1.0.18",+ "quote 1.0.7",+ "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"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"++[[package]]+name = "unicode-xid"+version = "0.2.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"++[[package]]+name = "wasm-bindgen"+version = "0.2.64"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "6a634620115e4a229108b71bde263bb4220c483b3f07f5ba514ee8d15064c4c2"+dependencies = [+ "cfg-if",+ "wasm-bindgen-macro",+]++[[package]]+name = "wasm-bindgen-backend"+version = "0.2.64"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "3e53963b583d18a5aa3aaae4b4c1cb535218246131ba22a71f05b518098571df"+dependencies = [+ "bumpalo",+ "lazy_static",+ "log",+ "proc-macro2 1.0.18",+ "quote 1.0.7",+ "syn",+ "wasm-bindgen-shared",+]++[[package]]+name = "wasm-bindgen-futures"+version = "0.3.27"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "83420b37346c311b9ed822af41ec2e82839bfe99867ec6c54e2da43b7538771c"+dependencies = [+ "cfg-if",+ "futures",+ "js-sys",+ "wasm-bindgen",+ "web-sys",+]++[[package]]+name = "wasm-bindgen-macro"+version = "0.2.64"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "3fcfd5ef6eec85623b4c6e844293d4516470d8f19cd72d0d12246017eb9060b8"+dependencies = [+ "quote 1.0.7",+ "wasm-bindgen-macro-support",+]++[[package]]+name = "wasm-bindgen-macro-support"+version = "0.2.64"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9adff9ee0e94b926ca81b57f57f86d5545cdcb1d259e21ec9bdd95b901754c75"+dependencies = [+ "proc-macro2 1.0.18",+ "quote 1.0.7",+ "syn",+ "wasm-bindgen-backend",+ "wasm-bindgen-shared",+]++[[package]]+name = "wasm-bindgen-shared"+version = "0.2.64"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "7f7b90ea6c632dd06fd765d44542e234d5e63d9bb917ecd64d79778a13bd79ae"++[[package]]+name = "wasm-bindgen-test"+version = "0.2.50"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "a2d9693b63a742d481c7f80587e057920e568317b2806988c59cd71618bc26c1"+dependencies = [+ "console_error_panic_hook",+ "futures",+ "js-sys",+ "scoped-tls",+ "wasm-bindgen",+ "wasm-bindgen-futures",+ "wasm-bindgen-test-macro",+]++[[package]]+name = "wasm-bindgen-test-macro"+version = "0.2.50"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "0789dac148a8840bbcf9efe13905463b733fa96543bfbf263790535c11af7ba5"+dependencies = [+ "proc-macro2 0.4.30",+ "quote 0.6.13",+]++[[package]]+name = "web-sys"+version = "0.3.41"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "863539788676619aac1a23e2df3655e96b32b0e05eb72ca34ba045ad573c625d"+dependencies = [+ "js-sys",+ "wasm-bindgen",+]++[[package]]+name = "wee_alloc"+version = "0.4.5"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e"+dependencies = [+ "cfg-if",+ "libc",+ "memory_units",+ "winapi",+]++[[package]]+name = "winapi"+version = "0.3.9"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"+dependencies = [+ "winapi-i686-pc-windows-gnu",+ "winapi-x86_64-pc-windows-gnu",+]++[[package]]+name = "winapi-i686-pc-windows-gnu"+version = "0.4.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"++[[package]]+name = "winapi-x86_64-pc-windows-gnu"+version = "0.4.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"diff --git a/codecs/visdif/Cargo.toml b/codecs/visdif/Cargo.tomlnew file mode 100644index 00000000..aa818570--- /dev/null+++ b/codecs/visdif/Cargo.toml@@ -0,0 +1,41 @@+[package]+name = "squoosh-visdiff"+version = "0.1.0"+authors = ["Surma <[email protected]>"]+publish = false++[lib]+#crate-type = ["cdylib", "rlib"]+crate-type = ["cdylib"]++[features]+default = ["console_error_panic_hook", "wee_alloc"]++[dependencies]+cfg-if = "0.1.2"+wasm-bindgen = "0.2.38"+# 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+# code size when deploying.+console_error_panic_hook = { version = "0.1.1", optional = true }++# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size+# compared to the default allocator's ~10K. It is slower than the default+# allocator, however.+#+# 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"++#dssim = "2.11.2"++[dev-dependencies]+wasm-bindgen-test = "0.2"++[profile.release]+# Tell `rustc` to optimize for small code size.+opt-level = "s"+lto = truediff --git a/codecs/visdif/package-lock.json b/codecs/visdif/package-lock.jsonnew file mode 100644index 00000000..ca1a498d--- /dev/null+++ b/codecs/visdif/package-lock.json@@ -0,0 +1,4 @@+{+ "name": "resize",+ "lockfileVersion": 1+}diff --git a/codecs/visdif/package.json b/codecs/visdif/package.jsonnew file mode 100644index 00000000..facaf7d4--- /dev/null+++ b/codecs/visdif/package.json@@ -0,0 +1,6 @@+{+ "name": "resize",+ "scripts": {+ "build": "../build-rust.sh"+ }+}diff --git a/codecs/visdif/src/lib.rs b/codecs/visdif/src/lib.rsnew file mode 100644index 00000000..2bb41ecf--- /dev/null+++ b/codecs/visdif/src/lib.rs@@ -0,0 +1,29 @@+mod utils;++use cfg_if::cfg_if;+use wasm_bindgen::prelude::*;++use dssim::ToRGBAPLU;++cfg_if! {+ // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global+ // allocator.+ if #[cfg(feature = "wee_alloc")] {+ extern crate wee_alloc;+ #[global_allocator]+ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;+ }+}++#[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();+ // TODO: Rearchitect entire thing to enable `set_save_ssim_maps`+ // accrossm multiple SSIM calculations.+ dssim::Dssim::new().compare(a, b)+}diff --git a/codecs/visdif/src/utils.rs b/codecs/visdif/src/utils.rsnew file mode 100644index 00000000..2ffc954d--- /dev/null+++ b/codecs/visdif/src/utils.rs@@ -0,0 +1,17 @@+use cfg_if::cfg_if;++cfg_if! {+ // When the `console_error_panic_hook` feature is enabled, we can call the+ // `set_panic_hook` function at least once during initialization, and then+ // we will get better error messages if our code ever panics.+ //+ // For more details see+ // https://github.com/rustwasm/console_error_panic_hook#readme+ if #[cfg(feature = "console_error_panic_hook")] {+ extern crate console_error_panic_hook;+ pub use self::console_error_panic_hook::set_once as set_panic_hook;+ } else {+ #[inline]+ pub fn set_panic_hook() {}+ }+}