lcms.c 17.3 KB
Newer Older
Mark Oteiza's avatar
Mark Oteiza committed
1
/* Interface to Little CMS
Paul Eggert's avatar
Paul Eggert committed
2
   Copyright (C) 2017-2020 Free Software Foundation, Inc.
Mark Oteiza's avatar
Mark Oteiza committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
17
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
Mark Oteiza's avatar
Mark Oteiza committed
18 19 20 21 22 23 24 25 26 27

#include <config.h>

#ifdef HAVE_LCMS2

#include <lcms2.h>
#include <math.h>

#include "lisp.h"

28 29 30 31 32 33 34
typedef struct
{
  double J;
  double a;
  double b;
} lcmsJab_t;

35 36
#ifdef WINDOWSNT
# include <windows.h>
37
# include "w32common.h"
38 39 40 41 42 43 44 45 46
# include "w32.h"

DEF_DLL_FN (cmsFloat64Number, cmsCIE2000DeltaE,
	    (const cmsCIELab* Lab1, const cmsCIELab* Lab2, cmsFloat64Number Kl,
	     cmsFloat64Number Kc, cmsFloat64Number Kh));
DEF_DLL_FN (cmsHANDLE, cmsCIECAM02Init,
	    (cmsContext ContextID, const cmsViewingConditions* pVC));
DEF_DLL_FN (void, cmsCIECAM02Forward,
	    (cmsHANDLE hModel, const cmsCIEXYZ* pIn, cmsJCh* pOut));
47 48
DEF_DLL_FN (void, cmsCIECAM02Reverse,
	    (cmsHANDLE hModel, const cmsJCh* pIn, cmsCIEXYZ* pOut));
49
DEF_DLL_FN (void, cmsCIECAM02Done, (cmsHANDLE hModel));
50 51 52
DEF_DLL_FN (cmsBool, cmsWhitePointFromTemp,
	    (cmsCIExyY* WhitePoint, cmsFloat64Number TempK));
DEF_DLL_FN (void, cmsxyY2XYZ, (cmsCIEXYZ* Dest, const cmsCIExyY* Source));
53 54 55 56 57 58 59 60 61 62 63 64 65 66

static bool lcms_initialized;

static bool
init_lcms_functions (void)
{
  HMODULE library = w32_delayed_load (Qlcms2);

  if (!library)
    return false;

  LOAD_DLL_FN (library, cmsCIE2000DeltaE);
  LOAD_DLL_FN (library, cmsCIECAM02Init);
  LOAD_DLL_FN (library, cmsCIECAM02Forward);
67
  LOAD_DLL_FN (library, cmsCIECAM02Reverse);
68
  LOAD_DLL_FN (library, cmsCIECAM02Done);
69 70
  LOAD_DLL_FN (library, cmsWhitePointFromTemp);
  LOAD_DLL_FN (library, cmsxyY2XYZ);
71 72 73 74 75 76
  return true;
}

# undef cmsCIE2000DeltaE
# undef cmsCIECAM02Init
# undef cmsCIECAM02Forward
77
# undef cmsCIECAM02Reverse
78
# undef cmsCIECAM02Done
79 80
# undef cmsWhitePointFromTemp
# undef cmsxyY2XYZ
81

82 83 84
# define cmsCIE2000DeltaE      fn_cmsCIE2000DeltaE
# define cmsCIECAM02Init       fn_cmsCIECAM02Init
# define cmsCIECAM02Forward    fn_cmsCIECAM02Forward
85
# define cmsCIECAM02Reverse    fn_cmsCIECAM02Reverse
86 87 88
# define cmsCIECAM02Done       fn_cmsCIECAM02Done
# define cmsWhitePointFromTemp fn_cmsWhitePointFromTemp
# define cmsxyY2XYZ            fn_cmsxyY2XYZ
89 90 91

#endif	/* WINDOWSNT */

