Skip to content

Make images smaller using best-in-class codecs, right in the browser.

JavaScript

Merge remote-tracking branch 'origin/dev' into visdf

Surmad8bd12fc6aed
diff --cc codecs/jxl/enc/jxl_enc.cpp
index 00000000,d1ca111b..81e7fb3d
mode 000000,100644..100644
--- a/codecs/jxl/enc/jxl_enc.cpp
+++ b/codecs/jxl/enc/jxl_enc.cpp
@@@ -1,0 -1,130 +1,130 @@@
+ #include <emscripten/bind.h>
+ #include <emscripten/val.h>
+
++#include "lib/jxl/base/thread_pool_internal.h"
+ #include "lib/jxl/enc_file.h"
+ #include "lib/jxl/external_image.h"
-#include "lib/jxl/base/thread_pool_internal.h"
+
+ using namespace emscripten;
+
+ thread_local const val Uint8Array = val::global("Uint8Array");
+
+ struct JXLOptions {
+ // 1 = slowest
+ // 7 = fastest
+ int speed;
+ float quality;
+ bool progressive;
+ int epf;
+ int nearLossless;
+ bool lossyPalette;
+ };
+
+ val encode(std::string image, int width, int height, JXLOptions options) {
+ jxl::CompressParams cparams;
+ jxl::PassesEncoderState passes_enc_state;
+ jxl::CodecInOut io;
+ jxl::PaddedBytes bytes;
+ jxl::ImageBundle* main = &io.Main();
- jxl::ThreadPoolInternal *pool_ptr = nullptr;
++ jxl::ThreadPoolInternal* pool_ptr = nullptr;
+ #ifdef __EMSCRIPTEN_PTHREADS__
+ jxl::ThreadPoolInternal pool;
+ pool_ptr = &pool;
+ #endif
+
+ cparams.epf = options.epf;
+ cparams.speed_tier = static_cast<jxl::SpeedTier>(options.speed);
+ cparams.near_lossless = options.nearLossless;
+
+ if (options.lossyPalette) {
+ cparams.lossy_palette = true;
+ cparams.palette_colors = 0;
+ cparams.options.predictor = jxl::Predictor::Zero;
+ }
+
+ // Reduce memory usage of tree learning for lossless data.
+ // TODO(veluca93): this is a mitigation for excessive memory usage in the JXL encoder.
+ float megapixels = width * height * 0.000001;
+ if (megapixels > 8) {
+ cparams.options.nb_repeats = 0.1;
+ } else if (megapixels > 4) {
+ cparams.options.nb_repeats = 0.3;
+ } else {
+ // default is OK.
+ }
+
+ float quality = options.quality;
+
+ // Quality settings roughly match libjpeg qualities.
+ if (quality < 7 || quality == 100) {
+ cparams.modular_mode = true;
+ // Internal modular quality to roughly match VarDCT size.
+ cparams.quality_pair.first = cparams.quality_pair.second =
+ std::min(35 + (quality - 7) * 3.0f, 100.0f);
+ } else {
+ cparams.modular_mode = false;
+ if (quality >= 30) {
+ cparams.butteraugli_distance = 0.1 + (100 - quality) * 0.09;
+ } else {
+ cparams.butteraugli_distance = 6.4 + pow(2.5, (30 - quality) / 5.0f) / 6.25f;
+ }
+ }
+
+ if (options.progressive) {
+ cparams.qprogressive_mode = true;
+ cparams.responsive = 1;
+ if (!cparams.modular_mode) {
+ cparams.progressive_dc = 1;
+ }
+ }
+
+ if (cparams.modular_mode) {
+ if (cparams.quality_pair.first != 100 || cparams.quality_pair.second != 100) {
+ cparams.color_transform = jxl::ColorTransform::kXYB;
+ } else {
+ cparams.color_transform = jxl::ColorTransform::kNone;
+ }
+ }
+
+ if (cparams.near_lossless) {
+ // Near-lossless assumes -R 0
+ cparams.responsive = 0;
+ cparams.modular_mode = true;
+ }
+
+ io.metadata.m.SetAlphaBits(8);
+ if (!io.metadata.size.Set(width, height)) {
+ return val::null();
+ }
+
+ uint8_t* inBuffer = (uint8_t*)image.c_str();
+
+ auto result = jxl::ConvertImage(
+ jxl::Span<const uint8_t>(reinterpret_cast<const uint8_t*>(image.data()), image.size()), width,
+ height, jxl::ColorEncoding::SRGB(/*is_gray=*/false), /*has_alpha=*/true,
+ /*alpha_is_premultiplied=*/false, /*bits_per_alpha=*/8, /*bits_per_sample=*/8,
+ /*big_endian=*/false, /*flipped_y=*/false, pool_ptr, main);
+
+ if (!result) {
+ return val::null();
+ }
+
+ auto js_result = val::null();
+ if (EncodeFile(cparams, &io, &passes_enc_state, &bytes, /*aux=*/nullptr, pool_ptr)) {
+ js_result = Uint8Array.new_(typed_memory_view(bytes.size(), bytes.data()));
+ }
+
+ return js_result;
+ }
+
+ EMSCRIPTEN_BINDINGS(my_module) {
+ value_object<JXLOptions>("JXLOptions")
+ .field("speed", &JXLOptions::speed)
+ .field("quality", &JXLOptions::quality)
+ .field("progressive", &JXLOptions::progressive)
+ .field("nearLossless", &JXLOptions::nearLossless)
+ .field("lossyPalette", &JXLOptions::lossyPalette)
+ .field("epf", &JXLOptions::epf);
+
+ function("encode", &encode);
+ }
diff --cc codecs/wp2/enc/wp2_enc.cpp
index 00000000,1efb2dd7..91989d4c
mode 000000,100644..100644
--- a/codecs/wp2/enc/wp2_enc.cpp
+++ b/codecs/wp2/enc/wp2_enc.cpp
@@@ -1,0 -1,69 +1,69 @@@
+ #include <emscripten/bind.h>
-#include <emscripten/val.h>
+ #include <emscripten/threading.h>
++#include <emscripten/val.h>
+ #include <cstdio>
+ #include "src/wp2/encode.h"
+
+ using namespace emscripten;
+
+ thread_local const val Uint8Array = val::global("Uint8Array");
+
+ struct WP2Options {
+ float quality;
+ float alpha_quality;
+ int effort;
+ int pass;
+ int uv_mode;
+ float sns;
+ int csp_type;
+ int error_diffusion;
+ bool use_random_matrix;
+ };
+
+ val encode(std::string image_in, int image_width, int image_height, WP2Options options) {
+ WP2::EncoderConfig config = {};
+
+ config.quality = options.quality;
+ config.alpha_quality = options.alpha_quality;
+ config.effort = options.effort;
+ config.pass = options.pass;
+ config.uv_mode = static_cast<WP2::EncoderConfig::UVMode>(options.uv_mode);
+ config.csp_type = static_cast<WP2::Csp>(options.csp_type);
+ config.sns = options.sns;
+ config.error_diffusion = options.error_diffusion;
+ config.use_random_matrix = options.use_random_matrix;
+
+ uint8_t* image_buffer = (uint8_t*)image_in.c_str();
+ WP2::ArgbBuffer src = WP2::ArgbBuffer();
+ WP2Status status =
+ src.Import(WP2_rgbA_32, // Format. WP2_RGBA_32 is the same but NOT premultiplied alpha
+ image_width, image_height, image_buffer, 4 * image_width);
+ if (status != WP2_STATUS_OK) {
+ return val::null();
+ }
+
+ WP2::MemoryWriter memory_writer;
+ // In WebP2, thread_level is number of *extra* threads to use (0 for no multithreading).
+ config.thread_level = emscripten_num_logical_cores() - 1;
+ status = WP2::Encode(src, &memory_writer, config);
+ if (status != WP2_STATUS_OK) {
+ return val::null();
+ }
+
+ return Uint8Array.new_(typed_memory_view(memory_writer.size_, memory_writer.mem_));
+ }
+
+ EMSCRIPTEN_BINDINGS(my_module) {
+ value_object<WP2Options>("WP2Options")
+ .field("quality", &WP2Options::quality)
+ .field("alpha_quality", &WP2Options::alpha_quality)
+ .field("effort", &WP2Options::effort)
+ .field("pass", &WP2Options::pass)
+ .field("uv_mode", &WP2Options::uv_mode)
+ .field("csp_type", &WP2Options::csp_type)
+ .field("error_diffusion", &WP2Options::error_diffusion)
+ .field("use_random_matrix", &WP2Options::use_random_matrix)
+ .field("sns", &WP2Options::sns);
+
+ function("encode", &encode);
+ }
diff --cc src/features/encoders/jxl/worker/jxlEncode.ts
index 00000000,7a154d3b..0378c2a7
mode 000000,100644..100644
--- a/src/features/encoders/jxl/worker/jxlEncode.ts
+++ b/src/features/encoders/jxl/worker/jxlEncode.ts
@@@ -1,0 -1,65 +1,58 @@@
+ /**
+ * Copyright 2020 Google Inc. All Rights Reserved.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+ import type { JXLModule } from 'codecs/jxl/enc/jxl_enc';
+ import type { EncodeOptions } from '../shared/meta';
+
+ import { initEmscriptenModule } from 'features/worker-utils';
+ import { threads, simd } from 'wasm-feature-detect';
+
+ import wasmUrl from 'url:codecs/jxl/enc/jxl_enc.wasm';
+
+ import wasmUrlWithMT from 'url:codecs/jxl/enc/jxl_enc_mt.wasm';
+ import workerUrl from 'omt:codecs/jxl/enc/jxl_enc_mt.worker.js';
+
+ import wasmUrlWithMTAndSIMD from 'url:codecs/jxl/enc/jxl_enc_mt_simd.wasm';
+ import workerUrlWithSIMD from 'omt:codecs/jxl/enc/jxl_enc_mt_simd.worker.js';
+
+ let emscriptenModule: Promise<JXLModule>;
+
+ async function init() {
+ if (await threads()) {
+ if (await simd()) {
+ const jxlEncoder = await import('codecs/jxl/enc/jxl_enc_mt_simd');
+ return initEmscriptenModule(
+ jxlEncoder.default,
+ wasmUrlWithMTAndSIMD,
+ workerUrlWithSIMD,
+ );
+ }
+ const jxlEncoder = await import('codecs/jxl/enc/jxl_enc_mt');
- return initEmscriptenModule(
- jxlEncoder.default,
- wasmUrlWithMT,
- workerUrl,
- );
++ return initEmscriptenModule(jxlEncoder.default, wasmUrlWithMT, workerUrl);
+ }
+ const jxlEncoder = await import('codecs/jxl/enc/jxl_enc');
- return initEmscriptenModule(
- jxlEncoder.default,
- wasmUrl,
- );
++ return initEmscriptenModule(jxlEncoder.default, wasmUrl);
+ }
+
+ export default async function encode(
+ data: ImageData,
+ options: EncodeOptions,
+ ): Promise<ArrayBuffer> {
+ if (!emscriptenModule) emscriptenModule = init();
+
+ const module = await emscriptenModule;
+ const result = module.encode(data.data, data.width, data.height, options);
+
+ if (!result) throw new Error('Encoding error.');
+
+ return result.buffer;
+ }
diff --cc src/features/encoders/wp2/worker/wp2Encode.ts
index 00000000,96b21e26..1c12573b
mode 000000,100644..100644
--- a/src/features/encoders/wp2/worker/wp2Encode.ts
+++ b/src/features/encoders/wp2/worker/wp2Encode.ts
@@@ -1,0 -1,65 +1,58 @@@
+ /**
+ * Copyright 2020 Google Inc. All Rights Reserved.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+ import type { WP2Module } from 'codecs/wp2/enc/wp2_enc';
+ import type { EncodeOptions } from '../shared/meta';
+
+ import { initEmscriptenModule } from 'features/worker-utils';
+ import { threads, simd } from 'wasm-feature-detect';
+
+ import wasmUrl from 'url:codecs/wp2/enc/wp2_enc.wasm';
+
+ import wasmUrlWithMT from 'url:codecs/wp2/enc/wp2_enc_mt.wasm';
+ import workerUrl from 'omt:codecs/wp2/enc/wp2_enc_mt.worker.js';
+
+ import wasmUrlWithMTAndSIMD from 'url:codecs/wp2/enc/wp2_enc_mt_simd.wasm';
+ import workerUrlWithSIMD from 'omt:codecs/wp2/enc/wp2_enc_mt_simd.worker.js';
+
+ let emscriptenModule: Promise<WP2Module>;
+
+ async function init() {
+ if (await threads()) {
+ if (await simd()) {
+ const wp2Encoder = await import('codecs/wp2/enc/wp2_enc_mt_simd');
+ return initEmscriptenModule(
+ wp2Encoder.default,
+ wasmUrlWithMTAndSIMD,
+ workerUrlWithSIMD,
+ );
+ }
+ const wp2Encoder = await import('codecs/wp2/enc/wp2_enc_mt');
- return initEmscriptenModule(
- wp2Encoder.default,
- wasmUrlWithMT,
- workerUrl,
- );
++ return initEmscriptenModule(wp2Encoder.default, wasmUrlWithMT, workerUrl);
+ }
+ const wp2Encoder = await import('codecs/wp2/enc/wp2_enc');
- return initEmscriptenModule(
- wp2Encoder.default,
- wasmUrl,
- );
++ return initEmscriptenModule(wp2Encoder.default, wasmUrl);
+ }
+
+ export default async function encode(
+ data: ImageData,
+ options: EncodeOptions,
+ ): Promise<ArrayBuffer> {
+ if (!emscriptenModule) emscriptenModule = init();
+
+ const module = await emscriptenModule;
+ const result = module.encode(data.data, data.width, data.height, options);
+
+ if (!result) throw new Error('Encoding error.');
+
+ return result.buffer;
+ }