• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #include "ultrahdr/gainmapmath.h"
19 
20 namespace ultrahdr {
21 
__anon73d019660102null22 static const std::vector<float> kPqOETF = [] {
23   std::vector<float> result;
24   for (size_t idx = 0; idx < kPqOETFNumEntries; idx++) {
25     float value = static_cast<float>(idx) / static_cast<float>(kPqOETFNumEntries - 1);
26     result.push_back(pqOetf(value));
27   }
28   return result;
29 }();
30 
__anon73d019660202null31 static const std::vector<float> kPqInvOETF = [] {
32   std::vector<float> result;
33   for (size_t idx = 0; idx < kPqInvOETFNumEntries; idx++) {
34     float value = static_cast<float>(idx) / static_cast<float>(kPqInvOETFNumEntries - 1);
35     result.push_back(pqInvOetf(value));
36   }
37   return result;
38 }();
39 
__anon73d019660302null40 static const std::vector<float> kHlgOETF = [] {
41   std::vector<float> result;
42   for (size_t idx = 0; idx < kHlgOETFNumEntries; idx++) {
43     float value = static_cast<float>(idx) / static_cast<float>(kHlgOETFNumEntries - 1);
44     result.push_back(hlgOetf(value));
45   }
46   return result;
47 }();
48 
__anon73d019660402null49 static const std::vector<float> kHlgInvOETF = [] {
50   std::vector<float> result;
51   for (size_t idx = 0; idx < kHlgInvOETFNumEntries; idx++) {
52     float value = static_cast<float>(idx) / static_cast<float>(kHlgInvOETFNumEntries - 1);
53     result.push_back(hlgInvOetf(value));
54   }
55   return result;
56 }();
57 
__anon73d019660502null58 static const std::vector<float> kSrgbInvOETF = [] {
59   std::vector<float> result;
60   for (size_t idx = 0; idx < kSrgbInvOETFNumEntries; idx++) {
61     float value = static_cast<float>(idx) / static_cast<float>(kSrgbInvOETFNumEntries - 1);
62     result.push_back(srgbInvOetf(value));
63   }
64   return result;
65 }();
66 
67 // Use Shepard's method for inverse distance weighting. For more information:
68 // en.wikipedia.org/wiki/Inverse_distance_weighting#Shepard's_method
69 
euclideanDistance(float x1,float x2,float y1,float y2)70 float ShepardsIDW::euclideanDistance(float x1, float x2, float y1, float y2) {
71   return sqrt(((y2 - y1) * (y2 - y1)) + (x2 - x1) * (x2 - x1));
72 }
73 
fillShepardsIDW(float * weights,int incR,int incB)74 void ShepardsIDW::fillShepardsIDW(float* weights, int incR, int incB) {
75   for (int y = 0; y < mMapScaleFactor; y++) {
76     for (int x = 0; x < mMapScaleFactor; x++) {
77       float pos_x = ((float)x) / mMapScaleFactor;
78       float pos_y = ((float)y) / mMapScaleFactor;
79       int curr_x = floor(pos_x);
80       int curr_y = floor(pos_y);
81       int next_x = curr_x + incR;
82       int next_y = curr_y + incB;
83       float e1_distance = euclideanDistance(pos_x, curr_x, pos_y, curr_y);
84       int index = y * mMapScaleFactor * 4 + x * 4;
85       if (e1_distance == 0) {
86         weights[index++] = 1.f;
87         weights[index++] = 0.f;
88         weights[index++] = 0.f;
89         weights[index++] = 0.f;
90       } else {
91         float e1_weight = 1.f / e1_distance;
92 
93         float e2_distance = euclideanDistance(pos_x, curr_x, pos_y, next_y);
94         float e2_weight = 1.f / e2_distance;
95 
96         float e3_distance = euclideanDistance(pos_x, next_x, pos_y, curr_y);
97         float e3_weight = 1.f / e3_distance;
98 
99         float e4_distance = euclideanDistance(pos_x, next_x, pos_y, next_y);
100         float e4_weight = 1.f / e4_distance;
101 
102         float total_weight = e1_weight + e2_weight + e3_weight + e4_weight;
103 
104         weights[index++] = e1_weight / total_weight;
105         weights[index++] = e2_weight / total_weight;
106         weights[index++] = e3_weight / total_weight;
107         weights[index++] = e4_weight / total_weight;
108       }
109     }
110   }
111 }
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 // sRGB transformations
115 
116 // See IEC 61966-2-1/Amd 1:2003, Equation F.7.
117 static const float kSrgbR = 0.2126f, kSrgbG = 0.7152f, kSrgbB = 0.0722f;
118 
srgbLuminance(Color e)119 float srgbLuminance(Color e) { return kSrgbR * e.r + kSrgbG * e.g + kSrgbB * e.b; }
120 
121 // See ITU-R BT.709-6, Section 3.
122 // Uses the same coefficients for deriving luma signal as
123 // IEC 61966-2-1/Amd 1:2003 states for luminance, so we reuse the luminance
124 // function above.
125 static const float kSrgbCb = 1.8556f, kSrgbCr = 1.5748f;
126 
srgbRgbToYuv(Color e_gamma)127 Color srgbRgbToYuv(Color e_gamma) {
128   float y_gamma = srgbLuminance(e_gamma);
129   return {{{y_gamma, (e_gamma.b - y_gamma) / kSrgbCb, (e_gamma.r - y_gamma) / kSrgbCr}}};
130 }
131 
132 // See ITU-R BT.709-6, Section 3.
133 // Same derivation to BT.2100's YUV->RGB, below. Similar to srgbRgbToYuv, we
134 // can reuse the luminance coefficients since they are the same.
135 static const float kSrgbGCb = kSrgbB * kSrgbCb / kSrgbG;
136 static const float kSrgbGCr = kSrgbR * kSrgbCr / kSrgbG;
137 
srgbYuvToRgb(Color e_gamma)138 Color srgbYuvToRgb(Color e_gamma) {
139   return {{{clampPixelFloat(e_gamma.y + kSrgbCr * e_gamma.v),
140             clampPixelFloat(e_gamma.y - kSrgbGCb * e_gamma.u - kSrgbGCr * e_gamma.v),
141             clampPixelFloat(e_gamma.y + kSrgbCb * e_gamma.u)}}};
142 }
143 
144 // See IEC 61966-2-1/Amd 1:2003, Equations F.5 and F.6.
srgbInvOetf(float e_gamma)145 float srgbInvOetf(float e_gamma) {
146   if (e_gamma <= 0.04045f) {
147     return e_gamma / 12.92f;
148   } else {
149     return pow((e_gamma + 0.055f) / 1.055f, 2.4);
150   }
151 }
152 
srgbInvOetf(Color e_gamma)153 Color srgbInvOetf(Color e_gamma) {
154   return {{{srgbInvOetf(e_gamma.r), srgbInvOetf(e_gamma.g), srgbInvOetf(e_gamma.b)}}};
155 }
156 
157 // See IEC 61966-2-1, Equations F.5 and F.6.
srgbInvOetfLUT(float e_gamma)158 float srgbInvOetfLUT(float e_gamma) {
159   int32_t value = static_cast<int32_t>(e_gamma * (kSrgbInvOETFNumEntries - 1) + 0.5);
160   // TODO() : Remove once conversion modules have appropriate clamping in place
161   value = CLIP3(value, 0, kSrgbInvOETFNumEntries - 1);
162   return kSrgbInvOETF[value];
163 }
164 
srgbInvOetfLUT(Color e_gamma)165 Color srgbInvOetfLUT(Color e_gamma) {
166   return {{{srgbInvOetfLUT(e_gamma.r), srgbInvOetfLUT(e_gamma.g), srgbInvOetfLUT(e_gamma.b)}}};
167 }
168 
srgbOetf(float e)169 float srgbOetf(float e) {
170   constexpr float kThreshold = 0.00304;
171   constexpr float kLowSlope = 12.92;
172   constexpr float kHighOffset = 0.055;
173   constexpr float kPowerExponent = 1.0 / 2.4;
174   if (e < kThreshold) {
175     return kLowSlope * e;
176   }
177   return (1.0 + kHighOffset) * std::pow(e, kPowerExponent) - kHighOffset;
178 }
179 
srgbOetf(Color e)180 Color srgbOetf(Color e) { return {{{srgbOetf(e.r), srgbOetf(e.g), srgbOetf(e.b)}}}; }
181 
182 ////////////////////////////////////////////////////////////////////////////////
183 // Display-P3 transformations
184 
185 // See SMPTE EG 432-1, Equation 7-8.
186 static const float kP3R = 0.20949f, kP3G = 0.72160f, kP3B = 0.06891f;
187 
p3Luminance(Color e)188 float p3Luminance(Color e) { return kP3R * e.r + kP3G * e.g + kP3B * e.b; }
189 
190 // See ITU-R BT.601-7, Sections 2.5.1 and 2.5.2.
191 // Unfortunately, calculation of luma signal differs from calculation of
192 // luminance for Display-P3, so we can't reuse p3Luminance here.
193 static const float kP3YR = 0.299f, kP3YG = 0.587f, kP3YB = 0.114f;
194 static const float kP3Cb = 1.772f, kP3Cr = 1.402f;
195 
p3RgbToYuv(Color e_gamma)196 Color p3RgbToYuv(Color e_gamma) {
197   float y_gamma = kP3YR * e_gamma.r + kP3YG * e_gamma.g + kP3YB * e_gamma.b;
198   return {{{y_gamma, (e_gamma.b - y_gamma) / kP3Cb, (e_gamma.r - y_gamma) / kP3Cr}}};
199 }
200 
201 // See ITU-R BT.601-7, Sections 2.5.1 and 2.5.2.
202 // Same derivation to BT.2100's YUV->RGB, below. Similar to p3RgbToYuv, we must
203 // use luma signal coefficients rather than the luminance coefficients.
204 static const float kP3GCb = kP3YB * kP3Cb / kP3YG;
205 static const float kP3GCr = kP3YR * kP3Cr / kP3YG;
206 
p3YuvToRgb(Color e_gamma)207 Color p3YuvToRgb(Color e_gamma) {
208   return {{{clampPixelFloat(e_gamma.y + kP3Cr * e_gamma.v),
209             clampPixelFloat(e_gamma.y - kP3GCb * e_gamma.u - kP3GCr * e_gamma.v),
210             clampPixelFloat(e_gamma.y + kP3Cb * e_gamma.u)}}};
211 }
212 
213 ////////////////////////////////////////////////////////////////////////////////
214 // BT.2100 transformations - according to ITU-R BT.2100-2
215 
216 // See ITU-R BT.2100-2, Table 5, HLG Reference OOTF
217 static const float kBt2100R = 0.2627f, kBt2100G = 0.6780f, kBt2100B = 0.0593f;
218 
bt2100Luminance(Color e)219 float bt2100Luminance(Color e) { return kBt2100R * e.r + kBt2100G * e.g + kBt2100B * e.b; }
220 
221 // See ITU-R BT.2100-2, Table 6, Derivation of colour difference signals.
222 // BT.2100 uses the same coefficients for calculating luma signal and luminance,
223 // so we reuse the luminance function here.
224 static const float kBt2100Cb = 1.8814f, kBt2100Cr = 1.4746f;
225 
bt2100RgbToYuv(Color e_gamma)226 Color bt2100RgbToYuv(Color e_gamma) {
227   float y_gamma = bt2100Luminance(e_gamma);
228   return {{{y_gamma, (e_gamma.b - y_gamma) / kBt2100Cb, (e_gamma.r - y_gamma) / kBt2100Cr}}};
229 }
230 
231 // See ITU-R BT.2100-2, Table 6, Derivation of colour difference signals.
232 //
233 // Similar to bt2100RgbToYuv above, we can reuse the luminance coefficients.
234 //
235 // Derived by inversing bt2100RgbToYuv. The derivation for R and B are  pretty
236 // straight forward; we just invert the formulas for U and V above. But deriving
237 // the formula for G is a bit more complicated:
238 //
239 // Start with equation for luminance:
240 //   Y = kBt2100R * R + kBt2100G * G + kBt2100B * B
241 // Solve for G:
242 //   G = (Y - kBt2100R * R - kBt2100B * B) / kBt2100B
243 // Substitute equations for R and B in terms YUV:
244 //   G = (Y - kBt2100R * (Y + kBt2100Cr * V) - kBt2100B * (Y + kBt2100Cb * U)) / kBt2100B
245 // Simplify:
246 //   G = Y * ((1 - kBt2100R - kBt2100B) / kBt2100G)
247 //     + U * (kBt2100B * kBt2100Cb / kBt2100G)
248 //     + V * (kBt2100R * kBt2100Cr / kBt2100G)
249 //
250 // We then get the following coeficients for calculating G from YUV:
251 //
252 // Coef for Y = (1 - kBt2100R - kBt2100B) / kBt2100G = 1
253 // Coef for U = kBt2100B * kBt2100Cb / kBt2100G = kBt2100GCb = ~0.1645
254 // Coef for V = kBt2100R * kBt2100Cr / kBt2100G = kBt2100GCr = ~0.5713
255 
256 static const float kBt2100GCb = kBt2100B * kBt2100Cb / kBt2100G;
257 static const float kBt2100GCr = kBt2100R * kBt2100Cr / kBt2100G;
258 
bt2100YuvToRgb(Color e_gamma)259 Color bt2100YuvToRgb(Color e_gamma) {
260   return {{{clampPixelFloat(e_gamma.y + kBt2100Cr * e_gamma.v),
261             clampPixelFloat(e_gamma.y - kBt2100GCb * e_gamma.u - kBt2100GCr * e_gamma.v),
262             clampPixelFloat(e_gamma.y + kBt2100Cb * e_gamma.u)}}};
263 }
264 
265 // See ITU-R BT.2100-2, Table 5, HLG Reference OETF.
266 static const float kHlgA = 0.17883277f, kHlgB = 0.28466892f, kHlgC = 0.55991073;
267 
hlgOetf(float e)268 float hlgOetf(float e) {
269   if (e <= 1.0f / 12.0f) {
270     return sqrt(3.0f * e);
271   } else {
272     return kHlgA * log(12.0f * e - kHlgB) + kHlgC;
273   }
274 }
275 
hlgOetf(Color e)276 Color hlgOetf(Color e) { return {{{hlgOetf(e.r), hlgOetf(e.g), hlgOetf(e.b)}}}; }
277 
hlgOetfLUT(float e)278 float hlgOetfLUT(float e) {
279   int32_t value = static_cast<int32_t>(e * (kHlgOETFNumEntries - 1) + 0.5);
280   // TODO() : Remove once conversion modules have appropriate clamping in place
281   value = CLIP3(value, 0, kHlgOETFNumEntries - 1);
282 
283   return kHlgOETF[value];
284 }
285 
hlgOetfLUT(Color e)286 Color hlgOetfLUT(Color e) { return {{{hlgOetfLUT(e.r), hlgOetfLUT(e.g), hlgOetfLUT(e.b)}}}; }
287 
288 // See ITU-R BT.2100-2, Table 5, HLG Reference EOTF.
hlgInvOetf(float e_gamma)289 float hlgInvOetf(float e_gamma) {
290   if (e_gamma <= 0.5f) {
291     return pow(e_gamma, 2.0f) / 3.0f;
292   } else {
293     return (exp((e_gamma - kHlgC) / kHlgA) + kHlgB) / 12.0f;
294   }
295 }
296 
hlgInvOetf(Color e_gamma)297 Color hlgInvOetf(Color e_gamma) {
298   return {{{hlgInvOetf(e_gamma.r), hlgInvOetf(e_gamma.g), hlgInvOetf(e_gamma.b)}}};
299 }
300 
hlgInvOetfLUT(float e_gamma)301 float hlgInvOetfLUT(float e_gamma) {
302   int32_t value = static_cast<int32_t>(e_gamma * (kHlgInvOETFNumEntries - 1) + 0.5);
303   // TODO() : Remove once conversion modules have appropriate clamping in place
304   value = CLIP3(value, 0, kHlgInvOETFNumEntries - 1);
305 
306   return kHlgInvOETF[value];
307 }
308 
hlgInvOetfLUT(Color e_gamma)309 Color hlgInvOetfLUT(Color e_gamma) {
310   return {{{hlgInvOetfLUT(e_gamma.r), hlgInvOetfLUT(e_gamma.g), hlgInvOetfLUT(e_gamma.b)}}};
311 }
312 
313 // See ITU-R BT.2100-2, Table 4, Reference PQ OETF.
314 static const float kPqM1 = 2610.0f / 16384.0f, kPqM2 = 2523.0f / 4096.0f * 128.0f;
315 static const float kPqC1 = 3424.0f / 4096.0f, kPqC2 = 2413.0f / 4096.0f * 32.0f,
316                    kPqC3 = 2392.0f / 4096.0f * 32.0f;
317 
pqOetf(float e)318 float pqOetf(float e) {
319   if (e <= 0.0f) return 0.0f;
320   return pow((kPqC1 + kPqC2 * pow(e, kPqM1)) / (1 + kPqC3 * pow(e, kPqM1)), kPqM2);
321 }
322 
pqOetf(Color e)323 Color pqOetf(Color e) { return {{{pqOetf(e.r), pqOetf(e.g), pqOetf(e.b)}}}; }
324 
pqOetfLUT(float e)325 float pqOetfLUT(float e) {
326   int32_t value = static_cast<int32_t>(e * (kPqOETFNumEntries - 1) + 0.5);
327   // TODO() : Remove once conversion modules have appropriate clamping in place
328   value = CLIP3(value, 0, kPqOETFNumEntries - 1);
329 
330   return kPqOETF[value];
331 }
332 
pqOetfLUT(Color e)333 Color pqOetfLUT(Color e) { return {{{pqOetfLUT(e.r), pqOetfLUT(e.g), pqOetfLUT(e.b)}}}; }
334 
335 // Derived from the inverse of the Reference PQ OETF.
336 static const float kPqInvA = 128.0f, kPqInvB = 107.0f, kPqInvC = 2413.0f, kPqInvD = 2392.0f,
337                    kPqInvE = 6.2773946361f, kPqInvF = 0.0126833f;
338 
pqInvOetf(float e_gamma)339 float pqInvOetf(float e_gamma) {
340   // This equation blows up if e_gamma is 0.0, and checking on <= 0.0 doesn't
341   // always catch 0.0. So, check on 0.0001, since anything this small will
342   // effectively be crushed to zero anyways.
343   if (e_gamma <= 0.0001f) return 0.0f;
344   return pow(
345       (kPqInvA * pow(e_gamma, kPqInvF) - kPqInvB) / (kPqInvC - kPqInvD * pow(e_gamma, kPqInvF)),
346       kPqInvE);
347 }
348 
pqInvOetf(Color e_gamma)349 Color pqInvOetf(Color e_gamma) {
350   return {{{pqInvOetf(e_gamma.r), pqInvOetf(e_gamma.g), pqInvOetf(e_gamma.b)}}};
351 }
352 
pqInvOetfLUT(float e_gamma)353 float pqInvOetfLUT(float e_gamma) {
354   int32_t value = static_cast<int32_t>(e_gamma * (kPqInvOETFNumEntries - 1) + 0.5);
355   // TODO() : Remove once conversion modules have appropriate clamping in place
356   value = CLIP3(value, 0, kPqInvOETFNumEntries - 1);
357 
358   return kPqInvOETF[value];
359 }
360 
pqInvOetfLUT(Color e_gamma)361 Color pqInvOetfLUT(Color e_gamma) {
362   return {{{pqInvOetfLUT(e_gamma.r), pqInvOetfLUT(e_gamma.g), pqInvOetfLUT(e_gamma.b)}}};
363 }
364 
365 ////////////////////////////////////////////////////////////////////////////////
366 // Color conversions
367 
bt709ToP3(Color e)368 Color bt709ToP3(Color e) {
369   return {{{0.82254f * e.r + 0.17755f * e.g + 0.00006f * e.b,
370             0.03312f * e.r + 0.96684f * e.g + -0.00001f * e.b,
371             0.01706f * e.r + 0.07240f * e.g + 0.91049f * e.b}}};
372 }
373 
bt709ToBt2100(Color e)374 Color bt709ToBt2100(Color e) {
375   return {{{0.62740f * e.r + 0.32930f * e.g + 0.04332f * e.b,
376             0.06904f * e.r + 0.91958f * e.g + 0.01138f * e.b,
377             0.01636f * e.r + 0.08799f * e.g + 0.89555f * e.b}}};
378 }
379 
p3ToBt709(Color e)380 Color p3ToBt709(Color e) {
381   return {{{1.22482f * e.r + -0.22490f * e.g + -0.00007f * e.b,
382             -0.04196f * e.r + 1.04199f * e.g + 0.00001f * e.b,
383             -0.01961f * e.r + -0.07865f * e.g + 1.09831f * e.b}}};
384 }
385 
p3ToBt2100(Color e)386 Color p3ToBt2100(Color e) {
387   return {{{0.75378f * e.r + 0.19862f * e.g + 0.04754f * e.b,
388             0.04576f * e.r + 0.94177f * e.g + 0.01250f * e.b,
389             -0.00121f * e.r + 0.01757f * e.g + 0.98359f * e.b}}};
390 }
391 
bt2100ToBt709(Color e)392 Color bt2100ToBt709(Color e) {
393   return {{{1.66045f * e.r + -0.58764f * e.g + -0.07286f * e.b,
394             -0.12445f * e.r + 1.13282f * e.g + -0.00837f * e.b,
395             -0.01811f * e.r + -0.10057f * e.g + 1.11878f * e.b}}};
396 }
397 
bt2100ToP3(Color e)398 Color bt2100ToP3(Color e) {
399   return {{{1.34369f * e.r + -0.28223f * e.g + -0.06135f * e.b,
400             -0.06533f * e.r + 1.07580f * e.g + -0.01051f * e.b,
401             0.00283f * e.r + -0.01957f * e.g + 1.01679f * e.b}}};
402 }
403 
404 // TODO: confirm we always want to convert like this before calculating
405 // luminance.
getHdrConversionFn(ultrahdr_color_gamut sdr_gamut,ultrahdr_color_gamut hdr_gamut)406 ColorTransformFn getHdrConversionFn(ultrahdr_color_gamut sdr_gamut,
407                                     ultrahdr_color_gamut hdr_gamut) {
408   switch (sdr_gamut) {
409     case ULTRAHDR_COLORGAMUT_BT709:
410       switch (hdr_gamut) {
411         case ULTRAHDR_COLORGAMUT_BT709:
412           return identityConversion;
413         case ULTRAHDR_COLORGAMUT_P3:
414           return p3ToBt709;
415         case ULTRAHDR_COLORGAMUT_BT2100:
416           return bt2100ToBt709;
417         case ULTRAHDR_COLORGAMUT_UNSPECIFIED:
418           return nullptr;
419       }
420       break;
421     case ULTRAHDR_COLORGAMUT_P3:
422       switch (hdr_gamut) {
423         case ULTRAHDR_COLORGAMUT_BT709:
424           return bt709ToP3;
425         case ULTRAHDR_COLORGAMUT_P3:
426           return identityConversion;
427         case ULTRAHDR_COLORGAMUT_BT2100:
428           return bt2100ToP3;
429         case ULTRAHDR_COLORGAMUT_UNSPECIFIED:
430           return nullptr;
431       }
432       break;
433     case ULTRAHDR_COLORGAMUT_BT2100:
434       switch (hdr_gamut) {
435         case ULTRAHDR_COLORGAMUT_BT709:
436           return bt709ToBt2100;
437         case ULTRAHDR_COLORGAMUT_P3:
438           return p3ToBt2100;
439         case ULTRAHDR_COLORGAMUT_BT2100:
440           return identityConversion;
441         case ULTRAHDR_COLORGAMUT_UNSPECIFIED:
442           return nullptr;
443       }
444       break;
445     case ULTRAHDR_COLORGAMUT_UNSPECIFIED:
446       return nullptr;
447   }
448   return nullptr;
449 }
450 
451 // All of these conversions are derived from the respective input YUV->RGB conversion followed by
452 // the RGB->YUV for the receiving encoding. They are consistent with the RGB<->YUV functions in
453 // gainmapmath.cpp, given that we use BT.709 encoding for sRGB and BT.601 encoding for Display-P3,
454 // to match DataSpace.
455 
456 // Yuv Bt709 -> Yuv Bt601
457 // Y' = (1.0 * Y) + ( 0.101579 * U) + ( 0.196076 * V)
458 // U' = (0.0 * Y) + ( 0.989854 * U) + (-0.110653 * V)
459 // V' = (0.0 * Y) + (-0.072453 * U) + ( 0.983398 * V)
460 const std::array<float, 9> kYuvBt709ToBt601 = {
461     1.0f, 0.101579f, 0.196076f, 0.0f, 0.989854f, -0.110653f, 0.0f, -0.072453f, 0.983398f};
462 
463 // Yuv Bt709 -> Yuv Bt2100
464 // Y' = (1.0 * Y) + (-0.016969 * U) + ( 0.096312 * V)
465 // U' = (0.0 * Y) + ( 0.995306 * U) + (-0.051192 * V)
466 // V' = (0.0 * Y) + ( 0.011507 * U) + ( 1.002637 * V)
467 const std::array<float, 9> kYuvBt709ToBt2100 = {
468     1.0f, -0.016969f, 0.096312f, 0.0f, 0.995306f, -0.051192f, 0.0f, 0.011507f, 1.002637f};
469 
470 // Yuv Bt601 -> Yuv Bt709
471 // Y' = (1.0 * Y) + (-0.118188 * U) + (-0.212685 * V)
472 // U' = (0.0 * Y) + ( 1.018640 * U) + ( 0.114618 * V)
473 // V' = (0.0 * Y) + ( 0.075049 * U) + ( 1.025327 * V)
474 const std::array<float, 9> kYuvBt601ToBt709 = {
475     1.0f, -0.118188f, -0.212685f, 0.0f, 1.018640f, 0.114618f, 0.0f, 0.075049f, 1.025327f};
476 
477 // Yuv Bt601 -> Yuv Bt2100
478 // Y' = (1.0 * Y) + (-0.128245 * U) + (-0.115879 * V)
479 // U' = (0.0 * Y) + ( 1.010016 * U) + ( 0.061592 * V)
480 // V' = (0.0 * Y) + ( 0.086969 * U) + ( 1.029350 * V)
481 const std::array<float, 9> kYuvBt601ToBt2100 = {
482     1.0f, -0.128245f, -0.115879, 0.0f, 1.010016f, 0.061592f, 0.0f, 0.086969f, 1.029350f};
483 
484 // Yuv Bt2100 -> Yuv Bt709
485 // Y' = (1.0 * Y) + ( 0.018149 * U) + (-0.095132 * V)
486 // U' = (0.0 * Y) + ( 1.004123 * U) + ( 0.051267 * V)
487 // V' = (0.0 * Y) + (-0.011524 * U) + ( 0.996782 * V)
488 const std::array<float, 9> kYuvBt2100ToBt709 = {
489     1.0f, 0.018149f, -0.095132f, 0.0f, 1.004123f, 0.051267f, 0.0f, -0.011524f, 0.996782f};
490 
491 // Yuv Bt2100 -> Yuv Bt601
492 // Y' = (1.0 * Y) + ( 0.117887 * U) + ( 0.105521 * V)
493 // U' = (0.0 * Y) + ( 0.995211 * U) + (-0.059549 * V)
494 // V' = (0.0 * Y) + (-0.084085 * U) + ( 0.976518 * V)
495 const std::array<float, 9> kYuvBt2100ToBt601 = {
496     1.0f, 0.117887f, 0.105521f, 0.0f, 0.995211f, -0.059549f, 0.0f, -0.084085f, 0.976518f};
497 
yuvColorGamutConversion(Color e_gamma,const std::array<float,9> & coeffs)498 Color yuvColorGamutConversion(Color e_gamma, const std::array<float, 9>& coeffs) {
499   const float y = e_gamma.y * std::get<0>(coeffs) + e_gamma.u * std::get<1>(coeffs) +
500                   e_gamma.v * std::get<2>(coeffs);
501   const float u = e_gamma.y * std::get<3>(coeffs) + e_gamma.u * std::get<4>(coeffs) +
502                   e_gamma.v * std::get<5>(coeffs);
503   const float v = e_gamma.y * std::get<6>(coeffs) + e_gamma.u * std::get<7>(coeffs) +
504                   e_gamma.v * std::get<8>(coeffs);
505   return {{{y, u, v}}};
506 }
507 
transformYuv420(jr_uncompressed_ptr image,const std::array<float,9> & coeffs)508 void transformYuv420(jr_uncompressed_ptr image, const std::array<float, 9>& coeffs) {
509   for (size_t y = 0; y < image->height / 2; ++y) {
510     for (size_t x = 0; x < image->width / 2; ++x) {
511       Color yuv1 = getYuv420Pixel(image, x * 2, y * 2);
512       Color yuv2 = getYuv420Pixel(image, x * 2 + 1, y * 2);
513       Color yuv3 = getYuv420Pixel(image, x * 2, y * 2 + 1);
514       Color yuv4 = getYuv420Pixel(image, x * 2 + 1, y * 2 + 1);
515 
516       yuv1 = yuvColorGamutConversion(yuv1, coeffs);
517       yuv2 = yuvColorGamutConversion(yuv2, coeffs);
518       yuv3 = yuvColorGamutConversion(yuv3, coeffs);
519       yuv4 = yuvColorGamutConversion(yuv4, coeffs);
520 
521       Color new_uv = (yuv1 + yuv2 + yuv3 + yuv4) / 4.0f;
522 
523       size_t pixel_y1_idx = x * 2 + y * 2 * image->luma_stride;
524       size_t pixel_y2_idx = (x * 2 + 1) + y * 2 * image->luma_stride;
525       size_t pixel_y3_idx = x * 2 + (y * 2 + 1) * image->luma_stride;
526       size_t pixel_y4_idx = (x * 2 + 1) + (y * 2 + 1) * image->luma_stride;
527 
528       uint8_t& y1_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y1_idx];
529       uint8_t& y2_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y2_idx];
530       uint8_t& y3_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y3_idx];
531       uint8_t& y4_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y4_idx];
532 
533       size_t pixel_count = image->chroma_stride * image->height / 2;
534       size_t pixel_uv_idx = x + y * (image->chroma_stride);
535 
536       uint8_t& u_uint = reinterpret_cast<uint8_t*>(image->chroma_data)[pixel_uv_idx];
537       uint8_t& v_uint = reinterpret_cast<uint8_t*>(image->chroma_data)[pixel_count + pixel_uv_idx];
538 
539       y1_uint = static_cast<uint8_t>(CLIP3((yuv1.y * 255.0f + 0.5f), 0, 255));
540       y2_uint = static_cast<uint8_t>(CLIP3((yuv2.y * 255.0f + 0.5f), 0, 255));
541       y3_uint = static_cast<uint8_t>(CLIP3((yuv3.y * 255.0f + 0.5f), 0, 255));
542       y4_uint = static_cast<uint8_t>(CLIP3((yuv4.y * 255.0f + 0.5f), 0, 255));
543 
544       u_uint = static_cast<uint8_t>(CLIP3((new_uv.u * 255.0f + 128.0f + 0.5f), 0, 255));
545       v_uint = static_cast<uint8_t>(CLIP3((new_uv.v * 255.0f + 128.0f + 0.5f), 0, 255));
546     }
547   }
548 }
549 
550 ////////////////////////////////////////////////////////////////////////////////
551 // Gain map calculations
encodeGain(float y_sdr,float y_hdr,ultrahdr_metadata_ptr metadata)552 uint8_t encodeGain(float y_sdr, float y_hdr, ultrahdr_metadata_ptr metadata) {
553   return encodeGain(y_sdr, y_hdr, metadata, log2(metadata->minContentBoost),
554                     log2(metadata->maxContentBoost));
555 }
556 
encodeGain(float y_sdr,float y_hdr,ultrahdr_metadata_ptr metadata,float log2MinContentBoost,float log2MaxContentBoost)557 uint8_t encodeGain(float y_sdr, float y_hdr, ultrahdr_metadata_ptr metadata,
558                    float log2MinContentBoost, float log2MaxContentBoost) {
559   float gain = 1.0f;
560   if (y_sdr > 0.0f) {
561     gain = y_hdr / y_sdr;
562   }
563 
564   if (gain < metadata->minContentBoost) gain = metadata->minContentBoost;
565   if (gain > metadata->maxContentBoost) gain = metadata->maxContentBoost;
566 
567   return static_cast<uint8_t>((log2(gain) - log2MinContentBoost) /
568                               (log2MaxContentBoost - log2MinContentBoost) * 255.0f);
569 }
570 
applyGain(Color e,float gain,ultrahdr_metadata_ptr metadata)571 Color applyGain(Color e, float gain, ultrahdr_metadata_ptr metadata) {
572   float logBoost =
573       log2(metadata->minContentBoost) * (1.0f - gain) + log2(metadata->maxContentBoost) * gain;
574   float gainFactor = exp2(logBoost);
575   return e * gainFactor;
576 }
577 
applyGain(Color e,float gain,ultrahdr_metadata_ptr metadata,float displayBoost)578 Color applyGain(Color e, float gain, ultrahdr_metadata_ptr metadata, float displayBoost) {
579   float logBoost =
580       log2(metadata->minContentBoost) * (1.0f - gain) + log2(metadata->maxContentBoost) * gain;
581   float gainFactor = exp2(logBoost * displayBoost / metadata->maxContentBoost);
582   return e * gainFactor;
583 }
584 
applyGainLUT(Color e,float gain,GainLUT & gainLUT)585 Color applyGainLUT(Color e, float gain, GainLUT& gainLUT) {
586   float gainFactor = gainLUT.getGainFactor(gain);
587   return e * gainFactor;
588 }
589 
applyGain(Color e,Color gain,ultrahdr_metadata_ptr metadata)590 Color applyGain(Color e, Color gain, ultrahdr_metadata_ptr metadata) {
591   float logBoostR =
592       log2(metadata->minContentBoost) * (1.0f - gain.r) + log2(metadata->maxContentBoost) * gain.r;
593   float logBoostG =
594       log2(metadata->minContentBoost) * (1.0f - gain.g) + log2(metadata->maxContentBoost) * gain.g;
595   float logBoostB =
596       log2(metadata->minContentBoost) * (1.0f - gain.b) + log2(metadata->maxContentBoost) * gain.b;
597   float gainFactorR = exp2(logBoostR);
598   float gainFactorG = exp2(logBoostG);
599   float gainFactorB = exp2(logBoostB);
600   return {{{e.r * gainFactorR, e.g * gainFactorG, e.b * gainFactorB}}};
601 }
602 
applyGain(Color e,Color gain,ultrahdr_metadata_ptr metadata,float displayBoost)603 Color applyGain(Color e, Color gain, ultrahdr_metadata_ptr metadata, float displayBoost) {
604   float logBoostR =
605       log2(metadata->minContentBoost) * (1.0f - gain.r) + log2(metadata->maxContentBoost) * gain.r;
606   float logBoostG =
607       log2(metadata->minContentBoost) * (1.0f - gain.g) + log2(metadata->maxContentBoost) * gain.g;
608   float logBoostB =
609       log2(metadata->minContentBoost) * (1.0f - gain.b) + log2(metadata->maxContentBoost) * gain.b;
610   float gainFactorR = exp2(logBoostR * displayBoost / metadata->maxContentBoost);
611   float gainFactorG = exp2(logBoostG * displayBoost / metadata->maxContentBoost);
612   float gainFactorB = exp2(logBoostB * displayBoost / metadata->maxContentBoost);
613   return {{{e.r * gainFactorR, e.g * gainFactorG, e.b * gainFactorB}}};
614 }
615 
applyGainLUT(Color e,Color gain,GainLUT & gainLUT)616 Color applyGainLUT(Color e, Color gain, GainLUT& gainLUT) {
617   float gainFactorR = gainLUT.getGainFactor(gain.r);
618   float gainFactorG = gainLUT.getGainFactor(gain.g);
619   float gainFactorB = gainLUT.getGainFactor(gain.b);
620   return {{{e.r * gainFactorR, e.g * gainFactorG, e.b * gainFactorB}}};
621 }
622 
getYuv420Pixel(jr_uncompressed_ptr image,size_t x,size_t y)623 Color getYuv420Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
624   uint8_t* luma_data = reinterpret_cast<uint8_t*>(image->data);
625   size_t luma_stride = image->luma_stride;
626   uint8_t* chroma_data = reinterpret_cast<uint8_t*>(image->chroma_data);
627   size_t chroma_stride = image->chroma_stride;
628 
629   size_t offset_cr = chroma_stride * (image->height / 2);
630   size_t pixel_y_idx = x + y * luma_stride;
631   size_t pixel_chroma_idx = x / 2 + (y / 2) * chroma_stride;
632 
633   uint8_t y_uint = luma_data[pixel_y_idx];
634   uint8_t u_uint = chroma_data[pixel_chroma_idx];
635   uint8_t v_uint = chroma_data[offset_cr + pixel_chroma_idx];
636 
637   // 128 bias for UV given we are using jpeglib; see:
638   // https://github.com/kornelski/libjpeg/blob/master/structure.doc
639   return {
640       {{static_cast<float>(y_uint) * (1 / 255.0f), static_cast<float>(u_uint - 128) * (1 / 255.0f),
641         static_cast<float>(v_uint - 128) * (1 / 255.0f)}}};
642 }
643 
getP010Pixel(jr_uncompressed_ptr image,size_t x,size_t y)644 Color getP010Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
645   uint16_t* luma_data = reinterpret_cast<uint16_t*>(image->data);
646   size_t luma_stride = image->luma_stride == 0 ? image->width : image->luma_stride;
647   uint16_t* chroma_data = reinterpret_cast<uint16_t*>(image->chroma_data);
648   size_t chroma_stride = image->chroma_stride;
649 
650   size_t pixel_y_idx = y * luma_stride + x;
651   size_t pixel_u_idx = (y >> 1) * chroma_stride + (x & ~0x1);
652   size_t pixel_v_idx = pixel_u_idx + 1;
653 
654   uint16_t y_uint = luma_data[pixel_y_idx] >> 6;
655   uint16_t u_uint = chroma_data[pixel_u_idx] >> 6;
656   uint16_t v_uint = chroma_data[pixel_v_idx] >> 6;
657 
658   // Conversions include taking narrow-range into account.
659   return {{{static_cast<float>(y_uint - 64) * (1 / 876.0f),
660             static_cast<float>(u_uint - 64) * (1 / 896.0f) - 0.5f,
661             static_cast<float>(v_uint - 64) * (1 / 896.0f) - 0.5f}}};
662 }
663 
664 typedef Color (*getPixelFn)(jr_uncompressed_ptr, size_t, size_t);
665 
samplePixels(jr_uncompressed_ptr image,size_t map_scale_factor,size_t x,size_t y,getPixelFn get_pixel_fn)666 static Color samplePixels(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y,
667                           getPixelFn get_pixel_fn) {
668   Color e = {{{0.0f, 0.0f, 0.0f}}};
669   for (size_t dy = 0; dy < map_scale_factor; ++dy) {
670     for (size_t dx = 0; dx < map_scale_factor; ++dx) {
671       e += get_pixel_fn(image, x * map_scale_factor + dx, y * map_scale_factor + dy);
672     }
673   }
674 
675   return e / static_cast<float>(map_scale_factor * map_scale_factor);
676 }
677 
sampleYuv420(jr_uncompressed_ptr image,size_t map_scale_factor,size_t x,size_t y)678 Color sampleYuv420(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) {
679   return samplePixels(image, map_scale_factor, x, y, getYuv420Pixel);
680 }
681 
sampleP010(jr_uncompressed_ptr image,size_t map_scale_factor,size_t x,size_t y)682 Color sampleP010(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) {
683   return samplePixels(image, map_scale_factor, x, y, getP010Pixel);
684 }
685 
686 // TODO: do we need something more clever for filtering either the map or images
687 // to generate the map?
688 
clamp(const size_t & val,const size_t & low,const size_t & high)689 static size_t clamp(const size_t& val, const size_t& low, const size_t& high) {
690   return val < low ? low : (high < val ? high : val);
691 }
692 
mapUintToFloat(uint8_t map_uint)693 static float mapUintToFloat(uint8_t map_uint) { return static_cast<float>(map_uint) / 255.0f; }
694 
pythDistance(float x_diff,float y_diff)695 static float pythDistance(float x_diff, float y_diff) {
696   return sqrt(pow(x_diff, 2.0f) + pow(y_diff, 2.0f));
697 }
698 
699 // TODO: If map_scale_factor is guaranteed to be an integer, then remove the following.
sampleMap(jr_uncompressed_ptr map,float map_scale_factor,size_t x,size_t y)700 float sampleMap(jr_uncompressed_ptr map, float map_scale_factor, size_t x, size_t y) {
701   float x_map = static_cast<float>(x) / map_scale_factor;
702   float y_map = static_cast<float>(y) / map_scale_factor;
703 
704   size_t x_lower = static_cast<size_t>(floor(x_map));
705   size_t x_upper = x_lower + 1;
706   size_t y_lower = static_cast<size_t>(floor(y_map));
707   size_t y_upper = y_lower + 1;
708 
709   x_lower = clamp(x_lower, 0, map->width - 1);
710   x_upper = clamp(x_upper, 0, map->width - 1);
711   y_lower = clamp(y_lower, 0, map->height - 1);
712   y_upper = clamp(y_upper, 0, map->height - 1);
713 
714   // Use Shepard's method for inverse distance weighting. For more information:
715   // en.wikipedia.org/wiki/Inverse_distance_weighting#Shepard's_method
716 
717   float e1 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_lower * map->width]);
718   float e1_dist =
719       pythDistance(x_map - static_cast<float>(x_lower), y_map - static_cast<float>(y_lower));
720   if (e1_dist == 0.0f) return e1;
721 
722   float e2 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_upper * map->width]);
723   float e2_dist =
724       pythDistance(x_map - static_cast<float>(x_lower), y_map - static_cast<float>(y_upper));
725   if (e2_dist == 0.0f) return e2;
726 
727   float e3 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_lower * map->width]);
728   float e3_dist =
729       pythDistance(x_map - static_cast<float>(x_upper), y_map - static_cast<float>(y_lower));
730   if (e3_dist == 0.0f) return e3;
731 
732   float e4 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_upper * map->width]);
733   float e4_dist =
734       pythDistance(x_map - static_cast<float>(x_upper), y_map - static_cast<float>(y_upper));
735   if (e4_dist == 0.0f) return e2;
736 
737   float e1_weight = 1.0f / e1_dist;
738   float e2_weight = 1.0f / e2_dist;
739   float e3_weight = 1.0f / e3_dist;
740   float e4_weight = 1.0f / e4_dist;
741   float total_weight = e1_weight + e2_weight + e3_weight + e4_weight;
742 
743   return e1 * (e1_weight / total_weight) + e2 * (e2_weight / total_weight) +
744          e3 * (e3_weight / total_weight) + e4 * (e4_weight / total_weight);
745 }
746 
sampleMap(jr_uncompressed_ptr map,size_t map_scale_factor,size_t x,size_t y,ShepardsIDW & weightTables)747 float sampleMap(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y,
748                 ShepardsIDW& weightTables) {
749   // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the
750   // following by computing log2(map_scale_factor) once and then using >> log2(map_scale_factor)
751   size_t x_lower = x / map_scale_factor;
752   size_t x_upper = x_lower + 1;
753   size_t y_lower = y / map_scale_factor;
754   size_t y_upper = y_lower + 1;
755 
756   x_lower = std::min(x_lower, map->width - 1);
757   x_upper = std::min(x_upper, map->width - 1);
758   y_lower = std::min(y_lower, map->height - 1);
759   y_upper = std::min(y_upper, map->height - 1);
760 
761   float e1 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_lower * map->width]);
762   float e2 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_upper * map->width]);
763   float e3 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_lower * map->width]);
764   float e4 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_upper * map->width]);
765 
766   // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the
767   // following by using & (map_scale_factor - 1)
768   int offset_x = x % map_scale_factor;
769   int offset_y = y % map_scale_factor;
770 
771   float* weights = weightTables.mWeights;
772   if (x_lower == x_upper && y_lower == y_upper)
773     weights = weightTables.mWeightsC;
774   else if (x_lower == x_upper)
775     weights = weightTables.mWeightsNR;
776   else if (y_lower == y_upper)
777     weights = weightTables.mWeightsNB;
778   weights += offset_y * map_scale_factor * 4 + offset_x * 4;
779 
780   return e1 * weights[0] + e2 * weights[1] + e3 * weights[2] + e4 * weights[3];
781 }
782 
sampleMap3Channel(jr_uncompressed_ptr map,float map_scale_factor,size_t x,size_t y,bool has_alpha)783 Color sampleMap3Channel(jr_uncompressed_ptr map, float map_scale_factor, size_t x, size_t y,
784                         bool has_alpha) {
785   float x_map = static_cast<float>(x) / map_scale_factor;
786   float y_map = static_cast<float>(y) / map_scale_factor;
787 
788   size_t x_lower = static_cast<size_t>(floor(x_map));
789   size_t x_upper = x_lower + 1;
790   size_t y_lower = static_cast<size_t>(floor(y_map));
791   size_t y_upper = y_lower + 1;
792 
793   x_lower = std::min(x_lower, map->width - 1);
794   x_upper = std::min(x_upper, map->width - 1);
795   y_lower = std::min(y_lower, map->height - 1);
796   y_upper = std::min(y_upper, map->height - 1);
797 
798   int factor = has_alpha ? 4 : 3;
799 
800   float r1 = mapUintToFloat(
801       reinterpret_cast<uint8_t*>(map->data)[(x_lower + y_lower * map->width) * factor]);
802   float r2 = mapUintToFloat(
803       reinterpret_cast<uint8_t*>(map->data)[(x_lower + y_upper * map->width) * factor]);
804   float r3 = mapUintToFloat(
805       reinterpret_cast<uint8_t*>(map->data)[(x_upper + y_lower * map->width) * factor]);
806   float r4 = mapUintToFloat(
807       reinterpret_cast<uint8_t*>(map->data)[(x_upper + y_upper * map->width) * factor]);
808 
809   float g1 = mapUintToFloat(
810       reinterpret_cast<uint8_t*>(map->data)[(x_lower + y_lower * map->width) * factor + 1]);
811   float g2 = mapUintToFloat(
812       reinterpret_cast<uint8_t*>(map->data)[(x_lower + y_upper * map->width) * factor + 1]);
813   float g3 = mapUintToFloat(
814       reinterpret_cast<uint8_t*>(map->data)[(x_upper + y_lower * map->width) * factor + 1]);
815   float g4 = mapUintToFloat(
816       reinterpret_cast<uint8_t*>(map->data)[(x_upper + y_upper * map->width) * factor + 1]);
817 
818   float b1 = mapUintToFloat(
819       reinterpret_cast<uint8_t*>(map->data)[(x_lower + y_lower * map->width) * factor + 2]);
820   float b2 = mapUintToFloat(
821       reinterpret_cast<uint8_t*>(map->data)[(x_lower + y_upper * map->width) * factor + 2]);
822   float b3 = mapUintToFloat(
823       reinterpret_cast<uint8_t*>(map->data)[(x_upper + y_lower * map->width) * factor + 2]);
824   float b4 = mapUintToFloat(
825       reinterpret_cast<uint8_t*>(map->data)[(x_upper + y_upper * map->width) * factor + 2]);
826 
827   Color rgb1 = {{{r1, g1, b1}}};
828   Color rgb2 = {{{r2, g2, b2}}};
829   Color rgb3 = {{{r3, g3, b3}}};
830   Color rgb4 = {{{r4, g4, b4}}};
831 
832   // Use Shepard's method for inverse distance weighting. For more information:
833   // en.wikipedia.org/wiki/Inverse_distance_weighting#Shepard's_method
834   float e1_dist =
835       pythDistance(x_map - static_cast<float>(x_lower), y_map - static_cast<float>(y_lower));
836   if (e1_dist == 0.0f) return rgb1;
837 
838   float e2_dist =
839       pythDistance(x_map - static_cast<float>(x_lower), y_map - static_cast<float>(y_upper));
840   if (e2_dist == 0.0f) return rgb2;
841 
842   float e3_dist =
843       pythDistance(x_map - static_cast<float>(x_upper), y_map - static_cast<float>(y_lower));
844   if (e3_dist == 0.0f) return rgb3;
845 
846   float e4_dist =
847       pythDistance(x_map - static_cast<float>(x_upper), y_map - static_cast<float>(y_upper));
848   if (e4_dist == 0.0f) return rgb4;
849 
850   float e1_weight = 1.0f / e1_dist;
851   float e2_weight = 1.0f / e2_dist;
852   float e3_weight = 1.0f / e3_dist;
853   float e4_weight = 1.0f / e4_dist;
854   float total_weight = e1_weight + e2_weight + e3_weight + e4_weight;
855 
856   return rgb1 * (e1_weight / total_weight) + rgb2 * (e2_weight / total_weight) +
857          rgb3 * (e3_weight / total_weight) + rgb4 * (e4_weight / total_weight);
858 }
859 
sampleMap3Channel(jr_uncompressed_ptr map,size_t map_scale_factor,size_t x,size_t y,ShepardsIDW & weightTables,bool has_alpha)860 Color sampleMap3Channel(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y,
861                         ShepardsIDW& weightTables, bool has_alpha) {
862   // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the
863   // following by computing log2(map_scale_factor) once and then using >> log2(map_scale_factor)
864   size_t x_lower = x / map_scale_factor;
865   size_t x_upper = x_lower + 1;
866   size_t y_lower = y / map_scale_factor;
867   size_t y_upper = y_lower + 1;
868 
869   x_lower = std::min(x_lower, map->width - 1);
870   x_upper = std::min(x_upper, map->width - 1);
871   y_lower = std::min(y_lower, map->height - 1);
872   y_upper = std::min(y_upper, map->height - 1);
873 
874   int factor = has_alpha ? 4 : 3;
875 
876   float r1 = mapUintToFloat(
877       reinterpret_cast<uint8_t*>(map->data)[(x_lower + y_lower * map->width) * factor]);
878   float r2 = mapUintToFloat(
879       reinterpret_cast<uint8_t*>(map->data)[(x_lower + y_upper * map->width) * factor]);
880   float r3 = mapUintToFloat(
881       reinterpret_cast<uint8_t*>(map->data)[(x_upper + y_lower * map->width) * factor]);
882   float r4 = mapUintToFloat(
883       reinterpret_cast<uint8_t*>(map->data)[(x_upper + y_upper * map->width) * factor]);
884 
885   float g1 = mapUintToFloat(
886       reinterpret_cast<uint8_t*>(map->data)[(x_lower + y_lower * map->width) * factor + 1]);
887   float g2 = mapUintToFloat(
888       reinterpret_cast<uint8_t*>(map->data)[(x_lower + y_upper * map->width) * factor + 1]);
889   float g3 = mapUintToFloat(
890       reinterpret_cast<uint8_t*>(map->data)[(x_upper + y_lower * map->width) * factor + 1]);
891   float g4 = mapUintToFloat(
892       reinterpret_cast<uint8_t*>(map->data)[(x_upper + y_upper * map->width) * factor + 1]);
893 
894   float b1 = mapUintToFloat(
895       reinterpret_cast<uint8_t*>(map->data)[(x_lower + y_lower * map->width) * factor + 2]);
896   float b2 = mapUintToFloat(
897       reinterpret_cast<uint8_t*>(map->data)[(x_lower + y_upper * map->width) * factor + 2]);
898   float b3 = mapUintToFloat(
899       reinterpret_cast<uint8_t*>(map->data)[(x_upper + y_lower * map->width) * factor + 2]);
900   float b4 = mapUintToFloat(
901       reinterpret_cast<uint8_t*>(map->data)[(x_upper + y_upper * map->width) * factor + 2]);
902 
903   Color rgb1 = {{{r1, g1, b1}}};
904   Color rgb2 = {{{r2, g2, b2}}};
905   Color rgb3 = {{{r3, g3, b3}}};
906   Color rgb4 = {{{r4, g4, b4}}};
907 
908   // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the
909   // following by using & (map_scale_factor - 1)
910   int offset_x = x % map_scale_factor;
911   int offset_y = y % map_scale_factor;
912 
913   float* weights = weightTables.mWeights;
914   if (x_lower == x_upper && y_lower == y_upper)
915     weights = weightTables.mWeightsC;
916   else if (x_lower == x_upper)
917     weights = weightTables.mWeightsNR;
918   else if (y_lower == y_upper)
919     weights = weightTables.mWeightsNB;
920   weights += offset_y * map_scale_factor * 4 + offset_x * 4;
921 
922   return rgb1 * weights[0] + rgb2 * weights[1] + rgb3 * weights[2] + rgb4 * weights[3];
923 }
924 
colorToRgba1010102(Color e_gamma)925 uint32_t colorToRgba1010102(Color e_gamma) {
926   return (0x3ff & static_cast<uint32_t>(e_gamma.r * 1023.0f)) |
927          ((0x3ff & static_cast<uint32_t>(e_gamma.g * 1023.0f)) << 10) |
928          ((0x3ff & static_cast<uint32_t>(e_gamma.b * 1023.0f)) << 20) |
929          (0x3 << 30);  // Set alpha to 1.0
930 }
931 
colorToRgbaF16(Color e_gamma)932 uint64_t colorToRgbaF16(Color e_gamma) {
933   return (uint64_t)floatToHalf(e_gamma.r) | (((uint64_t)floatToHalf(e_gamma.g)) << 16) |
934          (((uint64_t)floatToHalf(e_gamma.b)) << 32) | (((uint64_t)floatToHalf(1.0f)) << 48);
935 }
936 
convert_raw_input_to_ycbcr(uhdr_raw_image_t * src)937 std::unique_ptr<uhdr_raw_image_ext_t> convert_raw_input_to_ycbcr(uhdr_raw_image_t* src) {
938   std::unique_ptr<uhdr_raw_image_ext_t> dst = nullptr;
939   Color (*rgbToyuv)(Color) = nullptr;
940 
941   if (src->fmt == UHDR_IMG_FMT_32bppRGBA1010102 || src->fmt == UHDR_IMG_FMT_32bppRGBA8888) {
942     if (src->cg == UHDR_CG_BT_709) {
943       rgbToyuv = srgbRgbToYuv;
944     } else if (src->cg == UHDR_CG_BT_2100) {
945       rgbToyuv = bt2100RgbToYuv;
946     } else if (src->cg == UHDR_CG_DISPLAY_P3) {
947       rgbToyuv = p3RgbToYuv;
948     } else {
949       return dst;
950     }
951   }
952 
953   if (src->fmt == UHDR_IMG_FMT_32bppRGBA1010102) {
954     dst = std::make_unique<uhdr_raw_image_ext_t>(UHDR_IMG_FMT_24bppYCbCrP010, src->cg, src->ct,
955                                                  UHDR_CR_LIMITED_RANGE, src->w, src->h, 64);
956 
957     uint32_t* rgbData = static_cast<uint32_t*>(src->planes[UHDR_PLANE_PACKED]);
958     unsigned int srcStride = src->stride[UHDR_PLANE_PACKED];
959 
960     uint16_t* yData = static_cast<uint16_t*>(dst->planes[UHDR_PLANE_Y]);
961     uint16_t* uData = static_cast<uint16_t*>(dst->planes[UHDR_PLANE_UV]);
962     uint16_t* vData = uData + 1;
963 
964     for (size_t i = 0; i < dst->h; i += 2) {
965       for (size_t j = 0; j < dst->w; j += 2) {
966         Color pixel[4];
967 
968         pixel[0].r = float(rgbData[srcStride * i + j] & 0x3ff);
969         pixel[0].g = float((rgbData[srcStride * i + j] >> 10) & 0x3ff);
970         pixel[0].b = float((rgbData[srcStride * i + j] >> 20) & 0x3ff);
971 
972         pixel[1].r = float(rgbData[srcStride * i + j + 1] & 0x3ff);
973         pixel[1].g = float((rgbData[srcStride * i + j + 1] >> 10) & 0x3ff);
974         pixel[1].b = float((rgbData[srcStride * i + j + 1] >> 20) & 0x3ff);
975 
976         pixel[2].r = float(rgbData[srcStride * (i + 1) + j] & 0x3ff);
977         pixel[2].g = float((rgbData[srcStride * (i + 1) + j] >> 10) & 0x3ff);
978         pixel[2].b = float((rgbData[srcStride * (i + 1) + j] >> 20) & 0x3ff);
979 
980         pixel[3].r = float(rgbData[srcStride * (i + 1) + j + 1] & 0x3ff);
981         pixel[3].g = float((rgbData[srcStride * (i + 1) + j + 1] >> 10) & 0x3ff);
982         pixel[3].b = float((rgbData[srcStride * (i + 1) + j + 1] >> 20) & 0x3ff);
983 
984         for (int k = 0; k < 4; k++) {
985           pixel[k] /= 1023.0f;
986           pixel[k] = (*rgbToyuv)(pixel[k]);
987 
988           pixel[k].y = (pixel[k].y * 876.0f) + 64.0f + 0.5f;
989           pixel[k].y = CLIP3(pixel[k].y, 64.0f, 940.0f);
990         }
991 
992         yData[dst->stride[UHDR_PLANE_Y] * i + j] = uint16_t(pixel[0].y) << 6;
993         yData[dst->stride[UHDR_PLANE_Y] * i + j + 1] = uint16_t(pixel[1].y) << 6;
994         yData[dst->stride[UHDR_PLANE_Y] * (i + 1) + j] = uint16_t(pixel[2].y) << 6;
995         yData[dst->stride[UHDR_PLANE_Y] * (i + 1) + j + 1] = uint16_t(pixel[3].y) << 6;
996 
997         pixel[0].u = (pixel[0].u + pixel[1].u + pixel[2].u + pixel[3].u) / 4;
998         pixel[0].v = (pixel[0].v + pixel[1].v + pixel[2].v + pixel[3].v) / 4;
999 
1000         pixel[0].u = (pixel[0].u * 896.0f) + 512.0f + 0.5f;
1001         pixel[0].v = (pixel[0].v * 896.0f) + 512.0f + 0.5f;
1002 
1003         pixel[0].u = CLIP3(pixel[0].u, 64.0f, 960.0f);
1004         pixel[0].v = CLIP3(pixel[0].v, 64.0f, 960.0f);
1005 
1006         uData[dst->stride[UHDR_PLANE_UV] * (i / 2) + j] = uint16_t(pixel[0].u) << 6;
1007         vData[dst->stride[UHDR_PLANE_UV] * (i / 2) + j] = uint16_t(pixel[0].v) << 6;
1008       }
1009     }
1010   } else if (src->fmt == UHDR_IMG_FMT_32bppRGBA8888) {
1011     dst = std::make_unique<uhdr_raw_image_ext_t>(UHDR_IMG_FMT_12bppYCbCr420, src->cg, src->ct,
1012                                                  UHDR_CR_FULL_RANGE, src->w, src->h, 64);
1013     uint32_t* rgbData = static_cast<uint32_t*>(src->planes[UHDR_PLANE_PACKED]);
1014     unsigned int srcStride = src->stride[UHDR_PLANE_PACKED];
1015 
1016     uint8_t* yData = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_Y]);
1017     uint8_t* uData = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_U]);
1018     uint8_t* vData = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_V]);
1019     for (size_t i = 0; i < dst->h; i += 2) {
1020       for (size_t j = 0; j < dst->w; j += 2) {
1021         Color pixel[4];
1022 
1023         pixel[0].r = float(rgbData[srcStride * i + j] & 0xff);
1024         pixel[0].g = float((rgbData[srcStride * i + j] >> 8) & 0xff);
1025         pixel[0].b = float((rgbData[srcStride * i + j] >> 16) & 0xff);
1026 
1027         pixel[1].r = float(rgbData[srcStride * i + (j + 1)] & 0xff);
1028         pixel[1].g = float((rgbData[srcStride * i + (j + 1)] >> 8) & 0xff);
1029         pixel[1].b = float((rgbData[srcStride * i + (j + 1)] >> 16) & 0xff);
1030 
1031         pixel[2].r = float(rgbData[srcStride * (i + 1) + j] & 0xff);
1032         pixel[2].g = float((rgbData[srcStride * (i + 1) + j] >> 8) & 0xff);
1033         pixel[2].b = float((rgbData[srcStride * (i + 1) + j] >> 16) & 0xff);
1034 
1035         pixel[3].r = float(rgbData[srcStride * (i + 1) + (j + 1)] & 0xff);
1036         pixel[3].g = float((rgbData[srcStride * (i + 1) + (j + 1)] >> 8) & 0xff);
1037         pixel[3].b = float((rgbData[srcStride * (i + 1) + (j + 1)] >> 16) & 0xff);
1038 
1039         for (int k = 0; k < 4; k++) {
1040           pixel[k] /= 255.0f;
1041           pixel[k] = (*rgbToyuv)(pixel[k]);
1042 
1043           pixel[k].y = pixel[k].y * 255.0f + 0.5f;
1044           pixel[k].y = CLIP3(pixel[k].y, 0.0f, 255.0f);
1045         }
1046         yData[dst->stride[UHDR_PLANE_Y] * i + j] = uint8_t(pixel[0].y);
1047         yData[dst->stride[UHDR_PLANE_Y] * i + j + 1] = uint8_t(pixel[1].y);
1048         yData[dst->stride[UHDR_PLANE_Y] * (i + 1) + j] = uint8_t(pixel[2].y);
1049         yData[dst->stride[UHDR_PLANE_Y] * (i + 1) + j + 1] = uint8_t(pixel[3].y);
1050 
1051         pixel[0].u = (pixel[0].u + pixel[1].u + pixel[2].u + pixel[3].u) / 4;
1052         pixel[0].v = (pixel[0].v + pixel[1].v + pixel[2].v + pixel[3].v) / 4;
1053 
1054         pixel[0].u = pixel[0].u * 255.0f + 0.5 + 128.0f;
1055         pixel[0].v = pixel[0].v * 255.0f + 0.5 + 128.0f;
1056 
1057         pixel[0].u = CLIP3(pixel[0].u, 0.0f, 255.0f);
1058         pixel[0].v = CLIP3(pixel[0].v, 0.0f, 255.0f);
1059 
1060         uData[dst->stride[UHDR_PLANE_U] * (i / 2) + (j / 2)] = uint8_t(pixel[0].u);
1061         vData[dst->stride[UHDR_PLANE_V] * (i / 2) + (j / 2)] = uint8_t(pixel[0].v);
1062       }
1063     }
1064   } else if (src->fmt == UHDR_IMG_FMT_12bppYCbCr420) {
1065     dst = std::make_unique<ultrahdr::uhdr_raw_image_ext_t>(src->fmt, src->cg, src->ct, src->range,
1066                                                            src->w, src->h, 64);
1067 
1068     uint8_t* y_dst = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_Y]);
1069     uint8_t* y_src = static_cast<uint8_t*>(src->planes[UHDR_PLANE_Y]);
1070     uint8_t* u_dst = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_U]);
1071     uint8_t* u_src = static_cast<uint8_t*>(src->planes[UHDR_PLANE_U]);
1072     uint8_t* v_dst = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_V]);
1073     uint8_t* v_src = static_cast<uint8_t*>(src->planes[UHDR_PLANE_V]);
1074 
1075     // copy y
1076     for (size_t i = 0; i < src->h; i++) {
1077       memcpy(y_dst, y_src, src->w);
1078       y_dst += dst->stride[UHDR_PLANE_Y];
1079       y_src += src->stride[UHDR_PLANE_Y];
1080     }
1081     // copy cb & cr
1082     for (size_t i = 0; i < src->h / 2; i++) {
1083       memcpy(u_dst, u_src, src->w / 2);
1084       memcpy(v_dst, v_src, src->w / 2);
1085       u_dst += dst->stride[UHDR_PLANE_U];
1086       v_dst += dst->stride[UHDR_PLANE_V];
1087       u_src += src->stride[UHDR_PLANE_U];
1088       v_src += src->stride[UHDR_PLANE_V];
1089     }
1090   } else if (src->fmt == UHDR_IMG_FMT_24bppYCbCrP010) {
1091     dst = std::make_unique<ultrahdr::uhdr_raw_image_ext_t>(src->fmt, src->cg, src->ct, src->range,
1092                                                            src->w, src->h, 64);
1093 
1094     int bpp = 2;
1095     uint8_t* y_dst = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_Y]);
1096     uint8_t* y_src = static_cast<uint8_t*>(src->planes[UHDR_PLANE_Y]);
1097     uint8_t* uv_dst = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_UV]);
1098     uint8_t* uv_src = static_cast<uint8_t*>(src->planes[UHDR_PLANE_UV]);
1099 
1100     // copy y
1101     for (size_t i = 0; i < src->h; i++) {
1102       memcpy(y_dst, y_src, src->w * bpp);
1103       y_dst += (dst->stride[UHDR_PLANE_Y] * bpp);
1104       y_src += (src->stride[UHDR_PLANE_Y] * bpp);
1105     }
1106     // copy cbcr
1107     for (size_t i = 0; i < src->h / 2; i++) {
1108       memcpy(uv_dst, uv_src, src->w * bpp);
1109       uv_dst += (dst->stride[UHDR_PLANE_UV] * bpp);
1110       uv_src += (src->stride[UHDR_PLANE_UV] * bpp);
1111     }
1112   }
1113   return dst;
1114 }
1115 // Use double type for intermediate results for better precision.
floatToUnsignedFractionImpl(float v,uint32_t maxNumerator,uint32_t * numerator,uint32_t * denominator)1116 static bool floatToUnsignedFractionImpl(float v, uint32_t maxNumerator, uint32_t* numerator,
1117                                         uint32_t* denominator) {
1118   if (std::isnan(v) || v < 0 || v > maxNumerator) {
1119     return false;
1120   }
1121 
1122   // Maximum denominator: makes sure that the numerator is <= maxNumerator and the denominator
1123   // is <= UINT32_MAX.
1124   const uint64_t maxD = (v <= 1) ? UINT32_MAX : (uint64_t)floor(maxNumerator / v);
1125 
1126   // Find the best approximation of v as a fraction using continued fractions, see
1127   // https://en.wikipedia.org/wiki/Continued_fraction
1128   *denominator = 1;
1129   uint32_t previousD = 0;
1130   double currentV = (double)v - floor(v);
1131   int iter = 0;
1132   // Set a maximum number of iterations to be safe. Most numbers should
1133   // converge in less than ~20 iterations.
1134   // The golden ratio is the worst case and takes 39 iterations.
1135   const int maxIter = 39;
1136   while (iter < maxIter) {
1137     const double numeratorDouble = (double)(*denominator) * v;
1138     if (numeratorDouble > maxNumerator) {
1139       return false;
1140     }
1141     *numerator = (uint32_t)round(numeratorDouble);
1142     if (fabs(numeratorDouble - (*numerator)) == 0.0) {
1143       return true;
1144     }
1145     currentV = 1.0 / currentV;
1146     const double newD = previousD + floor(currentV) * (*denominator);
1147     if (newD > maxD) {
1148       // This is the best we can do with a denominator <= max_d.
1149       return true;
1150     }
1151     previousD = *denominator;
1152     if (newD > (double)UINT32_MAX) {
1153       return false;
1154     }
1155     *denominator = (uint32_t)newD;
1156     currentV -= floor(currentV);
1157     ++iter;
1158   }
1159   // Maximum number of iterations reached, return what we've found.
1160   // For max_iter >= 39 we shouldn't get here. max_iter can be set
1161   // to a lower value to speed up the algorithm if needed.
1162   *numerator = (uint32_t)round((double)(*denominator) * v);
1163   return true;
1164 }
1165 
floatToSignedFraction(float v,int32_t * numerator,uint32_t * denominator)1166 bool floatToSignedFraction(float v, int32_t* numerator, uint32_t* denominator) {
1167   uint32_t positive_numerator;
1168   if (!floatToUnsignedFractionImpl(fabs(v), INT32_MAX, &positive_numerator, denominator)) {
1169     return false;
1170   }
1171   *numerator = (int32_t)positive_numerator;
1172   if (v < 0) {
1173     *numerator *= -1;
1174   }
1175   return true;
1176 }
1177 
floatToUnsignedFraction(float v,uint32_t * numerator,uint32_t * denominator)1178 bool floatToUnsignedFraction(float v, uint32_t* numerator, uint32_t* denominator) {
1179   return floatToUnsignedFractionImpl(v, UINT32_MAX, numerator, denominator);
1180 }
1181 
1182 }  // namespace ultrahdr
1183