jdk/src/share/native/sun/java2d/cmm/lcms/cmsgamma.c
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2394 404cbe399601
child 6482 0f6a4442b29e
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2394
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2394
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2394
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2394
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2394
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
// This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
// License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
// However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
// file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
//  Little cms
2394
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
    32
//  Copyright (C) 1998-2007 Marti Maria
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
// Permission is hereby granted, free of charge, to any person obtaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
// a copy of this software and associated documentation files (the "Software"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
// to deal in the Software without restriction, including without limitation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
// and/or sell copies of the Software, and to permit persons to whom the Software
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
// is furnished to do so, subject to the following conditions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
// The above copyright notice and this permission notice shall be included in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
// all copies or substantial portions of the Software.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#include "lcms.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
// Gamma handling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
LPGAMMATABLE LCMSEXPORT cmsAllocGamma(int nEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
void         LCMSEXPORT cmsFreeGamma(LPGAMMATABLE Gamma);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
void         LCMSEXPORT cmsFreeGammaTriple(LPGAMMATABLE Gamma[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
LPGAMMATABLE LCMSEXPORT cmsBuildGamma(int nEntries, double Gamma);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
LPGAMMATABLE LCMSEXPORT cmsDupGamma(LPGAMMATABLE Src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
LPGAMMATABLE LCMSEXPORT cmsReverseGamma(int nResultSamples, LPGAMMATABLE InGamma);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
LPGAMMATABLE LCMSEXPORT cmsBuildParametricGamma(int nEntries, int Type, double Params[]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
LPGAMMATABLE LCMSEXPORT cmsJoinGamma(LPGAMMATABLE InGamma, LPGAMMATABLE OutGamma);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
LPGAMMATABLE LCMSEXPORT cmsJoinGammaEx(LPGAMMATABLE InGamma, LPGAMMATABLE OutGamma, int nPoints);
2394
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
    66
LCMSBOOL         LCMSEXPORT cmsSmoothGamma(LPGAMMATABLE Tab, double lambda);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
2394
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
    68
LCMSBOOL         cdecl _cmsSmoothEndpoints(LPWORD Table, int nPoints);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
// Sampled curves
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
LPSAMPLEDCURVE cdecl cmsAllocSampledCurve(int nItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
void           cdecl cmsFreeSampledCurve(LPSAMPLEDCURVE p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
void           cdecl cmsEndpointsOfSampledCurve(LPSAMPLEDCURVE p, double* Min, double* Max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
void           cdecl cmsClampSampledCurve(LPSAMPLEDCURVE p, double Min, double Max);
2394
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
    77
LCMSBOOL       cdecl cmsSmoothSampledCurve(LPSAMPLEDCURVE Tab, double SmoothingLambda);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
void           cdecl cmsRescaleSampledCurve(LPSAMPLEDCURVE p, double Min, double Max, int nPoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
LPSAMPLEDCURVE cdecl cmsJoinSampledCurves(LPSAMPLEDCURVE X, LPSAMPLEDCURVE Y, int nResultingPoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
double LCMSEXPORT cmsEstimateGamma(LPGAMMATABLE t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
double LCMSEXPORT cmsEstimateGammaEx(LPWORD GammaTable, int nEntries, double Thereshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
// ----------------------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
#define MAX_KNOTS   4096
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
typedef float vec[MAX_KNOTS+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
// Ciclic-redundant-check for assuring table is a true representation of parametric curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
// The usual polynomial, which is used for AAL5, FDDI, and probably Ethernet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
#define QUOTIENT 0x04c11db7
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
static
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
unsigned int Crc32(unsigned int result, LPVOID ptr, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    int          i,j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    BYTE         octet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    LPBYTE       data = (LPBYTE) ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    for (i=0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        octet = *data++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        for (j=0; j < 8; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (result & 0x80000000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                result = (result << 1) ^ QUOTIENT ^ (octet >> 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                result = (result << 1) ^ (octet >> 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            octet <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
// Get CRC of gamma table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
unsigned int _cmsCrc32OfGammaTable(LPGAMMATABLE Table)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    unsigned int crc = ~0U;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    crc = Crc32(crc, &Table -> Seed.Type,  sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    crc = Crc32(crc, Table ->Seed.Params,  sizeof(double)*10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    crc = Crc32(crc, &Table ->nEntries,    sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    crc = Crc32(crc, Table ->GammaTable,   sizeof(WORD) * Table -> nEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    return ~crc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
LPGAMMATABLE LCMSEXPORT cmsAllocGamma(int nEntries)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
       LPGAMMATABLE p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
       size_t size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
2394
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   146
       if (nEntries > 65530 || nEntries <= 0) {
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   147
                cmsSignalError(LCMS_ERRC_ABORTED, "Couldn't create gammatable of more than 65530 entries");
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   148
                return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
       size = sizeof(GAMMATABLE) + (sizeof(WORD) * (nEntries-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
2394
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   153
       p = (LPGAMMATABLE) _cmsMalloc(size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
       if (!p) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
       ZeroMemory(p, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
       p -> Seed.Type     = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
       p -> nEntries = nEntries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
       return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
void LCMSEXPORT cmsFreeGamma(LPGAMMATABLE Gamma)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
{
2394
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   166
       if (Gamma)  _cmsFree(Gamma);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
void LCMSEXPORT cmsFreeGammaTriple(LPGAMMATABLE Gamma[3])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    cmsFreeGamma(Gamma[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    cmsFreeGamma(Gamma[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    cmsFreeGamma(Gamma[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    Gamma[0] = Gamma[1] = Gamma[2] = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
// Duplicate a gamma table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
LPGAMMATABLE  LCMSEXPORT cmsDupGamma(LPGAMMATABLE In)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
       LPGAMMATABLE Ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
       size_t size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
       Ptr = cmsAllocGamma(In -> nEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
       if (Ptr == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
       size = sizeof(GAMMATABLE) + (sizeof(WORD) * (In -> nEntries-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
       CopyMemory(Ptr, In, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
       return Ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
// Handle gamma using interpolation tables. The resulting curves can become
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
// very stange, but are pleasent to eye.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
LPGAMMATABLE LCMSEXPORT cmsJoinGamma(LPGAMMATABLE InGamma,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                          LPGAMMATABLE OutGamma)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
       register int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
       L16PARAMS L16In, L16Out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
       LPWORD InPtr, OutPtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
       LPGAMMATABLE p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
       p = cmsAllocGamma(256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
       if (!p) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
       cmsCalcL16Params(InGamma -> nEntries, &L16In);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
       InPtr  = InGamma -> GammaTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
       cmsCalcL16Params(OutGamma -> nEntries, &L16Out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
       OutPtr = OutGamma-> GammaTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
       for (i=0; i < 256; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
       {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
              WORD wValIn, wValOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
              wValIn  = cmsLinearInterpLUT16(RGB_8_TO_16(i), InPtr, &L16In);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
              wValOut = cmsReverseLinearInterpLUT16(wValIn, OutPtr, &L16Out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
              p -> GammaTable[i] = wValOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
       return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
// New method, using smoothed parametric curves. This works FAR better.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
// We want to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
//      y = f(g^-1(x))      ; f = ingamma, g = outgamma
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
// And this can be parametrized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
//      y = f(t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
//      x = g(t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
LPGAMMATABLE LCMSEXPORT cmsJoinGammaEx(LPGAMMATABLE InGamma,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                       LPGAMMATABLE OutGamma, int nPoints)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    LPSAMPLEDCURVE x, y, r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    LPGAMMATABLE res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    x = cmsConvertGammaToSampledCurve(InGamma,  nPoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    y = cmsConvertGammaToSampledCurve(OutGamma, nPoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    r = cmsJoinSampledCurves(y, x, nPoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    // Does clean "hair"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    cmsSmoothSampledCurve(r, 0.001);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    cmsClampSampledCurve(r, 0.0, 65535.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    cmsFreeSampledCurve(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    cmsFreeSampledCurve(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    res = cmsConvertSampledCurveToGamma(r, 65535.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    cmsFreeSampledCurve(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
// Reverse a gamma table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
LPGAMMATABLE LCMSEXPORT cmsReverseGamma(int nResultSamples, LPGAMMATABLE InGamma)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
       register int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
       L16PARAMS L16In;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
       LPWORD InPtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
       LPGAMMATABLE p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
2394
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   280
       // Try to reverse it analytically whatever possible
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   281
       if (InGamma -> Seed.Type > 0 && InGamma -> Seed.Type <= 5 &&
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   282
            _cmsCrc32OfGammaTable(InGamma) == InGamma -> Seed.Crc32) {
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   283
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   284
                return cmsBuildParametricGamma(nResultSamples, -(InGamma -> Seed.Type), InGamma ->Seed.Params);
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   285
       }
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   286
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   287
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   288
       // Nope, reverse the table
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
       p = cmsAllocGamma(nResultSamples);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
       if (!p) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
       cmsCalcL16Params(InGamma -> nEntries, &L16In);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
       InPtr  = InGamma -> GammaTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
       for (i=0; i < nResultSamples; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
       {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
              WORD wValIn, wValOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
              wValIn = _cmsQuantizeVal(i, nResultSamples);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
              wValOut = cmsReverseLinearInterpLUT16(wValIn, InPtr, &L16In);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
              p -> GammaTable[i] = wValOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
       return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
// Parametric curves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
// Parameters goes as: Gamma, a, b, c, d, e, f
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
// Type is the ICC type +1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
// if type is negative, then the curve is analyticaly inverted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
LPGAMMATABLE LCMSEXPORT cmsBuildParametricGamma(int nEntries, int Type, double Params[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        LPGAMMATABLE Table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        double R, Val, dval, e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        int ParamsByType[] = { 0, 1, 3, 4, 5, 7 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        Table = cmsAllocGamma(nEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (NULL == Table) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        Table -> Seed.Type = Type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        CopyMemory(Table ->Seed.Params, Params, ParamsByType[abs(Type)] * sizeof(double));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        for (i=0; i < nEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                R   = (double) i / (nEntries-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                switch (Type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                // X = Y ^ Gamma
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                      Val = pow(R, Params[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                // Type 1 Reversed: X = Y ^1/gamma
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                case -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                      Val = pow(R, 1/Params[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                // CIE 122-1966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                // Y = (aX + b)^Gamma  | X >= -b/a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                // Y = 0               | else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    if (R >= -Params[2] / Params[1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                              e = Params[1]*R + Params[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                              if (e > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                Val = pow(e, Params[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                              else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                Val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                              Val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                // Type 2 Reversed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                // X = (Y ^1/g  - b) / a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                case -2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    Val = (pow(R, 1.0/Params[0]) - Params[2]) / Params[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    if (Val < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                            Val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                // IEC 61966-3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                // Y = (aX + b)^Gamma | X <= -b/a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                // Y = c              | else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    if (R >= -Params[2] / Params[1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                      e = Params[1]*R + Params[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                      Val = pow(e, Params[0]) + Params[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                      Val = Params[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                // Type 3 reversed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                // X=((Y-c)^1/g - b)/a      | (Y>=c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                // X=-b/a                   | (Y<c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                case -3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    if (R >= Params[3])  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        e = R - Params[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                        Val = (pow(e, 1/Params[0]) - Params[2]) / Params[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                        if (Val < 0) Val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                        Val = -Params[2] / Params[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                // IEC 61966-2.1 (sRGB)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                // Y = (aX + b)^Gamma | X >= d
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                // Y = cX             | X < d
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    if (R >= Params[4]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                              e = Params[1]*R + Params[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                              if (e > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                                Val = pow(e, Params[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                              else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                                Val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                      else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                              Val = R * Params[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                // Type 4 reversed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                // X=((Y^1/g-b)/a)    | Y >= (ad+b)^g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                // X=Y/c              | Y< (ad+b)^g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                case -4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    if (R >= pow(Params[1] * Params[4] + Params[2], Params[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        Val = (pow(R, 1.0/Params[0]) - Params[2]) / Params[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        Val = R / Params[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                // Y = (aX + b)^Gamma + e | X <= d
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                // Y = cX + f             | else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    if (R >= Params[4]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        e = Params[1]*R + Params[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        Val = pow(e, Params[0]) + Params[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        Val = R*Params[3] + Params[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                // Reversed type 5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                // X=((Y-e)1/g-b)/a   | Y >=(ad+b)^g+e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                // X=(Y-f)/c          | else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                case -5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                if (R >= pow(Params[1] * Params[4], Params[0]) + Params[5]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    Val = pow(R - Params[5], 1/Params[0]) - Params[2] / Params[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    Val = (R - Params[6]) / Params[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                        cmsSignalError(LCMS_ERRC_ABORTED, "Unsupported parametric curve type=%d", abs(Type)-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                        cmsFreeGamma(Table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        // Saturate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        dval = Val * 65535.0 + .5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (dval > 65535.) dval = 65535.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (dval < 0) dval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        Table->GammaTable[i] = (WORD) floor(dval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        Table -> Seed.Crc32 = _cmsCrc32OfGammaTable(Table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        return Table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
// Build a gamma table based on gamma constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
LPGAMMATABLE LCMSEXPORT cmsBuildGamma(int nEntries, double Gamma)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    return cmsBuildParametricGamma(nEntries, 1, &Gamma);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
// From: Eilers, P.H.C. (1994) Smoothing and interpolation with finite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
// differences. in: Graphic Gems IV, Heckbert, P.S. (ed.), Academic press.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
// Smoothing and interpolation with second differences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
//   Input:  weights (w), data (y): vector from 1 to m.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
//   Input:  smoothing parameter (lambda), length (m).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
//   Output: smoothed vector (z): vector from 1 to m.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
void smooth2(vec w, vec y, vec z, float lambda, int m)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
  int i, i1, i2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
  vec c, d, e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
  d[1] = w[1] + lambda;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
  c[1] = -2 * lambda / d[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
  e[1] = lambda /d[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
  z[1] = w[1] * y[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
  d[2] = w[2] + 5 * lambda - d[1] * c[1] *  c[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
  c[2] = (-4 * lambda - d[1] * c[1] * e[1]) / d[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
  e[2] = lambda / d[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
  z[2] = w[2] * y[2] - c[1] * z[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
  for (i = 3; i < m - 1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    i1 = i - 1; i2 = i - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    d[i]= w[i] + 6 * lambda - c[i1] * c[i1] * d[i1] - e[i2] * e[i2] * d[i2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    c[i] = (-4 * lambda -d[i1] * c[i1] * e[i1])/ d[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    e[i] = lambda / d[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    z[i] = w[i] * y[i] - c[i1] * z[i1] - e[i2] * z[i2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
  i1 = m - 2; i2 = m - 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
  d[m - 1] = w[m - 1] + 5 * lambda -c[i1] * c[i1] * d[i1] - e[i2] * e[i2] * d[i2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
  c[m - 1] = (-2 * lambda - d[i1] * c[i1] * e[i1]) / d[m - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
  z[m - 1] = w[m - 1] * y[m - 1] - c[i1] * z[i1] - e[i2] * z[i2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
  i1 = m - 1; i2 = m - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
  d[m] = w[m] + lambda - c[i1] * c[i1] * d[i1] - e[i2] * e[i2] * d[i2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
  z[m] = (w[m] * y[m] - c[i1] * z[i1] - e[i2] * z[i2]) / d[m];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
  z[m - 1] = z[m - 1] / d[m - 1] - c[m - 1] * z[m];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
  for (i = m - 2; 1<= i; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     z[i] = z[i] / d[i] - c[i] * z[i + 1] - e[i] * z[i + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
// Smooths a curve sampled at regular intervals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
2394
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   539
LCMSBOOL LCMSEXPORT cmsSmoothGamma(LPGAMMATABLE Tab, double lambda)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    vec w, y, z;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    int i, nItems, Zeros, Poles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    if (cmsIsLinear(Tab->GammaTable, Tab->nEntries)) return FALSE; // Nothing to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    nItems = Tab -> nEntries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    if (nItems > MAX_KNOTS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                cmsSignalError(LCMS_ERRC_ABORTED, "cmsSmoothGamma: too many points.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    ZeroMemory(w, nItems * sizeof(float));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    ZeroMemory(y, nItems * sizeof(float));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    ZeroMemory(z, nItems * sizeof(float));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    for (i=0; i < nItems; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        y[i+1] = (float) Tab -> GammaTable[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        w[i+1] = 1.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    smooth2(w, y, z, (float) lambda, nItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    // Do some reality - checking...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    Zeros = Poles = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    for (i=nItems; i > 1; --i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            if (z[i] == 0.) Zeros++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            if (z[i] >= 65535.) Poles++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            if (z[i] < z[i-1]) return FALSE; // Non-Monotonic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    if (Zeros > (nItems / 3)) return FALSE;  // Degenerated, mostly zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    if (Poles > (nItems / 3)) return FALSE;  // Degenerated, mostly poles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    // Seems ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    for (i=0; i < nItems; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        // Clamp to WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        float v = z[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (v < 0) v = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (v > 65535.) v = 65535.;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        Tab -> GammaTable[i] = (WORD) floor(v + .5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
// Check if curve is exponential, return gamma if so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
double LCMSEXPORT cmsEstimateGammaEx(LPWORD GammaTable, int nEntries, double Thereshold)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    double gamma, sum, sum2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    double n, x, y, Std;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    sum = sum2 = n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    // Does exclude endpoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    for (i=1; i < nEntries - 1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            x = (double) i / (nEntries - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            y = (double) GammaTable[i] / 65535.;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            // Avoid 7% on lower part to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            // artifacts due to linear ramps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            if (y > 0. && y < 1. && x > 0.07) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            gamma = log(y) / log(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            sum  += gamma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            sum2 += gamma * gamma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    // Take a look on SD to see if gamma isn't exponential at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    Std = sqrt((n * sum2 - sum * sum) / (n*(n-1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    if (Std > Thereshold)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return -1.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    return (sum / n);   // The mean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
double LCMSEXPORT cmsEstimateGamma(LPGAMMATABLE t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        return cmsEstimateGammaEx(t->GammaTable, t->nEntries, 0.7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
// -----------------------------------------------------------------Sampled curves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
// Allocate a empty curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
LPSAMPLEDCURVE cmsAllocSampledCurve(int nItems)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    LPSAMPLEDCURVE pOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
2394
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   651
    pOut = (LPSAMPLEDCURVE) _cmsMalloc(sizeof(SAMPLEDCURVE));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    if (pOut == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
2394
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   655
    if((pOut->Values = (double *) _cmsMalloc(nItems * sizeof(double))) == NULL)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    {
2394
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   657
         _cmsFree(pOut);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    pOut->nItems = nItems;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    ZeroMemory(pOut->Values, nItems * sizeof(double));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    return pOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
void cmsFreeSampledCurve(LPSAMPLEDCURVE p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
{
2394
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   670
     _cmsFree((LPVOID) p -> Values);
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   671
     _cmsFree((LPVOID) p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
// Does duplicate a sampled curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
LPSAMPLEDCURVE cmsDupSampledCurve(LPSAMPLEDCURVE p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    LPSAMPLEDCURVE out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    out = cmsAllocSampledCurve(p -> nItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    if (!out) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    CopyMemory(out ->Values, p ->Values, p->nItems * sizeof(double));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
// Take min, max of curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
void cmsEndpointsOfSampledCurve(LPSAMPLEDCURVE p, double* Min, double* Max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        *Min = 65536.;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        *Max = 0.;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        for (i=0; i < p -> nItems; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                double v = p -> Values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                if (v < *Min)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                        *Min = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                if (v > *Max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                        *Max = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        if (*Min < 0) *Min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if (*Max > 65535.0) *Max = 65535.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
// Clamps to Min, Max
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
void cmsClampSampledCurve(LPSAMPLEDCURVE p, double Min, double Max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        for (i=0; i < p -> nItems; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                double v = p -> Values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                if (v < Min)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                        v = Min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                if (v > Max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                        v = Max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                p -> Values[i] = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
// Smooths a curve sampled at regular intervals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
2394
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   742
LCMSBOOL cmsSmoothSampledCurve(LPSAMPLEDCURVE Tab, double lambda)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    vec w, y, z;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    int i, nItems;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    nItems = Tab -> nItems;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    if (nItems > MAX_KNOTS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                cmsSignalError(LCMS_ERRC_ABORTED, "cmsSmoothSampledCurve: too many points.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    ZeroMemory(w, nItems * sizeof(float));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    ZeroMemory(y, nItems * sizeof(float));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    ZeroMemory(z, nItems * sizeof(float));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    for (i=0; i < nItems; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        float value = (float) Tab -> Values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        y[i+1] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        w[i+1] = (float) ((value < 0.0) ?  0 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    smooth2(w, y, z, (float) lambda, nItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    for (i=0; i < nItems; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        Tab -> Values[i] = z[i+1];;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
// Scale a value v, within domain Min .. Max
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
// to a domain 0..(nPoints-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
double ScaleVal(double v, double Min, double Max, int nPoints)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        double a, b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        if (v <= Min) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        if (v >= Max) return (nPoints-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        a = (double) (nPoints - 1) / (Max - Min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        b = a * Min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        return (a * v) - b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
// Does rescale a sampled curve to fit in a 0..(nPoints-1) domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
void cmsRescaleSampledCurve(LPSAMPLEDCURVE p, double Min, double Max, int nPoints)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        for (i=0; i < p -> nItems; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                double v = p -> Values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                p -> Values[i] = ScaleVal(v, Min, Max, nPoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
// Joins two sampled curves for X and Y. Curves should be sorted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
LPSAMPLEDCURVE cmsJoinSampledCurves(LPSAMPLEDCURVE X, LPSAMPLEDCURVE Y, int nResultingPoints)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    LPSAMPLEDCURVE out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    double MinX, MinY, MaxX, MaxY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    double x, y, x1, y1, x2, y2, a, b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    out = cmsAllocSampledCurve(nResultingPoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    if (out == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    if (X -> nItems != Y -> nItems) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        cmsSignalError(LCMS_ERRC_ABORTED, "cmsJoinSampledCurves: invalid curve.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        cmsFreeSampledCurve(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    // Get endpoints of sampled curves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    cmsEndpointsOfSampledCurve(X, &MinX, &MaxX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    cmsEndpointsOfSampledCurve(Y, &MinY, &MaxY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    // Set our points
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    out ->Values[0] = MinY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    for (i=1; i < nResultingPoints; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        // Scale t to x domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        x = (i * (MaxX - MinX) / (nResultingPoints-1)) + MinX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        // Find interval in which j is within (always up,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        // since fn should be monotonic at all)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        j = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        while ((j < X ->nItems - 1) && X ->Values[j] < x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        // Now x is within X[j-1], X[j]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        x1 = X ->Values[j-1]; x2 = X ->Values[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        y1 = Y ->Values[j-1]; y2 = Y ->Values[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        // Interpolate  the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        a = (y1 - y2) / (x1 - x2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        b = y1 - a * x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        y = a* x + b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        out ->Values[i] = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    cmsClampSampledCurve(out, MinY, MaxY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
// Convert between curve types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
LPGAMMATABLE cmsConvertSampledCurveToGamma(LPSAMPLEDCURVE Sampled, double Max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    LPGAMMATABLE Gamma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    int i, nPoints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    nPoints = Sampled ->nItems;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    Gamma = cmsAllocGamma(nPoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    for (i=0; i < nPoints; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        Gamma->GammaTable[i] = (WORD) floor(ScaleVal(Sampled ->Values[i], 0, Max, 65536) + .5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    return Gamma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
// Inverse of anterior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
LPSAMPLEDCURVE cmsConvertGammaToSampledCurve(LPGAMMATABLE Gamma, int nPoints)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    LPSAMPLEDCURVE Sampled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    L16PARAMS L16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    WORD wQuant, wValIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    if (nPoints > 4096) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        cmsSignalError(LCMS_ERRC_ABORTED, "cmsConvertGammaToSampledCurve: too many points (max=4096)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    cmsCalcL16Params(Gamma -> nEntries, &L16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    Sampled = cmsAllocSampledCurve(nPoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    for (i=0; i < nPoints; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            wQuant  = _cmsQuantizeVal(i, nPoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            wValIn  = cmsLinearInterpLUT16(wQuant, Gamma ->GammaTable, &L16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            Sampled ->Values[i] = (float) wValIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    return Sampled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
// Smooth endpoints (used in Black/White compensation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
2394
404cbe399601 6821031: Upgrade OpenJDK's LittleCMS version to 1.18
prr
parents: 2
diff changeset
   926
LCMSBOOL _cmsSmoothEndpoints(LPWORD Table, int nEntries)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    vec w, y, z;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    int i, Zeros, Poles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    if (cmsIsLinear(Table, nEntries)) return FALSE; // Nothing to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    if (nEntries > MAX_KNOTS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                cmsSignalError(LCMS_ERRC_ABORTED, "_cmsSmoothEndpoints: too many points.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    ZeroMemory(w, nEntries * sizeof(float));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    ZeroMemory(y, nEntries * sizeof(float));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    ZeroMemory(z, nEntries * sizeof(float));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    for (i=0; i < nEntries; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        y[i+1] = (float) Table[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        w[i+1] = 1.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    w[1]        = 65535.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    w[nEntries] = 65535.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    smooth2(w, y, z, (float) nEntries, nEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    // Do some reality - checking...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    Zeros = Poles = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    for (i=nEntries; i > 1; --i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            if (z[i] == 0.) Zeros++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            if (z[i] >= 65535.) Poles++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            if (z[i] < z[i-1]) return FALSE; // Non-Monotonic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    if (Zeros > (nEntries / 3)) return FALSE;  // Degenerated, mostly zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    if (Poles > (nEntries / 3)) return FALSE;    // Degenerated, mostly poles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    // Seems ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    for (i=0; i < nEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        // Clamp to WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        float v = z[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        if (v < 0) v = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        if (v > 65535.) v = 65535.;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        Table[i] = (WORD) floor(v + .5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
}