Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions packages/webp/codec/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
CODEC_URL = https://github.com/webmproject/libwebp/archive/d2e245ea9e959a5a79e1db0ed2085206947e98f2.tar.gz
CODEC_VERSION = 1.6.0
CODEC_URL = https://github.com/webmproject/libwebp/archive/refs/tags/v$(CODEC_VERSION).tar.gz
CODEC_DIR = node_modules/libwebp
CODEC_BUILD_ROOT := $(CODEC_DIR)/build
CODEC_BASELINE_BUILD_DIR := $(CODEC_BUILD_ROOT)/baseline
Expand All @@ -18,6 +19,12 @@ $(filter enc/%,$(OUT_JS)): enc/webp_enc.o
$(filter dec/%,$(OUT_JS)): dec/webp_dec.o
enc/webp_enc.js dec/webp_dec.js: $(CODEC_BASELINE_BUILD_DIR)/libwebp.a
enc/webp_enc_simd.js: $(CODEC_SIMD_BUILD_DIR)/libwebp.a
# Sharp YUV moved out into its own library in libwebp 1.2.1, and the encoder
# references it unconditionally from picture_csp_enc.c. It has to come after
# libwebp.a on the link line. The decoder never pulls those symbols in, so it
# does not need it.
enc/webp_enc.js: $(CODEC_BASELINE_BUILD_DIR)/libsharpyuv.a
enc/webp_enc_simd.js: $(CODEC_SIMD_BUILD_DIR)/libsharpyuv.a

$(OUT_JS):
$(LD) \
Expand All @@ -38,16 +45,38 @@ $(OUT_JS):
-o $@ \
$<

%/libwebp.a: %/Makefile
# One CMake invocation produces both archives.
%/libwebp.a %/libsharpyuv.a: %/Makefile
$(MAKE) -C $(@D)

# Enable SIMD on a SIMD build.
$(CODEC_SIMD_BUILD_DIR)/Makefile: CMAKE_FLAGS+=-DWEBP_ENABLE_SIMD=1
#
# The extension set is pinned by hand rather than left to libwebp's probing.
# wasm is a fixed target - we know exactly which extensions exist - and 1.6.0's
# detection gets it wrong here in two separate ways.
#
# AVX2 is masked off with -U__AVX2__, which makes libwebp's probe for it fail.
# 1.6.0 added AVX2 lossless paths, but Emscripten emulates 256-bit intrinsics
# as pairs of 128-bit wasm SIMD ops, so they cost binary size without buying
# throughput - and the sources pass `__m256i*` into intrinsics Emscripten
# declares as taking `__m256i_u*`, which current clang rejects outright.
#
# SSE2 has to be asserted by hand. cpu.cmake stops probing at the third entry
# of its extension list under Emscripten; 1.6.0 prepended AVX2 to that list,
# pushing SSE2 out of range, so it is never detected and lands in config.h
# undefined. That silently empties every *_sse2.c - which is most of the SIMD
# win - and breaks enc_sse41.c, which needs the inline helpers in common_sse2.h.
SIMD_CFLAGS = -msimd128 -msse4.1 -DWEBP_HAVE_SSE2 -U__AVX2__
$(CODEC_SIMD_BUILD_DIR)/Makefile: CMAKE_FLAGS+=-DWEBP_ENABLE_SIMD=1 -DCMAKE_C_FLAGS="$(CFLAGS) $(SIMD_CFLAGS)"

# Threads are off because the modules are built for `web,worker` without
# pthreads. This says so directly; the CMAKE_DISABLE_FIND_PACKAGE_Threads=1 it
# replaces got there by making libwebp's find_package(Threads) fail, which
# libwebp no longer even reaches once WEBP_USE_THREAD is off.
%/Makefile: $(CODEC_DIR)/CMakeLists.txt
emcmake cmake \
$(CMAKE_FLAGS) \
-DCMAKE_DISABLE_FIND_PACKAGE_Threads=1 \
-DWEBP_USE_THREAD=0 \
-DWEBP_BUILD_ANIM_UTILS=0 \
-DWEBP_BUILD_CWEBP=0 \
-DWEBP_BUILD_DWEBP=0 \
Expand Down
2 changes: 1 addition & 1 deletion packages/webp/codec/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# WebP

