Path: blob/master/src/java.desktop/share/native/liblcms/cmswtpnt.c
41152 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324// This file is available under and governed by the GNU General Public25// License version 2 only, as published by the Free Software Foundation.26// However, the following notice accompanied the original version of this27// file:28//29//---------------------------------------------------------------------------------30//31// Little Color Management System32// Copyright (c) 1998-2020 Marti Maria Saguer33//34// Permission is hereby granted, free of charge, to any person obtaining35// a copy of this software and associated documentation files (the "Software"),36// to deal in the Software without restriction, including without limitation37// the rights to use, copy, modify, merge, publish, distribute, sublicense,38// and/or sell copies of the Software, and to permit persons to whom the Software39// is furnished to do so, subject to the following conditions:40//41// The above copyright notice and this permission notice shall be included in42// all copies or substantial portions of the Software.43//44// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,45// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO46// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND47// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE48// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION49// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION50// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.51//52//---------------------------------------------------------------------------------53//5455#include "lcms2_internal.h"565758// D50 - Widely used59const cmsCIEXYZ* CMSEXPORT cmsD50_XYZ(void)60{61static cmsCIEXYZ D50XYZ = {cmsD50X, cmsD50Y, cmsD50Z};6263return &D50XYZ;64}6566const cmsCIExyY* CMSEXPORT cmsD50_xyY(void)67{68static cmsCIExyY D50xyY;6970cmsXYZ2xyY(&D50xyY, cmsD50_XYZ());7172return &D50xyY;73}7475// Obtains WhitePoint from Temperature76cmsBool CMSEXPORT cmsWhitePointFromTemp(cmsCIExyY* WhitePoint, cmsFloat64Number TempK)77{78cmsFloat64Number x, y;79cmsFloat64Number T, T2, T3;80// cmsFloat64Number M1, M2;8182_cmsAssert(WhitePoint != NULL);8384T = TempK;85T2 = T*T; // Square86T3 = T2*T; // Cube8788// For correlated color temperature (T) between 4000K and 7000K:8990if (T >= 4000. && T <= 7000.)91{92x = -4.6070*(1E9/T3) + 2.9678*(1E6/T2) + 0.09911*(1E3/T) + 0.244063;93}94else95// or for correlated color temperature (T) between 7000K and 25000K:9697if (T > 7000.0 && T <= 25000.0)98{99x = -2.0064*(1E9/T3) + 1.9018*(1E6/T2) + 0.24748*(1E3/T) + 0.237040;100}101else {102cmsSignalError(0, cmsERROR_RANGE, "cmsWhitePointFromTemp: invalid temp");103return FALSE;104}105106// Obtain y(x)107y = -3.000*(x*x) + 2.870*x - 0.275;108109// wave factors (not used, but here for futures extensions)110111// M1 = (-1.3515 - 1.7703*x + 5.9114 *y)/(0.0241 + 0.2562*x - 0.7341*y);112// M2 = (0.0300 - 31.4424*x + 30.0717*y)/(0.0241 + 0.2562*x - 0.7341*y);113114WhitePoint -> x = x;115WhitePoint -> y = y;116WhitePoint -> Y = 1.0;117118return TRUE;119}120121122123typedef struct {124125cmsFloat64Number mirek; // temp (in microreciprocal kelvin)126cmsFloat64Number ut; // u coord of intersection w/ blackbody locus127cmsFloat64Number vt; // v coord of intersection w/ blackbody locus128cmsFloat64Number tt; // slope of ISOTEMPERATURE. line129130} ISOTEMPERATURE;131132static const ISOTEMPERATURE isotempdata[] = {133// {Mirek, Ut, Vt, Tt }134{0, 0.18006, 0.26352, -0.24341},135{10, 0.18066, 0.26589, -0.25479},136{20, 0.18133, 0.26846, -0.26876},137{30, 0.18208, 0.27119, -0.28539},138{40, 0.18293, 0.27407, -0.30470},139{50, 0.18388, 0.27709, -0.32675},140{60, 0.18494, 0.28021, -0.35156},141{70, 0.18611, 0.28342, -0.37915},142{80, 0.18740, 0.28668, -0.40955},143{90, 0.18880, 0.28997, -0.44278},144{100, 0.19032, 0.29326, -0.47888},145{125, 0.19462, 0.30141, -0.58204},146{150, 0.19962, 0.30921, -0.70471},147{175, 0.20525, 0.31647, -0.84901},148{200, 0.21142, 0.32312, -1.0182 },149{225, 0.21807, 0.32909, -1.2168 },150{250, 0.22511, 0.33439, -1.4512 },151{275, 0.23247, 0.33904, -1.7298 },152{300, 0.24010, 0.34308, -2.0637 },153{325, 0.24702, 0.34655, -2.4681 },154{350, 0.25591, 0.34951, -2.9641 },155{375, 0.26400, 0.35200, -3.5814 },156{400, 0.27218, 0.35407, -4.3633 },157{425, 0.28039, 0.35577, -5.3762 },158{450, 0.28863, 0.35714, -6.7262 },159{475, 0.29685, 0.35823, -8.5955 },160{500, 0.30505, 0.35907, -11.324 },161{525, 0.31320, 0.35968, -15.628 },162{550, 0.32129, 0.36011, -23.325 },163{575, 0.32931, 0.36038, -40.770 },164{600, 0.33724, 0.36051, -116.45 }165};166167#define NISO sizeof(isotempdata)/sizeof(ISOTEMPERATURE)168169170// Robertson's method171cmsBool CMSEXPORT cmsTempFromWhitePoint(cmsFloat64Number* TempK, const cmsCIExyY* WhitePoint)172{173cmsUInt32Number j;174cmsFloat64Number us,vs;175cmsFloat64Number uj,vj,tj,di,dj,mi,mj;176cmsFloat64Number xs, ys;177178_cmsAssert(WhitePoint != NULL);179_cmsAssert(TempK != NULL);180181di = mi = 0;182xs = WhitePoint -> x;183ys = WhitePoint -> y;184185// convert (x,y) to CIE 1960 (u,WhitePoint)186187us = (2*xs) / (-xs + 6*ys + 1.5);188vs = (3*ys) / (-xs + 6*ys + 1.5);189190191for (j=0; j < NISO; j++) {192193uj = isotempdata[j].ut;194vj = isotempdata[j].vt;195tj = isotempdata[j].tt;196mj = isotempdata[j].mirek;197198dj = ((vs - vj) - tj * (us - uj)) / sqrt(1.0 + tj * tj);199200if ((j != 0) && (di/dj < 0.0)) {201202// Found a match203*TempK = 1000000.0 / (mi + (di / (di - dj)) * (mj - mi));204return TRUE;205}206207di = dj;208mi = mj;209}210211// Not found212return FALSE;213}214215216// Compute chromatic adaptation matrix using Chad as cone matrix217218static219cmsBool ComputeChromaticAdaptation(cmsMAT3* Conversion,220const cmsCIEXYZ* SourceWhitePoint,221const cmsCIEXYZ* DestWhitePoint,222const cmsMAT3* Chad)223224{225226cmsMAT3 Chad_Inv;227cmsVEC3 ConeSourceXYZ, ConeSourceRGB;228cmsVEC3 ConeDestXYZ, ConeDestRGB;229cmsMAT3 Cone, Tmp;230231232Tmp = *Chad;233if (!_cmsMAT3inverse(&Tmp, &Chad_Inv)) return FALSE;234235_cmsVEC3init(&ConeSourceXYZ, SourceWhitePoint -> X,236SourceWhitePoint -> Y,237SourceWhitePoint -> Z);238239_cmsVEC3init(&ConeDestXYZ, DestWhitePoint -> X,240DestWhitePoint -> Y,241DestWhitePoint -> Z);242243_cmsMAT3eval(&ConeSourceRGB, Chad, &ConeSourceXYZ);244_cmsMAT3eval(&ConeDestRGB, Chad, &ConeDestXYZ);245246// Build matrix247_cmsVEC3init(&Cone.v[0], ConeDestRGB.n[0]/ConeSourceRGB.n[0], 0.0, 0.0);248_cmsVEC3init(&Cone.v[1], 0.0, ConeDestRGB.n[1]/ConeSourceRGB.n[1], 0.0);249_cmsVEC3init(&Cone.v[2], 0.0, 0.0, ConeDestRGB.n[2]/ConeSourceRGB.n[2]);250251252// Normalize253_cmsMAT3per(&Tmp, &Cone, Chad);254_cmsMAT3per(Conversion, &Chad_Inv, &Tmp);255256return TRUE;257}258259// Returns the final chrmatic adaptation from illuminant FromIll to Illuminant ToIll260// The cone matrix can be specified in ConeMatrix. If NULL, Bradford is assumed261cmsBool _cmsAdaptationMatrix(cmsMAT3* r, const cmsMAT3* ConeMatrix, const cmsCIEXYZ* FromIll, const cmsCIEXYZ* ToIll)262{263cmsMAT3 LamRigg = {{ // Bradford matrix264{{ 0.8951, 0.2664, -0.1614 }},265{{ -0.7502, 1.7135, 0.0367 }},266{{ 0.0389, -0.0685, 1.0296 }}267}};268269if (ConeMatrix == NULL)270ConeMatrix = &LamRigg;271272return ComputeChromaticAdaptation(r, FromIll, ToIll, ConeMatrix);273}274275// Same as anterior, but assuming D50 destination. White point is given in xyY276static277cmsBool _cmsAdaptMatrixToD50(cmsMAT3* r, const cmsCIExyY* SourceWhitePt)278{279cmsCIEXYZ Dn;280cmsMAT3 Bradford;281cmsMAT3 Tmp;282283cmsxyY2XYZ(&Dn, SourceWhitePt);284285if (!_cmsAdaptationMatrix(&Bradford, NULL, &Dn, cmsD50_XYZ())) return FALSE;286287Tmp = *r;288_cmsMAT3per(r, &Bradford, &Tmp);289290return TRUE;291}292293// Build a White point, primary chromas transfer matrix from RGB to CIE XYZ294// This is just an approximation, I am not handling all the non-linear295// aspects of the RGB to XYZ process, and assumming that the gamma correction296// has transitive property in the transformation chain.297//298// the alghoritm:299//300// - First I build the absolute conversion matrix using301// primaries in XYZ. This matrix is next inverted302// - Then I eval the source white point across this matrix303// obtaining the coeficients of the transformation304// - Then, I apply these coeficients to the original matrix305//306cmsBool _cmsBuildRGB2XYZtransferMatrix(cmsMAT3* r, const cmsCIExyY* WhitePt, const cmsCIExyYTRIPLE* Primrs)307{308cmsVEC3 WhitePoint, Coef;309cmsMAT3 Result, Primaries;310cmsFloat64Number xn, yn;311cmsFloat64Number xr, yr;312cmsFloat64Number xg, yg;313cmsFloat64Number xb, yb;314315xn = WhitePt -> x;316yn = WhitePt -> y;317xr = Primrs -> Red.x;318yr = Primrs -> Red.y;319xg = Primrs -> Green.x;320yg = Primrs -> Green.y;321xb = Primrs -> Blue.x;322yb = Primrs -> Blue.y;323324// Build Primaries matrix325_cmsVEC3init(&Primaries.v[0], xr, xg, xb);326_cmsVEC3init(&Primaries.v[1], yr, yg, yb);327_cmsVEC3init(&Primaries.v[2], (1-xr-yr), (1-xg-yg), (1-xb-yb));328329330// Result = Primaries ^ (-1) inverse matrix331if (!_cmsMAT3inverse(&Primaries, &Result))332return FALSE;333334335_cmsVEC3init(&WhitePoint, xn/yn, 1.0, (1.0-xn-yn)/yn);336337// Across inverse primaries ...338_cmsMAT3eval(&Coef, &Result, &WhitePoint);339340// Give us the Coefs, then I build transformation matrix341_cmsVEC3init(&r -> v[0], Coef.n[VX]*xr, Coef.n[VY]*xg, Coef.n[VZ]*xb);342_cmsVEC3init(&r -> v[1], Coef.n[VX]*yr, Coef.n[VY]*yg, Coef.n[VZ]*yb);343_cmsVEC3init(&r -> v[2], Coef.n[VX]*(1.0-xr-yr), Coef.n[VY]*(1.0-xg-yg), Coef.n[VZ]*(1.0-xb-yb));344345346return _cmsAdaptMatrixToD50(r, WhitePt);347348}349350351// Adapts a color to a given illuminant. Original color is expected to have352// a SourceWhitePt white point.353cmsBool CMSEXPORT cmsAdaptToIlluminant(cmsCIEXYZ* Result,354const cmsCIEXYZ* SourceWhitePt,355const cmsCIEXYZ* Illuminant,356const cmsCIEXYZ* Value)357{358cmsMAT3 Bradford;359cmsVEC3 In, Out;360361_cmsAssert(Result != NULL);362_cmsAssert(SourceWhitePt != NULL);363_cmsAssert(Illuminant != NULL);364_cmsAssert(Value != NULL);365366if (!_cmsAdaptationMatrix(&Bradford, NULL, SourceWhitePt, Illuminant)) return FALSE;367368_cmsVEC3init(&In, Value -> X, Value -> Y, Value -> Z);369_cmsMAT3eval(&Out, &Bradford, &In);370371Result -> X = Out.n[0];372Result -> Y = Out.n[1];373Result -> Z = Out.n[2];374375return TRUE;376}377378379380381