Mark Oteiza's avatar
Mark Oteiza committed
92 93 94 95
static bool
parse_lab_list (Lisp_Object lab_list, cmsCIELab *color)
{
#define PARSE_LAB_LIST_FIELD(field)					\
96
  if (CONSP (lab_list) && NUMBERP (XCAR (lab_list)))			\
Mark Oteiza's avatar
Mark Oteiza committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110
    {									\
      color->field = XFLOATINT (XCAR (lab_list));			\
      lab_list = XCDR (lab_list);					\
    }									\
  else									\
    return false;

  PARSE_LAB_LIST_FIELD (L);
  PARSE_LAB_LIST_FIELD (a);
  PARSE_LAB_LIST_FIELD (b);

  return true;
}

Paul Eggert's avatar
Paul Eggert committed
111
/* http://www.ece.rochester.edu/~gsharma/ciede2000/ciede2000noteCRNA.pdf */
Mark Oteiza's avatar
Mark Oteiza committed
112 113 114 115 116 117

DEFUN ("lcms-cie-de2000", Flcms_cie_de2000, Slcms_cie_de2000, 2, 5, 0,
       doc: /* Compute CIEDE2000 metric distance between COLOR1 and COLOR2.
Each color is a list of L*a*b* coordinates, where the L* channel ranges from
0 to 100, and the a* and b* channels range from -128 to 128.
Optional arguments KL, KC, KH are weighting parameters for lightness,
118
chroma, and hue, respectively. The parameters each default to 1.  */)
Mark Oteiza's avatar
Mark Oteiza committed
119 120 121 122 123 124
  (Lisp_Object color1, Lisp_Object color2,
   Lisp_Object kL, Lisp_Object kC, Lisp_Object kH)
{
  cmsCIELab Lab1, Lab2;
  cmsFloat64Number Kl, Kc, Kh;

125 126 127 128 129 130 131 132 133 134
#ifdef WINDOWSNT
  if (!lcms_initialized)
    lcms_initialized = init_lcms_functions ();
  if (!lcms_initialized)
    {
      message1 ("lcms2 library not found");
      return Qnil;
    }
#endif

Mark Oteiza's avatar
Mark Oteiza committed
135 136 137 138 139 140
  if (!(CONSP (color1) && parse_lab_list (color1, &Lab1)))
    signal_error ("Invalid color", color1);
  if (!(CONSP (color2) && parse_lab_list (color2, &Lab2)))
    signal_error ("Invalid color", color1);
  if (NILP (kL))
    Kl = 1.0f;
141
  else if (!(NUMBERP (kL) && (Kl = XFLOATINT(kL))))
Mark Oteiza's avatar
Mark Oteiza committed
142 143 144
    wrong_type_argument(Qnumberp, kL);
  if (NILP (kC))
    Kc = 1.0f;
145
  else if (!(NUMBERP (kC) && (Kc = XFLOATINT(kC))))
Mark Oteiza's avatar
Mark Oteiza committed
146 147 148
    wrong_type_argument(Qnumberp, kC);
  if (NILP (kL))
    Kh = 1.0f;
149
  else if (!(NUMBERP (kH) && (Kh = XFLOATINT(kH))))
Mark Oteiza's avatar
Mark Oteiza committed
150 151 152 153 154
    wrong_type_argument(Qnumberp, kH);

  return make_float (cmsCIE2000DeltaE (&Lab1, &Lab2, Kl, Kc, Kh));
}

155 156 157 158 159 160
static double
deg2rad (double degrees)
{
  return M_PI * degrees / 180.0;
}

161 162 163 164 165 166
static double
rad2deg (double radians)
{
  return 180.0 * radians / M_PI;
}

167 168 169 170 171 172 173 174 175 176 177 178 179 180
static cmsCIEXYZ illuminant_d65 = { .X = 95.0455, .Y = 100.0, .Z = 108.8753 };

