-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspotread.c
3655 lines (3320 loc) · 114 KB
/
spotread.c
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
/* Spectrometer/Colorimeter color spot reader utility */
/*
* Argyll Color Correction System
* Author: Graeme W. Gill
* Date: 3/10/2001
*
* Derived from printread.c/chartread.c
* Was called printspot.
*
* Copyright 2001 - 2013 Graeme W. Gill
* All rights reserved.
*
* This material is licenced under the GNU GENERAL PUBLIC LICENSE Version 2 or later :-
* see the License2.txt file for licencing details.
*/
/* This program reads a spot reflection/transmission/emission value using */
/* a spectrometer or colorimeter. */
/* TTBD
*
* Add option to automatically read continuously, until stopped. (A bit like -O)
*
* Make -V average the spectrum too (if present), and allow it to
* be saved to a .sp file.
*
* Should fix plot so that it is a separate object running its own thread,
* so that it can be sent a graph without needing to be clicked in all the time.
*
* Should add option for Y u' v' values.
*/
#undef DEBUG
#undef TEST_EVENT_CALLBACK /* Report async event callbacks */
#define COMPORT 1 /* Default com port 1..4 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include "mex.h"
#include <matrix.h>
#include <sys/stat.h>
#ifndef SALONEINSTLIB
# include "copyright.h"
# include "aconfig.h"
# include "numlib.h"
# include "cgats.h"
# include "xicc.h"
# include "conv.h"
//# include "plot.h"
# include "ui.h"
#else /* SALONEINSTLIB */
# include "sa_config.h"
# include "numsup.h"
# include "xspect.h"
# include "conv.h"
#endif /* SALONEINSTLIB */
#include "inst.h"
#include "icoms.h"
#include "ccss.h"
#include "ccmx.h"
#include "instappsup.h"
#include "iusb.h"
#if !defined(NOT_ALLINSTS) || defined(EN_SPYD2)
# include "spyd2.h"
#endif
#include "instappsup.h"
#if defined (NT)
#include <conio.h>
#endif
#undef DO_TM3015_PLOT /* Diagnostic */
#define COMMAND_LENGTH 100
/* ----------------------------------------------------------------- */
#ifdef DO_TM3015_PLOT
#pragma message("#### spectro/spotread.c DO_TM3015_PLOT is enabled ####")
#define SSAMP 4
// Note that bins is modified
// static void tm3015_plot(double bins[IES_TM_30_15_BINS][2][3]) {
// double rvecs[SSAMP * 16][2]; // DEBUG ref light circle vectors
// double tvecs[SSAMP * 16][2]; // test light circle vectors
// double shvec[2 * 16][2]; // Shift vectors
// int i, j, k;
// double maxr = 0.0;
// plot_g gg = { 0 };
// float lblack[3] = { 0.5, 0.5, 0.5 };
// float lred[3] = { 1.0, 0.5, 0.5 };
// float black[3] = { 0.0, 0.0, 0.0 };
// float red[3] = { 1.0, 0.0, 0.0 };
// float blue[3] = { 0.2, 0.2, 1.0 };
//
// #ifdef NEVER
// clear_g(&gg);
//
// for (i = 0; i < 16; i++) {
// int ip1 = i < 15 ? i+1 : 0;
//
// add_vec_g(&gg, bins[i][0][1], bins[i][0][2], bins[ip1][0][1], bins[ip1][0][2], lblack);
// add_vec_g(&gg, bins[i][1][1], bins[i][1][2], bins[ip1][1][1], bins[ip1][1][2], lred);
// }
// //do_plot_g(&gg, 0.0, 0.0, 0.0, 0.0, 1.0, 0, 1);
// #endif
//
// clear_g(&gg);
//
// /* Copy raw shift vectors */
// for (i = 0; i < 16; i++) {
// shvec[2 * i + 0][0] = bins[i][0][1];
// shvec[2 * i + 0][1] = bins[i][0][2];
// shvec[2 * i + 1][0] = bins[i][1][1];
// shvec[2 * i + 1][1] = bins[i][1][2];
// }
//
// // Convert to angle & radius
// for (i = 0; i < 16; i++) {
// for (j = 0; j < 2; j++) {
// double x = bins[i][j][1];
// double y = bins[i][j][2];
//
// // atan2 returns -pi < val <= +pi
// bins[i][j][1] = atan2(y, x);
// bins[i][j][2] = sqrt(x * x + y * y);
// }
// }
//
// // Interpolate test points, and normalize them by interp. reference points */
// for (i = 0; i < 16; i++) {
// int ip1 = i < 15 ? i+1 : 0;
// int k = i, kp1 = ip1;
// for (j = 0; j < SSAMP; j++) { // 4 x interpolation
// double ra, rr, ta, tr;
// double ra0, ra1, ta0, ta1;
// double bl = (double)j/SSAMP;
// double nf = 1.0;
//
// //printf("vec %d i %d j %d bl %f\n",SSAMP * i + j, i, j, bl);
//
// ra0 = bins[i][0][1];
// ra1 = bins[ip1][0][1];
// ta0 = bins[i][1][1];
// ta1 = bins[ip1][1][1];
//
// //printf("raw ra0 %f ra1 %f ta0 %f ta1 %f\n", ra0, ra1, ta0, ta1);
//
// // Make sure pairs are ordered
// if ((ra1 - ra0) > (1.0 * DBL_PI))
// ra0 += 2.0 * DBL_PI;
// else if ((ra1 - ra0) < -(1.0 * DBL_PI))
// ra0 -= 2.0 * DBL_PI;
//
// if ((ta1 - ta0) > (1.0 * DBL_PI))
// ta0 += 2.0 * DBL_PI;
// else if ((ta1 - ta0) < -(1.0 * DBL_PI))
// ta0 -= 2.0 * DBL_PI;
//
// //printf("fix ra0 %f ra1 %f ta0 %f ta1 %f\n", ra0, ra1, ta0, ta1);
//
// // Interpolated values
// ra = (1.0 - bl) * ra0 + bl * ra1;
// rr = (1.0 - bl) * bins[i][0][2] + bl * bins[ip1][0][2];
// ta = (1.0 - bl) * ta0 + bl * ta1;
// tr = (1.0 - bl) * bins[i][1][2] + bl * bins[ip1][1][2];
//
// //printf(" raw ra %f rr %f ta %f tr %f\n",ra, rr, ta, tr);
//
// nf = rr; // Normalization factor
//
// tr /= nf;
// rr /= nf;
//
// if (tr > maxr)
// maxr = tr;
//
// //printf(" norm ra %f rr %f ta %f tr %f\n",ra, rr, ta, tr);
//
// rvecs[SSAMP * i + j][0] = ra;
// rvecs[SSAMP * i + j][1] = rr;
//
// tvecs[SSAMP * i + j][0] = ta;
// tvecs[SSAMP * i + j][1] = tr;
//
// // Set shift vectors
// if (j == 0) {
// shvec[2 * i + 0][0] = ra;
// shvec[2 * i + 0][1] = rr;
// shvec[2 * i + 1][0] = ta;
// shvec[2 * i + 1][1] = tr;
// //printf(" shft ra %f rr %f ta %f tr %f\n",ra, rr, ta, tr);
// }
// //printf("\n");
// }
// }
//
// //printf("Done all, convert back\n");
//
// // Convert back to cartesian
// for (i = 0; i < (SSAMP * 16); i++) {
// double a = tvecs[i][0];
// double r = tvecs[i][1];
//
// tvecs[i][0] = r * cos(a);
// tvecs[i][1] = r * sin(a);
// //printf(" tvecs[%d] a %f r %f x %f y %f\n",i,a,r,tvecs[i][0],tvecs[i][1]);
// }
//
// for (i = 0; i < (SSAMP * 16); i++) {
// double a = rvecs[i][0];
// double r = rvecs[i][1];
//
// rvecs[i][0] = r * cos(a);
// rvecs[i][1] = r * sin(a);
// //printf(" rvecs[%d] a %f r %f x %f y %f\n",i,a,r,rvecs[i][0],rvecs[i][1]);
// }
//
// for (i = 0; i < (2 * 16); i++) {
// double a = shvec[i][0];
// double r = shvec[i][1];
//
// shvec[i][0] = r * cos(a);
// shvec[i][1] = r * sin(a);
// //printf(" shvec[%d] a %f r %f x %f y %f\n",i,a,r,shvec[i][0],shvec[i][1]);
// }
//
// //printf("About to plot\n");
//
// for (i = 0; i < (SSAMP * 16); i++) {
// int ip1 = i < (SSAMP * 16 -1) ? i+1 : 0;
// double x0, y0, x1, y1, r0, r1;
//
// x0 = tvecs[i][0];
// y0 = tvecs[i][1];
// x1 = tvecs[ip1][0];
// y1 = tvecs[ip1][1];
//
// r0 = sqrt(x0 * x0 + y0 * y0);
// r1 = sqrt(x1 * x1 + y1 * y1);
//
// add_vec_g(&gg, x0/r0, y0/r0, x1/r1, y1/r1, black);
// // add_vec_g(&gg, rvecs[i][0], rvecs[i][1], rvecs[ip1][0], rvecs[ip1][1], black);
//
// add_vec_g(&gg, x0, y0, x1, y1, red);
//
// }
//
// for (i = 0; i < 16; i++) {
// add_vec_g(&gg, shvec[2 * i + 0][0], shvec[2 * i + 0][1],
// shvec[2 * i + 1][0], shvec[2 * i + 1][1], blue);
// }
//
// do_plot_g(&gg, 0.0, 0.0, 0.0, 0.0, 1.0, 0, 1);
//
// }
#undef SSAMP
#endif /* DO_TM3015_PLOT */
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#ifdef NEVER /* Not currently used */
/* Convert control chars to ^[A-Z] notation in a string */
static char *
fix_asciiz(char *s) {
static char buf [200];
char *d;
for(d = buf; ;) {
if (*s < ' ' && *s > '\000') {
*d++ = '^';
*d++ = *s++ + '@';
} else
*d++ = *s++;
if (s[-1] == '\000')
break;
}
return buf;
}
#endif
#ifdef SALONEINSTLIB
#define D50_X_100 96.42
#define D50_Y_100 100.00
#define D50_Z_100 82.49
/* CIE XYZ 0..100 to perceptual D50 CIE 1976 L*a*b* */
static void
XYZ2Lab(double *out, double *in) {
double X = in[0], Y = in[1], Z = in[2];
double x,y,z,fx,fy,fz;
x = X/D50_X_100;
y = Y/D50_Y_100;
z = Z/D50_Z_100;
if (x > 0.008856451586)
fx = pow(x,1.0/3.0);
else
fx = 7.787036979 * x + 16.0/116.0;
if (y > 0.008856451586)
fy = pow(y,1.0/3.0);
else
fy = 7.787036979 * y + 16.0/116.0;
if (z > 0.008856451586)
fz = pow(z,1.0/3.0);
else
fz = 7.787036979 * z + 16.0/116.0;
out[0] = 116.0 * fy - 16.0;
out[1] = 500.0 * (fx - fy);
out[2] = 200.0 * (fy - fz);
}
/* Return the normal Delta E given two Lab values */
static double LabDE(double *Lab0, double *Lab1) {
double rv = 0.0, tt;
tt = Lab0[0] - Lab1[0];
rv += tt * tt;
tt = Lab0[1] - Lab1[1];
rv += tt * tt;
tt = Lab0[2] - Lab1[2];
rv += tt * tt;
return sqrt(rv);
}
/* Lab to LCh */
void Lab2LCh(double *out, double *in) {
double C, h;
C = sqrt(in[1] * in[1] + in[2] * in[2]);
h = (180.0/3.14159265359) * atan2(in[2], in[1]);
h = (h < 0.0) ? h + 360.0 : h;
out[0] = in[0];
out[1] = C;
out[2] = h;
}
/* XYZ to Yxy */
static void XYZ2Yxy(double *out, double *in) {
double sum = in[0] + in[1] + in[2];
double Y, x, y;
if (sum < 1e-9) {
Y = 0.0;
y = 0.0;
x = 0.0;
} else {
Y = in[1];
x = in[0]/sum;
y = in[1]/sum;
}
out[0] = Y;
out[1] = x;
out[2] = y;
}
#endif /* SALONEINSTLIB */
/* Replacement for gets */
char *getns(char *buf, int len) {
int i;
if (fgets(buf, len, stdin) == NULL)
return NULL;
for (i = 0; i < len; i++) {
if (buf[i] == '\n') {
buf[i] = '\000';
return buf;
}
}
buf[len-1] = '\000';
return buf;
}
/* Deal with an instrument error. */
/* Return 0 to retry, 1 to abort */
static int ierror(inst *it, inst_code ic) {
int ch;
empty_con_chars();
printf("Got '%s' (%s) error.\nHit Esc or Q to give up, any other key to retry:",
it->inst_interp_error(it, ic), it->interp_error(it, ic));
fflush(stdout);
ch = next_con_char();
printf("\n");
if (ch == 0x03 || ch == 0x1b || ch == 'q' || ch == 'Q') /* Escape, ^C or Q */
return 1;
return 0;
}
#ifdef TEST_EVENT_CALLBACK
void test_event_callback(void *cntx, inst_event_type event) {
a1logd(g_log,0,"Got event_callback with 0x%x\n",event);
}
#endif
#if defined(__APPLE__) && defined(__POWERPC__)
/* Workaround for a ppc gcc 3.3 optimiser bug... */
static int gcc_bug_fix(int i) {
static int nn;
nn += i;
return nn;
}
#endif /* APPLE */
/* A user callback to trigger for -O option */
static inst_code uicallback(void *cntx, inst_ui_purp purp) {
if (purp == inst_armed)
return inst_user_trig;
return inst_ok;
}
/*Determine whether a string is a number, if true, return number, else return -1*/
int isNumber(char *str){
int num = 0;
int i = 0;
while(str[i]){
if(str[i] >= '0' && str[i] <= '9'){
num = num * 10 + str[i] - '0';
}
else return -1;
i++;
}
return num;
}
/*Determine whether a string is a filename,if true return 1,else return 0*/
int isFile(char *str){
struct stat sb;
//check the return value of stat
if(stat(str, &sb) != -1 && S_ISREG(sb.st_mode) != 0){
return 1;
}
return 0;
}
/*
Flags used:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
upper ... . . .. . .. ...
lower . .... .. . .. ....
*/
void
usage(char *diag, ...) {
int i;
icompaths *icmps;
inst2_capability cap2 = 0;
fprintf(stderr,"Measure spot values, Version %s\n",ARGYLL_VERSION_STR);
fprintf(stderr,"Author: Graeme W. Gill, licensed under the GPL Version 2 or later\n");
if (diag != NULL) {
va_list args;
fprintf(stderr,"Diagnostic: ");
va_start(args, diag);
vfprintf(stderr, diag, args);
va_end(args);
fprintf(stderr,"\n");
}
fprintf(stderr,"usage: spotread [-options] [logfile]\n");
fprintf(stderr," -v Verbose mode\n");
fprintf(stderr," -s Print spectrum for each reading\n");
#ifndef SALONEINSTLIB
fprintf(stderr," -S Plot spectrum for each reading\n");
#endif /* !SALONEINSTLIB */
fprintf(stderr," -c listno Set instrument port from the following list (default %d)\n",COMPORT);
if ((icmps = new_icompaths(g_log)) != NULL) {
icompath **paths;
if ((paths = icmps->paths) != NULL) {
int i;
for (i = 0; ; i++) {
if (paths[i] == NULL)
break;
#if !defined(NOT_ALLINSTS) || defined(EN_SPYD2)
if ((paths[i]->dtype == instSpyder1 && setup_spyd2(0) == 0)
|| (paths[i]->dtype == instSpyder2 && setup_spyd2(1) == 0))
fprintf(stderr," %d = '%s' !! Disabled - no firmware !!\n",i+1,paths[i]->name);
else
#endif
fprintf(stderr," %d = '%s'\n",i+1,paths[i]->name);
}
} else
fprintf(stderr," ** No ports found **\n");
}
fprintf(stderr," -t Use transmission measurement mode\n");
fprintf(stderr," -e Use emissive measurement mode (absolute results)\n");
fprintf(stderr," -eb Use display white brightness relative measurement mode\n");
fprintf(stderr," -ew Use display white point relative chromatically adjusted mode\n");
fprintf(stderr," -p Use telephoto measurement mode (absolute results)\n");
fprintf(stderr," -pb Use projector white brightness relative measurement mode\n");
fprintf(stderr," -pw Use projector white point relative chromatically adjusted mode\n");
fprintf(stderr," -a Use ambient measurement mode (absolute results)\n");
fprintf(stderr," -f Use ambient flash measurement mode (absolute results)\n");
fprintf(stderr," -rw Use reflection white point relative chromatically adjusted mode\n");
cap2 = inst_show_disptype_options(stderr, " -y ", icmps, 0);
#ifndef SALONEINSTLIB
fprintf(stderr," -I illum Set simulated instrument illumination using FWA (def -i illum):\n");
fprintf(stderr," M0, M1, M2, A, C, D50, D50M2, D65, F5, F8, F10 or file.sp]\n");
#endif
fprintf(stderr," -i illum Choose illuminant for computation of CIE XYZ from spectral reflectance & FWA:\n");
#ifndef SALONEINSTLIB
fprintf(stderr," A, C, D50 (def.), D50M2, D65, F5, F8, F10 or file.sp\n");
#else
fprintf(stderr," A, C, D50 (def.), D65\n");
#endif
fprintf(stderr," -Q observ Choose CIE Observer for spectral data or CCSS instrument:\n");
#ifndef SALONEINSTLIB
fprintf(stderr," 1931_2 (def), 1964_10, 2012_2, 2012_10, S&B 1955_2, shaw, J&V 1978_2 or file.cmf\n");
#else
fprintf(stderr," 1931_2 (def), 1964_10, 2012_2, 2012_10 or file.cmf\n");
#endif
#ifndef SALONEINSTLIB
fprintf(stderr," (Choose FWA during operation)\n");
#endif
fprintf(stderr," -F filter Set filter configuration (if aplicable):\n");
fprintf(stderr," n None\n");
fprintf(stderr," p Polarising filter\n");
fprintf(stderr," 6 D65\n");
fprintf(stderr," u U.V. Cut\n");
fprintf(stderr," -E customfilter.sp Compensate for emission measurement filter\n");
fprintf(stderr," -A N|A|X|G XRGA conversion (default N)\n");
#ifndef SALONEINSTLIB
fprintf(stderr," -w Use -i param. illuminant for comuting L*a*b*\n");
#endif
fprintf(stderr," -x Display Yxy instead of Lab\n");
fprintf(stderr," -h Display LCh instead of Lab\n");
#ifndef SALONEINSTLIB
fprintf(stderr," -u Display Yuv instead of Lab\n");
#endif
fprintf(stderr," -V Show running average and std. devation from ref.\n");
#ifndef SALONEINSTLIB
fprintf(stderr," -T Display correlated color temperatures, CRI, TLCI & IES TM-30-15\n");
fprintf(stderr," -d Display density values\n");
#endif /* !SALONEINSTLIB */
// fprintf(stderr," -K type Run instrument calibration first\n");
fprintf(stderr," -N Disable auto calibration of instrument\n");
#ifndef SALONEINSTLIB
fprintf(stderr," -O [fname.sp] Do one cal. or measure and exit [save spectrum to file]\n");
#else
fprintf(stderr," -O Do one cal. or measure and exit\n");
#endif
fprintf(stderr," -H Start in high resolution spectrum mode (if available)\n");
if (cap2 & inst2_ccmx)
fprintf(stderr," -X file.ccmx Apply Colorimeter Correction Matrix\n");
if (cap2 & inst2_ccss) {
fprintf(stderr," -X file.ccss Use Colorimeter Calibration Spectral Samples for calibration\n");
}
#ifndef SALONEINSTLIB
fprintf(stderr," -R fname.sp Preset reference to spectrum\n");
#endif
fprintf(stderr," -Y r|n Override refresh, non-refresh display mode\n");
fprintf(stderr," -Y R:rate Override measured refresh rate with rate Hz\n");
fprintf(stderr," -Y A Use non-adaptive integration time mode (if available).\n");
fprintf(stderr," -Y l|L Test for i1Pro Lamp Drift (l), and remediate it (L)\n");
fprintf(stderr," -Y a Use Averaging mode (if available).\n");
// fprintf(stderr," -Y U Test i1pro2 UV measurement mode\n");
#ifndef SALONEINSTLIB
fprintf(stderr," -Y W:fname.sp Save white tile ref. spectrum to file\n");
#endif /* !SALONEINSTLIB */
fprintf(stderr," -W n|h|x Override serial port flow control: n = none, h = HW, x = Xon/Xoff\n");
fprintf(stderr," -D [level] Print debug diagnostics to stderr\n");
fprintf(stderr," logfile Optional file to save reading results as text\n");
if (icmps != NULL)
icmps->del(icmps);
exit(1);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
//check
if(nrhs < 1){
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs","at least one input required.");
usage("Usage requested");
}
// if(nlhs !=1){
// mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs","one outputs required.");
// }
//声明变量
// input matrix
//char command[256]={0};
//size of input matrix
//int ncols;
// output matrix
mxInt32 *output;
//读取输入数据
// buflen = (nrhs * 5) + 1;
// arr = mxCalloc(buflen, sizeof(char));
// inMatrix = mxGetString(prhs[0], arr, buflen);
//get dimensions of the input matrix
//ncols = mxGetM(prhs[0]);
//输出数据
plhs[0] = mxCreateNumericMatrix(1,1, mxINT32_CLASS, mxREAL);
//output = (int*)mxGetData(plhs[0]);
output = mxGetInt32s(plhs[0]);
int i, j;
int fa, nfa, mfa; /* current argument we're looking at */
int verb = 0;
int debug = 0;
int docalib = 0; /* Do a manual instrument calibration */
int nocal = 0; /* Disable auto calibration */
int doone = 0; /* 1 = Do one calibration or measure and exit */
/* 2 = + also save result to outspname */
int pspec = 0; /* 1 = Print out the spectrum for each reading */
/* 2 = Plot out the spectrum for each reading */
int refwr = 0; /* Reflection mode white relative mode */
int trans = 0; /* Use transmissioin mode */
int emiss = 0; /* 1 = Use emissive mode, 2 = display bright rel. */
/* 3 = display white rel. */
int tele = 0; /* 1 = Use telephoto emissive sub-mode. */
int ambient = 0; /* 1 = Use ambient emissive mode, 2 = ambient flash mode */
int highres = 0; /* Use high res mode if available */
int lampdrift = 0; /* i1Pro Lamp Drift test (1) & fix (2) */
int uvmode = 0; /* ~~~ i1pro2 test mode ~~~ */
xcalstd calstd = xcalstd_none; /* X-Rite calibration standard */
int refrmode = -1; /* -1 = default, 0 = non-refresh mode, 1 = refresh mode */
double refrate = 0.0; /* 0.0 = default, > 0.0 = override refresh rate */
int nadaptive = 0; /* Use non-apative mode if available */
int averagemode = 0; /* Use averaging mode if available */
int doYxy= 0; /* Display Yxy instead of Lab */
int doLCh= 0; /* Display LCh instead of Lab */
#ifndef SALONEINSTLIB
int doYuv= 0; /* Display Yuv instead of Lab */
#endif
int doCCT= 0; /* Display correlated color temperatures */
int doDensity= 0; /* Display density values */
inst_mode mode = 0, smode = 0; /* Normal mode and saved readings mode */
inst_opt_type trigmode = inst_opt_unknown; /* Chosen trigger mode */
inst_opt_filter fe = inst_opt_filter_unknown;
/* Filter configuration */
char outname[MAXNAMEL+1] = {0}; /* Output logfile name */
char ccxxname[MAXNAMEL+1] = {0}; /* Colorimeter Correction/Colorimeter Calibration name */
char filtername[MAXNAMEL+1] = {0}; /* Filter compensation */
char wtilename[MAXNAMEL+1] = {0}; /* White file spectrum */
char psetrefname[MAXNAMEL+1] = {0}; /* Preset reference spectrum */
char outspname[MAXNAMEL+1] = {0}; /* Save doone spectrum file */
FILE *fp = NULL; /* Logfile */
icompaths *icmps = NULL;
int comport = COMPORT; /* COM port used */
icompath *ipath = NULL;
int ditype = 0; /* Display type selection character(s) */
inst_mode cap = inst_mode_none; /* Instrument mode capabilities */
inst2_capability cap2 = inst2_none; /* Instrument capabilities 2 */
inst3_capability cap3 = inst3_none; /* Instrument capabilities 3 */
double lx, ly; /* Read location on xy table */
baud_rate br = baud_38400; /* Target baud rate */
flow_control fc = fc_nc; /* Default flow control */
inst *it; /* Instrument object */
inst_code rv;
int uswitch = 0; /* Instrument switch is enabled */
int spec = 0; /* Need spectral data for observer/illuminant flag */
int tillum_set = 0; /* User asked for custom target illuminant spectrum */
icxIllumeType tillum = icxIT_none; /* Target/simulated instrument illuminant */
xspect cust_tillum, *tillump = NULL; /* Custom target/simulated illumination spectrum */
int illum_set = 0; /* User asked for custom illuminant spectrum */
icxIllumeType illum = icxIT_D50; /* Spectral defaults */
xspect cust_illum; /* Custom illumination spectrum */
int labwpillum = 0; /* nz to use illum WP for L*a*b* conversion */
icmXYZNumber labwp = { icmD50_100.X, icmD50_100.Y, icmD50_100.Z }; /* Lab conversion wp */
char labwpname[100] = "D50"; /* Name of Lab conversion wp */
icxObserverType obType = icxOT_default; /* Default is 1931_2 */
xspect custObserver[3]; /* If obType = icxOT_custom */
xspect sp; /* Last spectrum read (fwa adjusted) */
xspect rsp; /* Reference spectrum (fwa adjusted) */
xsp2cie *sp2cie = NULL; /* default conversion */
xsp2cie *sp2cief[26]; /* FWA corrected conversions */
double wXYZ[3] = { -10.0, 0, 0 };/* White XYZ for display white relative */
double chmat[3][3]; /* Chromatic adapation matrix for white point relative */
double XYZ[3] = { 0.0, 0.0, 0.0 }; /* Last XYZ scaled 0..100 or absolute */
double Lab[3] = { -10.0, 0, 0}; /* Last Lab */
double rXYZ[3] = { 0.0, -10.0, 0}; /* Reference XYZ */
double rLab[3] = { -10.0, 0, 0}; /* Reference Lab */
double Yxy[3] = { 0.0, 0, 0}; /* Yxy value */
double LCh[3] = { 0.0, 0, 0}; /* LCh value */
#ifndef SALONEINSTLIB
double Yuv[3] = { 0.0, 0, 0}; /* Yuv value */
#endif
double refstats = 0; /* Print running avg & stddev against ref */
double rstat_n; /* Stats N */
double rstat_XYZ[3]; /* Stats sum of XYZ's */
double rstat_XYZsq[3]; /* Stats sum of XYZ's squared */
double rstat_Lab[3]; /* Stats sum of Lab's */
double rstat_Labsq[3]; /* Stats sum of Lab's squared */
int savdrd = 0; /* At least one saved reading is available */
int ix; /* Reading index number */
int loghead = 0; /* NZ if log file heading has been printed */
set_exe_path("./spotread"); /* Set global exe_path and error_program */
check_if_not_interactive();
for (i = 0; i < 26; i++)
sp2cief[i] = NULL;
sp.spec_n = 0;
rsp.spec_n = 0;
/*New Process arguments*/
//read in all parameters
char command_line[100][COMMAND_LENGTH] = {0};
int num_param = 0;
for(int ii = 0;ii < nrhs - 1;ii++){
char* command;
mwSize command_size;
int length = mxGetN(prhs[ii]);
if(length > COMMAND_LENGTH){
printf("The length of parameter is too long\n");
//todo
break;
}
command_size = length*sizeof(mxChar);
command = (char*)mxMalloc(command_size);
command = mxArrayToString(prhs[ii]);
strcpy(command_line[ii], command);
mxFree(command);
num_param++;
}
char* command;
mwSize command_size;
command_size = mxGetN(prhs[nrhs - 1])*sizeof(mxChar);
command = (char*)mxMalloc(command_size);
command = mxArrayToString(prhs[nrhs - 1]);
//以@开头的表示logfile
if(command[0] == '@'){
strcpy(outname, command + 1);
if((fp = fopen(outname, "w")) == NULL){
error("Unable to open logfile '%s' for writing\n", outname);
}
}else{
//否则就是普通参数
strcpy(command_line[nrhs - 1], command);
num_param++;
}
mxFree(command);
//process parameter
int k = 0;
while(k < num_param){
char command[COMMAND_LENGTH];
strcpy(command, command_line[k]);
//original param 'D'
if(stricmp(command, "printDebug") == 0){
debug = 1;
char next_param[COMMAND_LENGTH];
strcpy(next_param, command_line[k + 1]);
int num = isNumber(next_param);
if(num >= 0 && num <= 9){
debug = num;
k++;
}else{
printf("wrong level for print debug diagnostics");
}
g_log->debug = debug;
//original param '?'
}else if(stricmp(command, "?") == 0){
usage("Usage requested");
//original param 'v'
}else if(stricmp(command, "verboseMode") == 0){
verb = 1;
g_log->verb = verb;
//original param 's' or 'S', default 's',
}else if(stricmp(command, "showSpectrum") == 0){
//default print spectrum for each reading
pspec = 1;
char next_param[COMMAND_LENGTH];
strcpy(next_param, command_line[k + 1]);
if(strcmp(next_param, "1") == 0){
pspec = 1;
//plot spectrum for each reading
}else if(strcmp(next_param, "2") == 0){
pspec = 2;
}
//COM port, original parameter 'c'
}else if(stricmp(command, "comPort") == 0){
char next_param[COMMAND_LENGTH];
strcpy(next_param, command_line[k + 1]);
int num = isNumber(next_param);
if(num < 1 || num > 40){
printf("no parameter or parameter out of range\n");
}else{
comport = num;
}
//Display type,original parameter 'y'
}else if(stricmp(command, "displayType") == 0){
char next_param[COMMAND_LENGTH];
strcpy(next_param, command_line[k + 1]);
int num = isNumber(next_param);
if(num < 0){
printf("No parameter\n");
}
ditype = num;
//todo
}
#ifndef SALONEINSTLIB
//Simulated instrument illumination (FWA),original param 'I'
else if(stricmp(command, "setSimulatedInst") == 0){
tillum_set = spec = 1;
char next_param[COMMAND_LENGTH];
strcpy(next_param, command_line[k + 1]);
if(stricmp(next_param, "A") == 0 || stricmp(next_param, "M0") == 0){
tillum = icxIT_A;
}else if(stricmp(next_param, "C") == 0){
tillum = icxIT_C;
}else if(stricmp(next_param, "D50") == 0 || stricmp(next_param, "M1") == 0){
tillum = icxIT_D50;
}else if(stricmp(next_param, "D50M2") == 0 || stricmp(next_param, "M2") == 0){
tillum = icxIT_D50M2;
}else if(stricmp(next_param, "D65") == 0){
tillum = icxIT_D65;
}else if(stricmp(next_param, "F5") == 0){
tillum = icxIT_F8;
}else if(stricmp(next_param, "F8") == 0){
tillum = icxIT_F8;
}else if(stricmp(next_param, "F10") == 0){
tillum = icxIT_F10;
}else{
//Assume it's a filename
inst_meas_type mt;
tillum = icxIT_custom;
if(read_xspect(&cust_tillum, &mt, next_param) != 0){
usage("Failed to read custom target illuminant spectrum in file '%s'", next_param);
}
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",next_param);
}
}
#endif /*!SALONEINSTLIB*/
//Spectral Illuminant type for XYZ computation,original parameter 'i'
else if(stricmp(command, "spectralIlluminantType") == 0){
illum_set = spec = 1;
char next_param[COMMAND_LENGTH];
strcpy(next_param, command_line[k + 1]);
if(stricmp(next_param, "A") == 0){
illum = icxIT_A;
}else if(stricmp(next_param, "C") == 0){
illum = icxIT_C;
}else if(stricmp(next_param, "D50") == 0){
illum = icxIT_D50;
}else if(stricmp(next_param, "D50M2") == 0){
illum = icxIT_D50M2;
}else if(stricmp(next_param, "D65") == 0){
illum = icxIT_D65;
}
#ifndef SALONEINSTLIB
else if(stricmp(next_param, "F5") == 0){
illum = icxIT_F5;
}else if(stricmp(next_param, "F8") == 0){
illum = icxIT_F8;
}else if(stricmp(next_param, "F10") == 0){
illum = icxIT_F10;
}else{
//Assume it's a filename
inst_meas_type mt;
tillum = icxIT_custom;
if(read_xspect(&cust_tillum, &mt, next_param) != 0){
usage("Unable to read custom illuminant file '%s'", next_param);
}
if (mt != inst_mrt_none
&& mt != inst_mrt_emission
&& mt != inst_mrt_ambient
&& mt != inst_mrt_emission_flash
&& mt != inst_mrt_ambient_flash)
error("Custom illuminant '%s' is wrong measurement type",next_param);
}
#else /*SALONEINSTLIB*/
else usage("Unrecognised illuminant '%s'", next_param);
#endif
}
//Use -i illuminant for L*a*b conversion,original parameter 'w'
else if(stricmp(command, "useIParam") == 0){
labwpillum = 1;
//Spectral Observer type,original param 'Q'
}else if(stricmp(command, "chooseCIEObserver") == 0){
char next_param[COMMAND_LENGTH];
strcpy(next_param, command_line[k + 1]);
if (strcmp(next_param, "1931_2") == 0) { /* Classic 2 degree */
obType = icxOT_CIE_1931_2;
} else if (strcmp(next_param, "1964_10") == 0) { /* Classic 10 degree */
obType = icxOT_CIE_1964_10;
} else if (strcmp(next_param, "2012_2") == 0) { /* Latest 2 degree */
obType = icxOT_CIE_2012_2;
} else if (strcmp(next_param, "2012_10") == 0) { /* Latest 10 degree */
obType = icxOT_CIE_2012_10;
}
#ifndef SALONEINSTLIB
else if (strcmp(next_param, "1955_2") == 0) { /* Stiles and Burch 1955 2 degree */
obType = icxOT_Stiles_Burch_2;
} else if (strcmp(next_param, "1978_2") == 0) { /* Judd and Voss 1978 2 degree */
obType = icxOT_Judd_Voss_2;
} else if (strcmp(next_param, "shaw") == 0) { /* Shaw and Fairchilds 1997 2 degree */
obType = icxOT_Shaw_Fairchild_2;
}
#endif /* !SALONEINSTLIB */
else {
obType = icxOT_custom;
if (read_cmf(custObserver, next_param) != 0)
usage(0,"Failed to read custom observer CMF from -Q file '%s'",next_param);
}
//original param 'e'=0,'eb'=1,'ew'=2,'t'=3,'p'=4,'pb'=5,'pw'=6,'a'=7,'f'=8,'r'=9,'rw'=10
}else if(stricmp(command, "measureMode") == 0){
//printf("measureMode\n");
char next_param[COMMAND_LENGTH];
strcpy(next_param, command_line[k + 1]);
int num = isNumber(next_param);
//printf("%d\n", num);
if(num < 0 || num > 10){
printf("wrong number for measureMode,please input from 0 to 10");
break;
}else if(num == 0){
emiss = 1, trans = 0, tele = 0, ambient = 0;
}else if(num == 1){
emiss = 2, trans = 0, tele = 0, ambient = 0;
}else if(num == 2){
emiss = 3, trans = 0, tele = 0, ambient = 0;
}else if(num == 3){
emiss = 0, trans = 1, tele = 0, ambient = 0;
}else if(num == 4){
emiss = 1, trans = 0, tele = 1, ambient = 0;
}else if(num == 5){
emiss = 2, trans = 0, tele = 1, ambient = 0;
}else if(num == 6){
emiss = 3, trans = 0, tele = 1, ambient = 0;
}else if(num == 7){
if(trans) ambient = 1;
else{
emiss = 1, trans = 0, tele = 0, ambient = 1;
}
}else if(num == 8){
emiss = 1, trans = 0, tele = 0, ambient = 2;
}else if(num == 9){
emiss = 0, trans = 0, tele = 0, ambient = 0;
}else if(num == 10){
emiss = 0, trans = 0, tele = 0, ambient = 0, refwr = 1;
}
k++;
//Filter configuration,original parameter 'F n' = 0, 'F p' = 1, 'F 6' = 2, 'F u' = 3
}else if(stricmp(command, "filterConfig") == 0){
char next_param[COMMAND_LENGTH];
strcpy(next_param, command_line[k + 1]);
int num = isNumber(next_param);
if(num < 0 || num > 3){
printf("No parameter or no parameter recognised");
break;
}else if(num == 0){
fe = inst_opt_filter_none;
}else if(num == 1){
fe = inst_opt_filter_pol;
}else if(num == 2){
fe = inst_opt_filter_D65;
}else if(num == 3){
fe = inst_opt_filter_UVCut;
}
k++;
//Extra filter compensation file,original parameter 'E'
}else if(stricmp(command, "E") == 0){
char next_param[COMMAND_LENGTH];
strcpy(next_param, command_line[k + 1]);
if(isFile(next_param)){
strcpy(filtername, next_param);
k++;
}else{
usage("Parameter expected following -E");
}
//XRGA conversion,original parameter 'A'
}else if(stricmp(command, "xrgaConversion") == 0){
char next_param[COMMAND_LENGTH];