1 //---------------------------------------------------------------------------------
3 // Little Color Management System
4 // Copyright (c) 1998-2012 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;
207 iohandler->UsedSpace += size;
209 if (ResData ->Pointer > iohandler->UsedSpace)
210 iohandler->UsedSpace = ResData ->Pointer;
217 cmsBool MemoryClose(struct _cms_io_handler* iohandler)
219 FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
221 if (ResData ->FreeBlockOnClose) {
223 if (ResData ->Block) _cmsFree(iohandler ->ContextID, ResData ->Block);
226 _cmsFree(iohandler ->ContextID, ResData);
227 _cmsFree(iohandler ->ContextID, iohandler);
232 // Create a iohandler for memory block. AccessMode=='r' assumes the iohandler is going to read, and makes
233 // a copy of the memory block for letting user to free the memory after invoking open profile. In write
234 // mode ("w"), Buffere points to the begin of memory block to be written.
235 cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromMem(cmsContext ContextID, void *Buffer, cmsUInt32Number size, const char* AccessMode)
237 cmsIOHANDLER* iohandler = NULL;
240 _cmsAssert(AccessMode != NULL);
242 iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
243 if (iohandler == NULL) return NULL;
245 switch (*AccessMode) {
248 fm = (FILEMEM*) _cmsMallocZero(ContextID, sizeof(FILEMEM));
249 if (fm == NULL) goto Error;
251 if (Buffer == NULL) {
252 cmsSignalError(ContextID, cmsERROR_READ, "Couldn't read profile from NULL pointer");
256 fm ->Block = (cmsUInt8Number*) _cmsMalloc(ContextID, size);
257 if (fm ->Block == NULL) {
259 _cmsFree(ContextID, fm);
260 _cmsFree(ContextID, iohandler);
261 cmsSignalError(ContextID, cmsERROR_READ, "Couldn't allocate %ld bytes for profile", size);
266 memmove(fm->Block, Buffer, size);
267 fm ->FreeBlockOnClose = TRUE;
270 iohandler -> ReportedSize = size;
274 fm = (FILEMEM*) _cmsMallocZero(ContextID, sizeof(FILEMEM));
275 if (fm == NULL) goto Error;
277 fm ->Block = (cmsUInt8Number*) Buffer;
278 fm ->FreeBlockOnClose = FALSE;
281 iohandler -> ReportedSize = 0;
285 cmsSignalError(ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown access mode '%c'", *AccessMode);
289 iohandler ->ContextID = ContextID;
290 iohandler ->stream = (void*) fm;
291 iohandler ->UsedSpace = 0;
292 iohandler ->PhysicalFile[0] = 0;
294 iohandler ->Read = MemoryRead;
295 iohandler ->Seek = MemorySeek;
296 iohandler ->Close = MemoryClose;
297 iohandler ->Tell = MemoryTell;
298 iohandler ->Write = MemoryWrite;
303 if (fm) _cmsFree(ContextID, fm);
304 if (iohandler) _cmsFree(ContextID, iohandler);
308 // File-based stream -------------------------------------------------------
310 // Read count elements of size bytes each. Return number of elements read
312 cmsUInt32Number FileRead(cmsIOHANDLER* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count)
314 cmsUInt32Number nReaded = (cmsUInt32Number) fread(Buffer, size, count, (FILE*) iohandler->stream);
316 if (nReaded != count) {
317 cmsSignalError(iohandler ->ContextID, cmsERROR_FILE, "Read error. Got %d bytes, block should be of %d bytes", nReaded * size, count * size);
324 // Postion file pointer in the file
326 cmsBool FileSeek(cmsIOHANDLER* iohandler, cmsUInt32Number offset)
328 if (fseek((FILE*) iohandler ->stream, (long) offset, SEEK_SET) != 0) {
330 cmsSignalError(iohandler ->ContextID, cmsERROR_FILE, "Seek error; probably corrupted file");
337 // Returns file pointer position
339 cmsUInt32Number FileTell(cmsIOHANDLER* iohandler)
341 return ftell((FILE*)iohandler ->stream);
344 // Writes data to stream, also keeps used space for further reference. Returns TRUE on success, FALSE on error
346 cmsBool FileWrite(cmsIOHANDLER* iohandler, cmsUInt32Number size, const void* Buffer)
348 if (size == 0) return TRUE; // We allow to write 0 bytes, but nothing is written
350 iohandler->UsedSpace += size;
351 return (fwrite(Buffer, size, 1, (FILE*) iohandler->stream) == 1);
356 cmsBool FileClose(cmsIOHANDLER* iohandler)
358 if (fclose((FILE*) iohandler ->stream) != 0) return FALSE;
359 _cmsFree(iohandler ->ContextID, iohandler);
363 // Create a iohandler for disk based files. if FileName is NULL, then 'stream' member is also set
364 // to NULL and no real writting is performed. This only happens in writting access mode
365 cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromFile(cmsContext ContextID, const char* FileName, const char* AccessMode)
367 cmsIOHANDLER* iohandler = NULL;
370 iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
371 if (iohandler == NULL) return NULL;
373 switch (*AccessMode) {
376 fm = fopen(FileName, "rb");
378 _cmsFree(ContextID, iohandler);
379 cmsSignalError(ContextID, cmsERROR_FILE, "File '%s' not found", FileName);
382 iohandler -> ReportedSize = cmsfilelength(fm);
386 fm = fopen(FileName, "wb");
388 _cmsFree(ContextID, iohandler);
389 cmsSignalError(ContextID, cmsERROR_FILE, "Couldn't create '%s'", FileName);
392 iohandler -> ReportedSize = 0;
396 _cmsFree(ContextID, iohandler);
397 cmsSignalError(ContextID, cmsERROR_FILE, "Unknown access mode '%c'", *AccessMode);
401 iohandler ->ContextID = ContextID;
402 iohandler ->stream = (void*) fm;
403 iohandler ->UsedSpace = 0;
405 // Keep track of the original file
406 if (FileName != NULL) {
408 strncpy(iohandler -> PhysicalFile, FileName, sizeof(iohandler -> PhysicalFile)-1);
409 iohandler -> PhysicalFile[sizeof(iohandler -> PhysicalFile)-1] = 0;
412 iohandler ->Read = FileRead;
413 iohandler ->Seek = FileSeek;
414 iohandler ->Close = FileClose;
415 iohandler ->Tell = FileTell;
416 iohandler ->Write = FileWrite;
421 // Create a iohandler for stream based files
422 cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromStream(cmsContext ContextID, FILE* Stream)
424 cmsIOHANDLER* iohandler = NULL;
426 iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
427 if (iohandler == NULL) return NULL;
429 iohandler -> ContextID = ContextID;
430 iohandler -> stream = (void*) Stream;
431 iohandler -> UsedSpace = 0;
432 iohandler -> ReportedSize = cmsfilelength(Stream);
433 iohandler -> PhysicalFile[0] = 0;
435 iohandler ->Read = FileRead;
436 iohandler ->Seek = FileSeek;
437 iohandler ->Close = FileClose;
438 iohandler ->Tell = FileTell;
439 iohandler ->Write = FileWrite;
446 // Close an open IO handler
447 cmsBool CMSEXPORT cmsCloseIOhandler(cmsIOHANDLER* io)
449 return io -> Close(io);
452 // -------------------------------------------------------------------------------------------------------
454 // Creates an empty structure holding all required parameters
455 cmsHPROFILE CMSEXPORT cmsCreateProfilePlaceholder(cmsContext ContextID)
457 time_t now = time(NULL);
458 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) _cmsMallocZero(ContextID, sizeof(_cmsICCPROFILE));
459 if (Icc == NULL) return NULL;
461 Icc ->ContextID = ContextID;
466 // Set default version
467 Icc ->Version = 0x02100000;
469 // Set creation date/time
470 memmove(&Icc ->Created, gmtime(&now), sizeof(Icc ->Created));
473 return (cmsHPROFILE) Icc;
476 cmsContext CMSEXPORT cmsGetProfileContextID(cmsHPROFILE hProfile)
478 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
480 if (Icc == NULL) return NULL;
481 return Icc -> ContextID;
485 // Return the number of tags
486 cmsInt32Number CMSEXPORT cmsGetTagCount(cmsHPROFILE hProfile)
488 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
489 if (Icc == NULL) return -1;
491 return Icc->TagCount;
494 // Return the tag signature of a given tag number
495 cmsTagSignature CMSEXPORT cmsGetTagSignature(cmsHPROFILE hProfile, cmsUInt32Number n)
497 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
499 if (n > Icc->TagCount) return (cmsTagSignature) 0; // Mark as not available
500 if (n >= MAX_TABLE_TAG) return (cmsTagSignature) 0; // As double check
502 return Icc ->TagNames[n];
507 int SearchOneTag(_cmsICCPROFILE* Profile, cmsTagSignature sig)
511 for (i=0; i < Profile -> TagCount; i++) {
513 if (sig == Profile -> TagNames[i])
520 // Search for a specific tag in tag dictionary. Returns position or -1 if tag not found.
521 // If followlinks is turned on, then the position of the linked tag is returned
522 int _cmsSearchTag(_cmsICCPROFILE* Icc, cmsTagSignature sig, cmsBool lFollowLinks)
525 cmsTagSignature LinkedSig;
529 // Search for given tag in ICC profile directory
530 n = SearchOneTag(Icc, sig);
532 return -1; // Not found
535 return n; // Found, don't follow links
537 // Is this a linked tag?
538 LinkedSig = Icc ->TagLinked[n];
541 if (LinkedSig != (cmsTagSignature) 0) {
545 } while (LinkedSig != (cmsTagSignature) 0);
551 // Create a new tag entry
554 cmsBool _cmsNewTag(_cmsICCPROFILE* Icc, cmsTagSignature sig, int* NewPos)
558 // Search for the tag
559 i = _cmsSearchTag(Icc, sig, FALSE);
561 // Now let's do it easy. If the tag has been already written, that's an error
563 cmsSignalError(Icc ->ContextID, cmsERROR_ALREADY_DEFINED, "Tag '%x' already exists", sig);
570 if (Icc -> TagCount >= MAX_TABLE_TAG) {
571 cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", MAX_TABLE_TAG);
575 *NewPos = Icc ->TagCount;
584 cmsBool CMSEXPORT cmsIsTag(cmsHPROFILE hProfile, cmsTagSignature sig)
586 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) (void*) hProfile;
587 return _cmsSearchTag(Icc, sig, FALSE) >= 0;
591 // Read profile header and validate it
592 cmsBool _cmsReadHeader(_cmsICCPROFILE* Icc)
596 cmsUInt32Number i, j;
597 cmsUInt32Number HeaderSize;
598 cmsIOHANDLER* io = Icc ->IOhandler;
599 cmsUInt32Number TagCount;
603 if (io -> Read(io, &Header, sizeof(cmsICCHeader), 1) != 1) {
607 // Validate file as an ICC profile
608 if (_cmsAdjustEndianess32(Header.magic) != cmsMagicNumber) {
609 cmsSignalError(Icc ->ContextID, cmsERROR_BAD_SIGNATURE, "not an ICC profile, invalid signature");
613 // Adjust endianess of the used parameters
614 Icc -> DeviceClass = (cmsProfileClassSignature) _cmsAdjustEndianess32(Header.deviceClass);
615 Icc -> ColorSpace = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Header.colorSpace);
616 Icc -> PCS = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Header.pcs);
617 Icc -> RenderingIntent = _cmsAdjustEndianess32(Header.renderingIntent);
618 Icc -> flags = _cmsAdjustEndianess32(Header.flags);
619 Icc -> manufacturer = _cmsAdjustEndianess32(Header.manufacturer);
620 Icc -> model = _cmsAdjustEndianess32(Header.model);
621 _cmsAdjustEndianess64(&Icc -> attributes, &Header.attributes);
622 Icc -> Version = _cmsAdjustEndianess32(Header.version);
624 // Get size as reported in header
625 HeaderSize = _cmsAdjustEndianess32(Header.size);
627 // Make sure HeaderSize is lower than profile size
628 if (HeaderSize >= Icc ->IOhandler ->ReportedSize)
629 HeaderSize = Icc ->IOhandler ->ReportedSize;
632 // Get creation date/time
633 _cmsDecodeDateTimeNumber(&Header.date, &Icc ->Created);
635 // The profile ID are 32 raw bytes
636 memmove(Icc ->ProfileID.ID32, Header.profileID.ID32, 16);
639 // Read tag directory
640 if (!_cmsReadUInt32Number(io, &TagCount)) return FALSE;
641 if (TagCount > MAX_TABLE_TAG) {
643 cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", TagCount);
648 // Read tag directory
650 for (i=0; i < TagCount; i++) {
652 if (!_cmsReadUInt32Number(io, (cmsUInt32Number *) &Tag.sig)) return FALSE;
653 if (!_cmsReadUInt32Number(io, &Tag.offset)) return FALSE;
654 if (!_cmsReadUInt32Number(io, &Tag.size)) return FALSE;
656 // Perform some sanity check. Offset + size should fall inside file.
657 if (Tag.offset + Tag.size > HeaderSize ||
658 Tag.offset + Tag.size < Tag.offset)
661 Icc -> TagNames[Icc ->TagCount] = Tag.sig;
662 Icc -> TagOffsets[Icc ->TagCount] = Tag.offset;
663 Icc -> TagSizes[Icc ->TagCount] = Tag.size;
666 for (j=0; j < Icc ->TagCount; j++) {
668 if ((Icc ->TagOffsets[j] == Tag.offset) &&
669 (Icc ->TagSizes[j] == Tag.size)) {
671 Icc ->TagLinked[Icc ->TagCount] = Icc ->TagNames[j];
682 // Saves profile header
683 cmsBool _cmsWriteHeader(_cmsICCPROFILE* Icc, cmsUInt32Number UsedSpace)
688 cmsInt32Number Count = 0;
690 Header.size = _cmsAdjustEndianess32(UsedSpace);
691 Header.cmmId = _cmsAdjustEndianess32(lcmsSignature);
692 Header.version = _cmsAdjustEndianess32(Icc ->Version);
694 Header.deviceClass = (cmsProfileClassSignature) _cmsAdjustEndianess32(Icc -> DeviceClass);
695 Header.colorSpace = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Icc -> ColorSpace);
696 Header.pcs = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Icc -> PCS);
698 // NOTE: in v4 Timestamp must be in UTC rather than in local time
699 _cmsEncodeDateTimeNumber(&Header.date, &Icc ->Created);
701 Header.magic = _cmsAdjustEndianess32(cmsMagicNumber);
703 #ifdef CMS_IS_WINDOWS_
704 Header.platform = (cmsPlatformSignature) _cmsAdjustEndianess32(cmsSigMicrosoft);
706 Header.platform = (cmsPlatformSignature) _cmsAdjustEndianess32(cmsSigMacintosh);
709 Header.flags = _cmsAdjustEndianess32(Icc -> flags);
710 Header.manufacturer = _cmsAdjustEndianess32(Icc -> manufacturer);
711 Header.model = _cmsAdjustEndianess32(Icc -> model);
713 _cmsAdjustEndianess64(&Header.attributes, &Icc -> attributes);
715 // Rendering intent in the header (for embedded profiles)
716 Header.renderingIntent = _cmsAdjustEndianess32(Icc -> RenderingIntent);
718 // Illuminant is always D50
719 Header.illuminant.X = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(cmsD50_XYZ()->X));
720 Header.illuminant.Y = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(cmsD50_XYZ()->Y));
721 Header.illuminant.Z = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(cmsD50_XYZ()->Z));
723 // Created by LittleCMS (that's me!)
724 Header.creator = _cmsAdjustEndianess32(lcmsSignature);
726 memset(&Header.reserved, 0, sizeof(Header.reserved));
728 // Set profile ID. Endianess is always big endian
729 memmove(&Header.profileID, &Icc ->ProfileID, 16);
732 if (!Icc -> IOhandler->Write(Icc->IOhandler, sizeof(cmsICCHeader), &Header)) return FALSE;
734 // Saves Tag directory
737 for (i=0; i < Icc -> TagCount; i++) {
738 if (Icc ->TagNames[i] != 0)
742 // Store number of tags
743 if (!_cmsWriteUInt32Number(Icc ->IOhandler, Count)) return FALSE;
745 for (i=0; i < Icc -> TagCount; i++) {
747 if (Icc ->TagNames[i] == 0) continue; // It is just a placeholder
749 Tag.sig = (cmsTagSignature) _cmsAdjustEndianess32((cmsInt32Number) Icc -> TagNames[i]);
750 Tag.offset = _cmsAdjustEndianess32((cmsInt32Number) Icc -> TagOffsets[i]);
751 Tag.size = _cmsAdjustEndianess32((cmsInt32Number) Icc -> TagSizes[i]);
753 if (!Icc ->IOhandler -> Write(Icc-> IOhandler, sizeof(cmsTagEntry), &Tag)) return FALSE;
759 // ----------------------------------------------------------------------- Set/Get several struct members
762 cmsUInt32Number CMSEXPORT cmsGetHeaderRenderingIntent(cmsHPROFILE hProfile)
764 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
765 return Icc -> RenderingIntent;
768 void CMSEXPORT cmsSetHeaderRenderingIntent(cmsHPROFILE hProfile, cmsUInt32Number RenderingIntent)
770 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
771 Icc -> RenderingIntent = RenderingIntent;
774 cmsUInt32Number CMSEXPORT cmsGetHeaderFlags(cmsHPROFILE hProfile)
776 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
777 return (cmsUInt32Number) Icc -> flags;
780 void CMSEXPORT cmsSetHeaderFlags(cmsHPROFILE hProfile, cmsUInt32Number Flags)
782 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
783 Icc -> flags = (cmsUInt32Number) Flags;
786 cmsUInt32Number CMSEXPORT cmsGetHeaderManufacturer(cmsHPROFILE hProfile)
788 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
789 return (cmsUInt32Number) Icc ->manufacturer;
792 void CMSEXPORT cmsSetHeaderManufacturer(cmsHPROFILE hProfile, cmsUInt32Number manufacturer)
794 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
795 Icc -> manufacturer = (cmsUInt32Number) manufacturer;
798 cmsUInt32Number CMSEXPORT cmsGetHeaderModel(cmsHPROFILE hProfile)
800 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
801 return (cmsUInt32Number) Icc ->model;
804 void CMSEXPORT cmsSetHeaderModel(cmsHPROFILE hProfile, cmsUInt32Number model)
806 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
807 Icc -> model = (cmsUInt32Number) model;
811 void CMSEXPORT cmsGetHeaderAttributes(cmsHPROFILE hProfile, cmsUInt64Number* Flags)
813 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
814 memmove(Flags, &Icc -> attributes, sizeof(cmsUInt64Number));
817 void CMSEXPORT cmsSetHeaderAttributes(cmsHPROFILE hProfile, cmsUInt64Number Flags)
819 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
820 memmove(&Icc -> attributes, &Flags, sizeof(cmsUInt64Number));
823 void CMSEXPORT cmsGetHeaderProfileID(cmsHPROFILE hProfile, cmsUInt8Number* ProfileID)
825 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
826 memmove(ProfileID, Icc ->ProfileID.ID8, 16);
829 void CMSEXPORT cmsSetHeaderProfileID(cmsHPROFILE hProfile, cmsUInt8Number* ProfileID)
831 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
832 memmove(&Icc -> ProfileID, ProfileID, 16);
835 cmsBool CMSEXPORT cmsGetHeaderCreationDateTime(cmsHPROFILE hProfile, struct tm *Dest)
837 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
838 memmove(Dest, &Icc ->Created, sizeof(struct tm));
842 cmsColorSpaceSignature CMSEXPORT cmsGetPCS(cmsHPROFILE hProfile)
844 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
848 void CMSEXPORT cmsSetPCS(cmsHPROFILE hProfile, cmsColorSpaceSignature pcs)
850 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
854 cmsColorSpaceSignature CMSEXPORT cmsGetColorSpace(cmsHPROFILE hProfile)
856 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
857 return Icc -> ColorSpace;
860 void CMSEXPORT cmsSetColorSpace(cmsHPROFILE hProfile, cmsColorSpaceSignature sig)
862 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
863 Icc -> ColorSpace = sig;
866 cmsProfileClassSignature CMSEXPORT cmsGetDeviceClass(cmsHPROFILE hProfile)
868 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
869 return Icc -> DeviceClass;
872 void CMSEXPORT cmsSetDeviceClass(cmsHPROFILE hProfile, cmsProfileClassSignature sig)
874 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
875 Icc -> DeviceClass = sig;
878 cmsUInt32Number CMSEXPORT cmsGetEncodedICCversion(cmsHPROFILE hProfile)
880 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
881 return Icc -> Version;
884 void CMSEXPORT cmsSetEncodedICCversion(cmsHPROFILE hProfile, cmsUInt32Number Version)
886 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
887 Icc -> Version = Version;
890 // Get an hexadecimal number with same digits as v
892 cmsUInt32Number BaseToBase(cmsUInt32Number in, int BaseIn, int BaseOut)
898 for (len=0; in > 0 && len < 100; len++) {
900 Buff[len] = (char) (in % BaseIn);
904 for (i=len-1, out=0; i >= 0; --i) {
905 out = out * BaseOut + Buff[i];
911 void CMSEXPORT cmsSetProfileVersion(cmsHPROFILE hProfile, cmsFloat64Number Version)
913 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
917 Icc -> Version = BaseToBase((cmsUInt32Number) floor(Version * 100.0), 10, 16) << 16;
920 cmsFloat64Number CMSEXPORT cmsGetProfileVersion(cmsHPROFILE hProfile)
922 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
923 cmsUInt32Number n = Icc -> Version >> 16;
925 return BaseToBase(n, 16, 10) / 100.0;
927 // --------------------------------------------------------------------------------------------------------------
930 // Create profile from IOhandler
931 cmsHPROFILE CMSEXPORT cmsOpenProfileFromIOhandlerTHR(cmsContext ContextID, cmsIOHANDLER* io)
933 _cmsICCPROFILE* NewIcc;
934 cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
936 if (hEmpty == NULL) return NULL;
938 NewIcc = (_cmsICCPROFILE*) hEmpty;
940 NewIcc ->IOhandler = io;
941 if (!_cmsReadHeader(NewIcc)) goto Error;
945 cmsCloseProfile(hEmpty);
949 // Create profile from disk file
950 cmsHPROFILE CMSEXPORT cmsOpenProfileFromFileTHR(cmsContext ContextID, const char *lpFileName, const char *sAccess)
952 _cmsICCPROFILE* NewIcc;
953 cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
955 if (hEmpty == NULL) return NULL;
957 NewIcc = (_cmsICCPROFILE*) hEmpty;
959 NewIcc ->IOhandler = cmsOpenIOhandlerFromFile(ContextID, lpFileName, sAccess);
960 if (NewIcc ->IOhandler == NULL) goto Error;
962 if (*sAccess == 'W' || *sAccess == 'w') {
964 NewIcc -> IsWrite = TRUE;
969 if (!_cmsReadHeader(NewIcc)) goto Error;
973 cmsCloseProfile(hEmpty);
978 cmsHPROFILE CMSEXPORT cmsOpenProfileFromFile(const char *ICCProfile, const char *sAccess)
980 return cmsOpenProfileFromFileTHR(NULL, ICCProfile, sAccess);
984 cmsHPROFILE CMSEXPORT cmsOpenProfileFromStreamTHR(cmsContext ContextID, FILE* ICCProfile, const char *sAccess)
986 _cmsICCPROFILE* NewIcc;
987 cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
989 if (hEmpty == NULL) return NULL;
991 NewIcc = (_cmsICCPROFILE*) hEmpty;
993 NewIcc ->IOhandler = cmsOpenIOhandlerFromStream(ContextID, ICCProfile);
994 if (NewIcc ->IOhandler == NULL) goto Error;
996 if (*sAccess == 'w') {
998 NewIcc -> IsWrite = TRUE;
1002 if (!_cmsReadHeader(NewIcc)) goto Error;
1006 cmsCloseProfile(hEmpty);
1011 cmsHPROFILE CMSEXPORT cmsOpenProfileFromStream(FILE* ICCProfile, const char *sAccess)
1013 return cmsOpenProfileFromStreamTHR(NULL, ICCProfile, sAccess);
1017 // Open from memory block
1018 cmsHPROFILE CMSEXPORT cmsOpenProfileFromMemTHR(cmsContext ContextID, const void* MemPtr, cmsUInt32Number dwSize)
1020 _cmsICCPROFILE* NewIcc;
1023 hEmpty = cmsCreateProfilePlaceholder(ContextID);
1024 if (hEmpty == NULL) return NULL;
1026 NewIcc = (_cmsICCPROFILE*) hEmpty;
1028 // Ok, in this case const void* is casted to void* just because open IO handler
1029 // shares read and writting modes. Don't abuse this feature!
1030 NewIcc ->IOhandler = cmsOpenIOhandlerFromMem(ContextID, (void*) MemPtr, dwSize, "r");
1031 if (NewIcc ->IOhandler == NULL) goto Error;
1033 if (!_cmsReadHeader(NewIcc)) goto Error;
1038 cmsCloseProfile(hEmpty);
1042 cmsHPROFILE CMSEXPORT cmsOpenProfileFromMem(const void* MemPtr, cmsUInt32Number dwSize)
1044 return cmsOpenProfileFromMemTHR(NULL, MemPtr, dwSize);
1049 // Dump tag contents. If the profile is being modified, untouched tags are copied from FileOrig
1051 cmsBool SaveTags(_cmsICCPROFILE* Icc, _cmsICCPROFILE* FileOrig)
1053 cmsUInt8Number* Data;
1055 cmsUInt32Number Begin;
1056 cmsIOHANDLER* io = Icc ->IOhandler;
1057 cmsTagDescriptor* TagDescriptor;
1058 cmsTagTypeSignature TypeBase;
1059 cmsTagTypeHandler* TypeHandler;
1062 for (i=0; i < Icc -> TagCount; i++) {
1065 if (Icc ->TagNames[i] == 0) continue;
1067 // Linked tags are not written
1068 if (Icc ->TagLinked[i] != (cmsTagSignature) 0) continue;
1070 Icc -> TagOffsets[i] = Begin = io ->UsedSpace;
1072 Data = (cmsUInt8Number*) Icc -> TagPtrs[i];
1076 // Reach here if we are copying a tag from a disk-based ICC profile which has not been modified by user.
1077 // In this case a blind copy of the block data is performed
1078 if (FileOrig != NULL && Icc -> TagOffsets[i]) {
1080 cmsUInt32Number TagSize = FileOrig -> TagSizes[i];
1081 cmsUInt32Number TagOffset = FileOrig -> TagOffsets[i];
1084 if (!FileOrig ->IOhandler->Seek(FileOrig ->IOhandler, TagOffset)) return FALSE;
1086 Mem = _cmsMalloc(Icc ->ContextID, TagSize);
1087 if (Mem == NULL) return FALSE;
1089 if (FileOrig ->IOhandler->Read(FileOrig->IOhandler, Mem, TagSize, 1) != 1) return FALSE;
1090 if (!io ->Write(io, TagSize, Mem)) return FALSE;
1091 _cmsFree(Icc ->ContextID, Mem);
1093 Icc -> TagSizes[i] = (io ->UsedSpace - Begin);
1096 // Align to 32 bit boundary.
1097 if (! _cmsWriteAlignment(io))
1105 // Should this tag be saved as RAW? If so, tagsizes should be specified in advance (no further cooking is done)
1106 if (Icc ->TagSaveAsRaw[i]) {
1108 if (io -> Write(io, Icc ->TagSizes[i], Data) != 1) return FALSE;
1112 // Search for support on this tag
1113 TagDescriptor = _cmsGetTagDescriptor(Icc -> TagNames[i]);
1114 if (TagDescriptor == NULL) continue; // Unsupported, ignore it
1116 TypeHandler = Icc ->TagTypeHandlers[i];
1118 if (TypeHandler == NULL) {
1119 cmsSignalError(Icc ->ContextID, cmsERROR_INTERNAL, "(Internal) no handler for tag %x", Icc -> TagNames[i]);
1123 TypeBase = TypeHandler ->Signature;
1124 if (!_cmsWriteTypeBase(io, TypeBase))
1127 TypeHandler ->ContextID = Icc ->ContextID;
1128 TypeHandler ->ICCVersion = Icc ->Version;
1129 if (!TypeHandler ->WritePtr(TypeHandler, io, Data, TagDescriptor ->ElemCount)) {
1133 _cmsTagSignature2String(String, (cmsTagSignature) TypeBase);
1134 cmsSignalError(Icc ->ContextID, cmsERROR_WRITE, "Couldn't write type '%s'", String);
1140 Icc -> TagSizes[i] = (io ->UsedSpace - Begin);
1142 // Align to 32 bit boundary.
1143 if (! _cmsWriteAlignment(io))
1152 // Fill the offset and size fields for all linked tags
1154 cmsBool SetLinks( _cmsICCPROFILE* Icc)
1158 for (i=0; i < Icc -> TagCount; i++) {
1160 cmsTagSignature lnk = Icc ->TagLinked[i];
1161 if (lnk != (cmsTagSignature) 0) {
1163 int j = _cmsSearchTag(Icc, lnk, FALSE);
1166 Icc ->TagOffsets[i] = Icc ->TagOffsets[j];
1167 Icc ->TagSizes[i] = Icc ->TagSizes[j];
1176 // Low-level save to IOHANDLER. It returns the number of bytes used to
1177 // store the profile, or zero on error. io may be NULL and in this case
1178 // no data is written--only sizes are calculated
1179 cmsUInt32Number CMSEXPORT cmsSaveProfileToIOhandler(cmsHPROFILE hProfile, cmsIOHANDLER* io)
1181 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1182 _cmsICCPROFILE Keep;
1183 cmsIOHANDLER* PrevIO;
1184 cmsUInt32Number UsedSpace;
1185 cmsContext ContextID;
1187 memmove(&Keep, Icc, sizeof(_cmsICCPROFILE));
1189 ContextID = cmsGetProfileContextID(hProfile);
1190 PrevIO = Icc ->IOhandler = cmsOpenIOhandlerFromNULL(ContextID);
1191 if (PrevIO == NULL) return 0;
1193 // Pass #1 does compute offsets
1195 if (!_cmsWriteHeader(Icc, 0)) return 0;
1196 if (!SaveTags(Icc, &Keep)) return 0;
1198 UsedSpace = PrevIO ->UsedSpace;
1200 // Pass #2 does save to iohandler
1203 Icc ->IOhandler = io;
1204 if (!SetLinks(Icc)) goto CleanUp;
1205 if (!_cmsWriteHeader(Icc, UsedSpace)) goto CleanUp;
1206 if (!SaveTags(Icc, &Keep)) goto CleanUp;
1209 memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
1210 if (!cmsCloseIOhandler(PrevIO)) return 0;
1216 cmsCloseIOhandler(PrevIO);
1217 memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
1222 // Low-level save to disk.
1223 cmsBool CMSEXPORT cmsSaveProfileToFile(cmsHPROFILE hProfile, const char* FileName)
1225 cmsContext ContextID = cmsGetProfileContextID(hProfile);
1226 cmsIOHANDLER* io = cmsOpenIOhandlerFromFile(ContextID, FileName, "w");
1229 if (io == NULL) return FALSE;
1231 rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
1232 rc &= cmsCloseIOhandler(io);
1234 if (rc == FALSE) { // remove() is C99 per 7.19.4.1
1235 remove(FileName); // We have to IGNORE return value in this case
1240 // Same as anterior, but for streams
1241 cmsBool CMSEXPORT cmsSaveProfileToStream(cmsHPROFILE hProfile, FILE* Stream)
1244 cmsContext ContextID = cmsGetProfileContextID(hProfile);
1245 cmsIOHANDLER* io = cmsOpenIOhandlerFromStream(ContextID, Stream);
1247 if (io == NULL) return FALSE;
1249 rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
1250 rc &= cmsCloseIOhandler(io);
1256 // Same as anterior, but for memory blocks. In this case, a NULL as MemPtr means calculate needed space only
1257 cmsBool CMSEXPORT cmsSaveProfileToMem(cmsHPROFILE hProfile, void *MemPtr, cmsUInt32Number* BytesNeeded)
1261 cmsContext ContextID = cmsGetProfileContextID(hProfile);
1263 // Should we just calculate the needed space?
1264 if (MemPtr == NULL) {
1266 *BytesNeeded = cmsSaveProfileToIOhandler(hProfile, NULL);
1270 // That is a real write operation
1271 io = cmsOpenIOhandlerFromMem(ContextID, MemPtr, *BytesNeeded, "w");
1272 if (io == NULL) return FALSE;
1274 rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
1275 rc &= cmsCloseIOhandler(io);
1282 // Closes a profile freeing any involved resources
1283 cmsBool CMSEXPORT cmsCloseProfile(cmsHPROFILE hProfile)
1285 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1289 if (!Icc) return FALSE;
1291 // Was open in write mode?
1292 if (Icc ->IsWrite) {
1294 Icc ->IsWrite = FALSE; // Assure no further writting
1295 rc &= cmsSaveProfileToFile(hProfile, Icc ->IOhandler->PhysicalFile);
1298 for (i=0; i < Icc -> TagCount; i++) {
1300 if (Icc -> TagPtrs[i]) {
1302 cmsTagTypeHandler* TypeHandler = Icc ->TagTypeHandlers[i];
1304 if (TypeHandler != NULL) {
1306 TypeHandler ->ContextID = Icc ->ContextID; // As an additional parameters
1307 TypeHandler ->ICCVersion = Icc ->Version;
1308 TypeHandler ->FreePtr(TypeHandler, Icc -> TagPtrs[i]);
1311 _cmsFree(Icc ->ContextID, Icc ->TagPtrs[i]);
1315 if (Icc ->IOhandler != NULL) {
1316 rc &= cmsCloseIOhandler(Icc->IOhandler);
1319 _cmsFree(Icc ->ContextID, Icc); // Free placeholder memory
1325 // -------------------------------------------------------------------------------------------------------------------
1328 // Returns TRUE if a given tag is supported by a plug-in
1330 cmsBool IsTypeSupported(cmsTagDescriptor* TagDescriptor, cmsTagTypeSignature Type)
1332 cmsUInt32Number i, nMaxTypes;
1334 nMaxTypes = TagDescriptor->nSupportedTypes;
1335 if (nMaxTypes >= MAX_TYPES_IN_LCMS_PLUGIN)
1336 nMaxTypes = MAX_TYPES_IN_LCMS_PLUGIN;
1338 for (i=0; i < nMaxTypes; i++) {
1339 if (Type == TagDescriptor ->SupportedTypes[i]) return TRUE;
1346 // That's the main read function
1347 void* CMSEXPORT cmsReadTag(cmsHPROFILE hProfile, cmsTagSignature sig)
1349 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1350 cmsIOHANDLER* io = Icc ->IOhandler;
1351 cmsTagTypeHandler* TypeHandler;
1352 cmsTagDescriptor* TagDescriptor;
1353 cmsTagTypeSignature BaseType;
1354 cmsUInt32Number Offset, TagSize;
1355 cmsUInt32Number ElemCount;
1358 n = _cmsSearchTag(Icc, sig, TRUE);
1359 if (n < 0) return NULL; // Not found, return NULL
1362 // If the element is already in memory, return the pointer
1363 if (Icc -> TagPtrs[n]) {
1365 if (Icc ->TagSaveAsRaw[n]) return NULL; // We don't support read raw tags as cooked
1366 return Icc -> TagPtrs[n];
1369 // We need to read it. Get the offset and size to the file
1370 Offset = Icc -> TagOffsets[n];
1371 TagSize = Icc -> TagSizes[n];
1373 // Seek to its location
1374 if (!io -> Seek(io, Offset))
1377 // Search for support on this tag
1378 TagDescriptor = _cmsGetTagDescriptor(sig);
1379 if (TagDescriptor == NULL) return NULL; // Unsupported.
1381 // if supported, get type and check if in list
1382 BaseType = _cmsReadTypeBase(io);
1383 if (BaseType == 0) return NULL;
1385 if (!IsTypeSupported(TagDescriptor, BaseType)) return NULL;
1387 TagSize -= 8; // Alredy read by the type base logic
1390 TypeHandler = _cmsGetTagTypeHandler(BaseType);
1391 if (TypeHandler == NULL) return NULL;
1395 Icc -> TagTypeHandlers[n] = TypeHandler;
1397 TypeHandler ->ContextID = Icc ->ContextID;
1398 TypeHandler ->ICCVersion = Icc ->Version;
1399 Icc -> TagPtrs[n] = TypeHandler ->ReadPtr(TypeHandler, io, &ElemCount, TagSize);
1401 // The tag type is supported, but something wrong happend and we cannot read the tag.
1402 // let know the user about this (although it is just a warning)
1403 if (Icc -> TagPtrs[n] == NULL) {
1407 _cmsTagSignature2String(String, sig);
1408 cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "Corrupted tag '%s'", String);
1412 // This is a weird error that may be a symptom of something more serious, the number of
1413 // stored item is actually less than the number of required elements.
1414 if (ElemCount < TagDescriptor ->ElemCount) {
1418 _cmsTagSignature2String(String, sig);
1419 cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "'%s' Inconsistent number of items: expected %d, got %d",
1420 String, TagDescriptor ->ElemCount, ElemCount);
1425 return Icc -> TagPtrs[n];
1429 // Get true type of data
1430 cmsTagTypeSignature _cmsGetTagTrueType(cmsHPROFILE hProfile, cmsTagSignature sig)
1432 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1433 cmsTagTypeHandler* TypeHandler;
1436 // Search for given tag in ICC profile directory
1437 n = _cmsSearchTag(Icc, sig, TRUE);
1438 if (n < 0) return (cmsTagTypeSignature) 0; // Not found, return NULL
1440 // Get the handler. The true type is there
1441 TypeHandler = Icc -> TagTypeHandlers[n];
1442 return TypeHandler ->Signature;
1446 // Write a single tag. This just keeps track of the tak into a list of "to be written". If the tag is already
1447 // in that list, the previous version is deleted.
1448 cmsBool CMSEXPORT cmsWriteTag(cmsHPROFILE hProfile, cmsTagSignature sig, const void* data)
1450 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1451 cmsTagTypeHandler* TypeHandler = NULL;
1452 cmsTagDescriptor* TagDescriptor = NULL;
1453 cmsTagTypeSignature Type;
1455 cmsFloat64Number Version;
1456 char TypeString[5], SigString[5];
1461 i = _cmsSearchTag(Icc, sig, FALSE);
1463 Icc ->TagNames[i] = (cmsTagSignature) 0;
1464 // Unsupported by now, reserved for future ampliations (delete)
1468 i = _cmsSearchTag(Icc, sig, FALSE);
1471 if (Icc -> TagPtrs[i] != NULL) {
1473 // Already exists. Free previous version
1474 if (Icc ->TagSaveAsRaw[i]) {
1475 _cmsFree(Icc ->ContextID, Icc ->TagPtrs[i]);
1478 TypeHandler = Icc ->TagTypeHandlers[i];
1480 if (TypeHandler != NULL) {
1482 TypeHandler ->ContextID = Icc ->ContextID; // As an additional parameter
1483 TypeHandler ->ICCVersion = Icc ->Version;
1484 TypeHandler->FreePtr(TypeHandler, Icc -> TagPtrs[i]);
1491 i = Icc -> TagCount;
1493 if (i >= MAX_TABLE_TAG) {
1494 cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", MAX_TABLE_TAG);
1502 Icc ->TagSaveAsRaw[i] = FALSE;
1504 // This is not a link
1505 Icc ->TagLinked[i] = (cmsTagSignature) 0;
1507 // Get information about the TAG.
1508 TagDescriptor = _cmsGetTagDescriptor(sig);
1509 if (TagDescriptor == NULL){
1510 cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported tag '%x'", sig);
1515 // Now we need to know which type to use. It depends on the version.
1516 Version = cmsGetProfileVersion(hProfile);
1518 if (TagDescriptor ->DecideType != NULL) {
1520 // Let the tag descriptor to decide the type base on depending on
1521 // the data. This is useful for example on parametric curves, where
1522 // curves specified by a table cannot be saved as parametric and needs
1523 // to be revented to single v2-curves, even on v4 profiles.
1525 Type = TagDescriptor ->DecideType(Version, data);
1530 Type = TagDescriptor ->SupportedTypes[0];
1533 // Does the tag support this type?
1534 if (!IsTypeSupported(TagDescriptor, Type)) {
1536 _cmsTagSignature2String(TypeString, (cmsTagSignature) Type);
1537 _cmsTagSignature2String(SigString, sig);
1539 cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported type '%s' for tag '%s'", TypeString, SigString);
1543 // Does we have a handler for this type?
1544 TypeHandler = _cmsGetTagTypeHandler(Type);
1545 if (TypeHandler == NULL) {
1547 _cmsTagSignature2String(TypeString, (cmsTagSignature) Type);
1548 _cmsTagSignature2String(SigString, sig);
1550 cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported type '%s' for tag '%s'", TypeString, SigString);
1551 return FALSE; // Should never happen
1555 // Fill fields on icc structure
1556 Icc ->TagTypeHandlers[i] = TypeHandler;
1557 Icc ->TagNames[i] = sig;
1558 Icc ->TagSizes[i] = 0;
1559 Icc ->TagOffsets[i] = 0;
1561 TypeHandler ->ContextID = Icc ->ContextID;
1562 TypeHandler ->ICCVersion = Icc ->Version;
1563 Icc ->TagPtrs[i] = TypeHandler ->DupPtr(TypeHandler, data, TagDescriptor ->ElemCount);
1565 if (Icc ->TagPtrs[i] == NULL) {
1567 _cmsTagSignature2String(TypeString, (cmsTagSignature) Type);
1568 _cmsTagSignature2String(SigString, sig);
1569 cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "Malformed struct in type '%s' for tag '%s'", TypeString, SigString);
1577 // Read and write raw data. The only way those function would work and keep consistence with normal read and write
1578 // is to do an additional step of serialization. That means, readRaw would issue a normal read and then convert the obtained
1579 // data to raw bytes by using the "write" serialization logic. And vice-versa. I know this may end in situations where
1580 // raw data written does not exactly correspond with the raw data proposed to cmsWriteRaw data, but this approach allows
1581 // to write a tag as raw data and the read it as handled.
1583 cmsInt32Number CMSEXPORT cmsReadRawTag(cmsHPROFILE hProfile, cmsTagSignature sig, void* data, cmsUInt32Number BufferSize)
1585 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1588 cmsIOHANDLER* MemIO;
1589 cmsTagTypeHandler* TypeHandler = NULL;
1590 cmsTagDescriptor* TagDescriptor = NULL;
1592 cmsUInt32Number Offset, TagSize;
1594 // Search for given tag in ICC profile directory
1595 i = _cmsSearchTag(Icc, sig, TRUE);
1596 if (i < 0) return 0; // Not found, return 0
1598 // It is already read?
1599 if (Icc -> TagPtrs[i] == NULL) {
1601 // No yet, get original position
1602 Offset = Icc ->TagOffsets[i];
1603 TagSize = Icc ->TagSizes[i];
1606 // read the data directly, don't keep copy
1609 if (BufferSize < TagSize)
1610 TagSize = BufferSize;
1612 if (!Icc ->IOhandler ->Seek(Icc ->IOhandler, Offset)) return 0;
1613 if (!Icc ->IOhandler ->Read(Icc ->IOhandler, data, 1, TagSize)) return 0;
1616 return Icc ->TagSizes[i];
1619 // The data has been already read, or written. But wait!, maybe the user choosed to save as
1620 // raw data. In this case, return the raw data directly
1621 if (Icc ->TagSaveAsRaw[i]) {
1625 TagSize = Icc ->TagSizes[i];
1626 if (BufferSize < TagSize)
1627 TagSize = BufferSize;
1629 memmove(data, Icc ->TagPtrs[i], TagSize);
1632 return Icc ->TagSizes[i];
1635 // Already readed, or previously set by cmsWriteTag(). We need to serialize that
1636 // data to raw in order to maintain consistency.
1637 Object = cmsReadTag(hProfile, sig);
1638 if (Object == NULL) return 0;
1640 // Now we need to serialize to a memory block: just use a memory iohandler
1643 MemIO = cmsOpenIOhandlerFromNULL(cmsGetProfileContextID(hProfile));
1645 MemIO = cmsOpenIOhandlerFromMem(cmsGetProfileContextID(hProfile), data, BufferSize, "w");
1647 if (MemIO == NULL) return 0;
1649 // Obtain type handling for the tag
1650 TypeHandler = Icc ->TagTypeHandlers[i];
1651 TagDescriptor = _cmsGetTagDescriptor(sig);
1652 if (TagDescriptor == NULL) {
1653 cmsCloseIOhandler(MemIO);
1658 TypeHandler ->ContextID = Icc ->ContextID;
1659 TypeHandler ->ICCVersion = Icc ->Version;
1661 if (!_cmsWriteTypeBase(MemIO, TypeHandler ->Signature)) {
1662 cmsCloseIOhandler(MemIO);
1666 if (!TypeHandler ->WritePtr(TypeHandler, MemIO, Object, TagDescriptor ->ElemCount)) {
1667 cmsCloseIOhandler(MemIO);
1671 // Get Size and close
1672 rc = MemIO ->Tell(MemIO);
1673 cmsCloseIOhandler(MemIO); // Ignore return code this time
1678 // Similar to the anterior. This function allows to write directly to the ICC profile any data, without
1679 // checking anything. As a rule, mixing Raw with cooked doesn't work, so writting a tag as raw and then reading
1680 // it as cooked without serializing does result into an error. If that is wha you want, you will need to dump
1681 // the profile to memry or disk and then reopen it.
1682 cmsBool CMSEXPORT cmsWriteRawTag(cmsHPROFILE hProfile, cmsTagSignature sig, const void* data, cmsUInt32Number Size)
1684 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1687 if (!_cmsNewTag(Icc, sig, &i)) return FALSE;
1689 // Mark the tag as being written as RAW
1690 Icc ->TagSaveAsRaw[i] = TRUE;
1691 Icc ->TagNames[i] = sig;
1692 Icc ->TagLinked[i] = (cmsTagSignature) 0;
1694 // Keep a copy of the block
1695 Icc ->TagPtrs[i] = _cmsDupMem(Icc ->ContextID, data, Size);
1696 Icc ->TagSizes[i] = Size;
1701 // Using this function you can collapse several tag entries to the same block in the profile
1702 cmsBool CMSEXPORT cmsLinkTag(cmsHPROFILE hProfile, cmsTagSignature sig, cmsTagSignature dest)
1704 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1707 if (!_cmsNewTag(Icc, sig, &i)) return FALSE;
1709 // Keep necessary information
1710 Icc ->TagSaveAsRaw[i] = FALSE;
1711 Icc ->TagNames[i] = sig;
1712 Icc ->TagLinked[i] = dest;
1714 Icc ->TagPtrs[i] = NULL;
1715 Icc ->TagSizes[i] = 0;
1716 Icc ->TagOffsets[i] = 0;
1722 // Returns the tag linked to sig, in the case two tags are sharing same resource
1723 cmsTagSignature CMSEXPORT cmsTagLinkedTo(cmsHPROFILE hProfile, cmsTagSignature sig)
1725 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1728 // Search for given tag in ICC profile directory
1729 i = _cmsSearchTag(Icc, sig, FALSE);
1730 if (i < 0) return (cmsTagSignature) 0; // Not found, return 0
1732 return Icc -> TagLinked[i];