static void
default_viewing_conditions (const cmsCIEXYZ *wp, cmsViewingConditions *vc)
{
  vc->whitePoint.X = wp->X;
  vc->whitePoint.Y = wp->Y;
  vc->whitePoint.Z = wp->Z;
  vc->Yb = 20;
  vc->La = 100;
  vc->surround = AVG_SURROUND;
  vc->D_value = 1.0;
}

Mark Oteiza's avatar
Mark Oteiza committed
181 182 183 184 185 186
/* FIXME: code duplication */

static bool
parse_xyz_list (Lisp_Object xyz_list, cmsCIEXYZ *color)
{
#define PARSE_XYZ_LIST_FIELD(field)					\
187
  if (CONSP (xyz_list) && NUMBERP (XCAR (xyz_list)))			\
Mark Oteiza's avatar
Mark Oteiza committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201
    {									\
      color->field = 100.0 * XFLOATINT (XCAR (xyz_list));		\
      xyz_list = XCDR (xyz_list);					\
    }									\
  else									\
    return false;

  PARSE_XYZ_LIST_FIELD (X);
  PARSE_XYZ_LIST_FIELD (Y);
  PARSE_XYZ_LIST_FIELD (Z);

  return true;
}

202 203 204 205
static bool
parse_jch_list (Lisp_Object jch_list, cmsJCh *color)
{
#define PARSE_JCH_LIST_FIELD(field)					\
206
  if (CONSP (jch_list) && NUMBERP (XCAR (jch_list)))			\
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    {									\
      color->field = XFLOATINT (XCAR (jch_list));			\
      jch_list = XCDR (jch_list);					\
    }									\
  else									\
    return false;

  PARSE_JCH_LIST_FIELD (J);
  PARSE_JCH_LIST_FIELD (C);
  PARSE_JCH_LIST_FIELD (h);

  if (! NILP (jch_list))
    return false;
  return true;
}

static bool
parse_jab_list (Lisp_Object jab_list, lcmsJab_t *color)
{
#define PARSE_JAB_LIST_FIELD(field)					\
227
  if (CONSP (jab_list) && NUMBERP (XCAR (jab_list)))			\
228 229 230 231 232 233 234 235 236 237 238 239 240 241
    {									\
      color->field = XFLOATINT (XCAR (jab_list));			\
      jab_list = XCDR (jab_list);					\
    }									\
  else									\
    return false;

  PARSE_JAB_LIST_FIELD (J);
  PARSE_JAB_LIST_FIELD (a);
  PARSE_JAB_LIST_FIELD (b);

  return true;
}

242 243 244 245 246
static bool
parse_viewing_conditions (Lisp_Object view, const cmsCIEXYZ *wp,
                          cmsViewingConditions *vc)
{
#define PARSE_VIEW_CONDITION_FLOAT(field)				\
247
  if (CONSP (view) && NUMBERP (XCAR (view)))				\
248 249 250 251 252 253 254
    {									\
      vc->field = XFLOATINT (XCAR (view));				\
      view = XCDR (view);						\
    }									\
  else									\
    return false;
#define PARSE_VIEW_CONDITION_INT(field)					\
255
  if (CONSP (view) && FIXNATP (XCAR (view)))				\
256
    {									\
Paul Eggert's avatar
Paul Eggert committed
257
      vc->field = check_integer_range (XCAR (view), 1, 4);		\
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
      view = XCDR (view);						\
    }									\
  else									\
    return false;

  PARSE_VIEW_CONDITION_FLOAT (Yb);
  PARSE_VIEW_CONDITION_FLOAT (La);
  PARSE_VIEW_CONDITION_INT (surround);
  PARSE_VIEW_CONDITION_FLOAT (D_value);

  if (! NILP (view))
    return false;

  vc->whitePoint.X = wp->X;
  vc->whitePoint.Y = wp->Y;
  vc->whitePoint.Z = wp->Z;
  return true;
}

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
static void
xyz_to_jch (const cmsCIEXYZ *xyz, cmsJCh *jch, const cmsViewingConditions *vc)
{
  cmsHANDLE h;

  h = cmsCIECAM02Init (0, vc);
  cmsCIECAM02Forward (h, xyz, jch);
  cmsCIECAM02Done (h);
}

