blob: c2937e0b3bd39fc2a54f81617d5779123e7b8caa [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>
18
19#include <jpegrecoverymap/recoverymapmath.h>
20
21namespace android::recoverymap {
22
Nick Deakin594a4ca2022-11-16 20:57:42 -050023////////////////////////////////////////////////////////////////////////////////
24// sRGB transformations
Nick Deakinf6bca5a2022-11-04 10:43:43 -040025
Nick Deakin65f492a2022-11-29 22:47:40 -050026// See IEC 61966-2-1, Equation F.7.
27static const float kSrgbR = 0.2126f, kSrgbG = 0.7152f, kSrgbB = 0.0722f;
Nick Deakin594a4ca2022-11-16 20:57:42 -050028
29float srgbLuminance(Color e) {
30 return kSrgbR * e.r + kSrgbG * e.g + kSrgbB * e.b;
Nick Deakinf6bca5a2022-11-04 10:43:43 -040031}
32
Nick Deakin65f492a2022-11-29 22:47:40 -050033// See ECMA TR/98, Section 7.
Nick Deakinf6bca5a2022-11-04 10:43:43 -040034static const float kSrgbRCr = 1.402f, kSrgbGCb = 0.34414f, kSrgbGCr = 0.71414f, kSrgbBCb = 1.772f;
35
Nick Deakin594a4ca2022-11-16 20:57:42 -050036Color srgbYuvToRgb(Color e_gamma) {
37 return {{{ e_gamma.y + kSrgbRCr * e_gamma.v,
38 e_gamma.y - kSrgbGCb * e_gamma.u - kSrgbGCr * e_gamma.v,
39 e_gamma.y + kSrgbBCb * e_gamma.u }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -040040}
41
Nick Deakin65f492a2022-11-29 22:47:40 -050042// See ECMA TR/98, Section 7.
43static const float kSrgbYR = 0.299f, kSrgbYG = 0.587f, kSrgbYB = 0.114f;
Nick Deakin594a4ca2022-11-16 20:57:42 -050044static const float kSrgbUR = -0.1687f, kSrgbUG = -0.3313f, kSrgbUB = 0.5f;
45static const float kSrgbVR = 0.5f, kSrgbVG = -0.4187f, kSrgbVB = -0.0813f;
46
47Color srgbRgbToYuv(Color e_gamma) {
Nick Deakin65f492a2022-11-29 22:47:40 -050048 return {{{ kSrgbYR * e_gamma.r + kSrgbYG * e_gamma.g + kSrgbYB * e_gamma.b,
Nick Deakin594a4ca2022-11-16 20:57:42 -050049 kSrgbUR * e_gamma.r + kSrgbUG * e_gamma.g + kSrgbUB * e_gamma.b,
50 kSrgbVR * e_gamma.r + kSrgbVG * e_gamma.g + kSrgbVB * e_gamma.b }}};
51}
52
Nick Deakin65f492a2022-11-29 22:47:40 -050053// See IEC 61966-2-1, Equations F.5 and F.6.
Nick Deakin594a4ca2022-11-16 20:57:42 -050054float srgbInvOetf(float e_gamma) {
55 if (e_gamma <= 0.04045f) {
56 return e_gamma / 12.92f;
Nick Deakinf6bca5a2022-11-04 10:43:43 -040057 } else {
Nick Deakin594a4ca2022-11-16 20:57:42 -050058 return pow((e_gamma + 0.055f) / 1.055f, 2.4);
Nick Deakinf6bca5a2022-11-04 10:43:43 -040059 }
60}
61
Nick Deakin594a4ca2022-11-16 20:57:42 -050062Color srgbInvOetf(Color e_gamma) {
63 return {{{ srgbInvOetf(e_gamma.r),
64 srgbInvOetf(e_gamma.g),
65 srgbInvOetf(e_gamma.b) }}};
66}
67
68
69////////////////////////////////////////////////////////////////////////////////
70// Display-P3 transformations
71
Nick Deakin65f492a2022-11-29 22:47:40 -050072// See SMPTE EG 432-1, Table 7-2.
73static const float kP3R = 0.20949f, kP3G = 0.72160f, kP3B = 0.06891f;
Nick Deakin6bd90432022-11-20 16:26:37 -050074
75float p3Luminance(Color e) {
76 return kP3R * e.r + kP3G * e.g + kP3B * e.b;
77}
Nick Deakin594a4ca2022-11-16 20:57:42 -050078
79
80////////////////////////////////////////////////////////////////////////////////
81// BT.2100 transformations - according to ITU-R BT.2100-2
82
Nick Deakin65f492a2022-11-29 22:47:40 -050083// See ITU-R BT.2100-2, Table 5, HLG Reference OOTF
Nick Deakin594a4ca2022-11-16 20:57:42 -050084static const float kBt2100R = 0.2627f, kBt2100G = 0.6780f, kBt2100B = 0.0593f;
85
86float bt2100Luminance(Color e) {
87 return kBt2100R * e.r + kBt2100G * e.g + kBt2100B * e.b;
88}
89
Nick Deakin65f492a2022-11-29 22:47:40 -050090// See ITU-R BT.2100-2, Table 6, Derivation of colour difference signals.
Nick Deakin594a4ca2022-11-16 20:57:42 -050091static const float kBt2100Cb = 1.8814f, kBt2100Cr = 1.4746f;
92
93Color bt2100RgbToYuv(Color e_gamma) {
94 float y_gamma = bt2100Luminance(e_gamma);
95 return {{{ y_gamma,
96 (e_gamma.b - y_gamma) / kBt2100Cb,
97 (e_gamma.r - y_gamma) / kBt2100Cr }}};
98}
99
Nick Deakin65f492a2022-11-29 22:47:40 -0500100// Derived by inversing bt2100RgbToYuv. The derivation for R and B are pretty
101// straight forward; we just invert the formulas for U and V above. But deriving
102// the formula for G is a bit more complicated:
Nick Deakin594a4ca2022-11-16 20:57:42 -0500103//
104// Start with equation for luminance:
105// Y = kBt2100R * R + kBt2100G * G + kBt2100B * B
106// Solve for G:
107// G = (Y - kBt2100R * R - kBt2100B * B) / kBt2100B
108// Substitute equations for R and B in terms YUV:
109// G = (Y - kBt2100R * (Y + kBt2100Cr * V) - kBt2100B * (Y + kBt2100Cb * U)) / kBt2100B
110// Simplify:
111// G = Y * ((1 - kBt2100R - kBt2100B) / kBt2100G)
112// + U * (kBt2100B * kBt2100Cb / kBt2100G)
113// + V * (kBt2100R * kBt2100Cr / kBt2100G)
114//
115// We then get the following coeficients for calculating G from YUV:
116//
117// Coef for Y = (1 - kBt2100R - kBt2100B) / kBt2100G = 1
118// Coef for U = kBt2100B * kBt2100Cb / kBt2100G = kBt2100GCb = ~0.1645
119// Coef for V = kBt2100R * kBt2100Cr / kBt2100G = kBt2100GCr = ~0.5713
120
121static const float kBt2100GCb = kBt2100B * kBt2100Cb / kBt2100G;
122static const float kBt2100GCr = kBt2100R * kBt2100Cr / kBt2100G;
123
124Color bt2100YuvToRgb(Color e_gamma) {
125 return {{{ e_gamma.y + kBt2100Cr * e_gamma.v,
126 e_gamma.y - kBt2100GCb * e_gamma.u - kBt2100GCr * e_gamma.v,
127 e_gamma.y + kBt2100Cb * e_gamma.u }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400128}
129
Nick Deakin65f492a2022-11-29 22:47:40 -0500130// See ITU-R BT.2100-2, Table 5, HLG Reference OETF.
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400131static const float kHlgA = 0.17883277f, kHlgB = 0.28466892f, kHlgC = 0.55991073;
132
Nick Deakin65f492a2022-11-29 22:47:40 -0500133float hlgOetf(float e) {
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400134 if (e <= 1.0f/12.0f) {
135 return sqrt(3.0f * e);
136 } else {
137 return kHlgA * log(12.0f * e - kHlgB) + kHlgC;
138 }
139}
140
141Color hlgOetf(Color e) {
142 return {{{ hlgOetf(e.r), hlgOetf(e.g), hlgOetf(e.b) }}};
143}
144
Nick Deakin65f492a2022-11-29 22:47:40 -0500145// See ITU-R BT.2100-2, Table 5, HLG Reference EOTF.
146float hlgInvOetf(float e_gamma) {
Nick Deakin594a4ca2022-11-16 20:57:42 -0500147 if (e_gamma <= 0.5f) {
148 return pow(e_gamma, 2.0f) / 3.0f;
149 } else {
150 return (exp((e_gamma - kHlgC) / kHlgA) + kHlgB) / 12.0f;
151 }
152}
153
154Color hlgInvOetf(Color e_gamma) {
155 return {{{ hlgInvOetf(e_gamma.r),
156 hlgInvOetf(e_gamma.g),
157 hlgInvOetf(e_gamma.b) }}};
158}
159
Nick Deakin65f492a2022-11-29 22:47:40 -0500160// See ITU-R BT.2100-2, Table 4, Reference PQ OETF.
Nick Deakin6bd90432022-11-20 16:26:37 -0500161static const float kPqM1 = 2610.0f / 16384.0f, kPqM2 = 2523.0f / 4096.0f * 128.0f;
162static const float kPqC1 = 3424.0f / 4096.0f, kPqC2 = 2413.0f / 4096.0f * 32.0f,
163 kPqC3 = 2392.0f / 4096.0f * 32.0f;
164
Nick Deakin65f492a2022-11-29 22:47:40 -0500165float pqOetf(float e) {
166 if (e <= 0.0f) return 0.0f;
167 return pow((kPqC1 + kPqC2 * pow(e, kPqM1)) / (1 + kPqC3 * pow(e, kPqM1)),
Nick Deakin6bd90432022-11-20 16:26:37 -0500168 kPqM2);
169}
170
171Color pqOetf(Color e) {
172 return {{{ pqOetf(e.r), pqOetf(e.g), pqOetf(e.b) }}};
173}
174
Nick Deakin65f492a2022-11-29 22:47:40 -0500175// Derived from the inverse of the Reference PQ OETF.
176static const float kPqInvA = 128.0f, kPqInvB = 107.0f, kPqInvC = 2413.0f, kPqInvD = 2392.0f,
177 kPqInvE = 6.2773946361f, kPqInvF = 0.0126833f;
178
179float pqInvOetf(float e_gamma) {
180 // This equation blows up if e_gamma is 0.0, and checking on <= 0.0 doesn't
181 // always catch 0.0. So, check on 0.0001, since anything this small will
182 // effectively be crushed to zero anyways.
183 if (e_gamma <= 0.0001f) return 0.0f;
184 return pow((kPqInvA * pow(e_gamma, kPqInvF) - kPqInvB)
185 / (kPqInvC - kPqInvD * pow(e_gamma, kPqInvF)),
186 kPqInvE);
Nick Deakin6bd90432022-11-20 16:26:37 -0500187}
188
189Color pqInvOetf(Color e_gamma) {
190 return {{{ pqInvOetf(e_gamma.r),
191 pqInvOetf(e_gamma.g),
192 pqInvOetf(e_gamma.b) }}};
193}
194
Nick Deakin594a4ca2022-11-16 20:57:42 -0500195
196////////////////////////////////////////////////////////////////////////////////
197// Color conversions
198
Nick Deakin6bd90432022-11-20 16:26:37 -0500199Color bt709ToP3(Color e) {
200 return {{{ 0.82254f * e.r + 0.17755f * e.g + 0.00006f * e.b,
201 0.03312f * e.r + 0.96684f * e.g + -0.00001f * e.b,
202 0.01706f * e.r + 0.07240f * e.g + 0.91049f * e.b }}};
203}
204
205Color bt709ToBt2100(Color e) {
206 return {{{ 0.62740f * e.r + 0.32930f * e.g + 0.04332f * e.b,
207 0.06904f * e.r + 0.91958f * e.g + 0.01138f * e.b,
208 0.01636f * e.r + 0.08799f * e.g + 0.89555f * e.b }}};
209}
210
211Color p3ToBt709(Color e) {
212 return {{{ 1.22482f * e.r + -0.22490f * e.g + -0.00007f * e.b,
213 -0.04196f * e.r + 1.04199f * e.g + 0.00001f * e.b,
214 -0.01961f * e.r + -0.07865f * e.g + 1.09831f * e.b }}};
215}
216
217Color p3ToBt2100(Color e) {
218 return {{{ 0.75378f * e.r + 0.19862f * e.g + 0.04754f * e.b,
219 0.04576f * e.r + 0.94177f * e.g + 0.01250f * e.b,
220 -0.00121f * e.r + 0.01757f * e.g + 0.98359f * e.b }}};
221}
222
223Color bt2100ToBt709(Color e) {
224 return {{{ 1.66045f * e.r + -0.58764f * e.g + -0.07286f * e.b,
225 -0.12445f * e.r + 1.13282f * e.g + -0.00837f * e.b,
226 -0.01811f * e.r + -0.10057f * e.g + 1.11878f * e.b }}};
227}
228
229Color bt2100ToP3(Color e) {
230 return {{{ 1.34369f * e.r + -0.28223f * e.g + -0.06135f * e.b,
231 -0.06533f * e.r + 1.07580f * e.g + -0.01051f * e.b,
232 0.00283f * e.r + -0.01957f * e.g + 1.01679f * e.b
233 }}};
234}
235
236// TODO: confirm we always want to convert like this before calculating
237// luminance.
238ColorTransformFn getHdrConversionFn(jpegr_color_gamut sdr_gamut, jpegr_color_gamut hdr_gamut) {
Nick Deakin65f492a2022-11-29 22:47:40 -0500239 switch (sdr_gamut) {
Nick Deakin6bd90432022-11-20 16:26:37 -0500240 case JPEGR_COLORGAMUT_BT709:
241 switch (hdr_gamut) {
242 case JPEGR_COLORGAMUT_BT709:
243 return identityConversion;
244 case JPEGR_COLORGAMUT_P3:
245 return p3ToBt709;
246 case JPEGR_COLORGAMUT_BT2100:
247 return bt2100ToBt709;
248 case JPEGR_COLORGAMUT_UNSPECIFIED:
249 return nullptr;
250 }
251 break;
252 case JPEGR_COLORGAMUT_P3:
253 switch (hdr_gamut) {
254 case JPEGR_COLORGAMUT_BT709:
255 return bt709ToP3;
256 case JPEGR_COLORGAMUT_P3:
257 return identityConversion;
258 case JPEGR_COLORGAMUT_BT2100:
259 return bt2100ToP3;
260 case JPEGR_COLORGAMUT_UNSPECIFIED:
261 return nullptr;
262 }
263 break;
264 case JPEGR_COLORGAMUT_BT2100:
265 switch (hdr_gamut) {
266 case JPEGR_COLORGAMUT_BT709:
267 return bt709ToBt2100;
268 case JPEGR_COLORGAMUT_P3:
269 return p3ToBt2100;
270 case JPEGR_COLORGAMUT_BT2100:
271 return identityConversion;
272 case JPEGR_COLORGAMUT_UNSPECIFIED:
273 return nullptr;
274 }
275 break;
276 case JPEGR_COLORGAMUT_UNSPECIFIED:
277 return nullptr;
278 }
279}
280
Nick Deakin594a4ca2022-11-16 20:57:42 -0500281
282////////////////////////////////////////////////////////////////////////////////
283// Recovery map calculations
284
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500285uint8_t encodeRecovery(float y_sdr, float y_hdr, float hdr_ratio) {
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400286 float gain = 1.0f;
287 if (y_sdr > 0.0f) {
288 gain = y_hdr / y_sdr;
289 }
290
Nick Deakin65f492a2022-11-29 22:47:40 -0500291 if (gain < (1.0f / hdr_ratio)) gain = 1.0f / hdr_ratio;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400292 if (gain > hdr_ratio) gain = hdr_ratio;
293
294 return static_cast<uint8_t>(log2(gain) / log2(hdr_ratio) * 127.5f + 127.5f);
295}
296
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500297Color applyRecovery(Color e, float recovery, float hdr_ratio) {
Harish Mahendrakara5ddcc22022-12-13 12:45:23 -0800298 float recoveryFactor = pow(hdr_ratio, recovery);
299 return e * recoveryFactor;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400300}
301
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400302Color getYuv420Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
303 size_t pixel_count = image->width * image->height;
304
305 size_t pixel_y_idx = x + y * image->width;
306 size_t pixel_uv_idx = x / 2 + (y / 2) * (image->width / 2);
307
308 uint8_t y_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y_idx];
309 uint8_t u_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count + pixel_uv_idx];
310 uint8_t v_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count * 5 / 4 + pixel_uv_idx];
311
312 // 128 bias for UV given we are using jpeglib; see:
313 // https://github.com/kornelski/libjpeg/blob/master/structure.doc
314 return {{{ static_cast<float>(y_uint) / 255.0f,
315 (static_cast<float>(u_uint) - 128.0f) / 255.0f,
316 (static_cast<float>(v_uint) - 128.0f) / 255.0f }}};
317}
318
Nick Deakin594a4ca2022-11-16 20:57:42 -0500319Color getP010Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
320 size_t pixel_count = image->width * image->height;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400321
Nick Deakin594a4ca2022-11-16 20:57:42 -0500322 size_t pixel_y_idx = x + y * image->width;
323 size_t pixel_uv_idx = x / 2 + (y / 2) * (image->width / 2);
324
Nick Deakin86207ba2022-11-21 16:07:36 -0500325 uint16_t y_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_y_idx]
326 >> 6;
327 uint16_t u_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_count + pixel_uv_idx * 2]
328 >> 6;
329 uint16_t v_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_count + pixel_uv_idx * 2 + 1]
330 >> 6;
Nick Deakin594a4ca2022-11-16 20:57:42 -0500331
332 // Conversions include taking narrow-range into account.
Nick Deakin38125332022-12-12 15:48:24 -0500333 return {{{ (static_cast<float>(y_uint) - 64.0f) / 876.0f,
334 (static_cast<float>(u_uint) - 64.0f) / 896.0f - 0.5f,
335 (static_cast<float>(v_uint) - 64.0f) / 896.0f - 0.5f }}};
Nick Deakin594a4ca2022-11-16 20:57:42 -0500336}
337
338typedef Color (*getPixelFn)(jr_uncompressed_ptr, size_t, size_t);
339
340static Color samplePixels(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y,
341 getPixelFn get_pixel_fn) {
342 Color e = {{{ 0.0f, 0.0f, 0.0f }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400343 for (size_t dy = 0; dy < map_scale_factor; ++dy) {
344 for (size_t dx = 0; dx < map_scale_factor; ++dx) {
Nick Deakin594a4ca2022-11-16 20:57:42 -0500345 e += get_pixel_fn(image, x * map_scale_factor + dx, y * map_scale_factor + dy);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400346 }
347 }
348
349 return e / static_cast<float>(map_scale_factor * map_scale_factor);
350}
351
Nick Deakin594a4ca2022-11-16 20:57:42 -0500352Color sampleYuv420(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) {
353 return samplePixels(image, map_scale_factor, x, y, getYuv420Pixel);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400354}
355
Nick Deakin594a4ca2022-11-16 20:57:42 -0500356Color sampleP010(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) {
357 return samplePixels(image, map_scale_factor, x, y, getP010Pixel);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400358}
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500359
Nick Deakin65f492a2022-11-29 22:47:40 -0500360// TODO: do we need something more clever for filtering either the map or images
361// to generate the map?
362
363static size_t clamp(const size_t& val, const size_t& low, const size_t& high) {
364 return val < low ? low : (high < val ? high : val);
365}
366
367static float mapUintToFloat(uint8_t map_uint) {
368 return (static_cast<float>(map_uint) - 127.5f) / 127.5f;
369}
370
371static float pythDistance(float x_diff, float y_diff) {
372 return sqrt(pow(x_diff, 2.0f) + pow(y_diff, 2.0f));
373}
374
375float sampleMap(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y) {
376 float x_map = static_cast<float>(x) / static_cast<float>(map_scale_factor);
377 float y_map = static_cast<float>(y) / static_cast<float>(map_scale_factor);
378
379 size_t x_lower = static_cast<size_t>(floor(x_map));
380 size_t x_upper = x_lower + 1;
381 size_t y_lower = static_cast<size_t>(floor(y_map));
382 size_t y_upper = y_lower + 1;
383
384 x_lower = clamp(x_lower, 0, map->width - 1);
385 x_upper = clamp(x_upper, 0, map->width - 1);
386 y_lower = clamp(y_lower, 0, map->height - 1);
387 y_upper = clamp(y_upper, 0, map->height - 1);
388
389 // Use Shepard's method for inverse distance weighting. For more information:
390 // en.wikipedia.org/wiki/Inverse_distance_weighting#Shepard's_method
391
392 float e1 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_lower * map->width]);
393 float e1_dist = pythDistance(x_map - static_cast<float>(x_lower),
394 y_map - static_cast<float>(y_lower));
395 if (e1_dist == 0.0f) return e1;
396
397 float e2 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_upper * map->width]);
398 float e2_dist = pythDistance(x_map - static_cast<float>(x_lower),
399 y_map - static_cast<float>(y_upper));
400 if (e2_dist == 0.0f) return e2;
401
402 float e3 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_lower * map->width]);
403 float e3_dist = pythDistance(x_map - static_cast<float>(x_upper),
404 y_map - static_cast<float>(y_lower));
405 if (e3_dist == 0.0f) return e3;
406
407 float e4 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_upper * map->width]);
408 float e4_dist = pythDistance(x_map - static_cast<float>(x_upper),
409 y_map - static_cast<float>(y_upper));
410 if (e4_dist == 0.0f) return e2;
411
412 float e1_weight = 1.0f / e1_dist;
413 float e2_weight = 1.0f / e2_dist;
414 float e3_weight = 1.0f / e3_dist;
415 float e4_weight = 1.0f / e4_dist;
416 float total_weight = e1_weight + e2_weight + e3_weight + e4_weight;
417
418 return e1 * (e1_weight / total_weight)
419 + e2 * (e2_weight / total_weight)
420 + e3 * (e3_weight / total_weight)
421 + e4 * (e4_weight / total_weight);
422}
423
Nick Deakin6bd90432022-11-20 16:26:37 -0500424uint32_t colorToRgba1010102(Color e_gamma) {
425 return (0x3ff & static_cast<uint32_t>(e_gamma.r * 1023.0f))
426 | ((0x3ff & static_cast<uint32_t>(e_gamma.g * 1023.0f)) << 10)
427 | ((0x3ff & static_cast<uint32_t>(e_gamma.b * 1023.0f)) << 20)
428 | (0x3 << 30); // Set alpha to 1.0
429}
430
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400431} // namespace android::recoverymap