Path: blob/master/src/java.desktop/share/native/liblcms/cmsmtrx.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#define DSWAP(x, y) {cmsFloat64Number tmp = (x); (x)=(y); (y)=tmp;}596061// Initiate a vector62void CMSEXPORT _cmsVEC3init(cmsVEC3* r, cmsFloat64Number x, cmsFloat64Number y, cmsFloat64Number z)63{64r -> n[VX] = x;65r -> n[VY] = y;66r -> n[VZ] = z;67}6869// Vector subtraction70void CMSEXPORT _cmsVEC3minus(cmsVEC3* r, const cmsVEC3* a, const cmsVEC3* b)71{72r -> n[VX] = a -> n[VX] - b -> n[VX];73r -> n[VY] = a -> n[VY] - b -> n[VY];74r -> n[VZ] = a -> n[VZ] - b -> n[VZ];75}7677// Vector cross product78void CMSEXPORT _cmsVEC3cross(cmsVEC3* r, const cmsVEC3* u, const cmsVEC3* v)79{80r ->n[VX] = u->n[VY] * v->n[VZ] - v->n[VY] * u->n[VZ];81r ->n[VY] = u->n[VZ] * v->n[VX] - v->n[VZ] * u->n[VX];82r ->n[VZ] = u->n[VX] * v->n[VY] - v->n[VX] * u->n[VY];83}8485// Vector dot product86cmsFloat64Number CMSEXPORT _cmsVEC3dot(const cmsVEC3* u, const cmsVEC3* v)87{88return u->n[VX] * v->n[VX] + u->n[VY] * v->n[VY] + u->n[VZ] * v->n[VZ];89}9091// Euclidean length92cmsFloat64Number CMSEXPORT _cmsVEC3length(const cmsVEC3* a)93{94return sqrt(a ->n[VX] * a ->n[VX] +95a ->n[VY] * a ->n[VY] +96a ->n[VZ] * a ->n[VZ]);97}9899// Euclidean distance100cmsFloat64Number CMSEXPORT _cmsVEC3distance(const cmsVEC3* a, const cmsVEC3* b)101{102cmsFloat64Number d1 = a ->n[VX] - b ->n[VX];103cmsFloat64Number d2 = a ->n[VY] - b ->n[VY];104cmsFloat64Number d3 = a ->n[VZ] - b ->n[VZ];105106return sqrt(d1*d1 + d2*d2 + d3*d3);107}108109110111// 3x3 Identity112void CMSEXPORT _cmsMAT3identity(cmsMAT3* a)113{114_cmsVEC3init(&a-> v[0], 1.0, 0.0, 0.0);115_cmsVEC3init(&a-> v[1], 0.0, 1.0, 0.0);116_cmsVEC3init(&a-> v[2], 0.0, 0.0, 1.0);117}118119static120cmsBool CloseEnough(cmsFloat64Number a, cmsFloat64Number b)121{122return fabs(b - a) < (1.0 / 65535.0);123}124125126cmsBool CMSEXPORT _cmsMAT3isIdentity(const cmsMAT3* a)127{128cmsMAT3 Identity;129int i, j;130131_cmsMAT3identity(&Identity);132133for (i=0; i < 3; i++)134for (j=0; j < 3; j++)135if (!CloseEnough(a ->v[i].n[j], Identity.v[i].n[j])) return FALSE;136137return TRUE;138}139140141// Multiply two matrices142void CMSEXPORT _cmsMAT3per(cmsMAT3* r, const cmsMAT3* a, const cmsMAT3* b)143{144#define ROWCOL(i, j) \145a->v[i].n[0]*b->v[0].n[j] + a->v[i].n[1]*b->v[1].n[j] + a->v[i].n[2]*b->v[2].n[j]146147_cmsVEC3init(&r-> v[0], ROWCOL(0,0), ROWCOL(0,1), ROWCOL(0,2));148_cmsVEC3init(&r-> v[1], ROWCOL(1,0), ROWCOL(1,1), ROWCOL(1,2));149_cmsVEC3init(&r-> v[2], ROWCOL(2,0), ROWCOL(2,1), ROWCOL(2,2));150151#undef ROWCOL //(i, j)152}153154155156// Inverse of a matrix b = a^(-1)157cmsBool CMSEXPORT _cmsMAT3inverse(const cmsMAT3* a, cmsMAT3* b)158{159cmsFloat64Number det, c0, c1, c2;160161c0 = a -> v[1].n[1]*a -> v[2].n[2] - a -> v[1].n[2]*a -> v[2].n[1];162c1 = -a -> v[1].n[0]*a -> v[2].n[2] + a -> v[1].n[2]*a -> v[2].n[0];163c2 = a -> v[1].n[0]*a -> v[2].n[1] - a -> v[1].n[1]*a -> v[2].n[0];164165det = a -> v[0].n[0]*c0 + a -> v[0].n[1]*c1 + a -> v[0].n[2]*c2;166167if (fabs(det) < MATRIX_DET_TOLERANCE) return FALSE; // singular matrix; can't invert168169b -> v[0].n[0] = c0/det;170b -> v[0].n[1] = (a -> v[0].n[2]*a -> v[2].n[1] - a -> v[0].n[1]*a -> v[2].n[2])/det;171b -> v[0].n[2] = (a -> v[0].n[1]*a -> v[1].n[2] - a -> v[0].n[2]*a -> v[1].n[1])/det;172b -> v[1].n[0] = c1/det;173b -> v[1].n[1] = (a -> v[0].n[0]*a -> v[2].n[2] - a -> v[0].n[2]*a -> v[2].n[0])/det;174b -> v[1].n[2] = (a -> v[0].n[2]*a -> v[1].n[0] - a -> v[0].n[0]*a -> v[1].n[2])/det;175b -> v[2].n[0] = c2/det;176b -> v[2].n[1] = (a -> v[0].n[1]*a -> v[2].n[0] - a -> v[0].n[0]*a -> v[2].n[1])/det;177b -> v[2].n[2] = (a -> v[0].n[0]*a -> v[1].n[1] - a -> v[0].n[1]*a -> v[1].n[0])/det;178179return TRUE;180}181182183// Solve a system in the form Ax = b184cmsBool CMSEXPORT _cmsMAT3solve(cmsVEC3* x, cmsMAT3* a, cmsVEC3* b)185{186cmsMAT3 m, a_1;187188memmove(&m, a, sizeof(cmsMAT3));189190if (!_cmsMAT3inverse(&m, &a_1)) return FALSE; // Singular matrix191192_cmsMAT3eval(x, &a_1, b);193return TRUE;194}195196// Evaluate a vector across a matrix197void CMSEXPORT _cmsMAT3eval(cmsVEC3* r, const cmsMAT3* a, const cmsVEC3* v)198{199r->n[VX] = a->v[0].n[VX]*v->n[VX] + a->v[0].n[VY]*v->n[VY] + a->v[0].n[VZ]*v->n[VZ];200r->n[VY] = a->v[1].n[VX]*v->n[VX] + a->v[1].n[VY]*v->n[VY] + a->v[1].n[VZ]*v->n[VZ];201r->n[VZ] = a->v[2].n[VX]*v->n[VX] + a->v[2].n[VY]*v->n[VY] + a->v[2].n[VZ]*v->n[VZ];202}203204205206207