Skip to content

Commit e0c3653

Browse files
committed
Added initial libraries
1 parent 3164011 commit e0c3653

10 files changed

+2824
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
# aces-lib
22
C++ Implementation of ACES Libraries
3+
4+
Derived from [aces-dev](https://github.com/ampas/aces-dev) CTL implementation to work in C++11 and above
5+
6+
Library uses std::array for matrices
7+
8+
Includes custom *ACESlib.CustomDefs.h* file to define various constants and functions for dependencies
9+

lib/ACESlib.CustomDefs.h

Lines changed: 347 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,347 @@
1+
2+
// Custom Definitions for ACES Libraries
3+
//
4+
5+
#pragma once
6+
#include <cmath>
7+
#include <array>
8+
using namespace std;
9+
10+
11+
12+
#ifndef HALF_MIN
13+
#define HALF_MIN 5.96046448e-08f // Smallest positive half
14+
#endif
15+
#ifndef HALF_MAX
16+
#define HALF_MAX 65504.0f // Largest positive half
17+
#endif
18+
#ifndef HALF_POS_INF
19+
#define HALF_POS_INF 0x7c00 // Half Positive Infinity
20+
#endif
21+
#ifndef HALF_NEG_INF
22+
#define HALF_NEG_INF 0xfc00 // Half Negative Infinity
23+
#endif
24+
#ifndef FLT_NAN
25+
#define FLT_NAN 0x7fffffff
26+
#endif
27+
28+
struct Chromaticities
29+
{
30+
array <float, 2> red; // CIE xy coordinates of red primary
31+
array <float, 2> green; // CIE xy coordinates of green primary
32+
array <float, 2> blue; // CIE xy coordinates of blue primary
33+
array <float, 2> white; // CIE xy coordinates of white point
34+
};
35+
36+
37+
float pow10f(float x) {
38+
return powf(10.0f, x);
39+
}
40+
41+
float dot_f3_f3(const array <float, 3> &x, const array <float, 3> &y) {
42+
return x[0] * y[0] + x[1] * y[1] + x[2] * y[2];
43+
}
44+
45+
array <float, 3> mult_f_f3(float f, const array <float, 3> &x) {
46+
47+
array <float, 3> r;
48+
49+
r[0] = f * x[0];
50+
r[1] = f * x[1];
51+
r[2] = f * x[2];
52+
53+
return r;
54+
}
55+
56+
array <array <float, 3>, 3> transpose_f33(array <array <float, 3>, 3> &a) {
57+
58+
array <array <float, 3>, 3> r;
59+
60+
for (int i = 0; i < 3; ++i) {
61+
62+
for (int j = 0; j < 3; ++j) {
63+
r[i][j] = a[j][i];
64+
}
65+
}
66+
67+
return r;
68+
}
69+
70+
array <array <float, 4>, 4> transpose_f44(const array <array <float, 4>, 4> &a) {
71+
72+
array <array <float, 4>, 4> r;
73+
74+
for (int i = 0; i < 4; ++i) {
75+
76+
for (int j = 0; j < 4; ++j) {
77+
r[i][j] = a[j][i];
78+
}
79+
}
80+
81+
return r;
82+
}
83+
84+
array <array <float, 3>, 3> mult_f_f33(float f, const array <array <float, 3>, 3> &a) {
85+
86+
array <array <float, 3>, 3> r;
87+
88+
for (int i = 0; i < 4; i++) {
89+
for (int j = 0; j < 4; j++) {
90+
r[i][j] = f * a[i][j];
91+
}
92+
}
93+
94+
return r;
95+
}
96+
97+
array <array <float, 4>, 4> mult_f_f44(float f, const array <array <float, 4>, 4> &a) {
98+
99+
array <array <float, 4>, 4> r;
100+
101+
for (int i = 0; i < 4; ++i) {
102+
for (int j = 0; j < 4; ++j) {
103+
r[i][j] = f * a[i][j];
104+
}
105+
}
106+
107+
return r;
108+
}
109+
110+
111+
array <float, 3> mult_f3_f33(const array <float, 3>& x, const array <array <float, 3>, 3>& a) {
112+
array <float, 3> r;
113+
114+
for (int i = 0; i < 3; ++i) {
115+
116+
r[i] = 0.0f;
117+
118+
for (int j = 0; j < 3; ++j) {
119+
r[i] = r[i] + x[j] * a[j][i];
120+
}
121+
}
122+
return r;
123+
}
124+
125+
126+
array <float, 3> mult_f3_f44(const array <float, 3> &x, const array <array <float, 4>, 4> &a) {
127+
128+
array <float, 3> r;
129+
130+
for (int i = 0; i < 3; ++i) {
131+
132+
r[i] = 0.0f;
133+
134+
for (int j = 0; j < 3; ++j) {
135+
r[i] = r[i] + x[j] * a[j][i];
136+
}
137+
138+
r[i] = r[i] + a[3][i];
139+
}
140+
141+
float s = 1.0f / (x[0] * a[0][3] + x[1] * a[1][3] + x[2] * a[2][3] + a[3][3]);
142+
143+
for (int k = 0; k < 3; ++k) {
144+
r[k] = r[k] * s;
145+
}
146+
147+
return r;
148+
}
149+
150+
151+
array <array <float, 3>, 3> mult_f33_f33(const array <array <float, 3>, 3> &a, const array <array <float, 3>, 3> &b) {
152+
153+
array <array <float, 3>, 3> r;
154+
155+
for (int i = 0; i < 3; ++i) {
156+
for (int j = 0; j < 3; ++j) {
157+
158+
r[i][j] = 0.0f;
159+
160+
for (int k = 0; k < 3; ++k) {
161+
r[i][j] = r[i][j] + a[i][k] * b[k][j];
162+
}
163+
}
164+
}
165+
166+
return r;
167+
}
168+
169+
170+
array <array <float, 4>, 4> mult_f44_f44(const array <array <float, 4>, 4> &a, const array <array <float, 4>, 4> &b) {
171+
172+
array <array <float, 4>, 4> r;
173+
174+
for (int i = 0; i < 4; ++i) {
175+
for (int j = 0; j < 4; ++j) {
176+
177+
r[i][j] = 0.0f;
178+
179+
for (int k = 0; k < 4; ++k) {
180+
r[i][j] = r[i][j] + a[i][k] * b[k][j];
181+
}
182+
}
183+
}
184+
185+
return r;
186+
}
187+
188+
189+
array <array <float, 3>, 3> add_f33_f33(const array <array <float, 3>, 3> &a, const array <array <float, 3>, 3> &b) {
190+
191+
array <array <float, 3>, 3> r;
192+
193+
for (int i = 0; i < 3; ++i) {
194+
for (int j = 0; j < 3; ++j) {
195+
r[i][j] = a[i][j] + b[i][j];
196+
}
197+
}
198+
199+
return r;
200+
}
201+
202+
203+
array <array <float, 3>, 3> invert_f33(const array <array <float, 3>, 3>& a) {
204+
205+
float determinant = 0.0f;
206+
array <array <float, 3>, 3> res = { { {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}} };
207+
208+
for (int i = 0; i < 3; i++)
209+
determinant += (a[0][i] * (a[1][(i + 1) % 3] * a[2][(i + 2) % 3] - a[1][(i + 2) % 3] * a[2][(i + 1) % 3]));
210+
211+
for (int i = 0; i < 3; i++) {
212+
for (int j = 0; j < 3; j++)
213+
res[i][j] = ((a[(j + 1) % 3][(i + 1) % 3] * a[(j + 2) % 3][(i + 2) % 3]) - (a[(j + 1) % 3][(i + 2) % 3] * a[(j + 2) % 3][(i + 1) % 3])) / determinant;
214+
}
215+
216+
return res;
217+
218+
}
219+
220+
array <array <float, 4>, 4> invert_f44(const array <array <float, 4>, 4>& a) {
221+
222+
float determinant = 0.0f;
223+
array <array <float, 4>, 4> res = { { {1.0f, 0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 1.0f} } };
224+
int len = 3;
225+
226+
if (a[0][3] != 0 || a[1][3] != 0 || a[2][3] != 0 || a[3][3] != 1)
227+
len = 4;
228+
229+
for (int i = 0; i < len; i++)
230+
determinant += (a[0][i] * (a[1][(i + 1) % len] * a[2][(i + 2) % len] - a[1][(i + 2) % len] * a[2][(i + 1) % len]));
231+
232+
for (int i = 0; i < len; i++) {
233+
for (int j = 0; j < len; j++)
234+
res[i][j] = ((a[(j + 1) % len][(i + 1) % len] * a[(j + 2) % len][(i + 2) % len]) - (a[(j + 1) % len][(i + 2) % len] * a[(j + 2) % len][(i + 1) % len])) / determinant;
235+
}
236+
237+
return res;
238+
239+
}
240+
241+
242+
array <array <float, 4>, 4> RGBtoXYZ(const Chromaticities &chroma, float Y)
243+
{
244+
//
245+
// X and Z values of RGB value (1, 1, 1), or "white"
246+
//
247+
248+
float X = chroma.white[0] * Y / chroma.white[1];
249+
float Z = (1.0f - chroma.white[0] - chroma.white[1]) * Y / chroma.white[1];
250+
251+
//
252+
// Scale factors for matrix rows
253+
//
254+
255+
float d = chroma.red[0] * (chroma.blue[1] - chroma.green[1]) +
256+
chroma.blue[0] * (chroma.green[1] - chroma.red[1]) +
257+
chroma.green[0] * (chroma.red[1] - chroma.blue[1]);
258+
259+
float Sr = (X * (chroma.blue[1] - chroma.green[1]) -
260+
chroma.green[0] * (Y * (chroma.blue[1] - 1.0f) +
261+
chroma.blue[1] * (X + Z)) +
262+
chroma.blue[0] * (Y * (chroma.green[1] - 1.0f) +
263+
chroma.green[1] * (X + Z))) / d;
264+
265+
float Sg = (X * (chroma.red[1] - chroma.blue[1]) +
266+
chroma.red[0] * (Y * (chroma.blue[1] - 1.0f) +
267+
chroma.blue[1] * (X + Z)) -
268+
chroma.blue[0] * (Y * (chroma.red[1] - 1.0f) +
269+
chroma.red[1] * (X + Z))) / d;
270+
271+
float Sb = (X * (chroma.green[1] - chroma.red[1]) -
272+
chroma.red[0] * (Y * (chroma.green[1] - 1.0f) +
273+
chroma.green[1] * (X + Z)) +
274+
chroma.green[0] * (Y * (chroma.red[1] - 1.0f) +
275+
chroma.red[1] * (X + Z))) / d;
276+
277+
//
278+
// Assemble the matrix
279+
//
280+
281+
array <array <float, 4>, 4> M;
282+
283+
M[0][0] = Sr * chroma.red[0];
284+
M[0][1] = Sr * chroma.red[1];
285+
M[0][2] = Sr * (1.0f - chroma.red[0] - chroma.red[1]);
286+
M[0][3] = 0;
287+
288+
M[1][0] = Sg * chroma.green[0];
289+
M[1][1] = Sg * chroma.green[1];
290+
M[1][2] = Sg * (1.0f - chroma.green[0] - chroma.green[1]);
291+
M[1][3] = 0;
292+
293+
M[2][0] = Sb * chroma.blue[0];
294+
M[2][1] = Sb * chroma.blue[1];
295+
M[2][2] = Sb * (1.0f - chroma.blue[0] - chroma.blue[1]);
296+
M[2][3] = 0;
297+
298+
M[3][0] = 0;
299+
M[3][1] = 0;
300+
M[3][2] = 0;
301+
M[3][3] = 1;
302+
303+
return M;
304+
}
305+
306+
array <array <float, 4>, 4> XYZtoRGB(const Chromaticities &c, float Y) {
307+
308+
array <array <float, 4>, 4> M = RGBtoXYZ(c, Y);
309+
M = invert_f44(M);
310+
return M;
311+
}
312+
313+
314+
template <size_t int1D_N>
315+
float interpolate1D(const array <array <float, int1D_N>, 2> &table, float p)
316+
{
317+
int size = table.size();
318+
319+
if (size < 1)
320+
return 0;
321+
322+
if (p < table[0][0])
323+
return table[0][1];
324+
325+
if (p >= table[size - 1][0])
326+
return table[size - 1][1];
327+
328+
int i = 0;
329+
int j = size;
330+
331+
while (i < j - 1)
332+
{
333+
int k = (i + j) / 2;
334+
335+
if (table[k][0] == p)
336+
return table[k][1];
337+
else if (table[k][0] < p)
338+
i = k;
339+
else
340+
j = k;
341+
}
342+
343+
float t = (p - table[i][0]) / (table[i + 1][0] - table[i][0]);
344+
float s = 1 - t;
345+
346+
return s * table[i][1] + t * table[i + 1][1];
347+
}

0 commit comments

Comments
 (0)