Path: blob/master/src/java.desktop/share/native/liblcms/cmssm.c
41149 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// ------------------------------------------------------------------------5960// Gamut boundary description by using Jan Morovic's Segment maxima method61// Many thanks to Jan for allowing me to use his algorithm.6263// r = C*64// alpha = Hab65// theta = L*6667#define SECTORS 16 // number of divisions in alpha and theta6869// Spherical coordinates70typedef struct {7172cmsFloat64Number r;73cmsFloat64Number alpha;74cmsFloat64Number theta;7576} cmsSpherical;7778typedef enum {79GP_EMPTY,80GP_SPECIFIED,81GP_MODELED8283} GDBPointType;848586typedef struct {8788GDBPointType Type;89cmsSpherical p; // Keep also alpha & theta of maximum9091} cmsGDBPoint;929394typedef struct {9596cmsContext ContextID;97cmsGDBPoint Gamut[SECTORS][SECTORS];9899} cmsGDB;100101102// A line using the parametric form103// P = a + t*u104typedef struct {105106cmsVEC3 a;107cmsVEC3 u;108109} cmsLine;110111112// A plane using the parametric form113// Q = b + r*v + s*w114typedef struct {115116cmsVEC3 b;117cmsVEC3 v;118cmsVEC3 w;119120} cmsPlane;121122123124// --------------------------------------------------------------------------------------------125126// ATAN2() which always returns degree positive numbers127128static129cmsFloat64Number _cmsAtan2(cmsFloat64Number y, cmsFloat64Number x)130{131cmsFloat64Number a;132133// Deal with undefined case134if (x == 0.0 && y == 0.0) return 0;135136a = (atan2(y, x) * 180.0) / M_PI;137138while (a < 0) {139a += 360;140}141142return a;143}144145// Convert to spherical coordinates146static147void ToSpherical(cmsSpherical* sp, const cmsVEC3* v)148{149150cmsFloat64Number L, a, b;151152L = v ->n[VX];153a = v ->n[VY];154b = v ->n[VZ];155156sp ->r = sqrt( L*L + a*a + b*b );157158if (sp ->r == 0) {159sp ->alpha = sp ->theta = 0;160return;161}162163sp ->alpha = _cmsAtan2(a, b);164sp ->theta = _cmsAtan2(sqrt(a*a + b*b), L);165}166167168// Convert to cartesian from spherical169static170void ToCartesian(cmsVEC3* v, const cmsSpherical* sp)171{172cmsFloat64Number sin_alpha;173cmsFloat64Number cos_alpha;174cmsFloat64Number sin_theta;175cmsFloat64Number cos_theta;176cmsFloat64Number L, a, b;177178sin_alpha = sin((M_PI * sp ->alpha) / 180.0);179cos_alpha = cos((M_PI * sp ->alpha) / 180.0);180sin_theta = sin((M_PI * sp ->theta) / 180.0);181cos_theta = cos((M_PI * sp ->theta) / 180.0);182183a = sp ->r * sin_theta * sin_alpha;184b = sp ->r * sin_theta * cos_alpha;185L = sp ->r * cos_theta;186187v ->n[VX] = L;188v ->n[VY] = a;189v ->n[VZ] = b;190}191192193// Quantize sector of a spherical coordinate. Saturate 360, 180 to last sector194// The limits are the centers of each sector, so195static196void QuantizeToSector(const cmsSpherical* sp, int* alpha, int* theta)197{198*alpha = (int) floor(((sp->alpha * (SECTORS)) / 360.0) );199*theta = (int) floor(((sp->theta * (SECTORS)) / 180.0) );200201if (*alpha >= SECTORS)202*alpha = SECTORS-1;203if (*theta >= SECTORS)204*theta = SECTORS-1;205}206207208// Line determined by 2 points209static210void LineOf2Points(cmsLine* line, cmsVEC3* a, cmsVEC3* b)211{212213_cmsVEC3init(&line ->a, a ->n[VX], a ->n[VY], a ->n[VZ]);214_cmsVEC3init(&line ->u, b ->n[VX] - a ->n[VX],215b ->n[VY] - a ->n[VY],216b ->n[VZ] - a ->n[VZ]);217}218219220// Evaluate parametric line221static222void GetPointOfLine(cmsVEC3* p, const cmsLine* line, cmsFloat64Number t)223{224p ->n[VX] = line ->a.n[VX] + t * line->u.n[VX];225p ->n[VY] = line ->a.n[VY] + t * line->u.n[VY];226p ->n[VZ] = line ->a.n[VZ] + t * line->u.n[VZ];227}228229230231/*232Closest point in sector line1 to sector line2 (both are defined as 0 <=t <= 1)233http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm234235Copyright 2001, softSurfer (www.softsurfer.com)236This code may be freely used and modified for any purpose237providing that this copyright notice is included with it.238SoftSurfer makes no warranty for this code, and cannot be held239liable for any real or imagined damage resulting from its use.240Users of this code must verify correctness for their application.241242*/243244static245cmsBool ClosestLineToLine(cmsVEC3* r, const cmsLine* line1, const cmsLine* line2)246{247cmsFloat64Number a, b, c, d, e, D;248cmsFloat64Number sc, sN, sD;249//cmsFloat64Number tc; // left for future use250cmsFloat64Number tN, tD;251cmsVEC3 w0;252253_cmsVEC3minus(&w0, &line1 ->a, &line2 ->a);254255a = _cmsVEC3dot(&line1 ->u, &line1 ->u);256b = _cmsVEC3dot(&line1 ->u, &line2 ->u);257c = _cmsVEC3dot(&line2 ->u, &line2 ->u);258d = _cmsVEC3dot(&line1 ->u, &w0);259e = _cmsVEC3dot(&line2 ->u, &w0);260261D = a*c - b * b; // Denominator262sD = tD = D; // default sD = D >= 0263264if (D < MATRIX_DET_TOLERANCE) { // the lines are almost parallel265266sN = 0.0; // force using point P0 on segment S1267sD = 1.0; // to prevent possible division by 0.0 later268tN = e;269tD = c;270}271else { // get the closest points on the infinite lines272273sN = (b*e - c*d);274tN = (a*e - b*d);275276if (sN < 0.0) { // sc < 0 => the s=0 edge is visible277278sN = 0.0;279tN = e;280tD = c;281}282else if (sN > sD) { // sc > 1 => the s=1 edge is visible283sN = sD;284tN = e + b;285tD = c;286}287}288289if (tN < 0.0) { // tc < 0 => the t=0 edge is visible290291tN = 0.0;292// recompute sc for this edge293if (-d < 0.0)294sN = 0.0;295else if (-d > a)296sN = sD;297else {298sN = -d;299sD = a;300}301}302else if (tN > tD) { // tc > 1 => the t=1 edge is visible303304tN = tD;305306// recompute sc for this edge307if ((-d + b) < 0.0)308sN = 0;309else if ((-d + b) > a)310sN = sD;311else {312sN = (-d + b);313sD = a;314}315}316// finally do the division to get sc and tc317sc = (fabs(sN) < MATRIX_DET_TOLERANCE ? 0.0 : sN / sD);318//tc = (fabs(tN) < MATRIX_DET_TOLERANCE ? 0.0 : tN / tD); // left for future use.319320GetPointOfLine(r, line1, sc);321return TRUE;322}323324325326// ------------------------------------------------------------------ Wrapper327328329// Allocate & free structure330cmsHANDLE CMSEXPORT cmsGBDAlloc(cmsContext ContextID)331{332cmsGDB* gbd = (cmsGDB*) _cmsMallocZero(ContextID, sizeof(cmsGDB));333if (gbd == NULL) return NULL;334335gbd -> ContextID = ContextID;336337return (cmsHANDLE) gbd;338}339340341void CMSEXPORT cmsGBDFree(cmsHANDLE hGBD)342{343cmsGDB* gbd = (cmsGDB*) hGBD;344if (hGBD != NULL)345_cmsFree(gbd->ContextID, (void*) gbd);346}347348349// Auxiliary to retrieve a pointer to the segmentr containing the Lab value350static351cmsGDBPoint* GetPoint(cmsGDB* gbd, const cmsCIELab* Lab, cmsSpherical* sp)352{353cmsVEC3 v;354int alpha, theta;355356// Housekeeping357_cmsAssert(gbd != NULL);358_cmsAssert(Lab != NULL);359_cmsAssert(sp != NULL);360361// Center L* by subtracting half of its domain, that's 50362_cmsVEC3init(&v, Lab ->L - 50.0, Lab ->a, Lab ->b);363364// Convert to spherical coordinates365ToSpherical(sp, &v);366367if (sp ->r < 0 || sp ->alpha < 0 || sp->theta < 0) {368cmsSignalError(gbd ->ContextID, cmsERROR_RANGE, "spherical value out of range");369return NULL;370}371372// On which sector it falls?373QuantizeToSector(sp, &alpha, &theta);374375if (alpha < 0 || theta < 0 || alpha >= SECTORS || theta >= SECTORS) {376cmsSignalError(gbd ->ContextID, cmsERROR_RANGE, " quadrant out of range");377return NULL;378}379380// Get pointer to the sector381return &gbd ->Gamut[theta][alpha];382}383384// Add a point to gamut descriptor. Point to add is in Lab color space.385// GBD is centered on a=b=0 and L*=50386cmsBool CMSEXPORT cmsGDBAddPoint(cmsHANDLE hGBD, const cmsCIELab* Lab)387{388cmsGDB* gbd = (cmsGDB*) hGBD;389cmsGDBPoint* ptr;390cmsSpherical sp;391392393// Get pointer to the sector394ptr = GetPoint(gbd, Lab, &sp);395if (ptr == NULL) return FALSE;396397// If no samples at this sector, add it398if (ptr ->Type == GP_EMPTY) {399400ptr -> Type = GP_SPECIFIED;401ptr -> p = sp;402}403else {404405406// Substitute only if radius is greater407if (sp.r > ptr -> p.r) {408409ptr -> Type = GP_SPECIFIED;410ptr -> p = sp;411}412}413414return TRUE;415}416417// Check if a given point falls inside gamut418cmsBool CMSEXPORT cmsGDBCheckPoint(cmsHANDLE hGBD, const cmsCIELab* Lab)419{420cmsGDB* gbd = (cmsGDB*) hGBD;421cmsGDBPoint* ptr;422cmsSpherical sp;423424// Get pointer to the sector425ptr = GetPoint(gbd, Lab, &sp);426if (ptr == NULL) return FALSE;427428// If no samples at this sector, return no data429if (ptr ->Type == GP_EMPTY) return FALSE;430431// In gamut only if radius is greater432433return (sp.r <= ptr -> p.r);434}435436// -----------------------------------------------------------------------------------------------------------------------437438// Find near sectors. The list of sectors found is returned on Close[].439// The function returns the number of sectors as well.440441// 24 9 10 11 12442// 23 8 1 2 13443// 22 7 * 3 14444// 21 6 5 4 15445// 20 19 18 17 16446//447// Those are the relative movements448// {-2,-2}, {-1, -2}, {0, -2}, {+1, -2}, {+2, -2},449// {-2,-1}, {-1, -1}, {0, -1}, {+1, -1}, {+2, -1},450// {-2, 0}, {-1, 0}, {0, 0}, {+1, 0}, {+2, 0},451// {-2,+1}, {-1, +1}, {0, +1}, {+1, +1}, {+2, +1},452// {-2,+2}, {-1, +2}, {0, +2}, {+1, +2}, {+2, +2}};453454455static456const struct _spiral {457458int AdvX, AdvY;459460} Spiral[] = { {0, -1}, {+1, -1}, {+1, 0}, {+1, +1}, {0, +1}, {-1, +1},461{-1, 0}, {-1, -1}, {-1, -2}, {0, -2}, {+1, -2}, {+2, -2},462{+2, -1}, {+2, 0}, {+2, +1}, {+2, +2}, {+1, +2}, {0, +2},463{-1, +2}, {-2, +2}, {-2, +1}, {-2, 0}, {-2, -1}, {-2, -2} };464465#define NSTEPS (sizeof(Spiral) / sizeof(struct _spiral))466467static468int FindNearSectors(cmsGDB* gbd, int alpha, int theta, cmsGDBPoint* Close[])469{470int nSectors = 0;471int a, t;472cmsUInt32Number i;473cmsGDBPoint* pt;474475for (i=0; i < NSTEPS; i++) {476477a = alpha + Spiral[i].AdvX;478t = theta + Spiral[i].AdvY;479480// Cycle at the end481a %= SECTORS;482t %= SECTORS;483484// Cycle at the begin485if (a < 0) a = SECTORS + a;486if (t < 0) t = SECTORS + t;487488pt = &gbd ->Gamut[t][a];489490if (pt -> Type != GP_EMPTY) {491492Close[nSectors++] = pt;493}494}495496return nSectors;497}498499500// Interpolate a missing sector. Method identifies whatever this is top, bottom or mid501static502cmsBool InterpolateMissingSector(cmsGDB* gbd, int alpha, int theta)503{504cmsSpherical sp;505cmsVEC3 Lab;506cmsVEC3 Centre;507cmsLine ray;508int nCloseSectors;509cmsGDBPoint* Close[NSTEPS + 1];510cmsSpherical closel, templ;511cmsLine edge;512int k, m;513514// Is that point already specified?515if (gbd ->Gamut[theta][alpha].Type != GP_EMPTY) return TRUE;516517// Fill close points518nCloseSectors = FindNearSectors(gbd, alpha, theta, Close);519520521// Find a central point on the sector522sp.alpha = (cmsFloat64Number) ((alpha + 0.5) * 360.0) / (SECTORS);523sp.theta = (cmsFloat64Number) ((theta + 0.5) * 180.0) / (SECTORS);524sp.r = 50.0;525526// Convert to Cartesian527ToCartesian(&Lab, &sp);528529// Create a ray line from centre to this point530_cmsVEC3init(&Centre, 50.0, 0, 0);531LineOf2Points(&ray, &Lab, &Centre);532533// For all close sectors534closel.r = 0.0;535closel.alpha = 0;536closel.theta = 0;537538for (k=0; k < nCloseSectors; k++) {539540for(m = k+1; m < nCloseSectors; m++) {541542cmsVEC3 temp, a1, a2;543544// A line from sector to sector545ToCartesian(&a1, &Close[k]->p);546ToCartesian(&a2, &Close[m]->p);547548LineOf2Points(&edge, &a1, &a2);549550// Find a line551ClosestLineToLine(&temp, &ray, &edge);552553// Convert to spherical554ToSpherical(&templ, &temp);555556557if ( templ.r > closel.r &&558templ.theta >= (theta*180.0/SECTORS) &&559templ.theta <= ((theta+1)*180.0/SECTORS) &&560templ.alpha >= (alpha*360.0/SECTORS) &&561templ.alpha <= ((alpha+1)*360.0/SECTORS)) {562563closel = templ;564}565}566}567568gbd ->Gamut[theta][alpha].p = closel;569gbd ->Gamut[theta][alpha].Type = GP_MODELED;570571return TRUE;572573}574575576// Interpolate missing parts. The algorithm fist computes slices at577// theta=0 and theta=Max.578cmsBool CMSEXPORT cmsGDBCompute(cmsHANDLE hGBD, cmsUInt32Number dwFlags)579{580int alpha, theta;581cmsGDB* gbd = (cmsGDB*) hGBD;582583_cmsAssert(hGBD != NULL);584585// Interpolate black586for (alpha = 0; alpha < SECTORS; alpha++) {587588if (!InterpolateMissingSector(gbd, alpha, 0)) return FALSE;589}590591// Interpolate white592for (alpha = 0; alpha < SECTORS; alpha++) {593594if (!InterpolateMissingSector(gbd, alpha, SECTORS-1)) return FALSE;595}596597598// Interpolate Mid599for (theta = 1; theta < SECTORS; theta++) {600for (alpha = 0; alpha < SECTORS; alpha++) {601602if (!InterpolateMissingSector(gbd, alpha, theta)) return FALSE;603}604}605606// Done607return TRUE;608609cmsUNUSED_PARAMETER(dwFlags);610}611612613614615// --------------------------------------------------------------------------------------------------------616617// Great for debug, but not suitable for real use618619#if 0620cmsBool cmsGBDdumpVRML(cmsHANDLE hGBD, const char* fname)621{622FILE* fp;623int i, j;624cmsGDB* gbd = (cmsGDB*) hGBD;625cmsGDBPoint* pt;626627fp = fopen (fname, "wt");628if (fp == NULL)629return FALSE;630631fprintf (fp, "#VRML V2.0 utf8\n");632633// set the viewing orientation and distance634fprintf (fp, "DEF CamTest Group {\n");635fprintf (fp, "\tchildren [\n");636fprintf (fp, "\t\tDEF Cameras Group {\n");637fprintf (fp, "\t\t\tchildren [\n");638fprintf (fp, "\t\t\t\tDEF DefaultView Viewpoint {\n");639fprintf (fp, "\t\t\t\t\tposition 0 0 340\n");640fprintf (fp, "\t\t\t\t\torientation 0 0 1 0\n");641fprintf (fp, "\t\t\t\t\tdescription \"default view\"\n");642fprintf (fp, "\t\t\t\t}\n");643fprintf (fp, "\t\t\t]\n");644fprintf (fp, "\t\t},\n");645fprintf (fp, "\t]\n");646fprintf (fp, "}\n");647648// Output the background stuff649fprintf (fp, "Background {\n");650fprintf (fp, "\tskyColor [\n");651fprintf (fp, "\t\t.5 .5 .5\n");652fprintf (fp, "\t]\n");653fprintf (fp, "}\n");654655// Output the shape stuff656fprintf (fp, "Transform {\n");657fprintf (fp, "\tscale .3 .3 .3\n");658fprintf (fp, "\tchildren [\n");659660// Draw the axes as a shape:661fprintf (fp, "\t\tShape {\n");662fprintf (fp, "\t\t\tappearance Appearance {\n");663fprintf (fp, "\t\t\t\tmaterial Material {\n");664fprintf (fp, "\t\t\t\t\tdiffuseColor 0 0.8 0\n");665fprintf (fp, "\t\t\t\t\temissiveColor 1.0 1.0 1.0\n");666fprintf (fp, "\t\t\t\t\tshininess 0.8\n");667fprintf (fp, "\t\t\t\t}\n");668fprintf (fp, "\t\t\t}\n");669fprintf (fp, "\t\t\tgeometry IndexedLineSet {\n");670fprintf (fp, "\t\t\t\tcoord Coordinate {\n");671fprintf (fp, "\t\t\t\t\tpoint [\n");672fprintf (fp, "\t\t\t\t\t0.0 0.0 0.0,\n");673fprintf (fp, "\t\t\t\t\t%f 0.0 0.0,\n", 255.0);674fprintf (fp, "\t\t\t\t\t0.0 %f 0.0,\n", 255.0);675fprintf (fp, "\t\t\t\t\t0.0 0.0 %f]\n", 255.0);676fprintf (fp, "\t\t\t\t}\n");677fprintf (fp, "\t\t\t\tcoordIndex [\n");678fprintf (fp, "\t\t\t\t\t0, 1, -1\n");679fprintf (fp, "\t\t\t\t\t0, 2, -1\n");680fprintf (fp, "\t\t\t\t\t0, 3, -1]\n");681fprintf (fp, "\t\t\t}\n");682fprintf (fp, "\t\t}\n");683684685fprintf (fp, "\t\tShape {\n");686fprintf (fp, "\t\t\tappearance Appearance {\n");687fprintf (fp, "\t\t\t\tmaterial Material {\n");688fprintf (fp, "\t\t\t\t\tdiffuseColor 0 0.8 0\n");689fprintf (fp, "\t\t\t\t\temissiveColor 1 1 1\n");690fprintf (fp, "\t\t\t\t\tshininess 0.8\n");691fprintf (fp, "\t\t\t\t}\n");692fprintf (fp, "\t\t\t}\n");693fprintf (fp, "\t\t\tgeometry PointSet {\n");694695// fill in the points here696fprintf (fp, "\t\t\t\tcoord Coordinate {\n");697fprintf (fp, "\t\t\t\t\tpoint [\n");698699// We need to transverse all gamut hull.700for (i=0; i < SECTORS; i++)701for (j=0; j < SECTORS; j++) {702703cmsVEC3 v;704705pt = &gbd ->Gamut[i][j];706ToCartesian(&v, &pt ->p);707708fprintf (fp, "\t\t\t\t\t%g %g %g", v.n[0]+50, v.n[1], v.n[2]);709710if ((j == SECTORS - 1) && (i == SECTORS - 1))711fprintf (fp, "]\n");712else713fprintf (fp, ",\n");714715}716717fprintf (fp, "\t\t\t\t}\n");718719720721// fill in the face colors722fprintf (fp, "\t\t\t\tcolor Color {\n");723fprintf (fp, "\t\t\t\t\tcolor [\n");724725for (i=0; i < SECTORS; i++)726for (j=0; j < SECTORS; j++) {727728cmsVEC3 v;729730pt = &gbd ->Gamut[i][j];731732733ToCartesian(&v, &pt ->p);734735736if (pt ->Type == GP_EMPTY)737fprintf (fp, "\t\t\t\t\t%g %g %g", 0.0, 0.0, 0.0);738else739if (pt ->Type == GP_MODELED)740fprintf (fp, "\t\t\t\t\t%g %g %g", 1.0, .5, .5);741else {742fprintf (fp, "\t\t\t\t\t%g %g %g", 1.0, 1.0, 1.0);743744}745746if ((j == SECTORS - 1) && (i == SECTORS - 1))747fprintf (fp, "]\n");748else749fprintf (fp, ",\n");750}751fprintf (fp, "\t\t\t}\n");752753754fprintf (fp, "\t\t\t}\n");755fprintf (fp, "\t\t}\n");756fprintf (fp, "\t]\n");757fprintf (fp, "}\n");758759fclose (fp);760761return TRUE;762}763#endif764765766767