mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-12 16:57:26 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
668acf2698 | ||
|
|
7042491257 | ||
|
|
307e1f9356 |
13
.babelrc
Normal file
13
.babelrc
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
"transform-class-properties",
|
||||||
|
"transform-react-constant-elements",
|
||||||
|
"transform-react-remove-prop-types",
|
||||||
|
[
|
||||||
|
"transform-react-jsx",
|
||||||
|
{
|
||||||
|
"pragma": "h"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
||||||
5
.firebaserc
Normal file
5
.firebaserc
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"projects": {
|
||||||
|
"default": "squoosh-beta"
|
||||||
|
}
|
||||||
|
}
|
||||||
36
.github/ISSUE_TEMPLATE/bug_report.md
vendored
36
.github/ISSUE_TEMPLATE/bug_report.md
vendored
@@ -1,36 +0,0 @@
|
|||||||
---
|
|
||||||
name: Bug report
|
|
||||||
about: Something is not working as expected
|
|
||||||
labels:
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
**Before you start**
|
|
||||||
Please take a look at the [FAQ](https://github.com/GoogleChromeLabs/squoosh/wiki/FAQ) as well as the already opened issues! If nothing fits your problem, go ahead and fill out the following template:
|
|
||||||
|
|
||||||
**Describe the bug**
|
|
||||||
A clear and concise description of what the bug is.
|
|
||||||
|
|
||||||
**To Reproduce**
|
|
||||||
Steps to reproduce the behavior:
|
|
||||||
1. Go to '...'
|
|
||||||
2. Click on '....'
|
|
||||||
3. Scroll down to '....'
|
|
||||||
4. See error
|
|
||||||
|
|
||||||
**Expected behavior**
|
|
||||||
A clear and concise description of what you expected to happen.
|
|
||||||
|
|
||||||
**Version:**
|
|
||||||
- OS w/ version: [e.g. iOS 12]
|
|
||||||
- Browser w/ version [e.g. Chrome 70]
|
|
||||||
- Node version: [e.g. 10.11.0]
|
|
||||||
- npm version: [e.g. 6.4.1]
|
|
||||||
|
|
||||||
**Is your issue related to the quality of image compression?**
|
|
||||||
Please attach original and output images (you can drag & drop to attach).
|
|
||||||
- Original image
|
|
||||||
- Output image from Squoosh
|
|
||||||
|
|
||||||
**Additional context, screenshots, screencasts**
|
|
||||||
Add any other context about the problem here.
|
|
||||||
18
.github/ISSUE_TEMPLATE/feature_request.md
vendored
18
.github/ISSUE_TEMPLATE/feature_request.md
vendored
@@ -1,18 +0,0 @@
|
|||||||
---
|
|
||||||
name: Feature request
|
|
||||||
about: Suggest an idea for this project
|
|
||||||
labels:
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
**Is your feature request related to a problem? Please describe.**
|
|
||||||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
|
||||||
|
|
||||||
**Describe the solution you'd like**
|
|
||||||
A clear and concise description of what you want to happen.
|
|
||||||
|
|
||||||
**Does other service/app have this feature?**
|
|
||||||
Add any service you know/use that has this feature (We want to know for research)
|
|
||||||
|
|
||||||
**Additional context**
|
|
||||||
Add any other context or screenshots about the feature request here.
|
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,4 +3,3 @@ node_modules
|
|||||||
/*.log
|
/*.log
|
||||||
*.scss.d.ts
|
*.scss.d.ts
|
||||||
*.css.d.ts
|
*.css.d.ts
|
||||||
*.o
|
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
language: node_js
|
|
||||||
cache: npm
|
|
||||||
script: npm run build
|
|
||||||
after_success: npm run sizereport
|
|
||||||
20
Dockerfile
Normal file
20
Dockerfile
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
FROM selenium/node-chrome:latest
|
||||||
|
|
||||||
|
USER root
|
||||||
|
|
||||||
|
RUN apt-get update -qqy \
|
||||||
|
&& apt-get -qqy install python git build-essential \
|
||||||
|
&& rm -rf /var/lib/apt/lists/* /var/cache/apt/* \
|
||||||
|
&& rm /bin/sh && ln -s /bin/bash /bin/sh \
|
||||||
|
&& chown seluser /usr/local
|
||||||
|
|
||||||
|
ENV NVM_DIR /usr/local/nvm
|
||||||
|
RUN wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash \
|
||||||
|
&& source $NVM_DIR/nvm.sh \
|
||||||
|
&& nvm install v8
|
||||||
|
|
||||||
|
ENV CHROME_BIN /opt/google/chrome/chrome
|
||||||
|
ENV INSIDE_DOCKER=1
|
||||||
|
|
||||||
|
WORKDIR /usr/src
|
||||||
|
ENTRYPOINT source $NVM_DIR/nvm.sh && npm i && npm test
|
||||||
32
README.md
32
README.md
@@ -1,31 +1,5 @@
|
|||||||
# [Squoosh]!
|
# Squoosh!
|
||||||
|
|
||||||
[Squoosh] is an image compression web app that allows you to dive into the advanced options provided
|
Squoosh will be an image compression web app that allows you to dive into the
|
||||||
by various image compressors.
|
advanced options provided by various image compressors.
|
||||||
|
|
||||||
# Privacy
|
|
||||||
|
|
||||||
Google Analytics is used to record the following:
|
|
||||||
|
|
||||||
* [Basic visit data](https://support.google.com/analytics/answer/6004245?ref_topic=2919631).
|
|
||||||
* Before and after image size once an image is downloaded. These values are rounded to the nearest
|
|
||||||
kilobyte.
|
|
||||||
|
|
||||||
Image compression is handled locally; no additional data is sent to the server.
|
|
||||||
|
|
||||||
# Building locally
|
|
||||||
|
|
||||||
Clone the repo, and:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
npm install
|
|
||||||
npm run build
|
|
||||||
```
|
|
||||||
|
|
||||||
You can run the development server with:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
npm start
|
|
||||||
```
|
|
||||||
|
|
||||||
[Squoosh]: https://squoosh.app
|
|
||||||
|
|||||||
18
_headers.ejs
18
_headers.ejs
@@ -1,18 +0,0 @@
|
|||||||
# Long-term cache by default.
|
|
||||||
/*
|
|
||||||
Cache-Control: max-age=31536000
|
|
||||||
|
|
||||||
# And here are the exceptions:
|
|
||||||
/
|
|
||||||
Cache-Control: no-cache
|
|
||||||
|
|
||||||
/serviceworker.js
|
|
||||||
Cache-Control: no-cache
|
|
||||||
|
|
||||||
/manifest.json
|
|
||||||
Cache-Control: must-revalidate, max-age=3600
|
|
||||||
|
|
||||||
# URLs in /assets do not include a hash and are mutable.
|
|
||||||
# But it isn't a big deal if the user gets an old version.
|
|
||||||
/assets/*
|
|
||||||
Cache-Control: must-revalidate, max-age=3600
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
/index.html / 301
|
|
||||||
/* /index.html 301
|
|
||||||
@@ -11,6 +11,6 @@ $ npm install
|
|||||||
$ npm run build
|
$ npm run build
|
||||||
```
|
```
|
||||||
|
|
||||||
This will build two files: `<codec name>_<enc or dec>.js` and `<codec name>_<enc or dec>.wasm`. It will most likely be necessary to set [`Module["locateFile"]`](https://kripken.github.io/emscripten-site/docs/api_reference/module.html#affecting-execution) to successfully load the `.wasm` file. When the `.js` file is loaded, a global `<codec name>_<enc or dec>` is created with the same API as an [Emscripten `Module`](https://kripken.github.io/emscripten-site/docs/api_reference/module.html).
|
This will build two files: `<codec name>_<enc or dec>.js` and `<codec name>_<enc or dec>.wasm`. It will most likely be necessary to set [`Module["locateFile"]`](https://kripken.github.io/emscripten-site/docs/api_reference/module.html#affecting-execution) to sucessfully load the `.wasm` file. When the `.js` file is loaded, a global `<codec name>_<enc or dec>` is created with the same API as an [Emscripten `Module`](https://kripken.github.io/emscripten-site/docs/api_reference/module.html).
|
||||||
|
|
||||||
Each codec will document its API in its README.
|
Each codec will document its API in its README.
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 74 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 239 KiB |
5
codecs/hqx/.gitignore
vendored
5
codecs/hqx/.gitignore
vendored
@@ -1,5 +0,0 @@
|
|||||||
**/*.rs.bk
|
|
||||||
target
|
|
||||||
Cargo.lock
|
|
||||||
bin/
|
|
||||||
pkg/README.md
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "squooshhqx"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["Surma <surma@surma.link>"]
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
crate-type = ["cdylib"]
|
|
||||||
|
|
||||||
[features]
|
|
||||||
default = ["console_error_panic_hook", "wee_alloc"]
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
cfg-if = "0.1.2"
|
|
||||||
wasm-bindgen = "0.2.38"
|
|
||||||
# lazy_static = "1.0.0"
|
|
||||||
hqx = {git = "https://github.com/CryZe/wasmboy-rs", tag="v0.1.2"}
|
|
||||||
|
|
||||||
# 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
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
FROM rust
|
|
||||||
RUN rustup target add wasm32-unknown-unknown
|
|
||||||
RUN curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
|
|
||||||
|
|
||||||
RUN mkdir /opt/binaryen && \
|
|
||||||
curl -L https://github.com/WebAssembly/binaryen/releases/download/1.38.32/binaryen-1.38.32-x86-linux.tar.gz | tar -xzf - -C /opt/binaryen --strip 1
|
|
||||||
|
|
||||||
RUN mkdir /opt/wabt && \
|
|
||||||
curl -L https://github.com/WebAssembly/wabt/releases/download/1.0.11/wabt-1.0.11-linux.tar.gz | tar -xzf - -C /opt/wabt --strip 1
|
|
||||||
|
|
||||||
ENV PATH="/opt/binaryen:/opt/wabt:${PATH}"
|
|
||||||
WORKDIR /src
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling wasm"
|
|
||||||
echo "============================================="
|
|
||||||
(
|
|
||||||
wasm-pack build
|
|
||||||
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"
|
|
||||||
wasm-opt -Os --no-validation -o pkg/squooshhqx_bg.wasm pkg/squooshhqx_bg.wasm
|
|
||||||
rm pkg/.gitignore
|
|
||||||
)
|
|
||||||
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-hqx .\`"
|
|
||||||
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
||||||
4
codecs/hqx/package-lock.json
generated
4
codecs/hqx/package-lock.json
generated
@@ -1,4 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "hqx",
|
|
||||||
"lockfileVersion": 1
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "hqx",
|
|
||||||
"scripts": {
|
|
||||||
"build:image": "docker build -t squoosh-hqx .",
|
|
||||||
"build": "docker run --rm -v $(pwd):/src squoosh-hqx ./build.sh"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "squooshhqx",
|
|
||||||
"collaborators": [
|
|
||||||
"Surma <surma@surma.link>"
|
|
||||||
],
|
|
||||||
"version": "0.1.0",
|
|
||||||
"files": [
|
|
||||||
"squooshhqx_bg.wasm",
|
|
||||||
"squooshhqx.js",
|
|
||||||
"squooshhqx.d.ts"
|
|
||||||
],
|
|
||||||
"module": "squooshhqx.js",
|
|
||||||
"types": "squooshhqx.d.ts",
|
|
||||||
"sideEffects": "false"
|
|
||||||
}
|
|
||||||
9
codecs/hqx/pkg/squooshhqx.d.ts
vendored
9
codecs/hqx/pkg/squooshhqx.d.ts
vendored
@@ -1,9 +0,0 @@
|
|||||||
/* tslint:disable */
|
|
||||||
/**
|
|
||||||
* @param {Uint32Array} input_image
|
|
||||||
* @param {number} input_width
|
|
||||||
* @param {number} input_height
|
|
||||||
* @param {number} factor
|
|
||||||
* @returns {Uint32Array}
|
|
||||||
*/
|
|
||||||
export function resize(input_image: Uint32Array, input_width: number, input_height: number, factor: number): Uint32Array;
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
import * as wasm from './squooshhqx_bg.wasm';
|
|
||||||
|
|
||||||
let cachegetUint32Memory = null;
|
|
||||||
function getUint32Memory() {
|
|
||||||
if (cachegetUint32Memory === null || cachegetUint32Memory.buffer !== wasm.memory.buffer) {
|
|
||||||
cachegetUint32Memory = new Uint32Array(wasm.memory.buffer);
|
|
||||||
}
|
|
||||||
return cachegetUint32Memory;
|
|
||||||
}
|
|
||||||
|
|
||||||
let WASM_VECTOR_LEN = 0;
|
|
||||||
|
|
||||||
function passArray32ToWasm(arg) {
|
|
||||||
const ptr = wasm.__wbindgen_malloc(arg.length * 4);
|
|
||||||
getUint32Memory().set(arg, ptr / 4);
|
|
||||||
WASM_VECTOR_LEN = arg.length;
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
let cachegetInt32Memory = null;
|
|
||||||
function getInt32Memory() {
|
|
||||||
if (cachegetInt32Memory === null || cachegetInt32Memory.buffer !== wasm.memory.buffer) {
|
|
||||||
cachegetInt32Memory = new Int32Array(wasm.memory.buffer);
|
|
||||||
}
|
|
||||||
return cachegetInt32Memory;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getArrayU32FromWasm(ptr, len) {
|
|
||||||
return getUint32Memory().subarray(ptr / 4, ptr / 4 + len);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @param {Uint32Array} input_image
|
|
||||||
* @param {number} input_width
|
|
||||||
* @param {number} input_height
|
|
||||||
* @param {number} factor
|
|
||||||
* @returns {Uint32Array}
|
|
||||||
*/
|
|
||||||
export function resize(input_image, input_width, input_height, factor) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
5
codecs/hqx/pkg/squooshhqx_bg.d.ts
vendored
5
codecs/hqx/pkg/squooshhqx_bg.d.ts
vendored
@@ -1,5 +0,0 @@
|
|||||||
/* tslint:disable */
|
|
||||||
export const memory: WebAssembly.Memory;
|
|
||||||
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;
|
|
||||||
Binary file not shown.
@@ -1,55 +0,0 @@
|
|||||||
extern crate cfg_if;
|
|
||||||
extern crate hqx;
|
|
||||||
extern crate wasm_bindgen;
|
|
||||||
|
|
||||||
mod utils;
|
|
||||||
|
|
||||||
use cfg_if::cfg_if;
|
|
||||||
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(
|
|
||||||
input_image: Vec<u32>,
|
|
||||||
input_width: usize,
|
|
||||||
input_height: usize,
|
|
||||||
factor: usize,
|
|
||||||
) -> Vec<u32> {
|
|
||||||
let num_output_pixels = input_width * input_height * factor * factor;
|
|
||||||
let mut output_image = Vec::<u32>::with_capacity(num_output_pixels * 4);
|
|
||||||
output_image.resize(num_output_pixels, 0);
|
|
||||||
|
|
||||||
match factor {
|
|
||||||
2 => hqx::hq2x(
|
|
||||||
input_image.as_slice(),
|
|
||||||
output_image.as_mut_slice(),
|
|
||||||
input_width,
|
|
||||||
input_height,
|
|
||||||
),
|
|
||||||
3 => hqx::hq3x(
|
|
||||||
input_image.as_slice(),
|
|
||||||
output_image.as_mut_slice(),
|
|
||||||
input_width,
|
|
||||||
input_height,
|
|
||||||
),
|
|
||||||
4 => hqx::hq4x(
|
|
||||||
input_image.as_slice(),
|
|
||||||
output_image.as_mut_slice(),
|
|
||||||
input_width,
|
|
||||||
input_height,
|
|
||||||
),
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
return output_image;
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
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() {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
# ImageQuant
|
|
||||||
|
|
||||||
- Source: <https://github.com/ImageOptim/libimagequant>
|
|
||||||
- Version: v2.12.1
|
|
||||||
|
|
||||||
## Dependencies
|
|
||||||
|
|
||||||
- Docker
|
|
||||||
|
|
||||||
## Example
|
|
||||||
|
|
||||||
See `example.html`
|
|
||||||
|
|
||||||
## API
|
|
||||||
|
|
||||||
### `int version()`
|
|
||||||
|
|
||||||
Returns the version of libimagequant as a number. va.b.c is encoded as 0x0a0b0c
|
|
||||||
|
|
||||||
### `RawImage quantize(std::string buffer, int image_width, int image_height, int numColors, float dithering)`
|
|
||||||
|
|
||||||
Quantizes the given images, using at most `numColors`, a value between 2 and 256. `dithering` is a value between 0 and 1 controlling the amount of dithering. `RawImage` is a class with 3 fields: `buffer`, `width`, and `height`.
|
|
||||||
|
|
||||||
### `RawImage zx_quantize(std::string buffer, int image_width, int image_height, float dithering)`
|
|
||||||
|
|
||||||
???
|
|
||||||
|
|
||||||
### `void free_result()`
|
|
||||||
|
|
||||||
Frees the result created by `quantize()`.
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
export OPTIMIZE="-Os"
|
|
||||||
export LDFLAGS="${OPTIMIZE}"
|
|
||||||
export CFLAGS="${OPTIMIZE}"
|
|
||||||
export CPPFLAGS="${OPTIMIZE}"
|
|
||||||
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling libimagequant"
|
|
||||||
echo "============================================="
|
|
||||||
(
|
|
||||||
emcc \
|
|
||||||
--bind \
|
|
||||||
${OPTIMIZE} \
|
|
||||||
-s ALLOW_MEMORY_GROWTH=1 \
|
|
||||||
-s MODULARIZE=1 \
|
|
||||||
-s 'EXPORT_NAME="imagequant"' \
|
|
||||||
-I node_modules/libimagequant \
|
|
||||||
--std=c99 \
|
|
||||||
-c \
|
|
||||||
node_modules/libimagequant/{libimagequant,pam,mediancut,blur,mempool,kmeans,nearest}.c
|
|
||||||
)
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling wasm module"
|
|
||||||
echo "============================================="
|
|
||||||
(
|
|
||||||
emcc \
|
|
||||||
--bind \
|
|
||||||
${OPTIMIZE} \
|
|
||||||
-s ALLOW_MEMORY_GROWTH=1 \
|
|
||||||
-s MODULARIZE=1 \
|
|
||||||
-s 'EXPORT_NAME="imagequant"' \
|
|
||||||
-I node_modules/libimagequant \
|
|
||||||
-o ./imagequant.js \
|
|
||||||
--std=c++11 *.o \
|
|
||||||
-x c++ \
|
|
||||||
imagequant.cpp
|
|
||||||
)
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling wasm module done"
|
|
||||||
echo "============================================="
|
|
||||||
|
|
||||||
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
||||||
echo "Did you update your docker image?"
|
|
||||||
echo "Run \`docker pull trzeci/emscripten\`"
|
|
||||||
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
<!doctype html>
|
|
||||||
<style>
|
|
||||||
canvas {
|
|
||||||
image-rendering: pixelated;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
<script src='imagequant.js'></script>
|
|
||||||
<script>
|
|
||||||
const Module = imagequant();
|
|
||||||
|
|
||||||
async function loadImage(src) {
|
|
||||||
// Load image
|
|
||||||
const img = document.createElement('img');
|
|
||||||
img.src = src;
|
|
||||||
await new Promise(resolve => img.onload = resolve);
|
|
||||||
// Make canvas same size as image
|
|
||||||
const canvas = document.createElement('canvas');
|
|
||||||
[canvas.width, canvas.height] = [img.width, img.height];
|
|
||||||
// Draw image onto canvas
|
|
||||||
const ctx = canvas.getContext('2d');
|
|
||||||
ctx.drawImage(img, 0, 0);
|
|
||||||
return ctx.getImageData(0, 0, img.width, img.height);
|
|
||||||
}
|
|
||||||
|
|
||||||
Module.onRuntimeInitialized = async _ => {
|
|
||||||
console.log('Version:', Module.version().toString(16));
|
|
||||||
const image = await loadImage('../example.png');
|
|
||||||
// const rawImage = Module.quantize(image.data, image.width, image.height, 256, 1.0);
|
|
||||||
const rawImage = Module.zx_quantize(image.data, image.width, image.height, 1.0);
|
|
||||||
console.log('done');
|
|
||||||
Module.free_result();
|
|
||||||
|
|
||||||
const imageData = new ImageData(new Uint8ClampedArray(rawImage.buffer), rawImage.width, rawImage.height);
|
|
||||||
const canvas = document.createElement('canvas');
|
|
||||||
canvas.width = image.width;
|
|
||||||
canvas.height = image.height;
|
|
||||||
const ctx = canvas.getContext('2d');
|
|
||||||
ctx.putImageData(imageData, 0, 0);
|
|
||||||
document.body.appendChild(canvas);
|
|
||||||
};
|
|
||||||
</script>
|
|
||||||
@@ -1,245 +0,0 @@
|
|||||||
#include "emscripten/bind.h"
|
|
||||||
#include "emscripten/val.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <inttypes.h>
|
|
||||||
#include <limits.h>
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
#include "libimagequant.h"
|
|
||||||
|
|
||||||
using namespace emscripten;
|
|
||||||
|
|
||||||
int version() {
|
|
||||||
return (((LIQ_VERSION/10000) % 100) << 16) |
|
|
||||||
(((LIQ_VERSION/100 ) % 100) << 8) |
|
|
||||||
(((LIQ_VERSION/1 ) % 100) << 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
class RawImage {
|
|
||||||
public:
|
|
||||||
val buffer;
|
|
||||||
int width;
|
|
||||||
int height;
|
|
||||||
|
|
||||||
RawImage(val b, int w, int h)
|
|
||||||
: buffer(b), width(w), height(h) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
liq_attr *attr;
|
|
||||||
liq_image *image;
|
|
||||||
liq_result *res;
|
|
||||||
uint8_t* result;
|
|
||||||
RawImage quantize(std::string rawimage, int image_width, int image_height, int num_colors, float dithering) {
|
|
||||||
const uint8_t* image_buffer = (uint8_t*)rawimage.c_str();
|
|
||||||
int size = image_width * image_height;
|
|
||||||
attr = liq_attr_create();
|
|
||||||
image = liq_image_create_rgba(attr, image_buffer, image_width, image_height, 0);
|
|
||||||
liq_set_max_colors(attr, num_colors);
|
|
||||||
liq_image_quantize(image, attr, &res);
|
|
||||||
liq_set_dithering_level(res, dithering);
|
|
||||||
uint8_t* image8bit = (uint8_t*) malloc(size);
|
|
||||||
result = (uint8_t*) malloc(size * 4);
|
|
||||||
liq_write_remapped_image(res, image, image8bit, size);
|
|
||||||
const liq_palette *pal = liq_get_palette(res);
|
|
||||||
// Turn palletted image back into an RGBA image
|
|
||||||
for(int i = 0; i < size; i++) {
|
|
||||||
result[i * 4 + 0] = pal->entries[image8bit[i]].r;
|
|
||||||
result[i * 4 + 1] = pal->entries[image8bit[i]].g;
|
|
||||||
result[i * 4 + 2] = pal->entries[image8bit[i]].b;
|
|
||||||
result[i * 4 + 3] = pal->entries[image8bit[i]].a;
|
|
||||||
}
|
|
||||||
free(image8bit);
|
|
||||||
liq_result_destroy(res);
|
|
||||||
liq_image_destroy(image);
|
|
||||||
liq_attr_destroy(attr);
|
|
||||||
return {
|
|
||||||
val(typed_memory_view(image_width*image_height*4, result)),
|
|
||||||
image_width,
|
|
||||||
image_height
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const liq_color zx_colors[] = {
|
|
||||||
{.a = 255, .r = 0, .g = 0, .b = 0}, // regular black
|
|
||||||
{.a = 255, .r = 0, .g = 0, .b = 215}, // regular blue
|
|
||||||
{.a = 255, .r = 215, .g = 0, .b = 0}, // regular red
|
|
||||||
{.a = 255, .r = 215, .g = 0, .b = 215}, // regular magenta
|
|
||||||
{.a = 255, .r = 0, .g = 215, .b = 0}, // regular green
|
|
||||||
{.a = 255, .r = 0, .g = 215, .b = 215}, // regular cyan
|
|
||||||
{.a = 255, .r = 215, .g = 215, .b = 0}, // regular yellow
|
|
||||||
{.a = 255, .r = 215, .g = 215, .b = 215}, // regular white
|
|
||||||
{.a = 255, .r = 0, .g = 0, .b = 255}, // bright blue
|
|
||||||
{.a = 255, .r = 255, .g = 0, .b = 0}, // bright red
|
|
||||||
{.a = 255, .r = 255, .g = 0, .b = 255}, // bright magenta
|
|
||||||
{.a = 255, .r = 0, .g = 255, .b = 0}, // bright green
|
|
||||||
{.a = 255, .r = 0, .g = 255, .b = 255}, // bright cyan
|
|
||||||
{.a = 255, .r = 255, .g = 255, .b = 0}, // bright yellow
|
|
||||||
{.a = 255, .r = 255, .g = 255, .b = 255} // bright white
|
|
||||||
};
|
|
||||||
|
|
||||||
uint8_t block[8 * 8 * 4];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The ZX has one bit per pixel, but can assign two colours to an 8x8 block. The two colours must
|
|
||||||
* both be 'regular' or 'bright'. Black exists as both regular and bright.
|
|
||||||
*/
|
|
||||||
RawImage zx_quantize(std::string rawimage, int image_width, int image_height, float dithering) {
|
|
||||||
const uint8_t* image_buffer = (uint8_t*) rawimage.c_str();
|
|
||||||
int size = image_width * image_height;
|
|
||||||
int bytes_per_pixel = 4;
|
|
||||||
result = (uint8_t*) malloc(size * bytes_per_pixel);
|
|
||||||
uint8_t* image8bit = (uint8_t*) malloc(8 * 8);
|
|
||||||
|
|
||||||
// For each 8x8 grid
|
|
||||||
for (int block_start_y = 0; block_start_y < image_height; block_start_y += 8) {
|
|
||||||
for (int block_start_x = 0; block_start_x < image_width; block_start_x += 8) {
|
|
||||||
int color_popularity[15] = {0};
|
|
||||||
int block_index = 0;
|
|
||||||
int block_width = 8;
|
|
||||||
int block_height = 8;
|
|
||||||
|
|
||||||
// If the block hangs off the right/bottom of the image dimensions, make it smaller to fit.
|
|
||||||
if (block_start_y + block_height > image_height) {
|
|
||||||
block_height = image_height - block_start_y;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (block_start_x + block_width > image_width) {
|
|
||||||
block_width = image_width - block_start_x;
|
|
||||||
}
|
|
||||||
|
|
||||||
// For each pixel in that block:
|
|
||||||
for (int y = block_start_y; y < block_start_y + block_height; y++) {
|
|
||||||
for (int x = block_start_x; x < block_start_x + block_width; x++) {
|
|
||||||
int pixel_start = (y * image_width * bytes_per_pixel) + (x * bytes_per_pixel);
|
|
||||||
int smallest_distance = INT_MAX;
|
|
||||||
int winning_index = -1;
|
|
||||||
|
|
||||||
// Copy pixel data for quantizing later
|
|
||||||
block[block_index++] = image_buffer[pixel_start];
|
|
||||||
block[block_index++] = image_buffer[pixel_start + 1];
|
|
||||||
block[block_index++] = image_buffer[pixel_start + 2];
|
|
||||||
block[block_index++] = image_buffer[pixel_start + 3];
|
|
||||||
|
|
||||||
// Which zx color is this pixel closest to?
|
|
||||||
for (int color_index = 0; color_index < 15; color_index++) {
|
|
||||||
liq_color color = zx_colors[color_index];
|
|
||||||
|
|
||||||
// Using Euclidean distance. LibQuant has better methods, but it requires conversion to
|
|
||||||
// LAB, so I don't think it's worth it.
|
|
||||||
int distance =
|
|
||||||
pow(color.r - image_buffer[pixel_start + 0], 2) +
|
|
||||||
pow(color.g - image_buffer[pixel_start + 1], 2) +
|
|
||||||
pow(color.b - image_buffer[pixel_start + 2], 2);
|
|
||||||
|
|
||||||
if (distance < smallest_distance) {
|
|
||||||
winning_index = color_index;
|
|
||||||
smallest_distance = distance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
color_popularity[winning_index]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the three most popular colours for the block.
|
|
||||||
int first_color_index = 0;
|
|
||||||
int second_color_index = 0;
|
|
||||||
int third_color_index = 0;
|
|
||||||
int highest_popularity = -1;
|
|
||||||
int second_highest_popularity = -1;
|
|
||||||
int third_highest_popularity = -1;
|
|
||||||
|
|
||||||
for (int color_index = 0; color_index < 15; color_index++) {
|
|
||||||
if (color_popularity[color_index] > highest_popularity) {
|
|
||||||
// Store this as the most popular pixel, and demote the current values:
|
|
||||||
third_color_index = second_color_index;
|
|
||||||
third_highest_popularity = second_highest_popularity;
|
|
||||||
second_color_index = first_color_index;
|
|
||||||
second_highest_popularity = highest_popularity;
|
|
||||||
first_color_index = color_index;
|
|
||||||
highest_popularity = color_popularity[color_index];
|
|
||||||
} else if (color_popularity[color_index] > second_highest_popularity) {
|
|
||||||
third_color_index = second_color_index;
|
|
||||||
third_highest_popularity = second_highest_popularity;
|
|
||||||
second_color_index = color_index;
|
|
||||||
second_highest_popularity = color_popularity[color_index];
|
|
||||||
} else if (color_popularity[color_index] > third_highest_popularity) {
|
|
||||||
third_color_index = color_index;
|
|
||||||
third_highest_popularity = color_popularity[color_index];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ZX images can't mix bright and regular colours, except black which appears in both.
|
|
||||||
// Resolve any conflict:
|
|
||||||
while (1) {
|
|
||||||
// If either colour is black, there's no conflict to resolve.
|
|
||||||
if (first_color_index != 0 && second_color_index != 0) {
|
|
||||||
if (first_color_index >= 8 && second_color_index < 8) {
|
|
||||||
// Make the second color bright
|
|
||||||
second_color_index = second_color_index + 7;
|
|
||||||
} else if (first_color_index < 8 && second_color_index >= 8) {
|
|
||||||
// Make the second color regular
|
|
||||||
second_color_index = second_color_index - 7;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If, during conflict resolving, we now have two of the same colour (because we initially
|
|
||||||
// selected the bright & regular version of the same colour), retry again with the third
|
|
||||||
// most popular colour.
|
|
||||||
if (first_color_index == second_color_index) {
|
|
||||||
second_color_index = third_color_index;
|
|
||||||
} else break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Quantize
|
|
||||||
attr = liq_attr_create();
|
|
||||||
image = liq_image_create_rgba(attr, block, block_width, block_height, 0);
|
|
||||||
liq_set_max_colors(attr, 2);
|
|
||||||
liq_image_add_fixed_color(image, zx_colors[first_color_index]);
|
|
||||||
liq_image_add_fixed_color(image, zx_colors[second_color_index]);
|
|
||||||
liq_image_quantize(image, attr, &res);
|
|
||||||
liq_set_dithering_level(res, dithering);
|
|
||||||
liq_write_remapped_image(res, image, image8bit, size);
|
|
||||||
const liq_palette *pal = liq_get_palette(res);
|
|
||||||
|
|
||||||
// Turn palletted image back into an RGBA image, and write it into the full size result image.
|
|
||||||
for(int y = 0; y < block_height; y++) {
|
|
||||||
for(int x = 0; x < block_width; x++) {
|
|
||||||
int image8BitPos = y * block_width + x;
|
|
||||||
int resultStartPos = ((block_start_y + y) * bytes_per_pixel * image_width) + ((block_start_x + x) * bytes_per_pixel);
|
|
||||||
result[resultStartPos + 0] = pal->entries[image8bit[image8BitPos]].r;
|
|
||||||
result[resultStartPos + 1] = pal->entries[image8bit[image8BitPos]].g;
|
|
||||||
result[resultStartPos + 2] = pal->entries[image8bit[image8BitPos]].b;
|
|
||||||
result[resultStartPos + 3] = pal->entries[image8bit[image8BitPos]].a;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
liq_result_destroy(res);
|
|
||||||
liq_image_destroy(image);
|
|
||||||
liq_attr_destroy(attr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
free(image8bit);
|
|
||||||
return {
|
|
||||||
val(typed_memory_view(image_width*image_height*4, result)),
|
|
||||||
image_width,
|
|
||||||
image_height
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
void free_result() {
|
|
||||||
free(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
EMSCRIPTEN_BINDINGS(my_module) {
|
|
||||||
class_<RawImage>("RawImage")
|
|
||||||
.property("buffer", &RawImage::buffer)
|
|
||||||
.property("width", &RawImage::width)
|
|
||||||
.property("height", &RawImage::height);
|
|
||||||
|
|
||||||
function("quantize", &quantize);
|
|
||||||
function("zx_quantize", &zx_quantize);
|
|
||||||
function("version", &version);
|
|
||||||
function("free_result", &free_result);
|
|
||||||
}
|
|
||||||
15
codecs/imagequant/imagequant.d.ts
vendored
15
codecs/imagequant/imagequant.d.ts
vendored
@@ -1,15 +0,0 @@
|
|||||||
interface RawImage {
|
|
||||||
buffer: Uint8Array;
|
|
||||||
width: number;
|
|
||||||
height: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface QuantizerModule extends EmscriptenWasm.Module {
|
|
||||||
quantize(data: BufferSource, width: number, height: number, numColors: number, dither: number): RawImage;
|
|
||||||
zx_quantize(data: BufferSource, width: number, height: number, dither: number): RawImage;
|
|
||||||
free_result(): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function(opts: EmscriptenWasm.ModuleOpts): QuantizerModule;
|
|
||||||
|
|
||||||
|
|
||||||
File diff suppressed because one or more lines are too long
Binary file not shown.
1147
codecs/imagequant/package-lock.json
generated
1147
codecs/imagequant/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,13 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "imagequant",
|
|
||||||
"scripts": {
|
|
||||||
"install": "napa",
|
|
||||||
"build": "docker run --rm -v $(pwd):/src trzeci/emscripten ./build.sh"
|
|
||||||
},
|
|
||||||
"napa": {
|
|
||||||
"libimagequant": "ImageOptim/libimagequant#2.12.1"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"napa": "3.0.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -6,6 +6,8 @@
|
|||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
- Docker
|
- Docker
|
||||||
|
- Automake
|
||||||
|
- pkg-config
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
@@ -17,31 +19,26 @@ See `example.html`
|
|||||||
|
|
||||||
Returns the version of MozJPEG as a number. va.b.c is encoded as 0x0a0b0c
|
Returns the version of MozJPEG as a number. va.b.c is encoded as 0x0a0b0c
|
||||||
|
|
||||||
|
### `uint8_t* create_buffer(int width, int height)`
|
||||||
|
|
||||||
|
Allocates an RGBA buffer for an image with the given dimension.
|
||||||
|
|
||||||
|
### `void destroy_buffer(uint8_t* p)`
|
||||||
|
|
||||||
|
Frees a buffer created with `create_buffer`.
|
||||||
|
|
||||||
|
### `void encode(uint8_t* image_buffer, int image_width, int image_height, int quality)`
|
||||||
|
|
||||||
|
Encodes the given image with given dimension to JPEG. `quality` is a number between 0 and 100. The higher the number, the better the quality of the encoded image. The result is implicitly stored and can be accessed using the `get_result_*()` functions.
|
||||||
|
|
||||||
### `void free_result()`
|
### `void free_result()`
|
||||||
|
|
||||||
Frees the result created by `encode()`.
|
Frees the result created by `encode()`.
|
||||||
|
|
||||||
### `Uint8Array encode(std::string image_in, int image_width, int image_height, MozJpegOptions opts)`
|
### `int get_result_pointer()`
|
||||||
|
|
||||||
Encodes the given image with given dimension to JPEG. Options looks like this:
|
Returns the pointer to the start of the buffer holding the encoded data.
|
||||||
|
|
||||||
```c++
|
### `int get_result_size()`
|
||||||
struct MozJpegOptions {
|
|
||||||
int quality;
|
Returns the length of the buffer holding the encoded data.
|
||||||
bool baseline;
|
|
||||||
bool arithmetic;
|
|
||||||
bool progressive;
|
|
||||||
bool optimize_coding;
|
|
||||||
int smoothing;
|
|
||||||
int color_space;
|
|
||||||
int quant_table;
|
|
||||||
bool trellis_multipass;
|
|
||||||
bool trellis_opt_zero;
|
|
||||||
bool trellis_opt_table;
|
|
||||||
int trellis_loops;
|
|
||||||
bool auto_subsample;
|
|
||||||
int chroma_subsample;
|
|
||||||
bool separate_chroma_quality;
|
|
||||||
int chroma_quality;
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|||||||
@@ -1,53 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
export OPTIMIZE="-Os"
|
|
||||||
export LDFLAGS="${OPTIMIZE}"
|
|
||||||
export CFLAGS="${OPTIMIZE}"
|
|
||||||
export CPPFLAGS="${OPTIMIZE}"
|
|
||||||
|
|
||||||
apt-get update
|
|
||||||
apt-get install -qqy autoconf libtool libpng-dev pkg-config
|
|
||||||
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling mozjpeg"
|
|
||||||
echo "============================================="
|
|
||||||
(
|
|
||||||
cd node_modules/mozjpeg
|
|
||||||
autoreconf -fiv
|
|
||||||
emconfigure ./configure --without-simd
|
|
||||||
emmake make libjpeg.la
|
|
||||||
)
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling mozjpeg done"
|
|
||||||
echo "============================================="
|
|
||||||
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling wasm bindings"
|
|
||||||
echo "============================================="
|
|
||||||
(
|
|
||||||
emcc \
|
|
||||||
--bind \
|
|
||||||
${OPTIMIZE} \
|
|
||||||
-s WASM=1 \
|
|
||||||
-s ALLOW_MEMORY_GROWTH=1 \
|
|
||||||
-s MODULARIZE=1 \
|
|
||||||
-s 'EXPORT_NAME="mozjpeg_enc"' \
|
|
||||||
-I node_modules/mozjpeg \
|
|
||||||
-o ./mozjpeg_enc.js \
|
|
||||||
-Wno-deprecated-register \
|
|
||||||
-Wno-writable-strings \
|
|
||||||
node_modules/mozjpeg/rdswitch.c \
|
|
||||||
-x c++ -std=c++11 \
|
|
||||||
mozjpeg_enc.cpp \
|
|
||||||
node_modules/mozjpeg/.libs/libjpeg.a
|
|
||||||
)
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling wasm bindings done"
|
|
||||||
echo "============================================="
|
|
||||||
|
|
||||||
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
||||||
echo "Did you update your docker image?"
|
|
||||||
echo "Run \`docker pull trzeci/emscripten\`"
|
|
||||||
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<!doctype html>
|
<!doctype html>
|
||||||
<script src='mozjpeg_enc.js'></script>
|
<script src='mozjpeg_enc.js'></script>
|
||||||
<script>
|
<script>
|
||||||
const module = mozjpeg_enc();
|
const Module = mozjpeg_enc();
|
||||||
|
|
||||||
async function loadImage(src) {
|
async function loadImage(src) {
|
||||||
// Load image
|
// Load image
|
||||||
@@ -17,27 +17,26 @@
|
|||||||
return ctx.getImageData(0, 0, img.width, img.height);
|
return ctx.getImageData(0, 0, img.width, img.height);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.onRuntimeInitialized = async _ => {
|
Module.onRuntimeInitialized = async _ => {
|
||||||
console.log('Version:', module.version().toString(16));
|
const api = {
|
||||||
|
version: Module.cwrap('version', 'number', []),
|
||||||
|
create_buffer: Module.cwrap('create_buffer', 'number', ['number', 'number']),
|
||||||
|
destroy_buffer: Module.cwrap('destroy_buffer', '', ['number']),
|
||||||
|
encode: Module.cwrap('encode', '', ['number', 'number', 'number', 'number']),
|
||||||
|
free_result: Module.cwrap('free_result', '', ['number']),
|
||||||
|
get_result_pointer: Module.cwrap('get_result_pointer', 'number', []),
|
||||||
|
get_result_size: Module.cwrap('get_result_size', 'number', []),
|
||||||
|
};
|
||||||
const image = await loadImage('../example.png');
|
const image = await loadImage('../example.png');
|
||||||
const result = module.encode(image.data, image.width, image.height, {
|
const p = api.create_buffer(image.width, image.height);
|
||||||
quality: 75,
|
Module.HEAP8.set(image.data, p);
|
||||||
baseline: false,
|
api.encode(p, image.width, image.height, 2);
|
||||||
arithmetic: false,
|
const resultPointer = api.get_result_pointer();
|
||||||
progressive: true,
|
const resultSize = api.get_result_size();
|
||||||
optimize_coding: true,
|
const resultView = new Uint8Array(Module.HEAP8.buffer, resultPointer, resultSize);
|
||||||
smoothing: 0,
|
const result = new Uint8Array(resultView);
|
||||||
color_space: 3,
|
api.free_result(resultPointer);
|
||||||
quant_table: 3,
|
api.destroy_buffer(p);
|
||||||
trellis_multipass: false,
|
|
||||||
trellis_opt_zero: false,
|
|
||||||
trellis_opt_table: false,
|
|
||||||
trellis_loops: 1,
|
|
||||||
auto_subsample: true,
|
|
||||||
chroma_subsample: 2,
|
|
||||||
separate_chroma_quality: false,
|
|
||||||
chroma_quality: 75,
|
|
||||||
});
|
|
||||||
|
|
||||||
const blob = new Blob([result], {type: 'image/jpeg'});
|
const blob = new Blob([result], {type: 'image/jpeg'});
|
||||||
const blobURL = URL.createObjectURL(blob);
|
const blobURL = URL.createObjectURL(blob);
|
||||||
|
|||||||
@@ -1,40 +1,17 @@
|
|||||||
#include <emscripten/bind.h>
|
#include "emscripten.h"
|
||||||
#include <emscripten/val.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "config.h"
|
|
||||||
#include "jpeglib.h"
|
#include "jpeglib.h"
|
||||||
#include "cdjpeg.h"
|
#include "config.h"
|
||||||
|
|
||||||
using namespace emscripten;
|
// MozJPEG doesn’t expose a numeric version, so I have to do some fun C macro hackery to turn it into a string. More details here: https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html
|
||||||
|
|
||||||
// MozJPEG doesn’t expose a numeric version, so I have to do some fun C macro hackery to turn it
|
|
||||||
// into a string. More details here: https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html
|
|
||||||
#define xstr(s) str(s)
|
#define xstr(s) str(s)
|
||||||
#define str(s) #s
|
#define str(s) #s
|
||||||
|
|
||||||
struct MozJpegOptions {
|
EMSCRIPTEN_KEEPALIVE
|
||||||
int quality;
|
|
||||||
bool baseline;
|
|
||||||
bool arithmetic;
|
|
||||||
bool progressive;
|
|
||||||
bool optimize_coding;
|
|
||||||
int smoothing;
|
|
||||||
int color_space;
|
|
||||||
int quant_table;
|
|
||||||
bool trellis_multipass;
|
|
||||||
bool trellis_opt_zero;
|
|
||||||
bool trellis_opt_table;
|
|
||||||
int trellis_loops;
|
|
||||||
bool auto_subsample;
|
|
||||||
int chroma_subsample;
|
|
||||||
bool separate_chroma_quality;
|
|
||||||
int chroma_quality;
|
|
||||||
};
|
|
||||||
|
|
||||||
int version() {
|
int version() {
|
||||||
char buffer[] = xstr(MOZJPEG_VERSION);
|
char buffer[] = xstr(MOZJPEG_VERSION);
|
||||||
int version = 0;
|
int version = 0;
|
||||||
@@ -51,11 +28,27 @@ int version() {
|
|||||||
return version;
|
return version;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t* last_result;
|
EMSCRIPTEN_KEEPALIVE
|
||||||
struct jpeg_compress_struct cinfo;
|
uint8_t* create_buffer(int width, int height) {
|
||||||
|
return malloc(width * height * 4 * sizeof(uint8_t));
|
||||||
|
}
|
||||||
|
|
||||||
val encode(std::string image_in, int image_width, int image_height, MozJpegOptions opts) {
|
EMSCRIPTEN_KEEPALIVE
|
||||||
uint8_t* image_buffer = (uint8_t*) image_in.c_str();
|
void destroy_buffer(uint8_t* p) {
|
||||||
|
free(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
int result[2];
|
||||||
|
EMSCRIPTEN_KEEPALIVE
|
||||||
|
void encode(uint8_t* image_buffer, int image_width, int image_height, int quality) {
|
||||||
|
// Manually convert RGBA data into RGB
|
||||||
|
for(int y = 0; y < image_height; y++) {
|
||||||
|
for(int x = 0; x < image_width; x++) {
|
||||||
|
image_buffer[(y*image_width + x)*3 + 0] = image_buffer[(y*image_width + x)*4 + 0];
|
||||||
|
image_buffer[(y*image_width + x)*3 + 1] = image_buffer[(y*image_width + x)*4 + 1];
|
||||||
|
image_buffer[(y*image_width + x)*3 + 2] = image_buffer[(y*image_width + x)*4 + 2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// The code below is basically the `write_JPEG_file` function from
|
// The code below is basically the `write_JPEG_file` function from
|
||||||
// https://github.com/mozilla/mozjpeg/blob/master/example.c
|
// https://github.com/mozilla/mozjpeg/blob/master/example.c
|
||||||
@@ -68,6 +61,7 @@ val encode(std::string image_in, int image_width, int image_height, MozJpegOptio
|
|||||||
* compression/decompression processes, in existence at once. We refer
|
* compression/decompression processes, in existence at once. We refer
|
||||||
* to any one struct (and its associated working data) as a "JPEG object".
|
* to any one struct (and its associated working data) as a "JPEG object".
|
||||||
*/
|
*/
|
||||||
|
struct jpeg_compress_struct cinfo;
|
||||||
/* This struct represents a JPEG error handler. It is declared separately
|
/* This struct represents a JPEG error handler. It is declared separately
|
||||||
* because applications often want to supply a specialized error handler
|
* because applications often want to supply a specialized error handler
|
||||||
* (see the second half of this file for an example). But here we just
|
* (see the second half of this file for an example). But here we just
|
||||||
@@ -115,57 +109,18 @@ val encode(std::string image_in, int image_width, int image_height, MozJpegOptio
|
|||||||
*/
|
*/
|
||||||
cinfo.image_width = image_width; /* image width and height, in pixels */
|
cinfo.image_width = image_width; /* image width and height, in pixels */
|
||||||
cinfo.image_height = image_height;
|
cinfo.image_height = image_height;
|
||||||
cinfo.input_components = 4; /* # of color components per pixel */
|
cinfo.input_components = 3; /* # of color components per pixel */
|
||||||
cinfo.in_color_space = JCS_EXT_RGBA; /* colorspace of input image */
|
cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
|
||||||
/* Now use the library's routine to set default compression parameters.
|
/* Now use the library's routine to set default compression parameters.
|
||||||
* (You must set at least cinfo.in_color_space before calling this,
|
* (You must set at least cinfo.in_color_space before calling this,
|
||||||
* since the defaults depend on the source color space.)
|
* since the defaults depend on the source color space.)
|
||||||
*/
|
*/
|
||||||
jpeg_set_defaults(&cinfo);
|
jpeg_set_defaults(&cinfo);
|
||||||
|
/* Now you can set any non-default parameters you wish to.
|
||||||
|
* Here we just illustrate the use of quality (quantization table) scaling:
|
||||||
|
*/
|
||||||
|
jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
|
||||||
|
|
||||||
jpeg_set_colorspace(&cinfo, (J_COLOR_SPACE) opts.color_space);
|
|
||||||
|
|
||||||
if (opts.quant_table != -1) {
|
|
||||||
jpeg_c_set_int_param(&cinfo, JINT_BASE_QUANT_TBL_IDX, opts.quant_table);
|
|
||||||
}
|
|
||||||
|
|
||||||
cinfo.optimize_coding = opts.optimize_coding;
|
|
||||||
|
|
||||||
if (opts.arithmetic) {
|
|
||||||
cinfo.arith_code = TRUE;
|
|
||||||
cinfo.optimize_coding = FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
cinfo.smoothing_factor = opts.smoothing;
|
|
||||||
|
|
||||||
jpeg_c_set_bool_param(&cinfo, JBOOLEAN_USE_SCANS_IN_TRELLIS, opts.trellis_multipass);
|
|
||||||
jpeg_c_set_bool_param(&cinfo, JBOOLEAN_TRELLIS_EOB_OPT, opts.trellis_opt_zero);
|
|
||||||
jpeg_c_set_bool_param(&cinfo, JBOOLEAN_TRELLIS_Q_OPT, opts.trellis_opt_table);
|
|
||||||
jpeg_c_set_int_param(&cinfo, JINT_TRELLIS_NUM_LOOPS, opts.trellis_loops);
|
|
||||||
|
|
||||||
// A little hacky to build a string for this, but it means we can use set_quality_ratings which
|
|
||||||
// does some useful heuristic stuff.
|
|
||||||
std::string quality_str = std::to_string(opts.quality);
|
|
||||||
|
|
||||||
if (opts.separate_chroma_quality && opts.color_space == JCS_YCbCr) {
|
|
||||||
quality_str += "," + std::to_string(opts.chroma_quality);
|
|
||||||
}
|
|
||||||
|
|
||||||
char const *pqual = quality_str.c_str();
|
|
||||||
|
|
||||||
set_quality_ratings(&cinfo, (char*) pqual, opts.baseline);
|
|
||||||
|
|
||||||
if (!opts.auto_subsample && opts.color_space == JCS_YCbCr) {
|
|
||||||
cinfo.comp_info[0].h_samp_factor = opts.chroma_subsample;
|
|
||||||
cinfo.comp_info[0].v_samp_factor = opts.chroma_subsample;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!opts.baseline && opts.progressive) {
|
|
||||||
jpeg_simple_progression(&cinfo);
|
|
||||||
} else {
|
|
||||||
cinfo.num_scans = 0;
|
|
||||||
cinfo.scan_info = NULL;
|
|
||||||
}
|
|
||||||
/* Step 4: Start compressor */
|
/* Step 4: Start compressor */
|
||||||
|
|
||||||
/* TRUE ensures that we will write a complete interchange-JPEG file.
|
/* TRUE ensures that we will write a complete interchange-JPEG file.
|
||||||
@@ -181,7 +136,7 @@ val encode(std::string image_in, int image_width, int image_height, MozJpegOptio
|
|||||||
* To keep things simple, we pass one scanline per call; you can pass
|
* To keep things simple, we pass one scanline per call; you can pass
|
||||||
* more if you wish, though.
|
* more if you wish, though.
|
||||||
*/
|
*/
|
||||||
row_stride = image_width * 4; /* JSAMPLEs per row in image_buffer */
|
row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */
|
||||||
|
|
||||||
while (cinfo.next_scanline < cinfo.image_height) {
|
while (cinfo.next_scanline < cinfo.image_height) {
|
||||||
/* jpeg_write_scanlines expects an array of pointers to scanlines.
|
/* jpeg_write_scanlines expects an array of pointers to scanlines.
|
||||||
@@ -197,38 +152,27 @@ val encode(std::string image_in, int image_width, int image_height, MozJpegOptio
|
|||||||
jpeg_finish_compress(&cinfo);
|
jpeg_finish_compress(&cinfo);
|
||||||
/* Step 7: release JPEG compression object */
|
/* Step 7: release JPEG compression object */
|
||||||
|
|
||||||
last_result = output;
|
result[0] = (int)output;
|
||||||
|
result[1] = size;
|
||||||
|
|
||||||
/* And we're done! */
|
|
||||||
return val(typed_memory_view(size, output));
|
|
||||||
}
|
|
||||||
|
|
||||||
void free_result() {
|
|
||||||
/* This is an important step since it will release a good deal of memory. */
|
/* This is an important step since it will release a good deal of memory. */
|
||||||
jpeg_destroy_compress(&cinfo);
|
jpeg_destroy_compress(&cinfo);
|
||||||
|
|
||||||
|
/* And we're done! */
|
||||||
}
|
}
|
||||||
|
|
||||||
EMSCRIPTEN_BINDINGS(my_module) {
|
EMSCRIPTEN_KEEPALIVE
|
||||||
value_object<MozJpegOptions>("MozJpegOptions")
|
void free_result() {
|
||||||
.field("quality", &MozJpegOptions::quality)
|
free(result[0]); // not sure if this is right with mozjpeg
|
||||||
.field("baseline", &MozJpegOptions::baseline)
|
|
||||||
.field("arithmetic", &MozJpegOptions::arithmetic)
|
|
||||||
.field("progressive", &MozJpegOptions::progressive)
|
|
||||||
.field("optimize_coding", &MozJpegOptions::optimize_coding)
|
|
||||||
.field("smoothing", &MozJpegOptions::smoothing)
|
|
||||||
.field("color_space", &MozJpegOptions::color_space)
|
|
||||||
.field("quant_table", &MozJpegOptions::quant_table)
|
|
||||||
.field("trellis_multipass", &MozJpegOptions::trellis_multipass)
|
|
||||||
.field("trellis_opt_zero", &MozJpegOptions::trellis_opt_zero)
|
|
||||||
.field("trellis_opt_table", &MozJpegOptions::trellis_opt_table)
|
|
||||||
.field("trellis_loops", &MozJpegOptions::trellis_loops)
|
|
||||||
.field("chroma_subsample", &MozJpegOptions::chroma_subsample)
|
|
||||||
.field("auto_subsample", &MozJpegOptions::auto_subsample)
|
|
||||||
.field("separate_chroma_quality", &MozJpegOptions::separate_chroma_quality)
|
|
||||||
.field("chroma_quality", &MozJpegOptions::chroma_quality)
|
|
||||||
;
|
|
||||||
|
|
||||||
function("version", &version);
|
|
||||||
function("encode", &encode);
|
|
||||||
function("free_result", &free_result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EMSCRIPTEN_KEEPALIVE
|
||||||
|
int get_result_pointer() {
|
||||||
|
return result[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
EMSCRIPTEN_KEEPALIVE
|
||||||
|
int get_result_size() {
|
||||||
|
return result[1];
|
||||||
|
}
|
||||||
|
|
||||||
9
codecs/mozjpeg_enc/mozjpeg_enc.d.ts
vendored
9
codecs/mozjpeg_enc/mozjpeg_enc.d.ts
vendored
@@ -1,8 +1 @@
|
|||||||
import { EncodeOptions } from '../../src/codecs/mozjpeg/encoder-meta';
|
export default function(opts: EmscriptenWasm.ModuleOpts): EmscriptenWasm.Module;
|
||||||
|
|
||||||
interface MozJPEGModule extends EmscriptenWasm.Module {
|
|
||||||
encode(data: BufferSource, width: number, height: number, options: EncodeOptions): Uint8Array;
|
|
||||||
free_result(): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function(opts: EmscriptenWasm.ModuleOpts): MozJPEGModule;
|
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Binary file not shown.
@@ -2,12 +2,14 @@
|
|||||||
"name": "mozjpeg_enc",
|
"name": "mozjpeg_enc",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"install": "napa",
|
"install": "napa",
|
||||||
"build": "docker run --rm -v $(pwd):/src trzeci/emscripten ./build.sh"
|
"build": "npm run build:library && npm run build:wasm",
|
||||||
|
"build:library": "cd node_modules/mozjpeg && autoreconf -fiv && docker run --rm -v $(pwd):/src trzeci/emscripten emconfigure ./configure --without-simd && docker run --rm -v $(pwd):/src trzeci/emscripten emmake make libjpeg.la",
|
||||||
|
"build:wasm": "docker run --rm -v $(pwd):/src trzeci/emscripten emcc -O3 -s WASM=1 -s EXTRA_EXPORTED_RUNTIME_METHODS='[\"cwrap\"]' -s ALLOW_MEMORY_GROWTH=1 -s MODULARIZE=1 -s 'EXPORT_NAME=\"mozjpeg_enc\"' -I node_modules/mozjpeg -o ./mozjpeg_enc.js mozjpeg_enc.c node_modules/mozjpeg/.libs/libjpeg.a"
|
||||||
},
|
},
|
||||||
"napa": {
|
"napa": {
|
||||||
"mozjpeg": "mozilla/mozjpeg#v3.3.1"
|
"mozjpeg": "mozilla/mozjpeg#v3.3.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"napa": "3.0.0"
|
"napa": "^3.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
codecs/optipng/.gitignore
vendored
2
codecs/optipng/.gitignore
vendored
@@ -1,2 +0,0 @@
|
|||||||
build/
|
|
||||||
*.o
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
# OptiPNG
|
|
||||||
|
|
||||||
- Source: <http://optipng.sourceforge.net/>
|
|
||||||
- Version: v0.7.7
|
|
||||||
|
|
||||||
## Dependencies
|
|
||||||
|
|
||||||
- Docker
|
|
||||||
|
|
||||||
## Example
|
|
||||||
|
|
||||||
See `example.html`
|
|
||||||
|
|
||||||
## API
|
|
||||||
|
|
||||||
### `int version()`
|
|
||||||
|
|
||||||
Returns the version of optipng as a number. va.b.c is encoded as 0x0a0b0c
|
|
||||||
|
|
||||||
### `ArrayBuffer compress(std::string buffer, {level})`;
|
|
||||||
|
|
||||||
`compress` will re-compress the given PNG image via `buffer`. `level` is a number between 0 and 7.
|
|
||||||
|
|
||||||
### `void free_result()`
|
|
||||||
|
|
||||||
Frees the result created by `compress()`.
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
export OPTIMIZE="-Os"
|
|
||||||
export PREFIX="/src/build"
|
|
||||||
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling optipng"
|
|
||||||
echo "============================================="
|
|
||||||
(
|
|
||||||
cd node_modules/optipng
|
|
||||||
CFLAGS="${OPTIMIZE} -Isrc/zlib" emconfigure ./configure --prefix=${PREFIX}
|
|
||||||
emmake make
|
|
||||||
emmake make install
|
|
||||||
mkdir -p ${PREFIX}/lib
|
|
||||||
mv ${PREFIX}/bin/optipng ${PREFIX}/lib/liboptipng.so
|
|
||||||
)
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling optipng done"
|
|
||||||
echo "============================================="
|
|
||||||
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling optipng wrapper"
|
|
||||||
echo "============================================="
|
|
||||||
(
|
|
||||||
emcc \
|
|
||||||
--bind \
|
|
||||||
${OPTIMIZE} \
|
|
||||||
-s ALLOW_MEMORY_GROWTH=1 -s MODULARIZE=1 -s 'EXPORT_NAME="optipng"' \
|
|
||||||
-o "optipng.js" \
|
|
||||||
--std=c++11 \
|
|
||||||
optipng.cpp \
|
|
||||||
${PREFIX}/lib/liboptipng.so
|
|
||||||
)
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling optipng wrapper done"
|
|
||||||
echo "============================================="
|
|
||||||
|
|
||||||
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
||||||
echo "Did you update your docker image?"
|
|
||||||
echo "Run \`docker pull trzeci/emscripten-upstream\`"
|
|
||||||
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
<!doctype html>
|
|
||||||
<script src='optipng.js'></script>
|
|
||||||
<script>
|
|
||||||
const Module = optipng();
|
|
||||||
|
|
||||||
Module.onRuntimeInitialized = async _ => {
|
|
||||||
console.log('Version:', Module.version().toString(16));
|
|
||||||
const image = await fetch('../example_palette.png').then(r => r.arrayBuffer());
|
|
||||||
const newImage = Module.compress(image, {level: 3});
|
|
||||||
console.log('done');
|
|
||||||
Module.free_result();
|
|
||||||
|
|
||||||
console.log(`Old size: ${image.byteLength}, new size: ${newImage.byteLength} (${newImage.byteLength/image.byteLength*100}%)`);
|
|
||||||
const blobURL = URL.createObjectURL(new Blob([newImage], {type: 'image/png'}));
|
|
||||||
const img = document.createElement('img');
|
|
||||||
img.src = blobURL;
|
|
||||||
document.body.appendChild(img);
|
|
||||||
};
|
|
||||||
</script>
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
#include "emscripten/bind.h"
|
|
||||||
#include "emscripten/val.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
using namespace emscripten;
|
|
||||||
|
|
||||||
extern "C" int main(int argc, char *argv[]);
|
|
||||||
|
|
||||||
int version() {
|
|
||||||
// FIXME (@surma): Haven’t found a version in optipng :(
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct OptiPngOpts {
|
|
||||||
int level;
|
|
||||||
};
|
|
||||||
|
|
||||||
uint8_t* result;
|
|
||||||
val compress(std::string png, OptiPngOpts opts) {
|
|
||||||
remove("input.png");
|
|
||||||
remove("output.png");
|
|
||||||
FILE* infile = fopen("input.png", "wb");
|
|
||||||
fwrite(png.c_str(), png.length(), 1, infile);
|
|
||||||
fflush(infile);
|
|
||||||
fclose(infile);
|
|
||||||
|
|
||||||
char optlevel[8];
|
|
||||||
sprintf(&optlevel[0], "-o%d", opts.level);
|
|
||||||
char* args[] = {"optipng", optlevel, "-out", "output.png", "input.png"};
|
|
||||||
main(5, args);
|
|
||||||
|
|
||||||
FILE *outfile = fopen("output.png", "rb");
|
|
||||||
fseek(outfile, 0, SEEK_END);
|
|
||||||
int fsize = ftell(outfile);
|
|
||||||
result = (uint8_t*) malloc(fsize);
|
|
||||||
fseek(outfile, 0, SEEK_SET);
|
|
||||||
fread(result, fsize, 1, outfile);
|
|
||||||
return val(typed_memory_view(fsize, result));
|
|
||||||
}
|
|
||||||
|
|
||||||
void free_result() {
|
|
||||||
free(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
EMSCRIPTEN_BINDINGS(my_module) {
|
|
||||||
value_object<OptiPngOpts>("OptiPngOpts")
|
|
||||||
.field("level", &OptiPngOpts::level);
|
|
||||||
|
|
||||||
function("version", &version);
|
|
||||||
function("compress", &compress);
|
|
||||||
function("free_result", &free_result);
|
|
||||||
}
|
|
||||||
10
codecs/optipng/optipng.d.ts
vendored
10
codecs/optipng/optipng.d.ts
vendored
@@ -1,10 +0,0 @@
|
|||||||
import {EncodeOptions} from "src/codecs/optipng/encoder";
|
|
||||||
|
|
||||||
export interface OptiPngModule extends EmscriptenWasm.Module {
|
|
||||||
compress(data: BufferSource, opts: EncodeOptions): Uint8Array;
|
|
||||||
free_result(): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function(opts: EmscriptenWasm.ModuleOpts): OptiPngModule;
|
|
||||||
|
|
||||||
|
|
||||||
File diff suppressed because one or more lines are too long
Binary file not shown.
543
codecs/optipng/package-lock.json
generated
543
codecs/optipng/package-lock.json
generated
@@ -1,543 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "optipng",
|
|
||||||
"requires": true,
|
|
||||||
"lockfileVersion": 1,
|
|
||||||
"dependencies": {
|
|
||||||
"ajv": {
|
|
||||||
"version": "5.5.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/ajv/-/ajv-5.5.2.tgz",
|
|
||||||
"integrity": "sha1-c7Xuyj+rZT49P5Qis0GtQiBdyWU=",
|
|
||||||
"requires": {
|
|
||||||
"co": "^4.6.0",
|
|
||||||
"fast-deep-equal": "^1.0.0",
|
|
||||||
"fast-json-stable-stringify": "^2.0.0",
|
|
||||||
"json-schema-traverse": "^0.3.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"asn1": {
|
|
||||||
"version": "0.2.4",
|
|
||||||
"resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.4.tgz",
|
|
||||||
"integrity": "sha512-jxwzQpLQjSmWXgwaCZE9Nz+glAG01yF1QnWgbhGwHI5A6FRIEY6IVqtHhIepHqI7/kyEyQEagBC5mBEFlIYvdg==",
|
|
||||||
"requires": {
|
|
||||||
"safer-buffer": "~2.1.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"assert-plus": {
|
|
||||||
"version": "1.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz",
|
|
||||||
"integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU="
|
|
||||||
},
|
|
||||||
"asynckit": {
|
|
||||||
"version": "0.4.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
|
|
||||||
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k="
|
|
||||||
},
|
|
||||||
"aws-sign2": {
|
|
||||||
"version": "0.7.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/aws-sign2/-/aws-sign2-0.7.0.tgz",
|
|
||||||
"integrity": "sha1-tG6JCTSpWR8tL2+G1+ap8bP+dqg="
|
|
||||||
},
|
|
||||||
"aws4": {
|
|
||||||
"version": "1.8.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/aws4/-/aws4-1.8.0.tgz",
|
|
||||||
"integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ=="
|
|
||||||
},
|
|
||||||
"balanced-match": {
|
|
||||||
"version": "1.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
|
|
||||||
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c="
|
|
||||||
},
|
|
||||||
"bcrypt-pbkdf": {
|
|
||||||
"version": "1.0.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz",
|
|
||||||
"integrity": "sha1-pDAdOJtqQ/m2f/PKEaP2Y342Dp4=",
|
|
||||||
"optional": true,
|
|
||||||
"requires": {
|
|
||||||
"tweetnacl": "^0.14.3"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"brace-expansion": {
|
|
||||||
"version": "1.1.11",
|
|
||||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
|
|
||||||
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
|
|
||||||
"requires": {
|
|
||||||
"balanced-match": "^1.0.0",
|
|
||||||
"concat-map": "0.0.1"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"caseless": {
|
|
||||||
"version": "0.12.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz",
|
|
||||||
"integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw="
|
|
||||||
},
|
|
||||||
"chownr": {
|
|
||||||
"version": "1.0.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/chownr/-/chownr-1.0.1.tgz",
|
|
||||||
"integrity": "sha1-4qdQQqlVGQi+vSW4Uj1fl2nXkYE="
|
|
||||||
},
|
|
||||||
"co": {
|
|
||||||
"version": "4.6.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz",
|
|
||||||
"integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ="
|
|
||||||
},
|
|
||||||
"combined-stream": {
|
|
||||||
"version": "1.0.6",
|
|
||||||
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.6.tgz",
|
|
||||||
"integrity": "sha1-cj599ugBrFYTETp+RFqbactjKBg=",
|
|
||||||
"requires": {
|
|
||||||
"delayed-stream": "~1.0.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"concat-map": {
|
|
||||||
"version": "0.0.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
|
|
||||||
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s="
|
|
||||||
},
|
|
||||||
"core-util-is": {
|
|
||||||
"version": "1.0.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
|
|
||||||
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
|
|
||||||
},
|
|
||||||
"dashdash": {
|
|
||||||
"version": "1.14.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz",
|
|
||||||
"integrity": "sha1-hTz6D3y+L+1d4gMmuN1YEDX24vA=",
|
|
||||||
"requires": {
|
|
||||||
"assert-plus": "^1.0.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"delayed-stream": {
|
|
||||||
"version": "1.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
|
|
||||||
"integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk="
|
|
||||||
},
|
|
||||||
"ecc-jsbn": {
|
|
||||||
"version": "0.1.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz",
|
|
||||||
"integrity": "sha1-OoOpBOVDUyh4dMVkt1SThoSamMk=",
|
|
||||||
"optional": true,
|
|
||||||
"requires": {
|
|
||||||
"jsbn": "~0.1.0",
|
|
||||||
"safer-buffer": "^2.1.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"extsprintf": {
|
|
||||||
"version": "1.3.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/extsprintf/-/extsprintf-1.3.0.tgz",
|
|
||||||
"integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU="
|
|
||||||
},
|
|
||||||
"fast-deep-equal": {
|
|
||||||
"version": "1.1.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-1.1.0.tgz",
|
|
||||||
"integrity": "sha1-wFNHeBfIa1HaqFPIHgWbcz0CNhQ="
|
|
||||||
},
|
|
||||||
"fast-json-stable-stringify": {
|
|
||||||
"version": "2.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz",
|
|
||||||
"integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I="
|
|
||||||
},
|
|
||||||
"forever-agent": {
|
|
||||||
"version": "0.6.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz",
|
|
||||||
"integrity": "sha1-+8cfDEGt6zf5bFd60e1C2P2sypE="
|
|
||||||
},
|
|
||||||
"form-data": {
|
|
||||||
"version": "2.3.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/form-data/-/form-data-2.3.2.tgz",
|
|
||||||
"integrity": "sha1-SXBJi+YEwgwAXU9cI67NIda0kJk=",
|
|
||||||
"requires": {
|
|
||||||
"asynckit": "^0.4.0",
|
|
||||||
"combined-stream": "1.0.6",
|
|
||||||
"mime-types": "^2.1.12"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"fs-minipass": {
|
|
||||||
"version": "1.2.5",
|
|
||||||
"resolved": "https://registry.npmjs.org/fs-minipass/-/fs-minipass-1.2.5.tgz",
|
|
||||||
"integrity": "sha512-JhBl0skXjUPCFH7x6x61gQxrKyXsxB5gcgePLZCwfyCGGsTISMoIeObbrvVeP6Xmyaudw4TT43qV2Gz+iyd2oQ==",
|
|
||||||
"requires": {
|
|
||||||
"minipass": "^2.2.1"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"fs.realpath": {
|
|
||||||
"version": "1.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
|
|
||||||
"integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8="
|
|
||||||
},
|
|
||||||
"getpass": {
|
|
||||||
"version": "0.1.7",
|
|
||||||
"resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz",
|
|
||||||
"integrity": "sha1-Xv+OPmhNVprkyysSgmBOi6YhSfo=",
|
|
||||||
"requires": {
|
|
||||||
"assert-plus": "^1.0.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"glob": {
|
|
||||||
"version": "7.1.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz",
|
|
||||||
"integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==",
|
|
||||||
"requires": {
|
|
||||||
"fs.realpath": "^1.0.0",
|
|
||||||
"inflight": "^1.0.4",
|
|
||||||
"inherits": "2",
|
|
||||||
"minimatch": "^3.0.4",
|
|
||||||
"once": "^1.3.0",
|
|
||||||
"path-is-absolute": "^1.0.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"graceful-fs": {
|
|
||||||
"version": "4.1.11",
|
|
||||||
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz",
|
|
||||||
"integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg=",
|
|
||||||
"optional": true
|
|
||||||
},
|
|
||||||
"har-schema": {
|
|
||||||
"version": "2.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz",
|
|
||||||
"integrity": "sha1-qUwiJOvKwEeCoNkDVSHyRzW37JI="
|
|
||||||
},
|
|
||||||
"har-validator": {
|
|
||||||
"version": "5.1.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/har-validator/-/har-validator-5.1.0.tgz",
|
|
||||||
"integrity": "sha512-+qnmNjI4OfH2ipQ9VQOw23bBd/ibtfbVdK2fYbY4acTDqKTW/YDp9McimZdDbG8iV9fZizUqQMD5xvriB146TA==",
|
|
||||||
"requires": {
|
|
||||||
"ajv": "^5.3.0",
|
|
||||||
"har-schema": "^2.0.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"http-signature": {
|
|
||||||
"version": "1.2.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz",
|
|
||||||
"integrity": "sha1-muzZJRFHcvPZW2WmCruPfBj7rOE=",
|
|
||||||
"requires": {
|
|
||||||
"assert-plus": "^1.0.0",
|
|
||||||
"jsprim": "^1.2.2",
|
|
||||||
"sshpk": "^1.7.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"inflight": {
|
|
||||||
"version": "1.0.6",
|
|
||||||
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
|
|
||||||
"integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
|
|
||||||
"requires": {
|
|
||||||
"once": "^1.3.0",
|
|
||||||
"wrappy": "1"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"inherits": {
|
|
||||||
"version": "2.0.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
|
|
||||||
"integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4="
|
|
||||||
},
|
|
||||||
"is-typedarray": {
|
|
||||||
"version": "1.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz",
|
|
||||||
"integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo="
|
|
||||||
},
|
|
||||||
"isstream": {
|
|
||||||
"version": "0.1.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz",
|
|
||||||
"integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo="
|
|
||||||
},
|
|
||||||
"jsbn": {
|
|
||||||
"version": "0.1.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz",
|
|
||||||
"integrity": "sha1-peZUwuWi3rXyAdls77yoDA7y9RM=",
|
|
||||||
"optional": true
|
|
||||||
},
|
|
||||||
"json-schema": {
|
|
||||||
"version": "0.2.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.2.3.tgz",
|
|
||||||
"integrity": "sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM="
|
|
||||||
},
|
|
||||||
"json-schema-traverse": {
|
|
||||||
"version": "0.3.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz",
|
|
||||||
"integrity": "sha1-NJptRMU6Ud6JtAgFxdXlm0F9M0A="
|
|
||||||
},
|
|
||||||
"json-stringify-safe": {
|
|
||||||
"version": "5.0.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz",
|
|
||||||
"integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus="
|
|
||||||
},
|
|
||||||
"jsonfile": {
|
|
||||||
"version": "4.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz",
|
|
||||||
"integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=",
|
|
||||||
"requires": {
|
|
||||||
"graceful-fs": "^4.1.6"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"jsprim": {
|
|
||||||
"version": "1.4.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.1.tgz",
|
|
||||||
"integrity": "sha1-MT5mvB5cwG5Di8G3SZwuXFastqI=",
|
|
||||||
"requires": {
|
|
||||||
"assert-plus": "1.0.0",
|
|
||||||
"extsprintf": "1.3.0",
|
|
||||||
"json-schema": "0.2.3",
|
|
||||||
"verror": "1.10.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"mime-types": {
|
|
||||||
"version": "2.1.19",
|
|
||||||
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.19.tgz",
|
|
||||||
"integrity": "sha512-P1tKYHVSZ6uFo26mtnve4HQFE3koh1UWVkp8YUC+ESBHe945xWSoXuHHiGarDqcEZ+whpCDnlNw5LON0kLo+sw==",
|
|
||||||
"requires": {
|
|
||||||
"mime-db": "~1.35.0"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"mime-db": {
|
|
||||||
"version": "1.35.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.35.0.tgz",
|
|
||||||
"integrity": "sha512-JWT/IcCTsB0Io3AhWUMjRqucrHSPsSf2xKLaRldJVULioggvkJvggZ3VXNNSRkCddE6D+BUI4HEIZIA2OjwIvg=="
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"minimatch": {
|
|
||||||
"version": "3.0.4",
|
|
||||||
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
|
|
||||||
"integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
|
|
||||||
"requires": {
|
|
||||||
"brace-expansion": "^1.1.7"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"minipass": {
|
|
||||||
"version": "2.3.4",
|
|
||||||
"resolved": "https://registry.npmjs.org/minipass/-/minipass-2.3.4.tgz",
|
|
||||||
"integrity": "sha512-mlouk1OHlaUE8Odt1drMtG1bAJA4ZA6B/ehysgV0LUIrDHdKgo1KorZq3pK0b/7Z7LJIQ12MNM6aC+Tn6lUZ5w==",
|
|
||||||
"requires": {
|
|
||||||
"safe-buffer": "^5.1.2",
|
|
||||||
"yallist": "^3.0.0"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"safe-buffer": {
|
|
||||||
"version": "5.1.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
|
||||||
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"minizlib": {
|
|
||||||
"version": "1.1.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/minizlib/-/minizlib-1.1.0.tgz",
|
|
||||||
"integrity": "sha512-4T6Ur/GctZ27nHfpt9THOdRZNgyJ9FZchYO1ceg5S8Q3DNLCKYy44nCZzgCJgcvx2UM8czmqak5BCxJMrq37lA==",
|
|
||||||
"requires": {
|
|
||||||
"minipass": "^2.2.1"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"mkdirp": {
|
|
||||||
"version": "0.5.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
|
|
||||||
"integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
|
|
||||||
"requires": {
|
|
||||||
"minimist": "0.0.8"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"minimist": {
|
|
||||||
"version": "0.0.8",
|
|
||||||
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
|
|
||||||
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0="
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"oauth-sign": {
|
|
||||||
"version": "0.9.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz",
|
|
||||||
"integrity": "sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ=="
|
|
||||||
},
|
|
||||||
"once": {
|
|
||||||
"version": "1.4.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
|
|
||||||
"integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
|
|
||||||
"requires": {
|
|
||||||
"wrappy": "1"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"path-is-absolute": {
|
|
||||||
"version": "1.0.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
|
|
||||||
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18="
|
|
||||||
},
|
|
||||||
"performance-now": {
|
|
||||||
"version": "2.1.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz",
|
|
||||||
"integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns="
|
|
||||||
},
|
|
||||||
"psl": {
|
|
||||||
"version": "1.1.29",
|
|
||||||
"resolved": "https://registry.npmjs.org/psl/-/psl-1.1.29.tgz",
|
|
||||||
"integrity": "sha512-AeUmQ0oLN02flVHXWh9sSJF7mcdFq0ppid/JkErufc3hGIV/AMa8Fo9VgDo/cT2jFdOWoFvHp90qqBH54W+gjQ=="
|
|
||||||
},
|
|
||||||
"punycode": {
|
|
||||||
"version": "1.4.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz",
|
|
||||||
"integrity": "sha1-wNWmOycYgArY4esPpSachN1BhF4="
|
|
||||||
},
|
|
||||||
"qs": {
|
|
||||||
"version": "6.5.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz",
|
|
||||||
"integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA=="
|
|
||||||
},
|
|
||||||
"request": {
|
|
||||||
"version": "2.88.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/request/-/request-2.88.0.tgz",
|
|
||||||
"integrity": "sha512-NAqBSrijGLZdM0WZNsInLJpkJokL72XYjUpnB0iwsRgxh7dB6COrHnTBNwN0E+lHDAJzu7kLAkDeY08z2/A0hg==",
|
|
||||||
"requires": {
|
|
||||||
"aws-sign2": "~0.7.0",
|
|
||||||
"aws4": "^1.8.0",
|
|
||||||
"caseless": "~0.12.0",
|
|
||||||
"combined-stream": "~1.0.6",
|
|
||||||
"extend": "~3.0.2",
|
|
||||||
"forever-agent": "~0.6.1",
|
|
||||||
"form-data": "~2.3.2",
|
|
||||||
"har-validator": "~5.1.0",
|
|
||||||
"http-signature": "~1.2.0",
|
|
||||||
"is-typedarray": "~1.0.0",
|
|
||||||
"isstream": "~0.1.2",
|
|
||||||
"json-stringify-safe": "~5.0.1",
|
|
||||||
"mime-types": "~2.1.19",
|
|
||||||
"oauth-sign": "~0.9.0",
|
|
||||||
"performance-now": "^2.1.0",
|
|
||||||
"qs": "~6.5.2",
|
|
||||||
"safe-buffer": "^5.1.2",
|
|
||||||
"tough-cookie": "~2.4.3",
|
|
||||||
"tunnel-agent": "^0.6.0",
|
|
||||||
"uuid": "^3.3.2"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"extend": {
|
|
||||||
"version": "3.0.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz",
|
|
||||||
"integrity": "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g=="
|
|
||||||
},
|
|
||||||
"safe-buffer": {
|
|
||||||
"version": "5.1.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
|
||||||
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"rimraf": {
|
|
||||||
"version": "2.6.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.6.2.tgz",
|
|
||||||
"integrity": "sha512-lreewLK/BlghmxtfH36YYVg1i8IAce4TI7oao75I1g245+6BctqTVQiBP3YUJ9C6DQOXJmkYR9X9fCLtCOJc5w==",
|
|
||||||
"requires": {
|
|
||||||
"glob": "^7.0.5"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"safe-buffer": {
|
|
||||||
"version": "5.1.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz",
|
|
||||||
"integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg=="
|
|
||||||
},
|
|
||||||
"safer-buffer": {
|
|
||||||
"version": "2.1.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
|
|
||||||
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
|
|
||||||
},
|
|
||||||
"sshpk": {
|
|
||||||
"version": "1.14.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.14.2.tgz",
|
|
||||||
"integrity": "sha1-xvxhZIo9nE52T9P8306hBeSSupg=",
|
|
||||||
"requires": {
|
|
||||||
"asn1": "~0.2.3",
|
|
||||||
"assert-plus": "^1.0.0",
|
|
||||||
"bcrypt-pbkdf": "^1.0.0",
|
|
||||||
"dashdash": "^1.12.0",
|
|
||||||
"ecc-jsbn": "~0.1.1",
|
|
||||||
"getpass": "^0.1.1",
|
|
||||||
"jsbn": "~0.1.0",
|
|
||||||
"safer-buffer": "^2.0.2",
|
|
||||||
"tweetnacl": "~0.14.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"tar-dependency": {
|
|
||||||
"version": "0.0.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/tar-dependency/-/tar-dependency-0.0.3.tgz",
|
|
||||||
"integrity": "sha512-p6JSkOT4h9yNk5WYbKQcunoWJiu+BuPb4ynU1hwAfSXgJPf6qvOvOeSyC4WRwexCx6s6kUi4GEvdZrcrmxHYiw==",
|
|
||||||
"requires": {
|
|
||||||
"commander": "^2.11.0",
|
|
||||||
"jsonfile": "^4.0.0",
|
|
||||||
"mkdirp": "^0.5.1",
|
|
||||||
"request": "^2.83.0",
|
|
||||||
"rimraf": "^2.6.2",
|
|
||||||
"tar": "^4.0.2"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"commander": {
|
|
||||||
"version": "2.17.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/commander/-/commander-2.17.1.tgz",
|
|
||||||
"integrity": "sha512-wPMUt6FnH2yzG95SA6mzjQOEKUU3aLaDEmzs1ti+1E9h+CsrZghRlqEM/EJ4KscsQVG8uNN4uVreUeT8+drlgg=="
|
|
||||||
},
|
|
||||||
"safe-buffer": {
|
|
||||||
"version": "5.1.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
|
||||||
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
|
|
||||||
},
|
|
||||||
"tar": {
|
|
||||||
"version": "4.4.6",
|
|
||||||
"resolved": "https://registry.npmjs.org/tar/-/tar-4.4.6.tgz",
|
|
||||||
"integrity": "sha512-tMkTnh9EdzxyfW+6GK6fCahagXsnYk6kE6S9Gr9pjVdys769+laCTbodXDhPAjzVtEBazRgP0gYqOjnk9dQzLg==",
|
|
||||||
"requires": {
|
|
||||||
"chownr": "^1.0.1",
|
|
||||||
"fs-minipass": "^1.2.5",
|
|
||||||
"minipass": "^2.3.3",
|
|
||||||
"minizlib": "^1.1.0",
|
|
||||||
"mkdirp": "^0.5.0",
|
|
||||||
"safe-buffer": "^5.1.2",
|
|
||||||
"yallist": "^3.0.2"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"tough-cookie": {
|
|
||||||
"version": "2.4.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.4.3.tgz",
|
|
||||||
"integrity": "sha512-Q5srk/4vDM54WJsJio3XNn6K2sCG+CQ8G5Wz6bZhRZoAe/+TxjWB/GlFAnYEbkYVlON9FMk/fE3h2RLpPXo4lQ==",
|
|
||||||
"requires": {
|
|
||||||
"psl": "^1.1.24",
|
|
||||||
"punycode": "^1.4.1"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"tunnel-agent": {
|
|
||||||
"version": "0.6.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz",
|
|
||||||
"integrity": "sha1-J6XeoGs2sEoKmWZ3SykIaPD8QP0=",
|
|
||||||
"requires": {
|
|
||||||
"safe-buffer": "^5.0.1"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"tweetnacl": {
|
|
||||||
"version": "0.14.5",
|
|
||||||
"resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz",
|
|
||||||
"integrity": "sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q=",
|
|
||||||
"optional": true
|
|
||||||
},
|
|
||||||
"uuid": {
|
|
||||||
"version": "3.3.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz",
|
|
||||||
"integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA=="
|
|
||||||
},
|
|
||||||
"verror": {
|
|
||||||
"version": "1.10.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/verror/-/verror-1.10.0.tgz",
|
|
||||||
"integrity": "sha1-OhBcoXBTr1XW4nDB+CiGguGNpAA=",
|
|
||||||
"requires": {
|
|
||||||
"assert-plus": "^1.0.0",
|
|
||||||
"core-util-is": "1.0.2",
|
|
||||||
"extsprintf": "^1.2.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"wrappy": {
|
|
||||||
"version": "1.0.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
|
|
||||||
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8="
|
|
||||||
},
|
|
||||||
"yallist": {
|
|
||||||
"version": "3.0.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/yallist/-/yallist-3.0.2.tgz",
|
|
||||||
"integrity": "sha1-hFK0u36Dx8GI2AQcGoN8dz1ti7k="
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "optipng",
|
|
||||||
"scripts": {
|
|
||||||
"install": "tar-dependency install",
|
|
||||||
"build": "npm run build:wasm",
|
|
||||||
"build:wasm": "docker run --rm -v $(pwd):/src trzeci/emscripten-upstream ./build.sh"
|
|
||||||
},
|
|
||||||
"tarDependencies": {
|
|
||||||
"node_modules/optipng": {
|
|
||||||
"url": "https://netcologne.dl.sourceforge.net/project/optipng/OptiPNG/optipng-0.7.7/optipng-0.7.7.tar.gz",
|
|
||||||
"strip": 1
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"tar-dependency": "0.0.3"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
6
codecs/resize/.gitignore
vendored
6
codecs/resize/.gitignore
vendored
@@ -1,6 +0,0 @@
|
|||||||
**/*.rs.bk
|
|
||||||
target
|
|
||||||
Cargo.lock
|
|
||||||
bin/
|
|
||||||
pkg/README.md
|
|
||||||
lut.inc
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
[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
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
FROM rust
|
|
||||||
RUN rustup target add wasm32-unknown-unknown
|
|
||||||
RUN curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
|
|
||||||
|
|
||||||
RUN mkdir /opt/wabt && \
|
|
||||||
curl -L https://github.com/WebAssembly/wabt/releases/download/1.0.11/wabt-1.0.11-linux.tar.gz | tar -xzf - -C /opt/wabt --strip 1
|
|
||||||
|
|
||||||
ENV PATH="/opt/wabt:${PATH}"
|
|
||||||
WORKDIR /src
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
include!("./src/srgb.rs");
|
|
||||||
|
|
||||||
use std::io::Write;
|
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
|
||||||
let mut srgb_to_linear_lut = String::from("static SRGB_TO_LINEAR_LUT: [f32; 256] = [");
|
|
||||||
let mut linear_to_srgb_lut = String::from("static LINEAR_TO_SRGB_LUT: [f32; 256] = [");
|
|
||||||
for i in 0..256 {
|
|
||||||
srgb_to_linear_lut.push_str(&format!("{0:.7}", srgb_to_linear((i as f32) / 255.0)));
|
|
||||||
srgb_to_linear_lut.push_str(",");
|
|
||||||
linear_to_srgb_lut.push_str(&format!("{0:.7}", linear_to_srgb((i as f32) / 255.0)));
|
|
||||||
linear_to_srgb_lut.push_str(",");
|
|
||||||
}
|
|
||||||
srgb_to_linear_lut.pop().unwrap();
|
|
||||||
linear_to_srgb_lut.pop().unwrap();
|
|
||||||
srgb_to_linear_lut.push_str("];");
|
|
||||||
linear_to_srgb_lut.push_str("];");
|
|
||||||
|
|
||||||
let mut file = std::fs::File::create("src/lut.inc")?;
|
|
||||||
file.write_all(srgb_to_linear_lut.as_bytes())?;
|
|
||||||
file.write_all(linear_to_srgb_lut.as_bytes())?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling wasm"
|
|
||||||
echo "============================================="
|
|
||||||
(
|
|
||||||
wasm-pack build
|
|
||||||
wasm-strip pkg/resize_bg.wasm
|
|
||||||
rm pkg/.gitignore
|
|
||||||
)
|
|
||||||
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
4
codecs/resize/package-lock.json
generated
@@ -1,4 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "resize",
|
|
||||||
"lockfileVersion": 1
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "resize",
|
|
||||||
"scripts": {
|
|
||||||
"build:image": "docker build -t squoosh-resize .",
|
|
||||||
"build": "docker run --rm -v $(pwd):/src squoosh-resize ./build.sh"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "resize",
|
|
||||||
"collaborators": [
|
|
||||||
"Surma <surma@surma.link>"
|
|
||||||
],
|
|
||||||
"version": "0.1.0",
|
|
||||||
"files": [
|
|
||||||
"resize_bg.wasm",
|
|
||||||
"resize.js",
|
|
||||||
"resize.d.ts"
|
|
||||||
],
|
|
||||||
"module": "resize.js",
|
|
||||||
"types": "resize.d.ts",
|
|
||||||
"sideEffects": "false"
|
|
||||||
}
|
|
||||||
13
codecs/resize/pkg/resize.d.ts
vendored
13
codecs/resize/pkg/resize.d.ts
vendored
@@ -1,13 +0,0 @@
|
|||||||
/* tslint:disable */
|
|
||||||
/**
|
|
||||||
* @param {Uint8Array} input_image
|
|
||||||
* @param {number} input_width
|
|
||||||
* @param {number} input_height
|
|
||||||
* @param {number} output_width
|
|
||||||
* @param {number} output_height
|
|
||||||
* @param {number} typ_idx
|
|
||||||
* @param {boolean} premultiply
|
|
||||||
* @param {boolean} color_space_conversion
|
|
||||||
* @returns {Uint8Array}
|
|
||||||
*/
|
|
||||||
export function resize(input_image: Uint8Array, input_width: number, input_height: number, output_width: number, output_height: number, typ_idx: number, premultiply: boolean, color_space_conversion: boolean): Uint8Array;
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
import * as wasm from './resize_bg.wasm';
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
let cachegetInt32Memory = null;
|
|
||||||
function getInt32Memory() {
|
|
||||||
if (cachegetInt32Memory === null || cachegetInt32Memory.buffer !== wasm.memory.buffer) {
|
|
||||||
cachegetInt32Memory = new Int32Array(wasm.memory.buffer);
|
|
||||||
}
|
|
||||||
return cachegetInt32Memory;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getArrayU8FromWasm(ptr, len) {
|
|
||||||
return getUint8Memory().subarray(ptr / 1, ptr / 1 + len);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @param {Uint8Array} input_image
|
|
||||||
* @param {number} input_width
|
|
||||||
* @param {number} input_height
|
|
||||||
* @param {number} output_width
|
|
||||||
* @param {number} output_height
|
|
||||||
* @param {number} typ_idx
|
|
||||||
* @param {boolean} premultiply
|
|
||||||
* @param {boolean} color_space_conversion
|
|
||||||
* @returns {Uint8Array}
|
|
||||||
*/
|
|
||||||
export function resize(input_image, input_width, input_height, output_width, output_height, typ_idx, premultiply, color_space_conversion) {
|
|
||||||
const retptr = 8;
|
|
||||||
const ret = wasm.resize(retptr, passArray8ToWasm(input_image), WASM_VECTOR_LEN, input_width, input_height, output_width, output_height, typ_idx, premultiply, color_space_conversion);
|
|
||||||
const memi32 = getInt32Memory();
|
|
||||||
const v0 = getArrayU8FromWasm(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1]).slice();
|
|
||||||
wasm.__wbindgen_free(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1] * 1);
|
|
||||||
return v0;
|
|
||||||
}
|
|
||||||
|
|
||||||
5
codecs/resize/pkg/resize_bg.d.ts
vendored
5
codecs/resize/pkg/resize_bg.d.ts
vendored
@@ -1,5 +0,0 @@
|
|||||||
/* tslint:disable */
|
|
||||||
export const memory: WebAssembly.Memory;
|
|
||||||
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, j: number): void;
|
|
||||||
Binary file not shown.
@@ -1,121 +0,0 @@
|
|||||||
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::*;
|
|
||||||
|
|
||||||
mod srgb;
|
|
||||||
use srgb::Clamp;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
include!("./lut.inc");
|
|
||||||
|
|
||||||
// If `with_space_conversion` is true, this function returns 2 functions that
|
|
||||||
// convert from sRGB to linear RGB and vice versa. If `with_space_conversion` is
|
|
||||||
// false, the 2 functions returned do nothing.
|
|
||||||
fn converter_funcs(with_space_conversion: bool) -> ((fn(u8) -> f32), (fn(f32) -> u8)) {
|
|
||||||
if with_space_conversion {
|
|
||||||
(
|
|
||||||
|v| SRGB_TO_LINEAR_LUT[v as usize] * 255.0,
|
|
||||||
|v| (LINEAR_TO_SRGB_LUT[v as usize] * 255.0) as u8,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
(|v| v as f32, |v| v as u8)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If `with_alpha_premultiplication` is true, this function returns a function
|
|
||||||
// that premultiply the alpha channel with the given channel value and another
|
|
||||||
// function that reverses that process. If `with_alpha_premultiplication` is
|
|
||||||
// false, the functions just return the channel value.
|
|
||||||
fn alpha_multiplier_funcs(
|
|
||||||
with_alpha_premultiplication: bool,
|
|
||||||
) -> ((fn(f32, u8) -> u8), (fn(u8, u8) -> f32)) {
|
|
||||||
if with_alpha_premultiplication {
|
|
||||||
(
|
|
||||||
|v, a| (v * (a as f32) / 255.0) as u8,
|
|
||||||
|v, a| (v as f32) * 255.0 / (a as f32).clamp(0.0, 255.0),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
(|v, _a| v as u8, |v, _a| v as f32)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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,
|
|
||||||
color_space_conversion: 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;
|
|
||||||
|
|
||||||
let (to_linear, to_color_space) = converter_funcs(color_space_conversion);
|
|
||||||
let (premultiplier, demultiplier) = alpha_multiplier_funcs(premultiply);
|
|
||||||
|
|
||||||
// If both options are false, there is no preprocessing on the pixel valus
|
|
||||||
// and we can skip the loop.
|
|
||||||
if premultiply || color_space_conversion {
|
|
||||||
for i in 0..num_input_pixels {
|
|
||||||
for j in 0..3 {
|
|
||||||
input_image[4 * i + j] =
|
|
||||||
premultiplier(to_linear(input_image[4 * i + j]), input_image[4 * i + 3]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 || color_space_conversion {
|
|
||||||
for i in 0..num_output_pixels {
|
|
||||||
for j in 0..3 {
|
|
||||||
// We don’t 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] = to_color_space(demultiplier(
|
|
||||||
output_image[4 * i + j],
|
|
||||||
output_image[4 * i + 3],
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return output_image;
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
pub trait Clamp: std::cmp::PartialOrd + Sized {
|
|
||||||
fn clamp(self, min: Self, max: Self) -> Self {
|
|
||||||
if self.lt(&min) {
|
|
||||||
min
|
|
||||||
} else if self.gt(&max) {
|
|
||||||
max
|
|
||||||
} else {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Clamp for f32 {}
|
|
||||||
|
|
||||||
pub fn srgb_to_linear(v: f32) -> f32 {
|
|
||||||
if v < 0.04045 {
|
|
||||||
v / 12.92
|
|
||||||
} else {
|
|
||||||
((v + 0.055) / 1.055).powf(2.4).clamp(0.0, 1.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn linear_to_srgb(v: f32) -> f32 {
|
|
||||||
if v < 0.0031308 {
|
|
||||||
v * 12.92
|
|
||||||
} else {
|
|
||||||
(1.055 * v.powf(1.0 / 2.4) - 0.055).clamp(0.0, 1.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
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
2
codecs/rotate/.gitignore
vendored
@@ -1,2 +0,0 @@
|
|||||||
target
|
|
||||||
Cargo.lock
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
[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"
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
FROM rust
|
|
||||||
RUN rustup target add wasm32-unknown-unknown
|
|
||||||
|
|
||||||
RUN mkdir /opt/wabt && \
|
|
||||||
curl -L https://github.com/WebAssembly/wabt/releases/download/1.0.11/wabt-1.0.11-linux.tar.gz | tar -xzf - -C /opt/wabt --strip 1
|
|
||||||
|
|
||||||
ENV PATH="/opt/wabt:${PATH}"
|
|
||||||
WORKDIR /src
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
// 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. Don’t 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));
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling wasm"
|
|
||||||
echo "============================================="
|
|
||||||
(
|
|
||||||
cargo build \
|
|
||||||
--target wasm32-unknown-unknown \
|
|
||||||
--release
|
|
||||||
cp target/wasm32-unknown-unknown/release/rotate.wasm .
|
|
||||||
wasm-strip rotate.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-rotate .\`"
|
|
||||||
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "rotate",
|
|
||||||
"scripts": {
|
|
||||||
"build:image": "docker build -t squoosh-rotate .",
|
|
||||||
"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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,113 +0,0 @@
|
|||||||
use std::slice::{from_raw_parts, 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(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(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Binary file not shown.
@@ -1,22 +0,0 @@
|
|||||||
# WebP decoder
|
|
||||||
|
|
||||||
- Source: <https://github.com/webmproject/libwebp>
|
|
||||||
- Version: v1.0.2
|
|
||||||
|
|
||||||
## Example
|
|
||||||
|
|
||||||
See `example.html`
|
|
||||||
|
|
||||||
## API
|
|
||||||
|
|
||||||
### `int version()`
|
|
||||||
|
|
||||||
Returns the version of libwebp as a number. va.b.c is encoded as 0x0a0b0c
|
|
||||||
|
|
||||||
### `RawImage decode(std::string buffer)`
|
|
||||||
|
|
||||||
Decodes the given webp buffer into raw RGBA. `RawImage` is a class with 3 fields: `buffer`, `width`, and `height`.
|
|
||||||
|
|
||||||
### `void free_result()`
|
|
||||||
|
|
||||||
Frees the result created by `decode()`.
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
export OPTIMIZE="-Os"
|
|
||||||
export LDFLAGS="${OPTIMIZE}"
|
|
||||||
export CFLAGS="${OPTIMIZE}"
|
|
||||||
export CPPFLAGS="${OPTIMIZE}"
|
|
||||||
apt-get update
|
|
||||||
apt-get install -qqy autoconf libtool libpng-dev pkg-config
|
|
||||||
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling libwebp"
|
|
||||||
echo "============================================="
|
|
||||||
test -n "$SKIP_LIBWEBP" || (
|
|
||||||
cd node_modules/libwebp
|
|
||||||
autoreconf -fiv
|
|
||||||
rm -rf build || true
|
|
||||||
mkdir -p build && cd build
|
|
||||||
emconfigure ../configure \
|
|
||||||
--disable-libwebpdemux \
|
|
||||||
--disable-wic \
|
|
||||||
--disable-gif \
|
|
||||||
--disable-tiff \
|
|
||||||
--disable-jpeg \
|
|
||||||
--disable-png \
|
|
||||||
--disable-sdl \
|
|
||||||
--disable-gl \
|
|
||||||
--disable-threading \
|
|
||||||
--disable-neon-rtcd \
|
|
||||||
--disable-neon \
|
|
||||||
--disable-sse2 \
|
|
||||||
--disable-sse4.1
|
|
||||||
emmake make
|
|
||||||
)
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling wasm bindings"
|
|
||||||
echo "============================================="
|
|
||||||
(
|
|
||||||
emcc \
|
|
||||||
${OPTIMIZE} \
|
|
||||||
--bind \
|
|
||||||
-s ALLOW_MEMORY_GROWTH=1 \
|
|
||||||
-s MODULARIZE=1 \
|
|
||||||
-s 'EXPORT_NAME="webp_dec"' \
|
|
||||||
--std=c++11 \
|
|
||||||
-I node_modules/libwebp \
|
|
||||||
-o ./webp_dec.js \
|
|
||||||
-x c++ \
|
|
||||||
webp_dec.cpp \
|
|
||||||
node_modules/libwebp/build/src/.libs/libwebp.a
|
|
||||||
)
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling wasm bindings done"
|
|
||||||
echo "============================================="
|
|
||||||
|
|
||||||
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
||||||
echo "Did you update your docker image?"
|
|
||||||
echo "Run \`docker pull trzeci/emscripten\`"
|
|
||||||
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
<!doctype html>
|
|
||||||
<script src='webp_dec.js'></script>
|
|
||||||
<script>
|
|
||||||
const Module = webp_dec();
|
|
||||||
|
|
||||||
async function loadFile(src) {
|
|
||||||
const resp = await fetch(src);
|
|
||||||
return await resp.arrayBuffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
Module.onRuntimeInitialized = async _ => {
|
|
||||||
console.log('Version:', Module.version().toString(16));
|
|
||||||
const image = await loadFile('../example.webp');
|
|
||||||
const result = Module.decode(image);
|
|
||||||
const imageData = new ImageData(new Uint8ClampedArray(result.buffer), result.width, result.height);
|
|
||||||
Module.free_result();
|
|
||||||
const canvas = document.createElement('canvas');
|
|
||||||
canvas.width = result.width;
|
|
||||||
canvas.height = result.height;
|
|
||||||
document.body.appendChild(canvas);
|
|
||||||
const ctx = canvas.getContext('2d');
|
|
||||||
ctx.putImageData(imageData, 0, 0);
|
|
||||||
};
|
|
||||||
</script>
|
|
||||||
1147
codecs/webp_dec/package-lock.json
generated
1147
codecs/webp_dec/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,13 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "webp_dec",
|
|
||||||
"scripts": {
|
|
||||||
"install": "napa",
|
|
||||||
"build": "docker run --rm -v $(pwd):/src trzeci/emscripten ./build.sh"
|
|
||||||
},
|
|
||||||
"napa": {
|
|
||||||
"libwebp": "webmproject/libwebp#v1.0.2"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"napa": "3.0.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
#include "emscripten/bind.h"
|
|
||||||
#include "emscripten/val.h"
|
|
||||||
#include "src/webp/decode.h"
|
|
||||||
#include "src/webp/demux.h"
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
using namespace emscripten;
|
|
||||||
|
|
||||||
int version() {
|
|
||||||
return WebPGetDecoderVersion();
|
|
||||||
}
|
|
||||||
|
|
||||||
class RawImage {
|
|
||||||
public:
|
|
||||||
val buffer;
|
|
||||||
int width;
|
|
||||||
int height;
|
|
||||||
|
|
||||||
RawImage(val b, int w, int h)
|
|
||||||
: buffer(b), width(w), height(h) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
uint8_t* last_result;
|
|
||||||
RawImage decode(std::string buffer) {
|
|
||||||
int width, height;
|
|
||||||
last_result = WebPDecodeRGBA((const uint8_t*)buffer.c_str(), buffer.size(), &width, &height);
|
|
||||||
return RawImage(
|
|
||||||
val(typed_memory_view(width*height*4, last_result)),
|
|
||||||
width,
|
|
||||||
height
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
void free_result() {
|
|
||||||
free(last_result);
|
|
||||||
}
|
|
||||||
|
|
||||||
EMSCRIPTEN_BINDINGS(my_module) {
|
|
||||||
class_<RawImage>("RawImage")
|
|
||||||
.property("buffer", &RawImage::buffer)
|
|
||||||
.property("width", &RawImage::width)
|
|
||||||
.property("height", &RawImage::height);
|
|
||||||
|
|
||||||
function("decode", &decode);
|
|
||||||
function("version", &version);
|
|
||||||
function("free_result", &free_result);
|
|
||||||
}
|
|
||||||
13
codecs/webp_dec/webp_dec.d.ts
vendored
13
codecs/webp_dec/webp_dec.d.ts
vendored
@@ -1,13 +0,0 @@
|
|||||||
interface RawImage {
|
|
||||||
buffer: Uint8Array;
|
|
||||||
width: number;
|
|
||||||
height: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface WebPModule extends EmscriptenWasm.Module {
|
|
||||||
decode(data: BufferSource): RawImage;
|
|
||||||
free_result(): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function(opts: EmscriptenWasm.ModuleOpts): WebPModule;
|
|
||||||
|
|
||||||
File diff suppressed because one or more lines are too long
Binary file not shown.
@@ -1,7 +1,7 @@
|
|||||||
# WebP encoder
|
# WebP encoder
|
||||||
|
|
||||||
- Source: <https://github.com/webmproject/libwebp>
|
- Source: <https://github.com/webmproject/libwebp>
|
||||||
- Version: v1.0.2
|
- Version: v0.6.1
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
@@ -17,10 +17,26 @@ See `example.html`
|
|||||||
|
|
||||||
Returns the version of libwebp as a number. va.b.c is encoded as 0x0a0b0c
|
Returns the version of libwebp as a number. va.b.c is encoded as 0x0a0b0c
|
||||||
|
|
||||||
### `UInt8Array encode(uint8_t* image_buffer, int image_width, int image_height, WebPConfig config)`
|
### `uint8_t* create_buffer(int width, int height)`
|
||||||
|
|
||||||
Encodes the given image with given dimension to WebP.
|
Allocates an RGBA buffer for an image with the given dimension.
|
||||||
|
|
||||||
|
### `void destroy_buffer(uint8_t* p)`
|
||||||
|
|
||||||
|
Frees a buffer created with `create_buffer`.
|
||||||
|
|
||||||
|
### `void encode(uint8_t* image_buffer, int image_width, int image_height, float quality)`
|
||||||
|
|
||||||
|
Encodes the given image with given dimension to WebP. `quality` is a number between 0 and 100. The higher the number, the better the quality of the encoded image. The result is implicitly stored and can be accessed using the `get_result_*()` functions.
|
||||||
|
|
||||||
### `void free_result()`
|
### `void free_result()`
|
||||||
|
|
||||||
Frees the last result created by `encode()`.
|
Frees the result created by `encode()`.
|
||||||
|
|
||||||
|
### `int get_result_pointer()`
|
||||||
|
|
||||||
|
Returns the pointer to the start of the buffer holding the encoded data.
|
||||||
|
|
||||||
|
### `int get_result_size()`
|
||||||
|
|
||||||
|
Returns the length of the buffer holding the encoded data.
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
export OPTIMIZE="-Os"
|
|
||||||
export LDFLAGS="${OPTIMIZE}"
|
|
||||||
export CFLAGS="${OPTIMIZE}"
|
|
||||||
export CPPFLAGS="${OPTIMIZE}"
|
|
||||||
|
|
||||||
apt-get update
|
|
||||||
apt-get install -qqy autoconf libtool libpng-dev pkg-config
|
|
||||||
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling libwebp"
|
|
||||||
echo "============================================="
|
|
||||||
test -n "$SKIP_LIBWEBP" || (
|
|
||||||
cd node_modules/libwebp
|
|
||||||
autoreconf -fiv
|
|
||||||
rm -rf build || true
|
|
||||||
mkdir -p build && cd build
|
|
||||||
emconfigure ../configure \
|
|
||||||
--disable-libwebpdemux \
|
|
||||||
--disable-wic \
|
|
||||||
--disable-gif \
|
|
||||||
--disable-tiff \
|
|
||||||
--disable-jpeg \
|
|
||||||
--disable-png \
|
|
||||||
--disable-sdl \
|
|
||||||
--disable-gl \
|
|
||||||
--disable-threading \
|
|
||||||
--disable-neon-rtcd \
|
|
||||||
--disable-neon \
|
|
||||||
--disable-sse2 \
|
|
||||||
--disable-sse4.1
|
|
||||||
emmake make
|
|
||||||
)
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling wasm bindings"
|
|
||||||
echo "============================================="
|
|
||||||
(
|
|
||||||
emcc \
|
|
||||||
${OPTIMIZE} \
|
|
||||||
--bind \
|
|
||||||
-s ALLOW_MEMORY_GROWTH=1 \
|
|
||||||
-s MODULARIZE=1 \
|
|
||||||
-s 'EXPORT_NAME="webp_enc"' \
|
|
||||||
--std=c++11 \
|
|
||||||
-I node_modules/libwebp \
|
|
||||||
-o ./webp_enc.js \
|
|
||||||
-x c++ \
|
|
||||||
webp_enc.cpp \
|
|
||||||
node_modules/libwebp/build/src/.libs/libwebp.a
|
|
||||||
)
|
|
||||||
echo "============================================="
|
|
||||||
echo "Compiling wasm bindings done"
|
|
||||||
echo "============================================="
|
|
||||||
|
|
||||||
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
||||||
echo "Did you update your docker image?"
|
|
||||||
echo "Run \`docker pull trzeci/emscripten\`"
|
|
||||||
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<!doctype html>
|
<!doctype html>
|
||||||
<script src='webp_enc.js'></script>
|
<script src='webp_enc.js'></script>
|
||||||
<script>
|
<script>
|
||||||
const module = webp_enc();
|
const Module = webp_enc();
|
||||||
|
|
||||||
async function loadImage(src) {
|
async function loadImage(src) {
|
||||||
// Load image
|
// Load image
|
||||||
@@ -17,43 +17,28 @@
|
|||||||
return ctx.getImageData(0, 0, img.width, img.height);
|
return ctx.getImageData(0, 0, img.width, img.height);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.onRuntimeInitialized = async _ => {
|
Module.onRuntimeInitialized = async _ => {
|
||||||
console.log('Version:', module.version().toString(16));
|
const api = {
|
||||||
|
version: Module.cwrap('version', 'number', []),
|
||||||
|
create_buffer: Module.cwrap('create_buffer', 'number', ['number', 'number']),
|
||||||
|
destroy_buffer: Module.cwrap('destroy_buffer', '', ['number']),
|
||||||
|
encode: Module.cwrap('encode', '', ['number', 'number', 'number', 'number']),
|
||||||
|
free_result: Module.cwrap('free_result', '', ['number']),
|
||||||
|
get_result_pointer: Module.cwrap('get_result_pointer', 'number', []),
|
||||||
|
get_result_size: Module.cwrap('get_result_size', 'number', []),
|
||||||
|
};
|
||||||
const image = await loadImage('../example.png');
|
const image = await loadImage('../example.png');
|
||||||
const result = module.encode(image.data, image.width, image.height, {
|
const p = api.create_buffer(image.width, image.height);
|
||||||
quality: 75,
|
Module.HEAP8.set(image.data, p);
|
||||||
target_size: 0,
|
api.encode(p, image.width, image.height, 2);
|
||||||
target_PSNR: 0,
|
const resultPointer = api.get_result_pointer();
|
||||||
method: 4,
|
const resultSize = api.get_result_size();
|
||||||
sns_strength: 50,
|
const resultView = new Uint8Array(Module.HEAP8.buffer, resultPointer, resultSize);
|
||||||
filter_strength: 60,
|
const result = new Uint8Array(resultView);
|
||||||
filter_sharpness: 0,
|
api.free_result(resultPointer);
|
||||||
filter_type: 1,
|
api.destroy_buffer(p);
|
||||||
partitions: 0,
|
|
||||||
segments: 4,
|
|
||||||
pass: 1,
|
|
||||||
show_compressed: 0,
|
|
||||||
preprocessing: 0,
|
|
||||||
autofilter: 0,
|
|
||||||
partition_limit: 0,
|
|
||||||
alpha_compression: 1,
|
|
||||||
alpha_filtering: 1,
|
|
||||||
alpha_quality: 100,
|
|
||||||
lossless: 0,
|
|
||||||
exact: 0,
|
|
||||||
image_hint: 0,
|
|
||||||
emulate_jpeg_size: 0,
|
|
||||||
thread_level: 0,
|
|
||||||
low_memory: 0,
|
|
||||||
near_lossless: 100,
|
|
||||||
use_delta_palette: 0,
|
|
||||||
use_sharp_yuv: 0,
|
|
||||||
});
|
|
||||||
console.log('size', result.length);
|
|
||||||
const blob = new Blob([result], {type: 'image/webp'});
|
|
||||||
|
|
||||||
module.free_result();
|
|
||||||
|
|
||||||
|
const blob = new Blob([result], {type: 'image/jpeg'});
|
||||||
const blobURL = URL.createObjectURL(blob);
|
const blobURL = URL.createObjectURL(blob);
|
||||||
const img = document.createElement('img');
|
const img = document.createElement('img');
|
||||||
img.src = blobURL;
|
img.src = blobURL;
|
||||||
|
|||||||
@@ -2,12 +2,12 @@
|
|||||||
"name": "webp_enc",
|
"name": "webp_enc",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"install": "napa",
|
"install": "napa",
|
||||||
"build": "docker run --rm -v $(pwd):/src trzeci/emscripten ./build.sh"
|
"build": "docker run --rm -v $(pwd):/src trzeci/emscripten emcc -O3 -s WASM=1 -s EXTRA_EXPORTED_RUNTIME_METHODS='[\"cwrap\"]' -s ALLOW_MEMORY_GROWTH=1 -s MODULARIZE=1 -s 'EXPORT_NAME=\"webp_enc\"' -I node_modules/libwebp -o ./webp_enc.js webp_enc.c node_modules/libwebp/src/{dec,dsp,demux,enc,mux,utils}/*.c"
|
||||||
},
|
},
|
||||||
"napa": {
|
"napa": {
|
||||||
"libwebp": "webmproject/libwebp#v1.0.2"
|
"libwebp": "webmproject/libwebp#v0.6.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"napa": "3.0.0"
|
"napa": "^3.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
44
codecs/webp_enc/webp_enc.c
Normal file
44
codecs/webp_enc/webp_enc.c
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#include "emscripten.h"
|
||||||
|
#include "src/webp/encode.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
EMSCRIPTEN_KEEPALIVE
|
||||||
|
int version() {
|
||||||
|
return WebPGetEncoderVersion();
|
||||||
|
}
|
||||||
|
|
||||||
|
EMSCRIPTEN_KEEPALIVE
|
||||||
|
uint8_t* create_buffer(int width, int height) {
|
||||||
|
return malloc(width * height * 4 * sizeof(uint8_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
EMSCRIPTEN_KEEPALIVE
|
||||||
|
void destroy_buffer(uint8_t* p) {
|
||||||
|
free(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
int result[2];
|
||||||
|
EMSCRIPTEN_KEEPALIVE
|
||||||
|
void encode(uint8_t* img_in, int width, int height, float quality) {
|
||||||
|
uint8_t* img_out;
|
||||||
|
size_t size;
|
||||||
|
size = WebPEncodeRGBA(img_in, width, height, width * 4, quality, &img_out);
|
||||||
|
result[0] = (int)img_out;
|
||||||
|
result[1] = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
EMSCRIPTEN_KEEPALIVE
|
||||||
|
void free_result() {
|
||||||
|
WebPFree(result[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
EMSCRIPTEN_KEEPALIVE
|
||||||
|
int get_result_pointer() {
|
||||||
|
return result[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
EMSCRIPTEN_KEEPALIVE
|
||||||
|
int get_result_size() {
|
||||||
|
return result[1];
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,96 +0,0 @@
|
|||||||
#include <emscripten/bind.h>
|
|
||||||
#include <emscripten/val.h>
|
|
||||||
#include "src/webp/encode.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
using namespace emscripten;
|
|
||||||
|
|
||||||
int version() {
|
|
||||||
return WebPGetEncoderVersion();
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t* last_result;
|
|
||||||
|
|
||||||
val encode(std::string img, int width, int height, WebPConfig config) {
|
|
||||||
uint8_t* img_in = (uint8_t*) img.c_str();
|
|
||||||
|
|
||||||
// A lot of this is duplicated from Encode in picture_enc.c
|
|
||||||
WebPPicture pic;
|
|
||||||
WebPMemoryWriter wrt;
|
|
||||||
int ok;
|
|
||||||
|
|
||||||
if (!WebPPictureInit(&pic)) {
|
|
||||||
// shouldn't happen, except if system installation is broken
|
|
||||||
throw std::runtime_error("Unexpected error");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only use use_argb if we really need it, as it's slower.
|
|
||||||
pic.use_argb = config.lossless || config.use_sharp_yuv || config.preprocessing > 0;
|
|
||||||
pic.width = width;
|
|
||||||
pic.height = height;
|
|
||||||
pic.writer = WebPMemoryWrite;
|
|
||||||
pic.custom_ptr = &wrt;
|
|
||||||
|
|
||||||
WebPMemoryWriterInit(&wrt);
|
|
||||||
|
|
||||||
ok = WebPPictureImportRGBA(&pic, (uint8_t*) img_in, width * 4) && WebPEncode(&config, &pic);
|
|
||||||
WebPPictureFree(&pic);
|
|
||||||
if (!ok) {
|
|
||||||
WebPMemoryWriterClear(&wrt);
|
|
||||||
throw std::runtime_error("Encode failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
last_result = wrt.mem;
|
|
||||||
|
|
||||||
return val(typed_memory_view(wrt.size, wrt.mem));
|
|
||||||
}
|
|
||||||
|
|
||||||
void free_result() {
|
|
||||||
WebPFree(last_result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
EMSCRIPTEN_BINDINGS(my_module) {
|
|
||||||
enum_<WebPImageHint>("WebPImageHint")
|
|
||||||
.value("WEBP_HINT_DEFAULT", WebPImageHint::WEBP_HINT_DEFAULT)
|
|
||||||
.value("WEBP_HINT_PICTURE", WebPImageHint::WEBP_HINT_PICTURE)
|
|
||||||
.value("WEBP_HINT_PHOTO", WebPImageHint::WEBP_HINT_PHOTO)
|
|
||||||
.value("WEBP_HINT_GRAPH", WebPImageHint::WEBP_HINT_GRAPH)
|
|
||||||
;
|
|
||||||
|
|
||||||
value_object<WebPConfig>("WebPConfig")
|
|
||||||
.field("lossless", &WebPConfig::lossless)
|
|
||||||
.field("quality", &WebPConfig::quality)
|
|
||||||
.field("method", &WebPConfig::method)
|
|
||||||
.field("image_hint", &WebPConfig::image_hint)
|
|
||||||
.field("target_size", &WebPConfig::target_size)
|
|
||||||
.field("target_PSNR", &WebPConfig::target_PSNR)
|
|
||||||
.field("segments", &WebPConfig::segments)
|
|
||||||
.field("sns_strength", &WebPConfig::sns_strength)
|
|
||||||
.field("filter_strength", &WebPConfig::filter_strength)
|
|
||||||
.field("filter_sharpness", &WebPConfig::filter_sharpness)
|
|
||||||
.field("filter_type", &WebPConfig::filter_type)
|
|
||||||
.field("autofilter", &WebPConfig::autofilter)
|
|
||||||
.field("alpha_compression", &WebPConfig::alpha_compression)
|
|
||||||
.field("alpha_filtering", &WebPConfig::alpha_filtering)
|
|
||||||
.field("alpha_quality", &WebPConfig::alpha_quality)
|
|
||||||
.field("pass", &WebPConfig::pass)
|
|
||||||
.field("show_compressed", &WebPConfig::show_compressed)
|
|
||||||
.field("preprocessing", &WebPConfig::preprocessing)
|
|
||||||
.field("partitions", &WebPConfig::partitions)
|
|
||||||
.field("partition_limit", &WebPConfig::partition_limit)
|
|
||||||
.field("emulate_jpeg_size", &WebPConfig::emulate_jpeg_size)
|
|
||||||
.field("thread_level", &WebPConfig::thread_level)
|
|
||||||
.field("low_memory", &WebPConfig::low_memory)
|
|
||||||
.field("near_lossless", &WebPConfig::near_lossless)
|
|
||||||
.field("exact", &WebPConfig::exact)
|
|
||||||
.field("use_delta_palette", &WebPConfig::use_delta_palette)
|
|
||||||
.field("use_sharp_yuv", &WebPConfig::use_sharp_yuv)
|
|
||||||
;
|
|
||||||
|
|
||||||
function("version", &version);
|
|
||||||
function("encode", &encode);
|
|
||||||
function("free_result", &free_result);
|
|
||||||
}
|
|
||||||
9
codecs/webp_enc/webp_enc.d.ts
vendored
9
codecs/webp_enc/webp_enc.d.ts
vendored
@@ -1,9 +0,0 @@
|
|||||||
import { EncodeOptions } from '../../src/codecs/webp/encoder-meta';
|
|
||||||
|
|
||||||
interface WebPModule extends EmscriptenWasm.Module {
|
|
||||||
encode(data: BufferSource, width: number, height: number, options: EncodeOptions): Uint8Array;
|
|
||||||
free_result(): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export default function(opts: EmscriptenWasm.ModuleOpts): WebPModule;
|
|
||||||
File diff suppressed because one or more lines are too long
Binary file not shown.
@@ -1,74 +0,0 @@
|
|||||||
const DtsCreator = require('typed-css-modules');
|
|
||||||
const chokidar = require('chokidar');
|
|
||||||
const util = require('util');
|
|
||||||
const sass = require('node-sass');
|
|
||||||
|
|
||||||
const sassRender = util.promisify(sass.render);
|
|
||||||
|
|
||||||
async function sassToCss(path) {
|
|
||||||
const result = await sassRender({ file: path });
|
|
||||||
return result.css;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {Object} Opts
|
|
||||||
* @property {boolean} watch Watch for changes
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Create typing files for CSS & SCSS.
|
|
||||||
*
|
|
||||||
* @param {string[]} rootPaths Paths to search within
|
|
||||||
* @param {Opts} [opts={}] Options.
|
|
||||||
*/
|
|
||||||
function addCssTypes(rootPaths, opts = {}) {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
const { watch = false } = opts;
|
|
||||||
|
|
||||||
const paths = [];
|
|
||||||
const preReadyPromises = [];
|
|
||||||
let ready = false;
|
|
||||||
|
|
||||||
for (const rootPath of rootPaths) {
|
|
||||||
// Look for scss & css in each path.
|
|
||||||
paths.push(rootPath + '/**/*.scss');
|
|
||||||
paths.push(rootPath + '/**/*.css');
|
|
||||||
}
|
|
||||||
|
|
||||||
// For simplicity, the watcher is used even if we're not watching.
|
|
||||||
// If we're not watching, we stop the watcher after the initial files are found.
|
|
||||||
const watcher = chokidar.watch(paths, {
|
|
||||||
// Avoid processing already-processed files.
|
|
||||||
ignored: '*.d.*',
|
|
||||||
// Without this, travis and netlify builds never complete. I'm not sure why, but it might be
|
|
||||||
// related to https://github.com/paulmillr/chokidar/pull/758
|
|
||||||
persistent: watch,
|
|
||||||
});
|
|
||||||
|
|
||||||
function change(path) {
|
|
||||||
const promise = (async function() {
|
|
||||||
const creator = new DtsCreator({ camelCase: true });
|
|
||||||
const result = path.endsWith('.scss') ?
|
|
||||||
await creator.create(path, await sassToCss(path)) :
|
|
||||||
await creator.create(path);
|
|
||||||
|
|
||||||
await result.writeFile();
|
|
||||||
})();
|
|
||||||
|
|
||||||
if (!ready) preReadyPromises.push(promise);
|
|
||||||
}
|
|
||||||
|
|
||||||
watcher.on('change', change);
|
|
||||||
watcher.on('add', change);
|
|
||||||
|
|
||||||
// 'ready' is when events have been fired for file discovery.
|
|
||||||
watcher.on('ready', () => {
|
|
||||||
ready = true;
|
|
||||||
// Wait for the current set of processing to finish.
|
|
||||||
Promise.all(preReadyPromises).then(resolve);
|
|
||||||
// And if we're not watching, close the watcher.
|
|
||||||
if (!watch) watcher.close();
|
|
||||||
});
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = addCssTypes;
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
const path = require('path');
|
|
||||||
const fs = require('fs');
|
|
||||||
const ejs = require('ejs');
|
|
||||||
const AssetsPlugin = require('assets-webpack-plugin');
|
|
||||||
|
|
||||||
module.exports = class AssetTemplatePlugin extends AssetsPlugin {
|
|
||||||
constructor(options) {
|
|
||||||
options = options || {};
|
|
||||||
if (!options.template) throw Error('AssetTemplatePlugin: template option is required.');
|
|
||||||
super({
|
|
||||||
useCompilerPath: true,
|
|
||||||
filename: options.filename,
|
|
||||||
processOutput: files => this._processOutput(files)
|
|
||||||
});
|
|
||||||
this._template = path.resolve(process.cwd(), options.template);
|
|
||||||
const ignore = options.ignore || /(manifest\.json|\.DS_Store)$/;
|
|
||||||
this._ignore = typeof ignore === 'function' ? ({ test: ignore }) : ignore;
|
|
||||||
}
|
|
||||||
|
|
||||||
_processOutput(files) {
|
|
||||||
const mapping = {
|
|
||||||
all: [],
|
|
||||||
byType: {},
|
|
||||||
entries: {}
|
|
||||||
};
|
|
||||||
for (const entryName in files) {
|
|
||||||
// non-entry-point-derived assets are collected under an empty string key
|
|
||||||
// since that's a bit awkward, we'll call them "assets"
|
|
||||||
const name = entryName === '' ? 'assets' : entryName;
|
|
||||||
const listing = files[entryName];
|
|
||||||
const entry = mapping.entries[name] = {
|
|
||||||
all: [],
|
|
||||||
byType: {}
|
|
||||||
};
|
|
||||||
for (let type in listing) {
|
|
||||||
const list = [].concat(listing[type]).filter(file => !this._ignore.test(file));
|
|
||||||
if (!list.length) continue;
|
|
||||||
mapping.all = mapping.all.concat(list);
|
|
||||||
mapping.byType[type] = (mapping.byType[type] || []).concat(list);
|
|
||||||
entry.all = entry.all.concat(list);
|
|
||||||
entry.byType[type] = (entry.byType[type] || []).concat(list);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mapping.files = mapping.all;
|
|
||||||
return ejs.render(fs.readFileSync(this._template, 'utf8'), mapping);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1,158 +0,0 @@
|
|||||||
const util = require('util');
|
|
||||||
const minimatch = require('minimatch');
|
|
||||||
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
|
|
||||||
const WebWorkerTemplatePlugin = require('webpack/lib/webworker/WebWorkerTemplatePlugin');
|
|
||||||
const ParserHelpers = require('webpack/lib/ParserHelpers');
|
|
||||||
|
|
||||||
const NAME = 'auto-sw-plugin';
|
|
||||||
const JS_TYPES = ['auto', 'esm', 'dynamic'];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Automatically finds and bundles Service Workers by looking for navigator.serviceWorker.register(..).
|
|
||||||
* An Array of webpack assets is injected into the Service Worker bundle as a `BUILD_ASSETS` global.
|
|
||||||
* Hidden and `.map` files are excluded by default, and this can be customized using the include & exclude options.
|
|
||||||
* @example
|
|
||||||
* // webpack config
|
|
||||||
* plugins: [
|
|
||||||
* new AutoSWPlugin({
|
|
||||||
* exclude: [
|
|
||||||
* '**\/.*', // don't expose hidden files (default)
|
|
||||||
* '**\/*.map', // don't precache sourcemaps (default)
|
|
||||||
* 'index.html' // don't cache the page itself
|
|
||||||
* ]
|
|
||||||
* })
|
|
||||||
* ]
|
|
||||||
* @param {Object} [options={}]
|
|
||||||
* @param {string[]} [options.exclude] Minimatch pattern(s) of which assets to omit from BUILD_ASSETS.
|
|
||||||
* @param {string[]} [options.include] Minimatch pattern(s) of assets to allow in BUILD_ASSETS.
|
|
||||||
*/
|
|
||||||
module.exports = class AutoSWPlugin {
|
|
||||||
constructor(options) {
|
|
||||||
this.options = Object.assign({
|
|
||||||
exclude: [
|
|
||||||
'**/*.map',
|
|
||||||
'**/.*'
|
|
||||||
]
|
|
||||||
}, options || {});
|
|
||||||
}
|
|
||||||
|
|
||||||
apply(compiler) {
|
|
||||||
const serviceWorkers = [];
|
|
||||||
|
|
||||||
compiler.hooks.emit.tapPromise(NAME, compilation => this.emit(compiler, compilation, serviceWorkers));
|
|
||||||
|
|
||||||
compiler.hooks.normalModuleFactory.tap(NAME, (factory) => {
|
|
||||||
for (const type of JS_TYPES) {
|
|
||||||
factory.hooks.parser.for(`javascript/${type}`).tap(NAME, parser => {
|
|
||||||
let counter = 0;
|
|
||||||
|
|
||||||
const processRegisterCall = expr => {
|
|
||||||
const dep = parser.evaluateExpression(expr.arguments[0]);
|
|
||||||
|
|
||||||
if (!dep.isString()) {
|
|
||||||
parser.state.module.warnings.push({
|
|
||||||
message: 'navigator.serviceWorker.register() will only be bundled if passed a String literal.'
|
|
||||||
});
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const filename = dep.string;
|
|
||||||
const outputFilename = this.options.filename || 'serviceworker.js'
|
|
||||||
const context = parser.state.current.context;
|
|
||||||
serviceWorkers.push({
|
|
||||||
outputFilename,
|
|
||||||
filename,
|
|
||||||
context
|
|
||||||
});
|
|
||||||
|
|
||||||
const id = `__webpack__serviceworker__${++counter}`;
|
|
||||||
ParserHelpers.toConstantDependency(parser, id)(expr.arguments[0]);
|
|
||||||
return ParserHelpers.addParsedVariableToModule(parser, id, '__webpack_public_path__ + ' + JSON.stringify(outputFilename));
|
|
||||||
};
|
|
||||||
|
|
||||||
parser.hooks.call.for('navigator.serviceWorker.register').tap(NAME, processRegisterCall);
|
|
||||||
parser.hooks.call.for('self.navigator.serviceWorker.register').tap(NAME, processRegisterCall);
|
|
||||||
parser.hooks.call.for('window.navigator.serviceWorker.register').tap(NAME, processRegisterCall);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
createFilter(list) {
|
|
||||||
const filters = [].concat(list);
|
|
||||||
for (let i=0; i<filters.length; i++) {
|
|
||||||
if (typeof filters[i] === 'string') {
|
|
||||||
filters[i] = minimatch.filter(filters[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return filters;
|
|
||||||
}
|
|
||||||
|
|
||||||
async emit(compiler, compilation, serviceWorkers) {
|
|
||||||
let assetMapping = Object.keys(compilation.assets);
|
|
||||||
if (this.options.include) {
|
|
||||||
const filters = this.createFilter(this.options.include);
|
|
||||||
assetMapping = assetMapping.filter(filename => {
|
|
||||||
for (const filter of filters) {
|
|
||||||
if (filter(filename)) return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (this.options.exclude) {
|
|
||||||
const filters = this.createFilter(this.options.exclude);
|
|
||||||
assetMapping = assetMapping.filter(filename => {
|
|
||||||
for (const filter of filters) {
|
|
||||||
if (filter(filename)) return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
await Promise.all(serviceWorkers.map(
|
|
||||||
(serviceWorker, index) => this.compileServiceWorker(compiler, compilation, serviceWorker, index, assetMapping)
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
async compileServiceWorker(compiler, compilation, options, index, assetMapping) {
|
|
||||||
const entryFilename = options.filename;
|
|
||||||
|
|
||||||
const chunkFilename = compiler.options.output.chunkFilename.replace(/\.([a-z]+)$/i, '.serviceworker.$1');
|
|
||||||
const workerOptions = {
|
|
||||||
filename: options.outputFilename, // chunkFilename.replace(/\.?\[(?:chunkhash|contenthash|hash)(:\d+(?::\d+)?)?\]/g, ''),
|
|
||||||
chunkFilename: this.options.chunkFilename || chunkFilename,
|
|
||||||
globalObject: 'self'
|
|
||||||
};
|
|
||||||
|
|
||||||
const childCompiler = compilation.createChildCompiler(NAME, { filename: workerOptions.filename });
|
|
||||||
(new WebWorkerTemplatePlugin(workerOptions)).apply(childCompiler);
|
|
||||||
|
|
||||||
/* The duplication DefinePlugin ends up causing is problematic (it doesn't hoist injections), so we'll do it manually. */
|
|
||||||
// (new DefinePlugin({
|
|
||||||
// BUILD_ASSETS: JSON.stringify(assetMapping)
|
|
||||||
// })).apply(childCompiler);
|
|
||||||
(new SingleEntryPlugin(options.context, entryFilename, workerOptions.filename)).apply(childCompiler);
|
|
||||||
|
|
||||||
const subCache = `subcache ${__dirname} ${entryFilename} ${index}`;
|
|
||||||
let childCompilation;
|
|
||||||
childCompiler.hooks.compilation.tap(NAME, c => {
|
|
||||||
childCompilation = c;
|
|
||||||
if (childCompilation.cache) {
|
|
||||||
if (!childCompilation.cache[subCache]) childCompilation.cache[subCache] = {};
|
|
||||||
childCompilation.cache = childCompilation.cache[subCache];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
await (util.promisify(childCompiler.runAsChild.bind(childCompiler)))();
|
|
||||||
|
|
||||||
const versionVar = this.options.version ?
|
|
||||||
`var VERSION = ${JSON.stringify(this.options.version)};` : '';
|
|
||||||
const original = childCompilation.assets[workerOptions.filename].source();
|
|
||||||
const source = `${versionVar}var BUILD_ASSETS=${JSON.stringify(assetMapping)};${original}`;
|
|
||||||
childCompilation.assets[workerOptions.filename] = {
|
|
||||||
source: () => source,
|
|
||||||
size: () => Buffer.byteLength(source, 'utf8')
|
|
||||||
};
|
|
||||||
|
|
||||||
Object.assign(compilation.assets, childCompilation.assets);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
54
firebase.json
Normal file
54
firebase.json
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
{
|
||||||
|
"hosting": {
|
||||||
|
"public": "build",
|
||||||
|
"ignore": [
|
||||||
|
"firebase.json",
|
||||||
|
"**/.*",
|
||||||
|
"**/node_modules/**"
|
||||||
|
],
|
||||||
|
"rewrites": [
|
||||||
|
{
|
||||||
|
"source": "**",
|
||||||
|
"destination": "/index.html"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"headers": [
|
||||||
|
{
|
||||||
|
"source": "**/*.@(eot|otf|ttf|ttc|woff|font.css)",
|
||||||
|
"headers": [
|
||||||
|
{
|
||||||
|
"key": "Access-Control-Allow-Origin",
|
||||||
|
"value": "*"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"source": "**/*.@(jpg|jpeg|gif|png|ico)",
|
||||||
|
"headers": [
|
||||||
|
{
|
||||||
|
"key": "Cache-Control",
|
||||||
|
"value": "max-age=7200"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"source": "**/*.@(js|css|json|manifest|map)",
|
||||||
|
"headers": [
|
||||||
|
{
|
||||||
|
"key": "Cache-Control",
|
||||||
|
"value": "max-age=31536000"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"source": "sw.js",
|
||||||
|
"headers": [
|
||||||
|
{
|
||||||
|
"key": "Cache-Control",
|
||||||
|
"value": "max-age=0"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
11
global.d.ts
vendored
11
global.d.ts
vendored
@@ -1,23 +1,22 @@
|
|||||||
declare const __webpack_public_path__: string;
|
declare const __webpack_public_path__: string;
|
||||||
declare const PRERENDER: boolean;
|
|
||||||
|
|
||||||
declare interface NodeModule {
|
declare interface NodeModule {
|
||||||
hot: any;
|
hot: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
declare interface Window {
|
declare interface Window {
|
||||||
STATE: any;
|
STATE: any
|
||||||
ga: typeof ga;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
declare namespace JSX {
|
declare namespace JSX {
|
||||||
interface Element { }
|
interface Element { }
|
||||||
interface IntrinsicElements { }
|
interface IntrinsicElements { }
|
||||||
interface HTMLAttributes {
|
|
||||||
decoding?: string;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare module 'preact-i18n';
|
||||||
|
declare module 'preact-material-components-drawer';
|
||||||
|
declare module 'material-radial-progress';
|
||||||
|
|
||||||
declare module 'classnames' {
|
declare module 'classnames' {
|
||||||
export default function classnames(...args: any[]): string;
|
export default function classnames(...args: any[]): string;
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user