/* * 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. // //--------------------------------------------------------------------------------- //
// Conversion functions. From floating point to 16 bits static void FromFloatTo16(const cmsFloat32Number In[], cmsUInt16Number Out[], cmsUInt32Number n)
{
cmsUInt32Number i;
for (i=0; i < n; i++) {
Out[i] = _cmsQuickSaturateWord(In[i] * 65535.0);
}
}
// From 16 bits to floating point static void From16ToFloat(const cmsUInt16Number In[], cmsFloat32Number Out[], cmsUInt32Number n)
{
cmsUInt32Number i;
for (i=0; i < n; i++) {
Out[i] = (cmsFloat32Number) In[i] / 65535.0F;
}
}
// This function is quite useful to analyze the structure of a LUT and retrieve the MPE elements // that conform the LUT. It should be called with the LUT, the number of expected elements and // then a list of expected types followed with a list of cmsFloat64Number pointers to MPE elements. If // the function founds a match with current pipeline, it fills the pointers and returns TRUE // if not, returns FALSE without touching anything. Setting pointers to NULL does bypass // the storage process.
cmsBool CMSEXPORT cmsPipelineCheckAndRetreiveStages(const cmsPipeline* Lut, cmsUInt32Number n, ...)
{
va_list args;
cmsUInt32Number i;
cmsStage* mpe;
cmsStageSignature Type; void** ElemPtr;
// Make sure same number of elements if (cmsPipelineStageCount(Lut) != n) returnFALSE;
va_start(args, n);
// Iterate across asked types
mpe = Lut ->Elements; for (i=0; i < n; i++) {
// Get asked type. cmsStageSignature is promoted to int by compiler
Type = (cmsStageSignature)va_arg(args, int); if (mpe ->Type != Type) {
va_end(args); // Mismatch. We are done. returnFALSE;
}
mpe = mpe ->Next;
}
// Found a combination, fill pointers if not NULL
mpe = Lut ->Elements; for (i=0; i < n; i++) {
// Below there are implementations for several types of elements. Each type may be implemented by a // evaluation function, a duplication function, a function to free resources and a constructor.
// ************************************************************************************************* // Type cmsSigCurveSetElemType (curves) // *************************************************************************************************
// ************************************************************************************************* // Type cmsSigMatrixElemType (Matrices) // *************************************************************************************************
// Special care should be taken here because precision loss. A temporary cmsFloat64Number buffer is being used static void EvaluateMatrix(const cmsFloat32Number In[],
cmsFloat32Number Out[], const cmsStage *mpe)
{
cmsUInt32Number i, j;
_cmsStageMatrixData* Data = (_cmsStageMatrixData*) mpe ->Data;
cmsFloat64Number Tmp;
// Input is already in 0..1.0 notation for (i=0; i < mpe ->OutputChannels; i++) {
// Given an hypercube of b dimensions, with Dims[] number of nodes by dimension, calculate the total amount of nodes static
cmsUInt32Number CubeSize(const cmsUInt32Number Dims[], cmsUInt32Number b)
{
cmsUInt32Number rv, dim;
_cmsAssert(Dims != NULL);
for (rv = 1; b > 0; b--) {
dim = Dims[b-1]; if (dim <= 1) return 0; // Error
rv *= dim;
// Check for overflow if (rv > UINT_MAX / dim) return 0;
}
NewElem ->nEntries = Data ->nEntries;
NewElem ->HasFloatValues = Data ->HasFloatValues;
if (Data ->Tab.T) {
if (Data ->HasFloatValues) {
NewElem ->Tab.TFloat = (cmsFloat32Number*) _cmsDupMem(mpe ->ContextID, Data ->Tab.TFloat, Data ->nEntries * sizeof (cmsFloat32Number)); if (NewElem ->Tab.TFloat == NULL) goto Error;
} else {
NewElem ->Tab.T = (cmsUInt16Number*) _cmsDupMem(mpe ->ContextID, Data ->Tab.T, Data ->nEntries * sizeof (cmsUInt16Number)); if (NewElem ->Tab.T == NULL) goto Error;
}
}
NewElem ->Params = _cmsComputeInterpParamsEx(mpe ->ContextID,
Data ->Params ->nSamples,
Data ->Params ->nInputs,
Data ->Params ->nOutputs,
NewElem ->Tab.T,
Data ->Params ->dwFlags); if (NewElem->Params != NULL) return (void*) NewElem;
Error: if (NewElem->Tab.T) // This works for both types
_cmsFree(mpe ->ContextID, NewElem -> Tab.T);
_cmsFree(mpe ->ContextID, NewElem); return NULL;
}
static void CLutElemTypeFree(cmsStage* mpe)
{
_cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data;
// Already empty if (Data == NULL) return;
// This works for both types if (Data -> Tab.T)
_cmsFree(mpe ->ContextID, Data -> Tab.T);
// Allocates a 16-bit multidimensional CLUT. This is evaluated at 16-bit precision. Table may have different // granularity on each dimension.
cmsStage* CMSEXPORT cmsStageAllocCLut16bitGranular(cmsContext ContextID, const cmsUInt32Number clutPoints[],
cmsUInt32Number inputChan,
cmsUInt32Number outputChan, const cmsUInt16Number* Table)
{
cmsUInt32Number i, n;
_cmsStageCLutData* NewElem;
cmsStage* NewMPE;
_cmsAssert(clutPoints != NULL);
if (inputChan > MAX_INPUT_DIMENSIONS) {
cmsSignalError(ContextID, cmsERROR_RANGE, "Too many input channels (%d channels, max=%d)", inputChan, MAX_INPUT_DIMENSIONS); return NULL;
}
// There is a potential integer overflow on conputing n and nEntries.
NewElem -> nEntries = n = outputChan * CubeSize(clutPoints, inputChan);
NewElem -> HasFloatValues = TRUE;
if (n == 0) {
cmsStageFree(NewMPE); return NULL;
}
NewElem ->Tab.TFloat = (cmsFloat32Number*) _cmsCalloc(ContextID, n, sizeof(cmsFloat32Number)); if (NewElem ->Tab.TFloat == NULL) {
cmsStageFree(NewMPE); return NULL;
}
if (Table != NULL) { for (i=0; i < n; i++) {
NewElem ->Tab.TFloat[i] = Table[i];
}
}
static int IdentitySampler(CMSREGISTER const cmsUInt16Number In[], CMSREGISTER cmsUInt16Number Out[], CMSREGISTER void * Cargo)
{ int nChan = *(int*) Cargo; int i;
for (i=0; i < nChan; i++)
Out[i] = In[i];
return 1;
}
// Creates an MPE that just copies input to output
cmsStage* CMSEXPORT _cmsStageAllocIdentityCLut(cmsContext ContextID, cmsUInt32Number nChan)
{
cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS];
cmsStage* mpe ; int i;
for (i=0; i < MAX_INPUT_DIMENSIONS; i++)
Dimensions[i] = 2;
// Quantize a value 0 <= i < MaxSamples to 0..0xffff
cmsUInt16Number CMSEXPORT _cmsQuantizeVal(cmsFloat64Number i, cmsUInt32Number MaxSamples)
{
cmsFloat64Number x;
x = ((cmsFloat64Number) i * 65535.) / (cmsFloat64Number) (MaxSamples - 1); return _cmsQuickSaturateWord(x);
}
// This routine does a sweep on whole input space, and calls its callback // function on knots. returns TRUE if all ok, FALSE otherwise.
cmsBool CMSEXPORT cmsStageSampleCLut16bit(cmsStage* mpe, cmsSAMPLER16 Sampler, void * Cargo, cmsUInt32Number dwFlags)
{ int i, t, index, rest;
cmsUInt32Number nTotalPoints;
cmsUInt32Number nInputs, nOutputs;
cmsUInt32Number* nSamples;
cmsUInt16Number In[MAX_INPUT_DIMENSIONS+1], Out[MAX_STAGE_CHANNELS];
_cmsStageCLutData* clut;
if (nInputs <= 0) returnFALSE; if (nOutputs <= 0) returnFALSE; if (nInputs > MAX_INPUT_DIMENSIONS) returnFALSE; if (nOutputs >= MAX_STAGE_CHANNELS) returnFALSE;
if (nInputs <= 0) returnFALSE; if (nOutputs <= 0) returnFALSE; if (nInputs > MAX_INPUT_DIMENSIONS) returnFALSE; if (nOutputs >= MAX_STAGE_CHANNELS) returnFALSE;
nTotalPoints = CubeSize(nSamples, nInputs); if (nTotalPoints == 0) returnFALSE;
index = 0; for (i = 0; i < (int)nTotalPoints; i++) {
if (clut ->Tab.TFloat != NULL) { for (t=0; t < (int) nOutputs; t++)
Out[t] = clut->Tab.TFloat[index + t];
}
if (!Sampler(In, Out, Cargo)) returnFALSE;
if (!(dwFlags & SAMPLER_INSPECT)) {
if (clut ->Tab.TFloat != NULL) { for (t=0; t < (int) nOutputs; t++)
clut->Tab.TFloat[index + t] = Out[t];
}
}
index += nOutputs;
}
returnTRUE;
}
// This routine does a sweep on whole input space, and calls its callback // function on knots. returns TRUE if all ok, FALSE otherwise.
cmsBool CMSEXPORT cmsSliceSpace16(cmsUInt32Number nInputs, const cmsUInt32Number clutPoints[],
cmsSAMPLER16 Sampler, void * Cargo)
{ int i, t, rest;
cmsUInt32Number nTotalPoints;
cmsUInt16Number In[cmsMAXCHANNELS];
if (nInputs >= cmsMAXCHANNELS) returnFALSE;
nTotalPoints = CubeSize(clutPoints, nInputs); if (nTotalPoints == 0) returnFALSE;
// No dup or free routines needed, as the structure has no pointers in it.
cmsStage* CMSEXPORT _cmsStageAllocLab2XYZ(cmsContext ContextID)
{ return _cmsStageAllocPlaceholder(ContextID, cmsSigLab2XYZElemType, 3, 3, EvaluateLab2XYZ, NULL, NULL, NULL);
}
// v2 L=100 is supposed to be placed on 0xFF00. There is no reasonable // number of gridpoints that would make exact match. However, a prelinearization // of 258 entries, would map 0xFF00 exactly on entry 257, and this is good to avoid scum dot. // Almost all what we need but unfortunately, the rest of entries should be scaled by // (255*257/256) and this is not exact.
cmsStage* _cmsStageAllocLabV2ToV4curves(cmsContext ContextID)
{
cmsStage* mpe;
cmsToneCurve* LabTable[3]; int i, j;
if (LabTable[j] == NULL) {
cmsFreeToneCurveTriple(LabTable); return NULL;
}
// We need to map * (0xffff / 0xff00), that's same as (257 / 256) // So we can use 258-entry tables to do the trick (i / 257) * (255 * 257) * (257 / 256); for (i=0; i < 257; i++) {
// To Lab to float. Note that the MPE gives numbers in normal Lab range // and we need 0..1.0 range for the formatters // L* : 0...100 => 0...1.0 (L* / 100) // ab* : -128..+127 to 0..1 ((ab* + 128) / 255)
// Fom XYZ to floating point PCS
cmsStage* _cmsStageNormalizeFromXyzFloat(cmsContext ContextID)
{ #define n (32768.0/65535.0) staticconst cmsFloat64Number a1[] = {
n, 0, 0,
0, n, 0,
0, 0, n
}; #undef n
// This function sets up the channel count static
cmsBool BlessLUT(cmsPipeline* lut)
{ // We can set the input/output channels only if we have elements. if (lut ->Elements != NULL) {
// Unlink an element and return the pointer to it void CMSEXPORT cmsPipelineUnlinkStage(cmsPipeline* lut, cmsStageLoc loc, cmsStage** mpe)
{
cmsStage *Anterior, *pt, *Last;
cmsStage *Unlinked = NULL;
// If empty LUT, there is nothing to remove if (lut ->Elements == NULL) { if (mpe) *mpe = NULL; return;
}
// On depending on the strategy... switch (loc) {
case cmsAT_BEGIN:
{
cmsStage* elem = lut ->Elements;
case cmsAT_END:
Anterior = Last = NULL; for (pt = lut ->Elements;
pt != NULL;
pt = pt -> Next) {
Anterior = Last;
Last = pt;
}
Unlinked = Last; // Next already points to NULL
// Truncate the chain if (Anterior)
Anterior ->Next = NULL; else
lut ->Elements = NULL; break; default:;
}
if (mpe)
*mpe = Unlinked; else
cmsStageFree(Unlinked);
// May fail, but we ignore it
BlessLUT(lut);
}
// Concatenate two LUT into a new single one
cmsBool CMSEXPORT cmsPipelineCat(cmsPipeline* l1, const cmsPipeline* l2)
{
cmsStage* mpe;
// If both LUTS does not have elements, we need to inherit // the number of channels if (l1 ->Elements == NULL && l2 ->Elements == NULL) {
l1 ->InputChannels = l2 ->InputChannels;
l1 ->OutputChannels = l2 ->OutputChannels;
}
// Cat second for (mpe = l2 ->Elements;
mpe != NULL;
mpe = mpe ->Next) {
// We have to dup each element if (!cmsPipelineInsertStage(l1, cmsAT_END, cmsStageDup(mpe))) returnFALSE;
}
// This function may be used to set the optional evaluator and a block of private data. If private data is being used, an optional // duplicator and free functions should also be specified in order to duplicate the LUT construct. Use NULL to inhibit such functionality. void CMSEXPORT _cmsPipelineSetOptimizationParameters(cmsPipeline* Lut,
_cmsPipelineEval16Fn Eval16, void* PrivateData,
_cmsFreeUserDataFn FreePrivateDataFn,
_cmsDupUserDataFn DupPrivateDataFn)
{
// ----------------------------------------------------------- Reverse interpolation // Here's how it goes. The derivative Df(x) of the function f is the linear // transformation that best approximates f near the point x. It can be represented // by a matrix A whose entries are the partial derivatives of the components of f // with respect to all the coordinates. This is know as the Jacobian // // The best linear approximation to f is given by the matrix equation: // // y-y0 = A (x-x0) // // So, if x0 is a good "guess" for the zero of f, then solving for the zero of this // linear approximation will give a "better guess" for the zero of f. Thus let y=0, // and since y0=f(x0) one can solve the above equation for x. This leads to the // Newton's method formula: // // xn+1 = xn - A-1 f(xn) // // where xn+1 denotes the (n+1)-st guess, obtained from the n-th guess xn in the // fashion described above. Iterating this will give better and better approximations // if you have a "good enough" initial guess.
// Increment with reflexion on boundary static void IncDelta(cmsFloat32Number *Val)
{ if (*Val < (1.0 - JACOBIAN_EPSILON))
*Val += JACOBIAN_EPSILON;
else
*Val -= JACOBIAN_EPSILON;
}
// Euclidean distance between two vectors of n elements each one static
cmsFloat32Number EuclideanDistance(cmsFloat32Number a[], cmsFloat32Number b[], int n)
{
cmsFloat32Number sum = 0; int i;
for (i=0; i < n; i++) {
cmsFloat32Number dif = b[i] - a[i];
sum += dif * dif;
}
return sqrtf(sum);
}
// Evaluate a LUT in reverse direction. It only searches on 3->3 LUT. Uses Newton method // // x1 <- x - [J(x)]^-1 * f(x) // // lut: The LUT on where to do the search // Target: LabK, 3 values of Lab plus destination K which is fixed // Result: The obtained CMYK // Hint: Location where begin the search
// Only 3->3 and 4->3 are supported if (lut ->InputChannels != 3 && lut ->InputChannels != 4) returnFALSE; if (lut ->OutputChannels != 3) returnFALSE;
// Take the hint as starting point if specified if (Hint == NULL) {
// Begin at any point, we choose 1/3 of CMY axis
x[0] = x[1] = x[2] = 0.3f;
} else {
// Only copy 3 channels from hint... for (j=0; j < 3; j++)
x[j] = Hint[j];
}
// If Lut is 4-dimensions, then grab target[3], which is fixed if (lut ->InputChannels == 4) {
x[3] = Target[3];
} else x[3] = 0; // To keep lint happy
// Iterate for (i = 0; i < INVERSION_MAX_ITERATIONS; i++) {
// Get beginning fx
cmsPipelineEvalFloat(x, fx, lut);
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.