blob: ee15363b69daa8a22f2fb4e86174321b3ce94512 [file] [log] [blame]
Nick Deakinf6bca5a2022-11-04 10:43:43 -04001/*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <cmath>
Harish Mahendrakar555a06b2022-12-14 09:37:27 -080018#include <vector>
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000019#include <ultrahdr/gainmapmath.h>
Nick Deakinf6bca5a2022-11-04 10:43:43 -040020
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000021namespace android::ultrahdr {
Nick Deakinf6bca5a2022-11-04 10:43:43 -040022
Harish Mahendrakar555a06b2022-12-14 09:37:27 -080023static const std::vector<float> kPqOETF = [] {
24 std::vector<float> result;
Nick Deakind19e5762023-02-10 15:39:08 -050025 for (int idx = 0; idx < kPqOETFNumEntries; idx++) {
26 float value = static_cast<float>(idx) / static_cast<float>(kPqOETFNumEntries - 1);
Harish Mahendrakar555a06b2022-12-14 09:37:27 -080027 result.push_back(pqOetf(value));
28 }
29 return result;
30}();
31
Harish Mahendrakar555a06b2022-12-14 09:37:27 -080032static const std::vector<float> kPqInvOETF = [] {
33 std::vector<float> result;
Nick Deakind19e5762023-02-10 15:39:08 -050034 for (int idx = 0; idx < kPqInvOETFNumEntries; idx++) {
35 float value = static_cast<float>(idx) / static_cast<float>(kPqInvOETFNumEntries - 1);
Harish Mahendrakar555a06b2022-12-14 09:37:27 -080036 result.push_back(pqInvOetf(value));
37 }
38 return result;
39}();
40
Harish Mahendrakar555a06b2022-12-14 09:37:27 -080041static const std::vector<float> kHlgOETF = [] {
42 std::vector<float> result;
Nick Deakind19e5762023-02-10 15:39:08 -050043 for (int idx = 0; idx < kHlgOETFNumEntries; idx++) {
44 float value = static_cast<float>(idx) / static_cast<float>(kHlgOETFNumEntries - 1);
Harish Mahendrakar555a06b2022-12-14 09:37:27 -080045 result.push_back(hlgOetf(value));
46 }
47 return result;
48}();
49
Harish Mahendrakar555a06b2022-12-14 09:37:27 -080050static const std::vector<float> kHlgInvOETF = [] {
51 std::vector<float> result;
Nick Deakind19e5762023-02-10 15:39:08 -050052 for (int idx = 0; idx < kHlgInvOETFNumEntries; idx++) {
53 float value = static_cast<float>(idx) / static_cast<float>(kHlgInvOETFNumEntries - 1);
Harish Mahendrakar555a06b2022-12-14 09:37:27 -080054 result.push_back(hlgInvOetf(value));
55 }
56 return result;
57}();
58
Nick Deakind19e5762023-02-10 15:39:08 -050059static const std::vector<float> kSrgbInvOETF = [] {
Harish Mahendrakar555a06b2022-12-14 09:37:27 -080060 std::vector<float> result;
Nick Deakind19e5762023-02-10 15:39:08 -050061 for (int idx = 0; idx < kSrgbInvOETFNumEntries; idx++) {
62 float value = static_cast<float>(idx) / static_cast<float>(kSrgbInvOETFNumEntries - 1);
Harish Mahendrakar555a06b2022-12-14 09:37:27 -080063 result.push_back(srgbInvOetf(value));
64 }
65 return result;
66}();
Ram Mohanfe723d62022-12-15 00:59:11 +053067
68// Use Shepard's method for inverse distance weighting. For more information:
69// en.wikipedia.org/wiki/Inverse_distance_weighting#Shepard's_method
70
71float ShepardsIDW::euclideanDistance(float x1, float x2, float y1, float y2) {
72 return sqrt(((y2 - y1) * (y2 - y1)) + (x2 - x1) * (x2 - x1));
73}
74
75void ShepardsIDW::fillShepardsIDW(float *weights, int incR, int incB) {
76 for (int y = 0; y < mMapScaleFactor; y++) {
77 for (int x = 0; x < mMapScaleFactor; x++) {
78 float pos_x = ((float)x) / mMapScaleFactor;
79 float pos_y = ((float)y) / mMapScaleFactor;
80 int curr_x = floor(pos_x);
81 int curr_y = floor(pos_y);
82 int next_x = curr_x + incR;
83 int next_y = curr_y + incB;
84 float e1_distance = euclideanDistance(pos_x, curr_x, pos_y, curr_y);
85 int index = y * mMapScaleFactor * 4 + x * 4;
86 if (e1_distance == 0) {
87 weights[index++] = 1.f;
88 weights[index++] = 0.f;
89 weights[index++] = 0.f;
90 weights[index++] = 0.f;
91 } else {
92 float e1_weight = 1.f / e1_distance;
93
94 float e2_distance = euclideanDistance(pos_x, curr_x, pos_y, next_y);
95 float e2_weight = 1.f / e2_distance;
96
97 float e3_distance = euclideanDistance(pos_x, next_x, pos_y, curr_y);
98 float e3_weight = 1.f / e3_distance;
99
100 float e4_distance = euclideanDistance(pos_x, next_x, pos_y, next_y);
101 float e4_weight = 1.f / e4_distance;
102
103 float total_weight = e1_weight + e2_weight + e3_weight + e4_weight;
104
105 weights[index++] = e1_weight / total_weight;
106 weights[index++] = e2_weight / total_weight;
107 weights[index++] = e3_weight / total_weight;
108 weights[index++] = e4_weight / total_weight;
109 }
110 }
111 }
112}
113
Nick Deakin594a4ca2022-11-16 20:57:42 -0500114////////////////////////////////////////////////////////////////////////////////
115// sRGB transformations
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400116
Harish Mahendrakar1107ff32022-12-07 17:24:35 -0800117static const float kMaxPixelFloat = 1.0f;
118static float clampPixelFloat(float value) {
119 return (value < 0.0f) ? 0.0f : (value > kMaxPixelFloat) ? kMaxPixelFloat : value;
120}
121
Nick Deakin0db53ee2023-05-19 17:14:45 -0400122// See IEC 61966-2-1/Amd 1:2003, Equation F.7.
Nick Deakin65f492a2022-11-29 22:47:40 -0500123static const float kSrgbR = 0.2126f, kSrgbG = 0.7152f, kSrgbB = 0.0722f;
Nick Deakin594a4ca2022-11-16 20:57:42 -0500124
125float srgbLuminance(Color e) {
126 return kSrgbR * e.r + kSrgbG * e.g + kSrgbB * e.b;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400127}
128
Nick Deakin0db53ee2023-05-19 17:14:45 -0400129// See ITU-R BT.709-6, Section 3.
130// Uses the same coefficients for deriving luma signal as
131// IEC 61966-2-1/Amd 1:2003 states for luminance, so we reuse the luminance
132// function above.
133static const float kSrgbCb = 1.8556f, kSrgbCr = 1.5748f;
Nick Deakin594a4ca2022-11-16 20:57:42 -0500134
135Color srgbRgbToYuv(Color e_gamma) {
Nick Deakin0db53ee2023-05-19 17:14:45 -0400136 float y_gamma = srgbLuminance(e_gamma);
137 return {{{ y_gamma,
138 (e_gamma.b - y_gamma) / kSrgbCb,
139 (e_gamma.r - y_gamma) / kSrgbCr }}};
Nick Deakin594a4ca2022-11-16 20:57:42 -0500140}
141
Nick Deakin0db53ee2023-05-19 17:14:45 -0400142// See ITU-R BT.709-6, Section 3.
143// Same derivation to BT.2100's YUV->RGB, below. Similar to srgbRgbToYuv, we
144// can reuse the luminance coefficients since they are the same.
145static const float kSrgbGCb = kSrgbB * kSrgbCb / kSrgbG;
146static const float kSrgbGCr = kSrgbR * kSrgbCr / kSrgbG;
147
148Color srgbYuvToRgb(Color e_gamma) {
149 return {{{ clampPixelFloat(e_gamma.y + kSrgbCr * e_gamma.v),
150 clampPixelFloat(e_gamma.y - kSrgbGCb * e_gamma.u - kSrgbGCr * e_gamma.v),
151 clampPixelFloat(e_gamma.y + kSrgbCb * e_gamma.u) }}};
152}
153
154// See IEC 61966-2-1/Amd 1:2003, Equations F.5 and F.6.
Nick Deakin594a4ca2022-11-16 20:57:42 -0500155float srgbInvOetf(float e_gamma) {
156 if (e_gamma <= 0.04045f) {
157 return e_gamma / 12.92f;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400158 } else {
Nick Deakin594a4ca2022-11-16 20:57:42 -0500159 return pow((e_gamma + 0.055f) / 1.055f, 2.4);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400160 }
161}
162
Nick Deakin594a4ca2022-11-16 20:57:42 -0500163Color srgbInvOetf(Color e_gamma) {
164 return {{{ srgbInvOetf(e_gamma.r),
165 srgbInvOetf(e_gamma.g),
166 srgbInvOetf(e_gamma.b) }}};
167}
168
Harish Mahendrakar555a06b2022-12-14 09:37:27 -0800169// See IEC 61966-2-1, Equations F.5 and F.6.
170float srgbInvOetfLUT(float e_gamma) {
Nick Deakind19e5762023-02-10 15:39:08 -0500171 uint32_t value = static_cast<uint32_t>(e_gamma * kSrgbInvOETFNumEntries);
Harish Mahendrakar555a06b2022-12-14 09:37:27 -0800172 //TODO() : Remove once conversion modules have appropriate clamping in place
Nick Deakind19e5762023-02-10 15:39:08 -0500173 value = CLIP3(value, 0, kSrgbInvOETFNumEntries - 1);
174 return kSrgbInvOETF[value];
Harish Mahendrakar555a06b2022-12-14 09:37:27 -0800175}
176
177Color srgbInvOetfLUT(Color e_gamma) {
178 return {{{ srgbInvOetfLUT(e_gamma.r),
179 srgbInvOetfLUT(e_gamma.g),
180 srgbInvOetfLUT(e_gamma.b) }}};
181}
Nick Deakin594a4ca2022-11-16 20:57:42 -0500182
183////////////////////////////////////////////////////////////////////////////////
184// Display-P3 transformations
185
Nick Deakin0db53ee2023-05-19 17:14:45 -0400186// See SMPTE EG 432-1, Equation 7-8.
Nick Deakin65f492a2022-11-29 22:47:40 -0500187static const float kP3R = 0.20949f, kP3G = 0.72160f, kP3B = 0.06891f;
Nick Deakin6bd90432022-11-20 16:26:37 -0500188
189float p3Luminance(Color e) {
190 return kP3R * e.r + kP3G * e.g + kP3B * e.b;
191}
Nick Deakin594a4ca2022-11-16 20:57:42 -0500192
Nick Deakin0db53ee2023-05-19 17:14:45 -0400193// See ITU-R BT.601-7, Sections 2.5.1 and 2.5.2.
194// Unfortunately, calculation of luma signal differs from calculation of
195// luminance for Display-P3, so we can't reuse p3Luminance here.
196static const float kP3YR = 0.299f, kP3YG = 0.587f, kP3YB = 0.114f;
197static const float kP3Cb = 1.772f, kP3Cr = 1.402f;
198
199Color p3RgbToYuv(Color e_gamma) {
200 float y_gamma = kP3YR * e_gamma.r + kP3YG * e_gamma.g + kP3YB * e_gamma.b;
201 return {{{ y_gamma,
202 (e_gamma.b - y_gamma) / kP3Cb,
203 (e_gamma.r - y_gamma) / kP3Cr }}};
204}
205
206// See ITU-R BT.601-7, Sections 2.5.1 and 2.5.2.
207// Same derivation to BT.2100's YUV->RGB, below. Similar to p3RgbToYuv, we must
208// use luma signal coefficients rather than the luminance coefficients.
209static const float kP3GCb = kP3YB * kP3Cb / kP3YG;
210static const float kP3GCr = kP3YR * kP3Cr / kP3YG;
211
212Color p3YuvToRgb(Color e_gamma) {
213 return {{{ clampPixelFloat(e_gamma.y + kP3Cr * e_gamma.v),
214 clampPixelFloat(e_gamma.y - kP3GCb * e_gamma.u - kP3GCr * e_gamma.v),
215 clampPixelFloat(e_gamma.y + kP3Cb * e_gamma.u) }}};
216}
217
Nick Deakin594a4ca2022-11-16 20:57:42 -0500218
219////////////////////////////////////////////////////////////////////////////////
220// BT.2100 transformations - according to ITU-R BT.2100-2
221
Nick Deakin65f492a2022-11-29 22:47:40 -0500222// See ITU-R BT.2100-2, Table 5, HLG Reference OOTF
Nick Deakin594a4ca2022-11-16 20:57:42 -0500223static const float kBt2100R = 0.2627f, kBt2100G = 0.6780f, kBt2100B = 0.0593f;
224
225float bt2100Luminance(Color e) {
226 return kBt2100R * e.r + kBt2100G * e.g + kBt2100B * e.b;
227}
228
Nick Deakin65f492a2022-11-29 22:47:40 -0500229// See ITU-R BT.2100-2, Table 6, Derivation of colour difference signals.
Nick Deakin0db53ee2023-05-19 17:14:45 -0400230// BT.2100 uses the same coefficients for calculating luma signal and luminance,
231// so we reuse the luminance function here.
Nick Deakin594a4ca2022-11-16 20:57:42 -0500232static const float kBt2100Cb = 1.8814f, kBt2100Cr = 1.4746f;
233
234Color bt2100RgbToYuv(Color e_gamma) {
235 float y_gamma = bt2100Luminance(e_gamma);
236 return {{{ y_gamma,
237 (e_gamma.b - y_gamma) / kBt2100Cb,
238 (e_gamma.r - y_gamma) / kBt2100Cr }}};
239}
240
Nick Deakin0db53ee2023-05-19 17:14:45 -0400241// See ITU-R BT.2100-2, Table 6, Derivation of colour difference signals.
242//
243// Similar to bt2100RgbToYuv above, we can reuse the luminance coefficients.
244//
Nick Deakin65f492a2022-11-29 22:47:40 -0500245// Derived by inversing bt2100RgbToYuv. The derivation for R and B are pretty
246// straight forward; we just invert the formulas for U and V above. But deriving
247// the formula for G is a bit more complicated:
Nick Deakin594a4ca2022-11-16 20:57:42 -0500248//
249// Start with equation for luminance:
250// Y = kBt2100R * R + kBt2100G * G + kBt2100B * B
251// Solve for G:
252// G = (Y - kBt2100R * R - kBt2100B * B) / kBt2100B
253// Substitute equations for R and B in terms YUV:
254// G = (Y - kBt2100R * (Y + kBt2100Cr * V) - kBt2100B * (Y + kBt2100Cb * U)) / kBt2100B
255// Simplify:
256// G = Y * ((1 - kBt2100R - kBt2100B) / kBt2100G)
257// + U * (kBt2100B * kBt2100Cb / kBt2100G)
258// + V * (kBt2100R * kBt2100Cr / kBt2100G)
259//
260// We then get the following coeficients for calculating G from YUV:
261//
262// Coef for Y = (1 - kBt2100R - kBt2100B) / kBt2100G = 1
263// Coef for U = kBt2100B * kBt2100Cb / kBt2100G = kBt2100GCb = ~0.1645
264// Coef for V = kBt2100R * kBt2100Cr / kBt2100G = kBt2100GCr = ~0.5713
265
266static const float kBt2100GCb = kBt2100B * kBt2100Cb / kBt2100G;
267static const float kBt2100GCr = kBt2100R * kBt2100Cr / kBt2100G;
268
269Color bt2100YuvToRgb(Color e_gamma) {
Harish Mahendrakar1107ff32022-12-07 17:24:35 -0800270 return {{{ clampPixelFloat(e_gamma.y + kBt2100Cr * e_gamma.v),
271 clampPixelFloat(e_gamma.y - kBt2100GCb * e_gamma.u - kBt2100GCr * e_gamma.v),
272 clampPixelFloat(e_gamma.y + kBt2100Cb * e_gamma.u) }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400273}
274
Nick Deakin65f492a2022-11-29 22:47:40 -0500275// See ITU-R BT.2100-2, Table 5, HLG Reference OETF.
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400276static const float kHlgA = 0.17883277f, kHlgB = 0.28466892f, kHlgC = 0.55991073;
277
Nick Deakin65f492a2022-11-29 22:47:40 -0500278float hlgOetf(float e) {
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400279 if (e <= 1.0f/12.0f) {
280 return sqrt(3.0f * e);
281 } else {
282 return kHlgA * log(12.0f * e - kHlgB) + kHlgC;
283 }
284}
285
286Color hlgOetf(Color e) {
287 return {{{ hlgOetf(e.r), hlgOetf(e.g), hlgOetf(e.b) }}};
288}
289
Harish Mahendrakar555a06b2022-12-14 09:37:27 -0800290float hlgOetfLUT(float e) {
291 uint32_t value = static_cast<uint32_t>(e * kHlgOETFNumEntries);
292 //TODO() : Remove once conversion modules have appropriate clamping in place
293 value = CLIP3(value, 0, kHlgOETFNumEntries - 1);
294
295 return kHlgOETF[value];
296}
297
298Color hlgOetfLUT(Color e) {
299 return {{{ hlgOetfLUT(e.r), hlgOetfLUT(e.g), hlgOetfLUT(e.b) }}};
300}
301
Nick Deakin65f492a2022-11-29 22:47:40 -0500302// See ITU-R BT.2100-2, Table 5, HLG Reference EOTF.
303float hlgInvOetf(float e_gamma) {
Nick Deakin594a4ca2022-11-16 20:57:42 -0500304 if (e_gamma <= 0.5f) {
305 return pow(e_gamma, 2.0f) / 3.0f;
306 } else {
307 return (exp((e_gamma - kHlgC) / kHlgA) + kHlgB) / 12.0f;
308 }
309}
310
311Color hlgInvOetf(Color e_gamma) {
312 return {{{ hlgInvOetf(e_gamma.r),
313 hlgInvOetf(e_gamma.g),
314 hlgInvOetf(e_gamma.b) }}};
315}
316
Harish Mahendrakar555a06b2022-12-14 09:37:27 -0800317float hlgInvOetfLUT(float e_gamma) {
318 uint32_t value = static_cast<uint32_t>(e_gamma * kHlgInvOETFNumEntries);
319 //TODO() : Remove once conversion modules have appropriate clamping in place
320 value = CLIP3(value, 0, kHlgInvOETFNumEntries - 1);
321
322 return kHlgInvOETF[value];
323}
324
325Color hlgInvOetfLUT(Color e_gamma) {
326 return {{{ hlgInvOetfLUT(e_gamma.r),
327 hlgInvOetfLUT(e_gamma.g),
328 hlgInvOetfLUT(e_gamma.b) }}};
329}
330
Nick Deakin65f492a2022-11-29 22:47:40 -0500331// See ITU-R BT.2100-2, Table 4, Reference PQ OETF.
Nick Deakin6bd90432022-11-20 16:26:37 -0500332static const float kPqM1 = 2610.0f / 16384.0f, kPqM2 = 2523.0f / 4096.0f * 128.0f;
333static const float kPqC1 = 3424.0f / 4096.0f, kPqC2 = 2413.0f / 4096.0f * 32.0f,
334 kPqC3 = 2392.0f / 4096.0f * 32.0f;
335
Nick Deakin65f492a2022-11-29 22:47:40 -0500336float pqOetf(float e) {
337 if (e <= 0.0f) return 0.0f;
338 return pow((kPqC1 + kPqC2 * pow(e, kPqM1)) / (1 + kPqC3 * pow(e, kPqM1)),
Nick Deakin6bd90432022-11-20 16:26:37 -0500339 kPqM2);
340}
341
342Color pqOetf(Color e) {
343 return {{{ pqOetf(e.r), pqOetf(e.g), pqOetf(e.b) }}};
344}
345
Harish Mahendrakar555a06b2022-12-14 09:37:27 -0800346float pqOetfLUT(float e) {
347 uint32_t value = static_cast<uint32_t>(e * kPqOETFNumEntries);
348 //TODO() : Remove once conversion modules have appropriate clamping in place
349 value = CLIP3(value, 0, kPqOETFNumEntries - 1);
350
351 return kPqOETF[value];
352}
353
354Color pqOetfLUT(Color e) {
355 return {{{ pqOetfLUT(e.r), pqOetfLUT(e.g), pqOetfLUT(e.b) }}};
356}
357
Nick Deakin65f492a2022-11-29 22:47:40 -0500358// Derived from the inverse of the Reference PQ OETF.
359static const float kPqInvA = 128.0f, kPqInvB = 107.0f, kPqInvC = 2413.0f, kPqInvD = 2392.0f,
360 kPqInvE = 6.2773946361f, kPqInvF = 0.0126833f;
361
362float pqInvOetf(float e_gamma) {
363 // This equation blows up if e_gamma is 0.0, and checking on <= 0.0 doesn't
364 // always catch 0.0. So, check on 0.0001, since anything this small will
365 // effectively be crushed to zero anyways.
366 if (e_gamma <= 0.0001f) return 0.0f;
367 return pow((kPqInvA * pow(e_gamma, kPqInvF) - kPqInvB)
368 / (kPqInvC - kPqInvD * pow(e_gamma, kPqInvF)),
369 kPqInvE);
Nick Deakin6bd90432022-11-20 16:26:37 -0500370}
371
372Color pqInvOetf(Color e_gamma) {
373 return {{{ pqInvOetf(e_gamma.r),
374 pqInvOetf(e_gamma.g),
375 pqInvOetf(e_gamma.b) }}};
376}
377
Harish Mahendrakar555a06b2022-12-14 09:37:27 -0800378float pqInvOetfLUT(float e_gamma) {
379 uint32_t value = static_cast<uint32_t>(e_gamma * kPqInvOETFNumEntries);
380 //TODO() : Remove once conversion modules have appropriate clamping in place
381 value = CLIP3(value, 0, kPqInvOETFNumEntries - 1);
382
383 return kPqInvOETF[value];
384}
385
386Color pqInvOetfLUT(Color e_gamma) {
387 return {{{ pqInvOetfLUT(e_gamma.r),
388 pqInvOetfLUT(e_gamma.g),
389 pqInvOetfLUT(e_gamma.b) }}};
390}
391
Nick Deakin594a4ca2022-11-16 20:57:42 -0500392
393////////////////////////////////////////////////////////////////////////////////
394// Color conversions
395
Nick Deakin6bd90432022-11-20 16:26:37 -0500396Color bt709ToP3(Color e) {
397 return {{{ 0.82254f * e.r + 0.17755f * e.g + 0.00006f * e.b,
398 0.03312f * e.r + 0.96684f * e.g + -0.00001f * e.b,
399 0.01706f * e.r + 0.07240f * e.g + 0.91049f * e.b }}};
400}
401
402Color bt709ToBt2100(Color e) {
403 return {{{ 0.62740f * e.r + 0.32930f * e.g + 0.04332f * e.b,
404 0.06904f * e.r + 0.91958f * e.g + 0.01138f * e.b,
405 0.01636f * e.r + 0.08799f * e.g + 0.89555f * e.b }}};
406}
407
408Color p3ToBt709(Color e) {
409 return {{{ 1.22482f * e.r + -0.22490f * e.g + -0.00007f * e.b,
410 -0.04196f * e.r + 1.04199f * e.g + 0.00001f * e.b,
411 -0.01961f * e.r + -0.07865f * e.g + 1.09831f * e.b }}};
412}
413
414Color p3ToBt2100(Color e) {
415 return {{{ 0.75378f * e.r + 0.19862f * e.g + 0.04754f * e.b,
416 0.04576f * e.r + 0.94177f * e.g + 0.01250f * e.b,
417 -0.00121f * e.r + 0.01757f * e.g + 0.98359f * e.b }}};
418}
419
420Color bt2100ToBt709(Color e) {
421 return {{{ 1.66045f * e.r + -0.58764f * e.g + -0.07286f * e.b,
422 -0.12445f * e.r + 1.13282f * e.g + -0.00837f * e.b,
423 -0.01811f * e.r + -0.10057f * e.g + 1.11878f * e.b }}};
424}
425
426Color bt2100ToP3(Color e) {
427 return {{{ 1.34369f * e.r + -0.28223f * e.g + -0.06135f * e.b,
428 -0.06533f * e.r + 1.07580f * e.g + -0.01051f * e.b,
429 0.00283f * e.r + -0.01957f * e.g + 1.01679f * e.b
430 }}};
431}
432
433// TODO: confirm we always want to convert like this before calculating
434// luminance.
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000435ColorTransformFn getHdrConversionFn(ultrahdr_color_gamut sdr_gamut,
436 ultrahdr_color_gamut hdr_gamut) {
Nick Deakin65f492a2022-11-29 22:47:40 -0500437 switch (sdr_gamut) {
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000438 case ULTRAHDR_COLORGAMUT_BT709:
Nick Deakin6bd90432022-11-20 16:26:37 -0500439 switch (hdr_gamut) {
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000440 case ULTRAHDR_COLORGAMUT_BT709:
Nick Deakin6bd90432022-11-20 16:26:37 -0500441 return identityConversion;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000442 case ULTRAHDR_COLORGAMUT_P3:
Nick Deakin6bd90432022-11-20 16:26:37 -0500443 return p3ToBt709;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000444 case ULTRAHDR_COLORGAMUT_BT2100:
Nick Deakin6bd90432022-11-20 16:26:37 -0500445 return bt2100ToBt709;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000446 case ULTRAHDR_COLORGAMUT_UNSPECIFIED:
Nick Deakin6bd90432022-11-20 16:26:37 -0500447 return nullptr;
448 }
449 break;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000450 case ULTRAHDR_COLORGAMUT_P3:
Nick Deakin6bd90432022-11-20 16:26:37 -0500451 switch (hdr_gamut) {
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000452 case ULTRAHDR_COLORGAMUT_BT709:
Nick Deakin6bd90432022-11-20 16:26:37 -0500453 return bt709ToP3;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000454 case ULTRAHDR_COLORGAMUT_P3:
Nick Deakin6bd90432022-11-20 16:26:37 -0500455 return identityConversion;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000456 case ULTRAHDR_COLORGAMUT_BT2100:
Nick Deakin6bd90432022-11-20 16:26:37 -0500457 return bt2100ToP3;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000458 case ULTRAHDR_COLORGAMUT_UNSPECIFIED:
Nick Deakin6bd90432022-11-20 16:26:37 -0500459 return nullptr;
460 }
461 break;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000462 case ULTRAHDR_COLORGAMUT_BT2100:
Nick Deakin6bd90432022-11-20 16:26:37 -0500463 switch (hdr_gamut) {
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000464 case ULTRAHDR_COLORGAMUT_BT709:
Nick Deakin6bd90432022-11-20 16:26:37 -0500465 return bt709ToBt2100;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000466 case ULTRAHDR_COLORGAMUT_P3:
Nick Deakin6bd90432022-11-20 16:26:37 -0500467 return p3ToBt2100;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000468 case ULTRAHDR_COLORGAMUT_BT2100:
Nick Deakin6bd90432022-11-20 16:26:37 -0500469 return identityConversion;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000470 case ULTRAHDR_COLORGAMUT_UNSPECIFIED:
Nick Deakin6bd90432022-11-20 16:26:37 -0500471 return nullptr;
472 }
473 break;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000474 case ULTRAHDR_COLORGAMUT_UNSPECIFIED:
Nick Deakin6bd90432022-11-20 16:26:37 -0500475 return nullptr;
476 }
477}
478
Nick Deakin0db53ee2023-05-19 17:14:45 -0400479// All of these conversions are derived from the respective input YUV->RGB conversion followed by
480// the RGB->YUV for the receiving encoding. They are consistent with the RGB<->YUV functions in this
481// file, given that we uses BT.709 encoding for sRGB and BT.601 encoding for Display-P3, to match
482// DataSpace.
483
484Color yuv709To601(Color e_gamma) {
485 return {{{ 1.0f * e_gamma.y + 0.101579f * e_gamma.u + 0.196076f * e_gamma.v,
486 0.0f * e_gamma.y + 0.989854f * e_gamma.u + -0.110653f * e_gamma.v,
487 0.0f * e_gamma.y + -0.072453f * e_gamma.u + 0.983398f * e_gamma.v }}};
488}
489
490Color yuv709To2100(Color e_gamma) {
491 return {{{ 1.0f * e_gamma.y + -0.016969f * e_gamma.u + 0.096312f * e_gamma.v,
492 0.0f * e_gamma.y + 0.995306f * e_gamma.u + -0.051192f * e_gamma.v,
493 0.0f * e_gamma.y + 0.011507f * e_gamma.u + 1.002637f * e_gamma.v }}};
494}
495
496Color yuv601To709(Color e_gamma) {
497 return {{{ 1.0f * e_gamma.y + -0.118188f * e_gamma.u + -0.212685f * e_gamma.v,
498 0.0f * e_gamma.y + 1.018640f * e_gamma.u + 0.114618f * e_gamma.v,
499 0.0f * e_gamma.y + 0.075049f * e_gamma.u + 1.025327f * e_gamma.v }}};
500}
501
502Color yuv601To2100(Color e_gamma) {
503 return {{{ 1.0f * e_gamma.y + -0.128245f * e_gamma.u + -0.115879f * e_gamma.v,
504 0.0f * e_gamma.y + 1.010016f * e_gamma.u + 0.061592f * e_gamma.v,
505 0.0f * e_gamma.y + 0.086969f * e_gamma.u + 1.029350f * e_gamma.v }}};
506}
507
508Color yuv2100To709(Color e_gamma) {
509 return {{{ 1.0f * e_gamma.y + 0.018149f * e_gamma.u + -0.095132f * e_gamma.v,
510 0.0f * e_gamma.y + 1.004123f * e_gamma.u + 0.051267f * e_gamma.v,
511 0.0f * e_gamma.y + -0.011524f * e_gamma.u + 0.996782f * e_gamma.v }}};
512}
513
514Color yuv2100To601(Color e_gamma) {
515 return {{{ 1.0f * e_gamma.y + 0.117887f * e_gamma.u + 0.105521f * e_gamma.v,
516 0.0f * e_gamma.y + 0.995211f * e_gamma.u + -0.059549f * e_gamma.v,
517 0.0f * e_gamma.y + -0.084085f * e_gamma.u + 0.976518f * e_gamma.v }}};
518}
519
520void transformYuv420(jr_uncompressed_ptr image, size_t x_chroma, size_t y_chroma,
521 ColorTransformFn fn) {
522 Color yuv1 = getYuv420Pixel(image, x_chroma * 2, y_chroma * 2 );
523 Color yuv2 = getYuv420Pixel(image, x_chroma * 2 + 1, y_chroma * 2 );
524 Color yuv3 = getYuv420Pixel(image, x_chroma * 2, y_chroma * 2 + 1);
525 Color yuv4 = getYuv420Pixel(image, x_chroma * 2 + 1, y_chroma * 2 + 1);
526
527 yuv1 = fn(yuv1);
528 yuv2 = fn(yuv2);
529 yuv3 = fn(yuv3);
530 yuv4 = fn(yuv4);
531
532 Color new_uv = (yuv1 + yuv2 + yuv3 + yuv4) / 4.0f;
533
534 size_t pixel_y1_idx = x_chroma * 2 + y_chroma * 2 * image->width;
535 size_t pixel_y2_idx = (x_chroma * 2 + 1) + y_chroma * 2 * image->width;
536 size_t pixel_y3_idx = x_chroma * 2 + (y_chroma * 2 + 1) * image->width;
537 size_t pixel_y4_idx = (x_chroma * 2 + 1) + (y_chroma * 2 + 1) * image->width;
538
539 uint8_t& y1_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y1_idx];
540 uint8_t& y2_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y2_idx];
541 uint8_t& y3_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y3_idx];
542 uint8_t& y4_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y4_idx];
543
544 size_t pixel_count = image->width * image->height;
545 size_t pixel_uv_idx = x_chroma + y_chroma * (image->width / 2);
546
547 uint8_t& u_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count + pixel_uv_idx];
548 uint8_t& v_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count * 5 / 4 + pixel_uv_idx];
549
550 y1_uint = static_cast<uint8_t>(floor(yuv1.y * 255.0f + 0.5f));
551 y2_uint = static_cast<uint8_t>(floor(yuv2.y * 255.0f + 0.5f));
552 y3_uint = static_cast<uint8_t>(floor(yuv3.y * 255.0f + 0.5f));
553 y4_uint = static_cast<uint8_t>(floor(yuv4.y * 255.0f + 0.5f));
554
555 u_uint = static_cast<uint8_t>(floor(new_uv.u * 255.0f + 128.0f + 0.5f));
556 v_uint = static_cast<uint8_t>(floor(new_uv.v * 255.0f + 128.0f + 0.5f));
557}
Nick Deakin594a4ca2022-11-16 20:57:42 -0500558
559////////////////////////////////////////////////////////////////////////////////
Dichen Zhang10959a42023-04-10 16:28:16 -0700560// Gain map calculations
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000561uint8_t encodeGain(float y_sdr, float y_hdr, ultrahdr_metadata_ptr metadata) {
Dichen Zhang10959a42023-04-10 16:28:16 -0700562 return encodeGain(y_sdr, y_hdr, metadata,
563 log2(metadata->minContentBoost), log2(metadata->maxContentBoost));
Dichen Zhangf7a9e172023-04-06 18:23:47 -0700564}
565
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000566uint8_t encodeGain(float y_sdr, float y_hdr, ultrahdr_metadata_ptr metadata,
Dichen Zhang10959a42023-04-10 16:28:16 -0700567 float log2MinContentBoost, float log2MaxContentBoost) {
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400568 float gain = 1.0f;
569 if (y_sdr > 0.0f) {
570 gain = y_hdr / y_sdr;
571 }
572
Nick Deakind19e5762023-02-10 15:39:08 -0500573 if (gain < metadata->minContentBoost) gain = metadata->minContentBoost;
574 if (gain > metadata->maxContentBoost) gain = metadata->maxContentBoost;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400575
Dichen Zhangf7a9e172023-04-06 18:23:47 -0700576 return static_cast<uint8_t>((log2(gain) - log2MinContentBoost)
577 / (log2MaxContentBoost - log2MinContentBoost)
Nick Deakind19e5762023-02-10 15:39:08 -0500578 * 255.0f);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400579}
580
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000581Color applyGain(Color e, float gain, ultrahdr_metadata_ptr metadata) {
Dichen Zhang10959a42023-04-10 16:28:16 -0700582 float logBoost = log2(metadata->minContentBoost) * (1.0f - gain)
583 + log2(metadata->maxContentBoost) * gain;
584 float gainFactor = exp2(logBoost);
585 return e * gainFactor;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400586}
587
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000588Color applyGain(Color e, float gain, ultrahdr_metadata_ptr metadata, float displayBoost) {
Dichen Zhang10959a42023-04-10 16:28:16 -0700589 float logBoost = log2(metadata->minContentBoost) * (1.0f - gain)
590 + log2(metadata->maxContentBoost) * gain;
591 float gainFactor = exp2(logBoost * displayBoost / metadata->maxContentBoost);
592 return e * gainFactor;
Dichen Zhangc6605702023-03-15 18:40:55 -0700593}
594
Dichen Zhang10959a42023-04-10 16:28:16 -0700595Color applyGainLUT(Color e, float gain, GainLUT& gainLUT) {
596 float gainFactor = gainLUT.getGainFactor(gain);
597 return e * gainFactor;
Harish Mahendrakarf25991f2022-12-16 11:57:44 -0800598}
599
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400600Color getYuv420Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
601 size_t pixel_count = image->width * image->height;
602
603 size_t pixel_y_idx = x + y * image->width;
604 size_t pixel_uv_idx = x / 2 + (y / 2) * (image->width / 2);
605
606 uint8_t y_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y_idx];
607 uint8_t u_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count + pixel_uv_idx];
608 uint8_t v_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count * 5 / 4 + pixel_uv_idx];
609
610 // 128 bias for UV given we are using jpeglib; see:
611 // https://github.com/kornelski/libjpeg/blob/master/structure.doc
612 return {{{ static_cast<float>(y_uint) / 255.0f,
613 (static_cast<float>(u_uint) - 128.0f) / 255.0f,
614 (static_cast<float>(v_uint) - 128.0f) / 255.0f }}};
615}
616
Nick Deakin594a4ca2022-11-16 20:57:42 -0500617Color getP010Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
Dichen Zhang66ca6e32023-04-05 12:22:54 -0700618 size_t luma_stride = image->luma_stride;
619 size_t chroma_stride = image->chroma_stride;
620 uint16_t* luma_data = reinterpret_cast<uint16_t*>(image->data);
621 uint16_t* chroma_data = reinterpret_cast<uint16_t*>(image->chroma_data);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400622
Dichen Zhang66ca6e32023-04-05 12:22:54 -0700623 if (luma_stride == 0) {
624 luma_stride = image->width;
625 }
626 if (chroma_stride == 0) {
627 chroma_stride = luma_stride;
628 }
629 if (chroma_data == nullptr) {
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000630 chroma_data = &reinterpret_cast<uint16_t*>(image->data)[luma_stride * image->height];
Dichen Zhang66ca6e32023-04-05 12:22:54 -0700631 }
Nick Deakin594a4ca2022-11-16 20:57:42 -0500632
Dichen Zhang66ca6e32023-04-05 12:22:54 -0700633 size_t pixel_y_idx = y * luma_stride + x;
634 size_t pixel_u_idx = (y >> 1) * chroma_stride + (x & ~0x1);
635 size_t pixel_v_idx = pixel_u_idx + 1;
636
637 uint16_t y_uint = luma_data[pixel_y_idx] >> 6;
638 uint16_t u_uint = chroma_data[pixel_u_idx] >> 6;
639 uint16_t v_uint = chroma_data[pixel_v_idx] >> 6;
Nick Deakin594a4ca2022-11-16 20:57:42 -0500640
641 // Conversions include taking narrow-range into account.
Nick Deakin38125332022-12-12 15:48:24 -0500642 return {{{ (static_cast<float>(y_uint) - 64.0f) / 876.0f,
643 (static_cast<float>(u_uint) - 64.0f) / 896.0f - 0.5f,
644 (static_cast<float>(v_uint) - 64.0f) / 896.0f - 0.5f }}};
Nick Deakin594a4ca2022-11-16 20:57:42 -0500645}
646
647typedef Color (*getPixelFn)(jr_uncompressed_ptr, size_t, size_t);
648
649static Color samplePixels(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y,
650 getPixelFn get_pixel_fn) {
651 Color e = {{{ 0.0f, 0.0f, 0.0f }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400652 for (size_t dy = 0; dy < map_scale_factor; ++dy) {
653 for (size_t dx = 0; dx < map_scale_factor; ++dx) {
Nick Deakin594a4ca2022-11-16 20:57:42 -0500654 e += get_pixel_fn(image, x * map_scale_factor + dx, y * map_scale_factor + dy);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400655 }
656 }
657
658 return e / static_cast<float>(map_scale_factor * map_scale_factor);
659}
660
Nick Deakin594a4ca2022-11-16 20:57:42 -0500661Color sampleYuv420(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) {
662 return samplePixels(image, map_scale_factor, x, y, getYuv420Pixel);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400663}
664
Nick Deakin594a4ca2022-11-16 20:57:42 -0500665Color sampleP010(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) {
666 return samplePixels(image, map_scale_factor, x, y, getP010Pixel);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400667}
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500668
Nick Deakin65f492a2022-11-29 22:47:40 -0500669// TODO: do we need something more clever for filtering either the map or images
670// to generate the map?
671
672static size_t clamp(const size_t& val, const size_t& low, const size_t& high) {
673 return val < low ? low : (high < val ? high : val);
674}
675
676static float mapUintToFloat(uint8_t map_uint) {
Nick Deakind19e5762023-02-10 15:39:08 -0500677 return static_cast<float>(map_uint) / 255.0f;
Nick Deakin65f492a2022-11-29 22:47:40 -0500678}
679
680static float pythDistance(float x_diff, float y_diff) {
681 return sqrt(pow(x_diff, 2.0f) + pow(y_diff, 2.0f));
682}
683
Ram Mohanfe723d62022-12-15 00:59:11 +0530684// TODO: If map_scale_factor is guaranteed to be an integer, then remove the following.
Nick Deakind19e5762023-02-10 15:39:08 -0500685float sampleMap(jr_uncompressed_ptr map, float map_scale_factor, size_t x, size_t y) {
686 float x_map = static_cast<float>(x) / map_scale_factor;
687 float y_map = static_cast<float>(y) / map_scale_factor;
Nick Deakin65f492a2022-11-29 22:47:40 -0500688
689 size_t x_lower = static_cast<size_t>(floor(x_map));
690 size_t x_upper = x_lower + 1;
691 size_t y_lower = static_cast<size_t>(floor(y_map));
692 size_t y_upper = y_lower + 1;
693
694 x_lower = clamp(x_lower, 0, map->width - 1);
695 x_upper = clamp(x_upper, 0, map->width - 1);
696 y_lower = clamp(y_lower, 0, map->height - 1);
697 y_upper = clamp(y_upper, 0, map->height - 1);
698
699 // Use Shepard's method for inverse distance weighting. For more information:
700 // en.wikipedia.org/wiki/Inverse_distance_weighting#Shepard's_method
701
702 float e1 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_lower * map->width]);
703 float e1_dist = pythDistance(x_map - static_cast<float>(x_lower),
704 y_map - static_cast<float>(y_lower));
705 if (e1_dist == 0.0f) return e1;
706
707 float e2 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_upper * map->width]);
708 float e2_dist = pythDistance(x_map - static_cast<float>(x_lower),
709 y_map - static_cast<float>(y_upper));
710 if (e2_dist == 0.0f) return e2;
711
712 float e3 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_lower * map->width]);
713 float e3_dist = pythDistance(x_map - static_cast<float>(x_upper),
714 y_map - static_cast<float>(y_lower));
715 if (e3_dist == 0.0f) return e3;
716
717 float e4 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_upper * map->width]);
718 float e4_dist = pythDistance(x_map - static_cast<float>(x_upper),
719 y_map - static_cast<float>(y_upper));
720 if (e4_dist == 0.0f) return e2;
721
722 float e1_weight = 1.0f / e1_dist;
723 float e2_weight = 1.0f / e2_dist;
724 float e3_weight = 1.0f / e3_dist;
725 float e4_weight = 1.0f / e4_dist;
726 float total_weight = e1_weight + e2_weight + e3_weight + e4_weight;
727
728 return e1 * (e1_weight / total_weight)
729 + e2 * (e2_weight / total_weight)
730 + e3 * (e3_weight / total_weight)
731 + e4 * (e4_weight / total_weight);
732}
733
Ram Mohanfe723d62022-12-15 00:59:11 +0530734float sampleMap(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y,
735 ShepardsIDW& weightTables) {
736 // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the
737 // following by computing log2(map_scale_factor) once and then using >> log2(map_scale_factor)
738 int x_lower = x / map_scale_factor;
739 int x_upper = x_lower + 1;
740 int y_lower = y / map_scale_factor;
741 int y_upper = y_lower + 1;
742
743 x_lower = std::min(x_lower, map->width - 1);
744 x_upper = std::min(x_upper, map->width - 1);
745 y_lower = std::min(y_lower, map->height - 1);
746 y_upper = std::min(y_upper, map->height - 1);
747
748 float e1 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_lower * map->width]);
749 float e2 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_upper * map->width]);
750 float e3 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_lower * map->width]);
751 float e4 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_upper * map->width]);
752
753 // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the
754 // following by using & (map_scale_factor - 1)
755 int offset_x = x % map_scale_factor;
756 int offset_y = y % map_scale_factor;
757
758 float* weights = weightTables.mWeights;
759 if (x_lower == x_upper && y_lower == y_upper) weights = weightTables.mWeightsC;
760 else if (x_lower == x_upper) weights = weightTables.mWeightsNR;
761 else if (y_lower == y_upper) weights = weightTables.mWeightsNB;
762 weights += offset_y * map_scale_factor * 4 + offset_x * 4;
763
764 return e1 * weights[0] + e2 * weights[1] + e3 * weights[2] + e4 * weights[3];
765}
766
Nick Deakin6bd90432022-11-20 16:26:37 -0500767uint32_t colorToRgba1010102(Color e_gamma) {
768 return (0x3ff & static_cast<uint32_t>(e_gamma.r * 1023.0f))
769 | ((0x3ff & static_cast<uint32_t>(e_gamma.g * 1023.0f)) << 10)
770 | ((0x3ff & static_cast<uint32_t>(e_gamma.b * 1023.0f)) << 20)
771 | (0x3 << 30); // Set alpha to 1.0
772}
773
Dichen Zhang3e5798c2023-03-01 22:30:43 +0000774uint64_t colorToRgbaF16(Color e_gamma) {
775 return (uint64_t) floatToHalf(e_gamma.r)
776 | (((uint64_t) floatToHalf(e_gamma.g)) << 16)
777 | (((uint64_t) floatToHalf(e_gamma.b)) << 32)
778 | (((uint64_t) floatToHalf(1.0f)) << 48);
779}
780
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000781} // namespace android::ultrahdr