From 2ae3c547c37c0dd19f051cfb7b5427d24eb26138 Mon Sep 17 00:00:00 2001 From: Tejas Patel Date: Wed, 23 Sep 2026 19:25:11 +0000 Subject: [PATCH] jpegrutils: set use_base_cg for Apple gain maps The Apple decode path in getMetadataFromXMP() returns early without setting use_base_cg, so it stays false. Decoding is unaffected, but re-encoding the decoded base image, gain map and metadata (e.g. to resize an Apple HDR JPEG) then fails in encodeJPEGR with "ICC marker in gainmap jpeg is missing": use_base_cg == false tells the encoder the gain map lives in an alternate color space and must carry its own ICC profile, which Apple gain-map images do not. Apple applies the gain map in the base image's color space, so set use_base_cg = true, matching the ISO and Adobe XMP paths. Test: decodeApple now asserts use_base_cg for both Apple fixtures (it fails without this change). --- lib/src/jpegrutils.cpp | 3 +++ tests/jpegr_test.cpp | 1 + 2 files changed, 4 insertions(+) diff --git a/lib/src/jpegrutils.cpp b/lib/src/jpegrutils.cpp index 3f064ade..a1b0ac86 100644 --- a/lib/src/jpegrutils.cpp +++ b/lib/src/jpegrutils.cpp @@ -729,6 +729,9 @@ uhdr_error_info_t getMetadataFromXMP(uint8_t* xmp_data, size_t xmp_size, uint8_t metadata->offset_hdr[c] = 0.0f; } metadata->hdr_capacity_min = 1.0f; + // Apple applies the gain map in the base image's color space, and its gain-map + // image carries no ICC profile. + metadata->use_base_cg = true; float max_content_boost; bool present = false; diff --git a/tests/jpegr_test.cpp b/tests/jpegr_test.cpp index 8748dac1..019c0b85 100644 --- a/tests/jpegr_test.cpp +++ b/tests/jpegr_test.cpp @@ -1753,6 +1753,7 @@ TEST(JpegRTest, decodeApple) { } EXPECT_EQ(gainmapMetadata->hdr_capacity_min, 1.0f); EXPECT_FLOAT_EQ(gainmapMetadata->hdr_capacity_max, headroom); + EXPECT_TRUE(gainmapMetadata->use_base_cg); uhdr_release_decoder(dec); }