Compile JXL encoder

This commit is contained in:
Surma
2020-02-06 15:42:14 -08:00
committed by Ingvar Stepanyan
parent 36d23644aa
commit 98bc151907
9 changed files with 1398 additions and 0 deletions

18
codecs/jxl_enc/README.md Normal file
View File

@@ -0,0 +1,18 @@
# JPEG XL decoder
- Source: <https://gitlab.com/wg1/jpeg-xl>
- Version: ???
## Example
See `example.html`
## API
### `RawImage decode(std::string buffer)`
Decodes the given avif buffer into raw RGBA. `RawImage` is a class with 3 fields: `buffer`, `width`, and `height`.
### `void free_result()`
Frees the result created by `decode()`.

74
codecs/jxl_enc/build.sh Executable file
View File

@@ -0,0 +1,74 @@
#!/bin/bash
set -e
export OPTIMIZE="-Os"
export LDFLAGS="${OPTIMIZE}"
export CFLAGS="${OPTIMIZE}"
export CPPFLAGS="${OPTIMIZE}"
echo "============================================="
echo "Downloading libjxl"
echo "============================================="
test -d node_modules/jxl || (
cd node_modules
git clone --recursive https://gitlab.com/wg1/jpeg-xl.git jxl
)
echo "============================================="
echo "Downloading libjxl done"
echo "============================================="
echo "============================================="
echo "Compiling libjxl"
echo "============================================="
test -n "$SKIP_LIBJXL" || (
cd node_modules/jxl
git submodule update --init --recursive
mkdir -p build
cd build
emcmake cmake ../
emmake make jpegxl-static
)
echo "============================================="
echo "Compiling libjxl done"
echo "============================================="
echo "============================================="
echo "Compiling wasm bindings"
echo "============================================="
emcc \
${OPTIMIZE} \
--bind \
-s ALLOW_MEMORY_GROWTH=1 \
-s MODULARIZE=1 \
-s 'EXPORT_NAME="jxl_enc"' \
-I ./node_modules/jxl \
-I ./node_modules/jxl/include \
-I ./node_modules/jxl/third_party/brunsli \
-I ./node_modules/jxl/third_party/brunsli/c/include \
-I ./node_modules/jxl/third_party/highway \
-o ./jxl_enc.js \
-x c++ \
--std=c++11 \
jxl_enc.cpp \
./node_modules/jxl/build/libjpegxl-static.bc \
./node_modules/jxl/build/third_party/brunsli/libbrunslienc-static.bc \
./node_modules/jxl/build/third_party/brunsli/libbrunslidec-static.bc \
./node_modules/jxl/build/third_party/brunsli/libbrunslicommon-static.bc \
./node_modules/jxl/build/third_party/brotli/libbrotlienc-static.bc \
./node_modules/jxl/build/third_party/brotli/libbrotlidec-static.bc \
./node_modules/jxl/build/third_party/brotli/libbrotlicommon-static.bc \
./node_modules/jxl/build/third_party/liblcms2.bc \
./node_modules/jxl/build/third_party/highway/libhwy.bc
echo "============================================="
echo "Compiling wasm bindings done"
echo "============================================="
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "Did you update your docker image?"
echo "Run \`docker pull trzeci/emscripten\`"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"

12
codecs/jxl_enc/example.js Normal file
View File

@@ -0,0 +1,12 @@
const fs = require("fs");
const avifEnc = require("./jxl_enc.js")(fs.readFileSync("./jxl_enc.wasm"));
avifEnc.onRuntimeInitialized = () => {
const jxl = avifEnc.encode(
new Uint8Array([255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255]),
3,
1
);
console.log(jxl)
fs.writeFileSync("lol.jxl", jxl);
};

View File

@@ -0,0 +1,40 @@
#include <emscripten/bind.h>
#include <emscripten/val.h>
#include "jxl/enc_file.h"
using namespace emscripten;
uint8_t *result;
val encode(std::string image, int width, int height) {
jxl::CompressParams cparams;
jxl::PassesEncoderState passes_enc_state;
// jxl::ThreadPool pool;
jxl::CodecInOut io;
jxl::PaddedBytes bytes;
cparams.speed_tier = jxl::SpeedTier::kFalcon;
jxl::ImageBundle *main = &io.Main();
auto result = main->SetFromSRGB(
width, height, false, true, true, (uint8_t *)image.c_str(),
(uint8_t *)image.c_str() + width * height * 4);
if (!result) {
return val::null();
}
if (!EncodeFile(cparams, &io, &passes_enc_state, &bytes)) {
return val::null();
}
return val(typed_memory_view(bytes.size(), bytes.data()));
}
void free_result() { delete result; }
EMSCRIPTEN_BINDINGS(my_module) {
function("encode", &encode);
function("free_result", &free_result);
}

9
codecs/jxl_enc/jxl_enc.d.ts vendored Normal file
View File

@@ -0,0 +1,9 @@
// import { EncodeOptions } from '../../src/codecs/avif/encoder-meta';
interface JXLModule extends EmscriptenWasm.Module {
encode(data: BufferSource, width: number, height: number): Uint8Array;
free_result(): void;
}
export default function(opts: EmscriptenWasm.ModuleOpts): JXLModule;

22
codecs/jxl_enc/jxl_enc.js Normal file

File diff suppressed because one or more lines are too long

BIN
codecs/jxl_enc/jxl_enc.wasm Normal file

Binary file not shown.

1211
codecs/jxl_enc/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,12 @@
{
"name": "jxl_enc",
"scripts": {
"install": "napa",
"build": "docker run --rm -v $(pwd):/src trzeci/emscripten ./build.sh"
},
"napa": {
},
"devDependencies": {
"napa": "3.0.0"
}
}