-
Notifications
You must be signed in to change notification settings - Fork 1
/
PD-LogC.h
231 lines (203 loc) · 8.01 KB
/
PD-LogC.h
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2022-present Contributors to the photographic-dctl project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/mikaelsundell/photographic-dctls
// clang-format on
// logCColor
struct LogCColor
{
float stop;
float r;
float g;
float b;
};
__CONSTANT__ struct LogCColor logC_colors[] = {
{-8, 0.01f, 0.01f, 0.01f},
{-7, 0.05f, 0.05f, 0.05f},
{-6, 0.10f, 0.10f, 0.10f},
{-5, 0.40f, 0.25f, 0.60f},
{-4, 0.20f, 0.45f, 0.70f},
{-3, 0.40f, 0.60f, 0.95f},
{-2, 0.40f, 0.60f, 0.25f},
{-1, 0.60f, 0.90f, 0.55f},
{0, 0.50f, 0.50f, 0.50f},
{1, 1.00f, 0.95f, 0.25f},
{2, 0.90f, 0.60f, 0.25f},
{3, 0.90f, 0.50f, 0.25f},
{4, 0.90f, 0.35f, 0.30f},
{5, 0.90f, 0.30f, 0.20f},
{6, 0.90f, 0.90f, 0.90f},
{7, 0.95f, 0.95f, 0.95f},
{8, 0.99f, 0.99f, 0.99f}
};
#define logC_stops 17
// logC3 curve
struct LogC3Curve
{
int ei;
float cut;
float a;
float b;
float c;
float d;
float e;
float f;
};
__DEVICE__ struct LogC3Curve logC3_curve(int ei) {
struct LogC3Curve cv;
if (ei == EI160) {
cv.ei = 160; cv.cut = 0.005561; cv.a = 5.555556; cv.b = 0.080216; cv.c = 0.269036; cv.d = 0.381991; cv.e = 5.842037; cv.f = 0.092778;
}
else if (ei == EI200) {
cv.ei = 200; cv.cut = 0.006208; cv.a = 5.555556; cv.b = 0.076621; cv.c = 0.266007; cv.d = 0.382478; cv.e = 5.776265; cv.f = 0.092782;
}
else if (ei == EI250) {
cv.ei = 250; cv.cut = 0.006871; cv.a = 5.555556; cv.b = 0.072941; cv.c = 0.262978; cv.d = 0.382966; cv.e = 5.710494; cv.f = 0.092786;
}
else if (ei == EI320) {
cv.ei = 320; cv.cut = 0.007622; cv.a = 5.555556; cv.b = 0.068768; cv.c = 0.259627; cv.d = 0.383508; cv.e = 5.637732; cv.f = 0.092791;
}
else if (ei == EI400) {
cv.ei = 400; cv.cut = 0.008318; cv.a = 5.555556; cv.b = 0.064901; cv.c = 0.256598; cv.d = 0.383999; cv.e = 5.571960; cv.f = 0.092795;
}
else if (ei == EI500) {
cv.ei = 500; cv.cut = 0.009031; cv.a = 5.555556; cv.b = 0.060939; cv.c = 0.253569; cv.d = 0.384493; cv.e = 5.506188; cv.f = 0.092800;
}
else if (ei == EI640) {
cv.ei = 640; cv.cut = 0.009840; cv.a = 5.555556; cv.b = 0.056443; cv.c = 0.250219; cv.d = 0.385040; cv.e = 5.433426; cv.f = 0.092805;
}
else if (ei == EI800) {
cv.ei = 800; cv.cut = 0.010591; cv.a = 5.555556; cv.b = 0.052272; cv.c = 0.247190; cv.d = 0.385537; cv.e = 5.367655; cv.f = 0.092809;
}
else if (ei == EI1000) {
cv.ei = 1000; cv.cut = 0.011361; cv.a = 5.555556; cv.b = 0.047996; cv.c = 0.244161; cv.d = 0.386036; cv.e = 5.301883; cv.f = 0.092814;
}
else if (ei == EI1280) {
cv.ei = 1280; cv.cut = 0.012235; cv.a = 5.555556; cv.b = 0.043137; cv.c = 0.240810; cv.d = 0.386590; cv.e = 5.229121; cv.f = 0.092819;
}
else if (ei == EI1600) {
cv.ei = 1600; cv.cut = 0.013047; cv.a = 5.555556; cv.b = 0.038625; cv.c = 0.237781; cv.d = 0.387093; cv.e = 5.163350; cv.f = 0.092824;
}
return cv;
}
__DEVICE__ float LogC3Curve_lin_logC3(struct LogC3Curve cv, float lin) {
return ((lin > cv.cut) ? cv.c * log10_f(cv.a * lin + cv.b) + cv.d : cv.e * lin + cv.f);
}
__DEVICE__ float LogC3Curve_logC3_lin(struct LogC3Curve cv, float log) {
float lin = ((log > cv.e * cv.cut + cv.f) ? (pow_f(10, (log - cv.d) / cv.c) - cv.b) / cv.a : (log - cv.f) / cv.e);
return lin;
}
// logC4 curve
struct LogC4Curve
{
float a;
float b;
float c;
float d;
float s;
float t;
};
__DEVICE__ struct LogC4Curve logC4_curve() {
struct LogC4Curve cv;
cv.a = (pow_f(2.0, 18.0) - 16.0) / 117.45;
cv.b = (1023.0 - 95.0) / 1023;
cv.c = 95.0 / 1023.0;
cv.s = (7 * log_f(2.0) * pow_f(2.0, 7 - 14 * cv.c / cv.b)) / (cv.a * cv.b);
cv.t = (pow_f(2.0, 14.0 * (-cv.c / cv.b) + 6.0) - 64.0) / cv.a;
return cv;
}
__DEVICE__ float LogC4Curve_lin_logC4(struct LogC4Curve cv, float lin) {
return ((lin < cv.t) ? ((lin - cv.t) / cv.s) : (log2_f(cv.a * lin + 64) - 6.0) / 14.0 * cv.b + cv.c);
}
__DEVICE__ float LogC4Curve_logC4_lin(struct LogC4Curve cv, float log) {
return ((log < 0.0) ? (log * cv.s + cv.t) : (pow_f(2.0, (14.0 * (log - cv.c) / cv.b + 6.0)) - 64.0) / cv.a);
}
// logC colorspace
struct LogCColorspace
{
struct Matrix logC_matrix;
struct Matrix xyz_matrix;
};
// logC4 colorspace
struct LogC4Colorspace
{
struct Matrix logC4_matrix;
struct Matrix xyz_matrix;
};
__DEVICE__ struct LogCColorspace logC3_colorspace() {
struct LogCColorspace cs;
// colortool --inputcolorspace AWG3 -v
// logC3 matrix
cs.logC_matrix.m00 = 1.789066; cs.logC_matrix.m01 = -0.482534; cs.logC_matrix.m02 = -0.200076;
cs.logC_matrix.m03 = -0.639849; cs.logC_matrix.m04 = 1.396400; cs.logC_matrix.m05 = 0.194432;
cs.logC_matrix.m06 = -0.041532; cs.logC_matrix.m07 = 0.082335; cs.logC_matrix.m08 = 0.878868;
// xyz matrix
cs.xyz_matrix.m00 = 0.638008; cs.xyz_matrix.m01 = 0.214704; cs.xyz_matrix.m02 = 0.097744;
cs.xyz_matrix.m03 = 0.291954; cs.xyz_matrix.m04 = 0.823841; cs.xyz_matrix.m05 = -0.115795;
cs.xyz_matrix.m06 = 0.002798; cs.xyz_matrix.m07 = -0.067034; cs.xyz_matrix.m08 = 1.153294;
return cs;
}
__DEVICE__ struct LogCColorspace logC4_colorspace() {
struct LogCColorspace cs;
// colortool --inputcolorspace AWG4 -v
// logC4 matrix
cs.logC_matrix.m00 = 1.5092155; cs.logC_matrix.m01 = -0.2505973; cs.logC_matrix.m02 = -0.1688115;
cs.logC_matrix.m03 = -0.4915455; cs.logC_matrix.m04 = 1.3612455; cs.logC_matrix.m05 = 0.0972829;
cs.logC_matrix.m06 = 0.0000000; cs.logC_matrix.m07 = 0.0000000; cs.logC_matrix.m08 = 0.9182250;
// xyz matrix
cs.xyz_matrix.m00 = 0.7048583; cs.xyz_matrix.m01 = 0.1297603; cs.xyz_matrix.m02 = 0.1158373;
cs.xyz_matrix.m03 = 0.2545242; cs.xyz_matrix.m04 = 0.7814777; cs.xyz_matrix.m05 = -0.0360019;
cs.xyz_matrix.m06 = 0.0000000; cs.xyz_matrix.m07 = 0.0000000; cs.xyz_matrix.m08 = 1.0890578;
return cs;
}
__DEVICE__ float3 LogCColorspace_xyz_logC3(struct LogCColorspace cs, float3 xyz) {
return mult_matrix(xyz, cs.logC_matrix);
}
__DEVICE__ float3 LogCColorspace_logC3_xyz(struct LogCColorspace cs, float3 logC3) {
return mult_matrix(logC3, cs.xyz_matrix);
}
__DEVICE__ float3 LogCColorspace_xyz_logC4(struct LogCColorspace cs, float3 xyz) {
return mult_matrix(xyz, cs.logC_matrix);
}
__DEVICE__ float3 LogCColorspace_logC4_xyz(struct LogCColorspace cs, float3 logC4) {
return mult_matrix(logC4, cs.xyz_matrix);
}
// convert linear to LogC3
__DEVICE__ float3 lin_logC3(float3 rgb, int ei) {
struct LogC3Curve cv = logC3_curve(ei);
return make_float3(LogC3Curve_lin_logC3(cv, rgb.x), LogC3Curve_lin_logC3(cv, rgb.y), LogC3Curve_lin_logC3(cv, rgb.z));
}
// convert LogC3 to linear
__DEVICE__ float3 logC3_lin(float3 rgb, int ei) {
struct LogC3Curve cv = logC3_curve(ei);
return make_float3(LogC3Curve_logC3_lin(cv, rgb.x), LogC3Curve_logC3_lin(cv, rgb.y), LogC3Curve_logC3_lin(cv, rgb.z));
}
// convert LogC3 to xyz
__DEVICE__ float3 xyz_logC3(float3 rgb) {
struct LogCColorspace cs = logC3_colorspace();
return LogCColorspace_xyz_logC3(cs, rgb);
}
// convert XYZ to LogC3
__DEVICE__ float3 logC3_xyz(float3 rgb) {
struct LogCColorspace cs = logC3_colorspace();
return LogCColorspace_logC3_xyz(cs, rgb);
}
// convert linear to LogC4
__DEVICE__ float3 lin_logC4(float3 rgb) {
struct LogC4Curve cv = logC4_curve();
return make_float3(LogC4Curve_lin_logC4(cv, rgb.x), LogC4Curve_lin_logC4(cv, rgb.y), LogC4Curve_lin_logC4(cv, rgb.z));
}
// convert LogC4 to linear
__DEVICE__ float3 logC4_lin(float3 rgb) {
struct LogC4Curve cv = logC4_curve();
return make_float3(LogC4Curve_logC4_lin(cv, rgb.x), LogC4Curve_logC4_lin(cv, rgb.y), LogC4Curve_logC4_lin(cv, rgb.z));
}
// convert LogC4 to xyz
__DEVICE__ float3 xyz_logC4(float3 rgb) {
struct LogCColorspace cs = logC4_colorspace();
return LogCColorspace_xyz_logC4(cs, rgb);
}
// convert XYZ to LogC4
__DEVICE__ float3 logC4_xyz(float3 rgb) {
struct LogCColorspace cs = logC4_colorspace();
return LogCColorspace_logC4_xyz(cs, rgb);
}