Compare commits

...

88 Commits

Author SHA1 Message Date
Jake Archibald
bc5da7ef06 1.5.0 2019-03-08 11:19:34 +00:00
Surma
45221c0b03 Implement alpha premultiplication (#507)
* Implement alpha premultiplication

* Add benchmark to resize

* Only display "Premultiply alpha" if it's one of the rust resize types.

* Add comment about division by zero
2019-03-08 11:18:59 +00:00
Surma
d29cf2ffa7 Merge pull request #501 from GoogleChromeLabs/renovate/node-10.x
Update dependency @types/node to v10.12.30
2019-03-06 22:02:52 +00:00
renovate[bot]
f6c0b89d1f Update dependency @types/node to v10.12.30 2019-03-06 20:14:19 +00:00
Jake Archibald
ecd9e06665 1.4.0 2019-03-06 17:20:54 +00:00
Jake Archibald
9e5b66d5f4 Better resize methods (#498)
* Port resize to wasm

* Expose resize algorithms

* Lanczos3 working!

* lol copy paste

* Adding support for other resizers

* Don’t track generated README

* Cache wasm instance
2019-03-06 17:20:25 +00:00
Surma
8c35c3cdaa Merge pull request #497 from GoogleChromeLabs/renovate/node-10.x
Update Node.js to v10.15.3
2019-03-05 23:26:23 +00:00
renovate[bot]
828a6240fe Update Node.js to v10.15.3 2019-03-05 20:51:40 +00:00
Jake Archibald
eaad0eaee0 1.3.4 2019-03-04 14:53:11 +00:00
Surma
db76d4417c Don’t use instantiateStreaming (#494) 2019-03-04 14:50:15 +00:00
Surma
7a6c6ec210 Merge pull request #492 from GoogleChromeLabs/renovate/typed-css-modules-0.x
Update dependency typed-css-modules to v0.4.1
2019-03-03 17:22:26 +00:00
renovate[bot]
8e034f183b Update dependency typed-css-modules to v0.4.1 2019-03-03 15:37:17 +00:00
Surma
5a01b34cce Merge pull request #491 from GoogleChromeLabs/renovate/webpack-bundle-analyzer-3.x
Update dependency webpack-bundle-analyzer to v3.1.0
2019-03-03 13:21:08 +00:00
renovate[bot]
1399a9bffe Update dependency webpack-bundle-analyzer to v3.1.0 2019-03-02 16:47:49 +00:00
Surma
653c6ed85a Merge pull request #490 from GoogleChromeLabs/renovate/node-10.x
Update dependency @types/node to v10.12.29
2019-03-02 16:46:24 +00:00
renovate[bot]
ebbb7b58cb Update dependency @types/node to v10.12.29 2019-03-02 02:14:38 +00:00
Surma
7164e4e315 Merge pull request #489 from GoogleChromeLabs/renovate/tslint-5.x
Update dependency tslint to v5.13.1
2019-03-01 08:54:11 +00:00
renovate[bot]
23398d07f9 Update dependency tslint to v5.13.1 2019-03-01 03:07:29 +00:00
Surma
ec2bc3efa2 Merge pull request #488 from GoogleChromeLabs/renovate/assets-webpack-plugin-3.x
Update dependency assets-webpack-plugin to v3.9.10
2019-02-28 19:06:57 +00:00
renovate[bot]
86d78763c1 Update dependency assets-webpack-plugin to v3.9.10 2019-02-28 19:01:50 +00:00
Surma
fb5ae36d7e Merge pull request #487 from GoogleChromeLabs/renovate/assets-webpack-plugin-3.x
Update dependency assets-webpack-plugin to v3.9.9
2019-02-28 15:40:43 +00:00
renovate[bot]
51f812625b Update dependency assets-webpack-plugin to v3.9.9 2019-02-28 15:33:56 +00:00
Surma
479bfee647 Merge pull request #486 from GoogleChromeLabs/renovate/node-10.x
Update Node.js to v10.15.2
2019-02-28 14:03:11 +00:00
renovate[bot]
a3501a56cd Update Node.js to v10.15.2 2019-02-28 13:51:15 +00:00
Surma
c353e286b0 Merge pull request #485 from GoogleChromeLabs/renovate/assets-webpack-plugin-3.x
Update dependency assets-webpack-plugin to v3.9.8
2019-02-28 09:35:03 +00:00
renovate[bot]
8ed01e8a87 Update dependency assets-webpack-plugin to v3.9.8 2019-02-28 04:55:19 +00:00
Surma
36ed21b9f4 Merge pull request #484 from GoogleChromeLabs/renovate/webpack-dev-server-3.x
Update dependency webpack-dev-server to v3.2.1
2019-02-25 15:03:53 +00:00
renovate[bot]
cca41bb449 Update dependency webpack-dev-server to v3.2.1 2019-02-25 14:14:58 +00:00
Surma
8f787ad0e6 Merge pull request #483 from GoogleChromeLabs/renovate/terser-webpack-plugin-1.x
Update dependency terser-webpack-plugin to v1.2.3
2019-02-25 14:13:23 +00:00
renovate[bot]
9c1170f100 Update dependency terser-webpack-plugin to v1.2.3 2019-02-25 11:20:37 +00:00
Surma
5432be4a3f Merge pull request #482 from GoogleChromeLabs/renovate/tslint-5.x
Update dependency tslint to v5.13.0
2019-02-24 18:54:51 +00:00
renovate[bot]
7cae821db5 Update dependency tslint to v5.13.0 2019-02-23 22:21:29 +00:00
Surma
19ebb24f03 Merge pull request #481 from GoogleChromeLabs/renovate/node-10.x
Update dependency @types/node to v10.12.27
2019-02-23 08:45:17 +00:00
renovate[bot]
d07512566e Update dependency @types/node to v10.12.27 2019-02-22 22:27:01 +00:00
Surma
61929666f3 Merge pull request #480 from GoogleChromeLabs/renovate/prerender-loader-1.x
Update dependency prerender-loader to v1.3.0
2019-02-22 15:59:25 +00:00
renovate[bot]
792ffbfcd7 Update dependency prerender-loader to v1.3.0 2019-02-22 15:54:09 +00:00
Surma
9685271bb4 Merge pull request #479 from GoogleChromeLabs/renovate/typescript-3.x
Update dependency typescript to v3.3.3333
2019-02-22 15:03:59 +00:00
renovate[bot]
5b1a6cc95e Update dependency typescript to v3.3.3333 2019-02-22 14:23:45 +00:00
renovate[bot]
bf34075e6a Update dependency typescript to v3.3.1 (#444)
* Update dependency typescript to v3.3.1

* Fix trailing commas for function calls

* Reverting trailing comma change

* Avoiding trailing comma rule for imports

* I never know what the right thing to do is

* lockfile
2019-02-22 14:22:57 +00:00
Jake Archibald
f1859eeef2 Only compare against the last 'push' to master (#468) 2019-02-22 11:15:50 +00:00
Surma
fa12b37e53 Merge pull request #470 from GoogleChromeLabs/renovate/file-drop-element-0.x
Update dependency file-drop-element to v0.2.0
2019-02-21 23:42:35 +00:00
Paul Kinlan
520a5dc9f2 nggggg - file length === 0 2019-02-21 21:27:59 +00:00
Paul Kinlan
7af949b5a5 Fixing a second whoopsie. 2019-02-21 20:53:14 +00:00
Paul Kinlan
300612b09b Fixing a whoopsie 2019-02-21 20:38:08 +00:00
Paul Kinlan
6f00e9825c Fixing the multifile dep update: file->files 2019-02-21 20:35:49 +00:00
renovate[bot]
6ca9c5300e Update dependency file-drop-element to v0.2.0 2019-02-21 15:24:22 +00:00
Surma
cdeb31051b Merge pull request #464 from GoogleChromeLabs/tiling-rust
Tiling for Rotate
2019-02-21 15:23:32 +00:00
Surma
ba90517ad7 Remove baseline benchmark and switch to tile size 16 2019-02-21 15:16:26 +00:00
Surma
7aff949f47 Merge pull request #476 from jviide/tiling-rust-02
Fix potential over-the-bounds slicing in rotate.rs introduced in #474
2019-02-21 15:16:26 +00:00
Joachim Viide
0e8c0da3dd Update the built rotate.wasm file 2019-02-21 15:16:26 +00:00
Joachim Viide
3132a207e1 Fix potential over-the-bounds slicing in rotate.rs 2019-02-21 15:16:26 +00:00
Surma
88dd0e06c5 Merge pull request #474 from jviide/tiling-rust
Remove timing difference between 90° and 270° rotations (in the tiling Rust code)
2019-02-21 15:16:25 +00:00
Joachim Viide
f507a2464f Update the built rotate.wasm file 2019-02-21 15:16:25 +00:00
Joachim Viide
14baa6ebf8 Reorganize rotate.rs, separate rotations into their own functions 2019-02-21 15:16:25 +00:00
Joachim Viide
5d32126565 Use iteration in rotate.rs whenever possible 2019-02-21 15:16:25 +00:00
Joachim Viide
484ff7ab4c Fix unwrap_hard when debug_assertions is set 2019-02-21 15:16:25 +00:00
Surma
36f86385a2 Update benchmark flags 2019-02-21 15:16:24 +00:00
Surma
436faa17af More conservative tiling size 2019-02-21 15:16:24 +00:00
Surma
d205ae206f Implement 180 and 270 2019-02-21 15:16:24 +00:00
Surma
6baa5900fc Implement tiling 2019-02-21 15:16:24 +00:00
Surma
fadb53f075 Readd rotation cases 2019-02-21 15:16:24 +00:00
Surma
1a63387408 Use a trait to make it nicer 2019-02-21 15:16:23 +00:00
Surma
a316120b69 Switch to 8 byte offset 2019-02-21 15:16:23 +00:00
Surma
0d1e5ef119 Simplify rotation code to 90deg only 2019-02-21 15:16:23 +00:00
Surma
b49cfca39d Scriptify benchmark running 2019-02-21 15:16:23 +00:00
Surma
ab58df4c2c Benchmark all rotations 2019-02-21 15:16:22 +00:00
Surma
db20f10bd2 Write d8 benchmark using the baseline compiler 2019-02-21 15:16:22 +00:00
Surma
444cc5a193 Merge pull request #475 from GoogleChromeLabs/renovate/critters-webpack-plugin-2.x
Update dependency critters-webpack-plugin to v2.3.0
2019-02-21 14:57:58 +00:00
renovate[bot]
6c253bc9b4 Update dependency critters-webpack-plugin to v2.3.0 2019-02-21 02:46:18 +00:00
Surma
2fd28e174e Merge pull request #473 from GoogleChromeLabs/renovate/webpack-dev-server-3.x
Update dependency webpack-dev-server to v3.2.0
2019-02-20 15:03:01 +00:00
renovate[bot]
a188692c88 Update dependency webpack-dev-server to v3.2.0 2019-02-20 14:41:25 +00:00
Surma
b263419e08 Merge pull request #472 from GoogleChromeLabs/renovate/copy-webpack-plugin-5.x
Update dependency copy-webpack-plugin to v5
2019-02-20 14:39:15 +00:00
renovate[bot]
826e06c727 Update dependency copy-webpack-plugin to v5 2019-02-20 13:22:09 +00:00
Surma
dfcdfb105f Merge pull request #471 from GoogleChromeLabs/renovate/worker-plugin-3.x
Update dependency worker-plugin to v3.1.0
2019-02-20 10:46:01 +00:00
renovate[bot]
0508bbb16f Update dependency worker-plugin to v3.1.0 2019-02-19 16:15:03 +00:00
Surma
dfbfa85fd3 Merge pull request #467 from GoogleChromeLabs/renovate/chokidar-2.x
Update dependency chokidar to v2.1.2
2019-02-18 21:03:44 +01:00
renovate[bot]
b99ad4bdc3 Update dependency chokidar to v2.1.2 2019-02-18 19:59:01 +00:00
Jake Archibald
e801170496 1.3.3 2019-02-15 09:49:34 +00:00
Surma
91e7c9c5ad Make Rust rotate code smaller (#462)
* Make Rust rotate code smaller

* Back on the rust happy path
2019-02-15 09:47:26 +00:00
Jake Archibald
ca5162ed32 Updating package lock to fix Netlify 2019-02-13 15:08:56 +00:00
Surma
0bf87d0c87 Merge pull request #461 from GoogleChromeLabs/renovate/node-10.x
Update dependency @types/node to v10.12.26
2019-02-13 14:02:00 +00:00
renovate[bot]
ce91eb5bae Update dependency @types/node to v10.12.26 2019-02-13 00:00:41 +00:00
Surma
8d68056bca Merge pull request #457 from GoogleChromeLabs/renovate/chokidar-2.x
Update dependency chokidar to v2.1.1
2019-02-12 13:24:03 +00:00
renovate[bot]
d0de8e444a Update dependency chokidar to v2.1.1 2019-02-12 12:17:17 +00:00
Surma
dfef1f21cc Merge pull request #455 from GoogleChromeLabs/renovate/node-10.x
Update dependency @types/node to v10.12.25
2019-02-12 12:16:17 +00:00
renovate[bot]
2440ac4e87 Update dependency @types/node to v10.12.25 2019-02-12 11:09:55 +00:00
Surma
e90db78697 Merge pull request #459 from GoogleChromeLabs/renovate/webpack-bundle-analyzer-3.x
Update dependency webpack-bundle-analyzer to v3.0.4
2019-02-12 11:09:08 +00:00
renovate[bot]
5ae15d429c Update dependency webpack-bundle-analyzer to v3.0.4 2019-02-12 10:54:25 +00:00
41 changed files with 1227 additions and 785 deletions

2
.nvmrc
View File

@@ -1 +1 @@
v10.15.1
10.15.3

5
codecs/resize/.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
**/*.rs.bk
target
Cargo.lock
bin/
pkg/README.md

37
codecs/resize/Cargo.toml Normal file
View File

@@ -0,0 +1,37 @@
[package]
name = "resize"
version = "0.1.0"
authors = ["Surma <surma@surma.link>"]
[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"
resize = "0.3.0"
# 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 }
[dev-dependencies]
wasm-bindgen-test = "0.2"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
lto = true

18
codecs/resize/Dockerfile Normal file
View File

@@ -0,0 +1,18 @@
FROM ubuntu
RUN apt-get update && \
apt-get install -qqy git build-essential cmake python2.7
RUN git clone --recursive https://github.com/WebAssembly/wabt /usr/src/wabt
RUN mkdir -p /usr/src/wabt/build
WORKDIR /usr/src/wabt/build
RUN cmake .. -DCMAKE_INSTALL_PREFIX=/opt/wabt && \
make && \
make install
FROM rust
RUN rustup install nightly && \
rustup target add --toolchain nightly wasm32-unknown-unknown && \
cargo install wasm-pack
COPY --from=0 /opt/wabt /opt/wabt
ENV PATH="/opt/wabt/bin:${PATH}"
WORKDIR /src

View File

@@ -0,0 +1,41 @@
// THIS IS NOT A NODE SCRIPT
// This is a d8 script. Please install jsvu[1] and install v8.
// Then run `npm run --silent benchmark`.
// [1]: https://github.com/GoogleChromeLabs/jsvu
self = global = this;
load('./pkg/resize.js');
async function init() {
// Adjustable constants.
const inputDimensions = 2000;
const outputDimensions = 1500;
const algorithm = 3; // Lanczos
const iterations = new Array(100);
// Constants. Dont change.
const imageByteSize = inputDimensions * inputDimensions * 4;
const imageBuffer = new Uint8ClampedArray(imageByteSize);
const module = await WebAssembly.compile(readbuffer("./pkg/resize_bg.wasm"));
await wasm_bindgen(module);
[false, true].forEach(premulti => {
print(`\npremultiplication: ${premulti}`);
print(`==============================`);
for (let i = 0; i < 100; i++) {
const start = Date.now();
wasm_bindgen.resize(imageBuffer, inputDimensions, inputDimensions, outputDimensions, outputDimensions, algorithm, premulti);
iterations[i] = Date.now() - start;
}
const average = iterations.reduce((sum, c) => sum + c) / iterations.length;
const stddev = Math.sqrt(
iterations
.map(i => Math.pow(i - average, 2))
.reduce((sum, c) => sum + c) / iterations.length
);
print(`n = ${iterations.length}`);
print(`Average: ${average}`);
print(`StdDev: ${stddev}`);
});
}
init().catch(e => console.error(e, e.stack));

22
codecs/resize/build.sh Executable file
View File

@@ -0,0 +1,22 @@
#!/bin/bash
set -e
echo "============================================="
echo "Compiling wasm"
echo "============================================="
(
rustup run nightly \
wasm-pack build --target no-modules
wasm-strip pkg/resize_bg.wasm
)
echo "============================================="
echo "Compiling wasm done"
echo "============================================="
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "Did you update your docker image?"
echo "Run \`docker pull ubuntu\`"
echo "Run \`docker pull rust\`"
echo "Run \`docker build -t squoosh-resize .\`"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"

4
codecs/resize/package-lock.json generated Normal file
View File

@@ -0,0 +1,4 @@
{
"name": "resize",
"lockfileVersion": 1
}

View File

@@ -0,0 +1,8 @@
{
"name": "resize",
"scripts": {
"build:image": "docker build -t squoosh-resize .",
"build": "docker run --rm -v $(pwd):/src squoosh-resize ./build.sh",
"benchmark": "v8 --no-liftoff --no-wasm-tier-up ./benchmark.js"
}
}

12
codecs/resize/pkg/resize.d.ts vendored Normal file
View File

@@ -0,0 +1,12 @@
/* tslint:disable */
/**
* @param {Uint8Array} arg0
* @param {number} arg1
* @param {number} arg2
* @param {number} arg3
* @param {number} arg4
* @param {number} arg5
* @param {boolean} arg6
* @returns {Uint8Array}
*/
export function resize(arg0: Uint8Array, arg1: number, arg2: number, arg3: number, arg4: number, arg5: number, arg6: boolean): Uint8Array;

113
codecs/resize/pkg/resize.js Normal file
View File

@@ -0,0 +1,113 @@
(function() {
var wasm;
const __exports = {};
let cachegetUint8Memory = null;
function getUint8Memory() {
if (cachegetUint8Memory === null || cachegetUint8Memory.buffer !== wasm.memory.buffer) {
cachegetUint8Memory = new Uint8Array(wasm.memory.buffer);
}
return cachegetUint8Memory;
}
let WASM_VECTOR_LEN = 0;
function passArray8ToWasm(arg) {
const ptr = wasm.__wbindgen_malloc(arg.length * 1);
getUint8Memory().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
function getArrayU8FromWasm(ptr, len) {
return getUint8Memory().subarray(ptr / 1, ptr / 1 + len);
}
let cachedGlobalArgumentPtr = null;
function globalArgumentPtr() {
if (cachedGlobalArgumentPtr === null) {
cachedGlobalArgumentPtr = wasm.__wbindgen_global_argument_ptr();
}
return cachedGlobalArgumentPtr;
}
let cachegetUint32Memory = null;
function getUint32Memory() {
if (cachegetUint32Memory === null || cachegetUint32Memory.buffer !== wasm.memory.buffer) {
cachegetUint32Memory = new Uint32Array(wasm.memory.buffer);
}
return cachegetUint32Memory;
}
/**
* @param {Uint8Array} arg0
* @param {number} arg1
* @param {number} arg2
* @param {number} arg3
* @param {number} arg4
* @param {number} arg5
* @param {boolean} arg6
* @returns {Uint8Array}
*/
__exports.resize = function(arg0, arg1, arg2, arg3, arg4, arg5, arg6) {
const ptr0 = passArray8ToWasm(arg0);
const len0 = WASM_VECTOR_LEN;
const retptr = globalArgumentPtr();
wasm.resize(retptr, ptr0, len0, arg1, arg2, arg3, arg4, arg5, arg6);
const mem = getUint32Memory();
const rustptr = mem[retptr / 4];
const rustlen = mem[retptr / 4 + 1];
const realRet = getArrayU8FromWasm(rustptr, rustlen).slice();
wasm.__wbindgen_free(rustptr, rustlen * 1);
return realRet;
};
const heap = new Array(32);
heap.fill(undefined);
heap.push(undefined, null, true, false);
let heap_next = heap.length;
function dropObject(idx) {
if (idx < 36) return;
heap[idx] = heap_next;
heap_next = idx;
}
__exports.__wbindgen_object_drop_ref = function(i) { dropObject(i); };
function init(path_or_module) {
let instantiation;
const imports = { './resize': __exports };
if (path_or_module instanceof WebAssembly.Module) {
instantiation = WebAssembly.instantiate(path_or_module, imports)
.then(instance => {
return { instance, module: path_or_module }
});
} else {
const data = fetch(path_or_module);
if (typeof WebAssembly.instantiateStreaming === 'function') {
instantiation = WebAssembly.instantiateStreaming(data, imports)
.catch(e => {
console.warn("`WebAssembly.instantiateStreaming` failed. Assuming this is because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
return data
.then(r => r.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes, imports));
});
} else {
instantiation = data
.then(response => response.arrayBuffer())
.then(buffer => WebAssembly.instantiate(buffer, imports));
}
}
return instantiation.then(({instance}) => {
wasm = init.wasm = instance.exports;
});
};
self.wasm_bindgen = Object.assign(init, __exports);
})();

6
codecs/resize/pkg/resize_bg.d.ts vendored Normal file
View File

@@ -0,0 +1,6 @@
/* tslint:disable */
export const memory: WebAssembly.Memory;
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, g: number, h: number, i: number): void;

Binary file not shown.

79
codecs/resize/src/lib.rs Normal file
View File

@@ -0,0 +1,79 @@
extern crate cfg_if;
extern crate resize;
extern crate wasm_bindgen;
mod utils;
use cfg_if::cfg_if;
use resize::Pixel::RGBA;
use resize::Type;
use wasm_bindgen::prelude::*;
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 resize(
mut input_image: Vec<u8>,
input_width: usize,
input_height: usize,
output_width: usize,
output_height: usize,
typ_idx: usize,
premultiply: bool,
) -> Vec<u8> {
let typ = match typ_idx {
0 => Type::Triangle,
1 => Type::Catrom,
2 => Type::Mitchell,
3 => Type::Lanczos3,
_ => panic!("Nope"),
};
let num_input_pixels = input_width * input_height;
let num_output_pixels = output_width * output_height;
if premultiply {
for i in 0..num_input_pixels {
for j in 0..3 {
input_image[4 * i + j] = ((input_image[4 * i + j] as f32)
* (input_image[4 * i + 3] as f32)
/ 255.0) as u8;
}
}
}
let mut resizer = resize::new(
input_width,
input_height,
output_width,
output_height,
RGBA,
typ,
);
let mut output_image = Vec::<u8>::with_capacity(num_output_pixels * 4);
output_image.resize(num_output_pixels * 4, 0);
resizer.resize(input_image.as_slice(), output_image.as_mut_slice());
if premultiply {
for i in 0..num_output_pixels {
for j in 0..3 {
// We dont need to worry about division by zero, as division by zero
// is well-defined on floats to return `±Inf`. ±Inf is converted to 0
// when casting to integers.
output_image[4 * i + j] = ((output_image[4 * i + j] as f32) * 255.0
/ (output_image[4 * i + 3] as f32))
as u8;
}
}
}
return output_image;
}

View File

@@ -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() {}
}
}

2
codecs/rotate/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
target
Cargo.lock

14
codecs/rotate/Cargo.toml Normal file
View File

@@ -0,0 +1,14 @@
[package]
name = "rotate"
version = "0.1.0"
authors = ["Surma <surma@google.com>"]
edition = "2018"
[lib]
name = "rotate"
path = "rotate.rs"
crate-type = ["cdylib", "rlib"]
[profile.release]
lto = true
opt-level = "s"

View File

@@ -0,0 +1,45 @@
// THIS IS NOT A NODE SCRIPT
// This is a d8 script. Please install jsvu[1] and install v8.
// Then run `npm run --silent benchmark`.
// [1]: https://github.com/GoogleChromeLabs/jsvu
async function init() {
// Adjustable constants.
const imageDimensions = 4096;
const iterations = new Array(100);
// Constants. Dont change.
const imageByteSize = imageDimensions * imageDimensions * 4;
const wasmPageSize = 64 * 1024;
const buffer = readbuffer("rotate.wasm");
const { instance } = await WebAssembly.instantiate(buffer);
const pagesAvailable = Math.floor(
instance.exports.memory.buffer.byteLength / wasmPageSize
);
const pagesNeeded = Math.floor((imageByteSize * 2 + 4) / wasmPageSize) + 1;
const additionalPagesNeeded = pagesNeeded - pagesAvailable;
if (additionalPagesNeeded > 0) {
instance.exports.memory.grow(additionalPagesNeeded);
}
[0, 90, 180, 270].forEach(rotation => {
print(`\n${rotation} degrees`);
print(`==============================`);
for (let i = 0; i < 100; i++) {
const start = Date.now();
instance.exports.rotate(imageDimensions, imageDimensions, rotation);
iterations[i] = Date.now() - start;
}
const average = iterations.reduce((sum, c) => sum + c) / iterations.length;
const stddev = Math.sqrt(
iterations
.map(i => Math.pow(i - average, 2))
.reduce((sum, c) => sum + c) / iterations.length
);
print(`n = ${iterations.length}`);
print(`Average: ${average}`);
print(`StdDev: ${stddev}`);
});
}
init().catch(e => console.error(e.stack));

View File

@@ -7,12 +7,11 @@ echo "Compiling wasm"
echo "============================================="
(
rustup run nightly \
rustc \
--target=wasm32-unknown-unknown \
-C opt-level=3 \
-o rotate.wasm \
rotate.rs
wasm-strip rotate.wasm
cargo build \
--target wasm32-unknown-unknown \
--release
cp target/wasm32-unknown-unknown/release/rotate.wasm .
wasm-strip rotate.wasm
)
echo "============================================="
echo "Compiling wasm done"

View File

@@ -2,6 +2,10 @@
"name": "rotate",
"scripts": {
"build:image": "docker build -t squoosh-rotate .",
"build": "docker run --rm -v $(pwd):/src squoosh-rotate ./build.sh"
"build": "docker run --rm -v $(pwd):/src squoosh-rotate ./build.sh",
"benchmark": "echo File size after gzip && npm run benchmark:filesize && echo Optimizing && npm run -s benchmark:optimizing",
"benchmark:baseline": "v8 --liftoff --no-wasm-tier-up --no-opt ./benchmark.js",
"benchmark:optimizing": "v8 --no-liftoff --no-wasm-tier-up ./benchmark.js",
"benchmark:filesize": "cat rotate.wasm | gzip -c9n | wc -c"
}
}

View File

@@ -1,81 +1,113 @@
#![no_std]
#![no_main]
use std::slice::{from_raw_parts, from_raw_parts_mut};
use core::panic::PanicInfo;
use core::slice::from_raw_parts_mut;
// This function is taken from Zachary Dremann
// https://github.com/GoogleChromeLabs/squoosh/pull/462
trait HardUnwrap<T> {
fn unwrap_hard(self) -> T;
}
impl<T> HardUnwrap<T> for Option<T> {
#[cfg(not(debug_assertions))]
#[inline]
fn unwrap_hard(self) -> T {
match self {
Some(t) => t,
None => std::process::abort(),
}
}
#[cfg(debug_assertions)]
fn unwrap_hard(self) -> T {
self.unwrap()
}
}
const TILE_SIZE: usize = 16;
fn get_buffers<'a>(width: usize, height: usize) -> (&'a [u32], &'a mut [u32]) {
let num_pixels = width * height;
let in_b: &[u32];
let out_b: &mut [u32];
unsafe {
in_b = from_raw_parts::<u32>(8 as *const u32, num_pixels);
out_b = from_raw_parts_mut::<u32>((num_pixels * 4 + 8) as *mut u32, num_pixels);
}
return (in_b, out_b);
}
#[inline(never)]
fn rotate_0(width: usize, height: usize) {
let (in_b, out_b) = get_buffers(width, height);
for (in_p, out_p) in in_b.iter().zip(out_b.iter_mut()) {
*out_p = *in_p;
}
}
#[inline(never)]
fn rotate_90(width: usize, height: usize) {
let (in_b, out_b) = get_buffers(width, height);
let new_width = height;
let _new_height = width;
for y_start in (0..height).step_by(TILE_SIZE) {
for x_start in (0..width).step_by(TILE_SIZE) {
for y in y_start..(y_start + TILE_SIZE).min(height) {
let in_offset = y * width;
let in_bounds = if x_start + TILE_SIZE < width {
(in_offset + x_start)..(in_offset + x_start + TILE_SIZE)
} else {
(in_offset + x_start)..(in_offset + width)
};
let in_chunk = in_b.get(in_bounds).unwrap_hard();
for (x, in_p) in in_chunk.iter().enumerate() {
let new_x = (new_width - 1) - y;
let new_y = x + x_start;
*out_b.get_mut(new_y * new_width + new_x).unwrap_hard() = *in_p;
}
}
}
}
}
#[inline(never)]
fn rotate_180(width: usize, height: usize) {
let (in_b, out_b) = get_buffers(width, height);
for (in_p, out_p) in in_b.iter().zip(out_b.iter_mut().rev()) {
*out_p = *in_p;
}
}
#[inline(never)]
fn rotate_270(width: usize, height: usize) {
let (in_b, out_b) = get_buffers(width, height);
let new_width = height;
let new_height = width;
for y_start in (0..height).step_by(TILE_SIZE) {
for x_start in (0..width).step_by(TILE_SIZE) {
for y in y_start..(y_start + TILE_SIZE).min(height) {
let in_offset = y * width;
let in_bounds = if x_start + TILE_SIZE < width {
(in_offset + x_start)..(in_offset + x_start + TILE_SIZE)
} else {
(in_offset + x_start)..(in_offset + width)
};
let in_chunk = in_b.get(in_bounds).unwrap_hard();
for (x, in_p) in in_chunk.iter().enumerate() {
let new_x = y;
let new_y = new_height - 1 - (x_start + x);
*out_b.get_mut(new_y * new_width + new_x).unwrap_hard() = *in_p;
}
}
}
}
}
#[no_mangle]
fn rotate(input_width: isize, input_height: isize, rotate: isize) {
let mut i = 0isize;
// In the straight-copy case, d1 is x, d2 is y.
// x starts at 0 and increases.
// y starts at 0 and increases.
let mut d1_start: isize = 0;
let mut d1_limit: isize = input_width;
let mut d1_advance: isize = 1;
let mut d1_multiplier: isize = 1;
let mut d2_start: isize = 0;
let mut d2_limit: isize = input_height;
let mut d2_advance: isize = 1;
let mut d2_multiplier: isize = input_width;
if rotate == 90 {
// d1 is y, d2 is x.
// y starts at its max value and decreases.
// x starts at 0 and increases.
d1_start = input_height - 1;
d1_limit = input_height;
d1_advance = -1;
d1_multiplier = input_width;
d2_start = 0;
d2_limit = input_width;
d2_advance = 1;
d2_multiplier = 1;
} else if rotate == 180 {
// d1 is x, d2 is y.
// x starts at its max and decreases.
// y starts at its max and decreases.
d1_start = input_width - 1;
d1_limit = input_width;
d1_advance = -1;
d1_multiplier = 1;
d2_start = input_height - 1;
d2_limit = input_height;
d2_advance = -1;
d2_multiplier = input_width;
} else if rotate == 270 {
// d1 is y, d2 is x.
// y starts at 0 and increases.
// x starts at its max and decreases.
d1_start = 0;
d1_limit = input_height;
d1_advance = 1;
d1_multiplier = input_width;
d2_start = input_width - 1;
d2_limit = input_width;
d2_advance = -1;
d2_multiplier = 1;
}
let num_pixels = (input_width * input_height) as usize;
let in_b: &mut [u32];
let out_b: &mut [u32];
unsafe {
in_b = from_raw_parts_mut::<u32>(4 as *mut u32, num_pixels);
out_b = from_raw_parts_mut::<u32>((input_width * input_height * 4 + 4) as *mut u32, num_pixels);
}
for d2 in 0..d2_limit {
for d1 in 0..d1_limit {
let in_idx = (d1_start + d1 * d1_advance) * d1_multiplier + (d2_start + d2 * d2_advance) * d2_multiplier;
out_b[i as usize] = in_b[in_idx as usize];
i += 1;
fn rotate(width: usize, height: usize, rotate: usize) {
match rotate {
0 => rotate_0(width, height),
90 => rotate_90(width, height),
180 => rotate_180(width, height),
270 => rotate_270(width, height),
_ => std::process::abort(),
}
}
}
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}

Binary file not shown.

View File

@@ -22,6 +22,7 @@ function fetchTravisBuildInfo(user, repo, branch) {
'branch.name': branch,
state: 'passed',
limit: 1,
event_type: 'push',
}).then(r => r.json());
}
@@ -32,9 +33,7 @@ function fetchTravisText(path) {
/**
* Recursively-read a directory and turn it into an array of { name, size, gzipSize }
*/
async function dirToInfoArray(startPath, {
namePrefix = '',
} = {}) {
async function dirToInfoArray(startPath) {
const results = await new Promise((resolve, reject) => {
readdirp({ root: startPath }, (err, results) => {
if (err) reject(err); else resolve(results);

1042
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,12 +1,12 @@
{
"private": true,
"name": "squoosh",
"version": "1.3.2",
"version": "1.5.0",
"license": "apache-2.0",
"scripts": {
"start": "webpack-dev-server --host 0.0.0.0 --hot",
"build": "webpack -p",
"lint": "tslint -c tslint.json -p tsconfig.json -t verbose 'src/**/*.{ts,tsx,js,jsx}'",
"lint": "tslint -c tslint.json -p tsconfig.json -t verbose",
"lintfix": "tslint -c tslint.json -p tsconfig.json -t verbose --fix 'src/**/*.{ts,tsx,js,jsx}'",
"sizereport": "node config/size-report.js"
},
@@ -16,24 +16,24 @@
}
},
"devDependencies": {
"@types/node": "10.12.23",
"@types/node": "10.12.30",
"@types/pretty-bytes": "5.1.0",
"@types/webassembly-js-api": "0.0.2",
"@webcomponents/custom-elements": "1.2.1",
"@webpack-cli/serve": "0.1.3",
"assets-webpack-plugin": "3.9.7",
"chokidar": "2.1.0",
"assets-webpack-plugin": "3.9.10",
"chokidar": "2.1.2",
"chalk": "2.4.2",
"classnames": "2.2.6",
"clean-webpack-plugin": "1.0.1",
"comlink": "3.1.1",
"copy-webpack-plugin": "4.6.0",
"critters-webpack-plugin": "2.2.0",
"copy-webpack-plugin": "5.0.0",
"critters-webpack-plugin": "2.3.0",
"css-loader": "1.0.1",
"ejs": "2.6.1",
"escape-string-regexp": "1.0.5",
"exports-loader": "0.7.0",
"file-drop-element": "0.0.9",
"file-drop-element": "0.2.0",
"file-loader": "3.0.1",
"gzip-size": "5.0.0",
"html-webpack-plugin": "3.2.0",
@@ -48,7 +48,7 @@
"optimize-css-assets-webpack-plugin": "5.0.1",
"pointer-tracker": "2.0.3",
"preact": "8.4.2",
"prerender-loader": "1.2.0",
"prerender-loader": "1.3.0",
"pretty-bytes": "5.1.0",
"progress-bar-webpack-plugin": "1.12.1",
"raw-loader": "1.0.0",
@@ -57,19 +57,19 @@
"script-ext-html-webpack-plugin": "2.1.3",
"source-map-loader": "0.2.4",
"style-loader": "0.23.1",
"terser-webpack-plugin": "1.2.2",
"terser-webpack-plugin": "1.2.3",
"ts-loader": "5.3.3",
"tslint": "5.12.1",
"tslint": "5.13.1",
"tslint-config-airbnb": "5.11.1",
"tslint-config-semistandard": "7.0.0",
"tslint-react": "3.6.0",
"typed-css-modules": "0.3.7",
"typescript": "3.2.4",
"typed-css-modules": "0.4.1",
"typescript": "3.3.3333",
"url-loader": "1.1.2",
"webpack": "4.28.0",
"webpack-bundle-analyzer": "3.0.3",
"webpack-bundle-analyzer": "3.1.0",
"webpack-cli": "3.2.3",
"webpack-dev-server": "3.1.14",
"worker-plugin": "3.0.0"
"webpack-dev-server": "3.2.1",
"worker-plugin": "3.1.0"
}
}

View File

@@ -1,12 +1,12 @@
import imagequant, { QuantizerModule } from '../../../codecs/imagequant/imagequant';
import wasmUrl from '../../../codecs/imagequant/imagequant.wasm';
import { QuantizeOptions } from './processor-meta';
import { initWasmModule } from '../util';
import { initEmscriptenModule } from '../util';
let emscriptenModule: Promise<QuantizerModule>;
export async function process(data: ImageData, opts: QuantizeOptions): Promise<ImageData> {
if (!emscriptenModule) emscriptenModule = initWasmModule(imagequant, wasmUrl);
if (!emscriptenModule) emscriptenModule = initEmscriptenModule(imagequant, wasmUrl);
const module = await emscriptenModule;

View File

@@ -1,12 +1,12 @@
import mozjpeg_enc, { MozJPEGModule } from '../../../codecs/mozjpeg_enc/mozjpeg_enc';
import wasmUrl from '../../../codecs/mozjpeg_enc/mozjpeg_enc.wasm';
import { EncodeOptions } from './encoder-meta';
import { initWasmModule } from '../util';
import { initEmscriptenModule } from '../util';
let emscriptenModule: Promise<MozJPEGModule>;
export async function encode(data: ImageData, options: EncodeOptions): Promise<ArrayBuffer> {
if (!emscriptenModule) emscriptenModule = initWasmModule(mozjpeg_enc, wasmUrl);
if (!emscriptenModule) emscriptenModule = initEmscriptenModule(mozjpeg_enc, wasmUrl);
const module = await emscriptenModule;
const resultView = module.encode(data.data, data.width, data.height, options);

View File

@@ -1,12 +1,12 @@
import optipng, { OptiPngModule } from '../../../codecs/optipng/optipng';
import wasmUrl from '../../../codecs/optipng/optipng.wasm';
import { EncodeOptions } from './encoder-meta';
import { initWasmModule } from '../util';
import { initEmscriptenModule } from '../util';
let emscriptenModule: Promise<OptiPngModule>;
export async function compress(data: BufferSource, options: EncodeOptions): Promise<ArrayBuffer> {
if (!emscriptenModule) emscriptenModule = initWasmModule(optipng, wasmUrl);
if (!emscriptenModule) emscriptenModule = initEmscriptenModule(optipng, wasmUrl);
const module = await emscriptenModule;
const resultView = module.compress(data, options);

View File

@@ -5,8 +5,7 @@ async function mozjpegEncode(
): Promise<ArrayBuffer> {
const { encode } = await import(
/* webpackChunkName: "process-mozjpeg-enc" */
'../mozjpeg/encoder',
);
'../mozjpeg/encoder');
return encode(data, options);
}
@@ -15,8 +14,7 @@ async function quantize(
): Promise<ImageData> {
const { process } = await import(
/* webpackChunkName: "process-imagequant" */
'../imagequant/processor',
);
'../imagequant/processor');
return process(data, opts);
}
@@ -25,19 +23,27 @@ async function rotate(
): Promise<ImageData> {
const { rotate } = await import(
/* webpackChunkName: "process-rotate" */
'../rotate/processor',
);
'../rotate/processor');
return rotate(data, opts);
}
async function resize(
data: ImageData, opts: import('../resize/processor-meta').WorkerResizeOptions,
): Promise<ImageData> {
const { resize } = await import(
/* webpackChunkName: "process-resize" */
'../resize/processor');
return resize(data, opts);
}
async function optiPngEncode(
data: BufferSource, options: import('../optipng/encoder-meta').EncodeOptions,
): Promise<ArrayBuffer> {
const { compress } = await import(
/* webpackChunkName: "process-optipng" */
'../optipng/encoder',
);
'../optipng/encoder');
return compress(data, options);
}
@@ -46,20 +52,18 @@ async function webpEncode(
): Promise<ArrayBuffer> {
const { encode } = await import(
/* webpackChunkName: "process-webp-enc" */
'../webp/encoder',
);
'../webp/encoder');
return encode(data, options);
}
async function webpDecode(data: ArrayBuffer): Promise<ImageData> {
const { decode } = await import(
/* webpackChunkName: "process-webp-dec" */
'../webp/decoder',
);
'../webp/decoder');
return decode(data);
}
const exports = { mozjpegEncode, quantize, rotate, optiPngEncode, webpEncode, webpDecode };
const exports = { mozjpegEncode, quantize, rotate, resize, optiPngEncode, webpEncode, webpDecode };
export type ProcessorWorkerApi = typeof exports;
expose(exports, self);

View File

@@ -6,8 +6,8 @@ import { EncodeOptions as OptiPNGEncoderOptions } from './optipng/encoder-meta';
import { EncodeOptions as WebPEncoderOptions } from './webp/encoder-meta';
import { EncodeOptions as BrowserJPEGOptions } from './browser-jpeg/encoder-meta';
import { EncodeOptions as BrowserWebpEncodeOptions } from './browser-webp/encoder-meta';
import { BitmapResizeOptions, VectorResizeOptions } from './resize/processor-meta';
import { resize, vectorResize } from './resize/processor';
import { BrowserResizeOptions, VectorResizeOptions } from './resize/processor-meta';
import { browserResize, vectorResize } from './resize/processor-sync';
import * as browserBMP from './browser-bmp/encoder';
import * as browserPNG from './browser-png/encoder';
import * as browserJPEG from './browser-jpeg/encoder';
@@ -130,6 +130,13 @@ export default class Processor {
return this._workerApi!.rotate(data, opts);
}
@Processor._processingJob({ needsWorker: true })
workerResize(
data: ImageData, opts: import('./resize/processor-meta').WorkerResizeOptions,
): Promise<ImageData> {
return this._workerApi!.resize(data, opts);
}
@Processor._processingJob({ needsWorker: true })
mozjpegEncode(
data: ImageData, opts: MozJPEGEncoderOptions,
@@ -202,9 +209,9 @@ export default class Processor {
// Synchronous jobs
resize(data: ImageData, opts: BitmapResizeOptions) {
resize(data: ImageData, opts: BrowserResizeOptions) {
this.abortCurrent();
return resize(data, opts);
return browserResize(data, opts);
}
vectorResize(data: HTMLImageElement, opts: VectorResizeOptions) {

View File

@@ -1,8 +1,10 @@
import { h, Component } from 'preact';
import linkState from 'linkstate';
import { bind, linkRef } from '../../lib/initial-util';
import { inputFieldValueAsNumber, inputFieldValue, preventDefault } from '../../lib/util';
import { ResizeOptions } from './processor-meta';
import {
inputFieldValueAsNumber, inputFieldValue, preventDefault, inputFieldChecked,
} from '../../lib/util';
import { ResizeOptions, isWorkerOptions } from './processor-meta';
import * as style from '../../components/Options/style.scss';
import Checkbox from '../../components/checkbox';
import Expander from '../../components/expander';
@@ -17,11 +19,13 @@ interface Props {
interface State {
maintainAspect: boolean;
premultiply: boolean;
}
export default class ResizerOptions extends Component<Props, State> {
state: State = {
maintainAspect: true,
premultiply: true,
};
form?: HTMLFormElement;
@@ -38,6 +42,7 @@ export default class ResizerOptions extends Component<Props, State> {
width: inputFieldValueAsNumber(width),
height: inputFieldValueAsNumber(height),
method: form.resizeMethod.value,
premultiply: inputFieldChecked(form.premultiply, true),
// Casting, as the formfield only returns the correct values.
fitMethod: inputFieldValue(form.fitMethod, options.fitMethod) as ResizeOptions['fitMethod'],
};
@@ -87,6 +92,10 @@ export default class ResizerOptions extends Component<Props, State> {
onChange={this.onChange}
>
{isVector && <option value="vector">Vector</option>}
<option value="lanczos3">Lanczos3</option>
<option value="mitchell">Mitchell</option>
<option value="catrom">Catmull-Rom</option>
<option value="triangle">Triangle</option>
<option value="browser-pixelated">Browser pixelated</option>
<option value="browser-low">Browser low quality</option>
<option value="browser-medium">Browser medium quality</option>
@@ -117,6 +126,19 @@ export default class ResizerOptions extends Component<Props, State> {
onInput={this.onHeightInput}
/>
</label>
<Expander>
{isWorkerOptions(options) ?
<label class={style.optionInputFirst}>
<Checkbox
name="premultiply"
checked={options.premultiply}
onChange={this.onChange}
/>
Premultiply alpha channel
</label>
: null
}
</Expander>
<label class={style.optionInputFirst}>
<Checkbox
name="maintainAspect"

View File

@@ -1,26 +1,44 @@
type BitmapResizeMethods = 'browser-pixelated' | 'browser-low' | 'browser-medium' | 'browser-high';
type BrowserResizeMethods = 'browser-pixelated' | 'browser-low' | 'browser-medium' | 'browser-high';
type WorkerResizeMethods = 'triangle' | 'catrom' | 'mitchell' | 'lanczos3';
const workerResizeMethods: WorkerResizeMethods[] = ['triangle', 'catrom', 'mitchell', 'lanczos3'];
export interface ResizeOptions {
export type ResizeOptions = BrowserResizeOptions | WorkerResizeOptions | VectorResizeOptions;
export interface ResizeOptionsCommon {
width: number;
height: number;
method: 'vector' | BitmapResizeMethods;
fitMethod: 'stretch' | 'contain';
}
export interface BitmapResizeOptions extends ResizeOptions {
method: BitmapResizeMethods;
export interface BrowserResizeOptions extends ResizeOptionsCommon {
method: BrowserResizeMethods;
}
export interface VectorResizeOptions extends ResizeOptions {
export interface WorkerResizeOptions extends ResizeOptionsCommon {
method: WorkerResizeMethods;
premultiply: boolean;
}
export interface VectorResizeOptions extends ResizeOptionsCommon {
method: 'vector';
}
/**
* Return whether a set of options are worker resize options.
*
* @param opts
*/
export function isWorkerOptions(opts: ResizeOptions): opts is WorkerResizeOptions {
return (workerResizeMethods as string[]).includes(opts.method);
}
export const defaultOptions: ResizeOptions = {
// Width and height will always default to the image size.
// This is set elsewhere.
width: 1,
height: 1,
// This will be set to 'vector' if the input is SVG.
method: 'browser-high',
method: 'lanczos3',
fitMethod: 'stretch',
premultiply: true,
};

View File

@@ -0,0 +1,35 @@
import { nativeResize, NativeResizeMethod, drawableToImageData } from '../../lib/util';
import { BrowserResizeOptions, VectorResizeOptions } from './processor-meta';
import { getContainOffsets } from './util';
export function browserResize(data: ImageData, opts: BrowserResizeOptions): ImageData {
let sx = 0;
let sy = 0;
let sw = data.width;
let sh = data.height;
if (opts.fitMethod === 'contain') {
({ sx, sy, sw, sh } = getContainOffsets(sw, sh, opts.width, opts.height));
}
return nativeResize(
data, sx, sy, sw, sh, opts.width, opts.height,
opts.method.slice('browser-'.length) as NativeResizeMethod,
);
}
export function vectorResize(data: HTMLImageElement, opts: VectorResizeOptions): ImageData {
let sx = 0;
let sy = 0;
let sw = data.width;
let sh = data.height;
if (opts.fitMethod === 'contain') {
({ sx, sy, sw, sh } = getContainOffsets(sw, sh, opts.width, opts.height));
}
return drawableToImageData(data, {
sx, sy, sw, sh,
width: opts.width, height: opts.height,
});
}

View File

@@ -1,49 +1,52 @@
import { nativeResize, NativeResizeMethod, drawableToImageData } from '../../lib/util';
import { BitmapResizeOptions, VectorResizeOptions } from './processor-meta';
import wasmUrl from '../../../codecs/resize/pkg/resize_bg.wasm';
import '../../../codecs/resize/pkg/resize';
import { WorkerResizeOptions } from './processor-meta';
import { getContainOffsets } from './util';
function getContainOffsets(sw: number, sh: number, dw: number, dh: number) {
const currentAspect = sw / sh;
const endAspect = dw / dh;
if (endAspect > currentAspect) {
const newSh = sw / endAspect;
const newSy = (sh - newSh) / 2;
return { sw, sh: newSh, sx: 0, sy: newSy };
}
const newSw = sh * endAspect;
const newSx = (sw - newSw) / 2;
return { sh, sw: newSw, sx: newSx, sy: 0 };
interface WasmBindgenExports {
resize: typeof import('../../../codecs/resize/pkg/resize').resize;
}
export function resize(data: ImageData, opts: BitmapResizeOptions): ImageData {
let sx = 0;
let sy = 0;
let sw = data.width;
let sh = data.height;
type WasmBindgen = ((url: string) => Promise<void>) & WasmBindgenExports;
if (opts.fitMethod === 'contain') {
({ sx, sy, sw, sh } = getContainOffsets(sw, sh, opts.width, opts.height));
declare var wasm_bindgen: WasmBindgen;
const ready = wasm_bindgen(wasmUrl);
function crop(data: ImageData, sx: number, sy: number, sw: number, sh: number): ImageData {
const inputPixels = new Uint32Array(data.data.buffer);
// Copy within the same buffer for speed and memory efficiency.
for (let y = 0; y < sh; y += 1) {
const start = ((y + sy) * data.width) + sx;
inputPixels.copyWithin(y * sw, start, start + sw);
}
return nativeResize(
data, sx, sy, sw, sh, opts.width, opts.height,
opts.method.slice('browser-'.length) as NativeResizeMethod,
return new ImageData(
new Uint8ClampedArray(inputPixels.buffer.slice(0, sw * sh * 4)),
sw, sh,
);
}
export function vectorResize(data: HTMLImageElement, opts: VectorResizeOptions): ImageData {
let sx = 0;
let sy = 0;
let sw = data.width;
let sh = data.height;
/** Resize methods by index */
const resizeMethods: WorkerResizeOptions['method'][] = [
'triangle', 'catrom', 'mitchell', 'lanczos3',
];
export async function resize(data: ImageData, opts: WorkerResizeOptions): Promise<ImageData> {
let input = data;
if (opts.fitMethod === 'contain') {
({ sx, sy, sw, sh } = getContainOffsets(sw, sh, opts.width, opts.height));
const { sx, sy, sw, sh } = getContainOffsets(data.width, data.height, opts.width, opts.height);
input = crop(input, Math.round(sx), Math.round(sy), Math.round(sw), Math.round(sh));
}
return drawableToImageData(data, {
sx, sy, sw, sh,
width: opts.width, height: opts.height,
});
await ready;
const result = wasm_bindgen.resize(
new Uint8Array(input.data.buffer), input.width, input.height, opts.width, opts.height,
resizeMethods.indexOf(opts.method), opts.premultiply,
);
return new ImageData(new Uint8ClampedArray(result.buffer), opts.width, opts.height);
}

14
src/codecs/resize/util.ts Normal file
View File

@@ -0,0 +1,14 @@
export function getContainOffsets(sw: number, sh: number, dw: number, dh: number) {
const currentAspect = sw / sh;
const endAspect = dw / dh;
if (endAspect > currentAspect) {
const newSh = sw / endAspect;
const newSy = (sh - newSh) / 2;
return { sw, sh: newSh, sx: 0, sy: newSy };
}
const newSw = sh * endAspect;
const newSx = (sw - newSw) / 2;
return { sh, sw: newSw, sx: newSx, sy: 0 };
}

View File

@@ -1,32 +1,42 @@
import wasmUrl from '../../../codecs/rotate/rotate.wasm';
import { RotateOptions, RotateModuleInstance } from './processor-meta';
const instancePromise = (WebAssembly as any).instantiateStreaming(fetch(wasmUrl));
// We are loading a 500B module here. Loading the code to feature-detect
// `instantiateStreaming` probably takes longer to load than the time we save by
// using `instantiateStreaming` in the first place. So lets just use
// `ArrayBuffer`s here.
const instancePromise = fetch(wasmUrl)
.then(r => r.arrayBuffer())
.then(buf => WebAssembly.instantiate(buf));
export async function rotate(
data: ImageData,
opts: RotateOptions,
): Promise<ImageData> {
const { instance } = (await instancePromise) as {instance: RotateModuleInstance};
const { instance } = (await instancePromise) as {
instance: RotateModuleInstance;
};
// Number of wasm memory pages (á 64KiB) needed to store the image twice.
const bytesPerImage = data.width * data.height * 4;
const numPagesNeeded = Math.ceil((bytesPerImage * 2 + 4) / (64 * 1024));
const numPagesNeeded = Math.ceil((bytesPerImage * 2 + 8) / (64 * 1024));
// Only count full pages, just to be safe.
const numPagesAvailable = Math.floor(instance.exports.memory.buffer.byteLength / (64 * 1024));
const numPagesAvailable = Math.floor(
instance.exports.memory.buffer.byteLength / (64 * 1024),
);
const additionalPagesToAllocate = numPagesNeeded - numPagesAvailable;
if (additionalPagesToAllocate > 0) {
instance.exports.memory.grow(additionalPagesToAllocate);
}
const view = new Uint8ClampedArray(instance.exports.memory.buffer);
view.set(data.data, 4);
view.set(data.data, 8);
instance.exports.rotate(data.width, data.height, opts.rotate);
const flipDimensions = opts.rotate % 180 !== 0;
return new ImageData(
view.slice(bytesPerImage + 4, bytesPerImage * 2 + 4),
view.slice(bytesPerImage + 8, bytesPerImage * 2 + 8),
flipDimensions ? data.height : data.width,
flipDimensions ? data.width : data.height,
);

View File

@@ -2,7 +2,7 @@ type ModuleFactory<M extends EmscriptenWasm.Module> = (
opts: EmscriptenWasm.ModuleOpts,
) => M;
export function initWasmModule<T extends EmscriptenWasm.Module>(
export function initEmscriptenModule<T extends EmscriptenWasm.Module>(
moduleFactory: ModuleFactory<T>,
wasmUrl: string,
): Promise<T> {

View File

@@ -1,11 +1,11 @@
import webp_dec, { WebPModule } from '../../../codecs/webp_dec/webp_dec';
import wasmUrl from '../../../codecs/webp_dec/webp_dec.wasm';
import { initWasmModule } from '../util';
import { initEmscriptenModule } from '../util';
let emscriptenModule: Promise<WebPModule>;
export async function decode(data: ArrayBuffer): Promise<ImageData> {
if (!emscriptenModule) emscriptenModule = initWasmModule(webp_dec, wasmUrl);
if (!emscriptenModule) emscriptenModule = initEmscriptenModule(webp_dec, wasmUrl);
const module = await emscriptenModule;
const rawImage = module.decode(data);

View File

@@ -1,12 +1,12 @@
import webp_enc, { WebPModule } from '../../../codecs/webp_enc/webp_enc';
import wasmUrl from '../../../codecs/webp_enc/webp_enc.wasm';
import { EncodeOptions } from './encoder-meta';
import { initWasmModule } from '../util';
import { initEmscriptenModule } from '../util';
let emscriptenModule: Promise<WebPModule>;
export async function encode(data: ImageData, options: EncodeOptions): Promise<ArrayBuffer> {
if (!emscriptenModule) emscriptenModule = initWasmModule(webp_enc, wasmUrl);
if (!emscriptenModule) emscriptenModule = initEmscriptenModule(webp_enc, wasmUrl);
const module = await emscriptenModule;
const resultView = module.encode(data.data, data.width, data.height, options);

View File

@@ -13,12 +13,10 @@ const ROUTE_EDITOR = '/editor';
const compressPromise = import(
/* webpackChunkName: "main-app" */
'../compress',
);
'../compress');
const offlinerPromise = import(
/* webpackChunkName: "offliner" */
'../../lib/offliner',
);
'../../lib/offliner');
function back() {
window.history.back();
@@ -74,8 +72,9 @@ export default class App extends Component<Props, State> {
}
@bind
private onFileDrop({ file }: FileDropEvent) {
if (!file) return;
private onFileDrop({ files }: FileDropEvent) {
if (!files || files.length === 0) return;
const file = files[0];
this.openEditor();
this.setState({ file });
}

View File

@@ -18,20 +18,14 @@ import * as browserTIFF from '../../codecs/browser-tiff/encoder-meta';
import * as browserJP2 from '../../codecs/browser-jp2/encoder-meta';
import * as browserBMP from '../../codecs/browser-bmp/encoder-meta';
import * as browserPDF from '../../codecs/browser-pdf/encoder-meta';
import {
EncoderState,
EncoderType,
EncoderOptions,
encoderMap,
} from '../../codecs/encoders';
import {
PreprocessorState,
defaultPreprocessorState,
} from '../../codecs/preprocessors';
import { EncoderState, EncoderType, EncoderOptions, encoderMap } from '../../codecs/encoders';
import { PreprocessorState, defaultPreprocessorState } from '../../codecs/preprocessors';
import { decodeImage } from '../../codecs/decoders';
import { cleanMerge, cleanSet } from '../../lib/clean-modify';
import Processor from '../../codecs/processor';
import { VectorResizeOptions, BitmapResizeOptions } from '../../codecs/resize/processor-meta';
import {
BrowserResizeOptions, isWorkerOptions as isWorkerResizeOptions,
} from '../../codecs/resize/processor-meta';
import './custom-els/MultiPanel';
import Results from '../results';
import { ExpandIcon, CopyAcrossIconProps } from '../../lib/icons';
@@ -110,10 +104,12 @@ async function preprocessImage(
if (preprocessData.resize.method === 'vector' && source.vectorImage) {
result = processor.vectorResize(
source.vectorImage,
preprocessData.resize as VectorResizeOptions,
preprocessData.resize,
);
} else if (isWorkerResizeOptions(preprocessData.resize)) {
result = await processor.workerResize(result, preprocessData.resize);
} else {
result = processor.resize(result, preprocessData.resize as BitmapResizeOptions);
result = processor.resize(result, preprocessData.resize as BrowserResizeOptions);
}
}
if (preprocessData.quantizer.enabled) {
@@ -441,7 +437,7 @@ export default class Compress extends Component<Props, State> {
newState = cleanMerge(newState, `sides.${i}.latestSettings.preprocessorState.resize`, {
width: processed.width,
height: processed.height,
method: vectorImage ? 'vector' : 'browser-high',
method: vectorImage ? 'vector' : 'lanczos3',
});
}

View File

@@ -7,8 +7,7 @@ function init() {
if (!('customElements' in self)) {
import(
/* webpackChunkName: "wc-polyfill" */
'@webcomponents/custom-elements',
).then(init);
'@webcomponents/custom-elements').then(init);
} else {
init();
}