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
|
/*
* Argyll Color Correction System
* Colorimeter Correction Matrix
*
* Author: Graeme W. Gill
* Date: 9/8/2010
*
* Copyright 2010 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.
*/
/*
* TTBD:
*/
#undef DEBUG
#define verbo stdout
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <time.h>
#ifndef SALONEINSTLIB
#include "numlib.h"
#include "icc.h"
#else
#include "numsup.h"
#include "conv.h"
#endif
#include "cgats.h"
#include "xspect.h"
#include "ccss.h"
#ifdef NT /* You'd think there might be some standards.... */
# ifndef __BORLANDC__
# define stricmp _stricmp
# endif
#else
# define stricmp strcasecmp
#endif
/* Forward declarations */
static void free_ccss(ccss *p);
/* Utilities */
/* Method implimentations */
/* Write out the ccss to a CGATS format object */
/* Return nz on error */
static int create_ccss_cgats(
ccss *p, /* This */
cgats **pocg /* return CGATS structure */
) {
int i, j;
time_t clk = time(0);
struct tm *tsp = localtime(&clk);
char *atm = asctime(tsp); /* Ascii time */
cgats *ocg; /* CGATS structure */
int nsetel = 0;
cgats_set_elem *setel; /* Array of set value elements */
char buf[100];
atm[strlen(atm)-1] = '\000'; /* Remove \n from end */
/* Setup output cgats file */
ocg = new_cgats(); /* Create a CGATS structure */
ocg->add_other(ocg, "CCSS"); /* Type is Colorimeter Calibration Spectral Set */
ocg->add_table(ocg, tt_other, 0); /* Start the first table */
if (p->desc != NULL)
ocg->add_kword(ocg, 0, "DESCRIPTOR", p->desc,NULL);
if (p->orig != NULL)
ocg->add_kword(ocg, 0, "ORIGINATOR", p->orig, NULL);
else
ocg->add_kword(ocg, 0, "ORIGINATOR", "Argyll ccss", NULL);
if (p->crdate != NULL)
ocg->add_kword(ocg, 0, "CREATED",p->crdate, NULL);
else
ocg->add_kword(ocg, 0, "CREATED",atm, NULL);
if (p->disp)
ocg->add_kword(ocg, 0, "DISPLAY",p->disp, NULL);
if (p->tech)
ocg->add_kword(ocg, 0, "TECHNOLOGY", p->tech,NULL);
if (p->disp == NULL && p->tech == NULL) {
sprintf(p->err, "write_ccss: ccss doesn't contain display or techology strings");
ocg->del(ocg);
return 1;
}
if (p->refrmode >= 0)
ocg->add_kword(ocg, 0, "DISPLAY_TYPE_REFRESH", p->refrmode ? "YES" : "NO", NULL);
if (p->sel != NULL)
ocg->add_kword(ocg, 0, "UI_SELECTORS", p->sel, NULL);
if (p->ref != NULL)
ocg->add_kword(ocg, 0, "REFERENCE",p->ref, NULL);
sprintf(buf,"%d", p->samples[0].spec_n);
ocg->add_kword(ocg, 0, "SPECTRAL_BANDS",buf, NULL);
sprintf(buf,"%f", p->samples[0].spec_wl_short);
ocg->add_kword(ocg, 0, "SPECTRAL_START_NM",buf, NULL);
sprintf(buf,"%f", p->samples[0].spec_wl_long);
ocg->add_kword(ocg, 0, "SPECTRAL_END_NM",buf, NULL);
sprintf(buf,"%f", p->samples[0].norm);
ocg->add_kword(ocg, 0, "SPECTRAL_NORM",buf, NULL);
/* Fields we want */
if (ocg->add_field(ocg, 0, "SAMPLE_ID", nqcs_t) < 0) {
sprintf(p->err, "cgats add_field SAMPLE_ID failed with '%s'!",ocg->err);
ocg->del(ocg); /* Clean up */
return 2;
}
nsetel += 1; /* For id */
/* Generate fields for spectral values */
for (i = 0; i < p->samples[0].spec_n; i++) {
int nm;
/* Compute nearest integer wavelength */
nm = (int)(p->samples[0].spec_wl_short + ((double)i/(p->samples[0].spec_n-1.0))
* (p->samples[0].spec_wl_long - p->samples[0].spec_wl_short) + 0.5);
sprintf(buf,"SPEC_%03d",nm);
if (ocg->add_field(ocg, 0, buf, r_t) < 0) {
sprintf(p->err, "cgats add_field %s failed with '%s'",buf,ocg->err);
ocg->del(ocg); /* Clean up */
return 2;
}
}
nsetel += p->samples[0].spec_n; /* Spectral values */
if ((setel = (cgats_set_elem *)malloc(sizeof(cgats_set_elem) * nsetel)) == NULL) {
strcpy(p->err, "Malloc failed!");
ocg->del(ocg); /* Clean up */
return 2;
}
/* Write out the patch info to the output CGATS file */
for (i = 0; i < p->no_samp; i++) {
int k = 0;
sprintf(buf,"%d",i+1);
setel[k++].c = buf;
for (j = 0; j < p->samples[i].spec_n; j++)
setel[k++].d = p->samples[i].spec[j];
ocg->add_setarr(ocg, 0, setel);
}
free(setel);
if (pocg != NULL)
*pocg = ocg;
return 0;
}
/* Write out the ccss to a CGATS format .ccss file */
/* Return nz on error */
static int write_ccss(
ccss *p, /* This */
char *outname /* Filename to write to */
) {
int rv;
cgats *ocg; /* CGATS structure */
if (p->no_samp < 3) {
strcpy(p->err, "Need at least three spectral samples");
return 1;
}
/* Create CGATS elements */
if ((rv = create_ccss_cgats(p, &ocg)) != 0) {
return rv;
}
/* Write it to file */
if (ocg->write_name(ocg, outname)) {
strcpy(p->err, ocg->err);
ocg->del(ocg); /* Clean up */
return 1;
}
ocg->del(ocg); /* Clean up */
return 0;
}
/* write to a CGATS .ccss file to a memory buffer. */
/* return nz on error, with message in err[] */
static int buf_write_ccss(
ccss *p,
unsigned char **buf, /* Return allocated buffer */
int *len /* Return length */
) {
int rv;
cgats *ocg; /* CGATS structure */
cgatsFile *fp;
if (p->no_samp < 3) {
strcpy(p->err, "Need at least three spectral samples");
return 1;
}
/* Create CGATS elements */
if ((rv = create_ccss_cgats(p, &ocg)) != 0) {
return rv;
}
if ((fp = new_cgatsFileMem(NULL, 0)) == NULL) {
strcpy(p->err, "new_cgatsFileMem failed");
return 2;
}
/* Write it to file */
if (ocg->write(ocg, fp)) {
strcpy(p->err, ocg->err);
ocg->del(ocg); /* Clean up */
fp->del(fp);
return 1;
}
/* Get the buffer the ccss has been written to */
if (fp->get_buf(fp, buf, (size_t *)len)) {
strcpy(p->err, "cgatsFileMem get_buf failed");
return 2;
}
ocg->del(ocg); /* Clean up */
fp->del(fp);
return 0;
}
/* Read in the ccss CGATS .ccss file from cgats */
/* Return nz on error */
static int read_ccss_cgats(
ccss *p, /* This */
cgats *icg /* input cgats structure */
) {
int i, j;
int ti, ii; /* Temporary CGATs index */
int spi[XSPECT_MAX_BANDS]; /* CGATS indexes for each wavelength */
xspect sp;
if (icg->ntables == 0 || icg->t[0].tt != tt_other || icg->t[0].oi != 0) {
sprintf(p->err, "read_ccss: Input file isn't a CCSS format file");
icg->del(icg);
return 1;
}
if (icg->ntables != 1) {
sprintf(p->err, "Input file doesn't contain exactly one table");
icg->del(icg);
return 1;
}
free_ccss(p);
if ((ti = icg->find_kword(icg, 0, "DESCRIPTOR")) >= 0) {
if ((p->desc = strdup(icg->t[0].kdata[ti])) == NULL) {
sprintf(p->err, "read_ccss: malloc failed");
icg->del(icg);
return 2;
}
}
if ((ti = icg->find_kword(icg, 0, "ORIGINATOR")) >= 0) {
if ((p->orig = strdup(icg->t[0].kdata[ti])) == NULL) {
sprintf(p->err, "read_ccss: malloc failed");
icg->del(icg);
return 2;
}
}
if ((ti = icg->find_kword(icg, 0, "CREATED")) >= 0) {
if ((p->crdate = strdup(icg->t[0].kdata[ti])) == NULL) {
sprintf(p->err, "read_ccss: malloc failed");
icg->del(icg);
return 2;
}
}
if ((ti = icg->find_kword(icg, 0, "DISPLAY")) >= 0) {
if ((p->disp = strdup(icg->t[0].kdata[ti])) == NULL) {
sprintf(p->err, "read_ccss: malloc failed");
icg->del(icg);
return 2;
}
}
if ((ti = icg->find_kword(icg, 0, "TECHNOLOGY")) >= 0) {
if ((p->tech = strdup(icg->t[0].kdata[ti])) == NULL) {
sprintf(p->err, "read_ccss: malloc failed");
icg->del(icg);
return 2;
}
}
if (p->disp == NULL && p->tech == NULL) {
sprintf(p->err, "read_ccss: Input file doesn't contain keyword DISPLAY or TECHNOLOGY");
icg->del(icg);
return 1;
}
if ((ti = icg->find_kword(icg, 0, "DISPLAY_TYPE_REFRESH")) >= 0) {
if (stricmp(icg->t[0].kdata[ti], "YES") == 0)
p->refrmode = 1;
else if (stricmp(icg->t[0].kdata[ti], "NO") == 0)
p->refrmode = 0;
}
if ((ti = icg->find_kword(icg, 0, "UI_SELECTORS")) >= 0) {
if ((p->sel = strdup(icg->t[0].kdata[ti])) == NULL) {
sprintf(p->err, "read_ccss: malloc failed");
icg->del(icg);
return 2;
}
}
if ((ti = icg->find_kword(icg, 0, "REFERENCE")) >= 0) {
if ((p->ref = strdup(icg->t[0].kdata[ti])) == NULL) {
sprintf(p->err, "read_ccss: malloc failed");
icg->del(icg);
return 2;
}
}
if ((ii = icg->find_kword(icg, 0, "SPECTRAL_BANDS")) < 0) {
sprintf(p->err,"Input file doesn't contain keyword SPECTRAL_BANDS");
icg->del(icg);
return 1;
}
sp.spec_n = atoi(icg->t[0].kdata[ii]);
if ((ii = icg->find_kword(icg, 0, "SPECTRAL_START_NM")) < 0) {
sprintf(p->err,"Input file doesn't contain keyword SPECTRAL_START_NM");
icg->del(icg);
return 1;
}
sp.spec_wl_short = atof(icg->t[0].kdata[ii]);
if ((ii = icg->find_kword(icg, 0, "SPECTRAL_END_NM")) < 0) {
sprintf(p->err,"Input file doesn't contain keyword SPECTRAL_END_NM");
icg->del(icg);
return 1;
}
sp.spec_wl_long = atof(icg->t[0].kdata[ii]);
if ((ii = icg->find_kword(icg, 0, "SPECTRAL_NORM")) < 0) {
sp.norm = 1.0; /* Older versions don't have SPECTRAL_NORM */
} else {
sp.norm = atof(icg->t[0].kdata[ii]);
}
/* Find the fields for spectral values */
for (j = 0; j < sp.spec_n; j++) {
char buf[100];
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) {
sprintf(p->err,"Input file doesn't contain field %s",buf);
icg->del(icg);
return 1;
}
}
if ((p->no_samp = icg->t[0].nsets) < 3) {
sprintf(p->err, "Input file doesn't contain at least three spectral samples");
p->no_samp = 0;
icg->del(icg); /* Clean up */
return 1;
}
/* Allocate spectral array */
if ((p->samples = (xspect *)malloc(sizeof(xspect) * p->no_samp)) == NULL) {
strcpy(p->err, "Malloc failed!");
p->no_samp = 0;
icg->del(icg); /* Clean up */
return 2;
}
/* Fill sample array with the data */
for (i = 0; i < p->no_samp; i++) {
XSPECT_COPY_INFO(&p->samples[i], &sp);
/* Read the spectral values for this patch */
for (j = 0; j < sp.spec_n; j++) {
p->samples[i].spec[j] = *((double *)icg->t[0].fdata[i][spi[j]]);
}
}
return 0;
}
/* Read in the ccss CGATS .ccss file */
/* Return nz on error */
static int read_ccss(
ccss *p, /* This */
char *inname /* Filename to read from */
) {
int rv;
cgats *icg; /* input cgats structure */
/* Open and look at the .ccss file */
if ((icg = new_cgats()) == NULL) { /* Create a CGATS structure */
sprintf(p->err, "read_ccss: new_cgats() failed");
return 2;
}
icg->add_other(icg, "CCSS"); /* our special type is Model Printer Profile */
if (icg->read_name(icg, inname)) {
strcpy(p->err, icg->err);
icg->del(icg);
return 1;
}
if ((rv = read_ccss_cgats(p, icg)) != 0) {
icg->del(icg); /* Clean up */
return rv;
}
icg->del(icg); /* Clean up */
return 0;
}
/* Read in the ccss CGATS .ccss file from a memory buffer */
/* Return nz on error */
static int buf_read_ccss(
ccss *p, /* This */
unsigned char *buf,
int len
) {
int rv;
cgatsFile *fp;
cgats *icg; /* input cgats structure */
if ((fp = new_cgatsFileMem(buf, len)) == NULL) {
strcpy(p->err, "new_cgatsFileMem failed");
return 2;
}
/* Open and look at the .ccss file */
if ((icg = new_cgats()) == NULL) { /* Create a CGATS structure */
sprintf(p->err, "read_ccss: new_cgats() failed");
fp->del(fp);
return 2;
}
icg->add_other(icg, "CCSS"); /* our special type is Model Printer Profile */
if (icg->read(icg, fp)) {
strcpy(p->err, icg->err);
icg->del(icg);
fp->del(fp);
return 1;
}
fp->del(fp);
if ((rv = read_ccss_cgats(p, icg)) != 0) {
icg->del(icg); /* Clean up */
return rv;
}
icg->del(icg); /* Clean up */
return 0;
}
/* Set the contents of the ccss. return nz on error. */
/* (Copy all the values) */
static int set_ccss(ccss *p,
char *orig, /* Originator (May be NULL) */
char *crdate, /* Creation date in ctime() format (May be NULL) */
char *desc, /* General description (optional) */
char *disp, /* Display make and model (optional if tech) */
char *tech, /* Display technology description (optional if disp) */
int refrmode, /* Display refresh mode, -1 = unknown, 0 = n, 1 = yes */
char *sel, /* UI selector characters - NULL for none */
char *refd, /* Reference spectrometer description (optional) */
xspect *samples, /* Arry of spectral samples. All assumed to be same dim as first */
int no_samp /* Number of spectral samples */
) {
int i;
free_ccss(p);
if (orig != NULL) {
if ((p->orig = strdup(orig)) == NULL) {
sprintf(p->err, "set_ccss: malloc orig failed");
return 2;
}
}
if (desc != NULL) {
if ((p->desc = strdup(desc)) == NULL) {
sprintf(p->err, "set_ccss: malloc desc failed");
return 2;
}
}
if (crdate != NULL) {
if ((p->crdate = strdup(crdate)) == NULL) {
sprintf(p->err, "set_ccss: malloc crdate failed");
return 2;
}
}
if (disp != NULL) {
if ((p->disp = strdup(disp)) == NULL) {
sprintf(p->err, "set_ccss: malloc disp failed");
return 2;
}
}
if (tech != NULL) {
if ((p->tech = strdup(tech)) == NULL) {
sprintf(p->err, "set_ccss: malloc tech failed");
return 2;
}
}
p->refrmode = refrmode;
if (sel != NULL) {
if ((p->sel = strdup(sel)) == NULL) {
sprintf(p->err, "set_ccss: malloc sel failed");
return 2;
}
}
if (refd != NULL) {
if ((p->ref = strdup(refd)) == NULL) {
sprintf(p->err, "set_ccss: malloc ref failed");
return 2;
}
}
if (p->samples != NULL) {
free(p->samples);
p->samples = NULL;
}
if ((p->no_samp = no_samp) < 3) {
strcpy(p->err, "Must be at least three spectral samples");
p->no_samp = 0;
return 1;
}
/* Allocate spectral array */
if ((p->samples = (xspect *)malloc(sizeof(xspect) * p->no_samp)) == NULL) {
strcpy(p->err, "Malloc failed!");
p->no_samp = 0;
return 2;
}
/* And copy all the values */
for (i = 0; i < p->no_samp; i++) {
p->samples[i] = samples[i];
}
return 0;
}
/* Free the contents */
static void free_ccss(ccss *p) {
if (p != NULL) {
if (p->desc != NULL)
free(p->desc);
p->desc = NULL;
if (p->orig != NULL)
free(p->orig);
p->orig = NULL;
if (p->crdate != NULL)
free(p->crdate);
p->crdate = NULL;
if (p->disp != NULL)
free(p->disp);
p->disp = NULL;
if (p->tech != NULL)
free(p->tech);
p->tech = NULL;
if (p->sel != NULL)
free(p->sel);
p->sel = NULL;
if (p->ref != NULL)
free(p->ref);
p->ref = NULL;
if (p->samples != NULL)
free(p->samples);
p->samples = NULL;
p->no_samp = 0;
}
}
/* Delete it */
static void del_ccss(ccss *p) {
if (p != NULL) {
free_ccss(p);
free(p);
}
}
/* Allocate a new, uninitialised ccss */
/* Note thate black and white points aren't allocated */
ccss *new_ccss(void) {
ccss *p;
if ((p = (ccss *)calloc(1, sizeof(ccss))) == NULL)
return NULL;
/* Init method pointers */
p->del = del_ccss;
p->set_ccss = set_ccss;
p->write_ccss = write_ccss;
p->buf_write_ccss = buf_write_ccss;
p->read_ccss = read_ccss;
p->buf_read_ccss = buf_read_ccss;
return p;
}
|