Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/lcms2/src/cmsio0.c
4391 views
1
//---------------------------------------------------------------------------------
2
//
3
// Little Color Management System
4
// Copyright (c) 1998-2024 Marti Maria Saguer
5
//
6
// Permission is hereby granted, free of charge, to any person obtaining
7
// a copy of this software and associated documentation files (the "Software"),
8
// to deal in the Software without restriction, including without limitation
9
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
// and/or sell copies of the Software, and to permit persons to whom the Software
11
// is furnished to do so, subject to the following conditions:
12
//
13
// The above copyright notice and this permission notice shall be included in
14
// all copies or substantial portions of the Software.
15
//
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
18
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
//
24
//---------------------------------------------------------------------------------
25
//
26
27
#include "lcms2_internal.h"
28
29
// Generic I/O, tag dictionary management, profile struct
30
31
// IOhandlers are abstractions used by littleCMS to read from whatever file, stream,
32
// memory block or any storage. Each IOhandler provides implementations for read,
33
// write, seek and tell functions. LittleCMS code deals with IO across those objects.
34
// In this way, is easier to add support for new storage media.
35
36
// NULL stream, for taking care of used space -------------------------------------
37
38
// NULL IOhandler basically does nothing but keep track on how many bytes have been
39
// written. This is handy when creating profiles, where the file size is needed in the
40
// header. Then, whole profile is serialized across NULL IOhandler and a second pass
41
// writes the bytes to the pertinent IOhandler.
42
43
typedef struct {
44
cmsUInt32Number Pointer; // Points to current location
45
} FILENULL;
46
47
static
48
cmsUInt32Number NULLRead(cmsIOHANDLER* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count)
49
{
50
FILENULL* ResData = (FILENULL*) iohandler ->stream;
51
52
cmsUInt32Number len = size * count;
53
ResData -> Pointer += len;
54
return count;
55
56
cmsUNUSED_PARAMETER(Buffer);
57
}
58
59
static
60
cmsBool NULLSeek(cmsIOHANDLER* iohandler, cmsUInt32Number offset)
61
{
62
FILENULL* ResData = (FILENULL*) iohandler ->stream;
63
64
ResData ->Pointer = offset;
65
return TRUE;
66
}
67
68
static
69
cmsUInt32Number NULLTell(cmsIOHANDLER* iohandler)
70
{
71
FILENULL* ResData = (FILENULL*) iohandler ->stream;
72
return ResData -> Pointer;
73
}
74
75
static
76
cmsBool NULLWrite(cmsIOHANDLER* iohandler, cmsUInt32Number size, const void *Ptr)
77
{
78
FILENULL* ResData = (FILENULL*) iohandler ->stream;
79
80
ResData ->Pointer += size;
81
if (ResData ->Pointer > iohandler->UsedSpace)
82
iohandler->UsedSpace = ResData ->Pointer;
83
84
return TRUE;
85
86
cmsUNUSED_PARAMETER(Ptr);
87
}
88
89
static
90
cmsBool NULLClose(cmsIOHANDLER* iohandler)
91
{
92
FILENULL* ResData = (FILENULL*) iohandler ->stream;
93
94
_cmsFree(iohandler ->ContextID, ResData);
95
_cmsFree(iohandler ->ContextID, iohandler);
96
return TRUE;
97
}
98
99
// The NULL IOhandler creator
100
cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromNULL(cmsContext ContextID)
101
{
102
struct _cms_io_handler* iohandler = NULL;
103
FILENULL* fm = NULL;
104
105
iohandler = (struct _cms_io_handler*) _cmsMallocZero(ContextID, sizeof(struct _cms_io_handler));
106
if (iohandler == NULL) return NULL;
107
108
fm = (FILENULL*) _cmsMallocZero(ContextID, sizeof(FILENULL));
109
if (fm == NULL) goto Error;
110
111
fm ->Pointer = 0;
112
113
iohandler ->ContextID = ContextID;
114
iohandler ->stream = (void*) fm;
115
iohandler ->UsedSpace = 0;
116
iohandler ->ReportedSize = 0;
117
iohandler ->PhysicalFile[0] = 0;
118
119
iohandler ->Read = NULLRead;
120
iohandler ->Seek = NULLSeek;
121
iohandler ->Close = NULLClose;
122
iohandler ->Tell = NULLTell;
123
iohandler ->Write = NULLWrite;
124
125
return iohandler;
126
127
Error:
128
if (iohandler) _cmsFree(ContextID, iohandler);
129
return NULL;
130
131
}
132
133
134
// Memory-based stream --------------------------------------------------------------
135
136
// Those functions implements an iohandler which takes a block of memory as storage medium.
137
138
typedef struct {
139
cmsUInt8Number* Block; // Points to allocated memory
140
cmsUInt32Number Size; // Size of allocated memory
141
cmsUInt32Number Pointer; // Points to current location
142
int FreeBlockOnClose; // As title
143
144
} FILEMEM;
145
146
static
147
cmsUInt32Number MemoryRead(struct _cms_io_handler* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count)
148
{
149
FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
150
cmsUInt8Number* Ptr;
151
cmsUInt32Number len = size * count;
152
153
if (ResData -> Pointer + len > ResData -> Size){
154
155
len = (ResData -> Size - ResData -> Pointer);
156
cmsSignalError(iohandler ->ContextID, cmsERROR_READ, "Read from memory error. Got %d bytes, block should be of %d bytes", len, count * size);
157
return 0;
158
}
159
160
Ptr = ResData -> Block;
161
Ptr += ResData -> Pointer;
162
memmove(Buffer, Ptr, len);
163
ResData -> Pointer += len;
164
165
return count;
166
}
167
168
// SEEK_CUR is assumed
169
static
170
cmsBool MemorySeek(struct _cms_io_handler* iohandler, cmsUInt32Number offset)
171
{
172
FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
173
174
if (offset > ResData ->Size) {
175
cmsSignalError(iohandler ->ContextID, cmsERROR_SEEK, "Too few data; probably corrupted profile");
176
return FALSE;
177
}
178
179
ResData ->Pointer = offset;
180
return TRUE;
181
}
182
183
// Tell for memory
184
static
185
cmsUInt32Number MemoryTell(struct _cms_io_handler* iohandler)
186
{
187
FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
188
189
if (ResData == NULL) return 0;
190
return ResData -> Pointer;
191
}
192
193
194
// Writes data to memory, also keeps used space for further reference.
195
static
196
cmsBool MemoryWrite(struct _cms_io_handler* iohandler, cmsUInt32Number size, const void *Ptr)
197
{
198
FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
199
200
if (ResData == NULL) return FALSE; // Housekeeping
201
202
// Check for available space. Clip.
203
if (ResData->Pointer + size > ResData->Size) {
204
size = ResData ->Size - ResData->Pointer;
205
}
206
207
if (size == 0) return TRUE; // Write zero bytes is ok, but does nothing
208
209
memmove(ResData ->Block + ResData ->Pointer, Ptr, size);
210
ResData ->Pointer += size;
211
212
if (ResData ->Pointer > iohandler->UsedSpace)
213
iohandler->UsedSpace = ResData ->Pointer;
214
215
return TRUE;
216
}
217
218
219
static
220
cmsBool MemoryClose(struct _cms_io_handler* iohandler)
221
{
222
FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
223
224
if (ResData ->FreeBlockOnClose) {
225
226
if (ResData ->Block) _cmsFree(iohandler ->ContextID, ResData ->Block);
227
}
228
229
_cmsFree(iohandler ->ContextID, ResData);
230
_cmsFree(iohandler ->ContextID, iohandler);
231
232
return TRUE;
233
}
234
235
// Create a iohandler for memory block. AccessMode=='r' assumes the iohandler is going to read, and makes
236
// a copy of the memory block for letting user to free the memory after invoking open profile. In write
237
// mode ("w"), Buffer points to the begin of memory block to be written.
238
cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromMem(cmsContext ContextID, void *Buffer, cmsUInt32Number size, const char* AccessMode)
239
{
240
cmsIOHANDLER* iohandler = NULL;
241
FILEMEM* fm = NULL;
242
243
_cmsAssert(AccessMode != NULL);
244
245
iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
246
if (iohandler == NULL) return NULL;
247
248
switch (*AccessMode) {
249
250
case 'r':
251
fm = (FILEMEM*) _cmsMallocZero(ContextID, sizeof(FILEMEM));
252
if (fm == NULL) goto Error;
253
254
if (Buffer == NULL) {
255
cmsSignalError(ContextID, cmsERROR_READ, "Couldn't read profile from NULL pointer");
256
goto Error;
257
}
258
259
fm ->Block = (cmsUInt8Number*) _cmsMalloc(ContextID, size);
260
if (fm ->Block == NULL) {
261
262
_cmsFree(ContextID, fm);
263
_cmsFree(ContextID, iohandler);
264
cmsSignalError(ContextID, cmsERROR_READ, "Couldn't allocate %ld bytes for profile", (long) size);
265
return NULL;
266
}
267
268
269
memmove(fm->Block, Buffer, size);
270
fm ->FreeBlockOnClose = TRUE;
271
fm ->Size = size;
272
fm ->Pointer = 0;
273
iohandler -> ReportedSize = size;
274
break;
275
276
case 'w':
277
fm = (FILEMEM*) _cmsMallocZero(ContextID, sizeof(FILEMEM));
278
if (fm == NULL) goto Error;
279
280
if (Buffer == NULL) {
281
cmsSignalError(ContextID, cmsERROR_WRITE, "Couldn't write profile to NULL pointer");
282
goto Error;
283
}
284
285
fm ->Block = (cmsUInt8Number*) Buffer;
286
fm ->FreeBlockOnClose = FALSE;
287
fm ->Size = size;
288
fm ->Pointer = 0;
289
iohandler -> ReportedSize = 0;
290
break;
291
292
default:
293
cmsSignalError(ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown access mode '%c'", *AccessMode);
294
return NULL;
295
}
296
297
iohandler ->ContextID = ContextID;
298
iohandler ->stream = (void*) fm;
299
iohandler ->UsedSpace = 0;
300
iohandler ->PhysicalFile[0] = 0;
301
302
iohandler ->Read = MemoryRead;
303
iohandler ->Seek = MemorySeek;
304
iohandler ->Close = MemoryClose;
305
iohandler ->Tell = MemoryTell;
306
iohandler ->Write = MemoryWrite;
307
308
return iohandler;
309
310
Error:
311
if (fm) _cmsFree(ContextID, fm);
312
if (iohandler) _cmsFree(ContextID, iohandler);
313
return NULL;
314
}
315
316
// File-based stream -------------------------------------------------------
317
318
// Read count elements of size bytes each. Return number of elements read
319
static
320
cmsUInt32Number FileRead(cmsIOHANDLER* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count)
321
{
322
cmsUInt32Number nReaded = (cmsUInt32Number) fread(Buffer, size, count, (FILE*) iohandler->stream);
323
324
if (nReaded != count) {
325
cmsSignalError(iohandler ->ContextID, cmsERROR_FILE, "Read error. Got %d bytes, block should be of %d bytes", nReaded * size, count * size);
326
return 0;
327
}
328
329
return nReaded;
330
}
331
332
// Position file pointer in the file
333
static
334
cmsBool FileSeek(cmsIOHANDLER* iohandler, cmsUInt32Number offset)
335
{
336
if (fseek((FILE*) iohandler ->stream, (long) offset, SEEK_SET) != 0) {
337
338
cmsSignalError(iohandler ->ContextID, cmsERROR_FILE, "Seek error; probably corrupted file");
339
return FALSE;
340
}
341
342
return TRUE;
343
}
344
345
// Returns file pointer position or 0 on error, which is also a valid position.
346
static
347
cmsUInt32Number FileTell(cmsIOHANDLER* iohandler)
348
{
349
long t = ftell((FILE*)iohandler ->stream);
350
if (t == -1L) {
351
cmsSignalError(iohandler->ContextID, cmsERROR_FILE, "Tell error; probably corrupted file");
352
return 0;
353
}
354
355
return (cmsUInt32Number)t;
356
}
357
358
// Writes data to stream, also keeps used space for further reference. Returns TRUE on success, FALSE on error
359
static
360
cmsBool FileWrite(cmsIOHANDLER* iohandler, cmsUInt32Number size, const void* Buffer)
361
{
362
if (size == 0) return TRUE; // We allow to write 0 bytes, but nothing is written
363
364
iohandler->UsedSpace += size;
365
return (fwrite(Buffer, size, 1, (FILE*)iohandler->stream) == 1);
366
}
367
368
// Closes the file
369
static
370
cmsBool FileClose(cmsIOHANDLER* iohandler)
371
{
372
if (fclose((FILE*) iohandler ->stream) != 0) return FALSE;
373
_cmsFree(iohandler ->ContextID, iohandler);
374
return TRUE;
375
}
376
377
// Create a iohandler for disk based files.
378
cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromFile(cmsContext ContextID, const char* FileName, const char* AccessMode)
379
{
380
cmsIOHANDLER* iohandler = NULL;
381
FILE* fm = NULL;
382
cmsInt32Number fileLen;
383
char mode[4] = { 0,0,0,0 };
384
385
_cmsAssert(FileName != NULL);
386
_cmsAssert(AccessMode != NULL);
387
388
iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
389
if (iohandler == NULL) return NULL;
390
391
// Validate access mode
392
while (*AccessMode) {
393
394
switch (*AccessMode)
395
{
396
case 'r':
397
case 'w':
398
399
if (mode[0] == 0) {
400
mode[0] = *AccessMode;
401
mode[1] = 'b';
402
}
403
else {
404
_cmsFree(ContextID, iohandler);
405
cmsSignalError(ContextID, cmsERROR_FILE, "Access mode already specified '%c'", *AccessMode);
406
return NULL;
407
}
408
break;
409
410
// Close on exec. Not all runtime supports that. Up to the caller to decide.
411
case 'e':
412
mode[2] = 'e';
413
break;
414
415
default:
416
_cmsFree(ContextID, iohandler);
417
cmsSignalError(ContextID, cmsERROR_FILE, "Wrong access mode '%c'", *AccessMode);
418
return NULL;
419
}
420
421
AccessMode++;
422
}
423
424
switch (mode[0]) {
425
426
case 'r':
427
fm = fopen(FileName, mode);
428
if (fm == NULL) {
429
_cmsFree(ContextID, iohandler);
430
cmsSignalError(ContextID, cmsERROR_FILE, "File '%s' not found", FileName);
431
return NULL;
432
}
433
fileLen = (cmsInt32Number)cmsfilelength(fm);
434
if (fileLen < 0)
435
{
436
fclose(fm);
437
_cmsFree(ContextID, iohandler);
438
cmsSignalError(ContextID, cmsERROR_FILE, "Cannot get size of file '%s'", FileName);
439
return NULL;
440
}
441
iohandler -> ReportedSize = (cmsUInt32Number) fileLen;
442
break;
443
444
case 'w':
445
fm = fopen(FileName, mode);
446
if (fm == NULL) {
447
_cmsFree(ContextID, iohandler);
448
cmsSignalError(ContextID, cmsERROR_FILE, "Couldn't create '%s'", FileName);
449
return NULL;
450
}
451
iohandler -> ReportedSize = 0;
452
break;
453
454
default:
455
_cmsFree(ContextID, iohandler); // Would never reach
456
return NULL;
457
}
458
459
iohandler ->ContextID = ContextID;
460
iohandler ->stream = (void*) fm;
461
iohandler ->UsedSpace = 0;
462
463
// Keep track of the original file
464
strncpy(iohandler -> PhysicalFile, FileName, sizeof(iohandler -> PhysicalFile)-1);
465
iohandler -> PhysicalFile[sizeof(iohandler -> PhysicalFile)-1] = 0;
466
467
iohandler ->Read = FileRead;
468
iohandler ->Seek = FileSeek;
469
iohandler ->Close = FileClose;
470
iohandler ->Tell = FileTell;
471
iohandler ->Write = FileWrite;
472
473
return iohandler;
474
}
475
476
// Create a iohandler for stream based files
477
cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromStream(cmsContext ContextID, FILE* Stream)
478
{
479
cmsIOHANDLER* iohandler = NULL;
480
cmsInt32Number fileSize;
481
482
fileSize = (cmsInt32Number)cmsfilelength(Stream);
483
if (fileSize < 0)
484
{
485
cmsSignalError(ContextID, cmsERROR_FILE, "Cannot get size of stream");
486
return NULL;
487
}
488
489
iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
490
if (iohandler == NULL) return NULL;
491
492
iohandler -> ContextID = ContextID;
493
iohandler -> stream = (void*) Stream;
494
iohandler -> UsedSpace = 0;
495
iohandler -> ReportedSize = (cmsUInt32Number) fileSize;
496
iohandler -> PhysicalFile[0] = 0;
497
498
iohandler ->Read = FileRead;
499
iohandler ->Seek = FileSeek;
500
iohandler ->Close = FileClose;
501
iohandler ->Tell = FileTell;
502
iohandler ->Write = FileWrite;
503
504
return iohandler;
505
}
506
507
508
509
// Close an open IO handler
510
cmsBool CMSEXPORT cmsCloseIOhandler(cmsIOHANDLER* io)
511
{
512
return io -> Close(io);
513
}
514
515
// -------------------------------------------------------------------------------------------------------
516
517
cmsIOHANDLER* CMSEXPORT cmsGetProfileIOhandler(cmsHPROFILE hProfile)
518
{
519
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*)hProfile;
520
521
if (Icc == NULL) return NULL;
522
return Icc->IOhandler;
523
}
524
525
// Creates an empty structure holding all required parameters
526
cmsHPROFILE CMSEXPORT cmsCreateProfilePlaceholder(cmsContext ContextID)
527
{
528
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) _cmsMallocZero(ContextID, sizeof(_cmsICCPROFILE));
529
if (Icc == NULL) return NULL;
530
531
Icc ->ContextID = ContextID;
532
533
// Set it to empty
534
Icc -> TagCount = 0;
535
536
// Set default version
537
Icc ->Version = 0x02100000;
538
539
// Set default CMM (that's me!)
540
Icc ->CMM = lcmsSignature;
541
542
// Set default creator
543
// Created by LittleCMS (that's me!)
544
Icc ->creator = lcmsSignature;
545
546
// Set default platform
547
#ifdef CMS_IS_WINDOWS_
548
Icc ->platform = cmsSigMicrosoft;
549
#else
550
Icc ->platform = cmsSigMacintosh;
551
#endif
552
553
// Set default device class
554
Icc->DeviceClass = cmsSigDisplayClass;
555
556
// Set creation date/time
557
if (!_cmsGetTime(&Icc->Created))
558
goto Error;
559
560
// Create a mutex if the user provided proper plugin. NULL otherwise
561
Icc ->UsrMutex = _cmsCreateMutex(ContextID);
562
563
// Return the handle
564
return (cmsHPROFILE) Icc;
565
566
Error:
567
_cmsFree(ContextID, Icc);
568
return NULL;
569
}
570
571
cmsContext CMSEXPORT cmsGetProfileContextID(cmsHPROFILE hProfile)
572
{
573
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
574
575
if (Icc == NULL) return NULL;
576
return Icc -> ContextID;
577
}
578
579
580
// Return the number of tags
581
cmsInt32Number CMSEXPORT cmsGetTagCount(cmsHPROFILE hProfile)
582
{
583
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
584
if (Icc == NULL) return -1;
585
586
return (cmsInt32Number) Icc->TagCount;
587
}
588
589
// Return the tag signature of a given tag number
590
cmsTagSignature CMSEXPORT cmsGetTagSignature(cmsHPROFILE hProfile, cmsUInt32Number n)
591
{
592
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
593
594
if (n > Icc->TagCount) return (cmsTagSignature) 0; // Mark as not available
595
if (n >= MAX_TABLE_TAG) return (cmsTagSignature) 0; // As double check
596
597
return Icc ->TagNames[n];
598
}
599
600
601
static
602
int SearchOneTag(_cmsICCPROFILE* Profile, cmsTagSignature sig)
603
{
604
int i;
605
606
for (i=0; i < (int) Profile -> TagCount; i++) {
607
608
if (sig == Profile -> TagNames[i])
609
return i;
610
}
611
612
return -1;
613
}
614
615
// Search for a specific tag in tag dictionary. Returns position or -1 if tag not found.
616
// If followlinks is turned on, then the position of the linked tag is returned
617
int _cmsSearchTag(_cmsICCPROFILE* Icc, cmsTagSignature sig, cmsBool lFollowLinks)
618
{
619
int n;
620
cmsTagSignature LinkedSig;
621
622
do {
623
624
// Search for given tag in ICC profile directory
625
n = SearchOneTag(Icc, sig);
626
if (n < 0)
627
return -1; // Not found
628
629
if (!lFollowLinks)
630
return n; // Found, don't follow links
631
632
// Is this a linked tag?
633
LinkedSig = Icc ->TagLinked[n];
634
635
// Yes, follow link
636
if (LinkedSig != (cmsTagSignature) 0) {
637
sig = LinkedSig;
638
}
639
640
} while (LinkedSig != (cmsTagSignature) 0);
641
642
return n;
643
}
644
645
// Deletes a tag entry
646
647
static
648
void _cmsDeleteTagByPos(_cmsICCPROFILE* Icc, int i)
649
{
650
_cmsAssert(Icc != NULL);
651
_cmsAssert(i >= 0);
652
653
654
if (Icc -> TagPtrs[i] != NULL) {
655
656
// Free previous version
657
if (Icc ->TagSaveAsRaw[i]) {
658
_cmsFree(Icc ->ContextID, Icc ->TagPtrs[i]);
659
}
660
else {
661
cmsTagTypeHandler* TypeHandler = Icc ->TagTypeHandlers[i];
662
663
if (TypeHandler != NULL) {
664
665
cmsTagTypeHandler LocalTypeHandler = *TypeHandler;
666
LocalTypeHandler.ContextID = Icc ->ContextID; // As an additional parameter
667
LocalTypeHandler.ICCVersion = Icc ->Version;
668
LocalTypeHandler.FreePtr(&LocalTypeHandler, Icc -> TagPtrs[i]);
669
Icc ->TagPtrs[i] = NULL;
670
}
671
}
672
673
}
674
}
675
676
677
// Creates a new tag entry
678
static
679
cmsBool _cmsNewTag(_cmsICCPROFILE* Icc, cmsTagSignature sig, int* NewPos)
680
{
681
int i;
682
683
// Search for the tag
684
i = _cmsSearchTag(Icc, sig, FALSE);
685
if (i >= 0) {
686
687
// Already exists? delete it
688
_cmsDeleteTagByPos(Icc, i);
689
*NewPos = i;
690
}
691
else {
692
693
// No, make a new one
694
if (Icc -> TagCount >= MAX_TABLE_TAG) {
695
cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", MAX_TABLE_TAG);
696
return FALSE;
697
}
698
699
*NewPos = (int) Icc ->TagCount;
700
Icc -> TagCount++;
701
}
702
703
return TRUE;
704
}
705
706
707
// Check existence
708
cmsBool CMSEXPORT cmsIsTag(cmsHPROFILE hProfile, cmsTagSignature sig)
709
{
710
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) (void*) hProfile;
711
return _cmsSearchTag(Icc, sig, FALSE) >= 0;
712
}
713
714
715
716
// Checks for link compatibility
717
static
718
cmsBool CompatibleTypes(const cmsTagDescriptor* desc1, const cmsTagDescriptor* desc2)
719
{
720
cmsUInt32Number i;
721
722
if (desc1 == NULL || desc2 == NULL) return FALSE;
723
724
if (desc1->nSupportedTypes != desc2->nSupportedTypes) return FALSE;
725
if (desc1->ElemCount != desc2->ElemCount) return FALSE;
726
727
for (i = 0; i < desc1->nSupportedTypes; i++)
728
{
729
if (desc1->SupportedTypes[i] != desc2->SupportedTypes[i]) return FALSE;
730
}
731
732
return TRUE;
733
}
734
735
// Enforces that the profile version is per. spec.
736
// Operates on the big endian bytes from the profile.
737
// Called before converting to platform endianness.
738
// Byte 0 is BCD major version, so max 9.
739
// Byte 1 is 2 BCD digits, one per nibble.
740
// Reserved bytes 2 & 3 must be 0.
741
static
742
cmsUInt32Number _validatedVersion(cmsUInt32Number DWord)
743
{
744
cmsUInt8Number* pByte = (cmsUInt8Number*) &DWord;
745
cmsUInt8Number temp1;
746
cmsUInt8Number temp2;
747
748
if (*pByte > 0x09) *pByte = (cmsUInt8Number) 0x09;
749
temp1 = (cmsUInt8Number) (*(pByte+1) & 0xf0);
750
temp2 = (cmsUInt8Number) (*(pByte+1) & 0x0f);
751
if (temp1 > 0x90U) temp1 = 0x90U;
752
if (temp2 > 0x09U) temp2 = 0x09U;
753
*(pByte+1) = (cmsUInt8Number)(temp1 | temp2);
754
*(pByte+2) = (cmsUInt8Number)0;
755
*(pByte+3) = (cmsUInt8Number)0;
756
757
return DWord;
758
}
759
760
// Check device class
761
static
762
cmsBool validDeviceClass(cmsProfileClassSignature cl)
763
{
764
if ((int)cl == 0) return TRUE; // We allow zero because older lcms versions defaulted to that.
765
766
switch (cl)
767
{
768
case cmsSigInputClass:
769
case cmsSigDisplayClass:
770
case cmsSigOutputClass:
771
case cmsSigLinkClass:
772
case cmsSigAbstractClass:
773
case cmsSigColorSpaceClass:
774
case cmsSigNamedColorClass:
775
return TRUE;
776
777
default:
778
return FALSE;
779
}
780
781
}
782
783
// Read profile header and validate it
784
cmsBool _cmsReadHeader(_cmsICCPROFILE* Icc)
785
{
786
cmsTagEntry Tag;
787
cmsICCHeader Header;
788
cmsUInt32Number i, j;
789
cmsUInt32Number HeaderSize;
790
cmsIOHANDLER* io = Icc ->IOhandler;
791
cmsUInt32Number TagCount;
792
793
794
// Read the header
795
if (io -> Read(io, &Header, sizeof(cmsICCHeader), 1) != 1) {
796
return FALSE;
797
}
798
799
// Validate file as an ICC profile
800
if (_cmsAdjustEndianess32(Header.magic) != cmsMagicNumber) {
801
cmsSignalError(Icc ->ContextID, cmsERROR_BAD_SIGNATURE, "not an ICC profile, invalid signature");
802
return FALSE;
803
}
804
805
// Adjust endianness of the used parameters
806
Icc -> CMM = _cmsAdjustEndianess32(Header.cmmId);
807
Icc -> DeviceClass = (cmsProfileClassSignature) _cmsAdjustEndianess32(Header.deviceClass);
808
Icc -> ColorSpace = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Header.colorSpace);
809
Icc -> PCS = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Header.pcs);
810
811
Icc -> RenderingIntent = _cmsAdjustEndianess32(Header.renderingIntent);
812
Icc -> platform = (cmsPlatformSignature)_cmsAdjustEndianess32(Header.platform);
813
Icc -> flags = _cmsAdjustEndianess32(Header.flags);
814
Icc -> manufacturer = _cmsAdjustEndianess32(Header.manufacturer);
815
Icc -> model = _cmsAdjustEndianess32(Header.model);
816
Icc -> creator = _cmsAdjustEndianess32(Header.creator);
817
818
_cmsAdjustEndianess64(&Icc -> attributes, &Header.attributes);
819
Icc -> Version = _cmsAdjustEndianess32(_validatedVersion(Header.version));
820
821
if (Icc->Version > 0x5000000) {
822
cmsSignalError(Icc->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported profile version '0x%x'", Icc->Version);
823
return FALSE;
824
}
825
826
if (!validDeviceClass(Icc->DeviceClass)) {
827
cmsSignalError(Icc->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported device class '0x%x'", Icc->DeviceClass);
828
return FALSE;
829
}
830
831
// Get size as reported in header
832
HeaderSize = _cmsAdjustEndianess32(Header.size);
833
834
// Make sure HeaderSize is lower than profile size
835
if (HeaderSize >= Icc ->IOhandler ->ReportedSize)
836
HeaderSize = Icc ->IOhandler ->ReportedSize;
837
838
839
// Get creation date/time
840
_cmsDecodeDateTimeNumber(&Header.date, &Icc ->Created);
841
842
// The profile ID are 32 raw bytes
843
memmove(Icc ->ProfileID.ID32, Header.profileID.ID32, 16);
844
845
846
// Read tag directory
847
if (!_cmsReadUInt32Number(io, &TagCount)) return FALSE;
848
if (TagCount > MAX_TABLE_TAG) {
849
850
cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", TagCount);
851
return FALSE;
852
}
853
854
855
// Read tag directory
856
Icc -> TagCount = 0;
857
for (i=0; i < TagCount; i++) {
858
859
if (!_cmsReadUInt32Number(io, (cmsUInt32Number *) &Tag.sig)) return FALSE;
860
if (!_cmsReadUInt32Number(io, &Tag.offset)) return FALSE;
861
if (!_cmsReadUInt32Number(io, &Tag.size)) return FALSE;
862
863
// Perform some sanity check. Offset + size should fall inside file.
864
if (Tag.size == 0 || Tag.offset == 0) continue;
865
if (Tag.offset + Tag.size > HeaderSize ||
866
Tag.offset + Tag.size < Tag.offset)
867
continue;
868
869
Icc -> TagNames[Icc ->TagCount] = Tag.sig;
870
Icc -> TagOffsets[Icc ->TagCount] = Tag.offset;
871
Icc -> TagSizes[Icc ->TagCount] = Tag.size;
872
873
// Search for links
874
for (j=0; j < Icc ->TagCount; j++) {
875
876
if ((Icc ->TagOffsets[j] == Tag.offset) &&
877
(Icc ->TagSizes[j] == Tag.size)) {
878
879
// Check types.
880
if (CompatibleTypes(_cmsGetTagDescriptor(Icc->ContextID, Icc->TagNames[j]),
881
_cmsGetTagDescriptor(Icc->ContextID, Tag.sig))) {
882
883
Icc->TagLinked[Icc->TagCount] = Icc->TagNames[j];
884
}
885
}
886
887
}
888
889
Icc ->TagCount++;
890
}
891
892
893
for (i = 0; i < Icc->TagCount; i++) {
894
for (j = 0; j < Icc->TagCount; j++) {
895
896
// Tags cannot be duplicate
897
if ((i != j) && (Icc->TagNames[i] == Icc->TagNames[j])) {
898
cmsSignalError(Icc->ContextID, cmsERROR_RANGE, "Duplicate tag found");
899
return FALSE;
900
}
901
902
}
903
}
904
905
return TRUE;
906
}
907
908
// Saves profile header
909
cmsBool _cmsWriteHeader(_cmsICCPROFILE* Icc, cmsUInt32Number UsedSpace)
910
{
911
cmsICCHeader Header;
912
cmsUInt32Number i;
913
cmsTagEntry Tag;
914
cmsUInt32Number Count;
915
916
Header.size = _cmsAdjustEndianess32(UsedSpace);
917
Header.cmmId = _cmsAdjustEndianess32(Icc ->CMM);
918
Header.version = _cmsAdjustEndianess32(Icc ->Version);
919
920
Header.deviceClass = (cmsProfileClassSignature) _cmsAdjustEndianess32(Icc -> DeviceClass);
921
Header.colorSpace = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Icc -> ColorSpace);
922
Header.pcs = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Icc -> PCS);
923
924
// NOTE: in v4 Timestamp must be in UTC rather than in local time
925
_cmsEncodeDateTimeNumber(&Header.date, &Icc ->Created);
926
927
Header.magic = _cmsAdjustEndianess32(cmsMagicNumber);
928
929
Header.platform = (cmsPlatformSignature) _cmsAdjustEndianess32(Icc -> platform);
930
931
Header.flags = _cmsAdjustEndianess32(Icc -> flags);
932
Header.manufacturer = _cmsAdjustEndianess32(Icc -> manufacturer);
933
Header.model = _cmsAdjustEndianess32(Icc -> model);
934
935
_cmsAdjustEndianess64(&Header.attributes, &Icc -> attributes);
936
937
// Rendering intent in the header (for embedded profiles)
938
Header.renderingIntent = _cmsAdjustEndianess32(Icc -> RenderingIntent);
939
940
// Illuminant is always D50
941
Header.illuminant.X = (cmsS15Fixed16Number) _cmsAdjustEndianess32((cmsUInt32Number) _cmsDoubleTo15Fixed16(cmsD50_XYZ()->X));
942
Header.illuminant.Y = (cmsS15Fixed16Number) _cmsAdjustEndianess32((cmsUInt32Number) _cmsDoubleTo15Fixed16(cmsD50_XYZ()->Y));
943
Header.illuminant.Z = (cmsS15Fixed16Number) _cmsAdjustEndianess32((cmsUInt32Number) _cmsDoubleTo15Fixed16(cmsD50_XYZ()->Z));
944
945
Header.creator = _cmsAdjustEndianess32(Icc ->creator);
946
947
memset(&Header.reserved, 0, sizeof(Header.reserved));
948
949
// Set profile ID. Endianness is always big endian
950
memmove(&Header.profileID, &Icc ->ProfileID, 16);
951
952
// Dump the header
953
if (!Icc -> IOhandler->Write(Icc->IOhandler, sizeof(cmsICCHeader), &Header)) return FALSE;
954
955
// Saves Tag directory
956
957
// Get true count
958
Count = 0;
959
for (i=0; i < Icc -> TagCount; i++) {
960
if (Icc ->TagNames[i] != (cmsTagSignature) 0)
961
Count++;
962
}
963
964
// Store number of tags
965
if (!_cmsWriteUInt32Number(Icc ->IOhandler, Count)) return FALSE;
966
967
for (i=0; i < Icc -> TagCount; i++) {
968
969
if (Icc ->TagNames[i] == (cmsTagSignature) 0) continue; // It is just a placeholder
970
971
Tag.sig = (cmsTagSignature) _cmsAdjustEndianess32((cmsUInt32Number) Icc -> TagNames[i]);
972
Tag.offset = _cmsAdjustEndianess32((cmsUInt32Number) Icc -> TagOffsets[i]);
973
Tag.size = _cmsAdjustEndianess32((cmsUInt32Number) Icc -> TagSizes[i]);
974
975
if (!Icc ->IOhandler -> Write(Icc-> IOhandler, sizeof(cmsTagEntry), &Tag)) return FALSE;
976
}
977
978
return TRUE;
979
}
980
981
// ----------------------------------------------------------------------- Set/Get several struct members
982
983
984
cmsUInt32Number CMSEXPORT cmsGetHeaderRenderingIntent(cmsHPROFILE hProfile)
985
{
986
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
987
return Icc -> RenderingIntent;
988
}
989
990
void CMSEXPORT cmsSetHeaderRenderingIntent(cmsHPROFILE hProfile, cmsUInt32Number RenderingIntent)
991
{
992
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
993
Icc -> RenderingIntent = RenderingIntent;
994
}
995
996
cmsUInt32Number CMSEXPORT cmsGetHeaderFlags(cmsHPROFILE hProfile)
997
{
998
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
999
return (cmsUInt32Number) Icc -> flags;
1000
}
1001
1002
void CMSEXPORT cmsSetHeaderFlags(cmsHPROFILE hProfile, cmsUInt32Number Flags)
1003
{
1004
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1005
Icc -> flags = (cmsUInt32Number) Flags;
1006
}
1007
1008
cmsUInt32Number CMSEXPORT cmsGetHeaderManufacturer(cmsHPROFILE hProfile)
1009
{
1010
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1011
return Icc ->manufacturer;
1012
}
1013
1014
void CMSEXPORT cmsSetHeaderManufacturer(cmsHPROFILE hProfile, cmsUInt32Number manufacturer)
1015
{
1016
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1017
Icc -> manufacturer = manufacturer;
1018
}
1019
1020
cmsUInt32Number CMSEXPORT cmsGetHeaderCreator(cmsHPROFILE hProfile)
1021
{
1022
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1023
return Icc ->creator;
1024
}
1025
1026
cmsUInt32Number CMSEXPORT cmsGetHeaderModel(cmsHPROFILE hProfile)
1027
{
1028
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1029
return Icc ->model;
1030
}
1031
1032
void CMSEXPORT cmsSetHeaderModel(cmsHPROFILE hProfile, cmsUInt32Number model)
1033
{
1034
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1035
Icc -> model = model;
1036
}
1037
1038
void CMSEXPORT cmsGetHeaderAttributes(cmsHPROFILE hProfile, cmsUInt64Number* Flags)
1039
{
1040
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1041
memmove(Flags, &Icc -> attributes, sizeof(cmsUInt64Number));
1042
}
1043
1044
void CMSEXPORT cmsSetHeaderAttributes(cmsHPROFILE hProfile, cmsUInt64Number Flags)
1045
{
1046
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1047
memmove(&Icc -> attributes, &Flags, sizeof(cmsUInt64Number));
1048
}
1049
1050
void CMSEXPORT cmsGetHeaderProfileID(cmsHPROFILE hProfile, cmsUInt8Number* ProfileID)
1051
{
1052
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1053
memmove(ProfileID, Icc ->ProfileID.ID8, 16);
1054
}
1055
1056
void CMSEXPORT cmsSetHeaderProfileID(cmsHPROFILE hProfile, cmsUInt8Number* ProfileID)
1057
{
1058
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1059
memmove(&Icc -> ProfileID, ProfileID, 16);
1060
}
1061
1062
cmsBool CMSEXPORT cmsGetHeaderCreationDateTime(cmsHPROFILE hProfile, struct tm *Dest)
1063
{
1064
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1065
memmove(Dest, &Icc ->Created, sizeof(struct tm));
1066
return TRUE;
1067
}
1068
1069
cmsColorSpaceSignature CMSEXPORT cmsGetPCS(cmsHPROFILE hProfile)
1070
{
1071
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1072
return Icc -> PCS;
1073
}
1074
1075
void CMSEXPORT cmsSetPCS(cmsHPROFILE hProfile, cmsColorSpaceSignature pcs)
1076
{
1077
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1078
Icc -> PCS = pcs;
1079
}
1080
1081
cmsColorSpaceSignature CMSEXPORT cmsGetColorSpace(cmsHPROFILE hProfile)
1082
{
1083
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1084
return Icc -> ColorSpace;
1085
}
1086
1087
void CMSEXPORT cmsSetColorSpace(cmsHPROFILE hProfile, cmsColorSpaceSignature sig)
1088
{
1089
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1090
Icc -> ColorSpace = sig;
1091
}
1092
1093
cmsProfileClassSignature CMSEXPORT cmsGetDeviceClass(cmsHPROFILE hProfile)
1094
{
1095
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1096
return Icc -> DeviceClass;
1097
}
1098
1099
void CMSEXPORT cmsSetDeviceClass(cmsHPROFILE hProfile, cmsProfileClassSignature sig)
1100
{
1101
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1102
Icc -> DeviceClass = sig;
1103
}
1104
1105
cmsUInt32Number CMSEXPORT cmsGetEncodedICCversion(cmsHPROFILE hProfile)
1106
{
1107
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1108
return Icc -> Version;
1109
}
1110
1111
void CMSEXPORT cmsSetEncodedICCversion(cmsHPROFILE hProfile, cmsUInt32Number Version)
1112
{
1113
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1114
Icc -> Version = Version;
1115
}
1116
1117
// Get an hexadecimal number with same digits as v
1118
static
1119
cmsUInt32Number BaseToBase(cmsUInt32Number in, int BaseIn, int BaseOut)
1120
{
1121
char Buff[100];
1122
int i, len;
1123
cmsUInt32Number out;
1124
1125
for (len=0; in > 0 && len < 100; len++) {
1126
1127
Buff[len] = (char) (in % BaseIn);
1128
in /= BaseIn;
1129
}
1130
1131
for (i=len-1, out=0; i >= 0; --i) {
1132
out = out * BaseOut + Buff[i];
1133
}
1134
1135
return out;
1136
}
1137
1138
void CMSEXPORT cmsSetProfileVersion(cmsHPROFILE hProfile, cmsFloat64Number Version)
1139
{
1140
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1141
1142
// 4.2 -> 0x4200000
1143
1144
Icc -> Version = BaseToBase((cmsUInt32Number) floor(Version * 100.0 + 0.5), 10, 16) << 16;
1145
}
1146
1147
cmsFloat64Number CMSEXPORT cmsGetProfileVersion(cmsHPROFILE hProfile)
1148
{
1149
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1150
cmsUInt32Number n = Icc -> Version >> 16;
1151
1152
return BaseToBase(n, 16, 10) / 100.0;
1153
}
1154
// --------------------------------------------------------------------------------------------------------------
1155
1156
1157
// Create profile from IOhandler
1158
cmsHPROFILE CMSEXPORT cmsOpenProfileFromIOhandlerTHR(cmsContext ContextID, cmsIOHANDLER* io)
1159
{
1160
_cmsICCPROFILE* NewIcc;
1161
cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
1162
1163
if (hEmpty == NULL) return NULL;
1164
1165
NewIcc = (_cmsICCPROFILE*) hEmpty;
1166
1167
NewIcc ->IOhandler = io;
1168
if (!_cmsReadHeader(NewIcc)) goto Error;
1169
return hEmpty;
1170
1171
Error:
1172
cmsCloseProfile(hEmpty);
1173
return NULL;
1174
}
1175
1176
// Create profile from IOhandler
1177
cmsHPROFILE CMSEXPORT cmsOpenProfileFromIOhandler2THR(cmsContext ContextID, cmsIOHANDLER* io, cmsBool write)
1178
{
1179
_cmsICCPROFILE* NewIcc;
1180
cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
1181
1182
if (hEmpty == NULL) return NULL;
1183
1184
NewIcc = (_cmsICCPROFILE*) hEmpty;
1185
1186
NewIcc ->IOhandler = io;
1187
if (write) {
1188
1189
NewIcc -> IsWrite = TRUE;
1190
return hEmpty;
1191
}
1192
1193
if (!_cmsReadHeader(NewIcc)) goto Error;
1194
return hEmpty;
1195
1196
Error:
1197
cmsCloseProfile(hEmpty);
1198
return NULL;
1199
}
1200
1201
1202
// Create profile from disk file
1203
cmsHPROFILE CMSEXPORT cmsOpenProfileFromFileTHR(cmsContext ContextID, const char *lpFileName, const char *sAccess)
1204
{
1205
_cmsICCPROFILE* NewIcc;
1206
cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
1207
1208
if (hEmpty == NULL) return NULL;
1209
1210
NewIcc = (_cmsICCPROFILE*) hEmpty;
1211
1212
NewIcc ->IOhandler = cmsOpenIOhandlerFromFile(ContextID, lpFileName, sAccess);
1213
if (NewIcc ->IOhandler == NULL) goto Error;
1214
1215
if (*sAccess == 'W' || *sAccess == 'w') {
1216
1217
NewIcc -> IsWrite = TRUE;
1218
1219
return hEmpty;
1220
}
1221
1222
if (!_cmsReadHeader(NewIcc)) goto Error;
1223
return hEmpty;
1224
1225
Error:
1226
cmsCloseProfile(hEmpty);
1227
return NULL;
1228
}
1229
1230
1231
cmsHPROFILE CMSEXPORT cmsOpenProfileFromFile(const char *ICCProfile, const char *sAccess)
1232
{
1233
return cmsOpenProfileFromFileTHR(NULL, ICCProfile, sAccess);
1234
}
1235
1236
1237
cmsHPROFILE CMSEXPORT cmsOpenProfileFromStreamTHR(cmsContext ContextID, FILE* ICCProfile, const char *sAccess)
1238
{
1239
_cmsICCPROFILE* NewIcc;
1240
cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
1241
1242
if (hEmpty == NULL) return NULL;
1243
1244
NewIcc = (_cmsICCPROFILE*) hEmpty;
1245
1246
NewIcc ->IOhandler = cmsOpenIOhandlerFromStream(ContextID, ICCProfile);
1247
if (NewIcc ->IOhandler == NULL) goto Error;
1248
1249
if (*sAccess == 'w') {
1250
1251
NewIcc -> IsWrite = TRUE;
1252
return hEmpty;
1253
}
1254
1255
if (!_cmsReadHeader(NewIcc)) goto Error;
1256
return hEmpty;
1257
1258
Error:
1259
cmsCloseProfile(hEmpty);
1260
return NULL;
1261
1262
}
1263
1264
cmsHPROFILE CMSEXPORT cmsOpenProfileFromStream(FILE* ICCProfile, const char *sAccess)
1265
{
1266
return cmsOpenProfileFromStreamTHR(NULL, ICCProfile, sAccess);
1267
}
1268
1269
1270
// Open from memory block
1271
cmsHPROFILE CMSEXPORT cmsOpenProfileFromMemTHR(cmsContext ContextID, const void* MemPtr, cmsUInt32Number dwSize)
1272
{
1273
_cmsICCPROFILE* NewIcc;
1274
cmsHPROFILE hEmpty;
1275
1276
hEmpty = cmsCreateProfilePlaceholder(ContextID);
1277
if (hEmpty == NULL) return NULL;
1278
1279
NewIcc = (_cmsICCPROFILE*) hEmpty;
1280
1281
// Ok, in this case const void* is casted to void* just because open IO handler
1282
// shares read and writing modes. Don't abuse this feature!
1283
NewIcc ->IOhandler = cmsOpenIOhandlerFromMem(ContextID, (void*) MemPtr, dwSize, "r");
1284
if (NewIcc ->IOhandler == NULL) goto Error;
1285
1286
if (!_cmsReadHeader(NewIcc)) goto Error;
1287
1288
return hEmpty;
1289
1290
Error:
1291
cmsCloseProfile(hEmpty);
1292
return NULL;
1293
}
1294
1295
cmsHPROFILE CMSEXPORT cmsOpenProfileFromMem(const void* MemPtr, cmsUInt32Number dwSize)
1296
{
1297
return cmsOpenProfileFromMemTHR(NULL, MemPtr, dwSize);
1298
}
1299
1300
1301
1302
// Dump tag contents. If the profile is being modified, untouched tags are copied from FileOrig
1303
static
1304
cmsBool SaveTags(_cmsICCPROFILE* Icc, _cmsICCPROFILE* FileOrig)
1305
{
1306
cmsUInt8Number* Data;
1307
cmsUInt32Number i;
1308
cmsUInt32Number Begin;
1309
cmsIOHANDLER* io = Icc ->IOhandler;
1310
cmsTagDescriptor* TagDescriptor;
1311
cmsTagTypeSignature TypeBase;
1312
cmsTagTypeSignature Type;
1313
cmsTagTypeHandler* TypeHandler;
1314
cmsFloat64Number Version = cmsGetProfileVersion((cmsHPROFILE) Icc);
1315
cmsTagTypeHandler LocalTypeHandler;
1316
1317
for (i=0; i < Icc -> TagCount; i++) {
1318
1319
if (Icc ->TagNames[i] == (cmsTagSignature) 0) continue;
1320
1321
// Linked tags are not written
1322
if (Icc ->TagLinked[i] != (cmsTagSignature) 0) continue;
1323
1324
Icc -> TagOffsets[i] = Begin = io ->UsedSpace;
1325
1326
Data = (cmsUInt8Number*) Icc -> TagPtrs[i];
1327
1328
if (!Data) {
1329
1330
// Reach here if we are copying a tag from a disk-based ICC profile which has not been modified by user.
1331
// In this case a blind copy of the block data is performed
1332
if (FileOrig != NULL && Icc -> TagOffsets[i]) {
1333
1334
if (FileOrig->IOhandler != NULL)
1335
{
1336
cmsUInt32Number TagSize = FileOrig->TagSizes[i];
1337
cmsUInt32Number TagOffset = FileOrig->TagOffsets[i];
1338
void* Mem;
1339
1340
if (!FileOrig->IOhandler->Seek(FileOrig->IOhandler, TagOffset)) return FALSE;
1341
1342
Mem = _cmsMalloc(Icc->ContextID, TagSize);
1343
if (Mem == NULL) return FALSE;
1344
1345
if (FileOrig->IOhandler->Read(FileOrig->IOhandler, Mem, TagSize, 1) != 1) return FALSE;
1346
if (!io->Write(io, TagSize, Mem)) return FALSE;
1347
_cmsFree(Icc->ContextID, Mem);
1348
1349
Icc->TagSizes[i] = (io->UsedSpace - Begin);
1350
1351
1352
// Align to 32 bit boundary.
1353
if (!_cmsWriteAlignment(io))
1354
return FALSE;
1355
}
1356
}
1357
1358
continue;
1359
}
1360
1361
1362
// Should this tag be saved as RAW? If so, tagsizes should be specified in advance (no further cooking is done)
1363
if (Icc ->TagSaveAsRaw[i]) {
1364
1365
if (io -> Write(io, Icc ->TagSizes[i], Data) != 1) return FALSE;
1366
}
1367
else {
1368
1369
// Search for support on this tag
1370
TagDescriptor = _cmsGetTagDescriptor(Icc-> ContextID, Icc -> TagNames[i]);
1371
if (TagDescriptor == NULL) continue; // Unsupported, ignore it
1372
1373
if (TagDescriptor ->DecideType != NULL) {
1374
1375
Type = TagDescriptor ->DecideType(Version, Data);
1376
}
1377
else {
1378
1379
Type = TagDescriptor ->SupportedTypes[0];
1380
}
1381
1382
TypeHandler = _cmsGetTagTypeHandler(Icc->ContextID, Type);
1383
1384
if (TypeHandler == NULL) {
1385
cmsSignalError(Icc ->ContextID, cmsERROR_INTERNAL, "(Internal) no handler for tag %x", Icc -> TagNames[i]);
1386
continue;
1387
}
1388
1389
TypeBase = TypeHandler ->Signature;
1390
if (!_cmsWriteTypeBase(io, TypeBase))
1391
return FALSE;
1392
1393
LocalTypeHandler = *TypeHandler;
1394
LocalTypeHandler.ContextID = Icc ->ContextID;
1395
LocalTypeHandler.ICCVersion = Icc ->Version;
1396
if (!LocalTypeHandler.WritePtr(&LocalTypeHandler, io, Data, TagDescriptor ->ElemCount)) {
1397
1398
char String[5];
1399
1400
_cmsTagSignature2String(String, (cmsTagSignature) TypeBase);
1401
cmsSignalError(Icc ->ContextID, cmsERROR_WRITE, "Couldn't write type '%s'", String);
1402
return FALSE;
1403
}
1404
}
1405
1406
1407
Icc -> TagSizes[i] = (io ->UsedSpace - Begin);
1408
1409
// Align to 32 bit boundary.
1410
if (! _cmsWriteAlignment(io))
1411
return FALSE;
1412
}
1413
1414
1415
return TRUE;
1416
}
1417
1418
1419
// Fill the offset and size fields for all linked tags
1420
static
1421
cmsBool SetLinks( _cmsICCPROFILE* Icc)
1422
{
1423
cmsUInt32Number i;
1424
1425
for (i=0; i < Icc -> TagCount; i++) {
1426
1427
cmsTagSignature lnk = Icc ->TagLinked[i];
1428
if (lnk != (cmsTagSignature) 0) {
1429
1430
int j = _cmsSearchTag(Icc, lnk, FALSE);
1431
if (j >= 0) {
1432
1433
Icc ->TagOffsets[i] = Icc ->TagOffsets[j];
1434
Icc ->TagSizes[i] = Icc ->TagSizes[j];
1435
}
1436
1437
}
1438
}
1439
1440
return TRUE;
1441
}
1442
1443
// Low-level save to IOHANDLER. It returns the number of bytes used to
1444
// store the profile, or zero on error. io may be NULL and in this case
1445
// no data is written--only sizes are calculated
1446
cmsUInt32Number CMSEXPORT cmsSaveProfileToIOhandler(cmsHPROFILE hProfile, cmsIOHANDLER* io)
1447
{
1448
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1449
_cmsICCPROFILE Keep;
1450
cmsIOHANDLER* PrevIO = NULL;
1451
cmsUInt32Number UsedSpace;
1452
cmsContext ContextID;
1453
1454
_cmsAssert(hProfile != NULL);
1455
1456
if (!_cmsLockMutex(Icc->ContextID, Icc->UsrMutex)) return 0;
1457
memmove(&Keep, Icc, sizeof(_cmsICCPROFILE));
1458
1459
ContextID = cmsGetProfileContextID(hProfile);
1460
PrevIO = Icc ->IOhandler = cmsOpenIOhandlerFromNULL(ContextID);
1461
if (PrevIO == NULL) {
1462
_cmsUnlockMutex(Icc->ContextID, Icc->UsrMutex);
1463
return 0;
1464
}
1465
1466
// Pass #1 does compute offsets
1467
1468
if (!_cmsWriteHeader(Icc, 0)) goto Error;
1469
if (!SaveTags(Icc, &Keep)) goto Error;
1470
1471
UsedSpace = PrevIO ->UsedSpace;
1472
1473
// Pass #2 does save to iohandler
1474
1475
if (io != NULL) {
1476
1477
Icc ->IOhandler = io;
1478
if (!SetLinks(Icc)) goto Error;
1479
if (!_cmsWriteHeader(Icc, UsedSpace)) goto Error;
1480
if (!SaveTags(Icc, &Keep)) goto Error;
1481
}
1482
1483
memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
1484
if (!cmsCloseIOhandler(PrevIO))
1485
UsedSpace = 0; // As a error marker
1486
1487
_cmsUnlockMutex(Icc->ContextID, Icc->UsrMutex);
1488
1489
return UsedSpace;
1490
1491
1492
Error:
1493
cmsCloseIOhandler(PrevIO);
1494
memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
1495
_cmsUnlockMutex(Icc->ContextID, Icc->UsrMutex);
1496
1497
return 0;
1498
}
1499
1500
1501
// Low-level save to disk.
1502
cmsBool CMSEXPORT cmsSaveProfileToFile(cmsHPROFILE hProfile, const char* FileName)
1503
{
1504
cmsContext ContextID = cmsGetProfileContextID(hProfile);
1505
cmsIOHANDLER* io = cmsOpenIOhandlerFromFile(ContextID, FileName, "w");
1506
cmsBool rc;
1507
1508
if (io == NULL) return FALSE;
1509
1510
rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
1511
rc &= cmsCloseIOhandler(io);
1512
1513
if (rc == FALSE) { // remove() is C99 per 7.19.4.1
1514
remove(FileName); // We have to IGNORE return value in this case
1515
}
1516
return rc;
1517
}
1518
1519
// Same as anterior, but for streams
1520
cmsBool CMSEXPORT cmsSaveProfileToStream(cmsHPROFILE hProfile, FILE* Stream)
1521
{
1522
cmsBool rc;
1523
cmsContext ContextID = cmsGetProfileContextID(hProfile);
1524
cmsIOHANDLER* io = cmsOpenIOhandlerFromStream(ContextID, Stream);
1525
1526
if (io == NULL) return FALSE;
1527
1528
rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
1529
rc &= cmsCloseIOhandler(io);
1530
1531
return rc;
1532
}
1533
1534
1535
// Same as anterior, but for memory blocks. In this case, a NULL as MemPtr means calculate needed space only
1536
cmsBool CMSEXPORT cmsSaveProfileToMem(cmsHPROFILE hProfile, void *MemPtr, cmsUInt32Number* BytesNeeded)
1537
{
1538
cmsBool rc;
1539
cmsIOHANDLER* io;
1540
cmsContext ContextID = cmsGetProfileContextID(hProfile);
1541
1542
_cmsAssert(BytesNeeded != NULL);
1543
1544
// Should we just calculate the needed space?
1545
if (MemPtr == NULL) {
1546
1547
*BytesNeeded = cmsSaveProfileToIOhandler(hProfile, NULL);
1548
return (*BytesNeeded == 0) ? FALSE : TRUE;
1549
}
1550
1551
// That is a real write operation
1552
io = cmsOpenIOhandlerFromMem(ContextID, MemPtr, *BytesNeeded, "w");
1553
if (io == NULL) return FALSE;
1554
1555
rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
1556
rc &= cmsCloseIOhandler(io);
1557
1558
return rc;
1559
}
1560
1561
// Free one tag contents
1562
static
1563
void freeOneTag(_cmsICCPROFILE* Icc, cmsUInt32Number i)
1564
{
1565
if (Icc->TagPtrs[i]) {
1566
1567
cmsTagTypeHandler* TypeHandler = Icc->TagTypeHandlers[i];
1568
1569
if (TypeHandler != NULL) {
1570
cmsTagTypeHandler LocalTypeHandler = *TypeHandler;
1571
1572
LocalTypeHandler.ContextID = Icc->ContextID;
1573
LocalTypeHandler.ICCVersion = Icc->Version;
1574
LocalTypeHandler.FreePtr(&LocalTypeHandler, Icc->TagPtrs[i]);
1575
}
1576
else
1577
_cmsFree(Icc->ContextID, Icc->TagPtrs[i]);
1578
}
1579
}
1580
1581
// Closes a profile freeing any involved resources
1582
cmsBool CMSEXPORT cmsCloseProfile(cmsHPROFILE hProfile)
1583
{
1584
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1585
cmsBool rc = TRUE;
1586
cmsUInt32Number i;
1587
1588
if (!Icc) return FALSE;
1589
1590
// Was open in write mode?
1591
if (Icc ->IsWrite) {
1592
1593
Icc ->IsWrite = FALSE; // Assure no further writing
1594
rc &= cmsSaveProfileToFile(hProfile, Icc ->IOhandler->PhysicalFile);
1595
}
1596
1597
for (i=0; i < Icc -> TagCount; i++) {
1598
1599
freeOneTag(Icc, i);
1600
}
1601
1602
if (Icc ->IOhandler != NULL) {
1603
rc &= cmsCloseIOhandler(Icc->IOhandler);
1604
}
1605
1606
_cmsDestroyMutex(Icc->ContextID, Icc->UsrMutex);
1607
1608
_cmsFree(Icc ->ContextID, Icc); // Free placeholder memory
1609
1610
return rc;
1611
}
1612
1613
1614
// -------------------------------------------------------------------------------------------------------------------
1615
1616
1617
// Returns TRUE if a given tag is supported by a plug-in
1618
static
1619
cmsBool IsTypeSupported(cmsTagDescriptor* TagDescriptor, cmsTagTypeSignature Type)
1620
{
1621
cmsUInt32Number i, nMaxTypes;
1622
1623
nMaxTypes = TagDescriptor->nSupportedTypes;
1624
if (nMaxTypes >= MAX_TYPES_IN_LCMS_PLUGIN)
1625
nMaxTypes = MAX_TYPES_IN_LCMS_PLUGIN;
1626
1627
for (i=0; i < nMaxTypes; i++) {
1628
if (Type == TagDescriptor ->SupportedTypes[i]) return TRUE;
1629
}
1630
1631
return FALSE;
1632
}
1633
1634
1635
// That's the main read function
1636
void* CMSEXPORT cmsReadTag(cmsHPROFILE hProfile, cmsTagSignature sig)
1637
{
1638
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1639
cmsIOHANDLER* io;
1640
cmsTagTypeHandler* TypeHandler;
1641
cmsTagTypeHandler LocalTypeHandler;
1642
cmsTagDescriptor* TagDescriptor;
1643
cmsTagTypeSignature BaseType;
1644
cmsUInt32Number Offset, TagSize;
1645
cmsUInt32Number ElemCount;
1646
int n;
1647
1648
if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return NULL;
1649
1650
n = _cmsSearchTag(Icc, sig, TRUE);
1651
if (n < 0)
1652
{
1653
// Not found, return NULL
1654
_cmsUnlockMutex(Icc->ContextID, Icc->UsrMutex);
1655
return NULL;
1656
}
1657
1658
// If the element is already in memory, return the pointer
1659
if (Icc -> TagPtrs[n]) {
1660
1661
if (Icc->TagTypeHandlers[n] == NULL) goto Error;
1662
1663
// Sanity check
1664
BaseType = Icc->TagTypeHandlers[n]->Signature;
1665
if (BaseType == 0) goto Error;
1666
1667
TagDescriptor = _cmsGetTagDescriptor(Icc->ContextID, sig);
1668
if (TagDescriptor == NULL) goto Error;
1669
1670
if (!IsTypeSupported(TagDescriptor, BaseType)) goto Error;
1671
1672
if (Icc ->TagSaveAsRaw[n]) goto Error; // We don't support read raw tags as cooked
1673
1674
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1675
return Icc -> TagPtrs[n];
1676
}
1677
1678
// We need to read it. Get the offset and size to the file
1679
Offset = Icc -> TagOffsets[n];
1680
TagSize = Icc -> TagSizes[n];
1681
1682
if (TagSize < 8) goto Error;
1683
1684
io = Icc ->IOhandler;
1685
1686
if (io == NULL) { // This is a built-in profile that has been manipulated, abort early
1687
1688
cmsSignalError(Icc->ContextID, cmsERROR_CORRUPTION_DETECTED, "Corrupted built-in profile.");
1689
goto Error;
1690
}
1691
1692
// Seek to its location
1693
if (!io -> Seek(io, Offset))
1694
goto Error;
1695
1696
// Search for support on this tag
1697
TagDescriptor = _cmsGetTagDescriptor(Icc-> ContextID, sig);
1698
if (TagDescriptor == NULL) {
1699
1700
char String[5];
1701
1702
_cmsTagSignature2String(String, sig);
1703
1704
// An unknown element was found.
1705
cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown tag type '%s' found.", String);
1706
goto Error; // Unsupported.
1707
}
1708
1709
// if supported, get type and check if in list
1710
BaseType = _cmsReadTypeBase(io);
1711
if (BaseType == 0) goto Error;
1712
1713
if (!IsTypeSupported(TagDescriptor, BaseType)) goto Error;
1714
1715
TagSize -= 8; // Already read by the type base logic
1716
1717
// Get type handler
1718
TypeHandler = _cmsGetTagTypeHandler(Icc ->ContextID, BaseType);
1719
if (TypeHandler == NULL) goto Error;
1720
LocalTypeHandler = *TypeHandler;
1721
1722
1723
// Read the tag
1724
Icc -> TagTypeHandlers[n] = TypeHandler;
1725
1726
LocalTypeHandler.ContextID = Icc ->ContextID;
1727
LocalTypeHandler.ICCVersion = Icc ->Version;
1728
Icc -> TagPtrs[n] = LocalTypeHandler.ReadPtr(&LocalTypeHandler, io, &ElemCount, TagSize);
1729
1730
// The tag type is supported, but something wrong happened and we cannot read the tag.
1731
// let know the user about this (although it is just a warning)
1732
if (Icc -> TagPtrs[n] == NULL) {
1733
1734
char String[5];
1735
1736
_cmsTagSignature2String(String, sig);
1737
cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "Corrupted tag '%s'", String);
1738
goto Error;
1739
}
1740
1741
// This is a weird error that may be a symptom of something more serious, the number of
1742
// stored item is actually less than the number of required elements.
1743
if (ElemCount < TagDescriptor ->ElemCount) {
1744
1745
char String[5];
1746
1747
_cmsTagSignature2String(String, sig);
1748
cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "'%s' Inconsistent number of items: expected %d, got %d",
1749
String, TagDescriptor ->ElemCount, ElemCount);
1750
goto Error;
1751
}
1752
1753
1754
// Return the data
1755
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1756
return Icc -> TagPtrs[n];
1757
1758
1759
// Return error and unlock the data
1760
Error:
1761
1762
freeOneTag(Icc, n);
1763
Icc->TagPtrs[n] = NULL;
1764
1765
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1766
return NULL;
1767
}
1768
1769
1770
// Get true type of data
1771
cmsTagTypeSignature _cmsGetTagTrueType(cmsHPROFILE hProfile, cmsTagSignature sig)
1772
{
1773
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1774
cmsTagTypeHandler* TypeHandler;
1775
int n;
1776
1777
// Search for given tag in ICC profile directory
1778
n = _cmsSearchTag(Icc, sig, TRUE);
1779
if (n < 0) return (cmsTagTypeSignature) 0; // Not found, return NULL
1780
1781
// Get the handler. The true type is there
1782
TypeHandler = Icc -> TagTypeHandlers[n];
1783
return TypeHandler ->Signature;
1784
}
1785
1786
1787
// Write a single tag. This just keeps track of the tak into a list of "to be written". If the tag is already
1788
// in that list, the previous version is deleted.
1789
cmsBool CMSEXPORT cmsWriteTag(cmsHPROFILE hProfile, cmsTagSignature sig, const void* data)
1790
{
1791
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1792
cmsTagTypeHandler* TypeHandler = NULL;
1793
cmsTagTypeHandler LocalTypeHandler;
1794
cmsTagDescriptor* TagDescriptor = NULL;
1795
cmsTagTypeSignature Type;
1796
int i;
1797
cmsFloat64Number Version;
1798
char TypeString[5], SigString[5];
1799
1800
if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return FALSE;
1801
1802
// To delete tags.
1803
if (data == NULL) {
1804
1805
// Delete the tag
1806
i = _cmsSearchTag(Icc, sig, FALSE);
1807
if (i >= 0) {
1808
1809
// Use zero as a mark of deleted
1810
_cmsDeleteTagByPos(Icc, i);
1811
Icc ->TagNames[i] = (cmsTagSignature) 0;
1812
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1813
return TRUE;
1814
}
1815
// Didn't find the tag
1816
goto Error;
1817
}
1818
1819
if (!_cmsNewTag(Icc, sig, &i)) goto Error;
1820
1821
// This is not raw
1822
Icc ->TagSaveAsRaw[i] = FALSE;
1823
1824
// This is not a link
1825
Icc ->TagLinked[i] = (cmsTagSignature) 0;
1826
1827
// Get information about the TAG.
1828
TagDescriptor = _cmsGetTagDescriptor(Icc-> ContextID, sig);
1829
if (TagDescriptor == NULL){
1830
cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported tag '%x'", sig);
1831
goto Error;
1832
}
1833
1834
1835
// Now we need to know which type to use. It depends on the version.
1836
Version = cmsGetProfileVersion(hProfile);
1837
1838
if (TagDescriptor ->DecideType != NULL) {
1839
1840
// Let the tag descriptor to decide the type base on depending on
1841
// the data. This is useful for example on parametric curves, where
1842
// curves specified by a table cannot be saved as parametric and needs
1843
// to be casted to single v2-curves, even on v4 profiles.
1844
1845
Type = TagDescriptor ->DecideType(Version, data);
1846
}
1847
else {
1848
1849
Type = TagDescriptor ->SupportedTypes[0];
1850
}
1851
1852
// Does the tag support this type?
1853
if (!IsTypeSupported(TagDescriptor, Type)) {
1854
1855
_cmsTagSignature2String(TypeString, (cmsTagSignature) Type);
1856
_cmsTagSignature2String(SigString, sig);
1857
1858
cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported type '%s' for tag '%s'", TypeString, SigString);
1859
goto Error;
1860
}
1861
1862
// Does we have a handler for this type?
1863
TypeHandler = _cmsGetTagTypeHandler(Icc->ContextID, Type);
1864
if (TypeHandler == NULL) {
1865
1866
_cmsTagSignature2String(TypeString, (cmsTagSignature) Type);
1867
_cmsTagSignature2String(SigString, sig);
1868
1869
cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported type '%s' for tag '%s'", TypeString, SigString);
1870
goto Error; // Should never happen
1871
}
1872
1873
1874
// Fill fields on icc structure
1875
Icc ->TagTypeHandlers[i] = TypeHandler;
1876
Icc ->TagNames[i] = sig;
1877
Icc ->TagSizes[i] = 0;
1878
Icc ->TagOffsets[i] = 0;
1879
1880
LocalTypeHandler = *TypeHandler;
1881
LocalTypeHandler.ContextID = Icc ->ContextID;
1882
LocalTypeHandler.ICCVersion = Icc ->Version;
1883
Icc ->TagPtrs[i] = LocalTypeHandler.DupPtr(&LocalTypeHandler, data, TagDescriptor ->ElemCount);
1884
1885
if (Icc ->TagPtrs[i] == NULL) {
1886
1887
_cmsTagSignature2String(TypeString, (cmsTagSignature) Type);
1888
_cmsTagSignature2String(SigString, sig);
1889
cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "Malformed struct in type '%s' for tag '%s'", TypeString, SigString);
1890
1891
goto Error;
1892
}
1893
1894
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1895
return TRUE;
1896
1897
Error:
1898
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1899
return FALSE;
1900
1901
}
1902
1903
// Read and write raw data. Read/Write Raw/cooked pairs try to maintain consistency within the pair. Some sequences
1904
// raw/cooked would work, but at a cost. Data "cooked" may be converted to "raw" by using the "write" serialization logic.
1905
// In general it is better to avoid mixing pairs.
1906
1907
cmsUInt32Number CMSEXPORT cmsReadRawTag(cmsHPROFILE hProfile, cmsTagSignature sig, void* data, cmsUInt32Number BufferSize)
1908
{
1909
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1910
void *Object;
1911
int i;
1912
cmsIOHANDLER* MemIO;
1913
cmsTagTypeHandler* TypeHandler = NULL;
1914
cmsTagTypeHandler LocalTypeHandler;
1915
cmsTagDescriptor* TagDescriptor = NULL;
1916
cmsUInt32Number rc;
1917
cmsUInt32Number Offset, TagSize;
1918
1919
// Sanity check
1920
if (data != NULL && BufferSize == 0) return 0;
1921
1922
if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return 0;
1923
1924
// Search for given tag in ICC profile directory
1925
1926
i = _cmsSearchTag(Icc, sig, TRUE);
1927
if (i < 0) goto Error; // Not found,
1928
1929
// It is already read?
1930
if (Icc -> TagPtrs[i] == NULL) {
1931
1932
// Not yet, get original position
1933
Offset = Icc ->TagOffsets[i];
1934
TagSize = Icc ->TagSizes[i];
1935
1936
// read the data directly, don't keep copy
1937
1938
if (data != NULL) {
1939
1940
if (BufferSize < TagSize)
1941
TagSize = BufferSize;
1942
1943
if (!Icc ->IOhandler ->Seek(Icc ->IOhandler, Offset)) goto Error;
1944
if (!Icc ->IOhandler ->Read(Icc ->IOhandler, data, 1, TagSize)) goto Error;
1945
1946
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1947
return TagSize;
1948
}
1949
1950
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1951
return Icc ->TagSizes[i];
1952
}
1953
1954
// The data has been already read, or written. But wait!, maybe the user choose to save as
1955
// raw data. In this case, return the raw data directly
1956
1957
if (Icc ->TagSaveAsRaw[i]) {
1958
1959
if (data != NULL) {
1960
1961
TagSize = Icc ->TagSizes[i];
1962
if (BufferSize < TagSize)
1963
TagSize = BufferSize;
1964
1965
memmove(data, Icc ->TagPtrs[i], TagSize);
1966
1967
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1968
return TagSize;
1969
}
1970
1971
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1972
return Icc ->TagSizes[i];
1973
}
1974
1975
// Already read, or previously set by cmsWriteTag(). We need to serialize that
1976
// data to raw to get something that makes sense
1977
1978
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1979
Object = cmsReadTag(hProfile, sig);
1980
if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return 0;
1981
1982
if (Object == NULL) goto Error;
1983
1984
// Now we need to serialize to a memory block: just use a memory iohandler
1985
1986
if (data == NULL) {
1987
MemIO = cmsOpenIOhandlerFromNULL(cmsGetProfileContextID(hProfile));
1988
} else{
1989
MemIO = cmsOpenIOhandlerFromMem(cmsGetProfileContextID(hProfile), data, BufferSize, "w");
1990
}
1991
if (MemIO == NULL) goto Error;
1992
1993
// Obtain type handling for the tag
1994
TypeHandler = Icc ->TagTypeHandlers[i];
1995
TagDescriptor = _cmsGetTagDescriptor(Icc-> ContextID, sig);
1996
if (TagDescriptor == NULL) {
1997
cmsCloseIOhandler(MemIO);
1998
goto Error;
1999
}
2000
2001
if (TypeHandler == NULL) goto Error;
2002
2003
// Serialize
2004
LocalTypeHandler = *TypeHandler;
2005
LocalTypeHandler.ContextID = Icc ->ContextID;
2006
LocalTypeHandler.ICCVersion = Icc ->Version;
2007
2008
if (!_cmsWriteTypeBase(MemIO, TypeHandler ->Signature)) {
2009
cmsCloseIOhandler(MemIO);
2010
goto Error;
2011
}
2012
2013
if (!LocalTypeHandler.WritePtr(&LocalTypeHandler, MemIO, Object, TagDescriptor ->ElemCount)) {
2014
cmsCloseIOhandler(MemIO);
2015
goto Error;
2016
}
2017
2018
// Get Size and close
2019
rc = MemIO ->Tell(MemIO);
2020
cmsCloseIOhandler(MemIO); // Ignore return code this time
2021
2022
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
2023
return rc;
2024
2025
Error:
2026
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
2027
return 0;
2028
}
2029
2030
// Similar to the anterior. This function allows to write directly to the ICC profile any data, without
2031
// checking anything. As a rule, mixing Raw with cooked doesn't work, so writing a tag as raw and then reading
2032
// it as cooked without serializing does result into an error. If that is what you want, you will need to dump
2033
// the profile to memry or disk and then reopen it.
2034
cmsBool CMSEXPORT cmsWriteRawTag(cmsHPROFILE hProfile, cmsTagSignature sig, const void* data, cmsUInt32Number Size)
2035
{
2036
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
2037
int i;
2038
2039
if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return 0;
2040
2041
if (!_cmsNewTag(Icc, sig, &i)) {
2042
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
2043
return FALSE;
2044
}
2045
2046
// Mark the tag as being written as RAW
2047
Icc ->TagSaveAsRaw[i] = TRUE;
2048
Icc ->TagNames[i] = sig;
2049
Icc ->TagLinked[i] = (cmsTagSignature) 0;
2050
2051
// Keep a copy of the block
2052
Icc ->TagPtrs[i] = _cmsDupMem(Icc ->ContextID, data, Size);
2053
Icc ->TagSizes[i] = Size;
2054
2055
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
2056
2057
if (Icc->TagPtrs[i] == NULL) {
2058
Icc->TagNames[i] = (cmsTagSignature) 0;
2059
return FALSE;
2060
}
2061
return TRUE;
2062
}
2063
2064
// Using this function you can collapse several tag entries to the same block in the profile
2065
cmsBool CMSEXPORT cmsLinkTag(cmsHPROFILE hProfile, cmsTagSignature sig, cmsTagSignature dest)
2066
{
2067
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
2068
int i;
2069
2070
if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return FALSE;
2071
2072
if (!_cmsNewTag(Icc, sig, &i)) {
2073
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
2074
return FALSE;
2075
}
2076
2077
// Keep necessary information
2078
Icc ->TagSaveAsRaw[i] = FALSE;
2079
Icc ->TagNames[i] = sig;
2080
Icc ->TagLinked[i] = dest;
2081
2082
Icc ->TagPtrs[i] = NULL;
2083
Icc ->TagSizes[i] = 0;
2084
Icc ->TagOffsets[i] = 0;
2085
2086
_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
2087
return TRUE;
2088
}
2089
2090
2091
// Returns the tag linked to sig, in the case two tags are sharing same resource
2092
cmsTagSignature CMSEXPORT cmsTagLinkedTo(cmsHPROFILE hProfile, cmsTagSignature sig)
2093
{
2094
_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
2095
int i;
2096
2097
// Search for given tag in ICC profile directory
2098
i = _cmsSearchTag(Icc, sig, FALSE);
2099
if (i < 0) return (cmsTagSignature) 0; // Not found, return 0
2100
2101
return Icc -> TagLinked[i];
2102
}
2103
2104