mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-12 16:57:26 +00:00
Few more simplifications to JXL decoder
This commit is contained in:
@@ -11,6 +11,9 @@ using namespace emscripten;
|
|||||||
thread_local const val Uint8ClampedArray = val::global("Uint8ClampedArray");
|
thread_local const val Uint8ClampedArray = val::global("Uint8ClampedArray");
|
||||||
thread_local const val ImageData = val::global("ImageData");
|
thread_local const val ImageData = val::global("ImageData");
|
||||||
|
|
||||||
|
// R, G, B, A
|
||||||
|
#define COMPONENTS_PER_PIXEL 4
|
||||||
|
|
||||||
#define EXPECT_TRUE(a) \
|
#define EXPECT_TRUE(a) \
|
||||||
if (!(a)) \
|
if (!(a)) \
|
||||||
return val::null();
|
return val::null();
|
||||||
@@ -24,14 +27,14 @@ val decode(std::string data) {
|
|||||||
EXPECT_EQ(JXL_DEC_SUCCESS,
|
EXPECT_EQ(JXL_DEC_SUCCESS,
|
||||||
JxlDecoderSubscribeEvents(
|
JxlDecoderSubscribeEvents(
|
||||||
dec.get(), JXL_DEC_BASIC_INFO | JXL_DEC_COLOR_ENCODING | JXL_DEC_FULL_IMAGE));
|
dec.get(), JXL_DEC_BASIC_INFO | JXL_DEC_COLOR_ENCODING | JXL_DEC_FULL_IMAGE));
|
||||||
|
|
||||||
auto next_in = (const uint8_t*)data.c_str();
|
auto next_in = (const uint8_t*)data.c_str();
|
||||||
auto avail_in = data.size();
|
auto avail_in = data.size();
|
||||||
EXPECT_EQ(JXL_DEC_BASIC_INFO, JxlDecoderProcessInput(dec.get(), &next_in, &avail_in));
|
EXPECT_EQ(JXL_DEC_BASIC_INFO, JxlDecoderProcessInput(dec.get(), &next_in, &avail_in));
|
||||||
size_t buffer_size;
|
|
||||||
const JxlPixelFormat format = {4, JXL_LITTLE_ENDIAN, JXL_TYPE_FLOAT};
|
|
||||||
EXPECT_EQ(JXL_DEC_SUCCESS, JxlDecoderImageOutBufferSize(dec.get(), &format, &buffer_size));
|
|
||||||
JxlBasicInfo info;
|
JxlBasicInfo info;
|
||||||
EXPECT_EQ(JXL_DEC_SUCCESS, JxlDecoderGetBasicInfo(dec.get(), &info));
|
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;
|
||||||
|
|
||||||
EXPECT_EQ(JXL_DEC_COLOR_ENCODING, JxlDecoderProcessInput(dec.get(), &next_in, &avail_in));
|
EXPECT_EQ(JXL_DEC_COLOR_ENCODING, JxlDecoderProcessInput(dec.get(), &next_in, &avail_in));
|
||||||
size_t icc_size;
|
size_t icc_size;
|
||||||
@@ -42,12 +45,13 @@ val decode(std::string data) {
|
|||||||
JxlDecoderGetColorAsICCProfile(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA,
|
JxlDecoderGetColorAsICCProfile(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA,
|
||||||
icc_profile.data(), icc_profile.size()));
|
icc_profile.data(), icc_profile.size()));
|
||||||
|
|
||||||
auto float_pixels = std::make_unique<float[]>((buffer_size + 3) / 4);
|
auto float_pixels = std::make_unique<float[]>(component_count);
|
||||||
EXPECT_EQ(JXL_DEC_SUCCESS,
|
static const JxlPixelFormat format = {COMPONENTS_PER_PIXEL, JXL_LITTLE_ENDIAN, JXL_TYPE_FLOAT};
|
||||||
JxlDecoderSetImageOutBuffer(dec.get(), &format, float_pixels.get(), buffer_size));
|
EXPECT_EQ(JXL_DEC_SUCCESS, JxlDecoderSetImageOutBuffer(dec.get(), &format, float_pixels.get(),
|
||||||
|
component_count * sizeof(float)));
|
||||||
EXPECT_EQ(JXL_DEC_FULL_IMAGE, JxlDecoderProcessInput(dec.get(), &next_in, &avail_in));
|
EXPECT_EQ(JXL_DEC_FULL_IMAGE, JxlDecoderProcessInput(dec.get(), &next_in, &avail_in));
|
||||||
|
|
||||||
auto pixels = std::make_unique<uint8_t[]>(info.xsize * info.ysize * 4);
|
auto byte_pixels = std::make_unique<uint8_t[]>(component_count);
|
||||||
// Convert to sRGB.
|
// Convert to sRGB.
|
||||||
skcms_ICCProfile jxl_profile;
|
skcms_ICCProfile jxl_profile;
|
||||||
// If the image is encoded in its original color space, the decoded data will be in the color
|
// If the image is encoded in its original color space, the decoded data will be in the color
|
||||||
@@ -63,12 +67,12 @@ val decode(std::string data) {
|
|||||||
EXPECT_TRUE(skcms_Transform(
|
EXPECT_TRUE(skcms_Transform(
|
||||||
float_pixels.get(), skcms_PixelFormat_RGBA_ffff,
|
float_pixels.get(), skcms_PixelFormat_RGBA_ffff,
|
||||||
info.alpha_premultiplied ? skcms_AlphaFormat_PremulAsEncoded : skcms_AlphaFormat_Unpremul,
|
info.alpha_premultiplied ? skcms_AlphaFormat_PremulAsEncoded : skcms_AlphaFormat_Unpremul,
|
||||||
&jxl_profile, pixels.get(), skcms_PixelFormat_RGBA_8888, skcms_AlphaFormat_Unpremul,
|
&jxl_profile, byte_pixels.get(), skcms_PixelFormat_RGBA_8888, skcms_AlphaFormat_Unpremul,
|
||||||
skcms_sRGB_profile(), info.xsize * info.ysize));
|
skcms_sRGB_profile(), pixel_count));
|
||||||
|
|
||||||
return ImageData.new_(
|
return ImageData.new_(
|
||||||
Uint8ClampedArray.new_(typed_memory_view(info.xsize * info.ysize * 4, pixels.get())),
|
Uint8ClampedArray.new_(typed_memory_view(component_count, byte_pixels.get())), info.xsize,
|
||||||
info.xsize, info.ysize);
|
info.ysize);
|
||||||
}
|
}
|
||||||
|
|
||||||
EMSCRIPTEN_BINDINGS(my_module) {
|
EMSCRIPTEN_BINDINGS(my_module) {
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user