static void
jch_to_xyz (const cmsJCh *jch, cmsCIEXYZ *xyz, const cmsViewingConditions *vc)
{
  cmsHANDLE h;

  h = cmsCIECAM02Init (0, vc);
  cmsCIECAM02Reverse (h, jch, xyz);
  cmsCIECAM02Done (h);
}

static void
jch_to_jab (const cmsJCh *jch, lcmsJab_t *jab, double FL, double c1, double c2)
{
  double Mp = 43.86 * log (1.0 + c2 * (jch->C * sqrt (sqrt (FL))));
  jab->J = 1.7 * jch->J / (1.0 + (c1 * jch->J));
  jab->a = Mp * cos (deg2rad (jch->h));
  jab->b = Mp * sin (deg2rad (jch->h));
}

static void
jab_to_jch (const lcmsJab_t *jab, cmsJCh *jch, double FL, double c1, double c2)
{
  jch->J = jab->J / (1.0 + c1 * (100.0 - jab->J));
  jch->h = atan2 (jab->b, jab->a);
  double Mp = hypot (jab->a, jab->b);
  jch->h = rad2deg (jch->h);
  if (jch->h < 0.0)
    jch->h += 360.0;
  jch->C = (exp (c2 * Mp) - 1.0) / (c2 * sqrt (sqrt (FL)));
}

DEFUN ("lcms-xyz->jch", Flcms_xyz_to_jch, Slcms_xyz_to_jch, 1, 3, 0,
Mark Oteiza's avatar
Mark Oteiza committed
319
       doc: /* Convert CIE XYZ to CIE CAM02 JCh.
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
COLOR is a list (X Y Z), with Y scaled about unity.
Optional arguments WHITEPOINT and VIEW are the same as in `lcms-cam02-ucs',
which see.  */)
  (Lisp_Object color, Lisp_Object whitepoint, Lisp_Object view)
{
  cmsViewingConditions vc;
  cmsJCh jch;
  cmsCIEXYZ xyz, xyzw;

#ifdef WINDOWSNT
  if (!lcms_initialized)
    lcms_initialized = init_lcms_functions ();
  if (!lcms_initialized)
    {
      message1 ("lcms2 library not found");
      return Qnil;
    }
#endif

  if (!(CONSP (color) && parse_xyz_list (color, &xyz)))
    signal_error ("Invalid color", color);
  if (NILP (whitepoint))
    xyzw = illuminant_d65;
  else if (!(CONSP (whitepoint) && parse_xyz_list (whitepoint, &xyzw)))
    signal_error ("Invalid white point", whitepoint);
  if (NILP (view))
    default_viewing_conditions (&xyzw, &vc);
  else if (!(CONSP (view) && parse_viewing_conditions (view, &xyzw, &vc)))
    signal_error ("Invalid viewing conditions", view);

  xyz_to_jch(&xyz, &jch, &vc);
  return list3 (make_float (jch.J), make_float (jch.C), make_float (jch.h));
}

DEFUN ("lcms-jch->xyz", Flcms_jch_to_xyz, Slcms_jch_to_xyz, 1, 3, 0,
Mark Oteiza's avatar
Mark Oteiza committed
355
       doc: /* Convert CIE CAM02 JCh to CIE XYZ.
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
COLOR is a list (J C h), where lightness of white is equal to 100, and hue
is given in degrees.
Optional arguments WHITEPOINT and VIEW are the same as in `lcms-cam02-ucs',
which see.  */)
  (Lisp_Object color, Lisp_Object whitepoint, Lisp_Object view)
{
  cmsViewingConditions vc;
  cmsJCh jch;
  cmsCIEXYZ xyz, xyzw;

#ifdef WINDOWSNT
  if (!lcms_initialized)
    lcms_initialized = init_lcms_functions ();
  if (!lcms_initialized)
    {
      message1 ("lcms2 library not found");
      return Qnil;
    }
#endif

  if (!(CONSP (color) && parse_jch_list (color, &jch)))
    signal_error ("Invalid color", color);
  if (NILP (whitepoint))
    xyzw = illuminant_d65;
  else if (!(CONSP (whitepoint) && parse_xyz_list (whitepoint, &xyzw)))
    signal_error ("Invalid white point", whitepoint);
  if (NILP (view))
    default_viewing_conditions (&xyzw, &vc);
  else if (!(CONSP (view) && parse_viewing_conditions (view, &xyzw, &vc)))
    signal_error ("Invalid viewing conditions", view);

  jch_to_xyz(&jch, &xyz, &vc);
  return list3 (make_float (xyz.X / 100.0),
                make_float (xyz.Y / 100.0),
                make_float (xyz.Z / 100.0));
}

DEFUN ("lcms-jch->jab", Flcms_jch_to_jab, Slcms_jch_to_jab, 1, 3, 0,
       doc: /* Convert CIE CAM02 JCh to CAM02-UCS J'a'b'.
COLOR is a list (J C h) as described in `lcms-jch->xyz', which see.
Optional arguments WHITEPOINT and VIEW are the same as in `lcms-cam02-ucs',
which see.  */)
  (Lisp_Object color, Lisp_Object whitepoint, Lisp_Object view)
{
  cmsViewingConditions vc;
  lcmsJab_t jab;
  cmsJCh jch;
  cmsCIEXYZ xyzw;
  double FL, k, k4;

#ifdef WINDOWSNT
  if (!lcms_initialized)
    lcms_initialized = init_lcms_functions ();
  if (!lcms_initialized)
    {
      message1 ("lcms2 library not found");
      return Qnil;
    }
#endif

  if (!(CONSP (color) && parse_jch_list (color, &jch)))
    signal_error ("Invalid color", color);
  if (NILP (whitepoint))
    xyzw = illuminant_d65;
  else if (!(CONSP (whitepoint) && parse_xyz_list (whitepoint, &xyzw)))
    signal_error ("Invalid white point", whitepoint);
  if (NILP (view))
    default_viewing_conditions (&xyzw, &vc);
  else if (!(CONSP (view) && parse_viewing_conditions (view, &xyzw, &vc)))
    signal_error ("Invalid viewing conditions", view);

  k = 1.0 / (1.0 + (5.0 * vc.La));
  k4 = k * k * k * k;
  FL = vc.La * k4 + 0.1 * (1 - k4) * (1 - k4) * cbrt (5.0 * vc.La);
  jch_to_jab (&jch, &jab, FL, 0.007, 0.0228);
  return list3 (make_float (jab.J), make_float (jab.a), make_float (jab.b));
}

DEFUN ("lcms-jab->jch", Flcms_jab_to_jch, Slcms_jab_to_jch, 1, 3, 0,
       doc: /* Convert CAM02-UCS J'a'b' to CIE CAM02 JCh.
COLOR is a list (J' a' b'), where white corresponds to lightness J equal to 100.
Optional arguments WHITEPOINT and VIEW are the same as in `lcms-cam02-ucs',
which see.  */)
  (Lisp_Object color, Lisp_Object whitepoint, Lisp_Object view)
{
  cmsViewingConditions vc;
  cmsJCh jch;
  lcmsJab_t jab;
  cmsCIEXYZ xyzw;
  double FL, k, k4;

#ifdef WINDOWSNT
  if (!lcms_initialized)
    lcms_initialized = init_lcms_functions ();
  if (!lcms_initialized)
    {
      message1 ("lcms2 library not found");
      return Qnil;
    }
#endif

  if (!(CONSP (color) && parse_jab_list (color, &jab)))
    signal_error ("Invalid color", color);
  if (NILP (whitepoint))
    xyzw = illuminant_d65;
  else if (!(CONSP (whitepoint) && parse_xyz_list (whitepoint, &xyzw)))
    signal_error ("Invalid white point", whitepoint);
  if (NILP (view))
    default_viewing_conditions (&xyzw, &vc);
  else if (!(CONSP (view) && parse_viewing_conditions (view, &xyzw, &vc)))
    signal_error ("Invalid viewing conditions", view);

  k = 1.0 / (1.0 + (5.0 * vc.La));
  k4 = k * k * k * k;
  FL = vc.La * k4 + 0.1 * (1 - k4) * (1 - k4) * cbrt (5.0 * vc.La);
  jab_to_jch (&jab, &jch, FL, 0.007, 0.0228);
  return list3 (make_float (jch.J), make_float (jch.C), make_float (jch.h));
}

475 476 477 478 479 480 481
/* References:
   Li, Luo et al. "The CRI-CAM02UCS colour rendering index." COLOR research
   and application, 37 No.3, 2012.
   Luo et al. "Uniform colour spaces based on CIECAM02 colour appearance
   model." COLOR research and application, 31 No.4, 2006. */

DEFUN ("lcms-cam02-ucs", Flcms_cam02_ucs, Slcms_cam02_ucs, 2, 4, 0,
Mark Oteiza's avatar
Mark Oteiza committed
482
       doc: /* Compute CAM02-UCS metric distance between COLOR1 and COLOR2.
483 484 485 486 487 488 489 490 491 492 493 494
Each color is a list of XYZ tristimulus values, with Y scaled about unity.
Optional argument WHITEPOINT is the XYZ white point, which defaults to
illuminant D65.
Optional argument VIEW is a list containing the viewing conditions, and
is of the form (YB LA SURROUND DVALUE) where SURROUND corresponds to
  1   AVG_SURROUND
  2   DIM_SURROUND
  3   DARK_SURROUND
  4   CUTSHEET_SURROUND
The default viewing conditions are (20 100 1 1).  */)
  (Lisp_Object color1, Lisp_Object color2, Lisp_Object whitepoint,
   Lisp_Object view)
Mark Oteiza's avatar
Mark Oteiza committed
495 496 497 498
{
  cmsViewingConditions vc;
  cmsJCh jch1, jch2;
  cmsCIEXYZ xyz1, xyz2, xyzw;
499 500
  lcmsJab_t jab1, jab2;
  double FL, k, k4;
Mark Oteiza's avatar
Mark Oteiza committed
501

502 503 504 505 506 507 508 509 510 511
#ifdef WINDOWSNT
  if (!lcms_initialized)
    lcms_initialized = init_lcms_functions ();
  if (!lcms_initialized)
    {
      message1 ("lcms2 library not found");
      return Qnil;
    }
#endif

Mark Oteiza's avatar
Mark Oteiza committed
512 513 514
  if (!(CONSP (color1) && parse_xyz_list (color1, &xyz1)))
    signal_error ("Invalid color", color1);
  if (!(CONSP (color2) && parse_xyz_list (color2, &xyz2)))
Mark Oteiza's avatar
Mark Oteiza committed
515
    signal_error ("Invalid color", color2);
Mark Oteiza's avatar
Mark Oteiza committed
516
  if (NILP (whitepoint))
517
    xyzw = illuminant_d65;
518
  else if (!(CONSP (whitepoint) && parse_xyz_list (whitepoint, &xyzw)))
Mark Oteiza's avatar
Mark Oteiza committed
519
    signal_error ("Invalid white point", whitepoint);
520 521 522 523
  if (NILP (view))
    default_viewing_conditions (&xyzw, &vc);
  else if (!(CONSP (view) && parse_viewing_conditions (view, &xyzw, &vc)))
    signal_error ("Invalid view conditions", view);
Mark Oteiza's avatar
Mark Oteiza committed
524

525 526
  xyz_to_jch (&xyz1, &jch1, &vc);
  xyz_to_jch (&xyz2, &jch2, &vc);
Mark Oteiza's avatar
Mark Oteiza committed
527 528 529 530

  k = 1.0 / (1.0 + (5.0 * vc.La));
  k4 = k * k * k * k;
  FL = vc.La * k4 + 0.1 * (1 - k4) * (1 - k4) * cbrt (5.0 * vc.La);
531 532 533 534 535
  jch_to_jab (&jch1, &jab1, FL, 0.007, 0.0228);
  jch_to_jab (&jch2, &jab2, FL, 0.007, 0.0228);

  return make_float (hypot (jab2.J - jab1.J,
                            hypot (jab2.a - jab1.a, jab2.b - jab1.b)));
Mark Oteiza's avatar
Mark Oteiza committed
536 537
}

