blob: 7d29a3706e16f86fff551f0b070cfd9f77009a27 [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
Harish Mahendrakar1107ff32022-12-07 17:24:35 -080073static const float kMaxPixelFloat = 1.0f;
74static float clampPixelFloat(float value) {
75 return (value < 0.0f) ? 0.0f : (value > kMaxPixelFloat) ? kMaxPixelFloat : value;
76}
77
Nick Deakin65f492a2022-11-29 22:47:40 -050078// See IEC 61966-2-1, Equation F.7.
79static const float kSrgbR = 0.2126f, kSrgbG = 0.7152f, kSrgbB = 0.0722f;
Nick Deakin594a4ca2022-11-16 20:57:42 -050080
81float srgbLuminance(Color e) {
82 return kSrgbR * e.r + kSrgbG * e.g + kSrgbB * e.b;
Nick Deakinf6bca5a2022-11-04 10:43:43 -040083}
84
Nick Deakin65f492a2022-11-29 22:47:40 -050085// See ECMA TR/98, Section 7.
Nick Deakinf6bca5a2022-11-04 10:43:43 -040086static const float kSrgbRCr = 1.402f, kSrgbGCb = 0.34414f, kSrgbGCr = 0.71414f, kSrgbBCb = 1.772f;
87
Nick Deakin594a4ca2022-11-16 20:57:42 -050088Color srgbYuvToRgb(Color e_gamma) {
Harish Mahendrakar1107ff32022-12-07 17:24:35 -080089 return {{{ clampPixelFloat(e_gamma.y + kSrgbRCr * e_gamma.v),
90 clampPixelFloat(e_gamma.y - kSrgbGCb * e_gamma.u - kSrgbGCr * e_gamma.v),
91 clampPixelFloat(e_gamma.y + kSrgbBCb * e_gamma.u) }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -040092}
93
Nick Deakin65f492a2022-11-29 22:47:40 -050094// See ECMA TR/98, Section 7.
95static const float kSrgbYR = 0.299f, kSrgbYG = 0.587f, kSrgbYB = 0.114f;
Nick Deakin594a4ca2022-11-16 20:57:42 -050096static const float kSrgbUR = -0.1687f, kSrgbUG = -0.3313f, kSrgbUB = 0.5f;
97static const float kSrgbVR = 0.5f, kSrgbVG = -0.4187f, kSrgbVB = -0.0813f;
98
99Color srgbRgbToYuv(Color e_gamma) {
Nick Deakin65f492a2022-11-29 22:47:40 -0500100 return {{{ kSrgbYR * e_gamma.r + kSrgbYG * e_gamma.g + kSrgbYB * e_gamma.b,
Nick Deakin594a4ca2022-11-16 20:57:42 -0500101 kSrgbUR * e_gamma.r + kSrgbUG * e_gamma.g + kSrgbUB * e_gamma.b,
102 kSrgbVR * e_gamma.r + kSrgbVG * e_gamma.g + kSrgbVB * e_gamma.b }}};
103}
104
Nick Deakin65f492a2022-11-29 22:47:40 -0500105// See IEC 61966-2-1, Equations F.5 and F.6.
Nick Deakin594a4ca2022-11-16 20:57:42 -0500106float srgbInvOetf(float e_gamma) {
107 if (e_gamma <= 0.04045f) {
108 return e_gamma / 12.92f;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400109 } else {
Nick Deakin594a4ca2022-11-16 20:57:42 -0500110 return pow((e_gamma + 0.055f) / 1.055f, 2.4);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400111 }
112}
113
Nick Deakin594a4ca2022-11-16 20:57:42 -0500114Color srgbInvOetf(Color e_gamma) {
115 return {{{ srgbInvOetf(e_gamma.r),
116 srgbInvOetf(e_gamma.g),
117 srgbInvOetf(e_gamma.b) }}};
118}
119
120
121////////////////////////////////////////////////////////////////////////////////
122// Display-P3 transformations
123
Nick Deakin65f492a2022-11-29 22:47:40 -0500124// See SMPTE EG 432-1, Table 7-2.
125static const float kP3R = 0.20949f, kP3G = 0.72160f, kP3B = 0.06891f;
Nick Deakin6bd90432022-11-20 16:26:37 -0500126
127float p3Luminance(Color e) {
128 return kP3R * e.r + kP3G * e.g + kP3B * e.b;
129}
Nick Deakin594a4ca2022-11-16 20:57:42 -0500130
131
132////////////////////////////////////////////////////////////////////////////////
133// BT.2100 transformations - according to ITU-R BT.2100-2
134
Nick Deakin65f492a2022-11-29 22:47:40 -0500135// See ITU-R BT.2100-2, Table 5, HLG Reference OOTF
Nick Deakin594a4ca2022-11-16 20:57:42 -0500136static const float kBt2100R = 0.2627f, kBt2100G = 0.6780f, kBt2100B = 0.0593f;
137
138float bt2100Luminance(Color e) {
139 return kBt2100R * e.r + kBt2100G * e.g + kBt2100B * e.b;
140}
141
Nick Deakin65f492a2022-11-29 22:47:40 -0500142// See ITU-R BT.2100-2, Table 6, Derivation of colour difference signals.
Nick Deakin594a4ca2022-11-16 20:57:42 -0500143static const float kBt2100Cb = 1.8814f, kBt2100Cr = 1.4746f;
144
145Color bt2100RgbToYuv(Color e_gamma) {
146 float y_gamma = bt2100Luminance(e_gamma);
147 return {{{ y_gamma,
148 (e_gamma.b - y_gamma) / kBt2100Cb,
149 (e_gamma.r - y_gamma) / kBt2100Cr }}};
150}
151
Nick Deakin65f492a2022-11-29 22:47:40 -0500152// Derived by inversing bt2100RgbToYuv. The derivation for R and B are pretty
153// straight forward; we just invert the formulas for U and V above. But deriving
154// the formula for G is a bit more complicated:
Nick Deakin594a4ca2022-11-16 20:57:42 -0500155//
156// Start with equation for luminance:
157// Y = kBt2100R * R + kBt2100G * G + kBt2100B * B
158// Solve for G:
159// G = (Y - kBt2100R * R - kBt2100B * B) / kBt2100B
160// Substitute equations for R and B in terms YUV:
161// G = (Y - kBt2100R * (Y + kBt2100Cr * V) - kBt2100B * (Y + kBt2100Cb * U)) / kBt2100B
162// Simplify:
163// G = Y * ((1 - kBt2100R - kBt2100B) / kBt2100G)
164// + U * (kBt2100B * kBt2100Cb / kBt2100G)
165// + V * (kBt2100R * kBt2100Cr / kBt2100G)
166//
167// We then get the following coeficients for calculating G from YUV:
168//
169// Coef for Y = (1 - kBt2100R - kBt2100B) / kBt2100G = 1
170// Coef for U = kBt2100B * kBt2100Cb / kBt2100G = kBt2100GCb = ~0.1645
171// Coef for V = kBt2100R * kBt2100Cr / kBt2100G = kBt2100GCr = ~0.5713
172
173static const float kBt2100GCb = kBt2100B * kBt2100Cb / kBt2100G;
174static const float kBt2100GCr = kBt2100R * kBt2100Cr / kBt2100G;
175
176Color bt2100YuvToRgb(Color e_gamma) {
Harish Mahendrakar1107ff32022-12-07 17:24:35 -0800177 return {{{ clampPixelFloat(e_gamma.y + kBt2100Cr * e_gamma.v),
178 clampPixelFloat(e_gamma.y - kBt2100GCb * e_gamma.u - kBt2100GCr * e_gamma.v),
179 clampPixelFloat(e_gamma.y + kBt2100Cb * e_gamma.u) }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400180}
181
Nick Deakin65f492a2022-11-29 22:47:40 -0500182// See ITU-R BT.2100-2, Table 5, HLG Reference OETF.
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400183static const float kHlgA = 0.17883277f, kHlgB = 0.28466892f, kHlgC = 0.55991073;
184
Nick Deakin65f492a2022-11-29 22:47:40 -0500185float hlgOetf(float e) {
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400186 if (e <= 1.0f/12.0f) {
187 return sqrt(3.0f * e);
188 } else {
189 return kHlgA * log(12.0f * e - kHlgB) + kHlgC;
190 }
191}
192
193Color hlgOetf(Color e) {
194 return {{{ hlgOetf(e.r), hlgOetf(e.g), hlgOetf(e.b) }}};
195}
196
Nick Deakin65f492a2022-11-29 22:47:40 -0500197// See ITU-R BT.2100-2, Table 5, HLG Reference EOTF.
198float hlgInvOetf(float e_gamma) {
Nick Deakin594a4ca2022-11-16 20:57:42 -0500199 if (e_gamma <= 0.5f) {
200 return pow(e_gamma, 2.0f) / 3.0f;
201 } else {
202 return (exp((e_gamma - kHlgC) / kHlgA) + kHlgB) / 12.0f;
203 }
204}
205
206Color hlgInvOetf(Color e_gamma) {
207 return {{{ hlgInvOetf(e_gamma.r),
208 hlgInvOetf(e_gamma.g),
209 hlgInvOetf(e_gamma.b) }}};
210}
211
Nick Deakin65f492a2022-11-29 22:47:40 -0500212// See ITU-R BT.2100-2, Table 4, Reference PQ OETF.
Nick Deakin6bd90432022-11-20 16:26:37 -0500213static const float kPqM1 = 2610.0f / 16384.0f, kPqM2 = 2523.0f / 4096.0f * 128.0f;
214static const float kPqC1 = 3424.0f / 4096.0f, kPqC2 = 2413.0f / 4096.0f * 32.0f,
215 kPqC3 = 2392.0f / 4096.0f * 32.0f;
216
Nick Deakin65f492a2022-11-29 22:47:40 -0500217float pqOetf(float e) {
218 if (e <= 0.0f) return 0.0f;
219 return pow((kPqC1 + kPqC2 * pow(e, kPqM1)) / (1 + kPqC3 * pow(e, kPqM1)),
Nick Deakin6bd90432022-11-20 16:26:37 -0500220 kPqM2);
221}
222
223Color pqOetf(Color e) {
224 return {{{ pqOetf(e.r), pqOetf(e.g), pqOetf(e.b) }}};
225}
226
Nick Deakin65f492a2022-11-29 22:47:40 -0500227// Derived from the inverse of the Reference PQ OETF.
228static const float kPqInvA = 128.0f, kPqInvB = 107.0f, kPqInvC = 2413.0f, kPqInvD = 2392.0f,
229 kPqInvE = 6.2773946361f, kPqInvF = 0.0126833f;
230
231float pqInvOetf(float e_gamma) {
232 // This equation blows up if e_gamma is 0.0, and checking on <= 0.0 doesn't
233 // always catch 0.0. So, check on 0.0001, since anything this small will
234 // effectively be crushed to zero anyways.
235 if (e_gamma <= 0.0001f) return 0.0f;
236 return pow((kPqInvA * pow(e_gamma, kPqInvF) - kPqInvB)
237 / (kPqInvC - kPqInvD * pow(e_gamma, kPqInvF)),
238 kPqInvE);
Nick Deakin6bd90432022-11-20 16:26:37 -0500239}
240
241Color pqInvOetf(Color e_gamma) {
242 return {{{ pqInvOetf(e_gamma.r),
243 pqInvOetf(e_gamma.g),
244 pqInvOetf(e_gamma.b) }}};
245}
246
Nick Deakin594a4ca2022-11-16 20:57:42 -0500247
248////////////////////////////////////////////////////////////////////////////////
249// Color conversions
250
Nick Deakin6bd90432022-11-20 16:26:37 -0500251Color bt709ToP3(Color e) {
252 return {{{ 0.82254f * e.r + 0.17755f * e.g + 0.00006f * e.b,
253 0.03312f * e.r + 0.96684f * e.g + -0.00001f * e.b,
254 0.01706f * e.r + 0.07240f * e.g + 0.91049f * e.b }}};
255}
256
257Color bt709ToBt2100(Color e) {
258 return {{{ 0.62740f * e.r + 0.32930f * e.g + 0.04332f * e.b,
259 0.06904f * e.r + 0.91958f * e.g + 0.01138f * e.b,
260 0.01636f * e.r + 0.08799f * e.g + 0.89555f * e.b }}};
261}
262
263Color p3ToBt709(Color e) {
264 return {{{ 1.22482f * e.r + -0.22490f * e.g + -0.00007f * e.b,
265 -0.04196f * e.r + 1.04199f * e.g + 0.00001f * e.b,
266 -0.01961f * e.r + -0.07865f * e.g + 1.09831f * e.b }}};
267}
268
269Color p3ToBt2100(Color e) {
270 return {{{ 0.75378f * e.r + 0.19862f * e.g + 0.04754f * e.b,
271 0.04576f * e.r + 0.94177f * e.g + 0.01250f * e.b,
272 -0.00121f * e.r + 0.01757f * e.g + 0.98359f * e.b }}};
273}
274
275Color bt2100ToBt709(Color e) {
276 return {{{ 1.66045f * e.r + -0.58764f * e.g + -0.07286f * e.b,
277 -0.12445f * e.r + 1.13282f * e.g + -0.00837f * e.b,
278 -0.01811f * e.r + -0.10057f * e.g + 1.11878f * e.b }}};
279}
280
281Color bt2100ToP3(Color e) {
282 return {{{ 1.34369f * e.r + -0.28223f * e.g + -0.06135f * e.b,
283 -0.06533f * e.r + 1.07580f * e.g + -0.01051f * e.b,
284 0.00283f * e.r + -0.01957f * e.g + 1.01679f * e.b
285 }}};
286}
287
288// TODO: confirm we always want to convert like this before calculating
289// luminance.
290ColorTransformFn getHdrConversionFn(jpegr_color_gamut sdr_gamut, jpegr_color_gamut hdr_gamut) {
Nick Deakin65f492a2022-11-29 22:47:40 -0500291 switch (sdr_gamut) {
Nick Deakin6bd90432022-11-20 16:26:37 -0500292 case JPEGR_COLORGAMUT_BT709:
293 switch (hdr_gamut) {
294 case JPEGR_COLORGAMUT_BT709:
295 return identityConversion;
296 case JPEGR_COLORGAMUT_P3:
297 return p3ToBt709;
298 case JPEGR_COLORGAMUT_BT2100:
299 return bt2100ToBt709;
300 case JPEGR_COLORGAMUT_UNSPECIFIED:
301 return nullptr;
302 }
303 break;
304 case JPEGR_COLORGAMUT_P3:
305 switch (hdr_gamut) {
306 case JPEGR_COLORGAMUT_BT709:
307 return bt709ToP3;
308 case JPEGR_COLORGAMUT_P3:
309 return identityConversion;
310 case JPEGR_COLORGAMUT_BT2100:
311 return bt2100ToP3;
312 case JPEGR_COLORGAMUT_UNSPECIFIED:
313 return nullptr;
314 }
315 break;
316 case JPEGR_COLORGAMUT_BT2100:
317 switch (hdr_gamut) {
318 case JPEGR_COLORGAMUT_BT709:
319 return bt709ToBt2100;
320 case JPEGR_COLORGAMUT_P3:
321 return p3ToBt2100;
322 case JPEGR_COLORGAMUT_BT2100:
323 return identityConversion;
324 case JPEGR_COLORGAMUT_UNSPECIFIED:
325 return nullptr;
326 }
327 break;
328 case JPEGR_COLORGAMUT_UNSPECIFIED:
329 return nullptr;
330 }
331}
332
Nick Deakin594a4ca2022-11-16 20:57:42 -0500333
334////////////////////////////////////////////////////////////////////////////////
335// Recovery map calculations
336
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500337uint8_t encodeRecovery(float y_sdr, float y_hdr, float hdr_ratio) {
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400338 float gain = 1.0f;
339 if (y_sdr > 0.0f) {
340 gain = y_hdr / y_sdr;
341 }
342
Nick Deakin65f492a2022-11-29 22:47:40 -0500343 if (gain < (1.0f / hdr_ratio)) gain = 1.0f / hdr_ratio;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400344 if (gain > hdr_ratio) gain = hdr_ratio;
345
346 return static_cast<uint8_t>(log2(gain) / log2(hdr_ratio) * 127.5f + 127.5f);
347}
348
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500349Color applyRecovery(Color e, float recovery, float hdr_ratio) {
Harish Mahendrakara5ddcc22022-12-13 12:45:23 -0800350 float recoveryFactor = pow(hdr_ratio, recovery);
351 return e * recoveryFactor;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400352}
353
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400354Color getYuv420Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
355 size_t pixel_count = image->width * image->height;
356
357 size_t pixel_y_idx = x + y * image->width;
358 size_t pixel_uv_idx = x / 2 + (y / 2) * (image->width / 2);
359
360 uint8_t y_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y_idx];
361 uint8_t u_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count + pixel_uv_idx];
362 uint8_t v_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count * 5 / 4 + pixel_uv_idx];
363
364 // 128 bias for UV given we are using jpeglib; see:
365 // https://github.com/kornelski/libjpeg/blob/master/structure.doc
366 return {{{ static_cast<float>(y_uint) / 255.0f,
367 (static_cast<float>(u_uint) - 128.0f) / 255.0f,
368 (static_cast<float>(v_uint) - 128.0f) / 255.0f }}};
369}
370
Nick Deakin594a4ca2022-11-16 20:57:42 -0500371Color getP010Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
372 size_t pixel_count = image->width * image->height;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400373
Nick Deakin594a4ca2022-11-16 20:57:42 -0500374 size_t pixel_y_idx = x + y * image->width;
375 size_t pixel_uv_idx = x / 2 + (y / 2) * (image->width / 2);
376
Nick Deakin86207ba2022-11-21 16:07:36 -0500377 uint16_t y_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_y_idx]
378 >> 6;
379 uint16_t u_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_count + pixel_uv_idx * 2]
380 >> 6;
381 uint16_t v_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_count + pixel_uv_idx * 2 + 1]
382 >> 6;
Nick Deakin594a4ca2022-11-16 20:57:42 -0500383
384 // Conversions include taking narrow-range into account.
Nick Deakin38125332022-12-12 15:48:24 -0500385 return {{{ (static_cast<float>(y_uint) - 64.0f) / 876.0f,
386 (static_cast<float>(u_uint) - 64.0f) / 896.0f - 0.5f,
387 (static_cast<float>(v_uint) - 64.0f) / 896.0f - 0.5f }}};
Nick Deakin594a4ca2022-11-16 20:57:42 -0500388}
389
390typedef Color (*getPixelFn)(jr_uncompressed_ptr, size_t, size_t);
391
392static Color samplePixels(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y,
393 getPixelFn get_pixel_fn) {
394 Color e = {{{ 0.0f, 0.0f, 0.0f }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400395 for (size_t dy = 0; dy < map_scale_factor; ++dy) {
396 for (size_t dx = 0; dx < map_scale_factor; ++dx) {
Nick Deakin594a4ca2022-11-16 20:57:42 -0500397 e += get_pixel_fn(image, x * map_scale_factor + dx, y * map_scale_factor + dy);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400398 }
399 }
400
401 return e / static_cast<float>(map_scale_factor * map_scale_factor);
402}
403
Nick Deakin594a4ca2022-11-16 20:57:42 -0500404Color sampleYuv420(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) {
405 return samplePixels(image, map_scale_factor, x, y, getYuv420Pixel);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400406}
407
Nick Deakin594a4ca2022-11-16 20:57:42 -0500408Color sampleP010(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) {
409 return samplePixels(image, map_scale_factor, x, y, getP010Pixel);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400410}
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500411
Nick Deakin65f492a2022-11-29 22:47:40 -0500412// TODO: do we need something more clever for filtering either the map or images
413// to generate the map?
414
415static size_t clamp(const size_t& val, const size_t& low, const size_t& high) {
416 return val < low ? low : (high < val ? high : val);
417}
418
419static float mapUintToFloat(uint8_t map_uint) {
420 return (static_cast<float>(map_uint) - 127.5f) / 127.5f;
421}
422
423static float pythDistance(float x_diff, float y_diff) {
424 return sqrt(pow(x_diff, 2.0f) + pow(y_diff, 2.0f));
425}
426
Ram Mohanfe723d62022-12-15 00:59:11 +0530427// TODO: If map_scale_factor is guaranteed to be an integer, then remove the following.
Nick Deakin65f492a2022-11-29 22:47:40 -0500428float sampleMap(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y) {
429 float x_map = static_cast<float>(x) / static_cast<float>(map_scale_factor);
430 float y_map = static_cast<float>(y) / static_cast<float>(map_scale_factor);
431
432 size_t x_lower = static_cast<size_t>(floor(x_map));
433 size_t x_upper = x_lower + 1;
434 size_t y_lower = static_cast<size_t>(floor(y_map));
435 size_t y_upper = y_lower + 1;
436
437 x_lower = clamp(x_lower, 0, map->width - 1);
438 x_upper = clamp(x_upper, 0, map->width - 1);
439 y_lower = clamp(y_lower, 0, map->height - 1);
440 y_upper = clamp(y_upper, 0, map->height - 1);
441
442 // Use Shepard's method for inverse distance weighting. For more information:
443 // en.wikipedia.org/wiki/Inverse_distance_weighting#Shepard's_method
444
445 float e1 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_lower * map->width]);
446 float e1_dist = pythDistance(x_map - static_cast<float>(x_lower),
447 y_map - static_cast<float>(y_lower));
448 if (e1_dist == 0.0f) return e1;
449
450 float e2 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_upper * map->width]);
451 float e2_dist = pythDistance(x_map - static_cast<float>(x_lower),
452 y_map - static_cast<float>(y_upper));
453 if (e2_dist == 0.0f) return e2;
454
455 float e3 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_lower * map->width]);
456 float e3_dist = pythDistance(x_map - static_cast<float>(x_upper),
457 y_map - static_cast<float>(y_lower));
458 if (e3_dist == 0.0f) return e3;
459
460 float e4 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_upper * map->width]);
461 float e4_dist = pythDistance(x_map - static_cast<float>(x_upper),
462 y_map - static_cast<float>(y_upper));
463 if (e4_dist == 0.0f) return e2;
464
465 float e1_weight = 1.0f / e1_dist;
466 float e2_weight = 1.0f / e2_dist;
467 float e3_weight = 1.0f / e3_dist;
468 float e4_weight = 1.0f / e4_dist;
469 float total_weight = e1_weight + e2_weight + e3_weight + e4_weight;
470
471 return e1 * (e1_weight / total_weight)
472 + e2 * (e2_weight / total_weight)
473 + e3 * (e3_weight / total_weight)
474 + e4 * (e4_weight / total_weight);
475}
476
Ram Mohanfe723d62022-12-15 00:59:11 +0530477float sampleMap(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y,
478 ShepardsIDW& weightTables) {
479 // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the
480 // following by computing log2(map_scale_factor) once and then using >> log2(map_scale_factor)
481 int x_lower = x / map_scale_factor;
482 int x_upper = x_lower + 1;
483 int y_lower = y / map_scale_factor;
484 int y_upper = y_lower + 1;
485
486 x_lower = std::min(x_lower, map->width - 1);
487 x_upper = std::min(x_upper, map->width - 1);
488 y_lower = std::min(y_lower, map->height - 1);
489 y_upper = std::min(y_upper, map->height - 1);
490
491 float e1 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_lower * map->width]);
492 float e2 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_upper * map->width]);
493 float e3 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_lower * map->width]);
494 float e4 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_upper * map->width]);
495
496 // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the
497 // following by using & (map_scale_factor - 1)
498 int offset_x = x % map_scale_factor;
499 int offset_y = y % map_scale_factor;
500
501 float* weights = weightTables.mWeights;
502 if (x_lower == x_upper && y_lower == y_upper) weights = weightTables.mWeightsC;
503 else if (x_lower == x_upper) weights = weightTables.mWeightsNR;
504 else if (y_lower == y_upper) weights = weightTables.mWeightsNB;
505 weights += offset_y * map_scale_factor * 4 + offset_x * 4;
506
507 return e1 * weights[0] + e2 * weights[1] + e3 * weights[2] + e4 * weights[3];
508}
509
Nick Deakin6bd90432022-11-20 16:26:37 -0500510uint32_t colorToRgba1010102(Color e_gamma) {
511 return (0x3ff & static_cast<uint32_t>(e_gamma.r * 1023.0f))
512 | ((0x3ff & static_cast<uint32_t>(e_gamma.g * 1023.0f)) << 10)
513 | ((0x3ff & static_cast<uint32_t>(e_gamma.b * 1023.0f)) << 20)
514 | (0x3 << 30); // Set alpha to 1.0
515}
516
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400517} // namespace android::recoverymap