-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
441 lines (362 loc) · 9.94 KB
/
main.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/////////////////////////////////////////////////
/// This is a program to convert a spectrum ///
/// file (intensity by wavelength) into its ///
/// color, following the CIE color matching ///
/// functions from 1931, using their ///
/// analytic form as given by Wiman, C et ///
/// all on Journal of Computer Graphics ///
/// Tecniches, v.2, n.2, 2013. ///
/// ///
/// Programmed by Bernardo de Souza, 2017, ///
/// Brazil. ///
/////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <string.h>
#include <math.h>
#define eV_cm 8065.540106923572
using namespace std;
//
// Function to count the number of lines (c) in file f
//
int CountLines(ifstream &f,auto &c)
{
string text;
while (!f.eof())
{
f >> text >> text;
c++;
}
//After counting
f.clear();
f.seekg(0);
getline(f,text);
return 0;
}
//
// This function converts the X axis to nm
//
int ConvertXaxis(int argc, char **argv, double *xaxis, auto &c)
{
char inp[512];
for (auto i=1;i<argc;++i)
{
//Convert to cm-1
sprintf(inp,"-cm-1");
if (!strcmp(argv[i],inp))
{
cout << "here";
for (auto i=0;i<c;++i)
xaxis[i] = 1e7/xaxis[i];
}
//Convert eV to nm
sprintf(inp,"-eV");
if (!strcmp(argv[i],inp))
for (auto i=0;i<c;++i)
xaxis[i] = 1e7/(xaxis[i]*eV_cm);
}
return 0;
}
//
// This function normalizes the Y axis
int NormalizeYaxis(double *yaxis, auto &c)
{
double max=0;
//Find the largest element
for (auto i=0;i<c;++i)
max = (yaxis[i]>max)?yaxis[i]:max;
//Nomalize
for (auto i=0;i<c;++i)
yaxis[i] /= max;
return 0;
}
//
// Generates a normalized black body radiation of temperature T
// for illumination
//
int GenerateNormalizedBlackBodyRad(double *BBR,double *xaxis, double c, double T)
{
//First, make the radiation
for (auto i=0;i<c;++i)
BBR[i] = ((2*6.62e-34*9e16)/pow(xaxis[i]*1e-9,5))/(exp((6.62e-34*3e8)/(xaxis[i]*1e-9*1.38e-23*T))-1);
double max=0;
for (auto i=0;i<c;++i)
max = BBR[i]>max?BBR[i]:max;
for (auto i=0;i<c;++i)
BBR[i] /= max;
return 0;
}
//
// Gets the color matching function value for a given wavenumber in nm
//
double CMFX(double x)
{
double t1 = (x-442.0)*((x<442.0)?0.0624:0.0374);
double t2 = (x-599.8)*((x<599.8)?0.0264:0.0323);
double t3 = (x-501.1)*((x<501.1)?0.0490:0.0382);
return 0.362*exp(-0.5*t1*t1) + 1.056*exp(-0.5*t2*t2)
- 0.065*exp(-0.5*t3*t3);
}
//
// Gets the color matching function value for a given wavenumber in nm
//
double CMFY(double x)
{
double t1 = (x-568.8)*((x<568.8)?0.0213:0.0247);
double t2 = (x-530.9)*((x<530.9)?0.0613:0.0322);
return 0.821*exp(-0.5*t1*t1) + 0.286*exp(-0.5*t2*t2);
}
//
// Gets the color matching function value for a given wavenumber in nm
//
double CMFZ(double x)
{
double t1 = (x-437.0)*((x<437.0)?0.0845:0.0278);
double t2 = (x-459.0)*((x<459.0)?0.0385:0.0725);
return 1.217*exp(-0.5*t1*t1) + 0.681*exp(-0.5*t2*t2);
}
//
// Generates the color matching functions
// that correspond to xaxis
//
int GenerateCMFs(double *xaxis,double *cmf_x,double *cmf_y,double *cmf_z, auto &c)
{
for (auto i=0;i<c;++i)
{
cmf_x[i] = CMFX(xaxis[i]);
cmf_y[i] = CMFY(xaxis[i]);
cmf_z[i] = CMFZ(xaxis[i]);
}
return 0;
}
//
// This calculates the products yaxis*(color matching functions)
//
int CalcProducts(double *yaxis,double *cmf_x, double *cmf_y, double *cmf_z,
double *pX,double *pY,double *pZ,auto &c)
{
for (auto i=0;i<c;++i)
{
pX[i] = yaxis[i]*cmf_x[i];
pY[i] = yaxis[i]*cmf_y[i];
pZ[i] = yaxis[i]*cmf_z[i];
}
return 0;
}
//
// This integrates p over the X axis
//
double Integrate(double *xaxis,double *p, auto &c)
{
auto sum=0.0;
for (auto i=0;i<(c-1);++i)
sum += ((p[i]+p[i+1])/2.0)*fabs((xaxis[i+1]-xaxis[i]));
return sum;
}
int ConverttoRBG(double X, double Y, double Z,
double &R, double &G, double &B)
{
X /= 100;
Z /= 100;
Y /= 100;
R = X*3.2404542 + Y*-1.5371385 + Z*-0.4985312;
G = X*-0.9692660 + Y*1.8760108 + Z*0.0415560;
B = X*0.0556434 + Y*-0.2040259 + Z*1.0572252;
if (R > 0.0031308)
R = 1.055*(pow(R,(1.0/2.4)) - 0.055);
else
R = 12.92*R;
if (G > 0.0031308)
G = 1.055*(pow(G,(1.0/2.4)) - 0.055);
else
G = 12.92*G;
if (B > 0.0031308)
B = 1.055*(pow(B,(1.0/2.4)) - 0.055);
else
B = 12.92*B;
R = R*255;
G = G*255;
B = B*255;
return 0;
}
int ConverttoHSL(double R,double G,double B,
double &H, double &S, double &L)
{
R /= 255;
G /= 255;
B /= 255;
auto Min = min(min(R,G),B); //Min. value of RGB
auto Max = max(max(R,G),B); //Max. value of RGB
auto del_Max = Max - Min; //Delta RGB value
L = (Max + Min)/ 2;
if (del_Max == 0) //This is a gray, no chroma...
{
H = 0;
S = 0;
}
else //Chromatic data...
{
if (L < 0.5)
S = del_Max/(Max + Min);
else
S = del_Max/(2 - Max - Min);
auto del_R = (((Max - R)/6.0) + (del_Max/2.0))/del_Max;
auto del_G = (((Max - G)/6.0) + (del_Max/2.0))/del_Max;
auto del_B = (((Max - B)/6.0) + (del_Max/2.0))/del_Max;
if (R == Max)
H = del_B - del_G;
else
if (G == Max )
H = (1.0/3.0) + del_R - del_B;
else
if (B == Max )
H = (2.0/3.0) + del_G - del_R;
if (H < 0)
H += 1;
if (H > 1)
H -= 1;
}
return 0;
}
int ConverttoCMYK(double R,double G,double B,
double &C,double &M,double &Y,double &K)
{
C = 1 - (R/255);
M = 1 - (G/255);
Y = 1 - (B/255);
K = 1;
if (C < K)
K = C;
if (M < K)
K = M;
if (Y < K)
K = Y;
if (K == 1)
{
C = 0; //Black only
M = 0;
Y = 0;
}
else
{
C = (C - K)/(1 - K);
M = (M - K)/(1 - K);
Y = (Y - K)/(1 - K);
}
C *= 100;
M *= 100;
Y *= 100;
K *= 100;
return 0;
}
int main(int argc, char **argv)
{
//Introductory phrase
cout << "\n\t\t**************************\t\t" << endl;
cout << "\t\t* Welcome to spectocolor *\t\t" << endl;
cout << "\t\t**************************\t\t\n" << endl;
//Check if a filename was given
if (argc < 2)
{
cout << "We need a spectrum file as input!" << endl;
return 1;
}
//Open spectrum file
ifstream f (argv[1]);
//If file is not open, close
if (!f.is_open())
{
cout << "Could not open the file " << argv[1] << "!" << endl;
return 1;
}
//If everyting is OK, skip the first line and read all the rest
string text;
getline(f,text);
//Count the number of lines
auto c=-1;
CountLines(f,c);
//Read the input spectrum
double xaxis[c],yaxis[c];
for (auto i=0;i<c;++i)
f >> xaxis[i] >> yaxis[i];
//If cm-1 or eV, convert the X axis to nm
ConvertXaxis(argc, argv,xaxis,c);
//Normalize the Y axis
NormalizeYaxis(yaxis,c);
//If the spectra is of absorption
char inp[512];
//Define a temperature for the illuminant
double T=5500;
sprintf(inp,"-T");
for (auto i=1;i<argc;++i)
if (!strcmp(argv[i],inp))
T = atof(argv[i+1]);
sprintf(inp,"-abs");
for (auto i=1;i<argc;++i)
if (!strcmp(argv[i],inp))
{
//Create the illuminant spectrum
double BBR[c];
GenerateNormalizedBlackBodyRad(BBR,xaxis,c,T);
for (auto i=0;i<c;++i)
{
//Assume 100% absorbance at its maximum and excludes negatives
yaxis[i] = BBR[i] - yaxis[i];
if (yaxis[i] < 0)
yaxis[i] = 0;
}
}
//Create the corresponding color matching functions
double cmf_x[c],cmf_y[c],cmf_z[c];
GenerateCMFs(xaxis,cmf_x,cmf_y,cmf_z,c);
//Compute the products pX,pY and pZ
double pX[c],pY[c],pZ[c];
CalcProducts(yaxis,cmf_x,cmf_y,cmf_z,pX,pY,pZ,c);
//Compute integrals X, Y and Z
double X,Y,Z;
X = Integrate(xaxis,pX,c);
Y = Integrate(xaxis,pY,c);
Z = Integrate(xaxis,pZ,c);
//Convert to Yxy
double x,y;
x = X/(X+Y+Z);
y = Y/(X+Y+Z);
//Convert to RBG
double R,G,B;
ConverttoRBG(X,Y,Z,R,G,B);
//Convert to HSL
double H,S,L;
ConverttoHSL(R,G,B,H,S,L);
//Convert to CMYK
double C,M,Yy,K;
ConverttoCMYK(R,G,B,C,M,Yy,K);
/*for (auto i=0;i<c;++i)
cout << xaxis[i] << "\t" << yaxis[i] << "\t" << pX[i] << "\t" << pY[i] << "\t" << pZ[i]
<< "\t" << endl;
cout << X << " " << Y << " " << Z << endl;
*/
//Print results
cout << "Calculating the color from the spectra...\n" << endl;
cout << "XYZ color space:\n" << endl;
cout << "\tX: " << X << endl;
cout << "\tY: " << Y << endl;
cout << "\tZ: " << Z << endl;
cout << "\nYxy color space:\n" << endl;
cout << "\tY: " << Y << endl;
cout << "\tx: " << x << endl;
cout << "\ty: " << y << endl;
cout << "\nsRGB color space [0-255]:\n" << endl;
cout << "\tR: " << R << endl;
cout << "\tG: " << G << endl;
cout << "\tB: " << B << endl;
cout << "\nHSL color space [0-1]:\n" << endl;
cout << "\tH: " << H << endl;
cout << "\tS: " << S << endl;
cout << "\tL: " << L << endl;
cout << "\nCMYK color space [0-1]:\n" << endl;
cout << "\tC: " << C << endl;
cout << "\tM: " << M << endl;
cout << "\tY: " << Yy << endl;
cout << "\tK: " << K << endl;
return 0;
}