| 1 |
/* |
| 2 |
* Copyright 2025 Google Inc. |
| 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 |
#include "include/core/SkColorFilter.h" |
| 9 |
#include "include/core/SkStream.h" |
| 10 |
#include "include/core/SkString.h" |
| 11 |
#include "include/private/SkHdrMetadata.h" |
| 12 |
#include "src/codec/SkHdrAgtmPriv.h" |
| 13 |
#include "src/core/SkStreamPriv.h" |
| 14 |
|
| 15 |
namespace skhdr { |
| 16 |
|
| 17 |
SkString ContentLightLevelInformation::toString() const { |
| 18 |
return SkStringPrintf("{maxCLL:%f, maxFALL:%f}", fMaxCLL, fMaxFALL); |
| 19 |
} |
| 20 |
|
| 21 |
bool ContentLightLevelInformation::parse(const SkData* data) { |
| 22 |
if (data->size() != 4) { |
| 23 |
return false; |
| 24 |
} |
| 25 |
SkMemoryStream s(data->data(), data->size()); |
| 26 |
|
| 27 |
uint16_t max_cll = 0; |
| 28 |
uint16_t max_fall = 0; |
| 29 |
if (!SkStreamPriv::ReadU16BE(&s, &max_cll)) { |
| 30 |
return false; |
| 31 |
} |
| 32 |
if (!SkStreamPriv::ReadU16BE(&s, &max_fall)) { |
| 33 |
return false; |
| 34 |
} |
| 35 |
|
| 36 |
fMaxCLL = max_cll; |
| 37 |
fMaxFALL = max_fall; |
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
sk_sp<SkData> ContentLightLevelInformation::serialize() const { |
| 42 |
SkDynamicMemoryWStream s; |
| 43 |
SkStreamPriv::WriteU16BE(&s, std::llroundf(fMaxCLL)); |
| 44 |
SkStreamPriv::WriteU16BE(&s, std::llroundf(fMaxFALL)); |
| 45 |
return s.detachAsData(); |
| 46 |
} |
| 47 |
|
| 48 |
// The PNG CLLI metadata stores luminance as an integer equal to the floating point value, multipled |
| 49 |
// by clli_png_luminance_divisor. |
| 50 |
static constexpr float clli_png_luminance_divisor = 10000.f; |
| 51 |
|
| 52 |
bool ContentLightLevelInformation::parsePngChunk(const SkData* data) { |
| 53 |
if (data->size() != 8) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
SkMemoryStream s(data->data(), data->size()); |
| 57 |
|
| 58 |
uint32_t max_cll_times_10000 = 0; |
| 59 |
uint32_t max_fall_times_10000 = 0; |
| 60 |
if (!SkStreamPriv::ReadU32BE(&s, &max_cll_times_10000)) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
if (!SkStreamPriv::ReadU32BE(&s, &max_fall_times_10000)) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
fMaxCLL = max_cll_times_10000 / clli_png_luminance_divisor; |
| 68 |
fMaxFALL = max_fall_times_10000 / clli_png_luminance_divisor; |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
sk_sp<SkData> ContentLightLevelInformation::serializePngChunk() const { |
| 73 |
SkDynamicMemoryWStream s; |
| 74 |
SkStreamPriv::WriteU32BE(&s, std::llroundf(fMaxCLL * clli_png_luminance_divisor)); |
| 75 |
SkStreamPriv::WriteU32BE(&s, std::llroundf(fMaxFALL * clli_png_luminance_divisor)); |
| 76 |
return s.detachAsData(); |
| 77 |
} |
| 78 |
|
| 79 |
bool ContentLightLevelInformation::operator==(const ContentLightLevelInformation& other) const { |
| 80 |
return fMaxCLL == other.fMaxCLL && |
| 81 |
fMaxFALL == other.fMaxFALL; |
| 82 |
} |
| 83 |
|
| 84 |
// The MDCV metadata stores chrominance [luminance] as an integer equal to the floating point value, |
| 85 |
// multipled by mdcv_chrominance[luminance]_divisor. |
| 86 |
static constexpr float mdcv_chrominance_divisor = 50000.f; |
| 87 |
static constexpr float mdcv_luminance_divisor = 10000.f; |
| 88 |
|
| 89 |
SkString MasteringDisplayColorVolume::toString() const { |
| 90 |
return SkStringPrintf( |
| 91 |
"{red:[%1.8f,%1.8f], green:[%1.8f,%1.8f], blue:[%1.8f,%1.8f], white:[%1.8f,%1.8f], maxLum:%f, minLum:%f}", |
| 92 |
fDisplayPrimaries.fRX, fDisplayPrimaries.fRY, |
| 93 |
fDisplayPrimaries.fGX, fDisplayPrimaries.fGY, |
| 94 |
fDisplayPrimaries.fBX, fDisplayPrimaries.fBY, |
| 95 |
fDisplayPrimaries.fWX, fDisplayPrimaries.fWY, |
| 96 |
fMaximumDisplayMasteringLuminance, |
| 97 |
fMinimumDisplayMasteringLuminance); |
| 98 |
} |
| 99 |
|
| 100 |
bool MasteringDisplayColorVolume::parse(const SkData* data) { |
| 101 |
if (data->size() != 24) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
SkMemoryStream s(data->data(), data->size()); |
| 105 |
|
| 106 |
uint16_t chromaticities_times_50000[8]; |
| 107 |
for (auto& chromaticity_times_50000 : chromaticities_times_50000) { |
| 108 |
if (!SkStreamPriv::ReadU16BE(&s, &chromaticity_times_50000)) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
} |
| 112 |
uint32_t max_luminance_times_10000 = 0; |
| 113 |
uint32_t min_luminance_times_10000 = 0; |
| 114 |
if (!SkStreamPriv::ReadU32BE(&s, &max_luminance_times_10000)) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
if (!SkStreamPriv::ReadU32BE(&s, &min_luminance_times_10000)) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
fDisplayPrimaries = SkColorSpacePrimaries({ |
| 122 |
chromaticities_times_50000[0] / mdcv_chrominance_divisor, |
| 123 |
chromaticities_times_50000[1] / mdcv_chrominance_divisor, |
| 124 |
chromaticities_times_50000[2] / mdcv_chrominance_divisor, |
| 125 |
chromaticities_times_50000[3] / mdcv_chrominance_divisor, |
| 126 |
chromaticities_times_50000[4] / mdcv_chrominance_divisor, |
| 127 |
chromaticities_times_50000[5] / mdcv_chrominance_divisor, |
| 128 |
chromaticities_times_50000[6] / mdcv_chrominance_divisor, |
| 129 |
chromaticities_times_50000[7] / mdcv_chrominance_divisor, |
| 130 |
}); |
| 131 |
fMaximumDisplayMasteringLuminance = max_luminance_times_10000 / mdcv_luminance_divisor; |
| 132 |
fMinimumDisplayMasteringLuminance = min_luminance_times_10000 / mdcv_luminance_divisor; |
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
sk_sp<SkData> MasteringDisplayColorVolume::serialize() const { |
| 137 |
SkDynamicMemoryWStream s; |
| 138 |
SkStreamPriv::WriteU16BE(&s, std::llroundf(fDisplayPrimaries.fRX * mdcv_chrominance_divisor)); |
| 139 |
SkStreamPriv::WriteU16BE(&s, std::llroundf(fDisplayPrimaries.fRY * mdcv_chrominance_divisor)); |
| 140 |
SkStreamPriv::WriteU16BE(&s, std::llroundf(fDisplayPrimaries.fGX * mdcv_chrominance_divisor)); |
| 141 |
SkStreamPriv::WriteU16BE(&s, std::llroundf(fDisplayPrimaries.fGY * mdcv_chrominance_divisor)); |
| 142 |
SkStreamPriv::WriteU16BE(&s, std::llroundf(fDisplayPrimaries.fBX * mdcv_chrominance_divisor)); |
| 143 |
SkStreamPriv::WriteU16BE(&s, std::llroundf(fDisplayPrimaries.fBY * mdcv_chrominance_divisor)); |
| 144 |
SkStreamPriv::WriteU16BE(&s, std::llroundf(fDisplayPrimaries.fWX * mdcv_chrominance_divisor)); |
| 145 |
SkStreamPriv::WriteU16BE(&s, std::llroundf(fDisplayPrimaries.fWY * mdcv_chrominance_divisor)); |
| 146 |
SkStreamPriv::WriteU32BE( |
| 147 |
&s, std::llroundf(fMaximumDisplayMasteringLuminance * mdcv_luminance_divisor)); |
| 148 |
SkStreamPriv::WriteU32BE( |
| 149 |
&s, std::llroundf(fMinimumDisplayMasteringLuminance * mdcv_luminance_divisor)); |
| 150 |
return s.detachAsData(); |
| 151 |
} |
| 152 |
|
| 153 |
bool MasteringDisplayColorVolume::operator==(const MasteringDisplayColorVolume& other) const { |
| 154 |
return fDisplayPrimaries.fRX == other.fDisplayPrimaries.fRX && |
| 155 |
fDisplayPrimaries.fRY == other.fDisplayPrimaries.fRY && |
| 156 |
fDisplayPrimaries.fGX == other.fDisplayPrimaries.fGX && |
| 157 |
fDisplayPrimaries.fGY == other.fDisplayPrimaries.fGY && |
| 158 |
fDisplayPrimaries.fBX == other.fDisplayPrimaries.fBX && |
| 159 |
fDisplayPrimaries.fBY == other.fDisplayPrimaries.fBY && |
| 160 |
fDisplayPrimaries.fWX == other.fDisplayPrimaries.fWX && |
| 161 |
fDisplayPrimaries.fWY == other.fDisplayPrimaries.fWY && |
| 162 |
fMaximumDisplayMasteringLuminance == other.fMaximumDisplayMasteringLuminance && |
| 163 |
fMinimumDisplayMasteringLuminance == other.fMinimumDisplayMasteringLuminance; |
| 164 |
} |
| 165 |
|
| 166 |
Metadata Metadata::MakeEmpty() { |
| 167 |
return Metadata(); |
| 168 |
} |
| 169 |
|
| 170 |
bool Metadata::getContentLightLevelInformation(ContentLightLevelInformation* clli) const { |
| 171 |
if (!fContentLightLevelInformation.has_value()) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
if (clli) { |
| 175 |
*clli = fContentLightLevelInformation.value(); |
| 176 |
} |
| 177 |
return true; |
| 178 |
} |
| 179 |
|
| 180 |
bool Metadata::getMasteringDisplayColorVolume(MasteringDisplayColorVolume* mdcv) const { |
| 181 |
if (!fMasteringDisplayColorVolume.has_value()) { |
| 182 |
return false; |
| 183 |
} |
| 184 |
if (mdcv) { |
| 185 |
*mdcv = fMasteringDisplayColorVolume.value(); |
| 186 |
} |
| 187 |
return true; |
| 188 |
} |
| 189 |
|
| 190 |
bool Metadata::getAdaptiveGlobalToneMap(AdaptiveGlobalToneMap* agtm) const { |
| 191 |
if (!fAdaptiveGlobalToneMap.has_value()) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
if (agtm) { |
| 195 |
*agtm = fAdaptiveGlobalToneMap.value(); |
| 196 |
} |
| 197 |
return true; |
| 198 |
} |
| 199 |
|
| 200 |
sk_sp<const SkData> Metadata::getSerializedAgtm() const { |
| 201 |
return fAdaptiveGlobalToneMap.has_value() ? |
| 202 |
fAdaptiveGlobalToneMap->serialize() : nullptr; |
| 203 |
} |
| 204 |
|
| 205 |
void Metadata::setMasteringDisplayColorVolume(const MasteringDisplayColorVolume& mdcv) { |
| 206 |
fMasteringDisplayColorVolume = mdcv; |
| 207 |
} |
| 208 |
|
| 209 |
void Metadata::setContentLightLevelInformation(const ContentLightLevelInformation& clli) { |
| 210 |
fContentLightLevelInformation = clli; |
| 211 |
} |
| 212 |
|
| 213 |
void Metadata::setAdaptiveGlobalToneMap(const AdaptiveGlobalToneMap& agtm) { |
| 214 |
fAdaptiveGlobalToneMap = agtm; |
| 215 |
} |
| 216 |
|
| 217 |
void Metadata::setSerializedAgtm(sk_sp<const SkData> agtm_encoded) { |
| 218 |
AdaptiveGlobalToneMap agtm; |
| 219 |
if (agtm.parse(agtm_encoded.get())) { |
| 220 |
fAdaptiveGlobalToneMap = agtm; |
| 221 |
} else { |
| 222 |
fAdaptiveGlobalToneMap.reset(); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
SkString Metadata::toString() const { |
| 227 |
return SkStringPrintf("{clli:%s, mdcv:%s, agtm:%s}", |
| 228 |
fContentLightLevelInformation.has_value() ? |
| 229 |
fContentLightLevelInformation->toString().c_str() : "None", |
| 230 |
fMasteringDisplayColorVolume.has_value() ? |
| 231 |
fMasteringDisplayColorVolume->toString().c_str() : "None", |
| 232 |
fAdaptiveGlobalToneMap.has_value() ? |
| 233 |
fAdaptiveGlobalToneMap->toString().c_str() : "None"); |
| 234 |
} |
| 235 |
|
| 236 |
bool Metadata::operator==(const Metadata& other) const { |
| 237 |
return fContentLightLevelInformation == other.fContentLightLevelInformation && |
| 238 |
fMasteringDisplayColorVolume == other.fMasteringDisplayColorVolume && |
| 239 |
fAdaptiveGlobalToneMap == other.fAdaptiveGlobalToneMap; |
| 240 |
} |
| 241 |
|
| 242 |
sk_sp<SkColorFilter> Metadata::makeToneMapColorFilter( |
| 243 |
float targetedHdrHeadroom, const SkColorSpace* inputColorSpace) const { |
| 244 |
AdaptiveGlobalToneMap agtm; |
| 245 |
float scaleFactor = 1.f; |
| 246 |
if (!AgtmHelpers::PopulateToneMapAgtmParams(*this, inputColorSpace, &agtm, &scaleFactor)) { |
| 247 |
return nullptr; |
| 248 |
} |
| 249 |
|
| 250 |
auto& hatm = agtm.fHeadroomAdaptiveToneMap; |
| 251 |
if (!hatm.has_value()) { |
| 252 |
// TODO(https://crbug.com/395659818): Add default tone mapping. |
| 253 |
return nullptr; |
| 254 |
} |
| 255 |
return AgtmHelpers::MakeColorFilter(hatm.value(), targetedHdrHeadroom, scaleFactor); |
| 256 |
} |
| 257 |
|
| 258 |
} // namespace skhdr |
| 259 |
|