1 //---------------------------------------------------------------------------------
3 // Little Color Management System
4 // Copyright (c) 1998-2016 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 // Multilocalized unicode objects. That is an attempt to encapsulate i18n.
32 // Allocates an empty multi localizad unicode object
33 cmsMLU* CMSEXPORT cmsMLUalloc(cmsContext ContextID, cmsUInt32Number nItems)
37 // nItems should be positive if given
38 if (nItems <= 0) nItems = 2;
40 // Create the container
41 mlu = (cmsMLU*) _cmsMallocZero(ContextID, sizeof(cmsMLU));
42 if (mlu == NULL) return NULL;
44 mlu ->ContextID = ContextID;
47 mlu ->Entries = (_cmsMLUentry*) _cmsCalloc(ContextID, nItems, sizeof(_cmsMLUentry));
48 if (mlu ->Entries == NULL) {
49 _cmsFree(ContextID, mlu);
53 // Ok, keep indexes up to date
54 mlu ->AllocatedEntries = nItems;
55 mlu ->UsedEntries = 0;
61 // Grows a mempool table for a MLU. Each time this function is called, mempool size is multiplied times two.
63 cmsBool GrowMLUpool(cmsMLU* mlu)
69 if (mlu == NULL) return FALSE;
71 if (mlu ->PoolSize == 0)
74 size = mlu ->PoolSize * 2;
77 if (size < mlu ->PoolSize) return FALSE;
79 // Reallocate the pool
80 NewPtr = _cmsRealloc(mlu ->ContextID, mlu ->MemPool, size);
81 if (NewPtr == NULL) return FALSE;
84 mlu ->MemPool = NewPtr;
85 mlu ->PoolSize = size;
91 // Grows a entry table for a MLU. Each time this function is called, table size is multiplied times two.
93 cmsBool GrowMLUtable(cmsMLU* mlu)
95 cmsUInt32Number AllocatedEntries;
99 if (mlu == NULL) return FALSE;
101 AllocatedEntries = mlu ->AllocatedEntries * 2;
103 // Check for overflow
104 if (AllocatedEntries / 2 != mlu ->AllocatedEntries) return FALSE;
106 // Reallocate the memory
107 NewPtr = (_cmsMLUentry*)_cmsRealloc(mlu ->ContextID, mlu ->Entries, AllocatedEntries*sizeof(_cmsMLUentry));
108 if (NewPtr == NULL) return FALSE;
110 mlu ->Entries = NewPtr;
111 mlu ->AllocatedEntries = AllocatedEntries;
117 // Search for a specific entry in the structure. Language and Country are used.
119 int SearchMLUEntry(cmsMLU* mlu, cmsUInt16Number LanguageCode, cmsUInt16Number CountryCode)
124 if (mlu == NULL) return -1;
126 // Iterate whole table
127 for (i=0; i < mlu ->UsedEntries; i++) {
129 if (mlu ->Entries[i].Country == CountryCode &&
130 mlu ->Entries[i].Language == LanguageCode) return i;
137 // Add a block of characters to the intended MLU. Language and country are specified.
138 // Only one entry for Language/country pair is allowed.
140 cmsBool AddMLUBlock(cmsMLU* mlu, cmsUInt32Number size, const wchar_t *Block,
141 cmsUInt16Number LanguageCode, cmsUInt16Number CountryCode)
143 cmsUInt32Number Offset;
147 if (mlu == NULL) return FALSE;
149 // Is there any room available?
150 if (mlu ->UsedEntries >= mlu ->AllocatedEntries) {
151 if (!GrowMLUtable(mlu)) return FALSE;
154 // Only one ASCII string
155 if (SearchMLUEntry(mlu, LanguageCode, CountryCode) >= 0) return FALSE; // Only one is allowed!
158 while ((mlu ->PoolSize - mlu ->PoolUsed) < size) {
160 if (!GrowMLUpool(mlu)) return FALSE;
163 Offset = mlu ->PoolUsed;
165 Ptr = (cmsUInt8Number*) mlu ->MemPool;
166 if (Ptr == NULL) return FALSE;
169 memmove(Ptr + Offset, Block, size);
170 mlu ->PoolUsed += size;
172 mlu ->Entries[mlu ->UsedEntries].StrW = Offset;
173 mlu ->Entries[mlu ->UsedEntries].Len = size;
174 mlu ->Entries[mlu ->UsedEntries].Country = CountryCode;
175 mlu ->Entries[mlu ->UsedEntries].Language = LanguageCode;
181 // Convert from a 3-char code to a cmsUInt16Number. It is done inthis way because some
182 // compilers don't properly align beginning of strings
185 cmsUInt16Number strTo16(const char str[3])
187 cmsUInt16Number n = ((cmsUInt16Number) str[0] << 8) | str[1];
189 return n; // Always big endian in this case
193 void strFrom16(char str[3], cmsUInt16Number n)
195 // Assiming this would be aligned
203 c.n = n; // Always big endian in this case
205 str[0] = c.str[0]; str[1] = c.str[1]; str[2] = 0;
209 // Add an ASCII entry.
210 cmsBool CMSEXPORT cmsMLUsetASCII(cmsMLU* mlu, const char LanguageCode[3], const char CountryCode[3], const char* ASCIIString)
212 cmsUInt32Number i, len = (cmsUInt32Number) strlen(ASCIIString)+1;
215 cmsUInt16Number Lang = strTo16(LanguageCode);
216 cmsUInt16Number Cntry = strTo16(CountryCode);
218 if (mlu == NULL) return FALSE;
220 WStr = (wchar_t*) _cmsCalloc(mlu ->ContextID, len, sizeof(wchar_t));
221 if (WStr == NULL) return FALSE;
223 for (i=0; i < len; i++)
224 WStr[i] = (wchar_t) ASCIIString[i];
226 rc = AddMLUBlock(mlu, len * sizeof(wchar_t), WStr, Lang, Cntry);
228 _cmsFree(mlu ->ContextID, WStr);
233 // We don't need any wcs support library
235 cmsUInt32Number mywcslen(const wchar_t *s)
243 return (cmsUInt32Number)(p - s);
248 cmsBool CMSEXPORT cmsMLUsetWide(cmsMLU* mlu, const char Language[3], const char Country[3], const wchar_t* WideString)
250 cmsUInt16Number Lang = strTo16(Language);
251 cmsUInt16Number Cntry = strTo16(Country);
254 if (mlu == NULL) return FALSE;
255 if (WideString == NULL) return FALSE;
257 len = (cmsUInt32Number) (mywcslen(WideString) + 1) * sizeof(wchar_t);
258 return AddMLUBlock(mlu, len, WideString, Lang, Cntry);
261 // Duplicating a MLU is as easy as copying all members
262 cmsMLU* CMSEXPORT cmsMLUdup(const cmsMLU* mlu)
264 cmsMLU* NewMlu = NULL;
266 // Duplicating a NULL obtains a NULL
267 if (mlu == NULL) return NULL;
269 NewMlu = cmsMLUalloc(mlu ->ContextID, mlu ->UsedEntries);
270 if (NewMlu == NULL) return NULL;
272 // Should never happen
273 if (NewMlu ->AllocatedEntries < mlu ->UsedEntries)
277 if (NewMlu ->Entries == NULL || mlu ->Entries == NULL) goto Error;
279 memmove(NewMlu ->Entries, mlu ->Entries, mlu ->UsedEntries * sizeof(_cmsMLUentry));
280 NewMlu ->UsedEntries = mlu ->UsedEntries;
282 // The MLU may be empty
283 if (mlu ->PoolUsed == 0) {
284 NewMlu ->MemPool = NULL;
288 NewMlu ->MemPool = _cmsMalloc(mlu ->ContextID, mlu ->PoolUsed);
289 if (NewMlu ->MemPool == NULL) goto Error;
292 NewMlu ->PoolSize = mlu ->PoolUsed;
294 if (NewMlu ->MemPool == NULL || mlu ->MemPool == NULL) goto Error;
296 memmove(NewMlu ->MemPool, mlu->MemPool, mlu ->PoolUsed);
297 NewMlu ->PoolUsed = mlu ->PoolUsed;
303 if (NewMlu != NULL) cmsMLUfree(NewMlu);
307 // Free any used memory
308 void CMSEXPORT cmsMLUfree(cmsMLU* mlu)
312 if (mlu -> Entries) _cmsFree(mlu ->ContextID, mlu->Entries);
313 if (mlu -> MemPool) _cmsFree(mlu ->ContextID, mlu->MemPool);
315 _cmsFree(mlu ->ContextID, mlu);
320 // The algorithm first searches for an exact match of country and language, if not found it uses
321 // the Language. If none is found, first entry is used instead.
323 const wchar_t* _cmsMLUgetWide(const cmsMLU* mlu,
324 cmsUInt32Number *len,
325 cmsUInt16Number LanguageCode, cmsUInt16Number CountryCode,
326 cmsUInt16Number* UsedLanguageCode, cmsUInt16Number* UsedCountryCode)
329 cmsInt32Number Best = -1;
332 if (mlu == NULL) return NULL;
334 if (mlu -> AllocatedEntries <= 0) return NULL;
336 for (i=0; i < mlu ->UsedEntries; i++) {
338 v = mlu ->Entries + i;
340 if (v -> Language == LanguageCode) {
342 if (Best == -1) Best = i;
344 if (v -> Country == CountryCode) {
346 if (UsedLanguageCode != NULL) *UsedLanguageCode = v ->Language;
347 if (UsedCountryCode != NULL) *UsedCountryCode = v ->Country;
349 if (len != NULL) *len = v ->Len;
351 return (wchar_t*) ((cmsUInt8Number*) mlu ->MemPool + v -> StrW); // Found exact match
356 // No string found. Return First one
360 v = mlu ->Entries + Best;
362 if (UsedLanguageCode != NULL) *UsedLanguageCode = v ->Language;
363 if (UsedCountryCode != NULL) *UsedCountryCode = v ->Country;
365 if (len != NULL) *len = v ->Len;
367 return(wchar_t*) ((cmsUInt8Number*) mlu ->MemPool + v ->StrW);
371 // Obtain an ASCII representation of the wide string. Setting buffer to NULL returns the len
372 cmsUInt32Number CMSEXPORT cmsMLUgetASCII(const cmsMLU* mlu,
373 const char LanguageCode[3], const char CountryCode[3],
374 char* Buffer, cmsUInt32Number BufferSize)
377 cmsUInt32Number StrLen = 0;
378 cmsUInt32Number ASCIIlen, i;
380 cmsUInt16Number Lang = strTo16(LanguageCode);
381 cmsUInt16Number Cntry = strTo16(CountryCode);
384 if (mlu == NULL) return 0;
387 Wide = _cmsMLUgetWide(mlu, &StrLen, Lang, Cntry, NULL, NULL);
388 if (Wide == NULL) return 0;
390 ASCIIlen = StrLen / sizeof(wchar_t);
392 // Maybe we want only to know the len?
393 if (Buffer == NULL) return ASCIIlen + 1; // Note the zero at the end
395 // No buffer size means no data
396 if (BufferSize <= 0) return 0;
398 // Some clipping may be required
399 if (BufferSize < ASCIIlen + 1)
400 ASCIIlen = BufferSize - 1;
402 // Precess each character
403 for (i=0; i < ASCIIlen; i++) {
408 Buffer[i] = (char) Wide[i];
411 // We put a termination "\0"
412 Buffer[ASCIIlen] = 0;
416 // Obtain a wide representation of the MLU, on depending on current locale settings
417 cmsUInt32Number CMSEXPORT cmsMLUgetWide(const cmsMLU* mlu,
418 const char LanguageCode[3], const char CountryCode[3],
419 wchar_t* Buffer, cmsUInt32Number BufferSize)
422 cmsUInt32Number StrLen = 0;
424 cmsUInt16Number Lang = strTo16(LanguageCode);
425 cmsUInt16Number Cntry = strTo16(CountryCode);
428 if (mlu == NULL) return 0;
430 Wide = _cmsMLUgetWide(mlu, &StrLen, Lang, Cntry, NULL, NULL);
431 if (Wide == NULL) return 0;
433 // Maybe we want only to know the len?
434 if (Buffer == NULL) return StrLen + sizeof(wchar_t);
436 // No buffer size means no data
437 if (BufferSize <= 0) return 0;
439 // Some clipping may be required
440 if (BufferSize < StrLen + sizeof(wchar_t))
441 StrLen = BufferSize - + sizeof(wchar_t);
443 memmove(Buffer, Wide, StrLen);
444 Buffer[StrLen / sizeof(wchar_t)] = 0;
446 return StrLen + sizeof(wchar_t);
450 // Get also the language and country
451 CMSAPI cmsBool CMSEXPORT cmsMLUgetTranslation(const cmsMLU* mlu,
452 const char LanguageCode[3], const char CountryCode[3],
453 char ObtainedLanguage[3], char ObtainedCountry[3])
457 cmsUInt16Number Lang = strTo16(LanguageCode);
458 cmsUInt16Number Cntry = strTo16(CountryCode);
459 cmsUInt16Number ObtLang, ObtCode;
462 if (mlu == NULL) return FALSE;
464 Wide = _cmsMLUgetWide(mlu, NULL, Lang, Cntry, &ObtLang, &ObtCode);
465 if (Wide == NULL) return FALSE;
467 // Get used language and code
468 strFrom16(ObtainedLanguage, ObtLang);
469 strFrom16(ObtainedCountry, ObtCode);
476 // Get the number of translations in the MLU object
477 cmsUInt32Number CMSEXPORT cmsMLUtranslationsCount(const cmsMLU* mlu)
479 if (mlu == NULL) return 0;
480 return mlu->UsedEntries;
483 // Get the language and country codes for a specific MLU index
484 cmsBool CMSEXPORT cmsMLUtranslationsCodes(const cmsMLU* mlu,
486 char LanguageCode[3],
491 if (mlu == NULL) return FALSE;
493 if (idx >= mlu->UsedEntries) return FALSE;
495 entry = &mlu->Entries[idx];
497 strFrom16(LanguageCode, entry->Language);
498 strFrom16(CountryCode, entry->Country);
504 // Named color lists --------------------------------------------------------------------------------------------
506 // Grow the list to keep at least NumElements
508 cmsBool GrowNamedColorList(cmsNAMEDCOLORLIST* v)
510 cmsUInt32Number size;
511 _cmsNAMEDCOLOR * NewPtr;
513 if (v == NULL) return FALSE;
515 if (v ->Allocated == 0)
516 size = 64; // Initial guess
518 size = v ->Allocated * 2;
520 // Keep a maximum color lists can grow, 100K entries seems reasonable
521 if (size > 1024*100) return FALSE;
523 NewPtr = (_cmsNAMEDCOLOR*) _cmsRealloc(v ->ContextID, v ->List, size * sizeof(_cmsNAMEDCOLOR));
528 v ->Allocated = size;
532 // Allocate a list for n elements
533 cmsNAMEDCOLORLIST* CMSEXPORT cmsAllocNamedColorList(cmsContext ContextID, cmsUInt32Number n, cmsUInt32Number ColorantCount, const char* Prefix, const char* Suffix)
535 cmsNAMEDCOLORLIST* v = (cmsNAMEDCOLORLIST*) _cmsMallocZero(ContextID, sizeof(cmsNAMEDCOLORLIST));
537 if (v == NULL) return NULL;
541 v ->ContextID = ContextID;
543 while (v -> Allocated < n){
544 if (!GrowNamedColorList(v)) return NULL;
547 strncpy(v ->Prefix, Prefix, sizeof(v ->Prefix)-1);
548 strncpy(v ->Suffix, Suffix, sizeof(v ->Suffix)-1);
549 v->Prefix[32] = v->Suffix[32] = 0;
551 v -> ColorantCount = ColorantCount;
557 void CMSEXPORT cmsFreeNamedColorList(cmsNAMEDCOLORLIST* v)
559 if (v == NULL) return;
560 if (v ->List) _cmsFree(v ->ContextID, v ->List);
561 _cmsFree(v ->ContextID, v);
564 cmsNAMEDCOLORLIST* CMSEXPORT cmsDupNamedColorList(const cmsNAMEDCOLORLIST* v)
566 cmsNAMEDCOLORLIST* NewNC;
568 if (v == NULL) return NULL;
570 NewNC= cmsAllocNamedColorList(v ->ContextID, v -> nColors, v ->ColorantCount, v ->Prefix, v ->Suffix);
571 if (NewNC == NULL) return NULL;
573 // For really large tables we need this
574 while (NewNC ->Allocated < v ->Allocated){
575 if (!GrowNamedColorList(NewNC)) return NULL;
578 memmove(NewNC ->Prefix, v ->Prefix, sizeof(v ->Prefix));
579 memmove(NewNC ->Suffix, v ->Suffix, sizeof(v ->Suffix));
580 NewNC ->ColorantCount = v ->ColorantCount;
581 memmove(NewNC->List, v ->List, v->nColors * sizeof(_cmsNAMEDCOLOR));
582 NewNC ->nColors = v ->nColors;
587 // Append a color to a list. List pointer may change if reallocated
588 cmsBool CMSEXPORT cmsAppendNamedColor(cmsNAMEDCOLORLIST* NamedColorList,
590 cmsUInt16Number PCS[3], cmsUInt16Number Colorant[cmsMAXCHANNELS])
594 if (NamedColorList == NULL) return FALSE;
596 if (NamedColorList ->nColors + 1 > NamedColorList ->Allocated) {
597 if (!GrowNamedColorList(NamedColorList)) return FALSE;
600 for (i=0; i < NamedColorList ->ColorantCount; i++)
601 NamedColorList ->List[NamedColorList ->nColors].DeviceColorant[i] = Colorant == NULL? 0 : Colorant[i];
603 for (i=0; i < 3; i++)
604 NamedColorList ->List[NamedColorList ->nColors].PCS[i] = PCS == NULL ? 0 : PCS[i];
608 strncpy(NamedColorList ->List[NamedColorList ->nColors].Name, Name, cmsMAX_PATH-1);
609 NamedColorList ->List[NamedColorList ->nColors].Name[cmsMAX_PATH-1] = 0;
613 NamedColorList ->List[NamedColorList ->nColors].Name[0] = 0;
616 NamedColorList ->nColors++;
620 // Returns number of elements
621 cmsUInt32Number CMSEXPORT cmsNamedColorCount(const cmsNAMEDCOLORLIST* NamedColorList)
623 if (NamedColorList == NULL) return 0;
624 return NamedColorList ->nColors;
627 // Info aboout a given color
628 cmsBool CMSEXPORT cmsNamedColorInfo(const cmsNAMEDCOLORLIST* NamedColorList, cmsUInt32Number nColor,
632 cmsUInt16Number* PCS,
633 cmsUInt16Number* Colorant)
635 if (NamedColorList == NULL) return FALSE;
637 if (nColor >= cmsNamedColorCount(NamedColorList)) return FALSE;
639 if (Name) strcpy(Name, NamedColorList->List[nColor].Name);
640 if (Prefix) strcpy(Prefix, NamedColorList->Prefix);
641 if (Suffix) strcpy(Suffix, NamedColorList->Suffix);
643 memmove(PCS, NamedColorList ->List[nColor].PCS, 3*sizeof(cmsUInt16Number));
646 memmove(Colorant, NamedColorList ->List[nColor].DeviceColorant,
647 sizeof(cmsUInt16Number) * NamedColorList ->ColorantCount);
653 // Search for a given color name (no prefix or suffix)
654 cmsInt32Number CMSEXPORT cmsNamedColorIndex(const cmsNAMEDCOLORLIST* NamedColorList, const char* Name)
658 if (NamedColorList == NULL) return -1;
659 n = cmsNamedColorCount(NamedColorList);
660 for (i=0; i < n; i++) {
661 if (cmsstrcasecmp(Name, NamedColorList->List[i].Name) == 0)
668 // MPE support -----------------------------------------------------------------------------------------------------------------
671 void FreeNamedColorList(cmsStage* mpe)
673 cmsNAMEDCOLORLIST* List = (cmsNAMEDCOLORLIST*) mpe ->Data;
674 cmsFreeNamedColorList(List);
678 void* DupNamedColorList(cmsStage* mpe)
680 cmsNAMEDCOLORLIST* List = (cmsNAMEDCOLORLIST*) mpe ->Data;
681 return cmsDupNamedColorList(List);
685 void EvalNamedColorPCS(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
687 cmsNAMEDCOLORLIST* NamedColorList = (cmsNAMEDCOLORLIST*) mpe ->Data;
688 cmsUInt16Number index = (cmsUInt16Number) _cmsQuickSaturateWord(In[0] * 65535.0);
690 if (index >= NamedColorList-> nColors) {
691 cmsSignalError(NamedColorList ->ContextID, cmsERROR_RANGE, "Color %d out of range; ignored", index);
695 // Named color always uses Lab
696 Out[0] = (cmsFloat32Number) (NamedColorList->List[index].PCS[0] / 65535.0);
697 Out[1] = (cmsFloat32Number) (NamedColorList->List[index].PCS[1] / 65535.0);
698 Out[2] = (cmsFloat32Number) (NamedColorList->List[index].PCS[2] / 65535.0);
703 void EvalNamedColor(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
705 cmsNAMEDCOLORLIST* NamedColorList = (cmsNAMEDCOLORLIST*) mpe ->Data;
706 cmsUInt16Number index = (cmsUInt16Number) _cmsQuickSaturateWord(In[0] * 65535.0);
709 if (index >= NamedColorList-> nColors) {
710 cmsSignalError(NamedColorList ->ContextID, cmsERROR_RANGE, "Color %d out of range; ignored", index);
713 for (j=0; j < NamedColorList ->ColorantCount; j++)
714 Out[j] = (cmsFloat32Number) (NamedColorList->List[index].DeviceColorant[j] / 65535.0);
719 // Named color lookup element
720 cmsStage* _cmsStageAllocNamedColor(cmsNAMEDCOLORLIST* NamedColorList, cmsBool UsePCS)
722 return _cmsStageAllocPlaceholder(NamedColorList ->ContextID,
723 cmsSigNamedColorElemType,
724 1, UsePCS ? 3 : NamedColorList ->ColorantCount,
725 UsePCS ? EvalNamedColorPCS : EvalNamedColor,
728 cmsDupNamedColorList(NamedColorList));
733 // Retrieve the named color list from a transform. Should be first element in the LUT
734 cmsNAMEDCOLORLIST* CMSEXPORT cmsGetNamedColorList(cmsHTRANSFORM xform)
736 _cmsTRANSFORM* v = (_cmsTRANSFORM*) xform;
737 cmsStage* mpe = v ->Lut->Elements;
739 if (mpe ->Type != cmsSigNamedColorElemType) return NULL;
740 return (cmsNAMEDCOLORLIST*) mpe ->Data;
744 // Profile sequence description routines -------------------------------------------------------------------------------------
746 cmsSEQ* CMSEXPORT cmsAllocProfileSequenceDescription(cmsContext ContextID, cmsUInt32Number n)
751 if (n == 0) return NULL;
753 // In a absolutely arbitrary way, I hereby decide to allow a maxim of 255 profiles linked
754 // in a devicelink. It makes not sense anyway and may be used for exploits, so let's close the door!
755 if (n > 255) return NULL;
757 Seq = (cmsSEQ*) _cmsMallocZero(ContextID, sizeof(cmsSEQ));
758 if (Seq == NULL) return NULL;
760 Seq -> ContextID = ContextID;
761 Seq -> seq = (cmsPSEQDESC*) _cmsCalloc(ContextID, n, sizeof(cmsPSEQDESC));
764 if (Seq -> seq == NULL) {
765 _cmsFree(ContextID, Seq);
769 for (i=0; i < n; i++) {
770 Seq -> seq[i].Manufacturer = NULL;
771 Seq -> seq[i].Model = NULL;
772 Seq -> seq[i].Description = NULL;
778 void CMSEXPORT cmsFreeProfileSequenceDescription(cmsSEQ* pseq)
782 for (i=0; i < pseq ->n; i++) {
783 if (pseq ->seq[i].Manufacturer != NULL) cmsMLUfree(pseq ->seq[i].Manufacturer);
784 if (pseq ->seq[i].Model != NULL) cmsMLUfree(pseq ->seq[i].Model);
785 if (pseq ->seq[i].Description != NULL) cmsMLUfree(pseq ->seq[i].Description);
788 if (pseq ->seq != NULL) _cmsFree(pseq ->ContextID, pseq ->seq);
789 _cmsFree(pseq -> ContextID, pseq);
792 cmsSEQ* CMSEXPORT cmsDupProfileSequenceDescription(const cmsSEQ* pseq)
800 NewSeq = (cmsSEQ*) _cmsMalloc(pseq -> ContextID, sizeof(cmsSEQ));
801 if (NewSeq == NULL) return NULL;
804 NewSeq -> seq = (cmsPSEQDESC*) _cmsCalloc(pseq ->ContextID, pseq ->n, sizeof(cmsPSEQDESC));
805 if (NewSeq ->seq == NULL) goto Error;
807 NewSeq -> ContextID = pseq ->ContextID;
808 NewSeq -> n = pseq ->n;
810 for (i=0; i < pseq->n; i++) {
812 memmove(&NewSeq ->seq[i].attributes, &pseq ->seq[i].attributes, sizeof(cmsUInt64Number));
814 NewSeq ->seq[i].deviceMfg = pseq ->seq[i].deviceMfg;
815 NewSeq ->seq[i].deviceModel = pseq ->seq[i].deviceModel;
816 memmove(&NewSeq ->seq[i].ProfileID, &pseq ->seq[i].ProfileID, sizeof(cmsProfileID));
817 NewSeq ->seq[i].technology = pseq ->seq[i].technology;
819 NewSeq ->seq[i].Manufacturer = cmsMLUdup(pseq ->seq[i].Manufacturer);
820 NewSeq ->seq[i].Model = cmsMLUdup(pseq ->seq[i].Model);
821 NewSeq ->seq[i].Description = cmsMLUdup(pseq ->seq[i].Description);
829 cmsFreeProfileSequenceDescription(NewSeq);
833 // Dictionaries --------------------------------------------------------------------------------------------------------
835 // Dictionaries are just very simple linked lists
838 typedef struct _cmsDICT_struct {
840 cmsContext ContextID;
844 // Allocate an empty dictionary
845 cmsHANDLE CMSEXPORT cmsDictAlloc(cmsContext ContextID)
847 _cmsDICT* dict = (_cmsDICT*) _cmsMallocZero(ContextID, sizeof(_cmsDICT));
848 if (dict == NULL) return NULL;
850 dict ->ContextID = ContextID;
851 return (cmsHANDLE) dict;
856 void CMSEXPORT cmsDictFree(cmsHANDLE hDict)
858 _cmsDICT* dict = (_cmsDICT*) hDict;
859 cmsDICTentry *entry, *next;
861 _cmsAssert(dict != NULL);
863 // Walk the list freeing all nodes
865 while (entry != NULL) {
867 if (entry ->DisplayName != NULL) cmsMLUfree(entry ->DisplayName);
868 if (entry ->DisplayValue != NULL) cmsMLUfree(entry ->DisplayValue);
869 if (entry ->Name != NULL) _cmsFree(dict ->ContextID, entry -> Name);
870 if (entry ->Value != NULL) _cmsFree(dict ->ContextID, entry -> Value);
872 // Don't fall in the habitual trap...
874 _cmsFree(dict ->ContextID, entry);
879 _cmsFree(dict ->ContextID, dict);
883 // Duplicate a wide char string
885 wchar_t* DupWcs(cmsContext ContextID, const wchar_t* ptr)
887 if (ptr == NULL) return NULL;
888 return (wchar_t*) _cmsDupMem(ContextID, ptr, (mywcslen(ptr) + 1) * sizeof(wchar_t));
891 // Add a new entry to the linked list
892 cmsBool CMSEXPORT cmsDictAddEntry(cmsHANDLE hDict, const wchar_t* Name, const wchar_t* Value, const cmsMLU *DisplayName, const cmsMLU *DisplayValue)
894 _cmsDICT* dict = (_cmsDICT*) hDict;
897 _cmsAssert(dict != NULL);
898 _cmsAssert(Name != NULL);
900 entry = (cmsDICTentry*) _cmsMallocZero(dict ->ContextID, sizeof(cmsDICTentry));
901 if (entry == NULL) return FALSE;
903 entry ->DisplayName = cmsMLUdup(DisplayName);
904 entry ->DisplayValue = cmsMLUdup(DisplayValue);
905 entry ->Name = DupWcs(dict ->ContextID, Name);
906 entry ->Value = DupWcs(dict ->ContextID, Value);
908 entry ->Next = dict ->head;
915 // Duplicates an existing dictionary
916 cmsHANDLE CMSEXPORT cmsDictDup(cmsHANDLE hDict)
918 _cmsDICT* old_dict = (_cmsDICT*) hDict;
922 _cmsAssert(old_dict != NULL);
924 hNew = cmsDictAlloc(old_dict ->ContextID);
925 if (hNew == NULL) return NULL;
927 // Walk the list freeing all nodes
928 entry = old_dict ->head;
929 while (entry != NULL) {
931 if (!cmsDictAddEntry(hNew, entry ->Name, entry ->Value, entry ->DisplayName, entry ->DisplayValue)) {
937 entry = entry -> Next;
943 // Get a pointer to the linked list
944 const cmsDICTentry* CMSEXPORT cmsDictGetEntryList(cmsHANDLE hDict)
946 _cmsDICT* dict = (_cmsDICT*) hDict;
948 if (dict == NULL) return NULL;
952 // Helper For external languages
953 const cmsDICTentry* CMSEXPORT cmsDictNextEntry(const cmsDICTentry* e)
955 if (e == NULL) return NULL;