Compare commits
4 Commits
renovate/a
...
oxipng-old
Author | SHA1 | Date | |
---|---|---|---|
b6f69c57cf | |||
9e19c36b42 | |||
cf45520be3 | |||
8faf8a5b48 |
0
codecs/oxipng/.cargo-ok
Normal file
0
codecs/oxipng/.cargo-ok
Normal file
5
codecs/oxipng/.gitignore
vendored
Normal file
5
codecs/oxipng/.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
/target
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
||||
bin/
|
||||
wasm-pack.log
|
36
codecs/oxipng/Cargo.toml
Normal file
36
codecs/oxipng/Cargo.toml
Normal file
@ -0,0 +1,36 @@
|
||||
[package]
|
||||
name = "test"
|
||||
version = "0.1.0"
|
||||
authors = ["Surma <surma@surma.link>"]
|
||||
|
||||
[lib]
|
||||
path = "lib.rs"
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[features]
|
||||
default = ["console_error_panic_hook"]
|
||||
|
||||
[dependencies]
|
||||
cfg-if = "0.1.2"
|
||||
wasm-bindgen = "0.2"
|
||||
oxipng = "2.2.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"
|
52
codecs/oxipng/README.md
Normal file
52
codecs/oxipng/README.md
Normal file
@ -0,0 +1,52 @@
|
||||
# 🦀🕸️ `wasm-pack-template`
|
||||
|
||||
A template for kick starting a Rust and WebAssembly project using
|
||||
[`wasm-pack`](https://github.com/rustwasm/wasm-pack).
|
||||
|
||||
This template is designed for compiling Rust libraries into WebAssembly and
|
||||
publishing the resulting package to NPM.
|
||||
|
||||
* Want to use the published NPM package in a Website? [Check out
|
||||
`create-wasm-app`.](https://github.com/rustwasm/create-wasm-app)
|
||||
* Want to make a monorepo-style Website without publishing to NPM? Check out
|
||||
[`rust-webpack-template`](https://github.com/rustwasm/rust-webpack-template)
|
||||
and/or
|
||||
[`rust-parcel-template`](https://github.com/rustwasm/rust-parcel-template).
|
||||
|
||||
## 🔋 Batteries Included
|
||||
|
||||
* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating
|
||||
between WebAssembly and JavaScript.
|
||||
* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook)
|
||||
for logging panic messages to the developer console.
|
||||
* [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized
|
||||
for small code size.
|
||||
|
||||
## 🚴 Usage
|
||||
|
||||
### 🐑 Use `cargo generate` to Clone this Template
|
||||
|
||||
[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate)
|
||||
|
||||
```
|
||||
cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project
|
||||
cd my-project
|
||||
```
|
||||
|
||||
### 🛠️ Build with `wasm-pack build`
|
||||
|
||||
```
|
||||
wasm-pack build
|
||||
```
|
||||
|
||||
### 🔬 Test in Headless Browsers with `wasm-pack test`
|
||||
|
||||
```
|
||||
wasm-pack test --headless --firefox
|
||||
```
|
||||
|
||||
### 🎁 Publish to NPM with `wasm-pack publish`
|
||||
|
||||
```
|
||||
wasm-pack publish
|
||||
```
|
5
codecs/oxipng/Xargo.toml
Normal file
5
codecs/oxipng/Xargo.toml
Normal file
@ -0,0 +1,5 @@
|
||||
[target.wasm32-unknown-unknown.dependencies]
|
||||
time = {}
|
||||
|
||||
[target.wasm32-unknown-unknown.dependencies.std]
|
||||
features = ["wasm_syscall"]
|
52
codecs/oxipng/pkg/README.md
Normal file
52
codecs/oxipng/pkg/README.md
Normal file
@ -0,0 +1,52 @@
|
||||
# 🦀🕸️ `wasm-pack-template`
|
||||
|
||||
A template for kick starting a Rust and WebAssembly project using
|
||||
[`wasm-pack`](https://github.com/rustwasm/wasm-pack).
|
||||
|
||||
This template is designed for compiling Rust libraries into WebAssembly and
|
||||
publishing the resulting package to NPM.
|
||||
|
||||
* Want to use the published NPM package in a Website? [Check out
|
||||
`create-wasm-app`.](https://github.com/rustwasm/create-wasm-app)
|
||||
* Want to make a monorepo-style Website without publishing to NPM? Check out
|
||||
[`rust-webpack-template`](https://github.com/rustwasm/rust-webpack-template)
|
||||
and/or
|
||||
[`rust-parcel-template`](https://github.com/rustwasm/rust-parcel-template).
|
||||
|
||||
## 🔋 Batteries Included
|
||||
|
||||
* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating
|
||||
between WebAssembly and JavaScript.
|
||||
* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook)
|
||||
for logging panic messages to the developer console.
|
||||
* [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized
|
||||
for small code size.
|
||||
|
||||
## 🚴 Usage
|
||||
|
||||
### 🐑 Use `cargo generate` to Clone this Template
|
||||
|
||||
[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate)
|
||||
|
||||
```
|
||||
cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project
|
||||
cd my-project
|
||||
```
|
||||
|
||||
### 🛠️ Build with `wasm-pack build`
|
||||
|
||||
```
|
||||
wasm-pack build
|
||||
```
|
||||
|
||||
### 🔬 Test in Headless Browsers with `wasm-pack test`
|
||||
|
||||
```
|
||||
wasm-pack test --headless --firefox
|
||||
```
|
||||
|
||||
### 🎁 Publish to NPM with `wasm-pack publish`
|
||||
|
||||
```
|
||||
wasm-pack publish
|
||||
```
|
BIN
codecs/oxipng/pkg/img.png
Normal file
BIN
codecs/oxipng/pkg/img.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 29 KiB |
14
codecs/oxipng/pkg/lol.js
Normal file
14
codecs/oxipng/pkg/lol.js
Normal file
@ -0,0 +1,14 @@
|
||||
const oxipng = require("./oxipng_wasm");
|
||||
const repl = require("repl");
|
||||
const fs = require("fs");
|
||||
|
||||
async function init() {
|
||||
// const img = fs.readFileSync("img.png")
|
||||
// const output = oxipng.compress(img, 0);
|
||||
// fs.writeFileSync("output.png", output);
|
||||
console.log(">>>", oxipng.doit());
|
||||
const r = repl.start("node> ");
|
||||
r.context.i = oxipng;
|
||||
}
|
||||
|
||||
init();
|
3
codecs/oxipng/pkg/oxipng.d.ts
vendored
Normal file
3
codecs/oxipng/pkg/oxipng.d.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
/* tslint:disable */
|
||||
export function compress(arg0: Uint8Array, arg1: number): Uint8Array;
|
||||
|
73
codecs/oxipng/pkg/oxipng.js
Normal file
73
codecs/oxipng/pkg/oxipng.js
Normal file
@ -0,0 +1,73 @@
|
||||
/* tslint:disable */
|
||||
var wasm;
|
||||
|
||||
const TextDecoder = require('util').TextDecoder;
|
||||
|
||||
let cachedTextDecoder = new TextDecoder('utf-8');
|
||||
|
||||
let cachegetUint8Memory = null;
|
||||
function getUint8Memory() {
|
||||
if (cachegetUint8Memory === null || cachegetUint8Memory.buffer !== wasm.memory.buffer) {
|
||||
cachegetUint8Memory = new Uint8Array(wasm.memory.buffer);
|
||||
}
|
||||
return cachegetUint8Memory;
|
||||
}
|
||||
|
||||
function getStringFromWasm(ptr, len) {
|
||||
return cachedTextDecoder.decode(getUint8Memory().subarray(ptr, ptr + len));
|
||||
}
|
||||
|
||||
module.exports.__wbg_log_64e6f53d8e6d5db5 = function(arg0, arg1) {
|
||||
let varg0 = getStringFromWasm(arg0, arg1);
|
||||
console.log(varg0);
|
||||
};
|
||||
|
||||
let WASM_VECTOR_LEN = 0;
|
||||
|
||||
function passArray8ToWasm(arg) {
|
||||
const ptr = wasm.__wbindgen_malloc(arg.length * 1);
|
||||
getUint8Memory().set(arg, ptr / 1);
|
||||
WASM_VECTOR_LEN = arg.length;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
function getArrayU8FromWasm(ptr, len) {
|
||||
return getUint8Memory().subarray(ptr / 1, ptr / 1 + len);
|
||||
}
|
||||
|
||||
let cachedGlobalArgumentPtr = null;
|
||||
function globalArgumentPtr() {
|
||||
if (cachedGlobalArgumentPtr === null) {
|
||||
cachedGlobalArgumentPtr = wasm.__wbindgen_global_argument_ptr();
|
||||
}
|
||||
return cachedGlobalArgumentPtr;
|
||||
}
|
||||
|
||||
let cachegetUint32Memory = null;
|
||||
function getUint32Memory() {
|
||||
if (cachegetUint32Memory === null || cachegetUint32Memory.buffer !== wasm.memory.buffer) {
|
||||
cachegetUint32Memory = new Uint32Array(wasm.memory.buffer);
|
||||
}
|
||||
return cachegetUint32Memory;
|
||||
}
|
||||
/**
|
||||
* @param {Uint8Array} arg0
|
||||
* @param {number} arg1
|
||||
* @returns {Uint8Array}
|
||||
*/
|
||||
module.exports.compress = function(arg0, arg1) {
|
||||
const ptr0 = passArray8ToWasm(arg0);
|
||||
const len0 = WASM_VECTOR_LEN;
|
||||
const retptr = globalArgumentPtr();
|
||||
wasm.compress(retptr, ptr0, len0, arg1);
|
||||
const mem = getUint32Memory();
|
||||
const rustptr = mem[retptr / 4];
|
||||
const rustlen = mem[retptr / 4 + 1];
|
||||
|
||||
const realRet = getArrayU8FromWasm(rustptr, rustlen).slice();
|
||||
wasm.__wbindgen_free(rustptr, rustlen * 1);
|
||||
return realRet;
|
||||
|
||||
};
|
||||
|
||||
wasm = require('./oxipng_bg');
|
6
codecs/oxipng/pkg/oxipng_bg.d.ts
vendored
Normal file
6
codecs/oxipng/pkg/oxipng_bg.d.ts
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
/* tslint:disable */
|
||||
export const memory: WebAssembly.Memory;
|
||||
export function __wbindgen_global_argument_ptr(): number;
|
||||
export function compress(a: number, b: number, c: number, d: number): void;
|
||||
export function __wbindgen_malloc(a: number): number;
|
||||
export function __wbindgen_free(a: number, b: number): void;
|
9
codecs/oxipng/pkg/oxipng_bg.js
Normal file
9
codecs/oxipng/pkg/oxipng_bg.js
Normal file
@ -0,0 +1,9 @@
|
||||
|
||||
const path = require('path').join(__dirname, 'oxipng_bg.wasm');
|
||||
const bytes = require('fs').readFileSync(path);
|
||||
let imports = {};
|
||||
imports['./oxipng'] = require('./oxipng');
|
||||
|
||||
const wasmModule = new WebAssembly.Module(bytes);
|
||||
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
|
||||
module.exports = wasmInstance.exports;
|
BIN
codecs/oxipng/pkg/oxipng_bg.wasm
Normal file
BIN
codecs/oxipng/pkg/oxipng_bg.wasm
Normal file
Binary file not shown.
3
codecs/oxipng/pkg/oxipng_manual.d.ts
vendored
Normal file
3
codecs/oxipng/pkg/oxipng_manual.d.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
/* tslint:disable */
|
||||
export function compress(arg0: Uint8Array, arg1: number): Promise<Uint8Array>;
|
||||
|
77
codecs/oxipng/pkg/oxipng_manual.js
Normal file
77
codecs/oxipng/pkg/oxipng_manual.js
Normal file
@ -0,0 +1,77 @@
|
||||
/* tslint:disable */
|
||||
import wasmUrl from './oxipng_bg.wasm';
|
||||
|
||||
let wasm;
|
||||
const instancePromise = WebAssembly.instantiateStreaming(fetch(wasmUrl), {
|
||||
"./oxipng": {__wbg_log_64e6f53d8e6d5db5}
|
||||
});
|
||||
|
||||
let cachedTextDecoder = new TextDecoder('utf-8');
|
||||
|
||||
let cachegetUint8Memory = null;
|
||||
function getUint8Memory() {
|
||||
if (cachegetUint8Memory === null || cachegetUint8Memory.buffer !== wasm.memory.buffer) {
|
||||
cachegetUint8Memory = new Uint8Array(wasm.memory.buffer);
|
||||
}
|
||||
return cachegetUint8Memory;
|
||||
}
|
||||
|
||||
function getStringFromWasm(ptr, len) {
|
||||
return cachedTextDecoder.decode(getUint8Memory().subarray(ptr, ptr + len));
|
||||
}
|
||||
|
||||
export function __wbg_log_64e6f53d8e6d5db5(arg0, arg1) {
|
||||
let varg0 = getStringFromWasm(arg0, arg1);
|
||||
console.log(varg0);
|
||||
}
|
||||
|
||||
let WASM_VECTOR_LEN = 0;
|
||||
|
||||
function passArray8ToWasm(arg) {
|
||||
const ptr = wasm.__wbindgen_malloc(arg.length * 1);
|
||||
getUint8Memory().set(arg, ptr / 1);
|
||||
WASM_VECTOR_LEN = arg.length;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
function getArrayU8FromWasm(ptr, len) {
|
||||
return getUint8Memory().subarray(ptr / 1, ptr / 1 + len);
|
||||
}
|
||||
|
||||
let cachedGlobalArgumentPtr = null;
|
||||
function globalArgumentPtr() {
|
||||
if (cachedGlobalArgumentPtr === null) {
|
||||
cachedGlobalArgumentPtr = wasm.__wbindgen_global_argument_ptr();
|
||||
}
|
||||
return cachedGlobalArgumentPtr;
|
||||
}
|
||||
|
||||
let cachegetUint32Memory = null;
|
||||
function getUint32Memory() {
|
||||
if (cachegetUint32Memory === null || cachegetUint32Memory.buffer !== wasm.memory.buffer) {
|
||||
cachegetUint32Memory = new Uint32Array(wasm.memory.buffer);
|
||||
}
|
||||
return cachegetUint32Memory;
|
||||
}
|
||||
/**
|
||||
* @param {Uint8Array} arg0
|
||||
* @param {number} arg1
|
||||
* @returns {Uint8Array}
|
||||
*/
|
||||
export async function compress(arg0, arg1) {
|
||||
wasm = (await instancePromise).instance.exports;
|
||||
debugger;
|
||||
const ptr0 = passArray8ToWasm(arg0);
|
||||
const len0 = WASM_VECTOR_LEN;
|
||||
const retptr = globalArgumentPtr();
|
||||
wasm.compress(retptr, ptr0, len0, arg1);
|
||||
const mem = getUint32Memory();
|
||||
const rustptr = mem[retptr / 4];
|
||||
const rustlen = mem[retptr / 4 + 1];
|
||||
|
||||
const realRet = getArrayU8FromWasm(rustptr, rustlen).slice();
|
||||
wasm.__wbindgen_free(rustptr, rustlen * 1);
|
||||
return realRet;
|
||||
|
||||
}
|
||||
|
3
codecs/oxipng/pkg/oxipng_wasm.d.ts
vendored
Normal file
3
codecs/oxipng/pkg/oxipng_wasm.d.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
/* tslint:disable */
|
||||
export function doit(): number;
|
||||
|
11
codecs/oxipng/pkg/oxipng_wasm.js
Normal file
11
codecs/oxipng/pkg/oxipng_wasm.js
Normal file
@ -0,0 +1,11 @@
|
||||
/* tslint:disable */
|
||||
var wasm;
|
||||
|
||||
/**
|
||||
* @returns {number}
|
||||
*/
|
||||
module.exports.doit = function() {
|
||||
return wasm.doit();
|
||||
};
|
||||
|
||||
wasm = require('./oxipng_wasm_bg');
|
3
codecs/oxipng/pkg/oxipng_wasm_bg.d.ts
vendored
Normal file
3
codecs/oxipng/pkg/oxipng_wasm_bg.d.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
/* tslint:disable */
|
||||
export const memory: WebAssembly.Memory;
|
||||
export function doit(): number;
|
8
codecs/oxipng/pkg/oxipng_wasm_bg.js
Normal file
8
codecs/oxipng/pkg/oxipng_wasm_bg.js
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
const path = require('path').join(__dirname, 'oxipng_wasm_bg.wasm');
|
||||
const bytes = require('fs').readFileSync(path);
|
||||
let imports = {};
|
||||
|
||||
const wasmModule = new WebAssembly.Module(bytes);
|
||||
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
|
||||
module.exports = wasmInstance.exports;
|
BIN
codecs/oxipng/pkg/oxipng_wasm_bg.wasm
Normal file
BIN
codecs/oxipng/pkg/oxipng_wasm_bg.wasm
Normal file
Binary file not shown.
15
codecs/oxipng/pkg/package.json
Normal file
15
codecs/oxipng/pkg/package.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "oxipng-wasm",
|
||||
"collaborators": [
|
||||
"Surma <surma@surma.link>"
|
||||
],
|
||||
"version": "0.1.0",
|
||||
"files": [
|
||||
"oxipng_wasm_bg.wasm",
|
||||
"oxipng_wasm.js",
|
||||
"oxipng_wasm_bg.js",
|
||||
"oxipng_wasm.d.ts"
|
||||
],
|
||||
"main": "oxipng_wasm.js",
|
||||
"types": "oxipng_wasm.d.ts"
|
||||
}
|
0
codecs/oxipng/pkg/test/lol.rs
Normal file
0
codecs/oxipng/pkg/test/lol.rs
Normal file
42
codecs/oxipng/src/lib.rs
Normal file
42
codecs/oxipng/src/lib.rs
Normal file
@ -0,0 +1,42 @@
|
||||
extern crate cfg_if;
|
||||
extern crate wasm_bindgen;
|
||||
// extern crate oxipng;
|
||||
|
||||
mod utils;
|
||||
|
||||
use cfg_if::cfg_if;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use std::time::{Instant};
|
||||
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]
|
||||
// extern {
|
||||
// #[wasm_bindgen(js_namespace = console)]
|
||||
// fn log(s: &str);
|
||||
// }
|
||||
|
||||
// #[wasm_bindgen]
|
||||
// pub fn compress(img: Vec<u8>, level: u8) -> Vec<u8> {
|
||||
// log(&format!("len: {}, level: {}", img.len(), level));
|
||||
// let mut options = oxipng::Options::from_preset(level);
|
||||
// options.threads = 0;
|
||||
// let result = oxipng::optimize_from_memory(img.as_slice(), &options);
|
||||
// match result {
|
||||
// Ok(v) => v,
|
||||
// Err(e) => e.to_string().as_bytes().to_vec()
|
||||
// }
|
||||
// }
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn doit() -> u32 {
|
||||
let start = Instant::now();
|
||||
start.elapsed().as_secs() as u32
|
||||
}
|
17
codecs/oxipng/src/utils.rs
Normal file
17
codecs/oxipng/src/utils.rs
Normal file
@ -0,0 +1,17 @@
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
cfg_if! {
|
||||
// When the `console_error_panic_hook` feature is enabled, we can call the
|
||||
// `set_panic_hook` function at least once during initialization, and then
|
||||
// we will get better error messages if our code ever panics.
|
||||
//
|
||||
// For more details see
|
||||
// https://github.com/rustwasm/console_error_panic_hook#readme
|
||||
if #[cfg(feature = "console_error_panic_hook")] {
|
||||
extern crate console_error_panic_hook;
|
||||
pub use self::console_error_panic_hook::set_once as set_panic_hook;
|
||||
} else {
|
||||
#[inline]
|
||||
pub fn set_panic_hook() {}
|
||||
}
|
||||
}
|
1
codecs/oxipng/tmp/.gitignore
vendored
Normal file
1
codecs/oxipng/tmp/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
target
|
27
codecs/oxipng/tmp/Cargo.toml
Normal file
27
codecs/oxipng/tmp/Cargo.toml
Normal file
@ -0,0 +1,27 @@
|
||||
[package]
|
||||
name = "loltest"
|
||||
version = "0.1.0"
|
||||
authors = ["Surma <surma@surma.link>"]
|
||||
|
||||
[lib]
|
||||
path = "lib.rs"
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[features]
|
||||
default = ["console_error_panic_hook"]
|
||||
|
||||
[dependencies]
|
||||
wasm-bindgen = "0.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 }
|
||||
|
||||
[dev-dependencies]
|
||||
wasm-bindgen-test = "0.2"
|
||||
|
||||
[profile.release]
|
||||
# Tell `rustc` to optimize for small code size.
|
||||
opt-level = "s"
|
2
codecs/oxipng/tmp/Xargo.toml
Normal file
2
codecs/oxipng/tmp/Xargo.toml
Normal file
@ -0,0 +1,2 @@
|
||||
[dependencies.std]
|
||||
features = ["wasm_syscall"]
|
20
codecs/oxipng/tmp/_README.md
Normal file
20
codecs/oxipng/tmp/_README.md
Normal file
@ -0,0 +1,20 @@
|
||||
I’m trying to activate [the `wasm_syscall` feature][1] in Rust’s stdlib for WebAssembly.
|
||||
|
||||
Here is my `Cargo.toml` and my `Xargo.toml`. But even with this setup the generated wasm file is still hard-coded to panic.
|
||||
|
||||
**HELP?**
|
||||
|
||||
My current command to compile is:
|
||||
|
||||
```
|
||||
xargo build --target wasm32-unknown-unknown --release
|
||||
```
|
||||
|
||||
If you have [`wasm2wat`][2] installed, you can verify the generate code via
|
||||
|
||||
```
|
||||
wasm2wat target/wasm32-unknown-unknown/release/loltest.wasm | grep -A5 perform::
|
||||
```
|
||||
|
||||
[1]: https://github.com/rust-lang/rust/blob/b139669f374eb5024a50eb13f116ff763b1c5935/src/libstd/sys/wasm/mod.rs#L309
|
||||
[2]: https://github.com/WebAssembly/wabt
|
14
codecs/oxipng/tmp/lib.rs
Normal file
14
codecs/oxipng/tmp/lib.rs
Normal file
@ -0,0 +1,14 @@
|
||||
extern crate wasm_bindgen;
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
use std::thread::spawn;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn doit() {
|
||||
// let child = spawn(move || -> u32 {
|
||||
// 5
|
||||
// });
|
||||
// let result = child.join().unwrap();
|
||||
let result = spawn();
|
||||
println!("Result: {}", result);
|
||||
}
|
47
codecs/oxipng/tmp/lol.js
Normal file
47
codecs/oxipng/tmp/lol.js
Normal file
@ -0,0 +1,47 @@
|
||||
// const oxipng = require("./oxipng_wasm");
|
||||
const repl = require("repl");
|
||||
const fs = require("fs");
|
||||
|
||||
const dec = new TextDecoder();
|
||||
let buffer = '';
|
||||
async function init() {
|
||||
const { instance } = await WebAssembly.instantiate(
|
||||
fs.readFileSync("./target/wasm32-unknown-unknown/release/loltest.wasm"),
|
||||
{
|
||||
__wbindgen_placeholder__: {
|
||||
__wbindgen_describe(v) {
|
||||
console.log(`__wbindgen_desribe(${v})`);
|
||||
}
|
||||
},
|
||||
env: {
|
||||
// See https://github.com/rust-lang/rust/blob/master/src/libstd/sys/wasm/mod.rs
|
||||
rust_wasm_syscall(syscall, ptr) {
|
||||
switch(syscall) {
|
||||
case 1: // Write
|
||||
const [fd, dataPtr, len] = new Uint32Array(instance.exports.memory.buffer, ptr, 3 * 4);
|
||||
const fragment = new Uint8Array(instance.exports.memory.buffer, dataPtr, len);
|
||||
buffer += dec.decode(fragment);
|
||||
const idx = buffer.indexOf('\n');
|
||||
if(idx !== -1) {
|
||||
console.log(buffer.slice(0, idx));
|
||||
buffer = buffer.slice(idx);
|
||||
}
|
||||
return 1;
|
||||
case 6: // Time
|
||||
return 1;
|
||||
default:
|
||||
return 0; // False, unimplemented
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
try {
|
||||
instance.exports.doit();
|
||||
} catch{}
|
||||
const r = repl.start("node> ");
|
||||
r.context.i = instance;
|
||||
}
|
||||
|
||||
init();
|
1737
codecs/oxipng/tmp/thread_mod.rs
Normal file
1737
codecs/oxipng/tmp/thread_mod.rs
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,6 @@
|
||||
import * as identity from './identity/encoder-meta';
|
||||
import * as optiPNG from './optipng/encoder-meta';
|
||||
import * as oxiPNG from './oxipng/encoder-meta';
|
||||
import * as mozJPEG from './mozjpeg/encoder-meta';
|
||||
import * as webP from './webp/encoder-meta';
|
||||
import * as browserPNG from './browser-png/encoder-meta';
|
||||
@ -18,6 +19,7 @@ export interface EncoderSupportMap {
|
||||
export type EncoderState =
|
||||
identity.EncoderState |
|
||||
optiPNG.EncoderState |
|
||||
oxiPNG.EncoderState |
|
||||
mozJPEG.EncoderState |
|
||||
webP.EncoderState |
|
||||
browserPNG.EncoderState |
|
||||
@ -32,6 +34,7 @@ export type EncoderState =
|
||||
export type EncoderOptions =
|
||||
identity.EncodeOptions |
|
||||
optiPNG.EncodeOptions |
|
||||
oxiPNG.EncodeOptions |
|
||||
mozJPEG.EncodeOptions |
|
||||
webP.EncodeOptions |
|
||||
browserPNG.EncodeOptions |
|
||||
@ -48,6 +51,7 @@ export type EncoderType = keyof typeof encoderMap;
|
||||
export const encoderMap = {
|
||||
[identity.type]: identity,
|
||||
[optiPNG.type]: optiPNG,
|
||||
[oxiPNG.type]: oxiPNG,
|
||||
[mozJPEG.type]: mozJPEG,
|
||||
[webP.type]: webP,
|
||||
[browserPNG.type]: browserPNG,
|
||||
|
@ -3,7 +3,7 @@ export interface EncodeOptions {
|
||||
}
|
||||
export interface EncoderState { type: typeof type; options: EncodeOptions; }
|
||||
|
||||
export const type = 'png';
|
||||
export const type = 'optipng';
|
||||
export const label = 'OptiPNG';
|
||||
export const mimeType = 'image/png';
|
||||
export const extension = 'png';
|
||||
|
13
src/codecs/oxipng/encoder-meta.ts
Normal file
13
src/codecs/oxipng/encoder-meta.ts
Normal file
@ -0,0 +1,13 @@
|
||||
export interface EncodeOptions {
|
||||
level: number;
|
||||
}
|
||||
export interface EncoderState { type: typeof type; options: EncodeOptions; }
|
||||
|
||||
export const type = 'oxipng';
|
||||
export const label = 'OxiPNG';
|
||||
export const mimeType = 'image/png';
|
||||
export const extension = 'png';
|
||||
|
||||
export const defaultOptions: EncodeOptions = {
|
||||
level: 6,
|
||||
};
|
18
src/codecs/oxipng/encoder.ts
Normal file
18
src/codecs/oxipng/encoder.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import * as oxipng from '../../../codecs/oxipng/pkg/oxipng_manual';
|
||||
import { EncodeOptions } from './encoder-meta';
|
||||
|
||||
export async function compress(data: BufferSource, { level }: EncodeOptions): Promise<ArrayBuffer> {
|
||||
let buffer: ArrayBuffer;
|
||||
|
||||
if (ArrayBuffer.isView(data)) {
|
||||
buffer = data.buffer;
|
||||
} else {
|
||||
buffer = data;
|
||||
}
|
||||
debugger;
|
||||
const resultView = await oxipng.compress(new Uint8Array(buffer), level);
|
||||
const result = new Uint8Array(resultView);
|
||||
|
||||
// wasm can’t run on SharedArrayBuffers, so we hard-cast to ArrayBuffer.
|
||||
return result.buffer as ArrayBuffer;
|
||||
}
|
42
src/codecs/oxipng/options.tsx
Normal file
42
src/codecs/oxipng/options.tsx
Normal file
@ -0,0 +1,42 @@
|
||||
import { h, Component } from 'preact';
|
||||
import { bind } from '../../lib/initial-util';
|
||||
import { inputFieldValueAsNumber, preventDefault } from '../../lib/util';
|
||||
import { EncodeOptions } from './encoder-meta';
|
||||
import Range from '../../components/range';
|
||||
import * as style from '../../components/Options/style.scss';
|
||||
|
||||
type Props = {
|
||||
options: EncodeOptions;
|
||||
onChange(newOptions: EncodeOptions): void;
|
||||
};
|
||||
|
||||
export default class OxiPNGEncoderOptions extends Component<Props, {}> {
|
||||
@bind
|
||||
onChange(event: Event) {
|
||||
const form = (event.currentTarget as HTMLInputElement).closest('form') as HTMLFormElement;
|
||||
|
||||
const options: EncodeOptions = {
|
||||
level: inputFieldValueAsNumber(form.level),
|
||||
};
|
||||
this.props.onChange(options);
|
||||
}
|
||||
|
||||
render({ options }: Props) {
|
||||
return (
|
||||
<form class={style.optionsSection} onSubmit={preventDefault}>
|
||||
<div class={style.optionOneCell}>
|
||||
<Range
|
||||
name="level"
|
||||
min="0"
|
||||
max="9"
|
||||
step="1"
|
||||
value={options.level}
|
||||
onInput={this.onChange}
|
||||
>
|
||||
Effort:
|
||||
</Range>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
}
|
@ -41,6 +41,16 @@ async function optiPngEncode(
|
||||
return compress(data, options);
|
||||
}
|
||||
|
||||
async function oxiPngEncode(
|
||||
data: BufferSource, options: import('../oxipng/encoder-meta').EncodeOptions,
|
||||
): Promise<ArrayBuffer> {
|
||||
const { compress } = await import(
|
||||
/* webpackChunkName: "process-optipng" */
|
||||
'../oxipng/encoder',
|
||||
);
|
||||
return compress(data, options);
|
||||
}
|
||||
|
||||
async function webpEncode(
|
||||
data: ImageData, options: import('../webp/encoder-meta').EncodeOptions,
|
||||
): Promise<ArrayBuffer> {
|
||||
@ -59,7 +69,15 @@ async function webpDecode(data: ArrayBuffer): Promise<ImageData> {
|
||||
return decode(data);
|
||||
}
|
||||
|
||||
const exports = { mozjpegEncode, quantize, rotate, optiPngEncode, webpEncode, webpDecode };
|
||||
const exports = {
|
||||
mozjpegEncode,
|
||||
quantize,
|
||||
rotate,
|
||||
optiPngEncode,
|
||||
oxiPngEncode,
|
||||
webpEncode,
|
||||
webpDecode,
|
||||
};
|
||||
export type ProcessorWorkerApi = typeof exports;
|
||||
|
||||
expose(exports, self);
|
||||
|
@ -3,6 +3,7 @@ import { QuantizeOptions } from './imagequant/processor-meta';
|
||||
import { canvasEncode, blobToArrayBuffer } from '../lib/util';
|
||||
import { EncodeOptions as MozJPEGEncoderOptions } from './mozjpeg/encoder-meta';
|
||||
import { EncodeOptions as OptiPNGEncoderOptions } from './optipng/encoder-meta';
|
||||
import { EncodeOptions as OxiPNGEncoderOptions } from './oxipng/encoder-meta';
|
||||
import { EncodeOptions as WebPEncoderOptions } from './webp/encoder-meta';
|
||||
import { EncodeOptions as BrowserJPEGOptions } from './browser-jpeg/encoder-meta';
|
||||
import { EncodeOptions as BrowserWebpEncodeOptions } from './browser-webp/encoder-meta';
|
||||
@ -147,6 +148,16 @@ export default class Processor {
|
||||
return this._workerApi!.optiPngEncode(pngBuffer, opts);
|
||||
}
|
||||
|
||||
@Processor._processingJob({ needsWorker: true })
|
||||
async oxiPngEncode(
|
||||
data: ImageData, opts: OxiPNGEncoderOptions,
|
||||
): Promise<ArrayBuffer> {
|
||||
// OptiPNG expects PNG input.
|
||||
const pngBlob = await canvasEncode(data, 'image/png');
|
||||
const pngBuffer = await blobToArrayBuffer(pngBlob);
|
||||
return this._workerApi!.oxiPngEncode(pngBuffer, opts);
|
||||
}
|
||||
|
||||
@Processor._processingJob({ needsWorker: true })
|
||||
webpEncode(data: ImageData, opts: WebPEncoderOptions): Promise<ArrayBuffer> {
|
||||
return this._workerApi!.webpEncode(data, opts);
|
||||
|
@ -8,6 +8,7 @@ import Options from '../Options';
|
||||
import ResultCache from './result-cache';
|
||||
import * as identity from '../../codecs/identity/encoder-meta';
|
||||
import * as optiPNG from '../../codecs/optipng/encoder-meta';
|
||||
import * as oxiPNG from '../../codecs/oxipng/encoder-meta';
|
||||
import * as mozJPEG from '../../codecs/mozjpeg/encoder-meta';
|
||||
import * as webP from '../../codecs/webp/encoder-meta';
|
||||
import * as browserPNG from '../../codecs/browser-png/encoder-meta';
|
||||
@ -131,6 +132,7 @@ async function compressImage(
|
||||
const compressedData = await (() => {
|
||||
switch (encodeData.type) {
|
||||
case optiPNG.type: return processor.optiPngEncode(image, encodeData.options);
|
||||
case oxiPNG.type: return processor.oxiPngEncode(image, encodeData.options);
|
||||
case mozJPEG.type: return processor.mozjpegEncode(image, encodeData.options);
|
||||
case webP.type: return processor.webpEncode(image, encodeData.options);
|
||||
case browserPNG.type: return processor.browserPngEncode(image);
|
||||
|
Reference in New Issue
Block a user