Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 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> |
Harish Mahendrakar | 555a06b | 2022-12-14 09:37:27 -0800 | [diff] [blame^] | 18 | #include <vector> |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 19 | #include <jpegrecoverymap/recoverymapmath.h> |
| 20 | |
| 21 | namespace android::recoverymap { |
| 22 | |
Harish Mahendrakar | 555a06b | 2022-12-14 09:37:27 -0800 | [diff] [blame^] | 23 | #define CLIP3(x, min, max) ((x) < (min)) ? (min) : ((x) > (max)) ? (max) : (x) |
| 24 | |
| 25 | constexpr size_t kPqOETFPrecision = 10; |
| 26 | constexpr size_t kPqOETFNumEntries = 1 << kPqOETFPrecision; |
| 27 | |
| 28 | static const std::vector<float> kPqOETF = [] { |
| 29 | std::vector<float> result; |
| 30 | float increment = 1.0 / kPqOETFNumEntries; |
| 31 | float value = 0.0f; |
| 32 | for (int idx = 0; idx < kPqOETFNumEntries; idx++, value += increment) { |
| 33 | result.push_back(pqOetf(value)); |
| 34 | } |
| 35 | return result; |
| 36 | }(); |
| 37 | |
| 38 | constexpr size_t kPqInvOETFPrecision = 10; |
| 39 | constexpr size_t kPqInvOETFNumEntries = 1 << kPqInvOETFPrecision; |
| 40 | |
| 41 | static const std::vector<float> kPqInvOETF = [] { |
| 42 | std::vector<float> result; |
| 43 | float increment = 1.0 / kPqInvOETFNumEntries; |
| 44 | float value = 0.0f; |
| 45 | for (int idx = 0; idx < kPqInvOETFNumEntries; idx++, value += increment) { |
| 46 | result.push_back(pqInvOetf(value)); |
| 47 | } |
| 48 | return result; |
| 49 | }(); |
| 50 | |
| 51 | constexpr size_t kHlgOETFPrecision = 10; |
| 52 | constexpr size_t kHlgOETFNumEntries = 1 << kHlgOETFPrecision; |
| 53 | |
| 54 | static const std::vector<float> kHlgOETF = [] { |
| 55 | std::vector<float> result; |
| 56 | float increment = 1.0 / kHlgOETFNumEntries; |
| 57 | float value = 0.0f; |
| 58 | for (int idx = 0; idx < kHlgOETFNumEntries; idx++, value += increment) { |
| 59 | result.push_back(hlgOetf(value)); |
| 60 | } |
| 61 | return result; |
| 62 | }(); |
| 63 | |
| 64 | constexpr size_t kHlgInvOETFPrecision = 10; |
| 65 | constexpr size_t kHlgInvOETFNumEntries = 1 << kHlgInvOETFPrecision; |
| 66 | |
| 67 | static const std::vector<float> kHlgInvOETF = [] { |
| 68 | std::vector<float> result; |
| 69 | float increment = 1.0 / kHlgInvOETFNumEntries; |
| 70 | float value = 0.0f; |
| 71 | for (int idx = 0; idx < kHlgInvOETFNumEntries; idx++, value += increment) { |
| 72 | result.push_back(hlgInvOetf(value)); |
| 73 | } |
| 74 | return result; |
| 75 | }(); |
| 76 | |
| 77 | constexpr size_t kSRGBInvOETFPrecision = 10; |
| 78 | constexpr size_t kSRGBInvOETFNumEntries = 1 << kSRGBInvOETFPrecision; |
| 79 | static const std::vector<float> kSRGBInvOETF = [] { |
| 80 | std::vector<float> result; |
| 81 | float increment = 1.0 / kSRGBInvOETFNumEntries; |
| 82 | float value = 0.0f; |
| 83 | for (int idx = 0; idx < kSRGBInvOETFNumEntries; idx++, value += increment) { |
| 84 | result.push_back(srgbInvOetf(value)); |
| 85 | } |
| 86 | return result; |
| 87 | }(); |
Ram Mohan | fe723d6 | 2022-12-15 00:59:11 +0530 | [diff] [blame] | 88 | |
| 89 | // Use Shepard's method for inverse distance weighting. For more information: |
| 90 | // en.wikipedia.org/wiki/Inverse_distance_weighting#Shepard's_method |
| 91 | |
| 92 | float ShepardsIDW::euclideanDistance(float x1, float x2, float y1, float y2) { |
| 93 | return sqrt(((y2 - y1) * (y2 - y1)) + (x2 - x1) * (x2 - x1)); |
| 94 | } |
| 95 | |
| 96 | void ShepardsIDW::fillShepardsIDW(float *weights, int incR, int incB) { |
| 97 | for (int y = 0; y < mMapScaleFactor; y++) { |
| 98 | for (int x = 0; x < mMapScaleFactor; x++) { |
| 99 | float pos_x = ((float)x) / mMapScaleFactor; |
| 100 | float pos_y = ((float)y) / mMapScaleFactor; |
| 101 | int curr_x = floor(pos_x); |
| 102 | int curr_y = floor(pos_y); |
| 103 | int next_x = curr_x + incR; |
| 104 | int next_y = curr_y + incB; |
| 105 | float e1_distance = euclideanDistance(pos_x, curr_x, pos_y, curr_y); |
| 106 | int index = y * mMapScaleFactor * 4 + x * 4; |
| 107 | if (e1_distance == 0) { |
| 108 | weights[index++] = 1.f; |
| 109 | weights[index++] = 0.f; |
| 110 | weights[index++] = 0.f; |
| 111 | weights[index++] = 0.f; |
| 112 | } else { |
| 113 | float e1_weight = 1.f / e1_distance; |
| 114 | |
| 115 | float e2_distance = euclideanDistance(pos_x, curr_x, pos_y, next_y); |
| 116 | float e2_weight = 1.f / e2_distance; |
| 117 | |
| 118 | float e3_distance = euclideanDistance(pos_x, next_x, pos_y, curr_y); |
| 119 | float e3_weight = 1.f / e3_distance; |
| 120 | |
| 121 | float e4_distance = euclideanDistance(pos_x, next_x, pos_y, next_y); |
| 122 | float e4_weight = 1.f / e4_distance; |
| 123 | |
| 124 | float total_weight = e1_weight + e2_weight + e3_weight + e4_weight; |
| 125 | |
| 126 | weights[index++] = e1_weight / total_weight; |
| 127 | weights[index++] = e2_weight / total_weight; |
| 128 | weights[index++] = e3_weight / total_weight; |
| 129 | weights[index++] = e4_weight / total_weight; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 135 | //////////////////////////////////////////////////////////////////////////////// |
| 136 | // sRGB transformations |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 137 | |
Harish Mahendrakar | 1107ff3 | 2022-12-07 17:24:35 -0800 | [diff] [blame] | 138 | static const float kMaxPixelFloat = 1.0f; |
| 139 | static float clampPixelFloat(float value) { |
| 140 | return (value < 0.0f) ? 0.0f : (value > kMaxPixelFloat) ? kMaxPixelFloat : value; |
| 141 | } |
| 142 | |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 143 | // See IEC 61966-2-1, Equation F.7. |
| 144 | static const float kSrgbR = 0.2126f, kSrgbG = 0.7152f, kSrgbB = 0.0722f; |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 145 | |
| 146 | float srgbLuminance(Color e) { |
| 147 | return kSrgbR * e.r + kSrgbG * e.g + kSrgbB * e.b; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 150 | // See ECMA TR/98, Section 7. |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 151 | static const float kSrgbRCr = 1.402f, kSrgbGCb = 0.34414f, kSrgbGCr = 0.71414f, kSrgbBCb = 1.772f; |
| 152 | |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 153 | Color srgbYuvToRgb(Color e_gamma) { |
Harish Mahendrakar | 1107ff3 | 2022-12-07 17:24:35 -0800 | [diff] [blame] | 154 | return {{{ clampPixelFloat(e_gamma.y + kSrgbRCr * e_gamma.v), |
| 155 | clampPixelFloat(e_gamma.y - kSrgbGCb * e_gamma.u - kSrgbGCr * e_gamma.v), |
| 156 | clampPixelFloat(e_gamma.y + kSrgbBCb * e_gamma.u) }}}; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 157 | } |
| 158 | |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 159 | // See ECMA TR/98, Section 7. |
| 160 | static const float kSrgbYR = 0.299f, kSrgbYG = 0.587f, kSrgbYB = 0.114f; |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 161 | static const float kSrgbUR = -0.1687f, kSrgbUG = -0.3313f, kSrgbUB = 0.5f; |
| 162 | static const float kSrgbVR = 0.5f, kSrgbVG = -0.4187f, kSrgbVB = -0.0813f; |
| 163 | |
| 164 | Color srgbRgbToYuv(Color e_gamma) { |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 165 | return {{{ kSrgbYR * e_gamma.r + kSrgbYG * e_gamma.g + kSrgbYB * e_gamma.b, |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 166 | kSrgbUR * e_gamma.r + kSrgbUG * e_gamma.g + kSrgbUB * e_gamma.b, |
| 167 | kSrgbVR * e_gamma.r + kSrgbVG * e_gamma.g + kSrgbVB * e_gamma.b }}}; |
| 168 | } |
| 169 | |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 170 | // See IEC 61966-2-1, Equations F.5 and F.6. |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 171 | float srgbInvOetf(float e_gamma) { |
| 172 | if (e_gamma <= 0.04045f) { |
| 173 | return e_gamma / 12.92f; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 174 | } else { |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 175 | return pow((e_gamma + 0.055f) / 1.055f, 2.4); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 179 | Color srgbInvOetf(Color e_gamma) { |
| 180 | return {{{ srgbInvOetf(e_gamma.r), |
| 181 | srgbInvOetf(e_gamma.g), |
| 182 | srgbInvOetf(e_gamma.b) }}}; |
| 183 | } |
| 184 | |
Harish Mahendrakar | 555a06b | 2022-12-14 09:37:27 -0800 | [diff] [blame^] | 185 | // See IEC 61966-2-1, Equations F.5 and F.6. |
| 186 | float srgbInvOetfLUT(float e_gamma) { |
| 187 | uint32_t value = static_cast<uint32_t>(e_gamma * kSRGBInvOETFNumEntries); |
| 188 | //TODO() : Remove once conversion modules have appropriate clamping in place |
| 189 | value = CLIP3(value, 0, kSRGBInvOETFNumEntries - 1); |
| 190 | return kSRGBInvOETF[value]; |
| 191 | } |
| 192 | |
| 193 | Color srgbInvOetfLUT(Color e_gamma) { |
| 194 | return {{{ srgbInvOetfLUT(e_gamma.r), |
| 195 | srgbInvOetfLUT(e_gamma.g), |
| 196 | srgbInvOetfLUT(e_gamma.b) }}}; |
| 197 | } |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 198 | |
| 199 | //////////////////////////////////////////////////////////////////////////////// |
| 200 | // Display-P3 transformations |
| 201 | |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 202 | // See SMPTE EG 432-1, Table 7-2. |
| 203 | static const float kP3R = 0.20949f, kP3G = 0.72160f, kP3B = 0.06891f; |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 204 | |
| 205 | float p3Luminance(Color e) { |
| 206 | return kP3R * e.r + kP3G * e.g + kP3B * e.b; |
| 207 | } |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 208 | |
| 209 | |
| 210 | //////////////////////////////////////////////////////////////////////////////// |
| 211 | // BT.2100 transformations - according to ITU-R BT.2100-2 |
| 212 | |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 213 | // See ITU-R BT.2100-2, Table 5, HLG Reference OOTF |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 214 | static const float kBt2100R = 0.2627f, kBt2100G = 0.6780f, kBt2100B = 0.0593f; |
| 215 | |
| 216 | float bt2100Luminance(Color e) { |
| 217 | return kBt2100R * e.r + kBt2100G * e.g + kBt2100B * e.b; |
| 218 | } |
| 219 | |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 220 | // See ITU-R BT.2100-2, Table 6, Derivation of colour difference signals. |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 221 | static const float kBt2100Cb = 1.8814f, kBt2100Cr = 1.4746f; |
| 222 | |
| 223 | Color bt2100RgbToYuv(Color e_gamma) { |
| 224 | float y_gamma = bt2100Luminance(e_gamma); |
| 225 | return {{{ y_gamma, |
| 226 | (e_gamma.b - y_gamma) / kBt2100Cb, |
| 227 | (e_gamma.r - y_gamma) / kBt2100Cr }}}; |
| 228 | } |
| 229 | |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 230 | // Derived by inversing bt2100RgbToYuv. The derivation for R and B are pretty |
| 231 | // straight forward; we just invert the formulas for U and V above. But deriving |
| 232 | // the formula for G is a bit more complicated: |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 233 | // |
| 234 | // Start with equation for luminance: |
| 235 | // Y = kBt2100R * R + kBt2100G * G + kBt2100B * B |
| 236 | // Solve for G: |
| 237 | // G = (Y - kBt2100R * R - kBt2100B * B) / kBt2100B |
| 238 | // Substitute equations for R and B in terms YUV: |
| 239 | // G = (Y - kBt2100R * (Y + kBt2100Cr * V) - kBt2100B * (Y + kBt2100Cb * U)) / kBt2100B |
| 240 | // Simplify: |
| 241 | // G = Y * ((1 - kBt2100R - kBt2100B) / kBt2100G) |
| 242 | // + U * (kBt2100B * kBt2100Cb / kBt2100G) |
| 243 | // + V * (kBt2100R * kBt2100Cr / kBt2100G) |
| 244 | // |
| 245 | // We then get the following coeficients for calculating G from YUV: |
| 246 | // |
| 247 | // Coef for Y = (1 - kBt2100R - kBt2100B) / kBt2100G = 1 |
| 248 | // Coef for U = kBt2100B * kBt2100Cb / kBt2100G = kBt2100GCb = ~0.1645 |
| 249 | // Coef for V = kBt2100R * kBt2100Cr / kBt2100G = kBt2100GCr = ~0.5713 |
| 250 | |
| 251 | static const float kBt2100GCb = kBt2100B * kBt2100Cb / kBt2100G; |
| 252 | static const float kBt2100GCr = kBt2100R * kBt2100Cr / kBt2100G; |
| 253 | |
| 254 | Color bt2100YuvToRgb(Color e_gamma) { |
Harish Mahendrakar | 1107ff3 | 2022-12-07 17:24:35 -0800 | [diff] [blame] | 255 | return {{{ clampPixelFloat(e_gamma.y + kBt2100Cr * e_gamma.v), |
| 256 | clampPixelFloat(e_gamma.y - kBt2100GCb * e_gamma.u - kBt2100GCr * e_gamma.v), |
| 257 | clampPixelFloat(e_gamma.y + kBt2100Cb * e_gamma.u) }}}; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 258 | } |
| 259 | |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 260 | // See ITU-R BT.2100-2, Table 5, HLG Reference OETF. |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 261 | static const float kHlgA = 0.17883277f, kHlgB = 0.28466892f, kHlgC = 0.55991073; |
| 262 | |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 263 | float hlgOetf(float e) { |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 264 | if (e <= 1.0f/12.0f) { |
| 265 | return sqrt(3.0f * e); |
| 266 | } else { |
| 267 | return kHlgA * log(12.0f * e - kHlgB) + kHlgC; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | Color hlgOetf(Color e) { |
| 272 | return {{{ hlgOetf(e.r), hlgOetf(e.g), hlgOetf(e.b) }}}; |
| 273 | } |
| 274 | |
Harish Mahendrakar | 555a06b | 2022-12-14 09:37:27 -0800 | [diff] [blame^] | 275 | float hlgOetfLUT(float e) { |
| 276 | uint32_t value = static_cast<uint32_t>(e * kHlgOETFNumEntries); |
| 277 | //TODO() : Remove once conversion modules have appropriate clamping in place |
| 278 | value = CLIP3(value, 0, kHlgOETFNumEntries - 1); |
| 279 | |
| 280 | return kHlgOETF[value]; |
| 281 | } |
| 282 | |
| 283 | Color hlgOetfLUT(Color e) { |
| 284 | return {{{ hlgOetfLUT(e.r), hlgOetfLUT(e.g), hlgOetfLUT(e.b) }}}; |
| 285 | } |
| 286 | |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 287 | // See ITU-R BT.2100-2, Table 5, HLG Reference EOTF. |
| 288 | float hlgInvOetf(float e_gamma) { |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 289 | if (e_gamma <= 0.5f) { |
| 290 | return pow(e_gamma, 2.0f) / 3.0f; |
| 291 | } else { |
| 292 | return (exp((e_gamma - kHlgC) / kHlgA) + kHlgB) / 12.0f; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | Color hlgInvOetf(Color e_gamma) { |
| 297 | return {{{ hlgInvOetf(e_gamma.r), |
| 298 | hlgInvOetf(e_gamma.g), |
| 299 | hlgInvOetf(e_gamma.b) }}}; |
| 300 | } |
| 301 | |
Harish Mahendrakar | 555a06b | 2022-12-14 09:37:27 -0800 | [diff] [blame^] | 302 | float hlgInvOetfLUT(float e_gamma) { |
| 303 | uint32_t value = static_cast<uint32_t>(e_gamma * kHlgInvOETFNumEntries); |
| 304 | //TODO() : Remove once conversion modules have appropriate clamping in place |
| 305 | value = CLIP3(value, 0, kHlgInvOETFNumEntries - 1); |
| 306 | |
| 307 | return kHlgInvOETF[value]; |
| 308 | } |
| 309 | |
| 310 | Color hlgInvOetfLUT(Color e_gamma) { |
| 311 | return {{{ hlgInvOetfLUT(e_gamma.r), |
| 312 | hlgInvOetfLUT(e_gamma.g), |
| 313 | hlgInvOetfLUT(e_gamma.b) }}}; |
| 314 | } |
| 315 | |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 316 | // See ITU-R BT.2100-2, Table 4, Reference PQ OETF. |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 317 | static const float kPqM1 = 2610.0f / 16384.0f, kPqM2 = 2523.0f / 4096.0f * 128.0f; |
| 318 | static const float kPqC1 = 3424.0f / 4096.0f, kPqC2 = 2413.0f / 4096.0f * 32.0f, |
| 319 | kPqC3 = 2392.0f / 4096.0f * 32.0f; |
| 320 | |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 321 | float pqOetf(float e) { |
| 322 | if (e <= 0.0f) return 0.0f; |
| 323 | return pow((kPqC1 + kPqC2 * pow(e, kPqM1)) / (1 + kPqC3 * pow(e, kPqM1)), |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 324 | kPqM2); |
| 325 | } |
| 326 | |
| 327 | Color pqOetf(Color e) { |
| 328 | return {{{ pqOetf(e.r), pqOetf(e.g), pqOetf(e.b) }}}; |
| 329 | } |
| 330 | |
Harish Mahendrakar | 555a06b | 2022-12-14 09:37:27 -0800 | [diff] [blame^] | 331 | float pqOetfLUT(float e) { |
| 332 | uint32_t value = static_cast<uint32_t>(e * kPqOETFNumEntries); |
| 333 | //TODO() : Remove once conversion modules have appropriate clamping in place |
| 334 | value = CLIP3(value, 0, kPqOETFNumEntries - 1); |
| 335 | |
| 336 | return kPqOETF[value]; |
| 337 | } |
| 338 | |
| 339 | Color pqOetfLUT(Color e) { |
| 340 | return {{{ pqOetfLUT(e.r), pqOetfLUT(e.g), pqOetfLUT(e.b) }}}; |
| 341 | } |
| 342 | |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 343 | // Derived from the inverse of the Reference PQ OETF. |
| 344 | static const float kPqInvA = 128.0f, kPqInvB = 107.0f, kPqInvC = 2413.0f, kPqInvD = 2392.0f, |
| 345 | kPqInvE = 6.2773946361f, kPqInvF = 0.0126833f; |
| 346 | |
| 347 | float pqInvOetf(float e_gamma) { |
| 348 | // This equation blows up if e_gamma is 0.0, and checking on <= 0.0 doesn't |
| 349 | // always catch 0.0. So, check on 0.0001, since anything this small will |
| 350 | // effectively be crushed to zero anyways. |
| 351 | if (e_gamma <= 0.0001f) return 0.0f; |
| 352 | return pow((kPqInvA * pow(e_gamma, kPqInvF) - kPqInvB) |
| 353 | / (kPqInvC - kPqInvD * pow(e_gamma, kPqInvF)), |
| 354 | kPqInvE); |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | Color pqInvOetf(Color e_gamma) { |
| 358 | return {{{ pqInvOetf(e_gamma.r), |
| 359 | pqInvOetf(e_gamma.g), |
| 360 | pqInvOetf(e_gamma.b) }}}; |
| 361 | } |
| 362 | |
Harish Mahendrakar | 555a06b | 2022-12-14 09:37:27 -0800 | [diff] [blame^] | 363 | float pqInvOetfLUT(float e_gamma) { |
| 364 | uint32_t value = static_cast<uint32_t>(e_gamma * kPqInvOETFNumEntries); |
| 365 | //TODO() : Remove once conversion modules have appropriate clamping in place |
| 366 | value = CLIP3(value, 0, kPqInvOETFNumEntries - 1); |
| 367 | |
| 368 | return kPqInvOETF[value]; |
| 369 | } |
| 370 | |
| 371 | Color pqInvOetfLUT(Color e_gamma) { |
| 372 | return {{{ pqInvOetfLUT(e_gamma.r), |
| 373 | pqInvOetfLUT(e_gamma.g), |
| 374 | pqInvOetfLUT(e_gamma.b) }}}; |
| 375 | } |
| 376 | |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 377 | |
| 378 | //////////////////////////////////////////////////////////////////////////////// |
| 379 | // Color conversions |
| 380 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 381 | Color bt709ToP3(Color e) { |
| 382 | return {{{ 0.82254f * e.r + 0.17755f * e.g + 0.00006f * e.b, |
| 383 | 0.03312f * e.r + 0.96684f * e.g + -0.00001f * e.b, |
| 384 | 0.01706f * e.r + 0.07240f * e.g + 0.91049f * e.b }}}; |
| 385 | } |
| 386 | |
| 387 | Color bt709ToBt2100(Color e) { |
| 388 | return {{{ 0.62740f * e.r + 0.32930f * e.g + 0.04332f * e.b, |
| 389 | 0.06904f * e.r + 0.91958f * e.g + 0.01138f * e.b, |
| 390 | 0.01636f * e.r + 0.08799f * e.g + 0.89555f * e.b }}}; |
| 391 | } |
| 392 | |
| 393 | Color p3ToBt709(Color e) { |
| 394 | return {{{ 1.22482f * e.r + -0.22490f * e.g + -0.00007f * e.b, |
| 395 | -0.04196f * e.r + 1.04199f * e.g + 0.00001f * e.b, |
| 396 | -0.01961f * e.r + -0.07865f * e.g + 1.09831f * e.b }}}; |
| 397 | } |
| 398 | |
| 399 | Color p3ToBt2100(Color e) { |
| 400 | return {{{ 0.75378f * e.r + 0.19862f * e.g + 0.04754f * e.b, |
| 401 | 0.04576f * e.r + 0.94177f * e.g + 0.01250f * e.b, |
| 402 | -0.00121f * e.r + 0.01757f * e.g + 0.98359f * e.b }}}; |
| 403 | } |
| 404 | |
| 405 | Color bt2100ToBt709(Color e) { |
| 406 | return {{{ 1.66045f * e.r + -0.58764f * e.g + -0.07286f * e.b, |
| 407 | -0.12445f * e.r + 1.13282f * e.g + -0.00837f * e.b, |
| 408 | -0.01811f * e.r + -0.10057f * e.g + 1.11878f * e.b }}}; |
| 409 | } |
| 410 | |
| 411 | Color bt2100ToP3(Color e) { |
| 412 | return {{{ 1.34369f * e.r + -0.28223f * e.g + -0.06135f * e.b, |
| 413 | -0.06533f * e.r + 1.07580f * e.g + -0.01051f * e.b, |
| 414 | 0.00283f * e.r + -0.01957f * e.g + 1.01679f * e.b |
| 415 | }}}; |
| 416 | } |
| 417 | |
| 418 | // TODO: confirm we always want to convert like this before calculating |
| 419 | // luminance. |
| 420 | ColorTransformFn getHdrConversionFn(jpegr_color_gamut sdr_gamut, jpegr_color_gamut hdr_gamut) { |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 421 | switch (sdr_gamut) { |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 422 | case JPEGR_COLORGAMUT_BT709: |
| 423 | switch (hdr_gamut) { |
| 424 | case JPEGR_COLORGAMUT_BT709: |
| 425 | return identityConversion; |
| 426 | case JPEGR_COLORGAMUT_P3: |
| 427 | return p3ToBt709; |
| 428 | case JPEGR_COLORGAMUT_BT2100: |
| 429 | return bt2100ToBt709; |
| 430 | case JPEGR_COLORGAMUT_UNSPECIFIED: |
| 431 | return nullptr; |
| 432 | } |
| 433 | break; |
| 434 | case JPEGR_COLORGAMUT_P3: |
| 435 | switch (hdr_gamut) { |
| 436 | case JPEGR_COLORGAMUT_BT709: |
| 437 | return bt709ToP3; |
| 438 | case JPEGR_COLORGAMUT_P3: |
| 439 | return identityConversion; |
| 440 | case JPEGR_COLORGAMUT_BT2100: |
| 441 | return bt2100ToP3; |
| 442 | case JPEGR_COLORGAMUT_UNSPECIFIED: |
| 443 | return nullptr; |
| 444 | } |
| 445 | break; |
| 446 | case JPEGR_COLORGAMUT_BT2100: |
| 447 | switch (hdr_gamut) { |
| 448 | case JPEGR_COLORGAMUT_BT709: |
| 449 | return bt709ToBt2100; |
| 450 | case JPEGR_COLORGAMUT_P3: |
| 451 | return p3ToBt2100; |
| 452 | case JPEGR_COLORGAMUT_BT2100: |
| 453 | return identityConversion; |
| 454 | case JPEGR_COLORGAMUT_UNSPECIFIED: |
| 455 | return nullptr; |
| 456 | } |
| 457 | break; |
| 458 | case JPEGR_COLORGAMUT_UNSPECIFIED: |
| 459 | return nullptr; |
| 460 | } |
| 461 | } |
| 462 | |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 463 | |
| 464 | //////////////////////////////////////////////////////////////////////////////// |
| 465 | // Recovery map calculations |
| 466 | |
Nick Deakin | 5c20b9e | 2022-11-15 17:39:24 -0500 | [diff] [blame] | 467 | uint8_t encodeRecovery(float y_sdr, float y_hdr, float hdr_ratio) { |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 468 | float gain = 1.0f; |
| 469 | if (y_sdr > 0.0f) { |
| 470 | gain = y_hdr / y_sdr; |
| 471 | } |
| 472 | |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 473 | if (gain < (1.0f / hdr_ratio)) gain = 1.0f / hdr_ratio; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 474 | if (gain > hdr_ratio) gain = hdr_ratio; |
| 475 | |
| 476 | return static_cast<uint8_t>(log2(gain) / log2(hdr_ratio) * 127.5f + 127.5f); |
| 477 | } |
| 478 | |
Nick Deakin | 5c20b9e | 2022-11-15 17:39:24 -0500 | [diff] [blame] | 479 | Color applyRecovery(Color e, float recovery, float hdr_ratio) { |
Harish Mahendrakar | a5ddcc2 | 2022-12-13 12:45:23 -0800 | [diff] [blame] | 480 | float recoveryFactor = pow(hdr_ratio, recovery); |
| 481 | return e * recoveryFactor; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 482 | } |
| 483 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 484 | Color getYuv420Pixel(jr_uncompressed_ptr image, size_t x, size_t y) { |
| 485 | size_t pixel_count = image->width * image->height; |
| 486 | |
| 487 | size_t pixel_y_idx = x + y * image->width; |
| 488 | size_t pixel_uv_idx = x / 2 + (y / 2) * (image->width / 2); |
| 489 | |
| 490 | uint8_t y_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y_idx]; |
| 491 | uint8_t u_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count + pixel_uv_idx]; |
| 492 | uint8_t v_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count * 5 / 4 + pixel_uv_idx]; |
| 493 | |
| 494 | // 128 bias for UV given we are using jpeglib; see: |
| 495 | // https://github.com/kornelski/libjpeg/blob/master/structure.doc |
| 496 | return {{{ static_cast<float>(y_uint) / 255.0f, |
| 497 | (static_cast<float>(u_uint) - 128.0f) / 255.0f, |
| 498 | (static_cast<float>(v_uint) - 128.0f) / 255.0f }}}; |
| 499 | } |
| 500 | |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 501 | Color getP010Pixel(jr_uncompressed_ptr image, size_t x, size_t y) { |
| 502 | size_t pixel_count = image->width * image->height; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 503 | |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 504 | size_t pixel_y_idx = x + y * image->width; |
| 505 | size_t pixel_uv_idx = x / 2 + (y / 2) * (image->width / 2); |
| 506 | |
Nick Deakin | 86207ba | 2022-11-21 16:07:36 -0500 | [diff] [blame] | 507 | uint16_t y_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_y_idx] |
| 508 | >> 6; |
| 509 | uint16_t u_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_count + pixel_uv_idx * 2] |
| 510 | >> 6; |
| 511 | uint16_t v_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_count + pixel_uv_idx * 2 + 1] |
| 512 | >> 6; |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 513 | |
| 514 | // Conversions include taking narrow-range into account. |
Nick Deakin | 3812533 | 2022-12-12 15:48:24 -0500 | [diff] [blame] | 515 | return {{{ (static_cast<float>(y_uint) - 64.0f) / 876.0f, |
| 516 | (static_cast<float>(u_uint) - 64.0f) / 896.0f - 0.5f, |
| 517 | (static_cast<float>(v_uint) - 64.0f) / 896.0f - 0.5f }}}; |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | typedef Color (*getPixelFn)(jr_uncompressed_ptr, size_t, size_t); |
| 521 | |
| 522 | static Color samplePixels(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y, |
| 523 | getPixelFn get_pixel_fn) { |
| 524 | Color e = {{{ 0.0f, 0.0f, 0.0f }}}; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 525 | for (size_t dy = 0; dy < map_scale_factor; ++dy) { |
| 526 | for (size_t dx = 0; dx < map_scale_factor; ++dx) { |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 527 | e += get_pixel_fn(image, x * map_scale_factor + dx, y * map_scale_factor + dy); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | return e / static_cast<float>(map_scale_factor * map_scale_factor); |
| 532 | } |
| 533 | |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 534 | Color sampleYuv420(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) { |
| 535 | return samplePixels(image, map_scale_factor, x, y, getYuv420Pixel); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 536 | } |
| 537 | |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 538 | Color sampleP010(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) { |
| 539 | return samplePixels(image, map_scale_factor, x, y, getP010Pixel); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 540 | } |
Nick Deakin | 5c20b9e | 2022-11-15 17:39:24 -0500 | [diff] [blame] | 541 | |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 542 | // TODO: do we need something more clever for filtering either the map or images |
| 543 | // to generate the map? |
| 544 | |
| 545 | static size_t clamp(const size_t& val, const size_t& low, const size_t& high) { |
| 546 | return val < low ? low : (high < val ? high : val); |
| 547 | } |
| 548 | |
| 549 | static float mapUintToFloat(uint8_t map_uint) { |
| 550 | return (static_cast<float>(map_uint) - 127.5f) / 127.5f; |
| 551 | } |
| 552 | |
| 553 | static float pythDistance(float x_diff, float y_diff) { |
| 554 | return sqrt(pow(x_diff, 2.0f) + pow(y_diff, 2.0f)); |
| 555 | } |
| 556 | |
Ram Mohan | fe723d6 | 2022-12-15 00:59:11 +0530 | [diff] [blame] | 557 | // TODO: If map_scale_factor is guaranteed to be an integer, then remove the following. |
Nick Deakin | 65f492a | 2022-11-29 22:47:40 -0500 | [diff] [blame] | 558 | float sampleMap(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y) { |
| 559 | float x_map = static_cast<float>(x) / static_cast<float>(map_scale_factor); |
| 560 | float y_map = static_cast<float>(y) / static_cast<float>(map_scale_factor); |
| 561 | |
| 562 | size_t x_lower = static_cast<size_t>(floor(x_map)); |
| 563 | size_t x_upper = x_lower + 1; |
| 564 | size_t y_lower = static_cast<size_t>(floor(y_map)); |
| 565 | size_t y_upper = y_lower + 1; |
| 566 | |
| 567 | x_lower = clamp(x_lower, 0, map->width - 1); |
| 568 | x_upper = clamp(x_upper, 0, map->width - 1); |
| 569 | y_lower = clamp(y_lower, 0, map->height - 1); |
| 570 | y_upper = clamp(y_upper, 0, map->height - 1); |
| 571 | |
| 572 | // Use Shepard's method for inverse distance weighting. For more information: |
| 573 | // en.wikipedia.org/wiki/Inverse_distance_weighting#Shepard's_method |
| 574 | |
| 575 | float e1 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_lower * map->width]); |
| 576 | float e1_dist = pythDistance(x_map - static_cast<float>(x_lower), |
| 577 | y_map - static_cast<float>(y_lower)); |
| 578 | if (e1_dist == 0.0f) return e1; |
| 579 | |
| 580 | float e2 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_upper * map->width]); |
| 581 | float e2_dist = pythDistance(x_map - static_cast<float>(x_lower), |
| 582 | y_map - static_cast<float>(y_upper)); |
| 583 | if (e2_dist == 0.0f) return e2; |
| 584 | |
| 585 | float e3 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_lower * map->width]); |
| 586 | float e3_dist = pythDistance(x_map - static_cast<float>(x_upper), |
| 587 | y_map - static_cast<float>(y_lower)); |
| 588 | if (e3_dist == 0.0f) return e3; |
| 589 | |
| 590 | float e4 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_upper * map->width]); |
| 591 | float e4_dist = pythDistance(x_map - static_cast<float>(x_upper), |
| 592 | y_map - static_cast<float>(y_upper)); |
| 593 | if (e4_dist == 0.0f) return e2; |
| 594 | |
| 595 | float e1_weight = 1.0f / e1_dist; |
| 596 | float e2_weight = 1.0f / e2_dist; |
| 597 | float e3_weight = 1.0f / e3_dist; |
| 598 | float e4_weight = 1.0f / e4_dist; |
| 599 | float total_weight = e1_weight + e2_weight + e3_weight + e4_weight; |
| 600 | |
| 601 | return e1 * (e1_weight / total_weight) |
| 602 | + e2 * (e2_weight / total_weight) |
| 603 | + e3 * (e3_weight / total_weight) |
| 604 | + e4 * (e4_weight / total_weight); |
| 605 | } |
| 606 | |
Ram Mohan | fe723d6 | 2022-12-15 00:59:11 +0530 | [diff] [blame] | 607 | float sampleMap(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y, |
| 608 | ShepardsIDW& weightTables) { |
| 609 | // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the |
| 610 | // following by computing log2(map_scale_factor) once and then using >> log2(map_scale_factor) |
| 611 | int x_lower = x / map_scale_factor; |
| 612 | int x_upper = x_lower + 1; |
| 613 | int y_lower = y / map_scale_factor; |
| 614 | int y_upper = y_lower + 1; |
| 615 | |
| 616 | x_lower = std::min(x_lower, map->width - 1); |
| 617 | x_upper = std::min(x_upper, map->width - 1); |
| 618 | y_lower = std::min(y_lower, map->height - 1); |
| 619 | y_upper = std::min(y_upper, map->height - 1); |
| 620 | |
| 621 | float e1 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_lower * map->width]); |
| 622 | float e2 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_upper * map->width]); |
| 623 | float e3 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_lower * map->width]); |
| 624 | float e4 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_upper * map->width]); |
| 625 | |
| 626 | // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the |
| 627 | // following by using & (map_scale_factor - 1) |
| 628 | int offset_x = x % map_scale_factor; |
| 629 | int offset_y = y % map_scale_factor; |
| 630 | |
| 631 | float* weights = weightTables.mWeights; |
| 632 | if (x_lower == x_upper && y_lower == y_upper) weights = weightTables.mWeightsC; |
| 633 | else if (x_lower == x_upper) weights = weightTables.mWeightsNR; |
| 634 | else if (y_lower == y_upper) weights = weightTables.mWeightsNB; |
| 635 | weights += offset_y * map_scale_factor * 4 + offset_x * 4; |
| 636 | |
| 637 | return e1 * weights[0] + e2 * weights[1] + e3 * weights[2] + e4 * weights[3]; |
| 638 | } |
| 639 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 640 | uint32_t colorToRgba1010102(Color e_gamma) { |
| 641 | return (0x3ff & static_cast<uint32_t>(e_gamma.r * 1023.0f)) |
| 642 | | ((0x3ff & static_cast<uint32_t>(e_gamma.g * 1023.0f)) << 10) |
| 643 | | ((0x3ff & static_cast<uint32_t>(e_gamma.b * 1023.0f)) << 20) |
| 644 | | (0x3 << 30); // Set alpha to 1.0 |
| 645 | } |
| 646 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 647 | } // namespace android::recoverymap |