-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.cpp
143 lines (110 loc) · 3.81 KB
/
encode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "encode.h"
#include "float3.h"
// http://graphicrants.blogspot.fr/2009/04/rgbm-color-encoding.html
// M matrix, for encoding
const static float M[] = {0.2209, 0.3390, 0.4184, 0.1138, 0.6780, 0.7319, 0.0102, 0.1130, 0.2969};
// Inverse M matrix, for decoding
const static float InverseM[] = {6.0013, -2.700, -1.7995, -1.332, 3.1029, -5.7720, 0.3007, -1.088, 5.6268};
float3 mul(const float* v, const float* M)
{
float3 result;
result[0] = v[0] * M[0] + v[1] * M[3] + v[2] * M[6];
result[1] = v[0] * M[1] + v[1] * M[4] + v[2] * M[7];
result[2] = v[0] * M[2] + v[1] * M[5] + v[2] * M[8];
return result;
}
float4 LogLuvEncode(const float* vRGB)
{
float4 vResult;
float3 Xp_Y_XYZp;
float3 test;
Xp_Y_XYZp = mul(vRGB, M);
Xp_Y_XYZp = Xp_Y_XYZp.max(1e-6f);
vResult[0] = Xp_Y_XYZp[0] / Xp_Y_XYZp[2];
vResult[1] = Xp_Y_XYZp[1] / Xp_Y_XYZp[2];
float Le = 2.0f * log2(Xp_Y_XYZp[1]) + 127.0f;
vResult[3] = frac(Le);
vResult[2] = (Le - (floor(vResult[3] * 255.0f)) / 255.0f) / 255.0f;
return vResult;
}
void encodeLUV(uint8_t* luvDst, const float* rgbSrc)
{
float4 result = LogLuvEncode(rgbSrc);
luvDst[0] = uint8_t(result[0] * 255);
luvDst[1] = uint8_t(result[1] * 255);
luvDst[2] = uint8_t(result[2] * 255);
luvDst[3] = uint8_t(result[3] * 255);
}
float uncharted2Tonemap(float x)
{
float A = 0.15;
float B = 0.50;
float C = 0.10;
float D = 0.20;
float E = 0.02;
float F = 0.30;
return ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F;
}
void tonemap(uint8_t* rgbDst, const float* rgbSrc)
{
float W = 11.2;
float rgb[3];
float exposureBias = 2.0;
rgb[0] = uncharted2Tonemap(rgbSrc[0] * exposureBias);
rgb[1] = uncharted2Tonemap(rgbSrc[1] * exposureBias);
rgb[2] = uncharted2Tonemap(rgbSrc[2] * exposureBias);
float whiteScale = 1.0f / uncharted2Tonemap(W);
rgb[0] *= whiteScale;
rgb[1] *= whiteScale;
rgb[2] *= whiteScale;
float gammaInv = 1.0 / 2.2;
rgbDst[0] = (uint8_t)floor(255 * clamp(powf(rgb[0], gammaInv), 0.0f, 1.0f));
rgbDst[1] = (uint8_t)floor(255 * clamp(powf(rgb[1], gammaInv), 0.0f, 1.0f));
rgbDst[2] = (uint8_t)floor(255 * clamp(powf(rgb[2], gammaInv), 0.0f, 1.0f));
}
float3 LogLuvDecode(const float* vLogLuv)
{
float Le = vLogLuv[2] * 255 + vLogLuv[3];
float3 Xp_Y_XYZp;
Xp_Y_XYZp[1] = exp2((Le - 127) / 2.0f);
Xp_Y_XYZp[2] = Xp_Y_XYZp[1] / vLogLuv[1];
Xp_Y_XYZp[0] = vLogLuv[0] * Xp_Y_XYZp[2];
float3 vRGB;
vRGB = mul(&Xp_Y_XYZp[0], InverseM);
return vRGB.max(0.0);
}
void decodeLUV(float* rgb, const uint8_t* luv)
{
float4 value;
value[0] = luv[0] * 1.0 / 255.0;
value[1] = luv[1] * 1.0 / 255.0;
value[2] = luv[2] * 1.0 / 255.0;
value[3] = luv[3] * 1.0 / 255.0;
float3 result = LogLuvDecode(value.ptr());
rgb[0] = result[0];
rgb[1] = result[1];
rgb[2] = result[2];
}
static float RGBMMaxRange = 8.0;
void encodeRGBM(uint8_t* rgbm, const float* rgb)
{
// in our case,
const float kRGBMMaxRange = RGBMMaxRange;
const float kOneOverRGBMMaxRange = 1.0f / kRGBMMaxRange;
// encode to RGBM, c = ARGB colors in 0..1 floats
float r = rgb[0] * kOneOverRGBMMaxRange;
float g = rgb[1] * kOneOverRGBMMaxRange;
float b = rgb[2] * kOneOverRGBMMaxRange;
float a = fmaxf(fmax(r, g), fmaxf(b, 1e-6f));
a = ceilf(a * 255.0f) / 255.0f;
rgbm[0] = uint8_t(fminf(r / a, 1.0f) * 255);
rgbm[1] = uint8_t(fminf(g / a, 1.0f) * 255);
rgbm[2] = uint8_t(fminf(b / a, 1.0f) * 255);
rgbm[3] = uint8_t(fminf(a, 1.0f) * 255);
}
void decodeRGBM(float* rgb, const uint8_t* rgbm)
{
rgb[0] = rgbm[0] / 255.0f * RGBMMaxRange * rgbm[3] / 255.0f;
rgb[1] = rgbm[1] / 255.0f * RGBMMaxRange * rgbm[3] / 255.0f;
rgb[2] = rgbm[2] / 255.0f * RGBMMaxRange * rgbm[3] / 255.0f;
}