- Source: https://github.com/webmproject/libwebp
- Version: v1.0.2
- Version: v1.6.0
- License: BSD
2 changes: 1 addition & 1 deletion packages/webp/codec/dec/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# WebP decoder

- Source: <https://github.com/webmproject/libwebp>
- Version: v1.0.2
- Version: v1.6.0

## Example

Expand Down
18 changes: 2 additions & 16 deletions packages/webp/codec/dec/webp_dec.js

Large diffs are not rendered by default.

Binary file modified packages/webp/codec/dec/webp_dec.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/webp/codec/enc/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# WebP encoder

- Source: <https://github.com/webmproject/libwebp>
- Version: v1.0.2
- Version: v1.6.0

## Dependencies

Expand Down
8 changes: 4 additions & 4 deletions packages/webp/codec/enc/webp_enc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ val encode(std::string img, int width, int height, WebPConfig config) {
return val::null();
}

// Allow quality to go higher than 0.
config.qmax = 100;

// 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;
Expand Down Expand Up @@ -74,11 +71,14 @@ EMSCRIPTEN_BINDINGS(my_module) {
.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);
.field("use_sharp_yuv", &WebPConfig::use_sharp_yuv)
.field("qmin", &WebPConfig::qmin)
.field("qmax", &WebPConfig::qmax);

function("version", &version);
function("encode", &encode);
Expand Down
2 changes: 2 additions & 0 deletions packages/webp/codec/enc/webp_enc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export interface EncodeOptions {
near_lossless: number;
use_delta_palette: number;
use_sharp_yuv: number;
qmin: number;
qmax: number;
}

export interface WebPModule extends EmscriptenWasm.Module {
Expand Down
18 changes: 2 additions & 16 deletions packages/webp/codec/enc/webp_enc.js

Large diffs are not rendered by default.

Binary file modified packages/webp/codec/enc/webp_enc.wasm
Binary file not shown.
18 changes: 2 additions & 16 deletions packages/webp/codec/enc/webp_enc_simd.js

Large diffs are not rendered by default.

Binary file modified packages/webp/codec/enc/webp_enc_simd.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/webp/codec/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"scripts": {
"build": "../../../tools/build-cpp.sh"
"build": "EMSDK_VERSION=4.0.16 ../../../tools/build-cpp.sh"
},
"type": "module"
}
4 changes: 4 additions & 0 deletions packages/webp/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ export const defaultOptions: EncodeOptions = {
near_lossless: 100,
use_delta_palette: 0,
use_sharp_yuv: 0,
// The permissible quality range. libwebp clamps `quality` into it, and
// searches within it when target_size or target_PSNR is set.
qmin: 0,
qmax: 100,
};
25 changes: 25 additions & 0 deletions test/node/webp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,28 @@ test('can successfully encode image', async (t) => {
});
t.assert(data instanceof ArrayBuffer);
});

test('caps quality at qmax', async (t) => {
const encodeWasmModule = await importWasmModule(
'node_modules/@jsquash/webp/codec/enc/webp_enc.wasm',
);
await initEncode(encodeWasmModule);
// A flat image encodes to the same handful of bytes at any quality, so the
// ceiling only shows up on something with detail in it.
const width = 64;
const height = 64;
const data = new Uint8ClampedArray(4 * width * height);
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const i = (y * width + x) * 4;
data[i] = (x * 4) % 256;
data[i + 1] = (y * 4) % 256;
data[i + 2] = ((x + y) * 3) % 256;
data[i + 3] = 255;
}
}
const image = { data, width, height, colorSpace: 'srgb' as const };
const capped = await encode(image, { quality: 90, qmax: 40 });
const uncapped = await encode(image, { quality: 90 });
t.true(capped.byteLength < uncapped.byteLength);
});
2 changes: 1 addition & 1 deletion tools/cpp.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ARG DEFAULT_EMSCRIPTEN_SETTINGS="\
-s PTHREAD_POOL_SIZE=navigator.hardwareConcurrency \
-s FILESYSTEM=0 \
-s ALLOW_MEMORY_GROWTH=1 \
-s TEXTDECODER=0 \
-s TEXTDECODER=1 \
"

RUN apt-get update && apt-get install -qqy autoconf libtool pkg-config
Expand Down