Repository navigation
Description
Feature request
What are you trying to achieve?
Add support for transcoding JPG gain maps. These images enable the ability to share images edited for modern displays that support HDR (high dynamic range). And compared to HDR-only encoding, using a gain map allows the image to be properly adapted to any display which does not support the full HDR encoding. That adaptation is critical to being able to safely share HDR images on any display - including SDR ("standard dynamic range") and monitors with limited HDR capabilities.
These HDR displays are already more common than generally known. Roughly 80% of viewers on Instagram already have HDR support and you can see examples of HDR vs standard images in my Instagram feed in the IG phone app (or using Chrome with an M1+ MacBook Pro).
When you searched for similar feature requests, what did you find that might be related?
There is a request for JXL support (#4263). While that is a great format, JPG (and AVIF) with a gain map is more practical. The JPG format is 100% backwards compatible and already widely supported. The AVIF gain map encoding will be very important and already has support in Adobe software and Chrome. We're probably a year away from having enough critical support to use AVIF, but there is clear and steady progress towards that point. There is little support for JXL and it is unclear if it will gain acceptance on the web (especially as Chrome removed support).
What would you expect the API to look like?
Should support simple transcoding of JPG (crop, resize, compress) while retaining the HDR. That means the base image and auxiliary gain map should be edited in a consistent manner, and the HDR metadata should be retained.
The Google-backed open-source libultrahdr library already supports all of this. All that is needed is some integration with libvips.
What alternatives have you considered?
There is a request for libvips to add support: libvips/libvips#3799
Please provide sample image(s) that help explain this feature
More info on HDR and gain maps.
Here is a sample image. The image will tell you if you are viewing just the SDR base image, or viewing as HDR (when using a supported browser and display, you should see blue text). The image is encoded with both the ISO and Android XMP standards (the ISO encoding is really all that is needed, the XMP encoding is redundant for backwards compatibility, but won't be important for too long).

Developer resources for gain maps: https://gregbenzphotography.com/hdr/#developers
Activity
lovell commentedon Jan 20, 2025
This will be dependent on libvips/libvips#3799
gregbenz commentedon Sep 3, 2025
@lovell libvips has added initial support for libultrahdr: libvips/libvips#4645
This is built on mainline libultrahdr which supports transcoding of JPG (there is ongoing work to add support for AVIF / HEIF to libultrahdr).
lovell commentedon Dec 17, 2025
Proposed new API to expose JPEG gain maps (via libultrahdr) now that libvips v8.18.0 is available.
withGainMap()Convert pixel values to HDR via
.pipelineColourspace('scrgb')using the input gain map, if any, and then remove it. For JPEG output it also sets.toColourspace('scrgb')to force the creation of a new gain map.keepGainMap()Open input with gain map attached, process main image and gain map separately on a best-effort basis. Many operations might invalidate/remove the gain map, these will generate a warning and switch to
withGainMap()behaviour.The list of operations I'd like to support where this will "just work" are:
Notes:
libultrahdrwill ignore all JPEG output properties apart fromquality.The prebuilt binaries includemozjpegso this will be the same asmozjpeg: true, with no further control.libultrahdrappears to generate/attach gain maps using ISO 21496-1 format, which I think is via an APP2 MPF marker. I assume this is suitable for web browsers.dzsave, which has some built-in gain map logic.metadata().Add withGainMap to process HDR JPEGs with embedded gain map #4314
gregbenz commentedon Dec 20, 2025
@lovell Can you confirm that keepGainMap() would be the default behavior?
Many developers aren't yet familiar with gain maps, lack an HDR display for visual confirmation of results, or fail to use gain map source images in validation testing. So opting in to keepGainMap would likely result in a large number of transcodes defaulting to significant loss of quality (no HDR) without this being a deliberate choice by the developer.
I believe keeping the map should be the default when the following are true:
lovell commentedon Jan 4, 2026
A pre-release containing the (simpler)
withGainMap()feature is now available should anyone wish to test.The next step is to implement
keepGainMap(). This feature will be marked as experimental in its first release so should probably be opt-in until more people have tried it and had a chance to provide feedback.gregbenz commentedon Jan 4, 2026
@lovell Thank you for all the work to add support!
I just ran a test and it will transcode a gain map to a new gain map, but the output is incorrect (resulting in significant change to the image brightness and color). I've tested a few approaches, all using withGainMap() and they all seem to fail in a similar manner.
There are two issues:
Likely this usage needs to wait for keepGainMap(), but I wanted to note my observations. More importantly, I would not expect this result from withGainMap() - as the output is neither built from the source nor does it throw an error with my lack of an explicit / separate map source.
Aside from the image appearance, it also strips other metadata (unrelated to the gain map). I assume this is probably a default behavior for Sharp. Could you confirm if there is a command you need to preserve metadata (such as XMP-photoshop:Main)?
gregbenz commentedon Jan 4, 2026
@lovell If you need some images to test, here are sRGB and P3 test images. These include test patterns which catch most of the transcoding errors I have seen over the past few years of testing a wide range of software/services.
gain map tests.zip
Comparing source to the transcode on an M1 or later MacBook Pro using Chrome (or any related browser - but not Safari / FireFox) with system brightness no higher than 50% (to get a full 4 stops of headroom) works best to review the results.
lovell commentedon Jan 4, 2026
Thanks for taking a look at this Greg.
Yes,
withGainMapis the "convert pixel values to scRGB, using (then discarding) gain map if present, re-encode with new gain map for JPEG output" approach. The provisional docs for it are available at https://github.com/lovell/sharp/blob/0.35/docs/src/content/docs/api-output.md#withgainmapWhat you describe is more in line with the yet to be implemented
keepGainMap.gregbenz commentedon Jan 4, 2026
@lovell Thanks for the clarification.
I believe the name "withGainMap" is ambiguous. Any use of libultra for input or output involves a gain map and is therefore "with" a gain map. The "keepGainMap" clearly asks to preserve the map from the input, but it may be better to rename withGainMap to something which makes it clear that a new map will be generated.
Based on your intended use, perhaps "withGainMap" should be changed to something like "generateGainMap" or "newGainMap".
3 remaining items
gregbenz commentedon Feb 20, 2026
@lovell I've created a site to help make it easy to compare two images under various levels of HDR headroom support. You can select two images (source and the transcoded result) into the page, then use the slider to see the image anywhere between the SDR result and the best possible HDR supported by the display. Click the image button to toggle between to compare source to result.
This page should be viewed with a Chromium browser, ideally on a system supporting a full 4 stops of HDR headroom (such as an M1 or later MacBook Pro set to ~50% brightness).
https://gregbenzphotography.com/hdr-soft-proofing-test-hdr-gain-maps/
lovell commentedon Feb 22, 2026
Thanks for the updates Greg, I've finally purchased an HDR monitor (LG GX7) so can start to make progress on this.
gregbenz commentedon Feb 22, 2026
lovell commentedon Apr 6, 2026
For those interested in advance testing purposes, the latest release candidate contains the proposed new
keepGainMapfeature.https://github.com/lovell/sharp/blob/0.35/docs/src/content/docs/api-output.md#keepgainmap
This will be opt-in for now, at least while considered experimental.
gregbenz commentedon Apr 8, 2026
@lovell I'm seeing very good results with 0.35.0-rc.2. The default results look very good to me so far. No concerns with the image results (I did log a potential bug interacting with keepMetadata). Thank you for all your efforts to incorporate gain map support into sharp!
The performance is very good. I'm seeing a 100 megapixel Hasselblad JPG transcode to a smaller size in about 0.3s. This is considerably faster than wasm-vips (closer to 0.6s for the same operation on the same image). Would you expect wasm-vips to take ~2x longer, or is that something to report for that project?
I do not see options to control the compression of the map itself (both base and map use a factor consistent with the single compression factor input supported now). I don't see options to scale down the map (such as to 1:2) or convert a 3-channel map to luminosity only. I don't think any of these controls are critical (and down-sampling the map is rather negative for quality), but could be useful for those who wish to optimize for smaller sizes. For reference, these are all available via wasm-vips (and may be evaluated on my wasm-vips HDR demo page).
Sample code for others who may wish to test:
lovell commentedon Apr 11, 2026
The underlying libjpeg-turbo library does not yet support WebAssembly SIMD so this is currently expected behaviour.
For new, experimental features I generally prefer to minimise optional behaviour and use sensible defaults. As you've probably seen, the gain map scale factor in the output will be the same as the scale factor of the input, and the channel count will also remain the same. Let's see how people use this feature and what feedback we get.
Thanks again for all your help testing this and, on a wider scale, everything you've been doing to improve HDR support everywhere.
gregbenz commentedon Apr 11, 2026
Good to know libjpeg-turbo is the bottleneck there. It appears there is a long-standing feature request for it.
gregbenz commentedon Apr 21, 2026
@lovell Great to see keepGainMap() getting released!
With the end goal being for sharp to automatically use this where appropriate (ie no need to request keepGainMap), what are the logical next steps to help get there as quickly as is safe and practical? What may be done to help gain confidence that it it would be safe to automatically route images to use keepGainMap automatically?
lovell commentedon Jun 10, 2026
v0.35.0 now available with the (experimental for now)
keepGainMapandwithGainMapfeatures. Thanks for all your help with this Greg!After libvips gets proper CICP support I'll look at improving the API in sharp, including keeping gain maps where possible as a default.
gregbenz commentedon Jun 10, 2026
@lovell Awesome, thank you for all your efforts to incorporate it and move toward support by default when possible!
For sharp, is there a JPG-specific block now? CICP would impact AVIF transcoding, but is not supported in the JPG codestream (can be added as a tag in an ICC profile in a JPG, but is a bit of a novelty and not important to any use case I currently know). I have been testing sharp (locally as I don't have Node on my server) and seeing good results transcoding JPG gain maps. If you have an experimental which adds default support, I'd be happy to give that a look. I suspect it should work, as this is JPG-only at this time in libultrahdr (anything AVIF would use the default handling outside libultrahdr, which should be treated like any other AVIF without a gain map at this time).
lovell commentedon Jun 11, 2026
My comment about the work-in-progress CICP support relates to AVIF/HEIF rather than JPEG.
chore(deps): update dependency sharp to ^0.35.5 (#9)