forked from AcademySoftwareFoundation/rawtoaces
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rta.cpp
1744 lines (1414 loc) · 57.4 KB
/
rta.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////////////
// Copyright (c) 2013 Academy of Motion Picture Arts and Sciences
// ("A.M.P.A.S."). Portions contributed by others as indicated.
// All rights reserved.
//
// A worldwide, royalty-free, non-exclusive right to copy, modify, create
// derivatives, and use, in source and binary forms, is hereby granted,
// subject to acceptance of this license. Performance of any of the
// aforementioned acts indicates acceptance to be bound by the following
// terms and conditions:
//
// * Copies of source code, in whole or in part, must retain the
// above copyright notice, this list of conditions and the
// Disclaimer of Warranty.
//
// * Use in binary form must retain the above copyright notice,
// this list of conditions and the Disclaimer of Warranty in the
// documentation and/or other materials provided with the distribution.
//
// * Nothing in this license shall be deemed to grant any rights to
// trademarks, copyrights, patents, trade secrets or any other
// intellectual property of A.M.P.A.S. or any contributors, except
// as expressly stated herein.
//
// * Neither the name "A.M.P.A.S." nor the name of any other
// contributors to this software may be used to endorse or promote
// products derivative of or based on this software without express
// prior written permission of A.M.P.A.S. or the contributors, as
// appropriate.
//
// This license shall be construed pursuant to the laws of the State of
// California, and any disputes related thereto shall be subject to the
// jurisdiction of the courts therein.
//
// Disclaimer of Warranty: THIS SOFTWARE IS PROVIDED BY A.M.P.A.S. AND
// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED. IN NO
// EVENT SHALL A.M.P.A.S., OR ANY CONTRIBUTORS OR DISTRIBUTORS, BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, RESITUTIONARY,
// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// WITHOUT LIMITING THE GENERALITY OF THE FOREGOING, THE ACADEMY
// SPECIFICALLY DISCLAIMS ANY REPRESENTATIONS OR WARRANTIES WHATSOEVER
// RELATED TO PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS IN THE ACADEMY
// COLOR ENCODING SYSTEM, OR APPLICATIONS THEREOF, HELD BY PARTIES OTHER
// THAN A.M.P.A.S., WHETHER DISCLOSED OR UNDISCLOSED.
///////////////////////////////////////////////////////////////////////////
#include "rta.h"
namespace rta {
Illum::Illum() {
_inc = 5;
}
Illum::Illum( string type ){
_type = type;
_inc = 5;
}
Illum::~Illum() {
vector < double >().swap( _data );
}
// =====================================================================
// Set the type of Illuminant
//
// inputs:
// const char *: type (from user input)
//
// outputs:
// void: _type will be assigned a value to (private member)
void Illum::setIllumType ( const string & type ) {
assert( !type.empty() );
_type = type;
return;
}
// =====================================================================
// Set the increment of Illuminant SPD
//
// inputs:
// int : inc
//
// outputs:
// void: _inc will be assigned with a value (private member)
void Illum::setIllumInc ( const int & inc ) {
_inc = inc;
return;
}
// =====================================================================
// Set the index of Illuminant SPD
//
// inputs:
// int : index
//
// outputs:
// void: _index will be assigned with a value (private member)
void Illum::setIllumIndex ( const double & index ) {
_index = index;
return;
}
// =====================================================================
// Read the Illuminant data from JSON file(s)
//
// inputs:
// string: path to the Illuminant data file
// const char *: type of light source if user specifies
//
// outputs:
// int: If successufully parsed, private data members (e.g., _data)
// will be filled and return 1; Otherwise, return 0
int Illum::readSPD ( const string & path, const string & type ) {
assert(path.length() > 0 && type.length() > 0 );
try
{
// using libraries from boost::property_tree
ptree pt;
read_json ( path, pt );
const string stype = pt.get<string>( "header.illuminant" );
if ( type.compare(stype) != 0
&& type.compare("na") != 0 )
return 0;
_type = stype;
vector <int> wavs;
int dis;
BOOST_FOREACH ( ptree::value_type &row, pt.get_child ( "spectral_data.data.main" ) )
{
wavs.push_back(atoi((row.first).c_str()));
if ( wavs.size() == 2 )
dis = wavs[1] - wavs[0];
else if ( wavs.size() > 2 &&
wavs[wavs.size()-1] - wavs[wavs.size()-2] != dis ) {
fprintf ( stderr, "Please double check the Light "
"Source data (e.g. the increment "
"should be uniform from 380nm to 780nm).\n" );
exit(-1);
}
if ( wavs[wavs.size()-1] < 380 ||
wavs[wavs.size()-1] % 5 )
continue;
else if ( wavs[wavs.size()-1] > 780 )
break;
BOOST_FOREACH ( ptree::value_type &cell, row.second ) {
_data.push_back(cell.second.get_value<double>());
if ( wavs[wavs.size()-1] == 550 )
_index = cell.second.get_value<double>();
}
// printf ( "\"%i\": [ %18.13f ], \n",
// wavs[wavs.size()-1],
// _bestIllum._data[_bestIllum.data.size()-1] );
}
_inc = dis;
}
catch ( std::exception const& e )
{
std::cerr << e.what() << std::endl;
}
if ( _data.size() != 81 ) {
fprintf ( stderr, "Please double check the Light "
"Source data (e.g. the increment "
"should be 5nm from 380nm to 780nm).\n" );
exit(1);
}
return 1;
}
// =====================================================================
// Calculate the chromaticity values based on cct
//
// inputs:
// const int: cct / correlated color temperature
//
// outputs:
// vector <double>: xy / chromaticity values
//
vector <double> Illum::cctToxy ( const double & cctd ) const {
// assert( cctd >= 4000 && cct <= 25000 );
vector <double> xy(2, 1.0);
if ( cctd >= 4002.15 && cctd <= 7003.77 )
xy[0] = ( 0.244063 + 99.11/cctd
+ 2.9678 * 1000000/(std::pow(cctd,2))
- 4.6070 * 1000000000/(std::pow(cctd,3)) );
else
xy[0] = ( 0.237040 + 247.48/cctd
+ 1.9018*1000000/(std::pow(cctd,2))
- 2.0064*1000000000/(std::pow(cctd,3)) );
xy[1] = -3.0 * (std::pow(xy[0],2)) + 2.87 * xy[0] - 0.275;
return xy;
}
// =====================================================================
// Calculate spectral power distribution(SPD) of CIE standard daylight
// illuminant based on the requested Correlated Color Temperature
// input value(s):
//
// const int: cct / correlated color temperature
//
// outputs:
// int: If successufully processed, private data members (e.g., _data)
// will be filled and return 1; Otherwise, return 0
void Illum::calDayLightSPD ( const int & cct ) {
assert(( s_series[53].wl - s_series[0].wl) % _inc == 0 );
double cctd = 1.0;
if (cct >= 40 && cct <= 250)
cctd = cct * 100 * 1.4387752 / 1.438;
else if (cct >= 4000 && cct <= 25000)
cctd = cct * 1.0;
else {
fprintf ( stderr, "The range of Correlated Color Temperature for "
"Day Light should be from 4000 to 25000. \n");
exit(1);
}
if (_data.size() > 0) _data.clear();
if (!_type.size()) {
char buffer[10];
snprintf ( buffer, 10, "%d", cct );
_type = "d" + string(buffer);
}
vector <int> wls0, wls1;
vector <double> s00, s10, s20, s01, s11, s21;
vector <double> xy = cctToxy (cctd);
double m0 = 0.0241 + 0.2562*xy[0] - 0.7341*xy[1];
double m1 = (-1.3515 - 1.7703*xy[0] + 5.9114*xy[1]) / m0;
double m2 = (0.03000 - 31.4424*xy[0] + 30.0717*xy[1]) / m0;
FORI (54) {
wls0.push_back(s_series[i].wl);
s00.push_back(s_series[i].RGB[0]);
s10.push_back(s_series[i].RGB[1]);
s20.push_back(s_series[i].RGB[2]);
}
int size = (s_series[53].wl - s_series[0].wl)/_inc + 1;
FORI(size)
wls1.push_back(s_series[0].wl + _inc*i);
s01 = interp1DLinear(wls0, wls1, s00);
clearVM(s00);
s11 = interp1DLinear(wls0, wls1, s10);
clearVM(s10);
s21 = interp1DLinear(wls0, wls1, s20);
clearVM(s20);
clearVM(wls0);
clearVM(wls1);
FORI (size) {
int index = s_series[0].wl + _inc * i;
if ( index >= 380 && index <= 780 ) {
_data.push_back(s01[i] + m1 * s11[i] + m2 * s21[i]);
if ( index == 550 )
_index = _data[_data.size()-1];
}
}
clearVM(s01);
clearVM(s11);
clearVM(s21);
}
// =====================================================================
// Fetch Illuminant SPD data
//
// inputs:
// N/A
//
// outputs:
// const vector < double > : the SPD data of the Illuminant
const vector < double > Illum::getIllumData() const {
return _data;
}
// =====================================================================
// Fetch the type of the Illuminant
//
// inputs:
// N/A
//
// outputs:
// const string _type : the type of Illuminant
const string Illum::getIllumType() const {
return _type;
}
// =====================================================================
// Fetch the interval/increment of Illuminant SPD data
//
// inputs:
// N/A
//
// outputs:
// const int : the interval/increment of the Illuminant SPD data
const int Illum::getIllumInc() const {
return _inc;
}
// =====================================================================
// Fetch the index value of Illuminant SPD data at 550nm
//
// inputs:
// N/A
//
// outputs:
// const int : the index value of the Illuminant SPD data at 550nm
const double Illum::getIllumIndex() const {
return _index;
}
// =====================================================================
// Generates blackbody curve(s) of a given temperature
//
// const int: temp / temperature
//
// outputs:
// int: If successufully processed, private data members (e.g., _data)
// will be filled and return 1; Otherwise, return 0
void Illum::calBlackBodySPD ( const int & cct ) {
if (cct < 1500 || cct >= 4000) {
fprintf ( stderr, "The range of Color Temperature for BlackBody "
"should be from 1500 to 3999. \n");
exit(1);
}
if (_data.size() > 0) _data.clear();
if (!_type.size()) {
char buffer[10];
snprintf(buffer, 10, "%d", cct);
_type = string(buffer) + "k";
}
for ( int wav = 380; wav <= 780; wav+=5 ) {
double lambda = wav / 1e9;
double c1 = 2 * bh * (std::pow(bc, 2));
double c2 = ( bh * bc ) / ( bk * lambda * cct);
_data.push_back(c1 * pi / (std::pow(lambda, 5) * (std::exp(c2) - 1)));
}
}
// ------------------------------------------------------//
Spst::Spst() {
_brand = null_ptr;
_model = null_ptr;
_increment = 5;
_spstMaxCol = -1;
for (int i=0; i<81; i++) {
_rgbsen.push_back( RGBSen() );
}
}
Spst::Spst ( const Spst& spstobject ) {
assert ( spstobject._brand != null_ptr
&& spstobject._model != null_ptr );
size_t lenb = strlen(spstobject._brand);
assert(lenb < 64);
if(lenb > 64) lenb = 64;
_brand = (char *) malloc(lenb+1);
memset(_brand, 0x0, lenb);
memcpy(_brand, spstobject._brand, lenb);
_brand[lenb] = '\0';
size_t lenm = strlen(spstobject._model);
assert(lenm < 64);
if(lenm > 64) lenm = 64;
_model = (char *) malloc(lenm+1);
memset(_model, 0x0, lenm);
memcpy(_model, spstobject._model, lenm);
_model[lenm] = '\0';
_increment = spstobject._increment;
_spstMaxCol = spstobject._spstMaxCol;
_rgbsen = spstobject._rgbsen;
}
Spst::~Spst() {
delete _brand;
delete _model;
vector< RGBSen >().swap( _rgbsen );
}
// =====================================================================
// Fetch the brand of camera
//
// inputs:
// N/A
//
// outputs:
// const char *: the name of camera brand
const char * Spst::getBrand() const {
return _brand;
}
// =====================================================================
// Fetch the model of the camera
//
// inputs:
// N/A
//
// outputs:
// const char *: the model of the camera
const char * Spst::getModel() const {
return _model;
}
// =====================================================================
// Fetch wavelength increment value of the camera sensitivity
//
// inputs:
// N/A
//
// outputs:
// const uint8_t: Wavelength increment value (e.g., 5nm, 10nm) of
// the camera sensitivity
const uint8_t Spst::getWLIncrement() const {
return _increment;
}
// =====================================================================
// Fetch the sensitivity data of the camera (reading from the file)
//
// inputs:
// N/A
//
// outputs:
// const vector <RGBSen>: the sensitivity (in vector) of the camera
const vector <RGBSen> Spst::getSensitivity() const {
return _rgbsen;
}
// =====================================================================
// Fetch the brand of camera
//
// inputs:
// N/A
//
// outputs:
// char *: the name of camera brand
char * Spst::getBrand() {
return _brand;
}
// =====================================================================
// Fetch the model of the camera
//
// inputs:
// N/A
//
// outputs:
// char *: the model of the camera
char * Spst::getModel() {
return _model;
}
// =====================================================================
// Fetch the wavelength increment value of the camera sensitivity
//
// inputs:
// N/A
//
// outputs:
// uint8_t: Wavelength increment value (e.g., 5nm, 10nm) of the
// camera's sensitivity
int Spst::getWLIncrement() {
return _increment;
}
// =====================================================================
// Fetch the sensitivity data of the camera (reading from the file)
//
// inputs:
// string: path to the camera sensitivity file
// const char *: camera maker (from libraw)
// const char *: camera model (from libraw)
//
// outputs:
// int : the private data members (e.g., _rgbsen) will be filled
int Spst::loadSpst ( const string & path,
const char * maker,
const char * model ) {
assert( path.length() > 0
&& maker != null_ptr
&& model != null_ptr );
vector <RGBSen> rgbsen;
vector <double> max(3, dmin);
try
{
ptree pt;
read_json ( path, pt );
const char * cmaker = (pt.get<string>( "header.manufacturer" )).c_str();
if ( cmp_str(maker, cmaker) ) return 0;
setBrand(cmaker);
const char * cmodel = (pt.get<string>( "header.model" )).c_str();
if ( cmp_str(model, cmodel) ) return 0;
setModel(cmodel);
vector <int> wavs;
int inc;
BOOST_FOREACH ( ptree::value_type &row, pt.get_child ( "spectral_data.data.main" ) )
{
wavs.push_back(atoi((row.first).c_str()));
if ( wavs.size() == 2 )
inc = wavs[1] - wavs[0];
else if ( wavs.size() > 2 &&
wavs[wavs.size()-1] - wavs[wavs.size()-2] != inc ) {
fprintf ( stderr, "Please double check the Camera "
"Sensitivity data (e.g. the increment "
"should be uniform from 380nm to 780nm).\n" );
exit(1);
}
if ( wavs[wavs.size()-1] < 380 ||
wavs[wavs.size()-1] % 5 )
continue;
else if ( wavs[wavs.size()-1] > 780 )
break;
vector < double > data;
BOOST_FOREACH ( ptree::value_type &cell, row.second )
data.push_back ( cell.second.get_value<double>() );
// ensure there are three components
assert(data.size() == 3);
RGBSen tmp_sen ( data[0], data[1], data[2] );
if (tmp_sen._RSen > max[0]) max[0] = tmp_sen._RSen;
if (tmp_sen._GSen > max[1]) max[1] = tmp_sen._GSen;
if (tmp_sen._BSen > max[2]) max[2] = tmp_sen._BSen;
// printf( "\"%i\": [ %18.13f, %18.13f, %18.13f ], \n",
// wavs[wavs.size()-1],
// data[0],
// data[1],
// data[2] );
//
rgbsen.push_back(tmp_sen);
}
setWLIncrement(inc);
}
catch ( std::exception const& e )
{
std::cerr << e.what() << std::endl;
}
// it can be updated if there is a broader spectrum
// (e.g., 300nm-800nm) or a smaller increment values (e.g, 1nm)
if ( rgbsen.size() != 81 ) {
fprintf( stderr, "Please double check the Camera "
"Sensitivity data (e.g. the increment "
"should be uniform from 380nm to 780nm).\n" );
exit(-1);
}
_spstMaxCol = max_element (max.begin(), max.end()) - max.begin();
setSensitivity (rgbsen);
return 1;
}
// =====================================================================
// Fetch the sensitivity data of the camera (reading from the file)
//
// inputs:
// N/A
//
// outputs:
// vector <RGBSen>: the sensitivity (in vector) of the camera
vector < RGBSen > Spst::getSensitivity() {
return _rgbsen;
}
// =====================================================================
// Set the brand of camera
//
// inputs:
// const char *: brand (read from the file or
// the meta-data from libraw)
//
// outputs:
// void: _brand (private member)
void Spst::setBrand ( const char * brand ) {
assert( brand != null_ptr );
size_t len = strlen(brand);
assert(len < 64);
if(len > 64) len = 64;
_brand = (char *) malloc(len+1);
memset(_brand, 0x0, len);
memcpy(_brand, brand, len);
_brand[len] = '\0';
return;
}
// =====================================================================
// Set the model of camera
//
// inputs:
// const char *: model (read from the file or
// the meta-data from libraw)
//
// outputs:
// void: _brand (private member)
void Spst::setModel ( const char * model ) {
assert ( model != null_ptr );
size_t len = strlen(model);
assert(len < 64);
if(len > 64) len = 64;
_model = (char *)malloc(len+1);
memset(_model, 0x0, len);
memcpy(_model, model, len);
_model[len] = '\0';
return;
}
// =====================================================================
// Set the wavelength increment value of the camera sensitivity
//
// inputs:
// uint8_t: inc (read from the file)
//
// outputs:
// void: _increment (private member)
void Spst::setWLIncrement ( const int & inc ) {
_increment = inc;
return;
}
// =====================================================================
// Set the sensitivity data of the camera (reading from the file)
//
// inputs:
// const vector<RGBSen>: rgbsen (read from the file)
//
// outputs:
// void: _rgbsen (private member)
void Spst::setSensitivity ( const vector < RGBSen > & rgbsen ) {
_rgbsen = rgbsen;
return;
}
// ------------------------------------------------------//
Idt::Idt() {
_verbosity = 0;
FORI(81) {
_trainingSpec.push_back(trainSpec());
_cmf.push_back(CMF());
}
_idt.resize(3);
_wb.resize(3);
FORI(3) {
_idt[i].resize(3);
_wb[i] = 1.0;
FORJ(3) _idt[i][j] = neutral3[i][j];
}
}
Idt::~Idt() {
vector < Illum >().swap(_Illuminants);
vector < CMF >().swap(_cmf);
vector < trainSpec >().swap(_trainingSpec);
vector < double >().swap(_wb);
vector < vector<double> >().swap(_idt);
}
// =====================================================================
// Scale the Illuminant data using the max element of RGB code values
//
// inputs:
// Illum & Illuminant
//
// outputs:
// scaled Illuminant data set
void Idt::scaleLSC (Illum & Illuminant) {
assert( _cameraSpst._spstMaxCol >= 0
&& (Illuminant._data).size() != 0);
int size =_cameraSpst._rgbsen.size();
vector < double > colMax(size, 1.0);
switch (_cameraSpst._spstMaxCol){
case 0:
FORI(size) colMax[i] = _cameraSpst._rgbsen[i]._RSen;
break;
case 1:
FORI(size) colMax[i] = _cameraSpst._rgbsen[i]._GSen;
break;
case 2:
FORI(size) colMax[i] = _cameraSpst._rgbsen[i]._BSen;
break;
default:
return;
}
scaleVector ( Illuminant._data,
1.0 / sumVector ( mulVectorElement ( Illuminant._data, colMax ) ) );
}
// =====================================================================
// Load the Camera Sensitivty data
//
// inputs:
// string: path to the camera sensitivity file
// const char *: camera maker (from libraw)
// const char *: camera model (from libraw)
//
// outputs:
// boolean: If successufully parsed, _cameraSpst will be filled and return 1;
// Otherwise, return 0
int Idt::loadCameraSpst ( const string & path,
const char * maker,
const char * model ) {
return _cameraSpst.loadSpst (path, maker, model);
}
// =====================================================================
// Load the Illuminant data
//
// inputs:
// string: paths to various Illuminant data files
// string: type of light source if user specifies
//
// outputs:
// int: If successufully parsed, _bestIllum will be filled and return 1;
// Otherwise, return 0
int Idt::loadIlluminant ( const vector <string> & paths, string type ) {
assert ( paths.size() > 0 && !type.empty() );
if (_Illuminants.size() > 0) _Illuminants.clear();
if ( type.compare("na") != 0 ) {
// Daylight
if ( type[0] == 'd' ) {
Illum illumDay;
illumDay.setIllumType(type);
illumDay.calDayLightSPD(atoi(type.substr(1).c_str()));
_Illuminants.push_back(illumDay);
return 1;
}
// Blackbody
else if ( type[type.length()-1] == 'k' ){
Illum illumBB;
illumBB.setIllumType(type);
illumBB.calBlackBodySPD(atoi(type.substr(0, type.length()-1).c_str()));
_Illuminants.push_back(illumBB);
return 1;
}
else {
FORI ( paths.size() ) {
Illum IllumJson;
if ( IllumJson.readSPD (paths[i], type) &&
type.compare(IllumJson._type) == 0 ) {
_Illuminants.push_back(IllumJson);
return 1;
}
}
}
}
else {
// Daylight - pre-calculate
for ( int i = 4000; i <= 25000; i+=500 ) {
Illum illumDay;
illumDay.setIllumType("d"+(to_string(i/100)));
illumDay.calDayLightSPD(i);
_Illuminants.push_back(illumDay);
}
// Blackbody - pre-calculate
for ( int i = 1500; i < 4000; i+=500 ) {
Illum illumBB;
illumBB.setIllumType((to_string(i)+"k"));
illumBB.calBlackBodySPD(i);
_Illuminants.push_back(illumBB);
}
FORI ( paths.size() ) {
Illum IllumJson;
if ( IllumJson.readSPD (paths[i], type) )
_Illuminants.push_back(IllumJson);
}
}
return (_Illuminants.size() > 0);
}
// =====================================================================
// Load the 190-patch training data
//
// inputs:
// string : path to the 190-patch training data
//
// outputs:
// _trainingSpec: If successufully parsed, _trainingSpec will be filled
void Idt::loadTrainingData ( const string & path ) {
struct stat st;
assert (!stat( path.c_str(), &st ));
if ( _trainingSpec.size() > 0 ) {
FORI (_trainingSpec.size())
_trainingSpec[i]._data.clear();
}
try
{
ptree pt;
read_json ( path, pt );
int i = 0;
BOOST_FOREACH ( ptree::value_type &row, pt.get_child ( "spectral_data.data.main" ) )
{
_trainingSpec[i]._wl = atoi((row.first).c_str());
BOOST_FOREACH ( ptree::value_type &cell, row.second )
_trainingSpec[i]._data.push_back(cell.second.get_value<double>());
assert(_trainingSpec[i]._data.size() == 190);
i += 1;
}
}
catch ( std::exception const& e )
{
std::cerr << e.what() << std::endl;
}
}
// =====================================================================
// Load the CIE 1931 Color Matching Functions data
//
// inputs:
// string : path to the CIE 1931 Color Matching Functions data
//
// outputs:
// _cmf: If successufully parsed, _cmf will be filled
void Idt::loadCMF ( const string & path ) {
struct stat st;
assert (!stat( path.c_str(), &st ));
try
{
ptree pt;
read_json ( path, pt );
int i = 0;
BOOST_FOREACH ( ptree::value_type &row, pt.get_child ( "spectral_data.data.main" ) )
{
_cmf[i]._wl = atoi((row.first).c_str());
if ( _cmf[i]._wl < 380 ||
_cmf[i]._wl % 5 )
continue;
else if ( _cmf[i]._wl > 780 )
break;
vector < double > data;
BOOST_FOREACH ( ptree::value_type &cell, row.second )
data.push_back ( cell.second.get_value<double>() );
assert(data.size() == 3);
_cmf[i]._xbar = data[0];
_cmf[i]._ybar = data[1];
_cmf[i]._zbar = data[2];
i += 1;
}
}
catch ( std::exception const& e )
{
std::cerr << e.what() << std::endl;
}
}
// =====================================================================
// Push new Illuminant to further process Spectral Power Data
//
// inputs:
// Illum: Illuminant
//
// outputs:
// N/A: _Illuminants should have one more element
void Idt::setIlluminants ( const Illum & Illuminant ) {
_Illuminants.push_back(Illuminant);
}
// =====================================================================
// Set Verbosity value for the length of IDT generation status message
//
// inputs:
// int: verbosity
//
// outputs:
// int: _verbosity
void Idt::setVerbosity ( const int verbosity ) {
_verbosity = verbosity;
}
// =====================================================================
// Choose the best Light Source based on White Balance Coefficients from
// the camera read by libraw according to a given set of coefficients
//
// inputs: