Skip to content

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

JavaScript

Switch to stable Rust

Ingvar Stepanyan499956e216a5
diff --git a/codecs/hqx/Dockerfile b/codecs/hqx/Dockerfile
index d0287f33..47a13318 100644
--- a/codecs/hqx/Dockerfile
+++ b/codecs/hqx/Dockerfile
@@ -10,8 +10,7 @@ RUN cmake .. -DCMAKE_INSTALL_PREFIX=/opt/wabt && \
make install
FROM rust
-RUN rustup install nightly && \
- rustup target add --toolchain nightly wasm32-unknown-unknown && \
+RUN rustup target add wasm32-unknown-unknown && \
cargo install wasm-pack
COPY --from=0 /opt/wabt /opt/wabt
diff --git a/codecs/hqx/build.sh b/codecs/hqx/build.sh
index 6eadb9e8..7af2abe0 100755
--- a/codecs/hqx/build.sh
+++ b/codecs/hqx/build.sh
@@ -6,8 +6,7 @@ echo "============================================="
echo "Compiling wasm"
echo "============================================="
(
- rustup run nightly \
- wasm-pack build --target no-modules
+ wasm-pack build --target no-modules
wasm-strip pkg/squooshhqx_bg.wasm
echo "Optimising Wasm so it doesn't break Chrome (this takes like 10-15mins. get a cup of tea)"
echo "Once https://crbug.com/974804 is fixed, we can remove this step"
diff --git a/codecs/hqx/pkg/squooshhqx.js b/codecs/hqx/pkg/squooshhqx.js
index 06ab816f..12d43607 100644
--- a/codecs/hqx/pkg/squooshhqx.js
+++ b/codecs/hqx/pkg/squooshhqx.js
@@ -19,16 +19,16 @@
return ptr;
}
- function getArrayU32FromWasm(ptr, len) {
- return getUint32Memory().subarray(ptr / 4, ptr / 4 + len);
+ let cachegetInt32Memory = null;
+ function getInt32Memory() {
+ if (cachegetInt32Memory === null || cachegetInt32Memory.buffer !== wasm.memory.buffer) {
+ cachegetInt32Memory = new Int32Array(wasm.memory.buffer);
+ }
+ return cachegetInt32Memory;
}
- let cachedGlobalArgumentPtr = null;
- function globalArgumentPtr() {
- if (cachedGlobalArgumentPtr === null) {
- cachedGlobalArgumentPtr = wasm.__wbindgen_global_argument_ptr();
- }
- return cachedGlobalArgumentPtr;
+ function getArrayU32FromWasm(ptr, len) {
+ return getUint32Memory().subarray(ptr / 4, ptr / 4 + len);
}
/**
* @param {Uint32Array} input_image
@@ -38,18 +38,12 @@
* @returns {Uint32Array}
*/
__exports.resize = function(input_image, input_width, input_height, factor) {
- const ptr0 = passArray32ToWasm(input_image);
- const len0 = WASM_VECTOR_LEN;
- const retptr = globalArgumentPtr();
- wasm.resize(retptr, ptr0, len0, input_width, input_height, factor);
- const mem = getUint32Memory();
- const rustptr = mem[retptr / 4];
- const rustlen = mem[retptr / 4 + 1];
-
- const realRet = getArrayU32FromWasm(rustptr, rustlen).slice();
- wasm.__wbindgen_free(rustptr, rustlen * 4);
- return realRet;
-
+ const retptr = 8;
+ const ret = wasm.resize(retptr, passArray32ToWasm(input_image), WASM_VECTOR_LEN, input_width, input_height, factor);
+ const memi32 = getInt32Memory();
+ const v0 = getArrayU32FromWasm(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1]).slice();
+ wasm.__wbindgen_free(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1] * 4);
+ return v0;
};
function init(module) {
diff --git a/codecs/hqx/pkg/squooshhqx_bg.d.ts b/codecs/hqx/pkg/squooshhqx_bg.d.ts
index 7345dbb8..808e05a7 100644
--- a/codecs/hqx/pkg/squooshhqx_bg.d.ts
+++ b/codecs/hqx/pkg/squooshhqx_bg.d.ts
@@ -1,6 +1,5 @@
/* tslint:disable */
export const memory: WebAssembly.Memory;
-export function resize(a: number, b: number, c: number, d: number, e: number, f: number): void;
-export function __wbindgen_global_argument_ptr(): number;
export function __wbindgen_malloc(a: number): number;
export function __wbindgen_free(a: number, b: number): void;
+export function resize(a: number, b: number, c: number, d: number, e: number, f: number): void;
diff --git a/codecs/hqx/pkg/squooshhqx_bg.wasm b/codecs/hqx/pkg/squooshhqx_bg.wasm
index bf18ec0f..c5d4857b 100644
Binary files a/codecs/hqx/pkg/squooshhqx_bg.wasm and b/codecs/hqx/pkg/squooshhqx_bg.wasm differ
diff --git a/codecs/resize/Dockerfile b/codecs/resize/Dockerfile
index 1142b581..af9dcc32 100644
--- a/codecs/resize/Dockerfile
+++ b/codecs/resize/Dockerfile
@@ -9,8 +9,7 @@ RUN cmake .. -DCMAKE_INSTALL_PREFIX=/opt/wabt && \
make install
FROM rust
-RUN rustup install nightly && \
- rustup target add --toolchain nightly wasm32-unknown-unknown && \
+RUN rustup target add wasm32-unknown-unknown && \
cargo install wasm-pack
COPY --from=0 /opt/wabt /opt/wabt
diff --git a/codecs/resize/build.sh b/codecs/resize/build.sh
index 240b16ba..c2418c29 100755
--- a/codecs/resize/build.sh
+++ b/codecs/resize/build.sh
@@ -6,8 +6,7 @@ echo "============================================="
echo "Compiling wasm"
echo "============================================="
(
- rustup run nightly \
- wasm-pack build --target no-modules
+ wasm-pack build --target no-modules
wasm-strip pkg/resize_bg.wasm
)
echo "============================================="
diff --git a/codecs/resize/pkg/resize_bg.wasm b/codecs/resize/pkg/resize_bg.wasm
index c9620d5e..50fc8dc9 100644
Binary files a/codecs/resize/pkg/resize_bg.wasm and b/codecs/resize/pkg/resize_bg.wasm differ
diff --git a/codecs/rotate/Dockerfile b/codecs/rotate/Dockerfile
index bf4a416d..103ca5a3 100644
--- a/codecs/rotate/Dockerfile
+++ b/codecs/rotate/Dockerfile
@@ -9,8 +9,7 @@ RUN cmake .. -DCMAKE_INSTALL_PREFIX=/opt/wabt && \
make install
FROM rust
-RUN rustup install nightly && \
- rustup target add --toolchain nightly wasm32-unknown-unknown
+RUN rustup target add wasm32-unknown-unknown
COPY --from=0 /opt/wabt /opt/wabt
ENV PATH="/opt/wabt/bin:${PATH}"
diff --git a/codecs/rotate/build.sh b/codecs/rotate/build.sh
index 954438af..27e0c865 100755
--- a/codecs/rotate/build.sh
+++ b/codecs/rotate/build.sh
@@ -6,8 +6,7 @@ echo "============================================="
echo "Compiling wasm"
echo "============================================="
(
- rustup run nightly \
- cargo build \
+ cargo build \
--target wasm32-unknown-unknown \
--release
cp target/wasm32-unknown-unknown/release/rotate.wasm .
diff --git a/codecs/rotate/rotate.wasm b/codecs/rotate/rotate.wasm
index 246fe6b7..8884c6e6 100755
Binary files a/codecs/rotate/rotate.wasm and b/codecs/rotate/rotate.wasm differ