1 //---------------------------------------------------------------------------------
3 // Little Color Management System
4 // Copyright (c) 1998-2011 Marti Maria Saguer
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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.
24 //---------------------------------------------------------------------------------
27 #include "lcms2_internal.h"
29 // Generic I/O, tag dictionary management, profile struct
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.
36 // NULL stream, for taking care of used space -------------------------------------
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.
44 cmsUInt32Number Pointer; // Points to current location
48 cmsUInt32Number NULLRead(cmsIOHANDLER* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count)
50 FILENULL* ResData = (FILENULL*) iohandler ->stream;
52 cmsUInt32Number len = size * count;
53 ResData -> Pointer += len;
56 cmsUNUSED_PARAMETER(Buffer);
60 cmsBool NULLSeek(cmsIOHANDLER* iohandler, cmsUInt32Number offset)
62 FILENULL* ResData = (FILENULL*) iohandler ->stream;
64 ResData ->Pointer = offset;
69 cmsUInt32Number NULLTell(cmsIOHANDLER* iohandler)
71 FILENULL* ResData = (FILENULL*) iohandler ->stream;
72 return ResData -> Pointer;
76 cmsBool NULLWrite(cmsIOHANDLER* iohandler, cmsUInt32Number size, const void *Ptr)
78 FILENULL* ResData = (FILENULL*) iohandler ->stream;
80 ResData ->Pointer += size;
81 if (ResData ->Pointer > iohandler->UsedSpace)
82 iohandler->UsedSpace = ResData ->Pointer;
86 cmsUNUSED_PARAMETER(Ptr);
90 cmsBool NULLClose(cmsIOHANDLER* iohandler)
92 FILENULL* ResData = (FILENULL*) iohandler ->stream;
94 _cmsFree(iohandler ->ContextID, ResData);
95 _cmsFree(iohandler ->ContextID, iohandler);
99 // The NULL IOhandler creator
100 cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromNULL(cmsContext ContextID)
102 struct _cms_io_handler* iohandler = NULL;
105 iohandler = (struct _cms_io_handler*) _cmsMallocZero(ContextID, sizeof(struct _cms_io_handler));
106 if (iohandler == NULL) return NULL;
108 fm = (FILENULL*) _cmsMallocZero(ContextID, sizeof(FILENULL));
109 if (fm == NULL) goto Error;
113 iohandler ->ContextID = ContextID;
114 iohandler ->stream = (void*) fm;
115 iohandler ->UsedSpace = 0;
116 iohandler ->ReportedSize = 0;
117 iohandler ->PhysicalFile[0] = 0;
119 iohandler ->Read = NULLRead;
120 iohandler ->Seek = NULLSeek;
121 iohandler ->Close = NULLClose;
122 iohandler ->Tell = NULLTell;
123 iohandler ->Write = NULLWrite;
128 if (fm) _cmsFree(ContextID, fm);
129 if (iohandler) _cmsFree(ContextID, iohandler);
135 // Memory-based stream --------------------------------------------------------------
137 // Those functions implements an iohandler which takes a block of memory as storage medium.
140 cmsUInt8Number* Block; // Points to allocated memory
141 cmsUInt32Number Size; // Size of allocated memory
142 cmsUInt32Number Pointer; // Points to current location
143 int FreeBlockOnClose; // As title
148 cmsUInt32Number MemoryRead(struct _cms_io_handler* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count)
150 FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
152 cmsUInt32Number len = size * count;
154 if (ResData -> Pointer + len > ResData -> Size){
156 len = (ResData -> Size - ResData -> Pointer);
157 cmsSignalError(iohandler ->ContextID, cmsERROR_READ, "Read from memory error. Got %d bytes, block should be of %d bytes", len, count * size);
161 Ptr = ResData -> Block;
162 Ptr += ResData -> Pointer;
163 memmove(Buffer, Ptr, len);
164 ResData -> Pointer += len;
169 // SEEK_CUR is assumed
171 cmsBool MemorySeek(struct _cms_io_handler* iohandler, cmsUInt32Number offset)
173 FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
175 if (offset > ResData ->Size) {
176 cmsSignalError(iohandler ->ContextID, cmsERROR_SEEK, "Too few data; probably corrupted profile");
180 ResData ->Pointer = offset;
186 cmsUInt32Number MemoryTell(struct _cms_io_handler* iohandler)
188 FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
190 if (ResData == NULL) return 0;
191 return ResData -> Pointer;
195 // Writes data to memory, also keeps used space for further reference.
197 cmsBool MemoryWrite(struct _cms_io_handler* iohandler, cmsUInt32Number size, const void *Ptr)
199 FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
201 if (ResData == NULL) return FALSE; // Housekeeping
203 if (size == 0) return TRUE; // Write zero bytes is ok, but does nothing
205 memmove(ResData ->Block + ResData ->Pointer, Ptr, size);
206 ResData ->Pointer += size;
208 if (ResData ->Pointer > iohandler->UsedSpace)
209 iohandler->UsedSpace = ResData ->Pointer;
212 iohandler->UsedSpace += size;
219 cmsBool MemoryClose(struct _cms_io_handler* iohandler)
221 FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
223 if (ResData ->FreeBlockOnClose) {
225 if (ResData ->Block) _cmsFree(iohandler ->ContextID, ResData ->Block);
228 _cmsFree(iohandler ->ContextID, ResData);
229 _cmsFree(iohandler ->ContextID, iohandler);
234 // Create a iohandler for memory block. AccessMode=='r' assumes the iohandler is going to read, and makes
235 // a copy of the memory block for letting user to free the memory after invoking open profile. In write
236 // mode ("w"), Buffere points to the begin of memory block to be written.
237 cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromMem(cmsContext ContextID, void *Buffer, cmsUInt32Number size, const char* AccessMode)
239 cmsIOHANDLER* iohandler = NULL;
242 _cmsAssert(AccessMode != NULL);
244 iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
245 if (iohandler == NULL) return NULL;
247 switch (*AccessMode) {
250 fm = (FILEMEM*) _cmsMallocZero(ContextID, sizeof(FILEMEM));
251 if (fm == NULL) goto Error;
253 if (Buffer == NULL) {
254 cmsSignalError(ContextID, cmsERROR_READ, "Couldn't read profile from NULL pointer");
258 fm ->Block = (cmsUInt8Number*) _cmsMalloc(ContextID, size);
259 if (fm ->Block == NULL) {
261 _cmsFree(ContextID, fm);
262 _cmsFree(ContextID, iohandler);
263 cmsSignalError(ContextID, cmsERROR_READ, "Couldn't allocate %ld bytes for profile", size);
268 memmove(fm->Block, Buffer, size);
269 fm ->FreeBlockOnClose = TRUE;
272 iohandler -> ReportedSize = size;
276 fm = (FILEMEM*) _cmsMallocZero(ContextID, sizeof(FILEMEM));
277 if (fm == NULL) goto Error;
279 fm ->Block = (cmsUInt8Number*) Buffer;
280 fm ->FreeBlockOnClose = FALSE;
283 iohandler -> ReportedSize = 0;
287 cmsSignalError(ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown access mode '%c'", *AccessMode);
291 iohandler ->ContextID = ContextID;
292 iohandler ->stream = (void*) fm;
293 iohandler ->UsedSpace = 0;
294 iohandler ->PhysicalFile[0] = 0;
296 iohandler ->Read = MemoryRead;
297 iohandler ->Seek = MemorySeek;
298 iohandler ->Close = MemoryClose;
299 iohandler ->Tell = MemoryTell;
300 iohandler ->Write = MemoryWrite;
305 if (fm) _cmsFree(ContextID, fm);
306 if (iohandler) _cmsFree(ContextID, iohandler);
310 // File-based stream -------------------------------------------------------
312 // Read count elements of size bytes each. Return number of elements read
314 cmsUInt32Number FileRead(cmsIOHANDLER* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count)
316 cmsUInt32Number nReaded = (cmsUInt32Number) fread(Buffer, size, count, (FILE*) iohandler->stream);
318 if (nReaded != count) {
319 cmsSignalError(iohandler ->ContextID, cmsERROR_FILE, "Read error. Got %d bytes, block should be of %d bytes", nReaded * size, count * size);
326 // Postion file pointer in the file
328 cmsBool FileSeek(cmsIOHANDLER* iohandler, cmsUInt32Number offset)
330 if (fseek((FILE*) iohandler ->stream, (long) offset, SEEK_SET) != 0) {
332 cmsSignalError(iohandler ->ContextID, cmsERROR_FILE, "Seek error; probably corrupted file");
339 // Returns file pointer position
341 cmsUInt32Number FileTell(cmsIOHANDLER* iohandler)
343 return ftell((FILE*)iohandler ->stream);
346 // Writes data to stream, also keeps used space for further reference. Returns TRUE on success, FALSE on error
348 cmsBool FileWrite(cmsIOHANDLER* iohandler, cmsUInt32Number size, const void* Buffer)
350 if (size == 0) return TRUE; // We allow to write 0 bytes, but nothing is written
352 iohandler->UsedSpace += size;
353 return (fwrite(Buffer, size, 1, (FILE*) iohandler->stream) == 1);
358 cmsBool FileClose(cmsIOHANDLER* iohandler)
360 if (fclose((FILE*) iohandler ->stream) != 0) return FALSE;
361 _cmsFree(iohandler ->ContextID, iohandler);
365 // Create a iohandler for disk based files. if FileName is NULL, then 'stream' member is also set
366 // to NULL and no real writting is performed. This only happens in writting access mode
367 cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromFile(cmsContext ContextID, const char* FileName, const char* AccessMode)
369 cmsIOHANDLER* iohandler = NULL;
372 iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
373 if (iohandler == NULL) return NULL;
375 switch (*AccessMode) {
378 fm = fopen(FileName, "rb");
380 _cmsFree(ContextID, iohandler);
381 cmsSignalError(ContextID, cmsERROR_FILE, "File '%s' not found", FileName);
384 iohandler -> ReportedSize = cmsfilelength(fm);
388 fm = fopen(FileName, "wb");
390 _cmsFree(ContextID, iohandler);
391 cmsSignalError(ContextID, cmsERROR_FILE, "Couldn't create '%s'", FileName);
394 iohandler -> ReportedSize = 0;
398 _cmsFree(ContextID, iohandler);
399 cmsSignalError(ContextID, cmsERROR_FILE, "Unknown access mode '%c'", *AccessMode);
403 iohandler ->ContextID = ContextID;
404 iohandler ->stream = (void*) fm;
405 iohandler ->UsedSpace = 0;
407 // Keep track of the original file
408 if (FileName != NULL) {
410 strncpy(iohandler -> PhysicalFile, FileName, sizeof(iohandler -> PhysicalFile)-1);
411 iohandler -> PhysicalFile[sizeof(iohandler -> PhysicalFile)-1] = 0;
414 iohandler ->Read = FileRead;
415 iohandler ->Seek = FileSeek;
416 iohandler ->Close = FileClose;
417 iohandler ->Tell = FileTell;
418 iohandler ->Write = FileWrite;
423 // Create a iohandler for stream based files
424 cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromStream(cmsContext ContextID, FILE* Stream)
426 cmsIOHANDLER* iohandler = NULL;
428 iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
429 if (iohandler == NULL) return NULL;
431 iohandler -> ContextID = ContextID;
432 iohandler -> stream = (void*) Stream;
433 iohandler -> UsedSpace = 0;
434 iohandler -> ReportedSize = cmsfilelength(Stream);
435 iohandler -> PhysicalFile[0] = 0;
437 iohandler ->Read = FileRead;
438 iohandler ->Seek = FileSeek;
439 iohandler ->Close = FileClose;
440 iohandler ->Tell = FileTell;
441 iohandler ->Write = FileWrite;
448 // Close an open IO handler
449 cmsBool CMSEXPORT cmsCloseIOhandler(cmsIOHANDLER* io)
451 return io -> Close(io);
454 // -------------------------------------------------------------------------------------------------------
456 // Creates an empty structure holding all required parameters
457 cmsHPROFILE CMSEXPORT cmsCreateProfilePlaceholder(cmsContext ContextID)
459 time_t now = time(NULL);
460 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) _cmsMallocZero(ContextID, sizeof(_cmsICCPROFILE));
461 if (Icc == NULL) return NULL;
463 Icc ->ContextID = ContextID;
468 // Set default version
469 Icc ->Version = 0x02100000;
471 // Set creation date/time
472 memmove(&Icc ->Created, gmtime(&now), sizeof(Icc ->Created));
475 return (cmsHPROFILE) Icc;
478 cmsContext CMSEXPORT cmsGetProfileContextID(cmsHPROFILE hProfile)
480 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
482 if (Icc == NULL) return NULL;
483 return Icc -> ContextID;
487 // Return the number of tags
488 cmsInt32Number CMSEXPORT cmsGetTagCount(cmsHPROFILE hProfile)
490 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
491 if (Icc == NULL) return -1;
493 return Icc->TagCount;
496 // Return the tag signature of a given tag number
497 cmsTagSignature CMSEXPORT cmsGetTagSignature(cmsHPROFILE hProfile, cmsUInt32Number n)
499 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
501 if (n > Icc->TagCount) return (cmsTagSignature) 0; // Mark as not available
502 if (n >= MAX_TABLE_TAG) return (cmsTagSignature) 0; // As double check
504 return Icc ->TagNames[n];
509 int SearchOneTag(_cmsICCPROFILE* Profile, cmsTagSignature sig)
513 for (i=0; i < Profile -> TagCount; i++) {
515 if (sig == Profile -> TagNames[i])
522 // Search for a specific tag in tag dictionary. Returns position or -1 if tag not found.
523 // If followlinks is turned on, then the position of the linked tag is returned
524 int _cmsSearchTag(_cmsICCPROFILE* Icc, cmsTagSignature sig, cmsBool lFollowLinks)
527 cmsTagSignature LinkedSig;
531 // Search for given tag in ICC profile directory
532 n = SearchOneTag(Icc, sig);
534 return -1; // Not found
537 return n; // Found, don't follow links
539 // Is this a linked tag?
540 LinkedSig = Icc ->TagLinked[n];
543 if (LinkedSig != (cmsTagSignature) 0) {
547 } while (LinkedSig != (cmsTagSignature) 0);
553 // Create a new tag entry
556 cmsBool _cmsNewTag(_cmsICCPROFILE* Icc, cmsTagSignature sig, int* NewPos)
560 // Search for the tag
561 i = _cmsSearchTag(Icc, sig, FALSE);
563 // Now let's do it easy. If the tag has been already written, that's an error
565 cmsSignalError(Icc ->ContextID, cmsERROR_ALREADY_DEFINED, "Tag '%x' already exists", sig);
572 if (Icc -> TagCount >= MAX_TABLE_TAG) {
573 cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", MAX_TABLE_TAG);
577 *NewPos = Icc ->TagCount;
586 cmsBool CMSEXPORT cmsIsTag(cmsHPROFILE hProfile, cmsTagSignature sig)
588 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) (void*) hProfile;
589 return _cmsSearchTag(Icc, sig, FALSE) >= 0;
593 // Read profile header and validate it
594 cmsBool _cmsReadHeader(_cmsICCPROFILE* Icc)
598 cmsUInt32Number i, j;
599 cmsUInt32Number HeaderSize;
600 cmsIOHANDLER* io = Icc ->IOhandler;
601 cmsUInt32Number TagCount;
605 if (io -> Read(io, &Header, sizeof(cmsICCHeader), 1) != 1) {
609 // Validate file as an ICC profile
610 if (_cmsAdjustEndianess32(Header.magic) != cmsMagicNumber) {
611 cmsSignalError(Icc ->ContextID, cmsERROR_BAD_SIGNATURE, "not an ICC profile, invalid signature");
615 // Adjust endianess of the used parameters
616 Icc -> DeviceClass = (cmsProfileClassSignature) _cmsAdjustEndianess32(Header.deviceClass);
617 Icc -> ColorSpace = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Header.colorSpace);
618 Icc -> PCS = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Header.pcs);
619 Icc -> RenderingIntent = _cmsAdjustEndianess32(Header.renderingIntent);
620 Icc -> flags = _cmsAdjustEndianess32(Header.flags);
621 Icc -> manufacturer = _cmsAdjustEndianess32(Header.manufacturer);
622 Icc -> model = _cmsAdjustEndianess32(Header.model);
623 _cmsAdjustEndianess64(&Icc -> attributes, Header.attributes);
624 Icc -> Version = _cmsAdjustEndianess32(Header.version);
626 // Get size as reported in header
627 HeaderSize = _cmsAdjustEndianess32(Header.size);
629 // Make sure HeaderSize is lower than profile size
630 if (HeaderSize >= Icc ->IOhandler ->ReportedSize)
631 HeaderSize = Icc ->IOhandler ->ReportedSize;
634 // Get creation date/time
635 _cmsDecodeDateTimeNumber(&Header.date, &Icc ->Created);
637 // The profile ID are 32 raw bytes
638 memmove(Icc ->ProfileID.ID32, Header.profileID.ID32, 16);
641 // Read tag directory
642 if (!_cmsReadUInt32Number(io, &TagCount)) return FALSE;
643 if (TagCount > MAX_TABLE_TAG) {
645 cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", TagCount);
650 // Read tag directory
652 for (i=0; i < TagCount; i++) {
654 if (!_cmsReadUInt32Number(io, (cmsUInt32Number *) &Tag.sig)) return FALSE;
655 if (!_cmsReadUInt32Number(io, &Tag.offset)) return FALSE;
656 if (!_cmsReadUInt32Number(io, &Tag.size)) return FALSE;
658 // Perform some sanity check. Offset + size should fall inside file.
659 if (Tag.offset + Tag.size > HeaderSize ||
660 Tag.offset + Tag.size < Tag.offset)
663 Icc -> TagNames[Icc ->TagCount] = Tag.sig;
664 Icc -> TagOffsets[Icc ->TagCount] = Tag.offset;
665 Icc -> TagSizes[Icc ->TagCount] = Tag.size;
668 for (j=0; j < Icc ->TagCount; j++) {
670 if ((Icc ->TagOffsets[j] == Tag.offset) &&
671 (Icc ->TagSizes[j] == Tag.size)) {
673 Icc ->TagLinked[Icc ->TagCount] = Icc ->TagNames[j];
684 // Saves profile header
685 cmsBool _cmsWriteHeader(_cmsICCPROFILE* Icc, cmsUInt32Number UsedSpace)
690 cmsInt32Number Count = 0;
692 Header.size = _cmsAdjustEndianess32(UsedSpace);
693 Header.cmmId = _cmsAdjustEndianess32(lcmsSignature);
694 Header.version = _cmsAdjustEndianess32(Icc ->Version);
696 Header.deviceClass = (cmsProfileClassSignature) _cmsAdjustEndianess32(Icc -> DeviceClass);
697 Header.colorSpace = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Icc -> ColorSpace);
698 Header.pcs = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Icc -> PCS);
700 // NOTE: in v4 Timestamp must be in UTC rather than in local time
701 _cmsEncodeDateTimeNumber(&Header.date, &Icc ->Created);
703 Header.magic = _cmsAdjustEndianess32(cmsMagicNumber);
705 #ifdef CMS_IS_WINDOWS_
706 Header.platform = (cmsPlatformSignature) _cmsAdjustEndianess32(cmsSigMicrosoft);
708 Header.platform = (cmsPlatformSignature) _cmsAdjustEndianess32(cmsSigMacintosh);
711 Header.flags = _cmsAdjustEndianess32(Icc -> flags);
712 Header.manufacturer = _cmsAdjustEndianess32(Icc -> manufacturer);
713 Header.model = _cmsAdjustEndianess32(Icc -> model);
715 _cmsAdjustEndianess64(&Header.attributes, Icc -> attributes);
717 // Rendering intent in the header (for embedded profiles)
718 Header.renderingIntent = _cmsAdjustEndianess32(Icc -> RenderingIntent);
720 // Illuminant is always D50
721 Header.illuminant.X = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(cmsD50_XYZ()->X));
722 Header.illuminant.Y = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(cmsD50_XYZ()->Y));
723 Header.illuminant.Z = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(cmsD50_XYZ()->Z));
725 // Created by LittleCMS (that's me!)
726 Header.creator = _cmsAdjustEndianess32(lcmsSignature);
728 memset(&Header.reserved, 0, sizeof(Header.reserved));
730 // Set profile ID. Endianess is always big endian
731 memmove(&Header.profileID, &Icc ->ProfileID, 16);
734 if (!Icc -> IOhandler->Write(Icc->IOhandler, sizeof(cmsICCHeader), &Header)) return FALSE;
736 // Saves Tag directory
739 for (i=0; i < Icc -> TagCount; i++) {
740 if (Icc ->TagNames[i] != 0)
744 // Store number of tags
745 if (!_cmsWriteUInt32Number(Icc ->IOhandler, Count)) return FALSE;
747 for (i=0; i < Icc -> TagCount; i++) {
749 if (Icc ->TagNames[i] == 0) continue; // It is just a placeholder
751 Tag.sig = (cmsTagSignature) _cmsAdjustEndianess32((cmsInt32Number) Icc -> TagNames[i]);
752 Tag.offset = _cmsAdjustEndianess32((cmsInt32Number) Icc -> TagOffsets[i]);
753 Tag.size = _cmsAdjustEndianess32((cmsInt32Number) Icc -> TagSizes[i]);
755 if (!Icc ->IOhandler -> Write(Icc-> IOhandler, sizeof(cmsTagEntry), &Tag)) return FALSE;
761 // ----------------------------------------------------------------------- Set/Get several struct members
764 cmsUInt32Number CMSEXPORT cmsGetHeaderRenderingIntent(cmsHPROFILE hProfile)
766 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
767 return Icc -> RenderingIntent;
770 void CMSEXPORT cmsSetHeaderRenderingIntent(cmsHPROFILE hProfile, cmsUInt32Number RenderingIntent)
772 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
773 Icc -> RenderingIntent = RenderingIntent;
776 cmsUInt32Number CMSEXPORT cmsGetHeaderFlags(cmsHPROFILE hProfile)
778 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
779 return (cmsUInt32Number) Icc -> flags;
782 void CMSEXPORT cmsSetHeaderFlags(cmsHPROFILE hProfile, cmsUInt32Number Flags)
784 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
785 Icc -> flags = (cmsUInt32Number) Flags;
788 cmsUInt32Number CMSEXPORT cmsGetHeaderManufacturer(cmsHPROFILE hProfile)
790 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
791 return (cmsUInt32Number) Icc ->manufacturer;
794 void CMSEXPORT cmsSetHeaderManufacturer(cmsHPROFILE hProfile, cmsUInt32Number manufacturer)
796 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
797 Icc -> manufacturer = (cmsUInt32Number) manufacturer;
800 cmsUInt32Number CMSEXPORT cmsGetHeaderModel(cmsHPROFILE hProfile)
802 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
803 return (cmsUInt32Number) Icc ->model;
806 void CMSEXPORT cmsSetHeaderModel(cmsHPROFILE hProfile, cmsUInt32Number model)
808 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
809 Icc -> model = (cmsUInt32Number) model;
813 void CMSEXPORT cmsGetHeaderAttributes(cmsHPROFILE hProfile, cmsUInt64Number* Flags)
815 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
816 memmove(Flags, &Icc -> attributes, sizeof(cmsUInt64Number));
819 void CMSEXPORT cmsSetHeaderAttributes(cmsHPROFILE hProfile, cmsUInt64Number Flags)
821 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
822 memmove(&Icc -> attributes, &Flags, sizeof(cmsUInt64Number));
825 void CMSEXPORT cmsGetHeaderProfileID(cmsHPROFILE hProfile, cmsUInt8Number* ProfileID)
827 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
828 memmove(ProfileID, Icc ->ProfileID.ID8, 16);
831 void CMSEXPORT cmsSetHeaderProfileID(cmsHPROFILE hProfile, cmsUInt8Number* ProfileID)
833 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
834 memmove(&Icc -> ProfileID, ProfileID, 16);
837 cmsBool CMSEXPORT cmsGetHeaderCreationDateTime(cmsHPROFILE hProfile, struct tm *Dest)
839 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
840 memmove(Dest, &Icc ->Created, sizeof(struct tm));
844 cmsColorSpaceSignature CMSEXPORT cmsGetPCS(cmsHPROFILE hProfile)
846 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
850 void CMSEXPORT cmsSetPCS(cmsHPROFILE hProfile, cmsColorSpaceSignature pcs)
852 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
856 cmsColorSpaceSignature CMSEXPORT cmsGetColorSpace(cmsHPROFILE hProfile)
858 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
859 return Icc -> ColorSpace;
862 void CMSEXPORT cmsSetColorSpace(cmsHPROFILE hProfile, cmsColorSpaceSignature sig)
864 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
865 Icc -> ColorSpace = sig;
868 cmsProfileClassSignature CMSEXPORT cmsGetDeviceClass(cmsHPROFILE hProfile)
870 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
871 return Icc -> DeviceClass;
874 void CMSEXPORT cmsSetDeviceClass(cmsHPROFILE hProfile, cmsProfileClassSignature sig)
876 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
877 Icc -> DeviceClass = sig;
880 cmsUInt32Number CMSEXPORT cmsGetEncodedICCversion(cmsHPROFILE hProfile)
882 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
883 return Icc -> Version;
886 void CMSEXPORT cmsSetEncodedICCversion(cmsHPROFILE hProfile, cmsUInt32Number Version)
888 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
889 Icc -> Version = Version;
892 // Get an hexadecimal number with same digits as v
894 cmsUInt32Number BaseToBase(cmsUInt32Number in, int BaseIn, int BaseOut)
900 for (len=0; in > 0 && len < 100; len++) {
902 Buff[len] = (char) (in % BaseIn);
906 for (i=len-1, out=0; i >= 0; --i) {
907 out = out * BaseOut + Buff[i];
913 void CMSEXPORT cmsSetProfileVersion(cmsHPROFILE hProfile, cmsFloat64Number Version)
915 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
919 Icc -> Version = BaseToBase((cmsUInt32Number) floor(Version * 100.0), 10, 16) << 16;
922 cmsFloat64Number CMSEXPORT cmsGetProfileVersion(cmsHPROFILE hProfile)
924 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
925 cmsUInt32Number n = Icc -> Version >> 16;
927 return BaseToBase(n, 16, 10) / 100.0;
929 // --------------------------------------------------------------------------------------------------------------
932 // Create profile from IOhandler
933 cmsHPROFILE CMSEXPORT cmsOpenProfileFromIOhandlerTHR(cmsContext ContextID, cmsIOHANDLER* io)
935 _cmsICCPROFILE* NewIcc;
936 cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
938 if (hEmpty == NULL) return NULL;
940 NewIcc = (_cmsICCPROFILE*) hEmpty;
942 NewIcc ->IOhandler = io;
943 if (!_cmsReadHeader(NewIcc)) goto Error;
947 cmsCloseProfile(hEmpty);
951 // Create profile from disk file
952 cmsHPROFILE CMSEXPORT cmsOpenProfileFromFileTHR(cmsContext ContextID, const char *lpFileName, const char *sAccess)
954 _cmsICCPROFILE* NewIcc;
955 cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
957 if (hEmpty == NULL) return NULL;
959 NewIcc = (_cmsICCPROFILE*) hEmpty;
961 NewIcc ->IOhandler = cmsOpenIOhandlerFromFile(ContextID, lpFileName, sAccess);
962 if (NewIcc ->IOhandler == NULL) goto Error;
964 if (*sAccess == 'W' || *sAccess == 'w') {
966 NewIcc -> IsWrite = TRUE;
971 if (!_cmsReadHeader(NewIcc)) goto Error;
975 cmsCloseProfile(hEmpty);
980 cmsHPROFILE CMSEXPORT cmsOpenProfileFromFile(const char *ICCProfile, const char *sAccess)
982 return cmsOpenProfileFromFileTHR(NULL, ICCProfile, sAccess);
986 cmsHPROFILE CMSEXPORT cmsOpenProfileFromStreamTHR(cmsContext ContextID, FILE* ICCProfile, const char *sAccess)
988 _cmsICCPROFILE* NewIcc;
989 cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
991 if (hEmpty == NULL) return NULL;
993 NewIcc = (_cmsICCPROFILE*) hEmpty;
995 NewIcc ->IOhandler = cmsOpenIOhandlerFromStream(ContextID, ICCProfile);
996 if (NewIcc ->IOhandler == NULL) goto Error;
998 if (*sAccess == 'w') {
1000 NewIcc -> IsWrite = TRUE;
1004 if (!_cmsReadHeader(NewIcc)) goto Error;
1008 cmsCloseProfile(hEmpty);
1013 cmsHPROFILE CMSEXPORT cmsOpenProfileFromStream(FILE* ICCProfile, const char *sAccess)
1015 return cmsOpenProfileFromStreamTHR(NULL, ICCProfile, sAccess);
1019 // Open from memory block
1020 cmsHPROFILE CMSEXPORT cmsOpenProfileFromMemTHR(cmsContext ContextID, const void* MemPtr, cmsUInt32Number dwSize)
1022 _cmsICCPROFILE* NewIcc;
1025 hEmpty = cmsCreateProfilePlaceholder(ContextID);
1026 if (hEmpty == NULL) return NULL;
1028 NewIcc = (_cmsICCPROFILE*) hEmpty;
1030 // Ok, in this case const void* is casted to void* just because open IO handler
1031 // shares read and writting modes. Don't abuse this feature!
1032 NewIcc ->IOhandler = cmsOpenIOhandlerFromMem(ContextID, (void*) MemPtr, dwSize, "r");
1033 if (NewIcc ->IOhandler == NULL) goto Error;
1035 if (!_cmsReadHeader(NewIcc)) goto Error;
1040 cmsCloseProfile(hEmpty);
1044 cmsHPROFILE CMSEXPORT cmsOpenProfileFromMem(const void* MemPtr, cmsUInt32Number dwSize)
1046 return cmsOpenProfileFromMemTHR(NULL, MemPtr, dwSize);
1051 // Dump tag contents. If the profile is being modified, untouched tags are copied from FileOrig
1053 cmsBool SaveTags(_cmsICCPROFILE* Icc, _cmsICCPROFILE* FileOrig)
1055 cmsUInt8Number* Data;
1057 cmsUInt32Number Begin;
1058 cmsIOHANDLER* io = Icc ->IOhandler;
1059 cmsTagDescriptor* TagDescriptor;
1060 cmsTagTypeSignature TypeBase;
1061 cmsTagTypeHandler* TypeHandler;
1064 for (i=0; i < Icc -> TagCount; i++) {
1067 if (Icc ->TagNames[i] == 0) continue;
1069 // Linked tags are not written
1070 if (Icc ->TagLinked[i] != (cmsTagSignature) 0) continue;
1072 Icc -> TagOffsets[i] = Begin = io ->UsedSpace;
1074 Data = (cmsUInt8Number*) Icc -> TagPtrs[i];
1078 // Reach here if we are copying a tag from a disk-based ICC profile which has not been modified by user.
1079 // In this case a blind copy of the block data is performed
1080 if (FileOrig != NULL && Icc -> TagOffsets[i]) {
1082 cmsUInt32Number TagSize = FileOrig -> TagSizes[i];
1083 cmsUInt32Number TagOffset = FileOrig -> TagOffsets[i];
1086 if (!FileOrig ->IOhandler->Seek(FileOrig ->IOhandler, TagOffset)) return FALSE;
1088 Mem = _cmsMalloc(Icc ->ContextID, TagSize);
1089 if (Mem == NULL) return FALSE;
1091 if (FileOrig ->IOhandler->Read(FileOrig->IOhandler, Mem, TagSize, 1) != 1) return FALSE;
1092 if (!io ->Write(io, TagSize, Mem)) return FALSE;
1093 _cmsFree(Icc ->ContextID, Mem);
1095 Icc -> TagSizes[i] = (io ->UsedSpace - Begin);
1098 // Align to 32 bit boundary.
1099 if (! _cmsWriteAlignment(io))
1107 // Should this tag be saved as RAW? If so, tagsizes should be specified in advance (no further cooking is done)
1108 if (Icc ->TagSaveAsRaw[i]) {
1110 if (io -> Write(io, Icc ->TagSizes[i], Data) != 1) return FALSE;
1114 // Search for support on this tag
1115 TagDescriptor = _cmsGetTagDescriptor(Icc -> TagNames[i]);
1116 if (TagDescriptor == NULL) continue; // Unsupported, ignore it
1118 TypeHandler = Icc ->TagTypeHandlers[i];
1120 if (TypeHandler == NULL) {
1121 cmsSignalError(Icc ->ContextID, cmsERROR_INTERNAL, "(Internal) no handler for tag %x", Icc -> TagNames[i]);
1125 TypeBase = TypeHandler ->Signature;
1126 if (!_cmsWriteTypeBase(io, TypeBase))
1129 TypeHandler ->ContextID = Icc ->ContextID;
1130 TypeHandler ->ICCVersion = Icc ->Version;
1131 if (!TypeHandler ->WritePtr(TypeHandler, io, Data, TagDescriptor ->ElemCount)) {
1135 _cmsTagSignature2String(String, (cmsTagSignature) TypeBase);
1136 cmsSignalError(Icc ->ContextID, cmsERROR_WRITE, "Couldn't write type '%s'", String);
1142 Icc -> TagSizes[i] = (io ->UsedSpace - Begin);
1144 // Align to 32 bit boundary.
1145 if (! _cmsWriteAlignment(io))
1154 // Fill the offset and size fields for all linked tags
1156 cmsBool SetLinks( _cmsICCPROFILE* Icc)
1160 for (i=0; i < Icc -> TagCount; i++) {
1162 cmsTagSignature lnk = Icc ->TagLinked[i];
1163 if (lnk != (cmsTagSignature) 0) {
1165 int j = _cmsSearchTag(Icc, lnk, FALSE);
1168 Icc ->TagOffsets[i] = Icc ->TagOffsets[j];
1169 Icc ->TagSizes[i] = Icc ->TagSizes[j];
1178 // Low-level save to IOHANDLER. It returns the number of bytes used to
1179 // store the profile, or zero on error. io may be NULL and in this case
1180 // no data is written--only sizes are calculated
1181 cmsUInt32Number CMSEXPORT cmsSaveProfileToIOhandler(cmsHPROFILE hProfile, cmsIOHANDLER* io)
1183 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1184 _cmsICCPROFILE Keep;
1185 cmsIOHANDLER* PrevIO;
1186 cmsUInt32Number UsedSpace;
1187 cmsContext ContextID;
1189 memmove(&Keep, Icc, sizeof(_cmsICCPROFILE));
1191 ContextID = cmsGetProfileContextID(hProfile);
1192 PrevIO = Icc ->IOhandler = cmsOpenIOhandlerFromNULL(ContextID);
1193 if (PrevIO == NULL) return 0;
1195 // Pass #1 does compute offsets
1197 if (!_cmsWriteHeader(Icc, 0)) return 0;
1198 if (!SaveTags(Icc, &Keep)) return 0;
1200 UsedSpace = PrevIO ->UsedSpace;
1202 // Pass #2 does save to iohandler
1205 Icc ->IOhandler = io;
1206 if (!SetLinks(Icc)) goto CleanUp;
1207 if (!_cmsWriteHeader(Icc, UsedSpace)) goto CleanUp;
1208 if (!SaveTags(Icc, &Keep)) goto CleanUp;
1211 memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
1212 if (!cmsCloseIOhandler(PrevIO)) return 0;
1218 cmsCloseIOhandler(PrevIO);
1219 memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
1224 // Low-level save to disk.
1225 cmsBool CMSEXPORT cmsSaveProfileToFile(cmsHPROFILE hProfile, const char* FileName)
1227 cmsContext ContextID = cmsGetProfileContextID(hProfile);
1228 cmsIOHANDLER* io = cmsOpenIOhandlerFromFile(ContextID, FileName, "w");
1231 if (io == NULL) return FALSE;
1233 rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
1234 rc &= cmsCloseIOhandler(io);
1236 if (rc == FALSE) { // remove() is C99 per 7.19.4.1
1237 remove(FileName); // We have to IGNORE return value in this case
1242 // Same as anterior, but for streams
1243 cmsBool CMSEXPORT cmsSaveProfileToStream(cmsHPROFILE hProfile, FILE* Stream)
1246 cmsContext ContextID = cmsGetProfileContextID(hProfile);
1247 cmsIOHANDLER* io = cmsOpenIOhandlerFromStream(ContextID, Stream);
1249 if (io == NULL) return FALSE;
1251 rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
1252 rc &= cmsCloseIOhandler(io);
1258 // Same as anterior, but for memory blocks. In this case, a NULL as MemPtr means calculate needed space only
1259 cmsBool CMSEXPORT cmsSaveProfileToMem(cmsHPROFILE hProfile, void *MemPtr, cmsUInt32Number* BytesNeeded)
1263 cmsContext ContextID = cmsGetProfileContextID(hProfile);
1265 // Should we just calculate the needed space?
1266 if (MemPtr == NULL) {
1268 *BytesNeeded = cmsSaveProfileToIOhandler(hProfile, NULL);
1272 // That is a real write operation
1273 io = cmsOpenIOhandlerFromMem(ContextID, MemPtr, *BytesNeeded, "w");
1274 if (io == NULL) return FALSE;
1276 rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
1277 rc &= cmsCloseIOhandler(io);
1284 // Closes a profile freeing any involved resources
1285 cmsBool CMSEXPORT cmsCloseProfile(cmsHPROFILE hProfile)
1287 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1291 if (!Icc) return FALSE;
1293 // Was open in write mode?
1294 if (Icc ->IsWrite) {
1296 Icc ->IsWrite = FALSE; // Assure no further writting
1297 rc &= cmsSaveProfileToFile(hProfile, Icc ->IOhandler->PhysicalFile);
1300 for (i=0; i < Icc -> TagCount; i++) {
1302 if (Icc -> TagPtrs[i]) {
1304 cmsTagTypeHandler* TypeHandler = Icc ->TagTypeHandlers[i];
1306 if (TypeHandler != NULL) {
1308 TypeHandler ->ContextID = Icc ->ContextID; // As an additional parameters
1309 TypeHandler ->ICCVersion = Icc ->Version;
1310 TypeHandler ->FreePtr(TypeHandler, Icc -> TagPtrs[i]);
1313 _cmsFree(Icc ->ContextID, Icc ->TagPtrs[i]);
1317 if (Icc ->IOhandler != NULL) {
1318 rc &= cmsCloseIOhandler(Icc->IOhandler);
1321 _cmsFree(Icc ->ContextID, Icc); // Free placeholder memory
1327 // -------------------------------------------------------------------------------------------------------------------
1330 // Returns TRUE if a given tag is supported by a plug-in
1332 cmsBool IsTypeSupported(cmsTagDescriptor* TagDescriptor, cmsTagTypeSignature Type)
1334 cmsUInt32Number i, nMaxTypes;
1336 nMaxTypes = TagDescriptor->nSupportedTypes;
1337 if (nMaxTypes >= MAX_TYPES_IN_LCMS_PLUGIN)
1338 nMaxTypes = MAX_TYPES_IN_LCMS_PLUGIN;
1340 for (i=0; i < nMaxTypes; i++) {
1341 if (Type == TagDescriptor ->SupportedTypes[i]) return TRUE;
1348 // That's the main read function
1349 void* CMSEXPORT cmsReadTag(cmsHPROFILE hProfile, cmsTagSignature sig)
1351 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1352 cmsIOHANDLER* io = Icc ->IOhandler;
1353 cmsTagTypeHandler* TypeHandler;
1354 cmsTagDescriptor* TagDescriptor;
1355 cmsTagTypeSignature BaseType;
1356 cmsUInt32Number Offset, TagSize;
1357 cmsUInt32Number ElemCount;
1360 n = _cmsSearchTag(Icc, sig, TRUE);
1361 if (n < 0) return NULL; // Not found, return NULL
1364 // If the element is already in memory, return the pointer
1365 if (Icc -> TagPtrs[n]) {
1367 if (Icc ->TagSaveAsRaw[n]) return NULL; // We don't support read raw tags as cooked
1368 return Icc -> TagPtrs[n];
1371 // We need to read it. Get the offset and size to the file
1372 Offset = Icc -> TagOffsets[n];
1373 TagSize = Icc -> TagSizes[n];
1375 // Seek to its location
1376 if (!io -> Seek(io, Offset))
1379 // Search for support on this tag
1380 TagDescriptor = _cmsGetTagDescriptor(sig);
1381 if (TagDescriptor == NULL) return NULL; // Unsupported.
1383 // if supported, get type and check if in list
1384 BaseType = _cmsReadTypeBase(io);
1385 if (BaseType == 0) return NULL;
1387 if (!IsTypeSupported(TagDescriptor, BaseType)) return NULL;
1389 TagSize -= 8; // Alredy read by the type base logic
1392 TypeHandler = _cmsGetTagTypeHandler(BaseType);
1393 if (TypeHandler == NULL) return NULL;
1397 Icc -> TagTypeHandlers[n] = TypeHandler;
1399 TypeHandler ->ContextID = Icc ->ContextID;
1400 TypeHandler ->ICCVersion = Icc ->Version;
1401 Icc -> TagPtrs[n] = TypeHandler ->ReadPtr(TypeHandler, io, &ElemCount, TagSize);
1403 // The tag type is supported, but something wrong happend and we cannot read the tag.
1404 // let know the user about this (although it is just a warning)
1405 if (Icc -> TagPtrs[n] == NULL) {
1409 _cmsTagSignature2String(String, sig);
1410 cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "Corrupted tag '%s'", String);
1414 // This is a weird error that may be a symptom of something more serious, the number of
1415 // stored item is actually less than the number of required elements.
1416 if (ElemCount < TagDescriptor ->ElemCount) {
1420 _cmsTagSignature2String(String, sig);
1421 cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "'%s' Inconsistent number of items: expected %d, got %d",
1422 String, TagDescriptor ->ElemCount, ElemCount);
1427 return Icc -> TagPtrs[n];
1431 // Get true type of data
1432 cmsTagTypeSignature _cmsGetTagTrueType(cmsHPROFILE hProfile, cmsTagSignature sig)
1434 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1435 cmsTagTypeHandler* TypeHandler;
1438 // Search for given tag in ICC profile directory
1439 n = _cmsSearchTag(Icc, sig, TRUE);
1440 if (n < 0) return (cmsTagTypeSignature) 0; // Not found, return NULL
1442 // Get the handler. The true type is there
1443 TypeHandler = Icc -> TagTypeHandlers[n];
1444 return TypeHandler ->Signature;
1448 // Write a single tag. This just keeps track of the tak into a list of "to be written". If the tag is already
1449 // in that list, the previous version is deleted.
1450 cmsBool CMSEXPORT cmsWriteTag(cmsHPROFILE hProfile, cmsTagSignature sig, const void* data)
1452 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1453 cmsTagTypeHandler* TypeHandler = NULL;
1454 cmsTagDescriptor* TagDescriptor = NULL;
1455 cmsTagTypeSignature Type;
1457 cmsFloat64Number Version;
1458 char TypeString[5], SigString[5];
1463 i = _cmsSearchTag(Icc, sig, FALSE);
1465 Icc ->TagNames[i] = (cmsTagSignature) 0;
1466 // Unsupported by now, reserved for future ampliations (delete)
1470 i = _cmsSearchTag(Icc, sig, FALSE);
1473 if (Icc -> TagPtrs[i] != NULL) {
1475 // Already exists. Free previous version
1476 if (Icc ->TagSaveAsRaw[i]) {
1477 _cmsFree(Icc ->ContextID, Icc ->TagPtrs[i]);
1480 TypeHandler = Icc ->TagTypeHandlers[i];
1482 if (TypeHandler != NULL) {
1484 TypeHandler ->ContextID = Icc ->ContextID; // As an additional parameter
1485 TypeHandler ->ICCVersion = Icc ->Version;
1486 TypeHandler->FreePtr(TypeHandler, Icc -> TagPtrs[i]);
1493 i = Icc -> TagCount;
1495 if (i >= MAX_TABLE_TAG) {
1496 cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", MAX_TABLE_TAG);
1504 Icc ->TagSaveAsRaw[i] = FALSE;
1506 // This is not a link
1507 Icc ->TagLinked[i] = (cmsTagSignature) 0;
1509 // Get information about the TAG.
1510 TagDescriptor = _cmsGetTagDescriptor(sig);
1511 if (TagDescriptor == NULL){
1512 cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported tag '%x'", sig);
1517 // Now we need to know which type to use. It depends on the version.
1518 Version = cmsGetProfileVersion(hProfile);
1520 if (TagDescriptor ->DecideType != NULL) {
1522 // Let the tag descriptor to decide the type base on depending on
1523 // the data. This is useful for example on parametric curves, where
1524 // curves specified by a table cannot be saved as parametric and needs
1525 // to be revented to single v2-curves, even on v4 profiles.
1527 Type = TagDescriptor ->DecideType(Version, data);
1532 Type = TagDescriptor ->SupportedTypes[0];
1535 // Does the tag support this type?
1536 if (!IsTypeSupported(TagDescriptor, Type)) {
1538 _cmsTagSignature2String(TypeString, (cmsTagSignature) Type);
1539 _cmsTagSignature2String(SigString, sig);
1541 cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported type '%s' for tag '%s'", TypeString, SigString);
1545 // Does we have a handler for this type?
1546 TypeHandler = _cmsGetTagTypeHandler(Type);
1547 if (TypeHandler == NULL) {
1549 _cmsTagSignature2String(TypeString, (cmsTagSignature) Type);
1550 _cmsTagSignature2String(SigString, sig);
1552 cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported type '%s' for tag '%s'", TypeString, SigString);
1553 return FALSE; // Should never happen
1557 // Fill fields on icc structure
1558 Icc ->TagTypeHandlers[i] = TypeHandler;
1559 Icc ->TagNames[i] = sig;
1560 Icc ->TagSizes[i] = 0;
1561 Icc ->TagOffsets[i] = 0;
1563 TypeHandler ->ContextID = Icc ->ContextID;
1564 TypeHandler ->ICCVersion = Icc ->Version;
1565 Icc ->TagPtrs[i] = TypeHandler ->DupPtr(TypeHandler, data, TagDescriptor ->ElemCount);
1567 if (Icc ->TagPtrs[i] == NULL) {
1569 _cmsTagSignature2String(TypeString, (cmsTagSignature) Type);
1570 _cmsTagSignature2String(SigString, sig);
1571 cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "Malformed struct in type '%s' for tag '%s'", TypeString, SigString);
1579 // Read and write raw data. The only way those function would work and keep consistence with normal read and write
1580 // is to do an additional step of serialization. That means, readRaw would issue a normal read and then convert the obtained
1581 // data to raw bytes by using the "write" serialization logic. And vice-versa. I know this may end in situations where
1582 // raw data written does not exactly correspond with the raw data proposed to cmsWriteRaw data, but this approach allows
1583 // to write a tag as raw data and the read it as handled.
1585 cmsInt32Number CMSEXPORT cmsReadRawTag(cmsHPROFILE hProfile, cmsTagSignature sig, void* data, cmsUInt32Number BufferSize)
1587 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1590 cmsIOHANDLER* MemIO;
1591 cmsTagTypeHandler* TypeHandler = NULL;
1592 cmsTagDescriptor* TagDescriptor = NULL;
1594 cmsUInt32Number Offset, TagSize;
1596 // Search for given tag in ICC profile directory
1597 i = _cmsSearchTag(Icc, sig, TRUE);
1598 if (i < 0) return 0; // Not found, return 0
1600 // It is already read?
1601 if (Icc -> TagPtrs[i] == NULL) {
1603 // No yet, get original position
1604 Offset = Icc ->TagOffsets[i];
1605 TagSize = Icc ->TagSizes[i];
1608 // read the data directly, don't keep copy
1611 if (BufferSize < TagSize)
1612 TagSize = BufferSize;
1614 if (!Icc ->IOhandler ->Seek(Icc ->IOhandler, Offset)) return 0;
1615 if (!Icc ->IOhandler ->Read(Icc ->IOhandler, data, 1, TagSize)) return 0;
1618 return Icc ->TagSizes[i];
1621 // The data has been already read, or written. But wait!, maybe the user choosed to save as
1622 // raw data. In this case, return the raw data directly
1623 if (Icc ->TagSaveAsRaw[i]) {
1627 TagSize = Icc ->TagSizes[i];
1628 if (BufferSize < TagSize)
1629 TagSize = BufferSize;
1631 memmove(data, Icc ->TagPtrs[i], TagSize);
1634 return Icc ->TagSizes[i];
1637 // Already readed, or previously set by cmsWriteTag(). We need to serialize that
1638 // data to raw in order to maintain consistency.
1639 Object = cmsReadTag(hProfile, sig);
1640 if (Object == NULL) return 0;
1642 // Now we need to serialize to a memory block: just use a memory iohandler
1645 MemIO = cmsOpenIOhandlerFromNULL(cmsGetProfileContextID(hProfile));
1647 MemIO = cmsOpenIOhandlerFromMem(cmsGetProfileContextID(hProfile), data, BufferSize, "w");
1649 if (MemIO == NULL) return 0;
1651 // Obtain type handling for the tag
1652 TypeHandler = Icc ->TagTypeHandlers[i];
1653 TagDescriptor = _cmsGetTagDescriptor(sig);
1654 if (TagDescriptor == NULL) {
1655 cmsCloseIOhandler(MemIO);
1660 TypeHandler ->ContextID = Icc ->ContextID;
1661 TypeHandler ->ICCVersion = Icc ->Version;
1663 if (!_cmsWriteTypeBase(MemIO, TypeHandler ->Signature)) {
1664 cmsCloseIOhandler(MemIO);
1668 if (!TypeHandler ->WritePtr(TypeHandler, MemIO, Object, TagDescriptor ->ElemCount)) {
1669 cmsCloseIOhandler(MemIO);
1673 // Get Size and close
1674 rc = MemIO ->Tell(MemIO);
1675 cmsCloseIOhandler(MemIO); // Ignore return code this time
1680 // Similar to the anterior. This function allows to write directly to the ICC profile any data, without
1681 // checking anything. As a rule, mixing Raw with cooked doesn't work, so writting a tag as raw and then reading
1682 // it as cooked without serializing does result into an error. If that is wha you want, you will need to dump
1683 // the profile to memry or disk and then reopen it.
1684 cmsBool CMSEXPORT cmsWriteRawTag(cmsHPROFILE hProfile, cmsTagSignature sig, const void* data, cmsUInt32Number Size)
1686 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1689 if (!_cmsNewTag(Icc, sig, &i)) return FALSE;
1691 // Mark the tag as being written as RAW
1692 Icc ->TagSaveAsRaw[i] = TRUE;
1693 Icc ->TagNames[i] = sig;
1694 Icc ->TagLinked[i] = (cmsTagSignature) 0;
1696 // Keep a copy of the block
1697 Icc ->TagPtrs[i] = _cmsDupMem(Icc ->ContextID, data, Size);
1698 Icc ->TagSizes[i] = Size;
1703 // Using this function you can collapse several tag entries to the same block in the profile
1704 cmsBool CMSEXPORT cmsLinkTag(cmsHPROFILE hProfile, cmsTagSignature sig, cmsTagSignature dest)
1706 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1709 if (!_cmsNewTag(Icc, sig, &i)) return FALSE;
1711 // Keep necessary information
1712 Icc ->TagSaveAsRaw[i] = FALSE;
1713 Icc ->TagNames[i] = sig;
1714 Icc ->TagLinked[i] = dest;
1716 Icc ->TagPtrs[i] = NULL;
1717 Icc ->TagSizes[i] = 0;
1718 Icc ->TagOffsets[i] = 0;
1724 // Returns the tag linked to sig, in the case two tags are sharing same resource
1725 cmsTagSignature CMSEXPORT cmsTagLinkedTo(cmsHPROFILE hProfile, cmsTagSignature sig)
1727 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1730 // Search for given tag in ICC profile directory
1731 i = _cmsSearchTag(Icc, sig, FALSE);
1732 if (i < 0) return (cmsTagSignature) 0; // Not found, return 0
1734 return Icc -> TagLinked[i];