blob: 4a6feca2e8f8330675992e1d5dc7191b22eebeee [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
Ram Mohanfe723d62022-12-15 00:59:11 +053023
24// Use Shepard's method for inverse distance weighting. For more information:
25// en.wikipedia.org/wiki/Inverse_distance_weighting#Shepard's_method
26
27float ShepardsIDW::euclideanDistance(float x1, float x2, float y1, float y2) {
28 return sqrt(((y2 - y1) * (y2 - y1)) + (x2 - x1) * (x2 - x1));
29}
30
31void ShepardsIDW::fillShepardsIDW(float *weights, int incR, int incB) {
32 for (int y = 0; y < mMapScaleFactor; y++) {
33 for (int x = 0; x < mMapScaleFactor; x++) {
34 float pos_x = ((float)x) / mMapScaleFactor;
35 float pos_y = ((float)y) / mMapScaleFactor;
36 int curr_x = floor(pos_x);
37 int curr_y = floor(pos_y);
38 int next_x = curr_x + incR;
39 int next_y = curr_y + incB;
40 float e1_distance = euclideanDistance(pos_x, curr_x, pos_y, curr_y);
41 int index = y * mMapScaleFactor * 4 + x * 4;
42 if (e1_distance == 0) {
43 weights[index++] = 1.f;
44 weights[index++] = 0.f;
45 weights[index++] = 0.f;
46 weights[index++] = 0.f;
47 } else {
48 float e1_weight = 1.f / e1_distance;
49
50 float e2_distance = euclideanDistance(pos_x, curr_x, pos_y, next_y);
51 float e2_weight = 1.f / e2_distance;
52
53 float e3_distance = euclideanDistance(pos_x, next_x, pos_y, curr_y);
54 float e3_weight = 1.f / e3_distance;
55
56 float e4_distance = euclideanDistance(pos_x, next_x, pos_y, next_y);
57 float e4_weight = 1.f / e4_distance;
58
59 float total_weight = e1_weight + e2_weight + e3_weight + e4_weight;
60
61 weights[index++] = e1_weight / total_weight;
62 weights[index++] = e2_weight / total_weight;
63 weights[index++] = e3_weight / total_weight;
64 weights[index++] = e4_weight / total_weight;
65 }
66 }
67 }
68}
69
Nick Deakin594a4ca2022-11-16 20:57:42 -050070////////////////////////////////////////////////////////////////////////////////
71// sRGB transformations
Nick Deakinf6bca5a2022-11-04 10:43:43 -040072
Nick Deakin65f492a2022-11-29 22:47:40 -050073// See IEC 61966-2-1, Equation F.7.
74static const float kSrgbR = 0.2126f, kSrgbG = 0.7152f, kSrgbB = 0.0722f;
Nick Deakin594a4ca2022-11-16 20:57:42 -050075
76float srgbLuminance(Color e) {
77 return kSrgbR * e.r + kSrgbG * e.g + kSrgbB * e.b;
Nick Deakinf6bca5a2022-11-04 10:43:43 -040078}
79
Nick Deakin65f492a2022-11-29 22:47:40 -050080// See ECMA TR/98, Section 7.
Nick Deakinf6bca5a2022-11-04 10:43:43 -040081static const float kSrgbRCr = 1.402f, kSrgbGCb = 0.34414f, kSrgbGCr = 0.71414f, kSrgbBCb = 1.772f;
82
Nick Deakin594a4ca2022-11-16 20:57:42 -050083Color srgbYuvToRgb(Color e_gamma) {
84 return {{{ e_gamma.y + kSrgbRCr * e_gamma.v,
85 e_gamma.y - kSrgbGCb * e_gamma.u - kSrgbGCr * e_gamma.v,
86 e_gamma.y + kSrgbBCb * e_gamma.u }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -040087}
88
Nick Deakin65f492a2022-11-29 22:47:40 -050089// See ECMA TR/98, Section 7.
90static const float kSrgbYR = 0.299f, kSrgbYG = 0.587f, kSrgbYB = 0.114f;
Nick Deakin594a4ca2022-11-16 20:57:42 -050091static const float kSrgbUR = -0.1687f, kSrgbUG = -0.3313f, kSrgbUB = 0.5f;
92static const float kSrgbVR = 0.5f, kSrgbVG = -0.4187f, kSrgbVB = -0.0813f;
93
94Color srgbRgbToYuv(Color e_gamma) {
Nick Deakin65f492a2022-11-29 22:47:40 -050095 return {{{ kSrgbYR * e_gamma.r + kSrgbYG * e_gamma.g + kSrgbYB * e_gamma.b,
Nick Deakin594a4ca2022-11-16 20:57:42 -050096 kSrgbUR * e_gamma.r + kSrgbUG * e_gamma.g + kSrgbUB * e_gamma.b,
97 kSrgbVR * e_gamma.r + kSrgbVG * e_gamma.g + kSrgbVB * e_gamma.b }}};
98}
99
Nick Deakin65f492a2022-11-29 22:47:40 -0500100// See IEC 61966-2-1, Equations F.5 and F.6.
Nick Deakin594a4ca2022-11-16 20:57:42 -0500101float srgbInvOetf(float e_gamma) {
102 if (e_gamma <= 0.04045f) {
103 return e_gamma / 12.92f;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400104 } else {
Nick Deakin594a4ca2022-11-16 20:57:42 -0500105 return pow((e_gamma + 0.055f) / 1.055f, 2.4);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400106 }
107}
108
Nick Deakin594a4ca2022-11-16 20:57:42 -0500109Color srgbInvOetf(Color e_gamma) {
110 return {{{ srgbInvOetf(e_gamma.r),
111 srgbInvOetf(e_gamma.g),
112 srgbInvOetf(e_gamma.b) }}};
113}
114
115
116////////////////////////////////////////////////////////////////////////////////
117// Display-P3 transformations
118
Nick Deakin65f492a2022-11-29 22:47:40 -0500119// See SMPTE EG 432-1, Table 7-2.
120static const float kP3R = 0.20949f, kP3G = 0.72160f, kP3B = 0.06891f;
Nick Deakin6bd90432022-11-20 16:26:37 -0500121
122float p3Luminance(Color e) {
123 return kP3R * e.r + kP3G * e.g + kP3B * e.b;
124}
Nick Deakin594a4ca2022-11-16 20:57:42 -0500125
126
127////////////////////////////////////////////////////////////////////////////////
128// BT.2100 transformations - according to ITU-R BT.2100-2
129
Nick Deakin65f492a2022-11-29 22:47:40 -0500130// See ITU-R BT.2100-2, Table 5, HLG Reference OOTF
Nick Deakin594a4ca2022-11-16 20:57:42 -0500131static const float kBt2100R = 0.2627f, kBt2100G = 0.6780f, kBt2100B = 0.0593f;
132
133float bt2100Luminance(Color e) {
134 return kBt2100R * e.r + kBt2100G * e.g + kBt2100B * e.b;
135}
136
Nick Deakin65f492a2022-11-29 22:47:40 -0500137// See ITU-R BT.2100-2, Table 6, Derivation of colour difference signals.
Nick Deakin594a4ca2022-11-16 20:57:42 -0500138static const float kBt2100Cb = 1.8814f, kBt2100Cr = 1.4746f;
139
140Color bt2100RgbToYuv(Color e_gamma) {
141 float y_gamma = bt2100Luminance(e_gamma);
142 return {{{ y_gamma,
143 (e_gamma.b - y_gamma) / kBt2100Cb,
144 (e_gamma.r - y_gamma) / kBt2100Cr }}};
145}
146
Nick Deakin65f492a2022-11-29 22:47:40 -0500147// Derived by inversing bt2100RgbToYuv. The derivation for R and B are pretty
148// straight forward; we just invert the formulas for U and V above. But deriving
149// the formula for G is a bit more complicated:
Nick Deakin594a4ca2022-11-16 20:57:42 -0500150//
151// Start with equation for luminance:
152// Y = kBt2100R * R + kBt2100G * G + kBt2100B * B
153// Solve for G:
154// G = (Y - kBt2100R * R - kBt2100B * B) / kBt2100B
155// Substitute equations for R and B in terms YUV:
156// G = (Y - kBt2100R * (Y + kBt2100Cr * V) - kBt2100B * (Y + kBt2100Cb * U)) / kBt2100B
157// Simplify:
158// G = Y * ((1 - kBt2100R - kBt2100B) / kBt2100G)
159// + U * (kBt2100B * kBt2100Cb / kBt2100G)
160// + V * (kBt2100R * kBt2100Cr / kBt2100G)
161//
162// We then get the following coeficients for calculating G from YUV:
163//
164// Coef for Y = (1 - kBt2100R - kBt2100B) / kBt2100G = 1
165// Coef for U = kBt2100B * kBt2100Cb / kBt2100G = kBt2100GCb = ~0.1645
166// Coef for V = kBt2100R * kBt2100Cr / kBt2100G = kBt2100GCr = ~0.5713
167
168static const float kBt2100GCb = kBt2100B * kBt2100Cb / kBt2100G;
169static const float kBt2100GCr = kBt2100R * kBt2100Cr / kBt2100G;
170
171Color bt2100YuvToRgb(Color e_gamma) {
172 return {{{ e_gamma.y + kBt2100Cr * e_gamma.v,
173 e_gamma.y - kBt2100GCb * e_gamma.u - kBt2100GCr * e_gamma.v,
174 e_gamma.y + kBt2100Cb * e_gamma.u }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400175}
176
Nick Deakin65f492a2022-11-29 22:47:40 -0500177// See ITU-R BT.2100-2, Table 5, HLG Reference OETF.
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400178static const float kHlgA = 0.17883277f, kHlgB = 0.28466892f, kHlgC = 0.55991073;
179
Nick Deakin65f492a2022-11-29 22:47:40 -0500180float hlgOetf(float e) {
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400181 if (e <= 1.0f/12.0f) {
182 return sqrt(3.0f * e);
183 } else {
184 return kHlgA * log(12.0f * e - kHlgB) + kHlgC;
185 }
186}
187
188Color hlgOetf(Color e) {
189 return {{{ hlgOetf(e.r), hlgOetf(e.g), hlgOetf(e.b) }}};
190}
191
Nick Deakin65f492a2022-11-29 22:47:40 -0500192// See ITU-R BT.2100-2, Table 5, HLG Reference EOTF.
193float hlgInvOetf(float e_gamma) {
Nick Deakin594a4ca2022-11-16 20:57:42 -0500194 if (e_gamma <= 0.5f) {
195 return pow(e_gamma, 2.0f) / 3.0f;
196 } else {
197 return (exp((e_gamma - kHlgC) / kHlgA) + kHlgB) / 12.0f;
198 }
199}
200
201Color hlgInvOetf(Color e_gamma) {
202 return {{{ hlgInvOetf(e_gamma.r),
203 hlgInvOetf(e_gamma.g),
204 hlgInvOetf(e_gamma.b) }}};
205}
206
Nick Deakin65f492a2022-11-29 22:47:40 -0500207// See ITU-R BT.2100-2, Table 4, Reference PQ OETF.
Nick Deakin6bd90432022-11-20 16:26:37 -0500208static const float kPqM1 = 2610.0f / 16384.0f, kPqM2 = 2523.0f / 4096.0f * 128.0f;
209static const float kPqC1 = 3424.0f / 4096.0f, kPqC2 = 2413.0f / 4096.0f * 32.0f,
210 kPqC3 = 2392.0f / 4096.0f * 32.0f;
211
Nick Deakin65f492a2022-11-29 22:47:40 -0500212float pqOetf(float e) {
213 if (e <= 0.0f) return 0.0f;
214 return pow((kPqC1 + kPqC2 * pow(e, kPqM1)) / (1 + kPqC3 * pow(e, kPqM1)),
Nick Deakin6bd90432022-11-20 16:26:37 -0500215 kPqM2);
216}
217
218Color pqOetf(Color e) {
219 return {{{ pqOetf(e.r), pqOetf(e.g), pqOetf(e.b) }}};
220}
221
Nick Deakin65f492a2022-11-29 22:47:40 -0500222// Derived from the inverse of the Reference PQ OETF.
223static const float kPqInvA = 128.0f, kPqInvB = 107.0f, kPqInvC = 2413.0f, kPqInvD = 2392.0f,
224 kPqInvE = 6.2773946361f, kPqInvF = 0.0126833f;
225
226float pqInvOetf(float e_gamma) {
227 // This equation blows up if e_gamma is 0.0, and checking on <= 0.0 doesn't
228 // always catch 0.0. So, check on 0.0001, since anything this small will
229 // effectively be crushed to zero anyways.
230 if (e_gamma <= 0.0001f) return 0.0f;
231 return pow((kPqInvA * pow(e_gamma, kPqInvF) - kPqInvB)
232 / (kPqInvC - kPqInvD * pow(e_gamma, kPqInvF)),
233 kPqInvE);
Nick Deakin6bd90432022-11-20 16:26:37 -0500234}
235
236Color pqInvOetf(Color e_gamma) {
237 return {{{ pqInvOetf(e_gamma.r),
238 pqInvOetf(e_gamma.g),
239 pqInvOetf(e_gamma.b) }}};
240}
241
Nick Deakin594a4ca2022-11-16 20:57:42 -0500242
243////////////////////////////////////////////////////////////////////////////////
244// Color conversions
245
Nick Deakin6bd90432022-11-20 16:26:37 -0500246Color bt709ToP3(Color e) {
247 return {{{ 0.82254f * e.r + 0.17755f * e.g + 0.00006f * e.b,
248 0.03312f * e.r + 0.96684f * e.g + -0.00001f * e.b,
249 0.01706f * e.r + 0.07240f * e.g + 0.91049f * e.b }}};
250}
251
252Color bt709ToBt2100(Color e) {
253 return {{{ 0.62740f * e.r + 0.32930f * e.g + 0.04332f * e.b,
254 0.06904f * e.r + 0.91958f * e.g + 0.01138f * e.b,
255 0.01636f * e.r + 0.08799f * e.g + 0.89555f * e.b }}};
256}
257
258Color p3ToBt709(Color e) {
259 return {{{ 1.22482f * e.r + -0.22490f * e.g + -0.00007f * e.b,
260 -0.04196f * e.r + 1.04199f * e.g + 0.00001f * e.b,
261 -0.01961f * e.r + -0.07865f * e.g + 1.09831f * e.b }}};
262}
263
264Color p3ToBt2100(Color e) {
265 return {{{ 0.75378f * e.r + 0.19862f * e.g + 0.04754f * e.b,
266 0.04576f * e.r + 0.94177f * e.g + 0.01250f * e.b,
267 -0.00121f * e.r + 0.01757f * e.g + 0.98359f * e.b }}};
268}
269
270Color bt2100ToBt709(Color e) {
271 return {{{ 1.66045f * e.r + -0.58764f * e.g + -0.07286f * e.b,
272 -0.12445f * e.r + 1.13282f * e.g + -0.00837f * e.b,
273 -0.01811f * e.r + -0.10057f * e.g + 1.11878f * e.b }}};
274}
275
276Color bt2100ToP3(Color e) {
277 return {{{ 1.34369f * e.r + -0.28223f * e.g + -0.06135f * e.b,
278 -0.06533f * e.r + 1.07580f * e.g + -0.01051f * e.b,
279 0.00283f * e.r + -0.01957f * e.g + 1.01679f * e.b
280 }}};
281}
282
283// TODO: confirm we always want to convert like this before calculating
284// luminance.
285ColorTransformFn getHdrConversionFn(jpegr_color_gamut sdr_gamut, jpegr_color_gamut hdr_gamut) {
Nick Deakin65f492a2022-11-29 22:47:40 -0500286 switch (sdr_gamut) {
Nick Deakin6bd90432022-11-20 16:26:37 -0500287 case JPEGR_COLORGAMUT_BT709:
288 switch (hdr_gamut) {
289 case JPEGR_COLORGAMUT_BT709:
290 return identityConversion;
291 case JPEGR_COLORGAMUT_P3:
292 return p3ToBt709;
293 case JPEGR_COLORGAMUT_BT2100:
294 return bt2100ToBt709;
295 case JPEGR_COLORGAMUT_UNSPECIFIED:
296 return nullptr;
297 }
298 break;
299 case JPEGR_COLORGAMUT_P3:
300 switch (hdr_gamut) {
301 case JPEGR_COLORGAMUT_BT709:
302 return bt709ToP3;
303 case JPEGR_COLORGAMUT_P3:
304 return identityConversion;
305 case JPEGR_COLORGAMUT_BT2100:
306 return bt2100ToP3;
307 case JPEGR_COLORGAMUT_UNSPECIFIED:
308 return nullptr;
309 }
310 break;
311 case JPEGR_COLORGAMUT_BT2100:
312 switch (hdr_gamut) {
313 case JPEGR_COLORGAMUT_BT709:
314 return bt709ToBt2100;
315 case JPEGR_COLORGAMUT_P3:
316 return p3ToBt2100;
317 case JPEGR_COLORGAMUT_BT2100:
318 return identityConversion;
319 case JPEGR_COLORGAMUT_UNSPECIFIED:
320 return nullptr;
321 }
322 break;
323 case JPEGR_COLORGAMUT_UNSPECIFIED:
324 return nullptr;
325 }
326}
327
Nick Deakin594a4ca2022-11-16 20:57:42 -0500328
329////////////////////////////////////////////////////////////////////////////////
330// Recovery map calculations
331
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500332uint8_t encodeRecovery(float y_sdr, float y_hdr, float hdr_ratio) {
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400333 float gain = 1.0f;
334 if (y_sdr > 0.0f) {
335 gain = y_hdr / y_sdr;
336 }
337
Nick Deakin65f492a2022-11-29 22:47:40 -0500338 if (gain < (1.0f / hdr_ratio)) gain = 1.0f / hdr_ratio;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400339 if (gain > hdr_ratio) gain = hdr_ratio;
340
341 return static_cast<uint8_t>(log2(gain) / log2(hdr_ratio) * 127.5f + 127.5f);
342}
343
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500344Color applyRecovery(Color e, float recovery, float hdr_ratio) {
Harish Mahendrakara5ddcc22022-12-13 12:45:23 -0800345 float recoveryFactor = pow(hdr_ratio, recovery);
346 return e * recoveryFactor;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400347}
348
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400349Color getYuv420Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
350 size_t pixel_count = image->width * image->height;
351
352 size_t pixel_y_idx = x + y * image->width;
353 size_t pixel_uv_idx = x / 2 + (y / 2) * (image->width / 2);
354
355 uint8_t y_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y_idx];
356 uint8_t u_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count + pixel_uv_idx];
357 uint8_t v_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count * 5 / 4 + pixel_uv_idx];
358
359 // 128 bias for UV given we are using jpeglib; see:
360 // https://github.com/kornelski/libjpeg/blob/master/structure.doc
361 return {{{ static_cast<float>(y_uint) / 255.0f,
362 (static_cast<float>(u_uint) - 128.0f) / 255.0f,
363 (static_cast<float>(v_uint) - 128.0f) / 255.0f }}};
364}
365
Nick Deakin594a4ca2022-11-16 20:57:42 -0500366Color getP010Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
367 size_t pixel_count = image->width * image->height;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400368
Nick Deakin594a4ca2022-11-16 20:57:42 -0500369 size_t pixel_y_idx = x + y * image->width;
370 size_t pixel_uv_idx = x / 2 + (y / 2) * (image->width / 2);
371
Nick Deakin86207ba2022-11-21 16:07:36 -0500372 uint16_t y_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_y_idx]
373 >> 6;
374 uint16_t u_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_count + pixel_uv_idx * 2]
375 >> 6;
376 uint16_t v_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_count + pixel_uv_idx * 2 + 1]
377 >> 6;
Nick Deakin594a4ca2022-11-16 20:57:42 -0500378
379 // Conversions include taking narrow-range into account.
Nick Deakin38125332022-12-12 15:48:24 -0500380 return {{{ (static_cast<float>(y_uint) - 64.0f) / 876.0f,
381 (static_cast<float>(u_uint) - 64.0f) / 896.0f - 0.5f,
382 (static_cast<float>(v_uint) - 64.0f) / 896.0f - 0.5f }}};
Nick Deakin594a4ca2022-11-16 20:57:42 -0500383}
384
385typedef Color (*getPixelFn)(jr_uncompressed_ptr, size_t, size_t);
386
387static Color samplePixels(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y,
388 getPixelFn get_pixel_fn) {
389 Color e = {{{ 0.0f, 0.0f, 0.0f }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400390 for (size_t dy = 0; dy < map_scale_factor; ++dy) {
391 for (size_t dx = 0; dx < map_scale_factor; ++dx) {
Nick Deakin594a4ca2022-11-16 20:57:42 -0500392 e += get_pixel_fn(image, x * map_scale_factor + dx, y * map_scale_factor + dy);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400393 }
394 }
395
396 return e / static_cast<float>(map_scale_factor * map_scale_factor);
397}
398
Nick Deakin594a4ca2022-11-16 20:57:42 -0500399Color sampleYuv420(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) {
400 return samplePixels(image, map_scale_factor, x, y, getYuv420Pixel);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400401}
402
Nick Deakin594a4ca2022-11-16 20:57:42 -0500403Color sampleP010(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) {
404 return samplePixels(image, map_scale_factor, x, y, getP010Pixel);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400405}
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500406
Nick Deakin65f492a2022-11-29 22:47:40 -0500407// TODO: do we need something more clever for filtering either the map or images
408// to generate the map?
409
410static size_t clamp(const size_t& val, const size_t& low, const size_t& high) {
411 return val < low ? low : (high < val ? high : val);
412}
413
414static float mapUintToFloat(uint8_t map_uint) {
415 return (static_cast<float>(map_uint) - 127.5f) / 127.5f;
416}
417
418static float pythDistance(float x_diff, float y_diff) {
419 return sqrt(pow(x_diff, 2.0f) + pow(y_diff, 2.0f));
420}
421
Ram Mohanfe723d62022-12-15 00:59:11 +0530422// TODO: If map_scale_factor is guaranteed to be an integer, then remove the following.
Nick Deakin65f492a2022-11-29 22:47:40 -0500423float sampleMap(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y) {
424 float x_map = static_cast<float>(x) / static_cast<float>(map_scale_factor);
425 float y_map = static_cast<float>(y) / static_cast<float>(map_scale_factor);
426
427 size_t x_lower = static_cast<size_t>(floor(x_map));
428 size_t x_upper = x_lower + 1;
429 size_t y_lower = static_cast<size_t>(floor(y_map));
430 size_t y_upper = y_lower + 1;
431
432 x_lower = clamp(x_lower, 0, map->width - 1);
433 x_upper = clamp(x_upper, 0, map->width - 1);
434 y_lower = clamp(y_lower, 0, map->height - 1);
435 y_upper = clamp(y_upper, 0, map->height - 1);
436
437 // Use Shepard's method for inverse distance weighting. For more information:
438 // en.wikipedia.org/wiki/Inverse_distance_weighting#Shepard's_method
439
440 float e1 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_lower * map->width]);
441 float e1_dist = pythDistance(x_map - static_cast<float>(x_lower),
442 y_map - static_cast<float>(y_lower));
443 if (e1_dist == 0.0f) return e1;
444
445 float e2 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_upper * map->width]);
446 float e2_dist = pythDistance(x_map - static_cast<float>(x_lower),
447 y_map - static_cast<float>(y_upper));
448 if (e2_dist == 0.0f) return e2;
449
450 float e3 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_lower * map->width]);
451 float e3_dist = pythDistance(x_map - static_cast<float>(x_upper),
452 y_map - static_cast<float>(y_lower));
453 if (e3_dist == 0.0f) return e3;
454
455 float e4 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_upper * map->width]);
456 float e4_dist = pythDistance(x_map - static_cast<float>(x_upper),
457 y_map - static_cast<float>(y_upper));
458 if (e4_dist == 0.0f) return e2;
459
460 float e1_weight = 1.0f / e1_dist;
461 float e2_weight = 1.0f / e2_dist;
462 float e3_weight = 1.0f / e3_dist;
463 float e4_weight = 1.0f / e4_dist;
464 float total_weight = e1_weight + e2_weight + e3_weight + e4_weight;
465
466 return e1 * (e1_weight / total_weight)
467 + e2 * (e2_weight / total_weight)
468 + e3 * (e3_weight / total_weight)
469 + e4 * (e4_weight / total_weight);
470}
471
Ram Mohanfe723d62022-12-15 00:59:11 +0530472float sampleMap(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y,
473 ShepardsIDW& weightTables) {
474 // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the
475 // following by computing log2(map_scale_factor) once and then using >> log2(map_scale_factor)
476 int x_lower = x / map_scale_factor;
477 int x_upper = x_lower + 1;
478 int y_lower = y / map_scale_factor;
479 int y_upper = y_lower + 1;
480
481 x_lower = std::min(x_lower, map->width - 1);
482 x_upper = std::min(x_upper, map->width - 1);
483 y_lower = std::min(y_lower, map->height - 1);
484 y_upper = std::min(y_upper, map->height - 1);
485
486 float e1 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_lower * map->width]);
487 float e2 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_upper * map->width]);
488 float e3 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_lower * map->width]);
489 float e4 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_upper * map->width]);
490
491 // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the
492 // following by using & (map_scale_factor - 1)
493 int offset_x = x % map_scale_factor;
494 int offset_y = y % map_scale_factor;
495
496 float* weights = weightTables.mWeights;
497 if (x_lower == x_upper && y_lower == y_upper) weights = weightTables.mWeightsC;
498 else if (x_lower == x_upper) weights = weightTables.mWeightsNR;
499 else if (y_lower == y_upper) weights = weightTables.mWeightsNB;
500 weights += offset_y * map_scale_factor * 4 + offset_x * 4;
501
502 return e1 * weights[0] + e2 * weights[1] + e3 * weights[2] + e4 * weights[3];
503}
504
Nick Deakin6bd90432022-11-20 16:26:37 -0500505uint32_t colorToRgba1010102(Color e_gamma) {
506 return (0x3ff & static_cast<uint32_t>(e_gamma.r * 1023.0f))
507 | ((0x3ff & static_cast<uint32_t>(e_gamma.g * 1023.0f)) << 10)
508 | ((0x3ff & static_cast<uint32_t>(e_gamma.b * 1023.0f)) << 20)
509 | (0x3 << 30); // Set alpha to 1.0
510}
511
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400512} // namespace android::recoverymap