| 1 |
/* |
| 2 |
* Copyright 2025 Google LLC. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef SkHdrAgtmPriv_DEFINED |
| 9 |
#define SkHdrAgtmPriv_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkColorSpace.h" |
| 13 |
#include "include/core/SkImage.h" |
| 14 |
#include "include/core/SkRefCnt.h" |
| 15 |
#include "include/core/SkSpan.h" |
| 16 |
#include "include/private/SkHdrMetadata.h" |
| 17 |
|
| 18 |
#include <optional> |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
class SkData; |
| 22 |
class SkString; |
| 23 |
|
| 24 |
namespace skhdr { |
| 25 |
|
| 26 |
class Metadata; |
| 27 |
|
| 28 |
// Collection of functions and structures that could potentially be moved into |
| 29 |
// the AdaptiveGlobalToneMap structure or its sub-structures, but are not exposed yet. |
| 30 |
namespace AgtmHelpers { |
| 31 |
|
| 32 |
/** |
| 33 |
* The function evaluation described in Clause 6.3.2. |
| 34 |
*/ |
| 35 |
SkColor4f EvaluateColorGainFunction(const AdaptiveGlobalToneMap::ColorGainFunction& gain, |
| 36 |
const SkColor4f& c); |
| 37 |
|
| 38 |
/** |
| 39 |
* The function evaluation described in Clause 6.4.3. |
| 40 |
*/ |
| 41 |
SkColor4f EvaluateComponentMixingFunction(const AdaptiveGlobalToneMap::ComponentMixingFunction& mix, |
| 42 |
const SkColor4f& c); |
| 43 |
|
| 44 |
/** |
| 45 |
* The function evaluation described in Clause 6.5.3. |
| 46 |
*/ |
| 47 |
float EvaluateGainCurve(const AdaptiveGlobalToneMap::GainCurve& gainCurve, float x); |
| 48 |
|
| 49 |
/** |
| 50 |
* Populate the fM values using the Piecewise Cubic Hermite Interpolation Package (PCHIP) |
| 51 |
* algorithm, described in Clause C.3.9: Piecewise cubic hermite interpolation package slope |
| 52 |
* computation. |
| 53 |
*/ |
| 54 |
void PopulateSlopeFromPCHIP(AdaptiveGlobalToneMap::GainCurve& gainCurve); |
| 55 |
|
| 56 |
/** |
| 57 |
* Compute the weighting for the specified targeted HDR headroom according to the computations |
| 58 |
* in Clause 6.2.5, Computation of the headroom-adaptive tone map. |
| 59 |
*/ |
| 60 |
struct Weighting { |
| 61 |
// The index into fAlternateImages for fWeight. If fWeight[i] is 0 then |
| 62 |
// fAlternateImageIndex[i] is not used and should be set to kInvalidIndex. |
| 63 |
static constexpr uint8_t kInvalidIndex = 255; |
| 64 |
uint8_t fAlternateImageIndex[2] = {kInvalidIndex, kInvalidIndex}; |
| 65 |
|
| 66 |
// The value of fWeight[i] is weight for the fAlternateImageIndex[i]-th alternate image. |
| 67 |
float fWeight[2] = {0.f, 0.f}; |
| 68 |
}; |
| 69 |
Weighting ComputeWeighting(const AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap& hatm, |
| 70 |
float targetedHdrHeadroom); |
| 71 |
|
| 72 |
/** |
| 73 |
* This will populate the metadata with the Reference White Tone Mapping Operator (RWTMO) |
| 74 |
* parameters, based on the value of fBaselineHdrHeadroom. |
| 75 |
*/ |
| 76 |
void PopulateUsingRwtmo(AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap& hatm); |
| 77 |
|
| 78 |
/** |
| 79 |
* If tone mapping is to be performed for `inputColorSpace` with `metadata, then populate `outAgtm` |
| 80 |
* and `outScaleFactor` with the parameters to use for tone mapping with MakeColorFilter. If tone |
| 81 |
* mapping should not be performed, return false. |
| 82 |
*/ |
| 83 |
bool PopulateToneMapAgtmParams(const Metadata& metadata, |
| 84 |
const SkColorSpace* inputColorSpace, |
| 85 |
AdaptiveGlobalToneMap* outAgtm, |
| 86 |
float* outScaleFactor); |
| 87 |
|
| 88 |
/** |
| 89 |
* Apply the tone mapping to `colors` in the gain application color space, targeting the |
| 90 |
* specified `targetedHdrHeadroom`. |
| 91 |
*/ |
| 92 |
void ApplyGain( |
| 93 |
const AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap& hatm, |
| 94 |
SkSpan<SkColor4f> colors, |
| 95 |
float targetedHdrHeadroom); |
| 96 |
|
| 97 |
/** |
| 98 |
* Return an SkColorFilter that will first scale by `scaleFactor`, and then tone map to |
| 99 |
* the specified `targetedHdrHeadroom`. |
| 100 |
*/ |
| 101 |
sk_sp<SkColorFilter> MakeColorFilter( |
| 102 |
const AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap& hatm, |
| 103 |
float targetedHdrHeadroom, |
| 104 |
float scaleFactor); |
| 105 |
|
| 106 |
/** |
| 107 |
* Return an SkImage containing the control point values for use by the color filter. |
| 108 |
*/ |
| 109 |
sk_sp<SkImage> MakeGainCurveXYMImage( |
| 110 |
const AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap& hatm); |
| 111 |
|
| 112 |
/** |
| 113 |
* Return the gain application color space. |
| 114 |
*/ |
| 115 |
sk_sp<SkColorSpace> GetGainApplicationSpace( |
| 116 |
const AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap& hatm); |
| 117 |
|
| 118 |
/** |
| 119 |
* Return true if `hatm` or `agtm` satisfies all normative constraints. |
| 120 |
*/ |
| 121 |
bool Validate(const AdaptiveGlobalToneMap& agtm); |
| 122 |
bool Validate(const AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap& hatm); |
| 123 |
|
| 124 |
} // namespace AgtmHelpers |
| 125 |
|
| 126 |
} // namespace skhdr |
| 127 |
|
| 128 |
#endif |