blob: 0d3319fb8a4c69f64bb32f5c6f6e34d1bbba3134 [file] [log] [blame]
Nick Deakinf6bca5a2022-11-04 10:43:43 -04001/*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <cmath>
18
19#include <jpegrecoverymap/recoverymapmath.h>
20
21namespace android::recoverymap {
22
Nick Deakin594a4ca2022-11-16 20:57:42 -050023////////////////////////////////////////////////////////////////////////////////
24// sRGB transformations
Nick Deakinf6bca5a2022-11-04 10:43:43 -040025
Nick Deakin594a4ca2022-11-16 20:57:42 -050026static const float kSrgbR = 0.299f, kSrgbG = 0.587f, kSrgbB = 0.114f;
27
28float srgbLuminance(Color e) {
29 return kSrgbR * e.r + kSrgbG * e.g + kSrgbB * e.b;
Nick Deakinf6bca5a2022-11-04 10:43:43 -040030}
31
32static const float kSrgbRCr = 1.402f, kSrgbGCb = 0.34414f, kSrgbGCr = 0.71414f, kSrgbBCb = 1.772f;
33
Nick Deakin594a4ca2022-11-16 20:57:42 -050034Color srgbYuvToRgb(Color e_gamma) {
35 return {{{ e_gamma.y + kSrgbRCr * e_gamma.v,
36 e_gamma.y - kSrgbGCb * e_gamma.u - kSrgbGCr * e_gamma.v,
37 e_gamma.y + kSrgbBCb * e_gamma.u }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -040038}
39
Nick Deakin594a4ca2022-11-16 20:57:42 -050040static const float kSrgbUR = -0.1687f, kSrgbUG = -0.3313f, kSrgbUB = 0.5f;
41static const float kSrgbVR = 0.5f, kSrgbVG = -0.4187f, kSrgbVB = -0.0813f;
42
43Color srgbRgbToYuv(Color e_gamma) {
44 return {{{ kSrgbR * e_gamma.r + kSrgbG * e_gamma.g + kSrgbB * e_gamma.b,
45 kSrgbUR * e_gamma.r + kSrgbUG * e_gamma.g + kSrgbUB * e_gamma.b,
46 kSrgbVR * e_gamma.r + kSrgbVG * e_gamma.g + kSrgbVB * e_gamma.b }}};
47}
48
49float srgbInvOetf(float e_gamma) {
50 if (e_gamma <= 0.04045f) {
51 return e_gamma / 12.92f;
Nick Deakinf6bca5a2022-11-04 10:43:43 -040052 } else {
Nick Deakin594a4ca2022-11-16 20:57:42 -050053 return pow((e_gamma + 0.055f) / 1.055f, 2.4);
Nick Deakinf6bca5a2022-11-04 10:43:43 -040054 }
55}
56
Nick Deakin594a4ca2022-11-16 20:57:42 -050057Color srgbInvOetf(Color e_gamma) {
58 return {{{ srgbInvOetf(e_gamma.r),
59 srgbInvOetf(e_gamma.g),
60 srgbInvOetf(e_gamma.b) }}};
61}
62
63
64////////////////////////////////////////////////////////////////////////////////
65// Display-P3 transformations
66
67
68
69////////////////////////////////////////////////////////////////////////////////
70// BT.2100 transformations - according to ITU-R BT.2100-2
71
72static const float kBt2100R = 0.2627f, kBt2100G = 0.6780f, kBt2100B = 0.0593f;
73
74float bt2100Luminance(Color e) {
75 return kBt2100R * e.r + kBt2100G * e.g + kBt2100B * e.b;
76}
77
78static const float kBt2100Cb = 1.8814f, kBt2100Cr = 1.4746f;
79
80Color bt2100RgbToYuv(Color e_gamma) {
81 float y_gamma = bt2100Luminance(e_gamma);
82 return {{{ y_gamma,
83 (e_gamma.b - y_gamma) / kBt2100Cb,
84 (e_gamma.r - y_gamma) / kBt2100Cr }}};
85}
86
87// Derived from the reverse of bt2100RgbToYuv. The derivation for R and B are
88// pretty straight forward; we just reverse the formulas for U and V above. But
89// deriving the formula for G is a bit more complicated:
90//
91// Start with equation for luminance:
92// Y = kBt2100R * R + kBt2100G * G + kBt2100B * B
93// Solve for G:
94// G = (Y - kBt2100R * R - kBt2100B * B) / kBt2100B
95// Substitute equations for R and B in terms YUV:
96// G = (Y - kBt2100R * (Y + kBt2100Cr * V) - kBt2100B * (Y + kBt2100Cb * U)) / kBt2100B
97// Simplify:
98// G = Y * ((1 - kBt2100R - kBt2100B) / kBt2100G)
99// + U * (kBt2100B * kBt2100Cb / kBt2100G)
100// + V * (kBt2100R * kBt2100Cr / kBt2100G)
101//
102// We then get the following coeficients for calculating G from YUV:
103//
104// Coef for Y = (1 - kBt2100R - kBt2100B) / kBt2100G = 1
105// Coef for U = kBt2100B * kBt2100Cb / kBt2100G = kBt2100GCb = ~0.1645
106// Coef for V = kBt2100R * kBt2100Cr / kBt2100G = kBt2100GCr = ~0.5713
107
108static const float kBt2100GCb = kBt2100B * kBt2100Cb / kBt2100G;
109static const float kBt2100GCr = kBt2100R * kBt2100Cr / kBt2100G;
110
111Color bt2100YuvToRgb(Color e_gamma) {
112 return {{{ e_gamma.y + kBt2100Cr * e_gamma.v,
113 e_gamma.y - kBt2100GCb * e_gamma.u - kBt2100GCr * e_gamma.v,
114 e_gamma.y + kBt2100Cb * e_gamma.u }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400115}
116
117static const float kHlgA = 0.17883277f, kHlgB = 0.28466892f, kHlgC = 0.55991073;
118
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500119static float hlgOetf(float e) {
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400120 if (e <= 1.0f/12.0f) {
121 return sqrt(3.0f * e);
122 } else {
123 return kHlgA * log(12.0f * e - kHlgB) + kHlgC;
124 }
125}
126
127Color hlgOetf(Color e) {
128 return {{{ hlgOetf(e.r), hlgOetf(e.g), hlgOetf(e.b) }}};
129}
130
Nick Deakin594a4ca2022-11-16 20:57:42 -0500131float hlgInvOetf(float e_gamma) {
132 if (e_gamma <= 0.5f) {
133 return pow(e_gamma, 2.0f) / 3.0f;
134 } else {
135 return (exp((e_gamma - kHlgC) / kHlgA) + kHlgB) / 12.0f;
136 }
137}
138
139Color hlgInvOetf(Color e_gamma) {
140 return {{{ hlgInvOetf(e_gamma.r),
141 hlgInvOetf(e_gamma.g),
142 hlgInvOetf(e_gamma.b) }}};
143}
144
145
146////////////////////////////////////////////////////////////////////////////////
147// Color conversions
148
149
150////////////////////////////////////////////////////////////////////////////////
151// Recovery map calculations
152
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500153uint8_t encodeRecovery(float y_sdr, float y_hdr, float hdr_ratio) {
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400154 float gain = 1.0f;
155 if (y_sdr > 0.0f) {
156 gain = y_hdr / y_sdr;
157 }
158
159 if (gain < -hdr_ratio) gain = -hdr_ratio;
160 if (gain > hdr_ratio) gain = hdr_ratio;
161
162 return static_cast<uint8_t>(log2(gain) / log2(hdr_ratio) * 127.5f + 127.5f);
163}
164
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500165static float applyRecovery(float e, float recovery, float hdr_ratio) {
166 return exp2(log2(e) + recovery * log2(hdr_ratio));
167}
168
169Color applyRecovery(Color e, float recovery, float hdr_ratio) {
170 return {{{ applyRecovery(e.r, recovery, hdr_ratio),
171 applyRecovery(e.g, recovery, hdr_ratio),
172 applyRecovery(e.b, recovery, hdr_ratio) }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400173}
174
175// TODO: do we need something more clever for filtering either the map or images
176// to generate the map?
177
178static float mapUintToFloat(uint8_t map_uint) {
179 return (static_cast<float>(map_uint) - 127.5f) / 127.5f;
180}
181
182float sampleMap(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y) {
183 float x_map = static_cast<float>(x) / static_cast<float>(map_scale_factor);
184 float y_map = static_cast<float>(y) / static_cast<float>(map_scale_factor);
185
186 size_t x_lower = static_cast<size_t>(floor(x_map));
187 size_t x_upper = x_lower + 1;
188 size_t y_lower = static_cast<size_t>(floor(y_map));
189 size_t y_upper = y_lower + 1;
190
191 float x_influence = x_map - static_cast<float>(x_lower);
192 float y_influence = y_map - static_cast<float>(y_lower);
193
194 float e1 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_lower * map->width]);
195 float e2 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_upper * map->width]);
196 float e3 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_lower * map->width]);
197 float e4 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_upper * map->width]);
198
199 return e1 * (x_influence + y_influence) / 2.0f
200 + e2 * (x_influence + 1.0f - y_influence) / 2.0f
201 + e3 * (1.0f - x_influence + y_influence) / 2.0f
202 + e4 * (1.0f - x_influence + 1.0f - y_influence) / 2.0f;
203}
204
205Color getYuv420Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
206 size_t pixel_count = image->width * image->height;
207
208 size_t pixel_y_idx = x + y * image->width;
209 size_t pixel_uv_idx = x / 2 + (y / 2) * (image->width / 2);
210
211 uint8_t y_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y_idx];
212 uint8_t u_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count + pixel_uv_idx];
213 uint8_t v_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count * 5 / 4 + pixel_uv_idx];
214
215 // 128 bias for UV given we are using jpeglib; see:
216 // https://github.com/kornelski/libjpeg/blob/master/structure.doc
217 return {{{ static_cast<float>(y_uint) / 255.0f,
218 (static_cast<float>(u_uint) - 128.0f) / 255.0f,
219 (static_cast<float>(v_uint) - 128.0f) / 255.0f }}};
220}
221
Nick Deakin594a4ca2022-11-16 20:57:42 -0500222Color getP010Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
223 size_t pixel_count = image->width * image->height;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400224
Nick Deakin594a4ca2022-11-16 20:57:42 -0500225 size_t pixel_y_idx = x + y * image->width;
226 size_t pixel_uv_idx = x / 2 + (y / 2) * (image->width / 2);
227
228 uint16_t y_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_y_idx];
229 uint16_t u_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_count + pixel_uv_idx * 2];
230 uint16_t v_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_count + pixel_uv_idx * 2 + 1];
231
232 // Conversions include taking narrow-range into account.
233 return {{{ static_cast<float>(y_uint) / 940.0f,
234 (static_cast<float>(u_uint) - 64.0f) / 940.0f - 0.5f,
235 (static_cast<float>(v_uint) - 64.0f) / 940.0f - 0.5f }}};
236}
237
238typedef Color (*getPixelFn)(jr_uncompressed_ptr, size_t, size_t);
239
240static Color samplePixels(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y,
241 getPixelFn get_pixel_fn) {
242 Color e = {{{ 0.0f, 0.0f, 0.0f }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400243 for (size_t dy = 0; dy < map_scale_factor; ++dy) {
244 for (size_t dx = 0; dx < map_scale_factor; ++dx) {
Nick Deakin594a4ca2022-11-16 20:57:42 -0500245 e += get_pixel_fn(image, x * map_scale_factor + dx, y * map_scale_factor + dy);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400246 }
247 }
248
249 return e / static_cast<float>(map_scale_factor * map_scale_factor);
250}
251
Nick Deakin594a4ca2022-11-16 20:57:42 -0500252Color sampleYuv420(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) {
253 return samplePixels(image, map_scale_factor, x, y, getYuv420Pixel);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400254}
255
Nick Deakin594a4ca2022-11-16 20:57:42 -0500256Color sampleP010(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) {
257 return samplePixels(image, map_scale_factor, x, y, getP010Pixel);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400258}
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500259
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400260} // namespace android::recoverymap