Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libwebp/sharpyuv/sharpyuv_gamma.c
21363 views
1
// Copyright 2022 Google Inc. All Rights Reserved.
2
//
3
// Use of this source code is governed by a BSD-style license
4
// that can be found in the COPYING file in the root of the source
5
// tree. An additional intellectual property rights grant can be found
6
// in the file PATENTS. All contributing project authors may
7
// be found in the AUTHORS file in the root of the source tree.
8
// -----------------------------------------------------------------------------
9
//
10
// Gamma correction utilities.
11
12
#include "sharpyuv/sharpyuv_gamma.h"
13
14
#include <assert.h>
15
#include <float.h>
16
#include <math.h>
17
18
#include "sharpyuv/sharpyuv.h"
19
#include "src/webp/types.h"
20
21
// Gamma correction compensates loss of resolution during chroma subsampling.
22
// Size of pre-computed table for converting from gamma to linear.
23
#define GAMMA_TO_LINEAR_TAB_BITS 10
24
#define GAMMA_TO_LINEAR_TAB_SIZE (1 << GAMMA_TO_LINEAR_TAB_BITS)
25
static uint32_t kGammaToLinearTabS[GAMMA_TO_LINEAR_TAB_SIZE + 2];
26
#define LINEAR_TO_GAMMA_TAB_BITS 9
27
#define LINEAR_TO_GAMMA_TAB_SIZE (1 << LINEAR_TO_GAMMA_TAB_BITS)
28
static uint32_t kLinearToGammaTabS[LINEAR_TO_GAMMA_TAB_SIZE + 2];
29
30
#if defined(_MSC_VER)
31
static const double kGammaF = 2.222222222222222;
32
#else
33
static const double kGammaF = 1. / 0.45;
34
#endif
35
#define GAMMA_TO_LINEAR_BITS 16
36
37
static volatile int kGammaTablesSOk = 0;
38
void SharpYuvInitGammaTables(void) {
39
assert(GAMMA_TO_LINEAR_BITS <= 16);
40
if (!kGammaTablesSOk) {
41
int v;
42
const double a = 0.09929682680944;
43
const double thresh = 0.018053968510807;
44
const double final_scale = 1 << GAMMA_TO_LINEAR_BITS;
45
// Precompute gamma to linear table.
46
{
47
const double norm = 1. / GAMMA_TO_LINEAR_TAB_SIZE;
48
const double a_rec = 1. / (1. + a);
49
for (v = 0; v <= GAMMA_TO_LINEAR_TAB_SIZE; ++v) {
50
const double g = norm * v;
51
double value;
52
if (g <= thresh * 4.5) {
53
value = g / 4.5;
54
} else {
55
value = pow(a_rec * (g + a), kGammaF);
56
}
57
kGammaToLinearTabS[v] = (uint32_t)(value * final_scale + .5);
58
}
59
// to prevent small rounding errors to cause read-overflow:
60
kGammaToLinearTabS[GAMMA_TO_LINEAR_TAB_SIZE + 1] =
61
kGammaToLinearTabS[GAMMA_TO_LINEAR_TAB_SIZE];
62
}
63
// Precompute linear to gamma table.
64
{
65
const double scale = 1. / LINEAR_TO_GAMMA_TAB_SIZE;
66
for (v = 0; v <= LINEAR_TO_GAMMA_TAB_SIZE; ++v) {
67
const double g = scale * v;
68
double value;
69
if (g <= thresh) {
70
value = 4.5 * g;
71
} else {
72
value = (1. + a) * pow(g, 1. / kGammaF) - a;
73
}
74
kLinearToGammaTabS[v] =
75
(uint32_t)(final_scale * value + 0.5);
76
}
77
// to prevent small rounding errors to cause read-overflow:
78
kLinearToGammaTabS[LINEAR_TO_GAMMA_TAB_SIZE + 1] =
79
kLinearToGammaTabS[LINEAR_TO_GAMMA_TAB_SIZE];
80
}
81
kGammaTablesSOk = 1;
82
}
83
}
84
85
static WEBP_INLINE int Shift(int v, int shift) {
86
return (shift >= 0) ? (v << shift) : (v >> -shift);
87
}
88
89
static WEBP_INLINE uint32_t FixedPointInterpolation(int v, uint32_t* tab,
90
int tab_pos_shift_right,
91
int tab_value_shift) {
92
const uint32_t tab_pos = Shift(v, -tab_pos_shift_right);
93
// fractional part, in 'tab_pos_shift' fixed-point precision
94
const uint32_t x = v - (tab_pos << tab_pos_shift_right); // fractional part
95
// v0 / v1 are in kGammaToLinearBits fixed-point precision (range [0..1])
96
const uint32_t v0 = Shift(tab[tab_pos + 0], tab_value_shift);
97
const uint32_t v1 = Shift(tab[tab_pos + 1], tab_value_shift);
98
// Final interpolation.
99
const uint32_t v2 = (v1 - v0) * x; // note: v1 >= v0.
100
const int half =
101
(tab_pos_shift_right > 0) ? 1 << (tab_pos_shift_right - 1) : 0;
102
const uint32_t result = v0 + ((v2 + half) >> tab_pos_shift_right);
103
return result;
104
}
105
106
static uint32_t ToLinearSrgb(uint16_t v, int bit_depth) {
107
const int shift = GAMMA_TO_LINEAR_TAB_BITS - bit_depth;
108
if (shift > 0) {
109
return kGammaToLinearTabS[v << shift];
110
}
111
return FixedPointInterpolation(v, kGammaToLinearTabS, -shift, 0);
112
}
113
114
static uint16_t FromLinearSrgb(uint32_t value, int bit_depth) {
115
return FixedPointInterpolation(
116
value, kLinearToGammaTabS,
117
(GAMMA_TO_LINEAR_BITS - LINEAR_TO_GAMMA_TAB_BITS),
118
bit_depth - GAMMA_TO_LINEAR_BITS);
119
}
120
121
////////////////////////////////////////////////////////////////////////////////
122
123
#define CLAMP(x, low, high) \
124
(((x) < (low)) ? (low) : (((high) < (x)) ? (high) : (x)))
125
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
126
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
127
128
static WEBP_INLINE float Roundf(float x) {
129
if (x < 0)
130
return (float)ceil((double)(x - 0.5f));
131
else
132
return (float)floor((double)(x + 0.5f));
133
}
134
135
static WEBP_INLINE float Powf(float base, float exp) {
136
return (float)pow((double)base, (double)exp);
137
}
138
139
static WEBP_INLINE float Log10f(float x) { return (float)log10((double)x); }
140
141
static float ToLinear709(float gamma) {
142
if (gamma < 0.f) {
143
return 0.f;
144
} else if (gamma < 4.5f * 0.018053968510807f) {
145
return gamma / 4.5f;
146
} else if (gamma < 1.f) {
147
return Powf((gamma + 0.09929682680944f) / 1.09929682680944f, 1.f / 0.45f);
148
}
149
return 1.f;
150
}
151
152
static float FromLinear709(float linear) {
153
if (linear < 0.f) {
154
return 0.f;
155
} else if (linear < 0.018053968510807f) {
156
return linear * 4.5f;
157
} else if (linear < 1.f) {
158
return 1.09929682680944f * Powf(linear, 0.45f) - 0.09929682680944f;
159
}
160
return 1.f;
161
}
162
163
static float ToLinear470M(float gamma) {
164
return Powf(CLAMP(gamma, 0.f, 1.f), 2.2f);
165
}
166
167
static float FromLinear470M(float linear) {
168
return Powf(CLAMP(linear, 0.f, 1.f), 1.f / 2.2f);
169
}
170
171
static float ToLinear470Bg(float gamma) {
172
return Powf(CLAMP(gamma, 0.f, 1.f), 2.8f);
173
}
174
175
static float FromLinear470Bg(float linear) {
176
return Powf(CLAMP(linear, 0.f, 1.f), 1.f / 2.8f);
177
}
178
179
static float ToLinearSmpte240(float gamma) {
180
if (gamma < 0.f) {
181
return 0.f;
182
} else if (gamma < 4.f * 0.022821585529445f) {
183
return gamma / 4.f;
184
} else if (gamma < 1.f) {
185
return Powf((gamma + 0.111572195921731f) / 1.111572195921731f, 1.f / 0.45f);
186
}
187
return 1.f;
188
}
189
190
static float FromLinearSmpte240(float linear) {
191
if (linear < 0.f) {
192
return 0.f;
193
} else if (linear < 0.022821585529445f) {
194
return linear * 4.f;
195
} else if (linear < 1.f) {
196
return 1.111572195921731f * Powf(linear, 0.45f) - 0.111572195921731f;
197
}
198
return 1.f;
199
}
200
201
static float ToLinearLog100(float gamma) {
202
// The function is non-bijective so choose the middle of [0, 0.01].
203
const float mid_interval = 0.01f / 2.f;
204
return (gamma <= 0.0f) ? mid_interval
205
: Powf(10.0f, 2.f * (MIN(gamma, 1.f) - 1.0f));
206
}
207
208
static float FromLinearLog100(float linear) {
209
return (linear < 0.01f) ? 0.0f : 1.0f + Log10f(MIN(linear, 1.f)) / 2.0f;
210
}
211
212
static float ToLinearLog100Sqrt10(float gamma) {
213
// The function is non-bijective so choose the middle of [0, 0.00316227766f[.
214
const float mid_interval = 0.00316227766f / 2.f;
215
return (gamma <= 0.0f) ? mid_interval
216
: Powf(10.0f, 2.5f * (MIN(gamma, 1.f) - 1.0f));
217
}
218
219
static float FromLinearLog100Sqrt10(float linear) {
220
return (linear < 0.00316227766f) ? 0.0f
221
: 1.0f + Log10f(MIN(linear, 1.f)) / 2.5f;
222
}
223
224
static float ToLinearIec61966(float gamma) {
225
if (gamma <= -4.5f * 0.018053968510807f) {
226
return Powf((-gamma + 0.09929682680944f) / -1.09929682680944f, 1.f / 0.45f);
227
} else if (gamma < 4.5f * 0.018053968510807f) {
228
return gamma / 4.5f;
229
}
230
return Powf((gamma + 0.09929682680944f) / 1.09929682680944f, 1.f / 0.45f);
231
}
232
233
static float FromLinearIec61966(float linear) {
234
if (linear <= -0.018053968510807f) {
235
return -1.09929682680944f * Powf(-linear, 0.45f) + 0.09929682680944f;
236
} else if (linear < 0.018053968510807f) {
237
return linear * 4.5f;
238
}
239
return 1.09929682680944f * Powf(linear, 0.45f) - 0.09929682680944f;
240
}
241
242
static float ToLinearBt1361(float gamma) {
243
if (gamma < -0.25f) {
244
return -0.25f;
245
} else if (gamma < 0.f) {
246
return Powf((gamma - 0.02482420670236f) / -0.27482420670236f, 1.f / 0.45f) /
247
-4.f;
248
} else if (gamma < 4.5f * 0.018053968510807f) {
249
return gamma / 4.5f;
250
} else if (gamma < 1.f) {
251
return Powf((gamma + 0.09929682680944f) / 1.09929682680944f, 1.f / 0.45f);
252
}
253
return 1.f;
254
}
255
256
static float FromLinearBt1361(float linear) {
257
if (linear < -0.25f) {
258
return -0.25f;
259
} else if (linear < 0.f) {
260
return -0.27482420670236f * Powf(-4.f * linear, 0.45f) + 0.02482420670236f;
261
} else if (linear < 0.018053968510807f) {
262
return linear * 4.5f;
263
} else if (linear < 1.f) {
264
return 1.09929682680944f * Powf(linear, 0.45f) - 0.09929682680944f;
265
}
266
return 1.f;
267
}
268
269
static float ToLinearPq(float gamma) {
270
if (gamma > 0.f) {
271
const float pow_gamma = Powf(gamma, 32.f / 2523.f);
272
const float num = MAX(pow_gamma - 107.f / 128.f, 0.0f);
273
const float den = MAX(2413.f / 128.f - 2392.f / 128.f * pow_gamma, FLT_MIN);
274
return Powf(num / den, 4096.f / 653.f);
275
}
276
return 0.f;
277
}
278
279
static float FromLinearPq(float linear) {
280
if (linear > 0.f) {
281
const float pow_linear = Powf(linear, 653.f / 4096.f);
282
const float num = 107.f / 128.f + 2413.f / 128.f * pow_linear;
283
const float den = 1.0f + 2392.f / 128.f * pow_linear;
284
return Powf(num / den, 2523.f / 32.f);
285
}
286
return 0.f;
287
}
288
289
static float ToLinearSmpte428(float gamma) {
290
return Powf(MAX(gamma, 0.f), 2.6f) / 0.91655527974030934f;
291
}
292
293
static float FromLinearSmpte428(float linear) {
294
return Powf(0.91655527974030934f * MAX(linear, 0.f), 1.f / 2.6f);
295
}
296
297
// Conversion in BT.2100 requires RGB info. Simplify to gamma correction here.
298
static float ToLinearHlg(float gamma) {
299
if (gamma < 0.f) {
300
return 0.f;
301
} else if (gamma <= 0.5f) {
302
return Powf((gamma * gamma) * (1.f / 3.f), 1.2f);
303
}
304
return Powf((expf((gamma - 0.55991073f) / 0.17883277f) + 0.28466892f) / 12.0f,
305
1.2f);
306
}
307
308
static float FromLinearHlg(float linear) {
309
linear = Powf(linear, 1.f / 1.2f);
310
if (linear < 0.f) {
311
return 0.f;
312
} else if (linear <= (1.f / 12.f)) {
313
return sqrtf(3.f * linear);
314
}
315
return 0.17883277f * logf(12.f * linear - 0.28466892f) + 0.55991073f;
316
}
317
318
uint32_t SharpYuvGammaToLinear(uint16_t v, int bit_depth,
319
SharpYuvTransferFunctionType transfer_type) {
320
float v_float, linear;
321
if (transfer_type == kSharpYuvTransferFunctionSrgb) {
322
return ToLinearSrgb(v, bit_depth);
323
}
324
v_float = (float)v / ((1 << bit_depth) - 1);
325
switch (transfer_type) {
326
case kSharpYuvTransferFunctionBt709:
327
case kSharpYuvTransferFunctionBt601:
328
case kSharpYuvTransferFunctionBt2020_10Bit:
329
case kSharpYuvTransferFunctionBt2020_12Bit:
330
linear = ToLinear709(v_float);
331
break;
332
case kSharpYuvTransferFunctionBt470M:
333
linear = ToLinear470M(v_float);
334
break;
335
case kSharpYuvTransferFunctionBt470Bg:
336
linear = ToLinear470Bg(v_float);
337
break;
338
case kSharpYuvTransferFunctionSmpte240:
339
linear = ToLinearSmpte240(v_float);
340
break;
341
case kSharpYuvTransferFunctionLinear:
342
return v;
343
case kSharpYuvTransferFunctionLog100:
344
linear = ToLinearLog100(v_float);
345
break;
346
case kSharpYuvTransferFunctionLog100_Sqrt10:
347
linear = ToLinearLog100Sqrt10(v_float);
348
break;
349
case kSharpYuvTransferFunctionIec61966:
350
linear = ToLinearIec61966(v_float);
351
break;
352
case kSharpYuvTransferFunctionBt1361:
353
linear = ToLinearBt1361(v_float);
354
break;
355
case kSharpYuvTransferFunctionSmpte2084:
356
linear = ToLinearPq(v_float);
357
break;
358
case kSharpYuvTransferFunctionSmpte428:
359
linear = ToLinearSmpte428(v_float);
360
break;
361
case kSharpYuvTransferFunctionHlg:
362
linear = ToLinearHlg(v_float);
363
break;
364
default:
365
assert(0);
366
linear = 0;
367
break;
368
}
369
return (uint32_t)Roundf(linear * ((1 << 16) - 1));
370
}
371
372
uint16_t SharpYuvLinearToGamma(uint32_t v, int bit_depth,
373
SharpYuvTransferFunctionType transfer_type) {
374
float v_float, linear;
375
if (transfer_type == kSharpYuvTransferFunctionSrgb) {
376
return FromLinearSrgb(v, bit_depth);
377
}
378
v_float = (float)v / ((1 << 16) - 1);
379
switch (transfer_type) {
380
case kSharpYuvTransferFunctionBt709:
381
case kSharpYuvTransferFunctionBt601:
382
case kSharpYuvTransferFunctionBt2020_10Bit:
383
case kSharpYuvTransferFunctionBt2020_12Bit:
384
linear = FromLinear709(v_float);
385
break;
386
case kSharpYuvTransferFunctionBt470M:
387
linear = FromLinear470M(v_float);
388
break;
389
case kSharpYuvTransferFunctionBt470Bg:
390
linear = FromLinear470Bg(v_float);
391
break;
392
case kSharpYuvTransferFunctionSmpte240:
393
linear = FromLinearSmpte240(v_float);
394
break;
395
case kSharpYuvTransferFunctionLinear:
396
return v;
397
case kSharpYuvTransferFunctionLog100:
398
linear = FromLinearLog100(v_float);
399
break;
400
case kSharpYuvTransferFunctionLog100_Sqrt10:
401
linear = FromLinearLog100Sqrt10(v_float);
402
break;
403
case kSharpYuvTransferFunctionIec61966:
404
linear = FromLinearIec61966(v_float);
405
break;
406
case kSharpYuvTransferFunctionBt1361:
407
linear = FromLinearBt1361(v_float);
408
break;
409
case kSharpYuvTransferFunctionSmpte2084:
410
linear = FromLinearPq(v_float);
411
break;
412
case kSharpYuvTransferFunctionSmpte428:
413
linear = FromLinearSmpte428(v_float);
414
break;
415
case kSharpYuvTransferFunctionHlg:
416
linear = FromLinearHlg(v_float);
417
break;
418
default:
419
assert(0);
420
linear = 0;
421
break;
422
}
423
return (uint16_t)Roundf(linear * ((1 << bit_depth) - 1));
424
}
425
426