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 | |
---|---|
Creates a gainmap from a given 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 |
getGamma() 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 |
setEpsilonHdr(r: Float, g: Float, b: Float) Sets the hdr epsilon which is used to avoid numerical instability. |
Unit |
setEpsilonSdr(r: Float, g: Float, b: Float) Sets the sdr epsilon which is used to avoid numerical instability. |
Unit |
setGainmapContents(bitmap: Bitmap) Sets the image data of the gainmap. |
Unit |
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
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
Parameters | |
---|---|
gainmapContents |
Bitmap: This value cannot be null . |
Gainmap
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
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
fun getDisplayRatioForFullHdr(): Float
Gets the hdr/sdr ratio at which point the gainmap is fully applied.
Return | |
---|---|
Float |
This value cannot be null . |
getEpsilonHdr
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
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
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
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
fun getMinDisplayRatioForHdrTransition(): Float
Gets the hdr/sdr ratio below which only the SDR image is displayed.
Return | |
---|---|
Float |
This value cannot be null . |
getRatioMax
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
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
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
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
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
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
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
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
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
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
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. |