From 807e352e1b3d6d1dfb957d01411344492a53182d Mon Sep 17 00:00:00 2001 From: robo-mop Date: Wed, 30 Aug 2023 16:03:12 +0530 Subject: [PATCH] Code for migrating to JXL 0.7 --- codecs/cpp.Dockerfile | 4 ++-- codecs/jxl/Makefile | 4 ++-- codecs/jxl/dec/jxl_dec.cpp | 8 ++++++++ codecs/jxl/enc/example.html | 40 +++++++++++++++++++++++++++++++++++++ codecs/jxl/enc/jxl_enc.cpp | 34 ++++++++++++++++++------------- 5 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 codecs/jxl/enc/example.html diff --git a/codecs/cpp.Dockerfile b/codecs/cpp.Dockerfile index 0a2aa0c41..dfc8716a5 100644 --- a/codecs/cpp.Dockerfile +++ b/codecs/cpp.Dockerfile @@ -1,5 +1,5 @@ -FROM emscripten/emsdk:2.0.23 -# RUN apt-get update && apt-get install -qqy autoconf libtool pkg-config +FROM emscripten/emsdk:2.0.34 +RUN apt-get update && apt-get install -qqy autoconf libtool pkg-config ENV CFLAGS "-O3 -flto" ENV CXXFLAGS "${CFLAGS} -std=c++17" ENV LDFLAGS "${CFLAGS} \ diff --git a/codecs/jxl/Makefile b/codecs/jxl/Makefile index c59e52cc4..9a056d7c5 100644 --- a/codecs/jxl/Makefile +++ b/codecs/jxl/Makefile @@ -1,5 +1,5 @@ CODEC_URL = https://github.com/libjxl/libjxl.git -CODEC_VERSION = 9f544641ec83f6abd9da598bdd08178ee8a003e0 +CODEC_VERSION = f95da131cf7c7ccd4da256356fde2fec1fa23bb5 CODEC_DIR = node_modules/jxl CODEC_BUILD_ROOT := $(CODEC_DIR)/build CODEC_MT_BUILD_DIR := $(CODEC_BUILD_ROOT)/mt @@ -33,7 +33,7 @@ enc/jxl_enc_mt_simd.js: $(CODEC_MT_SIMD_BUILD_DIR)/lib/libjxl.a $(CODEC_MT_SIMD_ export CXXFLAGS += -Wno-deprecated-declarations # Compile multithreaded wrappers with -pthread. -enc/jxl_enc_mt.js enc/jxl_enc_mt_simd.js: CXXFLAGS+=-pthread +enc/jxl_enc_mt.js enc/jxl_enc_mt_simd.js dec/jxl_dec.js: CXXFLAGS+=-pthread $(OUT_JS): $(CXX) \ diff --git a/codecs/jxl/dec/jxl_dec.cpp b/codecs/jxl/dec/jxl_dec.cpp index e56f4bdb4..256f22935 100644 --- a/codecs/jxl/dec/jxl_dec.cpp +++ b/codecs/jxl/dec/jxl_dec.cpp @@ -43,6 +43,8 @@ thread_local const val ImageData = val::global("ImageData"); #endif val decode(std::string data) { + printf("JXL Decode start\n"); + // printf("Length of data: %lu\n", data.length()); std::unique_ptr> dec(JxlDecoderCreate(nullptr)); @@ -50,6 +52,7 @@ val decode(std::string data) { JxlDecoderSubscribeEvents( dec.get(), JXL_DEC_BASIC_INFO | JXL_DEC_COLOR_ENCODING | JXL_DEC_FULL_IMAGE)); + // printf("decode 1\n"); auto next_in = (const uint8_t*)data.c_str(); auto avail_in = data.size(); JxlDecoderSetInput(dec.get(), next_in, avail_in); @@ -58,7 +61,9 @@ val decode(std::string data) { EXPECT_EQ(JXL_DEC_SUCCESS, JxlDecoderGetBasicInfo(dec.get(), &info)); size_t pixel_count = info.xsize * info.ysize; size_t component_count = pixel_count * COMPONENTS_PER_PIXEL; + // printf("Component count = %zu\n", component_count); + // printf("decode 2\n"); EXPECT_EQ(JXL_DEC_COLOR_ENCODING, JxlDecoderProcessInput(dec.get())); static const JxlPixelFormat format = {COMPONENTS_PER_PIXEL, JXL_TYPE_FLOAT, JXL_LITTLE_ENDIAN, 0}; size_t icc_size; @@ -69,6 +74,7 @@ val decode(std::string data) { JxlDecoderGetColorAsICCProfile(dec.get(), &format, JXL_COLOR_PROFILE_TARGET_DATA, icc_profile.data(), icc_profile.size())); + // printf("decode 3\n"); EXPECT_EQ(JXL_DEC_NEED_IMAGE_OUT_BUFFER, JxlDecoderProcessInput(dec.get())); size_t buffer_size; EXPECT_EQ(JXL_DEC_SUCCESS, JxlDecoderImageOutBufferSize(dec.get(), &format, &buffer_size)); @@ -89,6 +95,8 @@ val decode(std::string data) { &jxl_profile, byte_pixels.get(), skcms_PixelFormat_RGBA_8888, skcms_AlphaFormat_Unpremul, skcms_sRGB_profile(), pixel_count)); + printf("JXL Decode end\n"); + // printf("info: xsize=%d ysize=%d\n", info.xsize, info.ysize); return ImageData.new_( Uint8ClampedArray.new_(typed_memory_view(component_count, byte_pixels.get())), info.xsize, info.ysize); diff --git a/codecs/jxl/enc/example.html b/codecs/jxl/enc/example.html new file mode 100644 index 000000000..cca38c415 --- /dev/null +++ b/codecs/jxl/enc/example.html @@ -0,0 +1,40 @@ + + + diff --git a/codecs/jxl/enc/jxl_enc.cpp b/codecs/jxl/enc/jxl_enc.cpp index 0a08386a4..51abd54c4 100644 --- a/codecs/jxl/enc/jxl_enc.cpp +++ b/codecs/jxl/enc/jxl_enc.cpp @@ -2,9 +2,9 @@ #include #include "lib/jxl/base/thread_pool_internal.h" +#include "lib/jxl/enc_color_management.h" #include "lib/jxl/enc_external_image.h" #include "lib/jxl/enc_file.h" -#include "lib/jxl/enc_color_management.h" using namespace emscripten; @@ -22,6 +22,7 @@ struct JXLOptions { }; val encode(std::string image, int width, int height, JXLOptions options) { + printf("encoding start\n"); jxl::CompressParams cparams; jxl::PassesEncoderState passes_enc_state; jxl::CodecInOut io; @@ -55,13 +56,13 @@ val encode(std::string image, int width, int height, JXLOptions options) { if (options.lossyModular || quality == 100) { cparams.modular_mode = true; // Internal modular quality to roughly match VarDCT size. - if (quality < 7) { - cparams.quality_pair.first = cparams.quality_pair.second = - std::min(35 + (quality - 7) * 3.0f, 100.0f); - } else { - cparams.quality_pair.first = cparams.quality_pair.second = - std::min(35 + (quality - 7) * 65.f / 93.f, 100.0f); - } + // if (quality < 7) { + // cparams.quality_pair.first = cparams.quality_pair.second = + // std::min(35 + (quality - 7) * 3.0f, 100.0f); + // } else { + // cparams.quality_pair.first = cparams.quality_pair.second = + // std::min(35 + (quality - 7) * 65.f / 93.f, 100.0f); + // } } else { cparams.modular_mode = false; if (quality >= 30) { @@ -80,7 +81,7 @@ val encode(std::string image, int width, int height, JXLOptions options) { } if (cparams.modular_mode) { - if (cparams.quality_pair.first != 100 || cparams.quality_pair.second != 100) { + if (!cparams.IsLossless()) { cparams.color_transform = jxl::ColorTransform::kXYB; } else { cparams.color_transform = jxl::ColorTransform::kNone; @@ -96,19 +97,24 @@ val encode(std::string image, int width, int height, JXLOptions options) { auto result = jxl::ConvertFromExternal( jxl::Span(reinterpret_cast(image.data()), image.size()), width, - height, jxl::ColorEncoding::SRGB(/*is_gray=*/false), /*has_alpha=*/true, - /*alpha_is_premultiplied=*/false, /*bits_per_sample=*/8, /*endiannes=*/JXL_LITTLE_ENDIAN, - /*flipped_y=*/false, pool_ptr, main, /*(only true if bits_per_sample==32) float_in=*/false); + height, jxl::ColorEncoding::SRGB(/*is_gray=*/false), 4, /*alpha_is_premultiplied=*/false, + /*bits_per_sample=*/8, /*endiannes=*/JXL_LITTLE_ENDIAN, pool_ptr, main, + /*(only true if bits_per_sample==32) float_in=*/false, 0); if (!result) { return val::null(); } auto js_result = val::null(); - if (EncodeFile(cparams, &io, &passes_enc_state, &bytes, jxl::GetJxlCms(), /*aux=*/nullptr, pool_ptr)) { - js_result = Uint8Array.new_(typed_memory_view(bytes.size(), bytes.data())); + if (EncodeFile(cparams, &io, &passes_enc_state, &bytes, jxl::GetJxlCms(), /*aux=*/nullptr, + pool_ptr)) { + printf("Number of bytes: %zu\n", bytes.size()); // works + auto g = typed_memory_view(bytes.size(), bytes.data()); + printf("Number of bytes 2: %zu\n", bytes.size()); // works + js_result = Uint8Array.new_(g); } + printf("encode end\n"); return js_result; }