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