/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
// This file is available under and governed by the GNU General Public // License version 2 only, as published by the Free Software Foundation. // However, the following notice accompanied the original version of this // file: // //--------------------------------------------------------------------------------- // // Little Color Management System // Copyright (c) 1998-2022 Marti Maria Saguer // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the Software // is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // //--------------------------------------------------------------------------------- //
#include"lcms2_internal.h"
// Multilocalized unicode objects. That is an attempt to encapsulate i18n.
// Allocates an empty multi localizad unicode object
cmsMLU* CMSEXPORT cmsMLUalloc(cmsContext ContextID, cmsUInt32Number nItems)
{
cmsMLU* mlu;
// nItems should be positive if given if (nItems <= 0) nItems = 2;
// Create the container
mlu = (cmsMLU*) _cmsMallocZero(ContextID, sizeof(cmsMLU)); if (mlu == NULL) return NULL;
// Ok, keep indexes up to date
mlu ->AllocatedEntries = nItems;
mlu ->UsedEntries = 0;
return mlu;
}
// Grows a mempool table for a MLU. Each time this function is called, mempool size is multiplied times two. static
cmsBool GrowMLUpool(cmsMLU* mlu)
{
cmsUInt32Number size; void *NewPtr;
// Check for overflow if (size < mlu ->PoolSize) returnFALSE;
// Reallocate the pool
NewPtr = _cmsRealloc(mlu ->ContextID, mlu ->MemPool, size); if (NewPtr == NULL) returnFALSE;
mlu ->MemPool = NewPtr;
mlu ->PoolSize = size;
returnTRUE;
}
// Grows a entry table for a MLU. Each time this function is called, table size is multiplied times two. static
cmsBool GrowMLUtable(cmsMLU* mlu)
{
cmsUInt32Number AllocatedEntries;
_cmsMLUentry *NewPtr;
// Sanity check if (mlu == NULL) returnFALSE;
AllocatedEntries = mlu ->AllocatedEntries * 2;
// Check for overflow if (AllocatedEntries / 2 != mlu ->AllocatedEntries) returnFALSE;
// Reallocate the memory
NewPtr = (_cmsMLUentry*)_cmsRealloc(mlu ->ContextID, mlu ->Entries, AllocatedEntries*sizeof(_cmsMLUentry)); if (NewPtr == NULL) returnFALSE;
// Search for a specific entry in the structure. Language and Country are used. static int SearchMLUEntry(cmsMLU* mlu, cmsUInt16Number LanguageCode, cmsUInt16Number CountryCode)
{
cmsUInt32Number i;
// Sanity check if (mlu == NULL) return -1;
// Iterate whole table for (i=0; i < mlu ->UsedEntries; i++) {
// Add a block of characters to the intended MLU. Language and country are specified. // Only one entry for Language/country pair is allowed. static
cmsBool AddMLUBlock(cmsMLU* mlu, cmsUInt32Number size, constwchar_t *Block,
cmsUInt16Number LanguageCode, cmsUInt16Number CountryCode)
{
cmsUInt32Number Offset;
cmsUInt8Number* Ptr;
// Sanity check if (mlu == NULL) returnFALSE;
// Is there any room available? if (mlu ->UsedEntries >= mlu ->AllocatedEntries) { if (!GrowMLUtable(mlu)) returnFALSE;
}
// Only one ASCII string if (SearchMLUEntry(mlu, LanguageCode, CountryCode) >= 0) returnFALSE; // Only one is allowed!
// Check for size while ((mlu ->PoolSize - mlu ->PoolUsed) < size) {
if (!GrowMLUpool(mlu)) returnFALSE;
}
Offset = mlu ->PoolUsed;
Ptr = (cmsUInt8Number*) mlu ->MemPool; if (Ptr == NULL) returnFALSE;
// Set the entry
memmove(Ptr + Offset, Block, size);
mlu ->PoolUsed += size;
// Convert from a 3-char code to a cmsUInt16Number. It is done in this way because some // compilers don't properly align beginning of strings static
cmsUInt16Number strTo16(constchar str[3])
{ const cmsUInt8Number* ptr8;
cmsUInt16Number n;
// For non-existent strings if (str == NULL) return 0;
ptr8 = (const cmsUInt8Number*)str;
n = (cmsUInt16Number)(((cmsUInt16Number)ptr8[0] << 8) | ptr8[1]);
// Add an ASCII entry. Do not add any \0 termination (ICC1v43_2010-12.pdf page 61) // In the case the user explicitly sets an empty string, we force a \0
cmsBool CMSEXPORT cmsMLUsetASCII(cmsMLU* mlu, constchar LanguageCode[3], constchar CountryCode[3], constchar* ASCIIString)
{
cmsUInt32Number i, len = (cmsUInt32Number) strlen(ASCIIString); wchar_t* WStr;
cmsBool rc;
cmsUInt16Number Lang = strTo16(LanguageCode);
cmsUInt16Number Cntry = strTo16(CountryCode);
if (mlu == NULL) returnFALSE;
// len == 0 would prevent operation, so we set a empty string pointing to zero if (len == 0)
{
len = 1;
}
// The MLU may be empty if (mlu ->PoolUsed == 0) {
NewMlu ->MemPool = NULL;
} else { // It is not empty
NewMlu ->MemPool = _cmsMalloc(mlu ->ContextID, mlu ->PoolUsed); if (NewMlu ->MemPool == NULL) goto Error;
}
if (NewMlu != NULL) cmsMLUfree(NewMlu); return NULL;
}
// Free any used memory void CMSEXPORT cmsMLUfree(cmsMLU* mlu)
{ if (mlu) {
if (mlu -> Entries) _cmsFree(mlu ->ContextID, mlu->Entries); if (mlu -> MemPool) _cmsFree(mlu ->ContextID, mlu->MemPool);
_cmsFree(mlu ->ContextID, mlu);
}
}
// The algorithm first searches for an exact match of country and language, if not found it uses // the Language. If none is found, first entry is used instead. static constwchar_t* _cmsMLUgetWide(const cmsMLU* mlu,
cmsUInt32Number *len,
cmsUInt16Number LanguageCode, cmsUInt16Number CountryCode,
cmsUInt16Number* UsedLanguageCode, cmsUInt16Number* UsedCountryCode)
{
cmsUInt32Number i; int Best = -1;
_cmsMLUentry* v;
if (mlu == NULL) return NULL;
if (mlu -> AllocatedEntries <= 0) return NULL;
for (i=0; i < mlu ->UsedEntries; i++) {
v = mlu ->Entries + i;
if (v -> Language == LanguageCode) {
if (Best == -1) Best = (int) i;
if (v -> Country == CountryCode) {
if (UsedLanguageCode != NULL) *UsedLanguageCode = v ->Language; if (UsedCountryCode != NULL) *UsedCountryCode = v ->Country;
if (len != NULL) *len = v ->Len;
return (wchar_t*) ((cmsUInt8Number*) mlu ->MemPool + v -> StrW); // Found exact match
}
}
}
// No string found. Return First one if (Best == -1)
Best = 0;
v = mlu ->Entries + Best;
if (UsedLanguageCode != NULL) *UsedLanguageCode = v ->Language; if (UsedCountryCode != NULL) *UsedCountryCode = v ->Country;
if (len != NULL) *len = v ->Len;
return(wchar_t*) ((cmsUInt8Number*) mlu ->MemPool + v ->StrW);
}
// Obtain an ASCII representation of the wide string. Setting buffer to NULL returns the len
cmsUInt32Number CMSEXPORT cmsMLUgetASCII(const cmsMLU* mlu, constchar LanguageCode[3], constchar CountryCode[3], char* Buffer, cmsUInt32Number BufferSize)
{ constwchar_t *Wide;
cmsUInt32Number StrLen = 0;
cmsUInt32Number ASCIIlen, i;
cmsUInt16Number Lang = strTo16(LanguageCode);
cmsUInt16Number Cntry = strTo16(CountryCode);
// Sanitize if (mlu == NULL) return 0;
// Get WideChar
Wide = _cmsMLUgetWide(mlu, &StrLen, Lang, Cntry, NULL, NULL); if (Wide == NULL) return 0;
ASCIIlen = StrLen / sizeof(wchar_t);
// Maybe we want only to know the len? if (Buffer == NULL) return ASCIIlen + 1; // Note the zero at the end
// No buffer size means no data if (BufferSize <= 0) return 0;
// Some clipping may be required if (BufferSize < ASCIIlen + 1)
ASCIIlen = BufferSize - 1;
// Precess each character for (i=0; i < ASCIIlen; i++) {
// Get also the language and country
CMSAPI cmsBool CMSEXPORT cmsMLUgetTranslation(const cmsMLU* mlu, constchar LanguageCode[3], constchar CountryCode[3], char ObtainedLanguage[3], char ObtainedCountry[3])
{ constwchar_t *Wide;
// Get used language and code
strFrom16(ObtainedLanguage, ObtLang);
strFrom16(ObtainedCountry, ObtCode);
returnTRUE;
}
// Get the number of translations in the MLU object
cmsUInt32Number CMSEXPORT cmsMLUtranslationsCount(const cmsMLU* mlu)
{ if (mlu == NULL) return 0; return mlu->UsedEntries;
}
// Get the language and country codes for a specific MLU index
cmsBool CMSEXPORT cmsMLUtranslationsCodes(const cmsMLU* mlu,
cmsUInt32Number idx, char LanguageCode[3], char CountryCode[3])
{
_cmsMLUentry *entry;
// Named color lists --------------------------------------------------------------------------------------------
// Grow the list to keep at least NumElements static
cmsBool GrowNamedColorList(cmsNAMEDCOLORLIST* v)
{
cmsUInt32Number size;
_cmsNAMEDCOLOR * NewPtr;
if (v == NULL) returnFALSE;
if (v ->Allocated == 0)
size = 64; // Initial guess else
size = v ->Allocated * 2;
// Keep a maximum color lists can grow, 100K entries seems reasonable if (size > 1024 * 100) {
_cmsFree(v->ContextID, (void*) v->List);
v->List = NULL; returnFALSE;
}
NewPtr = (_cmsNAMEDCOLOR*) _cmsRealloc(v ->ContextID, v ->List, size * sizeof(_cmsNAMEDCOLOR)); if (NewPtr == NULL) returnFALSE;
v ->List = NewPtr;
v ->Allocated = size; returnTRUE;
}
// Allocate a list for n elements
cmsNAMEDCOLORLIST* CMSEXPORT cmsAllocNamedColorList(cmsContext ContextID, cmsUInt32Number n, cmsUInt32Number ColorantCount, constchar* Prefix, constchar* Suffix)
{
cmsNAMEDCOLORLIST* v;
if (ColorantCount > cmsMAXCHANNELS) return NULL;
v = (cmsNAMEDCOLORLIST*)_cmsMallocZero(ContextID, sizeof(cmsNAMEDCOLORLIST)); if (v == NULL) return NULL;
v ->List = NULL;
v ->nColors = 0;
v ->ContextID = ContextID;
while (v -> Allocated < n) { if (!GrowNamedColorList(v)) {
cmsFreeNamedColorList(v); return NULL;
}
}
// Free a list void CMSEXPORT cmsFreeNamedColorList(cmsNAMEDCOLORLIST* v)
{ if (v == NULL) return; if (v ->List) _cmsFree(v ->ContextID, v ->List);
_cmsFree(v ->ContextID, v);
}
NewNC= cmsAllocNamedColorList(v ->ContextID, v -> nColors, v ->ColorantCount, v ->Prefix, v ->Suffix); if (NewNC == NULL) return NULL;
// For really large tables we need this while (NewNC ->Allocated < v ->Allocated){ if (!GrowNamedColorList(NewNC))
{
cmsFreeNamedColorList(NewNC); return NULL;
}
}
memmove(NewNC ->Prefix, v ->Prefix, sizeof(v ->Prefix));
memmove(NewNC ->Suffix, v ->Suffix, sizeof(v ->Suffix));
NewNC ->ColorantCount = v ->ColorantCount;
memmove(NewNC->List, v ->List, v->nColors * sizeof(_cmsNAMEDCOLOR));
NewNC ->nColors = v ->nColors; return NewNC;
}
// Append a color to a list. List pointer may change if reallocated
cmsBool CMSEXPORT cmsAppendNamedColor(cmsNAMEDCOLORLIST* NamedColorList, constchar* Name,
cmsUInt16Number PCS[3], cmsUInt16Number Colorant[cmsMAXCHANNELS])
{
cmsUInt32Number i;
if (NamedColorList == NULL) returnFALSE;
if (NamedColorList ->nColors + 1 > NamedColorList ->Allocated) { if (!GrowNamedColorList(NamedColorList)) returnFALSE;
}
for (i=0; i < NamedColorList ->ColorantCount; i++)
NamedColorList ->List[NamedColorList ->nColors].DeviceColorant[i] = Colorant == NULL ? (cmsUInt16Number)0 : Colorant[i];
for (i=0; i < 3; i++)
NamedColorList ->List[NamedColorList ->nColors].PCS[i] = PCS == NULL ? (cmsUInt16Number) 0 : PCS[i];
// Returns number of elements
cmsUInt32Number CMSEXPORT cmsNamedColorCount(const cmsNAMEDCOLORLIST* NamedColorList)
{ if (NamedColorList == NULL) return 0; return NamedColorList ->nColors;
}
// Info about a given color
cmsBool CMSEXPORT cmsNamedColorInfo(const cmsNAMEDCOLORLIST* NamedColorList, cmsUInt32Number nColor, char* Name, char* Prefix, char* Suffix,
cmsUInt16Number* PCS,
cmsUInt16Number* Colorant)
{ if (NamedColorList == NULL) returnFALSE;
if (nColor >= cmsNamedColorCount(NamedColorList)) returnFALSE;
// strcpy instead of strncpy because many apps are using small buffers if (Name) strcpy(Name, NamedColorList->List[nColor].Name); if (Prefix) strcpy(Prefix, NamedColorList->Prefix); if (Suffix) strcpy(Suffix, NamedColorList->Suffix); if (PCS)
memmove(PCS, NamedColorList ->List[nColor].PCS, 3*sizeof(cmsUInt16Number));
if (Colorant)
memmove(Colorant, NamedColorList ->List[nColor].DeviceColorant, sizeof(cmsUInt16Number) * NamedColorList ->ColorantCount);
returnTRUE;
}
// Search for a given color name (no prefix or suffix)
cmsInt32Number CMSEXPORT cmsNamedColorIndex(const cmsNAMEDCOLORLIST* NamedColorList, constchar* Name)
{
cmsUInt32Number i;
cmsUInt32Number n;
if (NamedColorList == NULL) return -1;
n = cmsNamedColorCount(NamedColorList); for (i=0; i < n; i++) { if (cmsstrcasecmp(Name, NamedColorList->List[i].Name) == 0) return (cmsInt32Number) i;
}
return -1;
}
// MPE support -----------------------------------------------------------------------------------------------------------------
// Retrieve the named color list from a transform. Should be first element in the LUT
cmsNAMEDCOLORLIST* CMSEXPORT cmsGetNamedColorList(cmsHTRANSFORM xform)
{
_cmsTRANSFORM* v = (_cmsTRANSFORM*) xform;
cmsStage* mpe = v ->Lut->Elements;
// In a absolutely arbitrary way, I hereby decide to allow a maxim of 255 profiles linked // in a devicelink. It makes not sense anyway and may be used for exploits, so let's close the door! if (n > 255) return NULL;
// Get a pointer to the linked list const cmsDICTentry* CMSEXPORT cmsDictGetEntryList(cmsHANDLE hDict)
{
_cmsDICT* dict = (_cmsDICT*) hDict;
if (dict == NULL) return NULL; return dict ->head;
}
// Helper For external languages const cmsDICTentry* CMSEXPORT cmsDictNextEntry(const cmsDICTentry* e)
{ if (e == NULL) return NULL; return e ->Next;
}
¤ Dauer der Verarbeitung: 0.21 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.