Skip to content

Fix JPEG segment ordering per JFIF and CIPA DC-007 standards#394

Open
minatoAI wants to merge 1 commit into
google:mainfrom
minatoAI:main
Open

Fix JPEG segment ordering per JFIF and CIPA DC-007 standards#394
minatoAI wants to merge 1 commit into
google:mainfrom
minatoAI:main

Conversation

@minatoAI

Copy link
Copy Markdown

Summary

The appendGainMap function builds JPEG output with incorrect APP marker ordering:

  1. JFIF (APP0) is written after XMP, ISO, MPF and ICC segments — it should be the first APP marker after SOI (per JFIF/JPEG specification)
  2. MPF (APP2) is written before JFIF and ICC, far from SOS — it should be placed right before SOS (per CIPA DC-007 Multi-Picture Format standard)

This causes HDR decoders that follow the standard ordering (e.g., Xiaohongshu/小红书) to fail to recognize the Ultra HDR gain map.

Changes

  1. lib/src/jpegr.cpp: Extract APP0(JFIF) from the base JPEG data and write it immediately after SOI. Parse the base JPEG marker segments (DQT, SOF, DHT) and write them before MPF. Write MPF right before SOS per CIPA DC-007.

  2. lib/src/jpegrutils.cpp: Add rdf:about="" attribute and hdrgm:OplusScale="-1" field to the gain map XMP metadata block, matching the Android Ultra HDR specification (developer.android.com/media/platform/hdr-image-format).

  3. CMakeLists.txt: Add -DWITH_CRT_DLL=ON to turbojpeg's ExternalProject CMAKE_ARGS when building on MSVC, to prevent CRT mismatch when the parent project uses dynamic CRT (/MD).

Testing

Verified that the fix produces a JPEG segment order identical to camera-captured Ultra HDR images (e.g., OPPO Find X8 Ultra), and the reordered file is correctly recognized as HDR by the Xiaohongshu platform.

@google-cla

google-cla Bot commented May 27, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

The appendGainMap function built JPEG output with incorrect marker ordering:
- JFIF (APP0) was written after XMP, ISO, MPF and ICC segments
  (must be the first APP marker after SOI per JFIF spec)
- MPF was written before JFIF and ICC, far from SOS
  (must be placed right before SOS per CIPA DC-007)

This caused platforms like Xiaohongshu to fail HDR recognition
because their decoders follow the standard marker ordering.

Fix:
1. Extract APP0(JFIF) from the base JPEG data and write it right after SOI
2. Parse the base JPEG marker segments (DQT, SOF, DHT) and write them before MPF
3. Write MPF right before SOS per CIPA DC-007
4. Write SOS + compressed data after MPF

Also:
- Added rdf:about="" attribute and hdrgm:OplusScale field to the gain map
  XMP metadata block (required by Android Ultra HDR specification)
- Added -DWITH_CRT_DLL=ON to turbojpeg ExternalProject for MSVC compatibility
- When BUILD_FOR_WINUI is defined, skip the forced /MT CRT override
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant