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
|
/*
* Argyll Color Correction System
* Spectral .ti3 or .sp file converter
*
* Copyright 2005 Gerhard Fuernkranz
* All rights reserved.
*
* Copyright 2006, 2007 Graeme Gill.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* See the License2.txt file for details.
*
* Pursuant to the above, this file is licenced in ArgyllCMS
* under the GNU GENERAL PUBLIC LICENSE Version 3 :-
* see the License.txt file for licencing details.
*
* This program takes the spectral data in a .ti3 or .sp file, converts them
* to XYZ and Lab and fills the XYZ_[XYZ] and LAB_[LAB] columns in the
* output .ti3 or .sp file with the computed XYZ and Lab values. If the columns
* XYZ_[XYZ] and/or LAB_[LAB] are missing in the input file, they are
* added to the output file.
*
* All other colums are copied from the input to the output .ti3 or .sp file.
*
* If the -f option is used, the FWA corrected spectral reflectances
* are written to the output .ti3 or .sp file, instead of simply copying the
* spectral reflectances from the input .ti3 or .sp file. In this case, the
* XYZ_[XYZ] and D50 LAB_[LAB] values are computed from the FWA corrected
* reflectances as well.
*/
/*
NOTE this uses hard coded space signatures, and should be converted
to use xcolorants instead.
This doesn't handle display .ti3's properly, as the resulting CIE values
need to be normalised to Y=100 or marked as not normalised.
Calibration tables aren't being passed through either ??
This is intended for conversion of reflective measurements to XYZ -
there is no illuminant for emissive values.
L*a*b* is always D50.
*/
#define ALLOW_PLOT
#define XRES 200
#include <stdio.h>
#if defined(__IBMC__)
#include <float.h>
#endif
#include "aconfig.h"
#include "numlib.h"
#include "cgats.h"
#include "xicc.h"
#include "insttypes.h"
#include "conv.h"
#include "icoms.h"
#include "inst.h"
#ifdef ALLOW_PLOT
#include "plot.h"
#include "ui.h"
#endif
void
usage (void)
{
fprintf (stderr, "Convert spectral .ti3 or .sp file, Version %s\n", ARGYLL_VERSION_STR);
fprintf (stderr, "Author: Gerhard Fuernkranz, licensed under the AGPL Version 3\n");
fprintf (stderr, "\n");
fprintf (stderr, "Usage: spec2cie [options] input.[ti3|sp] output.[ti3|sp]\n");
fprintf (stderr, " -v Verbose mode\n");
fprintf (stderr, " -I illum Override actual instrument illuminant in .ti3 or .sp file:\n");
fprintf (stderr, " A, C, D50, D50M2, D65, F5, F8, F10 or file.sp\n");
fprintf (stderr, " (only used in conjunction with -f)\n");
fprintf (stderr, " -f [illum] Use Fluorescent Whitening Agent compensation [simulated inst. illum.:\n");
fprintf (stderr, " M0, M1, M2, A, C, D50 (def.), D50M2, D65, F5, F8, F10 or file.sp]\n");
fprintf (stderr, " -i illum Choose illuminant for computation of CIE XYZ from spectral data & FWA:\n");
fprintf (stderr, " A, C, D50 (def.), D50M2, D65, F5, F8, F10 or file.sp\n");
fprintf (stderr, " -o observ Choose CIE Observer for spectral data:\n");
fprintf (stderr, " 1931_2 (def), 1964_10, S&B 1955_2, shaw, J&V 1978_2 or file.cmf\n");
fprintf (stderr, " -n Don't output spectral values\n");
#ifdef ALLOW_PLOT
fprintf (stderr, " -p Plot each values spectrum\n");
#endif
fprintf (stderr, " input.[ti3|sp] Measurement file\n");
fprintf (stderr, " output.[ti3|sp] Converted measurement file\n");
exit (1);
}
int
main(int argc, char *argv[])
{
int fa, nfa; /* current argument we're looking at */
int verb = 0;
int nospec = 0; /* NZ if not to output spectral values */
char *in_ti3_name;
char *out_ti3_name;
cgats *icg; /* input cgats structure */
cgats *ocg; /* output cgats structure */
cgats_set_elem *elems;
int isspect = 0; /* nz if SPECT file rathe than TI3 */
int isemis = 0; /* nz if this is an emissive reference */
int isdisp = 0; /* nz if this is a display device */
int isdnormed = 0; /* Has display data been normalised to 100 ? */
icColorSpaceSignature devspace = icmSigDefaultData; /* The device colorspace */
int isAdditive = 0; /* 0 if subtractive, 1 if additive colorspace */
int isInverted = 0; /* nz if inverted real device */
int ci, mi, yi, ki; /* Indexes of device values */
int fwacomp = 0; /* FWA compensation */
int doplot = 0; /* Plot each patches spectrum */
char* illum_str = "D50";
icxIllumeType tillum = icxIT_none; /* Target/simulated instrument illuminant, if set. */
xspect cust_tillum, *tillump = NULL; /* Custom target/simulated illumination spectrum */
/* if tillum == icxIT_custom */
icxIllumeType illum = icxIT_none; /* CIE calc. illuminant spectrum, and FWA inst. */
/* illuminant if tillum not set. */
xspect cust_illum; /* Custom CIE illumination spectrum if illum == icxIT_custom */
double *ill_wp = NULL; /* If illum is not D50, illum white point XYZ */
double _ill_wp[3]; /* (What ill_wp points at if it is not NULL) */
icxIllumeType inst_illum = icxIT_none; /* Actual instrument illumination */
xspect inst_cust_illum; /* Custom actual instrument illumination spectrum */
/* if inst_illum == icxIT_custom */
icxObserverType observ = icxOT_none;
xspect cust_observ[3]; /* Custom observer CMF's */
int npat; /* Number of patches */
int dti; /* Device Type index */
char *kw;
int i, j, jj, k;
#if defined(__IBMC__)
_control87 (EM_UNDERFLOW, EM_UNDERFLOW);
_control87 (EM_OVERFLOW, EM_OVERFLOW);
#endif
if (argc < 3)
usage ();
/* Process the arguments */
for (fa = 1; fa < argc; fa++) {
nfa = fa; /* skip to nfa if next argument is used */
if (argv[fa][0] == '-') { /* Look for any flags */
char *na = NULL; /* next argument after flag, null if none */
if (argv[fa][2] != '\000')
na = &argv[fa][2]; /* next is directly after flag */
else {
if ((fa + 1) < argc) {
if (argv[fa + 1][0] != '-') {
nfa = fa + 1;
na = argv[nfa]; /* next is seperate non-flag argument */
}
}
}
if (argv[fa][1] == '?')
usage ();
/* Verbose */
else if (argv[fa][1] == 'v' || argv[fa][1] == 'V')
verb = 1;
/* Don't output spectral */
else if (argv[fa][1] == 'n' || argv[fa][1] == 'N')
nospec = 1;
/* Plot each patch spectral value */
else if (argv[fa][1] == 'p' || argv[fa][1] == 'P')
doplot = 1;
/* Instrument Illuminant type */
else if (argv[fa][1] == 'I') {
fa = nfa;
if (na == NULL)
usage ();
if (strcmp (na, "A") == 0) {
inst_illum = icxIT_A;
}
else if (strcmp (na, "C") == 0) {
inst_illum = icxIT_C;
}
else if (strcmp (na, "D50") == 0) {
inst_illum = icxIT_D50;
}
else if (strcmp (na, "D50M2") == 0) {
inst_illum = icxIT_D50M2;
}
else if (strcmp (na, "D65") == 0) {
inst_illum = icxIT_D65;
}
else if (strcmp (na, "F5") == 0) {
inst_illum = icxIT_F5;
}
else if (strcmp (na, "F8") == 0) {
inst_illum = icxIT_F8;
}
else if (strcmp (na, "F10") == 0) {
inst_illum = icxIT_F10;
}
else { /* Assume it's a filename */
inst_meas_type mt;
inst_illum = icxIT_custom;
if (read_xspect (&inst_cust_illum, &mt,na) != 0)
usage ();
if (mt != inst_mrt_none
&& mt != inst_mrt_emission
&& mt != inst_mrt_ambient
&& mt != inst_mrt_emission_flash
&& mt != inst_mrt_ambient_flash)
error("Instrument illuminant '%s' is wrong measurement type",na);
}
}
/* FWA comp & simulated instrument illuminant */
else if (argv[fa][1] == 'f') {
fwacomp = 1;
if (na != NULL) { /* Argument is present - target/simulated instr. illum. */
fa = nfa;
if (strcmp(na, "A") == 0
|| strcmp(na, "M0") == 0) {
tillum = icxIT_A;
} else if (strcmp(na, "C") == 0) {
tillum = icxIT_C;
} else if (strcmp(na, "D50") == 0
|| strcmp(na, "M1") == 0) {
tillum = icxIT_D50;
} else if (strcmp(na, "D50M2") == 0
|| strcmp(na, "M2") == 0) {
tillum = icxIT_D50M2;
} else if (strcmp(na, "D65") == 0) {
tillum = icxIT_D65;
} else if (strcmp(na, "F5") == 0) {
tillum = icxIT_F5;
} else if (strcmp(na, "F8") == 0) {
tillum = icxIT_F8;
} else if (strcmp(na, "F10") == 0) {
tillum = icxIT_F10;
} else { /* Assume it's a filename */
inst_meas_type mt;
tillum = icxIT_custom;
if (read_xspect(&cust_tillum, &mt, na) != 0)
usage();
if (mt != inst_mrt_none
&& mt != inst_mrt_emission
&& mt != inst_mrt_ambient
&& mt != inst_mrt_emission_flash
&& mt != inst_mrt_ambient_flash)
error("Target illuminant '%s' is wrong measurement type",na);
}
}
}
/* CIE tristimulous spectral Illuminant type */
/* (and FWA simulated instrument illuminant if tillum == icxIT_none) */
else if (argv[fa][1] == 'i') {
fa = nfa;
if (na == NULL)
usage ();
illum_str = na;
if (strcmp (na, "A") == 0) {
illum = icxIT_A;
}
else if (strcmp (na, "C") == 0) {
illum = icxIT_C;
}
else if (strcmp (na, "D50") == 0) {
illum = icxIT_D50;
}
else if (strcmp (na, "D50M2") == 0) {
illum = icxIT_D50M2;
}
else if (strcmp (na, "D65") == 0) {
illum = icxIT_D65;
}
else if (strcmp (na, "F5") == 0) {
illum = icxIT_F5;
}
else if (strcmp (na, "F8") == 0) {
illum = icxIT_F8;
}
else if (strcmp (na, "F10") == 0) {
illum = icxIT_F10;
}
else { /* Assume it's a filename */
inst_meas_type mt;
illum = icxIT_custom;
if (read_xspect (&cust_illum, &mt, na) != 0)
usage ();
if (mt != inst_mrt_none
&& mt != inst_mrt_emission
&& mt != inst_mrt_ambient
&& mt != inst_mrt_emission_flash
&& mt != inst_mrt_ambient_flash)
error("CIE illuminant '%s' is wrong measurement type",na);
}
}
/* Spectral Observer type */
else if (argv[fa][1] == 'o' || argv[fa][1] == 'O') {
fa = nfa;
if (na == NULL)
usage ();
if (strcmp (na, "1931_2") == 0) { /* Classic 2 degree */
observ = icxOT_CIE_1931_2;
}
else if (strcmp (na, "1964_10") == 0) { /* Classic 10 degree */
observ = icxOT_CIE_1964_10;
}
else if (strcmp (na, "1955_2") == 0) { /* Stiles and Burch 1955 2 degree */
observ = icxOT_Stiles_Burch_2;
}
else if (strcmp (na, "1978_2") == 0) { /* Judd and Voss 1978 2 degree */
observ = icxOT_Judd_Voss_2;
}
else if (strcmp (na, "shaw") == 0) { /* Shaw and Fairchilds 1997 2 degree */
observ = icxOT_Shaw_Fairchild_2;
}
else { /* Assume it's a filename */
observ = icxOT_custom;
if (read_cmf (cust_observ, na) != 0)
usage ();
}
}
else
usage ();
}
else
break;
}
/* Get the file name arguments */
if (fa >= argc || argv[fa][0] == '-')
usage ();
in_ti3_name = argv[fa++];
if (fa >= argc || argv[fa][0] == '-')
usage ();
out_ti3_name = argv[fa++];
/* Open and look at the .ti3 profile patches file */
icg = new_cgats (); /* Create a CGATS structure */
icg->add_other (icg, "CTI3"); /* Calibration Target Information 3 */
icg->add_other (icg, "SPECT"); /* Spectral file */
ocg = new_cgats (); /* Create a CGATS structure */
ocg->add_other (ocg, "CTI3"); /* Calibration Target Information 3 */
icg->add_other (ocg, "SPECT"); /* Spectral file */
if (icg->read_name (icg, in_ti3_name))
error ("CGATS file read error: %s", icg->err);
if (icg->ntables == 0 || icg->t[0].tt != tt_other
|| (icg->t[0].oi != 0 && icg->t[0].oi != 1))
error ("Input file isn't a CTI3 or SPECT format file");
if (icg->t[0].oi == 1)
isspect = 1;
if (icg->ntables < 1)
error ("Input file doesn't contain at least one table");
/* add table to output file */
if (isspect)
ocg->add_table(ocg, tt_other, 1);
else
ocg->add_table(ocg, tt_other, 0);
/* copy keywords */
for (i = 0; i < icg->t[0].nkwords; i++) {
kw = icg->t[0].ksym[i];
if (fwacomp && strcmp(kw, "TARGET_INSTRUMENT") == 0) {
/*
* overwrite TARGET_INSTRUMENT with the new illuminant,
* since the FWA corrected spectral data are no longer
* valid for the original instrument's illuminant
*/
ocg->add_kword (ocg, 0, kw, illum_str, NULL);
}
else {
ocg->add_kword (ocg, 0, kw,
icg->t[0].kdata[i], icg->t[0].kcom[i]);
}
}
if (isspect) {
if ((dti = icg->find_kword (icg, 0, "MEAS_TYPE")) < 0)
error ("Input file doesn't contain keyword MEAS_TYPE");
/* Reflective options when not a reflective profile type */
if (strcmp(icg->t[0].kdata[dti],"EMISSION") == 0
|| strcmp(icg->t[0].kdata[dti],"AMBIENT") == 0
|| strcmp(icg->t[0].kdata[dti],"EMISSION_FLASH") == 0
|| strcmp(icg->t[0].kdata[dti],"AMBIENT_FLASH") == 0) {
isemis = 1;
if (illum != icxIT_none)
error("-i illuminant can't be used for emissive reference type");
if (fwacomp)
error("-f FWA compensation can't be used for emissive reference type");
fwacomp = 0;
tillum = icxIT_none;
}
} else {
if ((dti = icg->find_kword (icg, 0, "DEVICE_CLASS")) < 0)
error ("Input file doesn't contain keyword DEVICE_CLASS");
/* Reflective options when not a reflective profile type */
if (strcmp(icg->t[0].kdata[dti],"DISPLAY") == 0
|| strcmp(icg->t[0].kdata[dti],"EMISINPUT") == 0) {
isemis = 1;
if (illum != icxIT_none)
error("-i illuminant can't be used for emissive reference type");
if (fwacomp)
error("-f FWA compensation can't be used for emissive reference type");
fwacomp = 0;
tillum = icxIT_none;
}
}
/* Set defaults */
if (illum == icxIT_none)
illum = icxIT_D50;
if (observ == icxOT_none)
observ = icxOT_CIE_1931_2;
/* See if the display CIE data has been normalised to Y = 100 */
{
int ti;
if ((ti = icg->find_kword(icg, 0, "NORMALIZED_TO_Y_100")) < 0
|| strcmp(icg->t[0].kdata[ti],"NO") == 0) {
isdnormed = 0;
} else {
isdnormed = 1;
}
}
/* Figure out what sort of device it is */
if (icg->find_kword(icg, 0, "COLOR_REP") >= 0) {
int ti;
char *tos;
if ((ti = icg->find_kword(icg, 0, "COLOR_REP")) < 0)
error("Input file doesn't contain keyword COLOR_REP");
if (strcmp (icg->t[0].kdata[dti], "DISPLAY") == 0) {
isdisp = 1;
}
if ((tos = strchr(icg->t[0].kdata[ti], '_')) == NULL)
tos = icg->t[0].kdata[ti];
if (strncmp(icg->t[0].kdata[ti],"CMYK_",5) == 0
|| strncmp(tos,"_CMYK",5) == 0) {
devspace = icSigCmykData;
} else if (strncmp(icg->t[0].kdata[ti],"CMY_",4) == 0
|| strncmp(tos,"_CMY",4) == 0) {
devspace = icSigCmyData;
} else if (strncmp(icg->t[0].kdata[ti],"RGB_",4) == 0
|| strncmp(tos,"_RGB",4) == 0) {
devspace = icSigRgbData;
} else if (strncmp(icg->t[0].kdata[ti],"iRGB_",4) == 0
|| strncmp(tos,"_iRGB",4) == 0) {
devspace = icSigRgbData;
isInverted = 1;
} else if (strncmp(icg->t[0].kdata[ti],"K_",2) == 0
|| strncmp(tos,"_K",2) == 0) {
devspace = icSigGrayData;
isAdditive = 0;
} else if (strncmp(icg->t[0].kdata[ti],"W_",2) == 0
|| strncmp(tos,"_W",2) == 0) {
devspace = icSigGrayData;
isAdditive = 1;
} else
error("Device has unhandled color representation '%s'",icg->t[0].kdata[ti]);
if (devspace == icSigGrayData) {
if (isAdditive) {
if ((ci = icg->find_field(icg, 0, "GRAY_W")) < 0)
error("Input file doesn't contain field GRAY_W");
if (icg->t[0].ftype[ci] != r_t)
error("Field GRAY_W is wrong type - expect float");
} else {
if ((ci = icg->find_field(icg, 0, "GRAY_K")) < 0)
error("Input file doesn't contain field GRAY_K");
if (icg->t[0].ftype[ci] != r_t)
error("Field GRAY_K is wrong type - expect float");
}
mi = yi = ki = ci;
} else if (devspace == icSigRgbData) {
if ((ci = icg->find_field(icg, 0, "RGB_R")) < 0)
error("Input file doesn't contain field RGB_R");
if (icg->t[0].ftype[ci] != r_t)
error("Field RGB_R is wrong type - expect float");
if ((mi = icg->find_field(icg, 0, "RGB_G")) < 0)
error("Input file doesn't contain field RGB_G");
if (icg->t[0].ftype[mi] != r_t)
error("Field RGB_G is wrong type - expect float");
if ((yi = icg->find_field(icg, 0, "RGB_B")) < 0)
error("Input file doesn't contain field RGB_B");
if (icg->t[0].ftype[yi] != r_t)
error("Field RGB_B is wrong type - expect float");
ki = yi;
} else if (devspace == icSigCmyData) {
if ((ci = icg->find_field(icg, 0, "CMY_C")) < 0)
error("Input file doesn't contain field CMY_C");
if (icg->t[0].ftype[ci] != r_t)
error("Field CMY_C is wrong type - expect float");
if ((mi = icg->find_field(icg, 0, "CMY_M")) < 0)
error("Input file doesn't contain field CMY_M");
if (icg->t[0].ftype[mi] != r_t)
error("Field CMY_M is wrong type - expect float");
if ((yi = icg->find_field(icg, 0, "CMY_Y")) < 0)
error("Input file doesn't contain field CMY_Y");
if (icg->t[0].ftype[yi] != r_t)
error("Field CMY_Y is wrong type - expect float");
ki = yi;
} else { /* Assume CMYK */
if ((ci = icg->find_field(icg, 0, "CMYK_C")) < 0)
error("Input file doesn't contain field CMYK_C");
if (icg->t[0].ftype[ci] != r_t)
error("Field CMYK_C is wrong type - expect float",icg->t[0].ftype[ci],r_t);
if ((mi = icg->find_field(icg, 0, "CMYK_M")) < 0)
error("Input file doesn't contain field CMYK_M");
if (icg->t[0].ftype[mi] != r_t)
error("Field CMYK_M is wrong type - expect float");
if ((yi = icg->find_field(icg, 0, "CMYK_Y")) < 0)
error("Input file doesn't contain field CMYK_Y");
if (icg->t[0].ftype[yi] != r_t)
error("Field CMYK_Y is wrong type - expect float");
if ((ki = icg->find_field(icg, 0, "CMYK_K")) < 0)
error("Input file doesn't contain field CMYK_K");
if (icg->t[0].ftype[ki] != r_t)
error("Field CMYK_K is wrong type - expect float");
}
}
if ((npat = icg->t[0].nsets) <= 0)
error ("No sets of data");
/* Read in the CGATs fields */
{
// int sidx; /* Sample ID index */
int ti, ii;
int Xi, Yi, Zi, Li, ai, bi; /* CGATS indexes for each field */
int spi[XSPECT_MAX_BANDS]; /* CGATS indexes for each wavelength */
int oXi, oYi, oZi, oLi, oai, obi; /* CGATS indexes for each ouput field */
xsp2cie *sp2cie; /* Spectral conversion object */
xspect sp;
double XYZ[3];
double Lab[3];
char buf[100];
/* These are only set if fwa is needed */
xspect rmwsp; /* Raw medium white spectrum */
xspect mwsp; /* FWA compensated medium white spectrum */
double mwXYZ[3]; /* Media white XYZ */
#ifdef NEVER
if ((sidx = icg->find_field (icg, 0, "SAMPLE_ID")) < 0)
error ("Input file doesn't contain field SAMPLE_ID");
if (icg->t[0].ftype[sidx] != nqcs_t)
error ("Field SAMPLE_ID is wrong type");
#endif
/* Using spectral data */
if ((ii = icg->find_kword (icg, 0, "SPECTRAL_BANDS")) < 0)
error ("Input file doesn't contain keyword SPECTRAL_BANDS");
sp.spec_n = atoi (icg->t[0].kdata[ii]);
if ((ii = icg->find_kword (icg, 0, "SPECTRAL_START_NM")) < 0)
error ("Input file doesn't contain keyword SPECTRAL_START_NM");
sp.spec_wl_short = atof (icg->t[0].kdata[ii]);
if ((ii = icg->find_kword (icg, 0, "SPECTRAL_END_NM")) < 0)
error ("Input file doesn't contain keyword SPECTRAL_END_NM");
sp.spec_wl_long = atof (icg->t[0].kdata[ii]);
if (!isdisp || isdnormed != 0)
sp.norm = 100.0;
else
sp.norm = 1.0;
/* Find the fields for spectral values */
for (j = 0; j < sp.spec_n; j++) {
int nm;
/* Compute nearest integer wavelength */
nm = (int) (sp.spec_wl_short +
((double) j / (sp.spec_n - 1.0))
* (sp.spec_wl_long - sp.spec_wl_short) + 0.5);
sprintf (buf, "SPEC_%03d", nm);
if ((spi[j] = icg->find_field (icg, 0, buf)) < 0)
error ("Input file doesn't contain field %s", buf);
if (icg->t[0].ftype[spi[j]] != r_t)
error("Field %s is wrong type - expect float",buf);
}
if (isemis) {
illum = icxIT_none;
}
/* copy fields to output file (except spectral if nospec) */
for (i = 0; i < icg->t[0].nfields; i++) {
if (nospec) {
for (j = 0; nospec && j < sp.spec_n; j++) {
if (spi[j] == i) {
break; /* Yes it is */
}
}
if (j < sp.spec_n)
continue; /* Skip it */
}
ocg->add_field (ocg, 0, icg->t[0].fsym[i], icg->t[0].ftype[i]);
}
/* create field for XYZ and Lab if not present */
if ((Xi = icg->find_field(icg, 0, "XYZ_X")) < 0)
if ((Xi = ocg->add_field(ocg, 0, "XYZ_X", r_t)) < 0)
error ("Cannot add field to table");
if ((Yi = icg->find_field(icg, 0, "XYZ_Y")) < 0)
if ((Yi = ocg->add_field(ocg, 0, "XYZ_Y", r_t)) < 0)
error ("Cannot add field to table");
if ((Zi = icg->find_field(icg, 0, "XYZ_Z")) < 0)
if ((Zi = ocg->add_field(ocg, 0, "XYZ_Z", r_t)) < 0)
error ("Cannot add field to table");
if ((Li = icg->find_field(icg, 0, "LAB_L")) < 0)
if ((Li = ocg->add_field(ocg, 0, "LAB_L", r_t)) < 0)
error ("Cannot add field to table");
if ((ai = icg->find_field(icg, 0, "LAB_A")) < 0)
if ((ai = ocg->add_field(ocg, 0, "LAB_A", r_t)) < 0)
error ("Cannot add field to table");
if ((bi = icg->find_field(icg, 0, "LAB_B")) < 0)
if ((bi = ocg->add_field(ocg, 0, "LAB_B", r_t)) < 0)
error ("Cannot add field to table");
oXi = Xi;
oYi = Yi;
oZi = Zi;
oLi = Li;
oai = ai;
obi = bi;
/* allocate elements */
if ((elems = (cgats_set_elem *)
calloc(ocg->t[0].nfields, sizeof(cgats_set_elem))) == NULL)
{
error("Out of memory");
}
/* If CIE calculation illuminant is not standard, compute it's white point */
if (illum != icxIT_D50) {
ill_wp = _ill_wp;
/* Compute XYZ of illuminant */
if (icx_ill_sp2XYZ(ill_wp, observ, cust_observ, illum, 0.0, &cust_illum) != 0)
error("icx_ill_sp2XYZ returned error");
}
/* Create a spectral conversion object */
if ((sp2cie = new_xsp2cie(illum, &cust_illum, observ, cust_observ,
icSigXYZData, icxClamp)) == NULL)
{
error ("Creation of spectral conversion object failed");
}
if (fwacomp && devspace == icmSigDefaultData) {
// In theory could fake white spectra by accumulating max of
// all values as an alternative.
error ("No device values, so can't locate white patch for FWA compensation");
}
if (fwacomp) {
double nw = 0.0; /* Number of media white patches */
rmwsp = sp; /* Initial Struct copy */
/* Find the media white spectral reflectance */
for (j = 0; j < rmwsp.spec_n; j++)
rmwsp.spec[j] = 0.0;
/* Compute the mean of all the media white patches */
for (i = 0; i < npat; i++) {
int use = 0;
if (devspace == icSigGrayData) {
if (isAdditive) {
if (*((double *)icg->t[0].fdata[i][ci]) > (100.0 - 0.1))
use = 1;
} else {
if (*((double *)icg->t[0].fdata[i][ci]) < 0.1)
use = 1;
}
} else if (devspace == icSigRgbData) {
if (*((double *)icg->t[0].fdata[i][ci]) > (100.0 - 0.1)
&& *((double *)icg->t[0].fdata[i][mi]) > (100.0 - 0.1)
&& *((double *)icg->t[0].fdata[i][yi]) > (100.0 - 0.1))
use = 1;
} else if (devspace == icSigCmyData) {
if (*((double *)icg->t[0].fdata[i][ci]) < 0.1
&& *((double *)icg->t[0].fdata[i][mi]) < 0.1
&& *((double *)icg->t[0].fdata[i][yi]) < 0.1)
use = 1;
} else { /* Assume CMYK */
if (*((double *)icg->t[0].fdata[i][ci]) < 0.1
&& *((double *)icg->t[0].fdata[i][mi]) < 0.1
&& *((double *)icg->t[0].fdata[i][yi]) < 0.1
&& *((double *)icg->t[0].fdata[i][ki]) < 0.1) {
use = 1;
}
}
if (use) {
/* Read the spectral values for this patch */
for (j = 0; j < rmwsp.spec_n; j++) {
rmwsp.spec[j] += *((double *)icg->t[0].fdata[i][spi[j]]);
}
nw++;
}
}
if (nw == 0.0) {
warning("Can't find a media white patch to compute white reference");
warning("Using maximum of all spectral readings instead");
/* Track the maximum reflectance for any band to determine white. */
/* This might give bogus results if there is no white patch... */
for (i = 0; i < npat; i++) {
for (j = 0; j < rmwsp.spec_n; j++) {
double rv = *((double *)icg->t[0].fdata[i][spi[j]]);
if (rv > rmwsp.spec[j])
rmwsp.spec[j] = rv;
}
}
nw++;
}
for (j = 0; j < rmwsp.spec_n; j++) {
rmwsp.spec[j] /= nw; /* Compute average */
}
mwsp = rmwsp; /* Structure copy */
}
if (fwacomp) {
instType itype; /* Spectral instrument type */
xspect insp; /* Instrument illuminant */
if (inst_illum == icxIT_none) {
/* try to get from .ti3 file */
if ((ti = icg->find_kword (icg, 0, "TARGET_INSTRUMENT")) < 0)
error ("Can't find target instrument needed for FWA compensation");
if ((itype = inst_enum (icg->t[0].kdata[ti])) == instUnknown)
error ("Unrecognised target instrument '%s'",
icg->t[0].kdata[ti]);
if (inst_illuminant (&insp, itype) != 0)
error ("Instrument doesn't have an FWA illuminent");
}
else if (inst_illum == icxIT_custom) {
insp = inst_cust_illum; /* Structure copy */
}
else {
if (standardIlluminant(&insp, inst_illum, 0) != 0)
error ("Failed to find standard illuminant");
}
/* If we are setting a specific simulated instrument illuminant */
if (tillum != icxIT_none) {
tillump = &cust_tillum;
if (tillum != icxIT_custom) {
if (standardIlluminant(tillump, tillum, 0.0)) {
error("simulated inst. illum. not recognised");
}
}
}
/* Enable FWA, and use tillump as instrument illuminant if */
/* it is set, else use observer illuminant set by new_xsp2cie(). */
/* (Note that sp and mwsp.norm is set to 100.0) */
if (sp2cie->set_fwa(sp2cie, &insp, tillump, &mwsp))
error ("Set FWA on sp2cie failed");
if (verb) {
double FWAc;
sp2cie->get_fwa_info(sp2cie, &FWAc);
printf("FWA content = %f\n",FWAc);
}
/* Create an FWA compensated white spectrum and XYZ value */
sp2cie->sconvert (sp2cie, &rmwsp, mwXYZ, &mwsp);
}
/* If CIE conversion illuminant is non-standard, add it to the output */
if (ill_wp != NULL) {
char buf[100];
sprintf(buf,"%f %f %f", ill_wp[0], ill_wp[1], ill_wp[2]);
ocg->add_kword(ocg, 0, "ILLUMINANT_WHITE_POINT_XYZ",buf, NULL);
}
/* Transform patches from spectral to CIE */
for (i = 0; i < npat; i++) {
xspect corr_sp;
/* copy all input colums to output (except spectral if nospec) */
for (jj = j = 0; j < icg->t[0].nfields; j++) {
if (nospec) {
/* See if this is a spectral field */
for (k = 0; nospec && k < sp.spec_n; k++) {
if (spi[k] == j)
break;
}
/* It is a spectral field */
if (k < sp.spec_n) {
continue; /* Skip it */
}
}
/* Correct the other fields location in output */
if (j == Xi) oXi = jj;
if (j == Yi) oYi = jj;
if (j == Zi) oZi = jj;
if (j == Li) oLi = jj;
if (j == ai) oai = jj;
if (j == bi) obi = jj;
switch (icg->t[0].ftype[j]) {
case r_t:
elems[jj].d = *((double *) icg->t[0].fdata[i][j]);
break;
case i_t:
elems[jj].i = *((int *) icg->t[0].fdata[i][j]);
break;
default:
elems[jj].c = (char *) icg->t[0].fdata[i][j];
}
jj++;
}
/* Read the spectral values for this patch */
for (j = 0; j < sp.spec_n; j++) {
sp.spec[j] = *((double *)icg->t[0].fdata[i][spi[j]]);
}
if (fwacomp) {
corr_sp = sp; /* Copy spectrum */
/* Convert it to CIE space */
sp2cie->sconvert (sp2cie, &corr_sp, XYZ, &sp);
/* Write the corrected spectral values for this patch */
if (nospec == 0) {
for (j = 0; j < sp.spec_n; j++) {
elems[spi[j]].d = sp.spec[j] = corr_sp.spec[j];
}
}
}
/* No FWA comp */
else {
/* Convert it to CIE space */
sp2cie->convert (sp2cie, XYZ, &sp);
}
/* Could use sp2cie->get_cie_il() to get CIE white point */
/* if we wanted to return L*a*b* relative to that. */
/* We would have to mark that in the .ti3 though. */
/* This won't work for emmisive though, since get_cie_il() will return 'E' */
icmXYZ2Lab(&icmD50, Lab, XYZ);
#ifdef ALLOW_PLOT
if (doplot) {
int ii;
double xx[XRES];
double y1[XRES];
printf("Patch %d, XYZ = %f %f %f, Lab = %f %f %f\n",i,
XYZ[0], XYZ[1], XYZ[2], Lab[0], Lab[1], Lab[2]);
/* Plot spectrum out */
for (ii = 0; ii < XRES; ii++) {
double ww;
ww = (sp.spec_wl_long - sp.spec_wl_short)
* ((double)ii/(XRES-1.0)) + sp.spec_wl_short;
xx[ii] = ww;
y1[ii] = value_xspect(&sp, ww);
}
do_plot(xx,y1,NULL,NULL,ii);
}
#endif
elems[oXi].d = XYZ[0] * 100.0;
elems[oYi].d = XYZ[1] * 100.0;
elems[oZi].d = XYZ[2] * 100.0;
elems[oLi].d = Lab[0];
elems[oai].d = Lab[1];
elems[obi].d = Lab[2];
ocg->add_setarr(ocg, 0, elems);
}
if (ocg->write_name(ocg, out_ti3_name)) {
error ("Write error: %s", ocg->err);
}
sp2cie->del (sp2cie); /* Done with this */
ocg->del (ocg); /* Clean up */
icg->del (icg); /* Clean up */
free (elems);
}
return 0;
}
|