538 539
DEFUN ("lcms-temp->white-point", Flcms_temp_to_white_point, Slcms_temp_to_white_point, 1, 1, 0,
       doc: /* Return XYZ black body chromaticity from TEMPERATURE given in K.
540
Valid range of TEMPERATURE is from 4000K to 25000K.  */)
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
  (Lisp_Object temperature)
{
  cmsFloat64Number tempK;
  cmsCIExyY whitepoint;
  cmsCIEXYZ wp;

#ifdef WINDOWSNT
  if (!lcms_initialized)
    lcms_initialized = init_lcms_functions ();
  if (!lcms_initialized)
    {
      message1 ("lcms2 library not found");
      return Qnil;
    }
#endif

557
  CHECK_NUMBER (temperature);
558

559 560
  tempK = XFLOATINT (temperature);
  if (!(cmsWhitePointFromTemp (&whitepoint, tempK)))
561
    signal_error("Invalid temperature", temperature);
562
  cmsxyY2XYZ (&wp, &whitepoint);
563 564 565
  return list3 (make_float (wp.X), make_float (wp.Y), make_float (wp.Z));
}

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
DEFUN ("lcms2-available-p", Flcms2_available_p, Slcms2_available_p, 0, 0, 0,
       doc: /* Return t if lcms2 color calculations are available in this instance of Emacs.  */)
     (void)
{
#ifdef WINDOWSNT
  Lisp_Object found = Fassq (Qlcms2, Vlibrary_cache);
  if (CONSP (found))
    return XCDR (found);
  else
    {
      Lisp_Object status;
      lcms_initialized = init_lcms_functions ();
      status = lcms_initialized ? Qt : Qnil;
      Vlibrary_cache = Fcons (Fcons (Qlcms2, status), Vlibrary_cache);
      return status;
    }
#else  /* !WINDOWSNT */
  return Qt;
#endif
}

Mark Oteiza's avatar
Mark Oteiza committed
587 588 589 590 591 592

/* Initialization */
void
syms_of_lcms2 (void)
{
  defsubr (&Slcms_cie_de2000);
593 594 595 596
  defsubr (&Slcms_xyz_to_jch);
  defsubr (&Slcms_jch_to_xyz);
  defsubr (&Slcms_jch_to_jab);
  defsubr (&Slcms_jab_to_jch);
Mark Oteiza's avatar
Mark Oteiza committed
597
  defsubr (&Slcms_cam02_ucs);
598
  defsubr (&Slcms2_available_p);
599
  defsubr (&Slcms_temp_to_white_point);
Mark Oteiza's avatar
Mark Oteiza committed
600 601

  Fprovide (intern_c_string ("lcms2"), Qnil);
Mark Oteiza's avatar
Mark Oteiza committed
602 603 604
}

#endif /* HAVE_LCMS2 */