Added in API level 34

Gainmap


class Gainmap : Parcelable
kotlin.Any
   ↳ android.graphics.Gainmap

Gainmap represents a mechanism for augmenting an SDR image to produce an HDR one with variable display adjustment capability. It is a combination of a set of metadata describing how to apply the gainmap, as well as either a 1 (such as android.graphics.Bitmap.Config#ALPHA_8 or 3 (such as android.graphics.Bitmap.Config#ARGB_8888 with the alpha channel ignored) channel Bitmap that represents the gainmap data itself.

When rendering to an android.content.pm.ActivityInfo#COLOR_MODE_HDR activity, the hardware accelerated Canvas will automatically apply the gainmap when sufficient HDR headroom is available.

Gainmap Structure

The logical whole of a gainmap'd image consists of a base Bitmap that represents the original image as would be displayed without gainmap support in addition to a gainmap with a second enhancement image. In the case of a JPEG, the base image would be the typical 8-bit SDR image that the format is commonly associated with. The gainmap image is embedded alongside the base image, often at a lower resolution (such as 1/4th), along with some metadata to describe how to apply the gainmap. The gainmap image itself is then a greyscale image representing the transformation to apply onto the base image to reconstruct an HDR rendition of it.

As such these "gainmap images" consist of 3 parts - a base Bitmap with a Bitmap#getGainmap() that returns an instance of this class which in turn contains the enhancement layer represented as another Bitmap, accessible via getGainmapContents()

Applying a gainmap manually

When doing custom rendering such as to an OpenGL ES or Vulkan context, the gainmap is not automatically applied. In such situations, the following steps are appropriate to render the gainmap in combination with the base image.

Suppose our display has HDR to SDR ratio of H, and we wish to display an image with gainmap on this display. Let B be the pixel value from the base image in a color space that has the primaries of the base image and a linear transfer function. Let G be the pixel value from the gainmap. Let D be the output pixel in the same color space as B. The value of D is computed as follows:

First, let W be a weight parameter determining how much the gainmap will be applied.

W = clamp((log(H)                      - log(minDisplayRatioForHdrTransition)) /
              (log(displayRatioForFullHdr) - log(minDisplayRatioForHdrTransition), 0, 1)
Next, let L be the gainmap value in log space. We compute this from the value G that was sampled from the texture as follows:
L = mix(log(ratioMin), log(ratioMax), pow(G, gamma))
Finally, apply the gainmap to compute D, the displayed pixel. If the base image is SDR then compute:
D = (B + epsilonSdr) * exp(L * W) - epsilonHdr

In the above math, log() is a natural logarithm and exp() is natural exponentiation. The base for these functions cancels out and does not affect the result, so other bases may be used if preferred.

Summary

Inherited constants
Public constructors
Gainmap(gainmapContents: Bitmap)

Creates a gainmap from a given Bitmap.

Gainmap(gainmap: Gainmap, gainmapContents: Bitmap)

Creates a new gainmap using the provided gainmap as the metadata source and the provided bitmap as the replacement for the gainmapContents

Public methods
Int

No special parcel contents.

Float

Gets the hdr/sdr ratio at which point the gainmap is fully applied.

FloatArray

Gets the hdr epsilon.

FloatArray

Gets the sdr epsilon.

Bitmap

FloatArray

Gets the gainmap gamma.

Float

Gets the hdr/sdr ratio below which only the SDR image is displayed.

FloatArray

Gets the gainmap ratio max.

FloatArray

Gets the gainmap ratio max.

Unit

Sets the hdr/sdr ratio at which point the gainmap is fully applied.

Unit

Sets the hdr epsilon which is used to avoid numerical instability.

Unit

Sets the sdr epsilon which is used to avoid numerical instability.

Unit

Sets the image data of the gainmap.

Unit
setGamma(r: Float, g: Float, b: Float)

Sets the gainmap gamma.

Unit

Sets the hdr/sdr ratio below which only the SDR image is displayed.

Unit
setRatioMax(r: Float, g: Float, b: Float)

Sets the gainmap ratio max.

Unit
setRatioMin(r: Float, g: Float, b: Float)

Sets the gainmap ratio min.

Unit
writeToParcel(dest: Parcel, flags: Int)

Write the gainmap to the parcel.

Properties
static Parcelable.Creator<Gainmap!>

Public constructors

Gainmap

Added in API level 34
Gainmap(gainmapContents: Bitmap)

Creates a gainmap from a given Bitmap. The caller is responsible for setting the various fields to the desired values. The defaults are as follows:

  • Ratio min is 1f, 1f, 1f
  • Ratio max is 2f, 2f, 2f
  • Gamma is 1f, 1f, 1f
  • Epsilon SDR is 0f, 0f, 0f
  • Epsilon HDR is 0f, 0f, 0f
  • Display ratio SDR is 1f
  • Display ratio HDR is 2f
It is strongly recommended that at least the ratio max and display ratio HDR are adjusted to better suit the given gainmap contents.

Parameters
gainmapContents Bitmap: This value cannot be null.

Gainmap

Added in API level 35
Gainmap(
    gainmap: Gainmap,
    gainmapContents: Bitmap)

Creates a new gainmap using the provided gainmap as the metadata source and the provided bitmap as the replacement for the gainmapContents

Parameters
gainmap Gainmap: This value cannot be null.
gainmapContents Bitmap: This value cannot be null.

Public methods

describeContents

Added in API level 34
fun describeContents(): Int

No special parcel contents.

Return
Int a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or android.os.Parcelable#CONTENTS_FILE_DESCRIPTOR

getDisplayRatioForFullHdr

Added in API level 34
fun getDisplayRatioForFullHdr(): Float

Gets the hdr/sdr ratio at which point the gainmap is fully applied.

Return
Float This value cannot be null.

getEpsilonHdr

Added in API level 34
fun getEpsilonHdr(): FloatArray

Gets the hdr epsilon. For single-plane gainmaps, all 3 components should be the same. The components are in r, g, b order.

Return
FloatArray This value cannot be null.

getEpsilonSdr

Added in API level 34
fun getEpsilonSdr(): FloatArray

Gets the sdr epsilon. For single-plane gainmaps, all 3 components should be the same. The components are in r, g, b order.

Return
FloatArray This value cannot be null.

getGainmapContents

Added in API level 34
fun getGainmapContents(): Bitmap
Return
Bitmap Returns the image data of the gainmap represented as a Bitmap. This is represented as a Bitmap for broad API compatibility, however certain aspects of the Bitmap are ignored such as Bitmap#getColorSpace() or Bitmap#getGainmap() as they are not relevant to the gainmap's enhancement layer. This value cannot be null.

getGamma

Added in API level 34
fun getGamma(): FloatArray

Gets the gainmap gamma. For single-plane gainmaps, all 3 components should be the same. The components are in r, g, b order.

Return
FloatArray This value cannot be null.

getMinDisplayRatioForHdrTransition

Added in API level 34
fun getMinDisplayRatioForHdrTransition(): Float

Gets the hdr/sdr ratio below which only the SDR image is displayed.

Return
Float This value cannot be null.

getRatioMax

Added in API level 34
fun getRatioMax(): FloatArray

Gets the gainmap ratio max. For single-plane gainmaps, all 3 components should be the same. The components are in r, g, b order.

Return
FloatArray This value cannot be null.

getRatioMin

Added in API level 34
fun getRatioMin(): FloatArray

Gets the gainmap ratio max. For single-plane gainmaps, all 3 components should be the same. The components are in r, g, b order.

Return
FloatArray This value cannot be null.

setDisplayRatioForFullHdr

Added in API level 34
fun setDisplayRatioForFullHdr(max: Float): Unit

Sets the hdr/sdr ratio at which point the gainmap is fully applied.

Parameters
max Float: The hdr/sdr ratio at which the gainmap is fully applied. Must be >= 1.0f Value is 1.0f or greater

setEpsilonHdr

Added in API level 34
fun setEpsilonHdr(
    r: Float,
    g: Float,
    b: Float
): Unit

Sets the hdr epsilon which is used to avoid numerical instability. For single-plane gainmaps, r, g, and b should be the same.

setEpsilonSdr

Added in API level 34
fun setEpsilonSdr(
    r: Float,
    g: Float,
    b: Float
): Unit

Sets the sdr epsilon which is used to avoid numerical instability. For single-plane gainmaps, r, g, and b should be the same.

setGainmapContents

Added in API level 34
fun setGainmapContents(bitmap: Bitmap): Unit

Sets the image data of the gainmap. This is the 1 or 3 channel enhancement layer to apply to the base image. This is represented as a Bitmap for broad API compatibility, however certain aspects of the Bitmap are ignored such as Bitmap#getColorSpace() or Bitmap#getGainmap() as they are not relevant to the gainmap's enhancement layer.

Parameters
bitmap Bitmap: The non-null bitmap to set as the gainmap's contents

setGamma

Added in API level 34
fun setGamma(
    r: Float,
    g: Float,
    b: Float
): Unit

Sets the gainmap gamma. For single-plane gainmaps, r, g, and b should be the same.

setMinDisplayRatioForHdrTransition

Added in API level 34
fun setMinDisplayRatioForHdrTransition(min: Float): Unit

Sets the hdr/sdr ratio below which only the SDR image is displayed.

Parameters
min Float: The minimum hdr/sdr ratio at which to begin applying the gainmap. Must be >= 1.0f Value is 1.0f or greater

setRatioMax

Added in API level 34
fun setRatioMax(
    r: Float,
    g: Float,
    b: Float
): Unit

Sets the gainmap ratio max. For single-plane gainmaps, r, g, and b should be the same.

setRatioMin

Added in API level 34
fun setRatioMin(
    r: Float,
    g: Float,
    b: Float
): Unit

Sets the gainmap ratio min. For single-plane gainmaps, r, g, and b should be the same.

writeToParcel

Added in API level 34
fun writeToParcel(
    dest: Parcel,
    flags: Int
): Unit

Write the gainmap to the parcel.

Parameters
dest Parcel: Parcel object to write the gainmap data into This value cannot be null.
flags Int: Additional flags about how the object should be written.

Properties

CREATOR

Added in API level 34
static val CREATOR: Parcelable.Creator<Gainmap!>