| 1 |
/* |
| 2 |
* Copyright 2024 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 |
#include "include/private/SkGainmapInfo.h" |
| 9 |
|
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkData.h" |
| 12 |
#include "include/core/SkRefCnt.h" |
| 13 |
#include "include/core/SkStream.h" |
| 14 |
#include "src/base/SkEndian.h" |
| 15 |
#include "src/codec/SkCodecPriv.h" |
| 16 |
#include "src/core/SkStreamPriv.h" |
| 17 |
|
| 18 |
#include <cmath> |
| 19 |
#include <cstdint> |
| 20 |
#include <memory> |
| 21 |
|
| 22 |
namespace { |
| 23 |
constexpr uint8_t kIsMultiChannelMask = (1u << 7); |
| 24 |
constexpr uint8_t kUseBaseColourSpaceMask = (1u << 6); |
| 25 |
} // namespace |
| 26 |
|
| 27 |
static void write_rational_be(SkDynamicMemoryWStream& s, float x) { |
| 28 |
// TODO(b/338342146): Select denominator to get maximum precision and robustness. |
| 29 |
uint32_t denominator = 0x10000000; |
| 30 |
if (std::abs(x) > 1.f) { |
| 31 |
denominator = 0x1000; |
| 32 |
} |
| 33 |
int32_t numerator = static_cast<int32_t>(std::llround(static_cast<double>(x) * denominator)); |
| 34 |
SkStreamPriv::WriteS32BE(&s, numerator); |
| 35 |
SkStreamPriv::WriteU32BE(&s, denominator); |
| 36 |
} |
| 37 |
|
| 38 |
static void write_positive_rational_be(SkDynamicMemoryWStream& s, float x) { |
| 39 |
// TODO(b/338342146): Select denominator to get maximum precision and robustness. |
| 40 |
uint32_t denominator = 0x10000000; |
| 41 |
if (x > 1.f) { |
| 42 |
denominator = 0x1000; |
| 43 |
} |
| 44 |
uint32_t numerator = static_cast<uint32_t>(std::llround(static_cast<double>(x) * denominator)); |
| 45 |
SkStreamPriv::WriteU32BE(&s, numerator); |
| 46 |
SkStreamPriv::WriteU32BE(&s, denominator); |
| 47 |
} |
| 48 |
|
| 49 |
static bool read_rational_be(SkStream* s, float* value) { |
| 50 |
int32_t numerator = 0; |
| 51 |
uint32_t denominator = 0; |
| 52 |
if (!SkStreamPriv::ReadS32BE(s, &numerator)) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
if (!SkStreamPriv::ReadU32BE(s, &denominator)) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
*value = static_cast<float>(static_cast<double>(numerator) / static_cast<double>(denominator)); |
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
static bool read_positive_rational_be(SkStream* s, float* value) { |
| 63 |
uint32_t numerator = 0; |
| 64 |
uint32_t denominator = 0; |
| 65 |
if (!SkStreamPriv::ReadU32BE(s, &numerator)) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
if (!SkStreamPriv::ReadU32BE(s, &denominator)) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
*value = static_cast<float>(static_cast<double>(numerator) / static_cast<double>(denominator)); |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
static bool read_iso_gainmap_version(SkStream* s) { |
| 76 |
// Ensure minimum version is 0. |
| 77 |
uint16_t minimum_version = 0; |
| 78 |
if (!SkStreamPriv::ReadU16BE(s, &minimum_version)) { |
| 79 |
SkCodecPrintf("Failed to read ISO 21496-1 minimum version.\n"); |
| 80 |
return false; |
| 81 |
} |
| 82 |
if (minimum_version != 0) { |
| 83 |
SkCodecPrintf("Unsupported ISO 21496-1 minimum version.\n"); |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
// Ensure writer version is present. No value is invalid. |
| 88 |
uint16_t writer_version = 0; |
| 89 |
if (!SkStreamPriv::ReadU16BE(s, &writer_version)) { |
| 90 |
SkCodecPrintf("Failed to read ISO 21496-1 version.\n"); |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
static bool read_iso_gainmap_info(SkStream* s, SkGainmapInfo& info) { |
| 98 |
if (!read_iso_gainmap_version(s)) { |
| 99 |
SkCodecPrintf("Failed to read ISO 21496-1 version.\n"); |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
uint8_t flags = 0; |
| 104 |
if (!s->readU8(&flags)) { |
| 105 |
SkCodecPrintf("Failed to read ISO 21496-1 flags.\n"); |
| 106 |
return false; |
| 107 |
} |
| 108 |
bool isMultiChannel = (flags & kIsMultiChannelMask) != 0; |
| 109 |
bool useBaseColourSpace = (flags & kUseBaseColourSpaceMask) != 0; |
| 110 |
|
| 111 |
float baseHdrHeadroom = 0.f; |
| 112 |
if (!read_positive_rational_be(s, &baseHdrHeadroom)) { |
| 113 |
SkCodecPrintf("Failed to read ISO 21496-1 base HDR headroom.\n"); |
| 114 |
return false; |
| 115 |
} |
| 116 |
float altrHdrHeadroom = 0.f; |
| 117 |
if (!read_positive_rational_be(s, &altrHdrHeadroom)) { |
| 118 |
SkCodecPrintf("Failed to read ISO 21496-1 altr HDR headroom.\n"); |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
float gainMapMin[3] = {0.f}; |
| 123 |
float gainMapMax[3] = {0.f}; |
| 124 |
float gamma[3] = {0.f}; |
| 125 |
float baseOffset[3] = {0.f}; |
| 126 |
float altrOffset[3] = {0.f}; |
| 127 |
|
| 128 |
int channelCount = isMultiChannel ? 3 : 1; |
| 129 |
for (int i = 0; i < channelCount; ++i) { |
| 130 |
if (!read_rational_be(s, gainMapMin + i)) { |
| 131 |
SkCodecPrintf("Failed to read ISO 21496-1 gainmap minimum.\n"); |
| 132 |
return false; |
| 133 |
} |
| 134 |
if (!read_rational_be(s, gainMapMax + i)) { |
| 135 |
SkCodecPrintf("Failed to read ISO 21496-1 gainmap maximum.\n"); |
| 136 |
return false; |
| 137 |
} |
| 138 |
if (!read_positive_rational_be(s, gamma + i)) { |
| 139 |
SkCodecPrintf("Failed to read ISO 21496-1 gamma.\n"); |
| 140 |
return false; |
| 141 |
} |
| 142 |
if (!read_rational_be(s, baseOffset + i)) { |
| 143 |
SkCodecPrintf("Failed to read ISO 21496-1 base offset.\n"); |
| 144 |
return false; |
| 145 |
} |
| 146 |
if (!read_rational_be(s, altrOffset + i)) { |
| 147 |
SkCodecPrintf("Failed to read ISO 21496-1 altr offset.\n"); |
| 148 |
return false; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
info = SkGainmapInfo(); |
| 153 |
if (!useBaseColourSpace) { |
| 154 |
info.fGainmapMathColorSpace = SkColorSpace::MakeSRGB(); |
| 155 |
} |
| 156 |
if (baseHdrHeadroom < altrHdrHeadroom) { |
| 157 |
info.fBaseImageType = SkGainmapInfo::BaseImageType::kSDR; |
| 158 |
info.fDisplayRatioSdr = std::exp2(baseHdrHeadroom); |
| 159 |
info.fDisplayRatioHdr = std::exp2(altrHdrHeadroom); |
| 160 |
} else { |
| 161 |
info.fBaseImageType = SkGainmapInfo::BaseImageType::kHDR; |
| 162 |
info.fDisplayRatioHdr = std::exp2(baseHdrHeadroom); |
| 163 |
info.fDisplayRatioSdr = std::exp2(altrHdrHeadroom); |
| 164 |
} |
| 165 |
for (int i = 0; i < 3; ++i) { |
| 166 |
int j = i >= channelCount ? 0 : i; |
| 167 |
info.fGainmapRatioMin[i] = std::exp2(gainMapMin[j]); |
| 168 |
info.fGainmapRatioMax[i] = std::exp2(gainMapMax[j]); |
| 169 |
info.fGainmapGamma[i] = 1.f / gamma[j]; |
| 170 |
switch (info.fBaseImageType) { |
| 171 |
case SkGainmapInfo::BaseImageType::kSDR: |
| 172 |
info.fEpsilonSdr[i] = baseOffset[j]; |
| 173 |
info.fEpsilonHdr[i] = altrOffset[j]; |
| 174 |
break; |
| 175 |
case SkGainmapInfo::BaseImageType::kHDR: |
| 176 |
info.fEpsilonHdr[i] = baseOffset[j]; |
| 177 |
info.fEpsilonSdr[i] = altrOffset[j]; |
| 178 |
break; |
| 179 |
} |
| 180 |
} |
| 181 |
return true; |
| 182 |
} |
| 183 |
|
| 184 |
bool SkGainmapInfo::isUltraHDRv1Compatible() const { |
| 185 |
// UltraHDR v1 supports having the base image be HDR in theory, but it is largely |
| 186 |
// untested. |
| 187 |
if (fBaseImageType == BaseImageType::kHDR) { |
| 188 |
return false; |
| 189 |
} |
| 190 |
// UltraHDR v1 doesn't support a non-base gainmap math color space. |
| 191 |
if (fGainmapMathColorSpace) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
return true; |
| 195 |
} |
| 196 |
|
| 197 |
bool SkGainmapInfo::ParseVersion(const SkData* data) { |
| 198 |
if (!data) { |
| 199 |
return false; |
| 200 |
} |
| 201 |
auto s = SkMemoryStream::MakeDirect(data->data(), data->size()); |
| 202 |
return read_iso_gainmap_version(s.get()); |
| 203 |
} |
| 204 |
|
| 205 |
bool SkGainmapInfo::Parse(const SkData* data, SkGainmapInfo& info) { |
| 206 |
if (!data) { |
| 207 |
return false; |
| 208 |
} |
| 209 |
auto s = SkMemoryStream::MakeDirect(data->data(), data->size()); |
| 210 |
return read_iso_gainmap_info(s.get(), info); |
| 211 |
} |
| 212 |
|
| 213 |
sk_sp<SkData> SkGainmapInfo::SerializeVersion() { |
| 214 |
SkDynamicMemoryWStream s; |
| 215 |
SkStreamPriv::WriteU16BE(&s, 0); // Minimum reader version |
| 216 |
SkStreamPriv::WriteU16BE(&s, 0); // Writer version |
| 217 |
return s.detachAsData(); |
| 218 |
} |
| 219 |
|
| 220 |
static bool is_single_channel(SkColor4f c) { return c.fR == c.fG && c.fG == c.fB; } |
| 221 |
|
| 222 |
sk_sp<SkData> SkGainmapInfo::serialize() const { |
| 223 |
SkDynamicMemoryWStream s; |
| 224 |
// Version. |
| 225 |
SkStreamPriv::WriteU16BE(&s, 0); // Minimum reader version |
| 226 |
SkStreamPriv::WriteU16BE(&s, 0); // Writer version |
| 227 |
|
| 228 |
// Flags. |
| 229 |
bool all_single_channel = is_single_channel(fGainmapRatioMin) && |
| 230 |
is_single_channel(fGainmapRatioMax) && |
| 231 |
is_single_channel(fGainmapGamma) && is_single_channel(fEpsilonSdr) && |
| 232 |
is_single_channel(fEpsilonHdr); |
| 233 |
uint8_t flags = 0; |
| 234 |
if (!fGainmapMathColorSpace) { |
| 235 |
flags |= kUseBaseColourSpaceMask; |
| 236 |
} |
| 237 |
if (!all_single_channel) { |
| 238 |
flags |= kIsMultiChannelMask; |
| 239 |
} |
| 240 |
s.write8(flags); |
| 241 |
|
| 242 |
// Base and altr headroom. |
| 243 |
switch (fBaseImageType) { |
| 244 |
case SkGainmapInfo::BaseImageType::kSDR: |
| 245 |
write_positive_rational_be(s, std::log2(fDisplayRatioSdr)); |
| 246 |
write_positive_rational_be(s, std::log2(fDisplayRatioHdr)); |
| 247 |
break; |
| 248 |
case SkGainmapInfo::BaseImageType::kHDR: |
| 249 |
write_positive_rational_be(s, std::log2(fDisplayRatioHdr)); |
| 250 |
write_positive_rational_be(s, std::log2(fDisplayRatioSdr)); |
| 251 |
break; |
| 252 |
} |
| 253 |
|
| 254 |
// Per-channel information. |
| 255 |
for (int i = 0; i < (all_single_channel ? 1 : 3); ++i) { |
| 256 |
write_rational_be(s, std::log2(fGainmapRatioMin[i])); |
| 257 |
write_rational_be(s, std::log2(fGainmapRatioMax[i])); |
| 258 |
write_positive_rational_be(s, 1.f / fGainmapGamma[i]); |
| 259 |
switch (fBaseImageType) { |
| 260 |
case SkGainmapInfo::BaseImageType::kSDR: |
| 261 |
write_rational_be(s, fEpsilonSdr[i]); |
| 262 |
write_rational_be(s, fEpsilonHdr[i]); |
| 263 |
break; |
| 264 |
case SkGainmapInfo::BaseImageType::kHDR: |
| 265 |
write_rational_be(s, fEpsilonHdr[i]); |
| 266 |
write_rational_be(s, fEpsilonSdr[i]); |
| 267 |
break; |
| 268 |
} |
| 269 |
} |
| 270 |
return s.detachAsData(); |
| 271 |
} |