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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
|
//---------------------------------------------------------------------------------
//
// Little Color Management System
// Copyright (c) 1998-2010 Marti Maria Saguer
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//---------------------------------------------------------------------------------
//
#include "lcms2_internal.h"
// Generic I/O, tag dictionary management, profile struct
// IOhandlers are abstractions used by littleCMS to read from whatever file, stream,
// memory block or any storage. Each IOhandler provides implementations for read,
// write, seek and tell functions. LittleCMS code deals with IO across those objects.
// In this way, is easier to add support for new storage media.
// NULL stream, for taking care of used space -------------------------------------
// NULL IOhandler basically does nothing but keep track on how many bytes have been
// written. This is handy when creating profiles, where the file size is needed in the
// header. Then, whole profile is serialized across NULL IOhandler and a second pass
// writes the bytes to the pertinent IOhandler.
typedef struct {
cmsUInt32Number Pointer; // Points to current location
} FILENULL;
static
cmsUInt32Number NULLRead(cmsIOHANDLER* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count)
{
FILENULL* ResData = (FILENULL*) iohandler ->stream;
cmsUInt32Number len = size * count;
ResData -> Pointer += len;
return count;
cmsUNUSED_PARAMETER(Buffer);
}
static
cmsBool NULLSeek(cmsIOHANDLER* iohandler, cmsUInt32Number offset)
{
FILENULL* ResData = (FILENULL*) iohandler ->stream;
ResData ->Pointer = offset;
return TRUE;
}
static
cmsUInt32Number NULLTell(cmsIOHANDLER* iohandler)
{
FILENULL* ResData = (FILENULL*) iohandler ->stream;
return ResData -> Pointer;
}
static
cmsBool NULLWrite(cmsIOHANDLER* iohandler, cmsUInt32Number size, const void *Ptr)
{
FILENULL* ResData = (FILENULL*) iohandler ->stream;
ResData ->Pointer += size;
if (ResData ->Pointer > iohandler->UsedSpace)
iohandler->UsedSpace = ResData ->Pointer;
return TRUE;
cmsUNUSED_PARAMETER(Ptr);
}
static
cmsBool NULLClose(cmsIOHANDLER* iohandler)
{
FILENULL* ResData = (FILENULL*) iohandler ->stream;
_cmsFree(iohandler ->ContextID, ResData);
_cmsFree(iohandler ->ContextID, iohandler);
return TRUE;
}
// The NULL IOhandler creator
cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromNULL(cmsContext ContextID)
{
struct _cms_io_handler* iohandler = NULL;
FILENULL* fm = NULL;
iohandler = (struct _cms_io_handler*) _cmsMallocZero(ContextID, sizeof(struct _cms_io_handler));
if (iohandler == NULL) return NULL;
fm = (FILENULL*) _cmsMallocZero(ContextID, sizeof(FILENULL));
if (fm == NULL) goto Error;
fm ->Pointer = 0;
iohandler ->ContextID = ContextID;
iohandler ->stream = (void*) fm;
iohandler ->UsedSpace = 0;
iohandler ->PhysicalFile[0] = 0;
iohandler ->Read = NULLRead;
iohandler ->Seek = NULLSeek;
iohandler ->Close = NULLClose;
iohandler ->Tell = NULLTell;
iohandler ->Write = NULLWrite;
return iohandler;
Error:
if (fm) _cmsFree(ContextID, fm);
if (iohandler) _cmsFree(ContextID, iohandler);
return NULL;
}
// Memory-based stream --------------------------------------------------------------
// Those functions implements an iohandler which takes a block of memory as storage medium.
typedef struct {
cmsUInt8Number* Block; // Points to allocated memory
cmsUInt32Number Size; // Size of allocated memory
cmsUInt32Number Pointer; // Points to current location
int FreeBlockOnClose; // As title
} FILEMEM;
static
cmsUInt32Number MemoryRead(struct _cms_io_handler* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count)
{
FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
cmsUInt8Number* Ptr;
cmsUInt32Number len = size * count;
if (ResData -> Pointer + len > ResData -> Size){
len = (ResData -> Size - ResData -> Pointer);
cmsSignalError(iohandler ->ContextID, cmsERROR_READ, "Read from memory error. Got %d bytes, block should be of %d bytes", len, count * size);
return 0;
}
Ptr = ResData -> Block;
Ptr += ResData -> Pointer;
memmove(Buffer, Ptr, len);
ResData -> Pointer += len;
return count;
}
// SEEK_CUR is assumed
static
cmsBool MemorySeek(struct _cms_io_handler* iohandler, cmsUInt32Number offset)
{
FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
if (offset > ResData ->Size) {
cmsSignalError(iohandler ->ContextID, cmsERROR_SEEK, "Too few data; probably corrupted profile");
return FALSE;
}
ResData ->Pointer = offset;
return TRUE;
}
// Tell for memory
static
cmsUInt32Number MemoryTell(struct _cms_io_handler* iohandler)
{
FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
if (ResData == NULL) return 0;
return ResData -> Pointer;
}
// Writes data to memory, also keeps used space for further reference.
static
cmsBool MemoryWrite(struct _cms_io_handler* iohandler, cmsUInt32Number size, const void *Ptr)
{
FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
if (ResData == NULL) return FALSE; // Housekeeping
if (size == 0) return TRUE; // Write zero bytes is ok, but does nothing
memmove(ResData ->Block + ResData ->Pointer, Ptr, size);
ResData ->Pointer += size;
if (ResData ->Pointer > iohandler->UsedSpace)
iohandler->UsedSpace = ResData ->Pointer;
iohandler->UsedSpace += size;
return TRUE;
}
static
cmsBool MemoryClose(struct _cms_io_handler* iohandler)
{
FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
if (ResData ->FreeBlockOnClose) {
if (ResData ->Block) _cmsFree(iohandler ->ContextID, ResData ->Block);
}
_cmsFree(iohandler ->ContextID, ResData);
_cmsFree(iohandler ->ContextID, iohandler);
return TRUE;
}
// Create a iohandler for memory block. AccessMode=='r' assumes the iohandler is going to read, and makes
// a copy of the memory block for letting user to free the memory after invoking open profile. In write
// mode ("w"), Buffere points to the begin of memory block to be written.
cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromMem(cmsContext ContextID, void *Buffer, cmsUInt32Number size, const char* AccessMode)
{
cmsIOHANDLER* iohandler = NULL;
FILEMEM* fm = NULL;
_cmsAssert(AccessMode != NULL);
iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
if (iohandler == NULL) return NULL;
switch (*AccessMode) {
case 'r':
fm = (FILEMEM*) _cmsMallocZero(ContextID, sizeof(FILEMEM));
if (fm == NULL) goto Error;
if (Buffer == NULL) {
cmsSignalError(ContextID, cmsERROR_READ, "Couldn't read profile from NULL pointer");
goto Error;
}
fm ->Block = (cmsUInt8Number*) _cmsMalloc(ContextID, size);
if (fm ->Block == NULL) {
_cmsFree(ContextID, fm);
_cmsFree(ContextID, iohandler);
cmsSignalError(ContextID, cmsERROR_READ, "Couldn't allocate %ld bytes for profile", size);
return NULL;
}
memmove(fm->Block, Buffer, size);
fm ->FreeBlockOnClose = TRUE;
fm ->Size = size;
fm ->Pointer = 0;
break;
case 'w':
fm = (FILEMEM*) _cmsMallocZero(ContextID, sizeof(FILEMEM));
if (fm == NULL) goto Error;
fm ->Block = (cmsUInt8Number*) Buffer;
fm ->FreeBlockOnClose = FALSE;
fm ->Size = size;
fm ->Pointer = 0;
break;
default:
cmsSignalError(ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknow access mode '%c'", *AccessMode);
return NULL;
}
iohandler ->ContextID = ContextID;
iohandler ->stream = (void*) fm;
iohandler ->UsedSpace = 0;
iohandler ->PhysicalFile[0] = 0;
iohandler ->Read = MemoryRead;
iohandler ->Seek = MemorySeek;
iohandler ->Close = MemoryClose;
iohandler ->Tell = MemoryTell;
iohandler ->Write = MemoryWrite;
return iohandler;
Error:
if (fm) _cmsFree(ContextID, fm);
if (iohandler) _cmsFree(ContextID, iohandler);
return NULL;
}
// File-based stream -------------------------------------------------------
// Read count elements of size bytes each. Return number of elements read
static
cmsUInt32Number FileRead(cmsIOHANDLER* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count)
{
cmsUInt32Number nReaded = (cmsUInt32Number) fread(Buffer, size, count, (FILE*) iohandler->stream);
if (nReaded != count) {
cmsSignalError(iohandler ->ContextID, cmsERROR_FILE, "Read error. Got %d bytes, block should be of %d bytes", nReaded * size, count * size);
return 0;
}
return nReaded;
}
// Postion file pointer in the file
static
cmsBool FileSeek(cmsIOHANDLER* iohandler, cmsUInt32Number offset)
{
if (fseek((FILE*) iohandler ->stream, (long) offset, SEEK_SET) != 0) {
cmsSignalError(iohandler ->ContextID, cmsERROR_FILE, "Seek error; probably corrupted file");
return FALSE;
}
return TRUE;
}
// Returns file pointer position
static
cmsUInt32Number FileTell(cmsIOHANDLER* iohandler)
{
return ftell((FILE*)iohandler ->stream);
}
// Writes data to stream, also keeps used space for further reference. Returns TRUE on success, FALSE on error
static
cmsBool FileWrite(cmsIOHANDLER* iohandler, cmsUInt32Number size, const void* Buffer)
{
if (size == 0) return TRUE; // We allow to write 0 bytes, but nothing is written
iohandler->UsedSpace += size;
return (fwrite(Buffer, size, 1, (FILE*) iohandler->stream) == 1);
}
// Closes the file
static
cmsBool FileClose(cmsIOHANDLER* iohandler)
{
if (fclose((FILE*) iohandler ->stream) != 0) return FALSE;
_cmsFree(iohandler ->ContextID, iohandler);
return TRUE;
}
// Create a iohandler for disk based files. if FileName is NULL, then 'stream' member is also set
// to NULL and no real writting is performed. This only happens in writting access mode
cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromFile(cmsContext ContextID, const char* FileName, const char* AccessMode)
{
cmsIOHANDLER* iohandler = NULL;
FILE* fm = NULL;
iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
if (iohandler == NULL) return NULL;
switch (*AccessMode) {
case 'r':
fm = fopen(FileName, "rb");
if (fm == NULL) {
_cmsFree(ContextID, iohandler);
cmsSignalError(ContextID, cmsERROR_FILE, "File '%s' not found", FileName);
return NULL;
}
break;
case 'w':
fm = fopen(FileName, "wb");
if (fm == NULL) {
_cmsFree(ContextID, iohandler);
cmsSignalError(ContextID, cmsERROR_FILE, "Couldn't create '%s'", FileName);
return NULL;
}
break;
default:
_cmsFree(ContextID, iohandler);
cmsSignalError(ContextID, cmsERROR_FILE, "Unknow access mode '%c'", *AccessMode);
return NULL;
}
iohandler ->ContextID = ContextID;
iohandler ->stream = (void*) fm;
iohandler ->UsedSpace = 0;
// Keep track of the original file
if (FileName != NULL) {
strncpy(iohandler -> PhysicalFile, FileName, sizeof(iohandler -> PhysicalFile)-1);
iohandler -> PhysicalFile[sizeof(iohandler -> PhysicalFile)-1] = 0;
}
iohandler ->Read = FileRead;
iohandler ->Seek = FileSeek;
iohandler ->Close = FileClose;
iohandler ->Tell = FileTell;
iohandler ->Write = FileWrite;
return iohandler;
}
// Create a iohandler for stream based files
cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromStream(cmsContext ContextID, FILE* Stream)
{
cmsIOHANDLER* iohandler = NULL;
iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
if (iohandler == NULL) return NULL;
iohandler -> ContextID = ContextID;
iohandler -> stream = (void*) Stream;
iohandler -> UsedSpace = 0;
iohandler -> PhysicalFile[0] = 0;
iohandler ->Read = FileRead;
iohandler ->Seek = FileSeek;
iohandler ->Close = FileClose;
iohandler ->Tell = FileTell;
iohandler ->Write = FileWrite;
return iohandler;
}
// Close an open IO handler
cmsBool CMSEXPORT cmsCloseIOhandler(cmsIOHANDLER* io)
{
return io -> Close(io);
}
// -------------------------------------------------------------------------------------------------------
// Creates an empty structure holding all required parameters
cmsHPROFILE CMSEXPORT cmsCreateProfilePlaceholder(cmsContext ContextID)
{
time_t now = time(NULL);
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) _cmsMallocZero(ContextID, sizeof(_cmsICCPROFILE));
if (Icc == NULL) return NULL;
Icc ->ContextID = ContextID;
// Set it to empty
Icc -> TagCount = 0;
// Set default version
Icc ->Version = 0x02100000;
// Set creation date/time
memmove(&Icc ->Created, gmtime(&now), sizeof(Icc ->Created));
// Return the handle
return (cmsHPROFILE) Icc;
}
cmsContext CMSEXPORT cmsGetProfileContextID(cmsHPROFILE hProfile)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
if (Icc == NULL) return NULL;
return Icc -> ContextID;
}
// Return the number of tags
cmsInt32Number CMSEXPORT cmsGetTagCount(cmsHPROFILE hProfile)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
if (Icc == NULL) return -1;
return Icc->TagCount;
}
// Return the tag signature of a given tag number
cmsTagSignature CMSEXPORT cmsGetTagSignature(cmsHPROFILE hProfile, cmsUInt32Number n)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
if (n > Icc->TagCount) return (cmsTagSignature) 0; // Mark as not available
if (n >= MAX_TABLE_TAG) return (cmsTagSignature) 0; // As double check
return Icc ->TagNames[n];
}
static
int SearchOneTag(_cmsICCPROFILE* Profile, cmsTagSignature sig)
{
cmsUInt32Number i;
for (i=0; i < Profile -> TagCount; i++) {
if (sig == Profile -> TagNames[i])
return i;
}
return -1;
}
// Search for a specific tag in tag dictionary. Returns position or -1 if tag not found.
// If followlinks is turned on, then the position of the linked tag is returned
int _cmsSearchTag(_cmsICCPROFILE* Icc, cmsTagSignature sig, cmsBool lFollowLinks)
{
int n;
cmsTagSignature LinkedSig;
do {
// Search for given tag in ICC profile directory
n = SearchOneTag(Icc, sig);
if (n < 0)
return -1; // Not found
if (!lFollowLinks)
return n; // Found, don't follow links
// Is this a linked tag?
LinkedSig = Icc ->TagLinked[n];
// Yes, follow link
if (LinkedSig != (cmsTagSignature) 0) {
sig = LinkedSig;
}
} while (LinkedSig != (cmsTagSignature) 0);
return n;
}
// Create a new tag entry
static
cmsBool _cmsNewTag(_cmsICCPROFILE* Icc, cmsTagSignature sig, int* NewPos)
{
int i;
// Search for the tag
i = _cmsSearchTag(Icc, sig, FALSE);
// Now let's do it easy. If the tag has been already written, that's an error
if (i >= 0) {
cmsSignalError(Icc ->ContextID, cmsERROR_ALREADY_DEFINED, "Tag '%x' already exists", sig);
return FALSE;
}
else {
// New one
if (Icc -> TagCount >= MAX_TABLE_TAG) {
cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", MAX_TABLE_TAG);
return FALSE;
}
*NewPos = Icc ->TagCount;
Icc -> TagCount++;
}
return TRUE;
}
// Check existance
cmsBool CMSEXPORT cmsIsTag(cmsHPROFILE hProfile, cmsTagSignature sig)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) (void*) hProfile;
return _cmsSearchTag(Icc, sig, FALSE) >= 0;
}
// Read profile header and validate it
cmsBool _cmsReadHeader(_cmsICCPROFILE* Icc)
{
cmsTagEntry Tag;
cmsICCHeader Header;
cmsUInt32Number i, j;
cmsUInt32Number HeaderSize;
cmsIOHANDLER* io = Icc ->IOhandler;
cmsUInt32Number TagCount;
// Read the header
if (io -> Read(io, &Header, sizeof(cmsICCHeader), 1) != 1) {
return FALSE;
}
// Validate file as an ICC profile
if (_cmsAdjustEndianess32(Header.magic) != cmsMagicNumber) {
cmsSignalError(Icc ->ContextID, cmsERROR_BAD_SIGNATURE, "not an ICC profile, invalid signature");
return FALSE;
}
// Adjust endianess of the used parameters
Icc -> DeviceClass = (cmsProfileClassSignature) _cmsAdjustEndianess32(Header.deviceClass);
Icc -> ColorSpace = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Header.colorSpace);
Icc -> PCS = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Header.pcs);
Icc -> RenderingIntent = _cmsAdjustEndianess32(Header.renderingIntent);
Icc -> flags = _cmsAdjustEndianess32(Header.flags);
Icc -> manufacturer = _cmsAdjustEndianess32(Header.manufacturer);
Icc -> model = _cmsAdjustEndianess32(Header.model);
_cmsAdjustEndianess64(&Icc -> attributes, Header.attributes);
Icc -> Version = _cmsAdjustEndianess32(Header.version);
// Get size as reported in header
HeaderSize = _cmsAdjustEndianess32(Header.size);
// Get creation date/time
_cmsDecodeDateTimeNumber(&Header.date, &Icc ->Created);
// The profile ID are 32 raw bytes
memmove(Icc ->ProfileID.ID32, Header.profileID.ID32, 16);
// Read tag directory
if (!_cmsReadUInt32Number(io, &TagCount)) return FALSE;
if (TagCount > MAX_TABLE_TAG) {
cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", TagCount);
return FALSE;
}
// Read tag directory
Icc -> TagCount = 0;
for (i=0; i < TagCount; i++) {
if (!_cmsReadUInt32Number(io, (cmsUInt32Number *) &Tag.sig)) return FALSE;
if (!_cmsReadUInt32Number(io, &Tag.offset)) return FALSE;
if (!_cmsReadUInt32Number(io, &Tag.size)) return FALSE;
// Perform some sanity check. Offset + size should fall inside file.
if (Tag.offset + Tag.size > HeaderSize)
continue;
Icc -> TagNames[Icc ->TagCount] = Tag.sig;
Icc -> TagOffsets[Icc ->TagCount] = Tag.offset;
Icc -> TagSizes[Icc ->TagCount] = Tag.size;
// Search for links
for (j=0; j < Icc ->TagCount; j++) {
if ((Icc ->TagOffsets[j] == Tag.offset) &&
(Icc ->TagSizes[j] == Tag.size)) {
Icc ->TagLinked[Icc ->TagCount] = Icc ->TagNames[j];
}
}
Icc ->TagCount++;
}
return TRUE;
}
// Saves profile header
cmsBool _cmsWriteHeader(_cmsICCPROFILE* Icc, cmsUInt32Number UsedSpace)
{
cmsICCHeader Header;
cmsUInt32Number i;
cmsTagEntry Tag;
cmsInt32Number Count = 0;
Header.size = _cmsAdjustEndianess32(UsedSpace);
Header.cmmId = _cmsAdjustEndianess32(lcmsSignature);
Header.version = _cmsAdjustEndianess32(Icc ->Version);
Header.deviceClass = (cmsProfileClassSignature) _cmsAdjustEndianess32(Icc -> DeviceClass);
Header.colorSpace = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Icc -> ColorSpace);
Header.pcs = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Icc -> PCS);
// NOTE: in v4 Timestamp must be in UTC rather than in local time
_cmsEncodeDateTimeNumber(&Header.date, &Icc ->Created);
Header.magic = _cmsAdjustEndianess32(cmsMagicNumber);
#ifdef CMS_IS_WINDOWS_
Header.platform = (cmsPlatformSignature) _cmsAdjustEndianess32(cmsSigMicrosoft);
#else
Header.platform = (cmsPlatformSignature) _cmsAdjustEndianess32(cmsSigMacintosh);
#endif
Header.flags = _cmsAdjustEndianess32(Icc -> flags);
Header.manufacturer = _cmsAdjustEndianess32(Icc -> manufacturer);
Header.model = _cmsAdjustEndianess32(Icc -> model);
_cmsAdjustEndianess64(&Header.attributes, Icc -> attributes);
// Rendering intent in the header (for embedded profiles)
Header.renderingIntent = _cmsAdjustEndianess32(Icc -> RenderingIntent);
// Illuminant is always D50
Header.illuminant.X = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(cmsD50_XYZ()->X));
Header.illuminant.Y = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(cmsD50_XYZ()->Y));
Header.illuminant.Z = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(cmsD50_XYZ()->Z));
// Created by LittleCMS (that's me!)
Header.creator = _cmsAdjustEndianess32(lcmsSignature);
memset(&Header.reserved, 0, sizeof(Header.reserved));
// Set profile ID. Endianess is always big endian
memmove(&Header.profileID, &Icc ->ProfileID, 16);
// Dump the header
if (!Icc -> IOhandler->Write(Icc->IOhandler, sizeof(cmsICCHeader), &Header)) return FALSE;
// Saves Tag directory
// Get true count
for (i=0; i < Icc -> TagCount; i++) {
if (Icc ->TagNames[i] != 0)
Count++;
}
// Store number of tags
if (!_cmsWriteUInt32Number(Icc ->IOhandler, Count)) return FALSE;
for (i=0; i < Icc -> TagCount; i++) {
if (Icc ->TagNames[i] == 0) continue; // It is just a placeholder
Tag.sig = (cmsTagSignature) _cmsAdjustEndianess32((cmsInt32Number) Icc -> TagNames[i]);
Tag.offset = _cmsAdjustEndianess32((cmsInt32Number) Icc -> TagOffsets[i]);
Tag.size = _cmsAdjustEndianess32((cmsInt32Number) Icc -> TagSizes[i]);
if (!Icc ->IOhandler -> Write(Icc-> IOhandler, sizeof(cmsTagEntry), &Tag)) return FALSE;
}
return TRUE;
}
// ----------------------------------------------------------------------- Set/Get several struct members
cmsUInt32Number CMSEXPORT cmsGetHeaderRenderingIntent(cmsHPROFILE hProfile)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
return Icc -> RenderingIntent;
}
void CMSEXPORT cmsSetHeaderRenderingIntent(cmsHPROFILE hProfile, cmsUInt32Number RenderingIntent)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
Icc -> RenderingIntent = RenderingIntent;
}
cmsUInt32Number CMSEXPORT cmsGetHeaderFlags(cmsHPROFILE hProfile)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
return (cmsUInt32Number) Icc -> flags;
}
void CMSEXPORT cmsSetHeaderFlags(cmsHPROFILE hProfile, cmsUInt32Number Flags)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
Icc -> flags = (cmsUInt32Number) Flags;
}
cmsUInt32Number CMSEXPORT cmsGetHeaderManufacturer(cmsHPROFILE hProfile)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
return (cmsUInt32Number) Icc ->manufacturer;
}
void CMSEXPORT cmsSetHeaderManufacturer(cmsHPROFILE hProfile, cmsUInt32Number manufacturer)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
Icc -> manufacturer = (cmsUInt32Number) manufacturer;
}
cmsUInt32Number CMSEXPORT cmsGetHeaderModel(cmsHPROFILE hProfile)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
return (cmsUInt32Number) Icc ->model;
}
void CMSEXPORT cmsSetHeaderModel(cmsHPROFILE hProfile, cmsUInt32Number model)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
Icc -> manufacturer = (cmsUInt32Number) model;
}
void CMSEXPORT cmsGetHeaderAttributes(cmsHPROFILE hProfile, cmsUInt64Number* Flags)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
memmove(Flags, &Icc -> attributes, sizeof(cmsUInt64Number));
}
void CMSEXPORT cmsSetHeaderAttributes(cmsHPROFILE hProfile, cmsUInt64Number Flags)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
memmove(&Icc -> attributes, &Flags, sizeof(cmsUInt64Number));
}
void CMSEXPORT cmsGetHeaderProfileID(cmsHPROFILE hProfile, cmsUInt8Number* ProfileID)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
memmove(ProfileID, Icc ->ProfileID.ID8, 16);
}
void CMSEXPORT cmsSetHeaderProfileID(cmsHPROFILE hProfile, cmsUInt8Number* ProfileID)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
memmove(&Icc -> ProfileID, ProfileID, 16);
}
cmsBool CMSEXPORT cmsGetHeaderCreationDateTime(cmsHPROFILE hProfile, struct tm *Dest)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
memmove(Dest, &Icc ->Created, sizeof(struct tm));
return TRUE;
}
cmsColorSpaceSignature CMSEXPORT cmsGetPCS(cmsHPROFILE hProfile)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
return Icc -> PCS;
}
void CMSEXPORT cmsSetPCS(cmsHPROFILE hProfile, cmsColorSpaceSignature pcs)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
Icc -> PCS = pcs;
}
cmsColorSpaceSignature CMSEXPORT cmsGetColorSpace(cmsHPROFILE hProfile)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
return Icc -> ColorSpace;
}
void CMSEXPORT cmsSetColorSpace(cmsHPROFILE hProfile, cmsColorSpaceSignature sig)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
Icc -> ColorSpace = sig;
}
cmsProfileClassSignature CMSEXPORT cmsGetDeviceClass(cmsHPROFILE hProfile)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
return Icc -> DeviceClass;
}
void CMSEXPORT cmsSetDeviceClass(cmsHPROFILE hProfile, cmsProfileClassSignature sig)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
Icc -> DeviceClass = sig;
}
cmsUInt32Number CMSEXPORT cmsGetEncodedICCversion(cmsHPROFILE hProfile)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
return Icc -> Version;
}
void CMSEXPORT cmsSetEncodedICCversion(cmsHPROFILE hProfile, cmsUInt32Number Version)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
Icc -> Version = Version;
}
// Get an hexadecimal number with same digits as v
static
cmsUInt32Number BaseToBase(cmsUInt32Number in, int BaseIn, int BaseOut)
{
char Buff[100];
int i, len;
cmsUInt32Number out;
for (len=0; in > 0 && len < 100; len++) {
Buff[len] = (char) (in % BaseIn);
in /= BaseIn;
}
for (i=len-1, out=0; i >= 0; --i) {
out = out * BaseOut + Buff[i];
}
return out;
}
void CMSEXPORT cmsSetProfileVersion(cmsHPROFILE hProfile, cmsFloat64Number Version)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
// 4.2 -> 0x4200000
Icc -> Version = BaseToBase((cmsUInt32Number) floor(Version * 100.0), 10, 16) << 16;
}
cmsFloat64Number CMSEXPORT cmsGetProfileVersion(cmsHPROFILE hProfile)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
cmsUInt32Number n = Icc -> Version >> 16;
return BaseToBase(n, 16, 10) / 100.0;
}
// --------------------------------------------------------------------------------------------------------------
// Create profile from IOhandler
cmsHPROFILE CMSEXPORT cmsOpenProfileFromIOhandlerTHR(cmsContext ContextID, cmsIOHANDLER* io)
{
_cmsICCPROFILE* NewIcc;
cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
if (hEmpty == NULL) return NULL;
NewIcc = (_cmsICCPROFILE*) hEmpty;
NewIcc ->IOhandler = io;
if (!_cmsReadHeader(NewIcc)) goto Error;
return hEmpty;
Error:
cmsCloseProfile(hEmpty);
return NULL;
}
// Create profile from disk file
cmsHPROFILE CMSEXPORT cmsOpenProfileFromFileTHR(cmsContext ContextID, const char *lpFileName, const char *sAccess)
{
_cmsICCPROFILE* NewIcc;
cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
if (hEmpty == NULL) return NULL;
NewIcc = (_cmsICCPROFILE*) hEmpty;
NewIcc ->IOhandler = cmsOpenIOhandlerFromFile(ContextID, lpFileName, sAccess);
if (NewIcc ->IOhandler == NULL) goto Error;
if (*sAccess == 'W' || *sAccess == 'w') {
NewIcc -> IsWrite = TRUE;
return hEmpty;
}
if (!_cmsReadHeader(NewIcc)) goto Error;
return hEmpty;
Error:
cmsCloseProfile(hEmpty);
return NULL;
}
cmsHPROFILE CMSEXPORT cmsOpenProfileFromFile(const char *ICCProfile, const char *sAccess)
{
return cmsOpenProfileFromFileTHR(NULL, ICCProfile, sAccess);
}
cmsHPROFILE CMSEXPORT cmsOpenProfileFromStreamTHR(cmsContext ContextID, FILE* ICCProfile, const char *sAccess)
{
_cmsICCPROFILE* NewIcc;
cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
if (hEmpty == NULL) return NULL;
NewIcc = (_cmsICCPROFILE*) hEmpty;
NewIcc ->IOhandler = cmsOpenIOhandlerFromStream(ContextID, ICCProfile);
if (NewIcc ->IOhandler == NULL) goto Error;
if (*sAccess == 'w') {
NewIcc -> IsWrite = TRUE;
return hEmpty;
}
if (!_cmsReadHeader(NewIcc)) goto Error;
return hEmpty;
Error:
cmsCloseProfile(hEmpty);
return NULL;
}
cmsHPROFILE CMSEXPORT cmsOpenProfileFromStream(FILE* ICCProfile, const char *sAccess)
{
return cmsOpenProfileFromStreamTHR(NULL, ICCProfile, sAccess);
}
// Open from memory block
cmsHPROFILE CMSEXPORT cmsOpenProfileFromMemTHR(cmsContext ContextID, const void* MemPtr, cmsUInt32Number dwSize)
{
_cmsICCPROFILE* NewIcc;
cmsHPROFILE hEmpty;
hEmpty = cmsCreateProfilePlaceholder(ContextID);
if (hEmpty == NULL) return NULL;
NewIcc = (_cmsICCPROFILE*) hEmpty;
// Ok, in this case const void* is casted to void* just because open IO handler
// shares read and writting modes. Don't abuse this feature!
NewIcc ->IOhandler = cmsOpenIOhandlerFromMem(ContextID, (void*) MemPtr, dwSize, "r");
if (NewIcc ->IOhandler == NULL) goto Error;
if (!_cmsReadHeader(NewIcc)) goto Error;
return hEmpty;
Error:
cmsCloseProfile(hEmpty);
return NULL;
}
cmsHPROFILE CMSEXPORT cmsOpenProfileFromMem(const void* MemPtr, cmsUInt32Number dwSize)
{
return cmsOpenProfileFromMemTHR(NULL, MemPtr, dwSize);
}
// Dump tag contents. If the profile is being modified, untouched tags are copied from FileOrig
static
cmsBool SaveTags(_cmsICCPROFILE* Icc, _cmsICCPROFILE* FileOrig)
{
cmsUInt8Number* Data;
cmsUInt32Number i;
cmsUInt32Number Begin;
cmsIOHANDLER* io = Icc ->IOhandler;
cmsTagDescriptor* TagDescriptor;
cmsTagTypeSignature TypeBase;
cmsTagTypeHandler* TypeHandler;
for (i=0; i < Icc -> TagCount; i++) {
if (Icc ->TagNames[i] == 0) continue;
// Linked tags are not written
if (Icc ->TagLinked[i] != (cmsTagSignature) 0) continue;
Icc -> TagOffsets[i] = Begin = io ->UsedSpace;
Data = (cmsUInt8Number*) Icc -> TagPtrs[i];
if (!Data) {
// Reach here if we are copying a tag from a disk-based ICC profile which has not been modified by user.
// In this case a blind copy of the block data is performed
if (FileOrig != NULL && Icc -> TagOffsets[i]) {
cmsUInt32Number TagSize = FileOrig -> TagSizes[i];
cmsUInt32Number TagOffset = FileOrig -> TagOffsets[i];
void* Mem;
if (!FileOrig ->IOhandler->Seek(FileOrig ->IOhandler, TagOffset)) return FALSE;
Mem = _cmsMalloc(Icc ->ContextID, TagSize);
if (Mem == NULL) return FALSE;
if (FileOrig ->IOhandler->Read(FileOrig->IOhandler, Mem, TagSize, 1) != 1) return FALSE;
if (!io ->Write(io, TagSize, Mem)) return FALSE;
_cmsFree(Icc ->ContextID, Mem);
Icc -> TagSizes[i] = (io ->UsedSpace - Begin);
// Align to 32 bit boundary.
if (! _cmsWriteAlignment(io))
return FALSE;
}
continue;
}
// Should this tag be saved as RAW? If so, tagsizes should be specified in advance (no further cooking is done)
if (Icc ->TagSaveAsRaw[i]) {
if (io -> Write(io, Icc ->TagSizes[i], Data) != 1) return FALSE;
}
else {
// Search for support on this tag
TagDescriptor = _cmsGetTagDescriptor(Icc -> TagNames[i]);
if (TagDescriptor == NULL) continue; // Unsupported, ignore it
TypeHandler = Icc ->TagTypeHandlers[i];
if (TypeHandler == NULL) {
cmsSignalError(Icc ->ContextID, cmsERROR_INTERNAL, "(Internal) no handler for tag %x", Icc -> TagNames[i]);
continue;
}
TypeBase = TypeHandler ->Signature;
if (!_cmsWriteTypeBase(io, TypeBase))
return FALSE;
if (!TypeHandler ->WritePtr(TypeHandler, io, Data, TagDescriptor ->ElemCount)) {
char String[5];
_cmsTagSignature2String(String, (cmsTagSignature) TypeBase);
cmsSignalError(Icc ->ContextID, cmsERROR_WRITE, "Couldn't write type '%s'", String);
return FALSE;
}
}
Icc -> TagSizes[i] = (io ->UsedSpace - Begin);
// Align to 32 bit boundary.
if (! _cmsWriteAlignment(io))
return FALSE;
}
return TRUE;
}
// Fill the offset and size fields for all linked tags
static
cmsBool SetLinks( _cmsICCPROFILE* Icc)
{
cmsUInt32Number i;
for (i=0; i < Icc -> TagCount; i++) {
cmsTagSignature lnk = Icc ->TagLinked[i];
if (lnk != (cmsTagSignature) 0) {
int j = _cmsSearchTag(Icc, lnk, FALSE);
if (j >= 0) {
Icc ->TagOffsets[i] = Icc ->TagOffsets[j];
Icc ->TagSizes[i] = Icc ->TagSizes[j];
}
}
}
return TRUE;
}
// Low-level save to IOHANDLER. It returns the number of bytes used to
// store the profile, or zero on error. io may be NULL and in this case
// no data is written--only sizes are calculated
cmsUInt32Number CMSEXPORT cmsSaveProfileToIOhandler(cmsHPROFILE hProfile, cmsIOHANDLER* io)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
_cmsICCPROFILE Keep;
cmsIOHANDLER* PrevIO;
cmsUInt32Number UsedSpace;
cmsContext ContextID;
memmove(&Keep, Icc, sizeof(_cmsICCPROFILE));
ContextID = cmsGetProfileContextID(hProfile);
PrevIO = Icc ->IOhandler = cmsOpenIOhandlerFromNULL(ContextID);
if (PrevIO == NULL) return 0;
// Pass #1 does compute offsets
if (!_cmsWriteHeader(Icc, 0)) return 0;
if (!SaveTags(Icc, &Keep)) return 0;
UsedSpace = PrevIO ->UsedSpace;
// Pass #2 does save to iohandler
if (io != NULL) {
Icc ->IOhandler = io;
if (!SetLinks(Icc)) goto CleanUp;
if (!_cmsWriteHeader(Icc, UsedSpace)) goto CleanUp;
if (!SaveTags(Icc, &Keep)) goto CleanUp;
}
memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
if (!cmsCloseIOhandler(PrevIO)) return 0;
return UsedSpace;
CleanUp:
cmsCloseIOhandler(PrevIO);
memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
return 0;
}
// Low-level save to disk.
cmsBool CMSEXPORT cmsSaveProfileToFile(cmsHPROFILE hProfile, const char* FileName)
{
cmsContext ContextID = cmsGetProfileContextID(hProfile);
cmsIOHANDLER* io = cmsOpenIOhandlerFromFile(ContextID, FileName, "w");
cmsBool rc;
if (io == NULL) return FALSE;
rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
rc &= cmsCloseIOhandler(io);
if (rc == FALSE) { // remove() is C99 per 7.19.4.1
remove(FileName); // We have to IGNORE return value in this case
}
return rc;
}
// Same as anterior, but for streams
cmsBool CMSEXPORT cmsSaveProfileToStream(cmsHPROFILE hProfile, FILE* Stream)
{
cmsBool rc;
cmsContext ContextID = cmsGetProfileContextID(hProfile);
cmsIOHANDLER* io = cmsOpenIOhandlerFromStream(ContextID, Stream);
if (io == NULL) return FALSE;
rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
rc &= cmsCloseIOhandler(io);
return rc;
}
// Same as anterior, but for memory blocks. In this case, a NULL as MemPtr means calculate needed space only
cmsBool CMSEXPORT cmsSaveProfileToMem(cmsHPROFILE hProfile, void *MemPtr, cmsUInt32Number* BytesNeeded)
{
cmsBool rc;
cmsIOHANDLER* io;
cmsContext ContextID = cmsGetProfileContextID(hProfile);
// Should we just calculate the needed space?
if (MemPtr == NULL) {
*BytesNeeded = cmsSaveProfileToIOhandler(hProfile, NULL);
return TRUE;
}
// That is a real write operation
io = cmsOpenIOhandlerFromMem(ContextID, MemPtr, *BytesNeeded, "w");
if (io == NULL) return FALSE;
rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
rc &= cmsCloseIOhandler(io);
return rc;
}
// Closes a profile freeing any involved resources
cmsBool CMSEXPORT cmsCloseProfile(cmsHPROFILE hProfile)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
cmsBool rc = TRUE;
cmsUInt32Number i;
if (!Icc) return FALSE;
// Was open in write mode?
if (Icc ->IsWrite) {
Icc ->IsWrite = FALSE; // Assure no further writting
rc &= cmsSaveProfileToFile(hProfile, Icc ->IOhandler->PhysicalFile);
}
for (i=0; i < Icc -> TagCount; i++) {
if (Icc -> TagPtrs[i]) {
cmsTagTypeHandler* TypeHandler = Icc ->TagTypeHandlers[i];
if (TypeHandler != NULL)
TypeHandler ->FreePtr(TypeHandler, Icc -> TagPtrs[i]);
else
_cmsFree(Icc ->ContextID, Icc ->TagPtrs[i]);
}
}
if (Icc ->IOhandler != NULL) {
rc &= cmsCloseIOhandler(Icc->IOhandler);
}
_cmsFree(Icc ->ContextID, Icc); // Free placeholder memory
return rc;
}
// -------------------------------------------------------------------------------------------------------------------
// Returns TRUE if a given tag is supported by a plug-in
static
cmsBool IsTypeSupported(cmsTagDescriptor* TagDescriptor, cmsTagTypeSignature Type)
{
cmsUInt32Number i, nMaxTypes;
nMaxTypes = TagDescriptor->nSupportedTypes;
if (nMaxTypes >= MAX_TYPES_IN_LCMS_PLUGIN)
nMaxTypes = MAX_TYPES_IN_LCMS_PLUGIN;
for (i=0; i < nMaxTypes; i++) {
if (Type == TagDescriptor ->SupportedTypes[i]) return TRUE;
}
return FALSE;
}
// That's the main read function
void* CMSEXPORT cmsReadTag(cmsHPROFILE hProfile, cmsTagSignature sig)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
cmsIOHANDLER* io = Icc ->IOhandler;
cmsTagTypeHandler* TypeHandler;
cmsTagDescriptor* TagDescriptor;
cmsTagTypeSignature BaseType;
cmsUInt32Number Offset, TagSize;
cmsUInt32Number ElemCount;
int n;
n = _cmsSearchTag(Icc, sig, TRUE);
if (n < 0) return NULL; // Not found, return NULL
// If the element is already in memory, return the pointer
if (Icc -> TagPtrs[n]) {
if (Icc ->TagSaveAsRaw[n]) return NULL; // We don't support read raw tags as cooked
return Icc -> TagPtrs[n];
}
// We need to read it. Get the offset and size to the file
Offset = Icc -> TagOffsets[n];
TagSize = Icc -> TagSizes[n];
// Seek to its location
if (!io -> Seek(io, Offset))
return NULL;
// Search for support on this tag
TagDescriptor = _cmsGetTagDescriptor(sig);
if (TagDescriptor == NULL) return NULL; // Unsupported.
// if supported, get type and check if in list
BaseType = _cmsReadTypeBase(io);
if (BaseType == 0) return NULL;
if (!IsTypeSupported(TagDescriptor, BaseType)) return NULL;
TagSize -= 8; // Alredy read by the type base logic
// Get type handler
TypeHandler = _cmsGetTagTypeHandler(BaseType);
if (TypeHandler == NULL) return NULL;
// Read the tag
Icc -> TagTypeHandlers[n] = TypeHandler;
Icc -> TagPtrs[n] = TypeHandler ->ReadPtr(TypeHandler, io, &ElemCount, TagSize);
// The tag type is supported, but something wrong happend and we cannot read the tag.
// let know the user about this (although it is just a warning)
if (Icc -> TagPtrs[n] == NULL) {
char String[5];
_cmsTagSignature2String(String, sig);
cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "Corrupted tag '%s'", String);
return NULL;
}
// This is a weird error that may be a symptom of something more serious, the number of
// stored item is actually less than the number of required elements.
if (ElemCount < TagDescriptor ->ElemCount) {
char String[5];
_cmsTagSignature2String(String, sig);
cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "'%s' Inconsistent number of items: expected %d, got %d",
String, TagDescriptor ->ElemCount, ElemCount);
}
// Return the data
return Icc -> TagPtrs[n];
}
// Get true type of data
cmsTagTypeSignature _cmsGetTagTrueType(cmsHPROFILE hProfile, cmsTagSignature sig)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
cmsTagTypeHandler* TypeHandler;
int n;
// Search for given tag in ICC profile directory
n = _cmsSearchTag(Icc, sig, TRUE);
if (n < 0) return (cmsTagTypeSignature) 0; // Not found, return NULL
// Get the handler. The true type is there
TypeHandler = Icc -> TagTypeHandlers[n];
return TypeHandler ->Signature;
}
// Write a single tag. This just keeps track of the tak into a list of "to be written". If the tag is already
// in that list, the previous version is deleted.
cmsBool CMSEXPORT cmsWriteTag(cmsHPROFILE hProfile, cmsTagSignature sig, const void* data)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
cmsTagTypeHandler* TypeHandler = NULL;
cmsTagDescriptor* TagDescriptor = NULL;
cmsTagTypeSignature Type;
int i;
cmsFloat64Number Version;
if (data == NULL) {
cmsSignalError(cmsGetProfileContextID(hProfile), cmsERROR_NULL, "couldn't wite NULL to tag");
return FALSE;
}
i = _cmsSearchTag(Icc, sig, FALSE);
if (i >=0) {
if (Icc -> TagPtrs[i] != NULL) {
// Already exists. Free previous version
if (Icc ->TagSaveAsRaw[i]) {
_cmsFree(Icc ->ContextID, Icc ->TagPtrs[i]);
}
else {
TypeHandler = Icc ->TagTypeHandlers[i];
TypeHandler->FreePtr(TypeHandler, Icc -> TagPtrs[i]);
}
}
}
else {
// New one
i = Icc -> TagCount;
if (i >= MAX_TABLE_TAG) {
cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", MAX_TABLE_TAG);
return FALSE;
}
Icc -> TagCount++;
}
// This is not raw
Icc ->TagSaveAsRaw[i] = FALSE;
// This is not a link
Icc ->TagLinked[i] = (cmsTagSignature) 0;
// Get information about the TAG.
TagDescriptor = _cmsGetTagDescriptor(sig);
if (TagDescriptor == NULL){
cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported tag '%x'", sig);
return FALSE;
}
// Now we need to know which type to use. It depends on the version.
Version = cmsGetProfileVersion(hProfile);
if (TagDescriptor ->DecideType != NULL) {
// Let the tag descriptor to decide the type base on depending on
// the data. This is useful for example on parametric curves, where
// curves specified by a table cannot be saved as parametric and needs
// to be revented to single v2-curves, even on v4 profiles.
Type = TagDescriptor ->DecideType(Version, data);
}
else {
Type = TagDescriptor ->SupportedTypes[0];
}
// Does the tag support this type?
if (!IsTypeSupported(TagDescriptor, Type)) {
cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported type '%x' for tag '%x'", Type, sig);
return FALSE;
}
// Does we have a handler for this type?
TypeHandler = _cmsGetTagTypeHandler(Type);
if (TypeHandler == NULL) {
cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported type '%x' for tag '%x'", Type, sig);
return FALSE; // Should never happen
}
// Fill fields on icc structure
Icc ->TagTypeHandlers[i] = TypeHandler;
Icc ->TagNames[i] = sig;
Icc ->TagSizes[i] = 0;
Icc ->TagOffsets[i] = 0;
Icc ->TagPtrs[i] = TypeHandler ->DupPtr(TypeHandler, data, TagDescriptor ->ElemCount);
if (Icc ->TagPtrs[i] == NULL) {
TypeHandler ->DupPtr(TypeHandler, data, TagDescriptor ->ElemCount);
cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "Malformed struct in type '%x' for tag '%x'", Type, sig);
return FALSE;
}
return TRUE;
}
// Read and write raw data. The only way those function would work and keep consistence with normal read and write
// is to do an additional step of serialization. That means, readRaw would issue a normal read and then convert the obtained
// data to raw bytes by using the "write" serialization logic. And vice-versa. I know this may end in situations where
// raw data written does not exactly correspond with the raw data proposed to cmsWriteRaw data, but this approach allows
// to write a tag as raw data and the read it as handled.
cmsInt32Number CMSEXPORT cmsReadRawTag(cmsHPROFILE hProfile, cmsTagSignature sig, void* data, cmsUInt32Number BufferSize)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
void *Object;
int i;
cmsIOHANDLER* MemIO;
cmsTagTypeHandler* TypeHandler = NULL;
cmsTagDescriptor* TagDescriptor = NULL;
cmsUInt32Number rc;
cmsUInt32Number Offset, TagSize;
// Search for given tag in ICC profile directory
i = _cmsSearchTag(Icc, sig, TRUE);
if (i < 0) return 0; // Not found, return 0
// It is already read?
if (Icc -> TagPtrs[i] == NULL) {
// No yet, get original position
Offset = Icc ->TagOffsets[i];
TagSize = Icc ->TagSizes[i];
// read the data directly, don't keep copy
if (data != NULL) {
if (BufferSize < TagSize)
TagSize = BufferSize;
if (!Icc ->IOhandler ->Seek(Icc ->IOhandler, Offset)) return 0;
if (!Icc ->IOhandler ->Read(Icc ->IOhandler, data, 1, TagSize)) return 0;
}
return Icc ->TagSizes[i];
}
// The data has been already read, or written. But wait!, maybe the user choosed to save as
// raw data. In this case, return the raw data directly
if (Icc ->TagSaveAsRaw[i]) {
if (data != NULL) {
TagSize = Icc ->TagSizes[i];
if (BufferSize < TagSize)
TagSize = BufferSize;
memmove(data, Icc ->TagPtrs[i], TagSize);
}
return Icc ->TagSizes[i];
}
// Already readed, or previously set by cmsWriteTag(). We need to serialize that
// data to raw in order to maintain consistency.
Object = cmsReadTag(hProfile, sig);
if (Object == NULL) return 0;
// Now we need to serialize to a memory block: just use a memory iohandler
if (data == NULL) {
MemIO = cmsOpenIOhandlerFromNULL(cmsGetProfileContextID(hProfile));
} else{
MemIO = cmsOpenIOhandlerFromMem(cmsGetProfileContextID(hProfile), data, BufferSize, "w");
}
if (MemIO == NULL) return 0;
// Obtain type handling for the tag
TypeHandler = Icc ->TagTypeHandlers[i];
TagDescriptor = _cmsGetTagDescriptor(sig);
// Serialize
if (!TypeHandler ->WritePtr(TypeHandler, MemIO, Object, TagDescriptor ->ElemCount)) return 0;
// Get Size and close
rc = MemIO ->Tell(MemIO);
cmsCloseIOhandler(MemIO); // Ignore return code this time
return rc;
}
// Similar to the anterior. This function allows to write directly to the ICC profile any data, without
// checking anything. As a rule, mixing Raw with cooked doesn't work, so writting a tag as raw and then reading
// it as cooked without serializing does result into an error. If that is wha you want, you will need to dump
// the profile to memry or disk and then reopen it.
cmsBool CMSEXPORT cmsWriteRawTag(cmsHPROFILE hProfile, cmsTagSignature sig, const void* data, cmsUInt32Number Size)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
int i;
if (!_cmsNewTag(Icc, sig, &i)) return FALSE;
// Mark the tag as being written as RAW
Icc ->TagSaveAsRaw[i] = TRUE;
Icc ->TagNames[i] = sig;
Icc ->TagLinked[i] = (cmsTagSignature) 0;
// Keep a copy of the block
Icc ->TagPtrs[i] = _cmsDupMem(Icc ->ContextID, data, Size);
Icc ->TagSizes[i] = Size;
return TRUE;
}
// Using this function you can collapse several tag entries to the same block in the profile
cmsBool CMSEXPORT cmsLinkTag(cmsHPROFILE hProfile, cmsTagSignature sig, cmsTagSignature dest)
{
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
int i;
if (!_cmsNewTag(Icc, sig, &i)) return FALSE;
// Keep necessary information
Icc ->TagSaveAsRaw[i] = FALSE;
Icc ->TagNames[i] = sig;
Icc ->TagLinked[i] = dest;
Icc ->TagPtrs[i] = NULL;
Icc ->TagSizes[i] = 0;
Icc ->TagOffsets[i] = 0;
return TRUE;
}
|