gimpcolorprofile.c 55 KB
Newer Older
1 2 3
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
 *
4
 * gimpcolorprofile.c
5 6
 * Copyright (C) 2014  Michael Natterer <mitch@gimp.org>
 *                     Elle Stone <ellestone@ninedegreesbelow.com>
7
 *                     Øyvind Kolås <pippin@gimp.org>
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
21
 * <https://www.gnu.org/licenses/>.
22 23 24 25
 */

#include "config.h"

26 27
#include <string.h>

28 29
#include <lcms2.h>

30
#include <gio/gio.h>
31 32
#include <gegl.h>

33 34
#include "libgimpbase/gimpbase.h"

35 36
#include "gimpcolortypes.h"

37
#include "gimpcolorprofile.h"
38

39 40
#include "libgimp/libgimp-intl.h"

41

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#ifndef TYPE_RGBA_DBL
#define TYPE_RGBA_DBL       (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(0))
#endif

#ifndef TYPE_GRAYA_HALF_FLT
#define TYPE_GRAYA_HALF_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_GRAY)|EXTRA_SH(1)|CHANNELS_SH(1)|BYTES_SH(2))
#endif

#ifndef TYPE_GRAYA_FLT
#define TYPE_GRAYA_FLT      (FLOAT_SH(1)|COLORSPACE_SH(PT_GRAY)|EXTRA_SH(1)|CHANNELS_SH(1)|BYTES_SH(4))
#endif

#ifndef TYPE_GRAYA_DBL
#define TYPE_GRAYA_DBL      (FLOAT_SH(1)|COLORSPACE_SH(PT_GRAY)|EXTRA_SH(1)|CHANNELS_SH(1)|BYTES_SH(0))
#endif

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#ifndef TYPE_CMYKA_DBL
#define TYPE_CMYKA_DBL      (FLOAT_SH(1)|COLORSPACE_SH(PT_CMYK)|EXTRA_SH(1)|CHANNELS_SH(4)|BYTES_SH(0))
#endif

#ifndef TYPE_CMYKA_HALF_FLT
#define TYPE_CMYKA_HALF_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_CMYK)|EXTRA_SH(1)|CHANNELS_SH(4)|BYTES_SH(2))
#endif

#ifndef TYPE_CMYKA_FLT
#define TYPE_CMYKA_FLT      (FLOAT_SH(1)|COLORSPACE_SH(PT_CMYK)|EXTRA_SH(1)|CHANNELS_SH(4)|BYTES_SH(4))
#endif

#ifndef TYPE_CMYKA_16
#define TYPE_CMYKA_16       (COLORSPACE_SH(PT_CMYK)|EXTRA_SH(1)|CHANNELS_SH(4)|BYTES_SH(2))
#endif

74

75
/**
76 77
 * SECTION: gimpcolorprofile
 * @title: GimpColorProfile
78 79 80 81 82
 * @short_description: Definitions and Functions relating to LCMS.
 *
 * Definitions and Functions relating to LCMS.
 **/

83 84 85 86 87 88 89 90
/**
 * GimpColorProfile:
 *
 * Simply a typedef to #gpointer, but actually is a cmsHPROFILE. It's
 * used in public GIMP APIs in order to avoid having to include LCMS
 * headers.
 **/

91

92 93 94 95 96
struct _GimpColorProfilePrivate
{
  cmsHPROFILE  lcms_profile;
  guint8      *data;
  gsize        length;
97 98 99 100 101 102 103

  gchar       *description;
  gchar       *manufacturer;
  gchar       *model;
  gchar       *copyright;
  gchar       *label;
  gchar       *summary;
104 105 106 107 108 109
};


static void   gimp_color_profile_finalize (GObject *object);


110
G_DEFINE_TYPE_WITH_PRIVATE (GimpColorProfile, gimp_color_profile, G_TYPE_OBJECT)
111 112 113 114

#define parent_class gimp_color_profile_parent_class


115 116
#define GIMP_COLOR_PROFILE_ERROR gimp_color_profile_error_quark ()

117
static GQuark
118
gimp_color_profile_error_quark (void)
119 120 121 122
{
  static GQuark quark = 0;

  if (G_UNLIKELY (quark == 0))
123
    quark = g_quark_from_static_string ("gimp-color-profile-error-quark");
124 125 126 127

  return quark;
}

128 129 130 131 132 133 134 135 136 137 138
static void
gimp_color_profile_class_init (GimpColorProfileClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = gimp_color_profile_finalize;
}

static void
gimp_color_profile_init (GimpColorProfile *profile)
{
139
  profile->priv = gimp_color_profile_get_instance_private (profile);
140 141 142 143 144 145 146
}

static void
gimp_color_profile_finalize (GObject *object)
{
  GimpColorProfile *profile = GIMP_COLOR_PROFILE (object);

147 148 149 150 151 152 153 154 155 156 157
  g_clear_pointer (&profile->priv->lcms_profile, cmsCloseProfile);

  g_clear_pointer (&profile->priv->data, g_free);
  profile->priv->length = 0;

  g_clear_pointer (&profile->priv->description,  g_free);
  g_clear_pointer (&profile->priv->manufacturer, g_free);
  g_clear_pointer (&profile->priv->model,        g_free);
  g_clear_pointer (&profile->priv->copyright,    g_free);
  g_clear_pointer (&profile->priv->label,        g_free);
  g_clear_pointer (&profile->priv->summary,      g_free);
158

159 160 161 162
  G_OBJECT_CLASS (parent_class)->finalize (object);
}


163
/**
164
 * gimp_color_profile_new_from_file:
165 166 167 168 169
 * @file:  a #GFile
 * @error: return location for #GError
 *
 * This function opens an ICC color profile from @file.
 *
170
 * Returns: (nullable): the #GimpColorProfile, or %NULL. On error, %NULL is
171 172
 *               returned and @error is set.
 *
173
 * Since: 2.10
174
 **/
175 176 177
GimpColorProfile *
gimp_color_profile_new_from_file (GFile   *file,
                                  GError **error)
178
{
179 180 181 182
  GimpColorProfile *profile      = NULL;
  cmsHPROFILE       lcms_profile = NULL;
  guint8           *data         = NULL;
  gsize             length       = 0;
183
  gchar            *path;
184

185
  g_return_val_if_fail (G_IS_FILE (file), NULL);
186 187
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

188
  path = g_file_get_path (file);
189

190 191
  if (path)
    {
192
      GMappedFile *mapped;
193

194
      mapped = g_mapped_file_new (path, FALSE, error);
195
      g_free (path);
196

197 198
      if (! mapped)
        return NULL;
199

200
      length = g_mapped_file_get_length (mapped);
201
      data   = g_memdup (g_mapped_file_get_contents (mapped), length);
202

203
      lcms_profile = cmsOpenProfileFromMem (data, length);
204 205 206 207 208 209 210 211 212 213 214 215 216 217

      g_mapped_file_unref (mapped);
    }
  else
    {
      GFileInfo *info;

      info = g_file_query_info (file,
                                G_FILE_ATTRIBUTE_STANDARD_SIZE,
                                G_FILE_QUERY_INFO_NONE,
                                NULL, error);
      if (info)
        {
          GInputStream *input;
218 219 220

          length = g_file_info_get_size (info);
          data   = g_malloc (length);
221 222 223 224 225 226 227 228 229 230 231 232 233

          g_object_unref (info);

          input = G_INPUT_STREAM (g_file_read (file, NULL, error));

          if (input)
            {
              gsize bytes_read;

              if (g_input_stream_read_all (input, data, length,
                                           &bytes_read, NULL, error) &&
                  bytes_read == length)
                {
234
                  lcms_profile = cmsOpenProfileFromMem (data, length);
235 236 237 238 239 240 241
                }

              g_object_unref (input);
            }
        }
    }

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
  if (lcms_profile)
    {
      profile = g_object_new (GIMP_TYPE_COLOR_PROFILE, NULL);

      profile->priv->lcms_profile = lcms_profile;
      profile->priv->data         = data;
      profile->priv->length       = length;
    }
  else
    {
      if (data)
        g_free (data);

      if (error && *error == NULL)
        {
257
          g_set_error (error, GIMP_COLOR_PROFILE_ERROR, 0,
258 259 260 261
                       _("'%s' does not appear to be an ICC color profile"),
                       gimp_file_get_utf8_name (file));
        }
    }
262 263 264 265

  return profile;
}

266
/**
267
 * gimp_color_profile_new_from_icc_profile:
268
 * @data:   pointer to memory containing an ICC profile
269
 * @length: length of the profile in memory, in bytes
270 271 272 273 274
 * @error:  return location for #GError
 *
 * This function opens an ICC color profile from memory. On error,
 * %NULL is returned and @error is set.
 *
275
 * Returns: (nullable): the #GimpColorProfile, or %NULL.
276
 *
277
 * Since: 2.10
278
 **/
279 280 281 282
GimpColorProfile *
gimp_color_profile_new_from_icc_profile (const guint8  *data,
                                         gsize          length,
                                         GError       **error)
283
{
284 285
  cmsHPROFILE       lcms_profile = 0;
  GimpColorProfile *profile      = NULL;
286

287
  g_return_val_if_fail (data != NULL || length == 0, NULL);
288 289
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

290 291
  if (length > 0)
    lcms_profile = cmsOpenProfileFromMem (data, length);
292 293 294 295

  if (lcms_profile)
    {
      profile = g_object_new (GIMP_TYPE_COLOR_PROFILE, NULL);
296

297 298 299 300 301 302
      profile->priv->lcms_profile = lcms_profile;
      profile->priv->data         = g_memdup (data, length);
      profile->priv->length       = length;
   }
  else
    {
303
      g_set_error_literal (error, GIMP_COLOR_PROFILE_ERROR, 0,
304 305
                           _("Data does not appear to be an ICC color profile"));
    }
306 307 308

  return profile;
}
309

310
/**
311 312 313
 * gimp_color_profile_new_from_lcms_profile:
 * @lcms_profile: an LCMS cmsHPROFILE pointer
 * @error:        return location for #GError
314
 *
315 316 317 318
 * This function creates a GimpColorProfile from a cmsHPROFILE. On
 * error, %NULL is returned and @error is set. The passed
 * @lcms_profile pointer is not retained by the created
 * #GimpColorProfile.
319
 *
320
 * Returns: (nullable): the #GimpColorProfile, or %NULL.
321
 *
322
 * Since: 2.10
323
 **/
324 325 326
GimpColorProfile *
gimp_color_profile_new_from_lcms_profile (gpointer   lcms_profile,
                                          GError   **error)
327 328 329
{
  cmsUInt32Number size;

330
  g_return_val_if_fail (lcms_profile != NULL, NULL);
331 332
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

333
  if (cmsSaveProfileToMem (lcms_profile, NULL, &size))
334 335 336
    {
      guint8 *data = g_malloc (size);

337
      if (cmsSaveProfileToMem (lcms_profile, data, &size))
338
        {
339 340 341 342 343 344 345 346 347
          gsize length = size;

          lcms_profile = cmsOpenProfileFromMem (data, length);

          if (lcms_profile)
            {
              GimpColorProfile *profile;

              profile = g_object_new (GIMP_TYPE_COLOR_PROFILE, NULL);
348

349 350 351 352 353 354
              profile->priv->lcms_profile = lcms_profile;
              profile->priv->data         = data;
              profile->priv->length       = length;

              return profile;
            }
355 356 357 358 359
        }

      g_free (data);
    }

360
  g_set_error_literal (error, GIMP_COLOR_PROFILE_ERROR, 0,
361 362 363 364 365
                       _("Could not save color profile to memory"));

  return NULL;
}

366 367
/**
 * gimp_color_profile_save_to_file:
368 369 370
 * @profile: a #GimpColorProfile
 * @file:    a #GFile
 * @error:   return location for #GError
371 372 373
 *
 * This function saves @profile to @file as ICC profile.
 *
374
 * Returns: %TRUE on success, %FALSE if an error occurred.
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
 *
 * Since: 2.10
 **/
gboolean
gimp_color_profile_save_to_file (GimpColorProfile  *profile,
                                 GFile             *file,
                                 GError           **error)
{
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), FALSE);
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  return g_file_replace_contents (file,
                                  (const gchar *) profile->priv->data,
                                  profile->priv->length,
                                  NULL, FALSE,
                                  G_FILE_CREATE_NONE,
                                  NULL,
                                  NULL,
                                  error);
}

397
/**
398
 * gimp_color_profile_get_icc_profile:
399
 * @profile: a #GimpColorProfile
400
 * @length:  return location for the number of bytes
401
 *
402 403 404
 * This function returns @profile as ICC profile data. The returned
 * memory belongs to @profile and must not be modified or freed.
 *
405
 * Returns: a pointer to the IIC profile data.
406
 *
407
 * Since: 2.10
408
 **/
409 410 411
const guint8 *
gimp_color_profile_get_icc_profile (GimpColorProfile  *profile,
                                    gsize             *length)
412
{
413 414 415 416
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), NULL);
  g_return_val_if_fail (length != NULL, NULL);

  *length = profile->priv->length;
417

418 419 420 421 422 423 424 425 426 427
  return profile->priv->data;
}

/**
 * gimp_color_profile_get_lcms_profile:
 * @profile: a #GimpColorProfile
 *
 * This function returns @profile's cmsHPROFILE. The returned
 * value belongs to @profile and must not be modified or freed.
 *
428
 * Returns: a pointer to the cmsHPROFILE.
429 430 431 432 433 434 435 436 437
 *
 * Since: 2.10
 **/
gpointer
gimp_color_profile_get_lcms_profile (GimpColorProfile *profile)
{
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), NULL);

  return profile->priv->lcms_profile;
438 439
}

440
static gchar *
441 442
gimp_color_profile_get_info (GimpColorProfile *profile,
                             cmsInfoType       info)
443 444
{
  cmsUInt32Number  size;
445
  gchar           *text = NULL;
446

447
  size = cmsGetProfileInfoASCII (profile->priv->lcms_profile, info,
448 449 450 451 452
                                 "en", "US", NULL, 0);
  if (size > 0)
    {
      gchar *data = g_new (gchar, size + 1);

453
      size = cmsGetProfileInfoASCII (profile->priv->lcms_profile, info,
454 455
                                     "en", "US", data, size);
      if (size > 0)
456
        text = gimp_any_to_utf8 (data, -1, NULL);
457 458 459 460

      g_free (data);
    }

461
  return text;
462 463
}

464
/**
465
 * gimp_color_profile_get_description:
466 467
 * @profile: a #GimpColorProfile
 *
468
 * Returns: a string containing @profile's description. The
469 470
 *               returned value belongs to @profile and must not be
 *               modified or freed.
471
 *
472
 * Since: 2.10
473
 **/
474
const gchar *
475
gimp_color_profile_get_description (GimpColorProfile *profile)
476
{
477 478 479 480 481 482 483
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), NULL);

  if (! profile->priv->description)
    profile->priv->description =
      gimp_color_profile_get_info (profile, cmsInfoDescription);

  return profile->priv->description;
484
}
485

486
/**
487
 * gimp_color_profile_get_manufacturer:
488 489
 * @profile: a #GimpColorProfile
 *
490
 * Returns: a string containing @profile's manufacturer. The
491 492
 *               returned value belongs to @profile and must not be
 *               modified or freed.
493
 *
494
 * Since: 2.10
495
 **/
496
const gchar *
497
gimp_color_profile_get_manufacturer (GimpColorProfile *profile)
498
{
499 500 501 502 503 504 505
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), NULL);

  if (! profile->priv->manufacturer)
    profile->priv->manufacturer =
      gimp_color_profile_get_info (profile, cmsInfoManufacturer);

  return profile->priv->manufacturer;
506
}
507

508
/**
509
 * gimp_color_profile_get_model:
510 511
 * @profile: a #GimpColorProfile
 *
512
 * Returns: a string containing @profile's model. The returned
513 514
 *               value belongs to @profile and must not be modified or
 *               freed.
515
 *
516
 * Since: 2.10
517
 **/
518
const gchar *
519
gimp_color_profile_get_model (GimpColorProfile *profile)
520
{
521 522 523 524 525 526 527
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), NULL);

  if (! profile->priv->model)
    profile->priv->model =
      gimp_color_profile_get_info (profile, cmsInfoModel);

  return profile->priv->model;
528
}
529

530
/**
531
 * gimp_color_profile_get_copyright:
532 533
 * @profile: a #GimpColorProfile
 *
534
 * Returns: a string containing @profile's copyright. The
535 536
 *               returned value belongs to @profile and must not be
 *               modified or freed.
537
 *
538
 * Since: 2.10
539
 **/
540
const gchar *
541
gimp_color_profile_get_copyright (GimpColorProfile *profile)
542
{
543 544 545 546 547 548 549
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), NULL);

  if (! profile->priv->copyright)
    profile->priv->copyright =
      gimp_color_profile_get_info (profile, cmsInfoCopyright);

  return profile->priv->copyright;
550 551
}

552
/**
553
 * gimp_color_profile_get_label:
554 555
 * @profile: a #GimpColorProfile
 *
556 557
 * This function returns a string containing @profile's "title", a
 * string that can be used to label the profile in a user interface.
558
 *
559
 * Unlike gimp_color_profile_get_description(), this function always
560
 * returns a string (as a fallback, it returns "(unnamed profile)").
561
 *
562
 * Returns: the @profile's label. The returned value belongs to
563
 *               @profile and must not be modified or freed.
564
 *
565
 * Since: 2.10
566
 **/
567
const gchar *
568
gimp_color_profile_get_label (GimpColorProfile *profile)
569 570
{

571
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), NULL);
572

573
  if (! profile->priv->label)
574
    {
575
      const gchar *label = gimp_color_profile_get_description (profile);
576

577 578
      if (! label || ! strlen (label))
        label = _("(unnamed profile)");
579

580 581
      profile->priv->label = g_strdup (label);
    }
582

583
  return profile->priv->label;
584 585
}

586
/**
587
 * gimp_color_profile_get_summary:
588 589
 * @profile: a #GimpColorProfile
 *
590 591
 * This function return a string containing a multi-line summary of
 * @profile's description, model, manufacturer and copyright, to be
592
 * used as detailed information about the profile in a user
593
 * interface.
594
 *
595
 * Returns: the @profile's summary. The returned value belongs to
596
 *               @profile and must not be modified or freed.
597
 *
598
 * Since: 2.10
599
 **/
600
const gchar *
601
gimp_color_profile_get_summary (GimpColorProfile *profile)
602
{
603
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), NULL);
604

605
  if (! profile->priv->summary)
606
    {
607 608
      GString     *string = g_string_new (NULL);
      const gchar *text;
609

610 611 612
      text = gimp_color_profile_get_description (profile);
      if (text)
        g_string_append (string, text);
613

614 615 616 617 618
      text = gimp_color_profile_get_model (profile);
      if (text)
        {
          if (string->len > 0)
            g_string_append (string, "\n");
619

620
          g_string_append_printf (string, _("Model: %s"), text);
621
        }
622

623 624 625 626 627
      text = gimp_color_profile_get_manufacturer (profile);
      if (text)
        {
          if (string->len > 0)
            g_string_append (string, "\n");
628

629
          g_string_append_printf (string, _("Manufacturer: %s"), text);
630 631 632 633 634 635 636
        }

      text = gimp_color_profile_get_copyright (profile);
      if (text)
        {
          if (string->len > 0)
            g_string_append (string, "\n");
637 638

          g_string_append_printf (string, _("Copyright: %s"), text);
639
        }
640

641
      profile->priv->summary = g_string_free (string, FALSE);
642 643
    }

644
  return profile->priv->summary;
645 646
}

647
/**
648
 * gimp_color_profile_is_equal:
649 650 651 652 653
 * @profile1: a #GimpColorProfile
 * @profile2: a #GimpColorProfile
 *
 * Compares two profiles.
 *
654
 * Returns: %TRUE if the profiles are equal, %FALSE otherwise.
655
 *
656
 * Since: 2.10
657
 **/
658
gboolean
659 660
gimp_color_profile_is_equal (GimpColorProfile *profile1,
                             GimpColorProfile *profile2)
661
{
662
  const gsize header_len = sizeof (cmsICCHeader);
663

664
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile1), FALSE);
665
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile2), FALSE);
666

667 668
  return profile1 == profile2                              ||
         (profile1->priv->length == profile2->priv->length &&
669 670 671
          memcmp (profile1->priv->data + header_len,
                  profile2->priv->data + header_len,
                  profile1->priv->length - header_len) == 0);
672 673
}

674
/**
675
 * gimp_color_profile_is_rgb:
676 677
 * @profile: a #GimpColorProfile
 *
678
 * Returns: %TRUE if the profile's color space is RGB, %FALSE
679 680
 * otherwise.
 *
681
 * Since: 2.10
682
 **/
683
gboolean
684
gimp_color_profile_is_rgb (GimpColorProfile *profile)
685
{
686
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), FALSE);
687

688
  return (cmsGetColorSpace (profile->priv->lcms_profile) == cmsSigRgbData);
689 690
}

691 692 693 694
/**
 * gimp_color_profile_is_gray:
 * @profile: a #GimpColorProfile
 *
695
 * Returns: %TRUE if the profile's color space is grayscale, %FALSE
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
 * otherwise.
 *
 * Since: 2.10
 **/
gboolean
gimp_color_profile_is_gray (GimpColorProfile *profile)
{
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), FALSE);

  return (cmsGetColorSpace (profile->priv->lcms_profile) == cmsSigGrayData);
}

/**
 * gimp_color_profile_is_cmyk:
 * @profile: a #GimpColorProfile
 *
712
 * Returns: %TRUE if the profile's color space is CMYK, %FALSE
713 714 715 716 717 718 719 720 721 722 723 724
 * otherwise.
 *
 * Since: 2.10
 **/
gboolean
gimp_color_profile_is_cmyk (GimpColorProfile *profile)
{
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), FALSE);

  return (cmsGetColorSpace (profile->priv->lcms_profile) == cmsSigCmykData);
}

725 726 727 728 729 730 731 732 733

/**
 * gimp_color_profile_is_linear:
 * @profile: a #GimpColorProfile
 *
 * This function determines is the ICC profile represented by a GimpColorProfile
 * is a linear RGB profile or not, some profiles that are LUTs though linear
 * will also return FALSE;
 *
734
 * Returns: %TRUE if the profile is a matrix shaping profile with linear
735 736 737 738 739 740 741
 * TRCs, %FALSE otherwise.
 *
 * Since: 2.10
 **/
gboolean
gimp_color_profile_is_linear (GimpColorProfile *profile)
{
742
  cmsHPROFILE   prof;
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
  cmsToneCurve *curve;

  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), FALSE);

  prof = profile->priv->lcms_profile;

  if (! cmsIsMatrixShaper (prof))
    return FALSE;

  if (cmsIsCLUT (prof, INTENT_PERCEPTUAL, LCMS_USED_AS_INPUT))
    return FALSE;

  if (cmsIsCLUT (prof, INTENT_PERCEPTUAL, LCMS_USED_AS_OUTPUT))
    return FALSE;

758 759 760 761 762
  if (gimp_color_profile_is_rgb (profile))
    {
      curve = cmsReadTag(prof, cmsSigRedTRCTag);
      if (curve == NULL || ! cmsIsToneCurveLinear (curve))
        return FALSE;
763

764 765 766
      curve = cmsReadTag (prof, cmsSigGreenTRCTag);
      if (curve == NULL || ! cmsIsToneCurveLinear (curve))
        return FALSE;
767

768 769 770 771 772 773 774 775 776 777 778 779 780 781
      curve = cmsReadTag (prof, cmsSigBlueTRCTag);
      if (curve == NULL || ! cmsIsToneCurveLinear (curve))
        return FALSE;
    }
  else if (gimp_color_profile_is_gray (profile))
    {
      curve = cmsReadTag(prof, cmsSigGrayTRCTag);
      if (curve == NULL || ! cmsIsToneCurveLinear (curve))
        return FALSE;
    }
  else
    {
      return FALSE;
    }
782 783 784 785

  return TRUE;
}

786
static void
787 788 789
gimp_color_profile_set_tag (cmsHPROFILE      profile,
                            cmsTagSignature  sig,
                            const gchar     *tag)
790 791 792 793 794 795 796 797 798
{
  cmsMLU *mlu;

  mlu = cmsMLUalloc (NULL, 1);
  cmsMLUsetASCII (mlu, "en", "US", tag);
  cmsWriteTag (profile, sig, mlu);
  cmsMLUfree (mlu);
}

799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
static gboolean
gimp_color_profile_get_rgb_matrix_colorants (GimpColorProfile *profile,
                                             GimpMatrix3      *matrix)
{
  cmsHPROFILE  lcms_profile;
  cmsCIEXYZ   *red;
  cmsCIEXYZ   *green;
  cmsCIEXYZ   *blue;

  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), FALSE);

  lcms_profile = profile->priv->lcms_profile;

  red   = cmsReadTag (lcms_profile, cmsSigRedColorantTag);
  green = cmsReadTag (lcms_profile, cmsSigGreenColorantTag);
  blue  = cmsReadTag (lcms_profile, cmsSigBlueColorantTag);

  if (red && green && blue)
    {
      if (matrix)
        {
          matrix->coeff[0][0] = red->X;
          matrix->coeff[0][1] = red->Y;
          matrix->coeff[0][2] = red->Z;

          matrix->coeff[1][0] = green->X;
          matrix->coeff[1][1] = green->Y;
          matrix->coeff[1][2] = green->Z;

          matrix->coeff[2][0] = blue->X;
829 830
          matrix->coeff[2][1] = blue->Y;
          matrix->coeff[2][2] = blue->Z;
831 832 833 834 835 836 837 838
        }

      return TRUE;
    }

  return FALSE;
}

839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
static void
gimp_color_profile_make_tag (cmsHPROFILE       profile,
                             cmsTagSignature   sig,
                             const gchar      *gimp_tag,
                             const gchar      *gimp_prefix,
                             const gchar      *gimp_prefix_alt,
                             const gchar      *original_tag)
{
  if (! original_tag || ! strlen (original_tag) ||
      ! strcmp (original_tag, gimp_tag))
    {
      /* if there is no original tag (or it is the same as the new
       * tag), just use the new tag
       */

      gimp_color_profile_set_tag (profile, sig, gimp_tag);
    }
  else
    {
      /* otherwise prefix the existing tag with a gimp prefix
       * indicating that the profile has been generated
       */

      if (g_str_has_prefix (original_tag, gimp_prefix))
        {
          /* don't add multiple GIMP prefixes */
          gimp_color_profile_set_tag (profile, sig, original_tag);
        }
      else if (gimp_prefix_alt &&
               g_str_has_prefix (original_tag, gimp_prefix_alt))
        {
          /* replace GIMP prefix_alt by prefix */
          gchar *new_tag = g_strconcat (gimp_prefix,
                                        original_tag + strlen (gimp_prefix_alt),
                                        NULL);

          gimp_color_profile_set_tag (profile, sig, new_tag);
          g_free (new_tag);
        }
      else
        {
          gchar *new_tag = g_strconcat (gimp_prefix,
                                        original_tag,
                                        NULL);

          gimp_color_profile_set_tag (profile, sig, new_tag);
          g_free (new_tag);
        }
    }
}

890
static GimpColorProfile *
891 892
gimp_color_profile_new_from_color_profile (GimpColorProfile *profile,
                                           gboolean          linear)
893 894 895
{
  GimpColorProfile *new_profile;
  cmsHPROFILE       target_profile;
896
  GimpMatrix3       matrix = { { { 0, } } };
897 898
  cmsCIEXYZ        *whitepoint;
  cmsToneCurve     *curve;
899 900 901

  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), NULL);

902 903 904 905 906 907 908 909 910
  if (gimp_color_profile_is_rgb (profile))
    {
      if (! gimp_color_profile_get_rgb_matrix_colorants (profile, &matrix))
        return NULL;
    }
  else if (! gimp_color_profile_is_gray (profile))
    {
      return NULL;
    }
911

912 913 914
  whitepoint = cmsReadTag (profile->priv->lcms_profile,
                           cmsSigMediaWhitePointTag);

915 916 917 918 919
  target_profile = cmsCreateProfilePlaceholder (0);

  cmsSetProfileVersion (target_profile, 4.3);
  cmsSetDeviceClass (target_profile, cmsSigDisplayClass);
  cmsSetPCS (target_profile, cmsSigXYZData);
920 921 922 923 924 925 926

  cmsWriteTag (target_profile, cmsSigMediaWhitePointTag, whitepoint);

  if (linear)
    {
      /* linear light */
      curve = cmsBuildGamma (NULL, 1.00);
927

928 929 930 931 932
      gimp_color_profile_make_tag (target_profile, cmsSigProfileDescriptionTag,
                                   "linear TRC from unnamed profile",
                                   "linear TRC from ",
                                   "sRGB TRC from ",
                                   gimp_color_profile_get_description (profile));
933 934 935 936 937 938 939 940 941
    }
  else
    {
      cmsFloat64Number srgb_parameters[5] =
        { 2.4, 1.0 / 1.055,  0.055 / 1.055, 1.0 / 12.92, 0.04045 };

      /* sRGB curve */
      curve = cmsBuildParametricToneCurve (NULL, 4, srgb_parameters);

942 943 944 945 946
      gimp_color_profile_make_tag (target_profile, cmsSigProfileDescriptionTag,
                                   "sRGB TRC from unnamed profile",
                                   "sRGB TRC from ",
                                   "linear TRC from ",
                                   gimp_color_profile_get_description (profile));
947 948
    }

949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
  if (gimp_color_profile_is_rgb (profile))
    {
      cmsCIEXYZ red;
      cmsCIEXYZ green;
      cmsCIEXYZ blue;

      cmsSetColorSpace (target_profile, cmsSigRgbData);

      red.X = matrix.coeff[0][0];
      red.Y = matrix.coeff[0][1];
      red.Z = matrix.coeff[0][2];

      green.X = matrix.coeff[1][0];
      green.Y = matrix.coeff[1][1];
      green.Z = matrix.coeff[1][2];

      blue.X = matrix.coeff[2][0];
      blue.Y = matrix.coeff[2][1];
      blue.Z = matrix.coeff[2][2];

      cmsWriteTag (target_profile, cmsSigRedColorantTag,   &red);
      cmsWriteTag (target_profile, cmsSigGreenColorantTag, &green);
      cmsWriteTag (target_profile, cmsSigBlueColorantTag,  &blue);

      cmsWriteTag (target_profile, cmsSigRedTRCTag,   curve);
      cmsWriteTag (target_profile, cmsSigGreenTRCTag, curve);
      cmsWriteTag (target_profile, cmsSigBlueTRCTag,  curve);
    }
  else
    {
      cmsSetColorSpace (target_profile, cmsSigGrayData);

      cmsWriteTag (target_profile, cmsSigGrayTRCTag, curve);
    }
983 984

  cmsFreeToneCurve (curve);
985

986 987 988 989 990 991 992 993 994 995 996 997
  gimp_color_profile_make_tag (target_profile, cmsSigDeviceMfgDescTag,
                               "GIMP",
                               "GIMP from ", NULL,
                               gimp_color_profile_get_manufacturer (profile));
  gimp_color_profile_make_tag (target_profile, cmsSigDeviceModelDescTag,
                               "Generated by GIMP",
                               "GIMP from ", NULL,
                               gimp_color_profile_get_model (profile));
  gimp_color_profile_make_tag (target_profile, cmsSigCopyrightTag,
                               "Public Domain",
                               "GIMP from ", NULL,
                               gimp_color_profile_get_copyright (profile));
998

999 1000 1001 1002 1003 1004 1005
  new_profile = gimp_color_profile_new_from_lcms_profile (target_profile, NULL);

  cmsCloseProfile (target_profile);

  return new_profile;
}

1006
/**
1007
 * gimp_color_profile_new_srgb_trc_from_color_profile:
1008 1009 1010 1011 1012
 * @profile: a #GimpColorProfile
 *
 * This function creates a new RGB #GimpColorProfile with a sRGB gamma
 * TRC and @profile's RGB chromacities and whitepoint.
 *
1013
 * Returns: (nullable) (transfer full): the new #GimpColorProfile, or %NULL if
1014
 *               @profile is not an RGB profile or not matrix-based.
1015 1016 1017 1018
 *
 * Since: 2.10
 **/
GimpColorProfile *
1019
gimp_color_profile_new_srgb_trc_from_color_profile (GimpColorProfile *profile)
1020 1021 1022 1023 1024 1025 1026
{
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), NULL);

  return gimp_color_profile_new_from_color_profile (profile, FALSE);
}

/**
1027
 * gimp_color_profile_new_linear_from_color_profile:
1028 1029 1030 1031 1032
 * @profile: a #GimpColorProfile
 *
 * This function creates a new RGB #GimpColorProfile with a linear TRC
 * and @profile's RGB chromacities and whitepoint.
 *
1033
 * Returns: (nullable) (transfer full): the new #GimpColorProfile, or %NULL if
1034
 *               @profile is not an RGB profile or not matrix-based.
1035 1036 1037 1038
 *
 * Since: 2.10
 **/
GimpColorProfile *
1039
gimp_color_profile_new_linear_from_color_profile (GimpColorProfile *profile)
1040 1041 1042 1043 1044 1045
{
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (profile), NULL);

  return gimp_color_profile_new_from_color_profile (profile, TRUE);
}

1046
static cmsHPROFILE *
1047
gimp_color_profile_new_rgb_srgb_internal (void)
1048
{
1049
  cmsHPROFILE profile;
1050

1051 1052 1053 1054 1055 1056 1057 1058
  /* white point is D65 from the sRGB specs */
  cmsCIExyY whitepoint = { 0.3127, 0.3290, 1.0 };

  /* primaries are ITU‐R BT.709‐5 (xYY), which are also the primaries
   * from the sRGB specs, modified to properly account for hexadecimal
   * quantization during the profile making process.
   */
  cmsCIExyYTRIPLE primaries =
1059
    {
1060 1061 1062 1063 1064 1065
      /* R { 0.6400, 0.3300, 1.0 }, */
      /* G { 0.3000, 0.6000, 1.0 }, */
      /* B { 0.1500, 0.0600, 1.0 }  */
      /* R */ { 0.639998686, 0.330010138, 1.0 },
      /* G */ { 0.300003784, 0.600003357, 1.0 },
      /* B */ { 0.150002046, 0.059997204, 1.0 }
1066 1067 1068 1069 1070
    };

  cmsFloat64Number srgb_parameters[5] =
    { 2.4, 1.0 / 1.055,  0.055 / 1.055, 1.0 / 12.92, 0.04045 };

1071
  cmsToneCurve *curve[3];
1072

1073 1074 1075
  /* sRGB curve */
  curve[0] = curve[1] = curve[2] = cmsBuildParametricToneCurve (NULL, 4,
                                                                srgb_parameters);
1076

1077
  profile = cmsCreateRGBProfile (&whitepoint, &primaries, curve);
1078

1079
  cmsFreeToneCurve (curve[0]);
1080

1081
  gimp_color_profile_set_tag (profile, cmsSigProfileDescriptionTag,
1082
                              "GIMP built-in sRGB");
1083
  gimp_color_profile_set_tag (profile, cmsSigDeviceMfgDescTag,
1084
                              "GIMP");
1085
  gimp_color_profile_set_tag (profile, cmsSigDeviceModelDescTag,
1086
                              "sRGB");
1087
  gimp_color_profile_set_tag (profile, cmsSigCopyrightTag,
1088
                              "Public Domain");
1089

1090
  /* The following line produces a V2 profile with a point curve TRC.
1091 1092 1093 1094 1095 1096 1097
   * Profiles with point curve TRCs can't be used in LCMS2 unbounded
   * mode ICC profile conversions. A V2 profile might be appropriate
   * for embedding in sRGB images saved to disk, if the image is to be
   * opened by an image editing application that doesn't understand V4
   * profiles.
   *
   * cmsSetProfileVersion (srgb_profile, 2.1);
1098
   */
1099

1100
  return profile;
1101
}
1102 1103

/**
1104
 * gimp_color_profile_new_rgb_srgb:
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
 *
 * This function is a replacement for cmsCreate_sRGBProfile() and
 * returns an sRGB profile that is functionally the same as the
 * ArgyllCMS sRGB.icm profile. "Functionally the same" means it has
 * the same red, green, and blue colorants and the V4 "chad"
 * equivalent of the ArgyllCMS V2 white point. The profile TRC is also
 * functionally equivalent to the ArgyllCMS sRGB.icm TRC and is the
 * same as the LCMS sRGB built-in profile TRC.
 *
 * The actual primaries in the sRGB specification are
 * red xy:   {0.6400, 0.3300, 1.0}
 * green xy: {0.3000, 0.6000, 1.0}
 * blue xy:  {0.1500, 0.0600, 1.0}
 *
 * The sRGB primaries given below are "pre-quantized" to compensate
 * for hexadecimal quantization during the profile-making process.
 * Unless the profile-making code compensates for this quantization,
 * the resulting profile's red, green, and blue colorants will deviate
 * slightly from the correct XYZ values.
 *
 * LCMS2 doesn't compensate for hexadecimal quantization. The
 * "pre-quantized" primaries below were back-calculated from the
 * ArgyllCMS sRGB.icm profile. The resulting sRGB profile's colorants
 * exactly matches the ArgyllCMS sRGB.icm profile colorants.
 *
1130
 * Returns: the sRGB #GimpColorProfile.
1131
 *
1132
 * Since: 2.10
1133
 **/
1134
GimpColorProfile *
1135
gimp_color_profile_new_rgb_srgb (void)
1136
{
1137
  static GimpColorProfile *profile = NULL;
1138

1139 1140
  const guint8 *data;
  gsize         length;
1141

1142 1143
  if (G_UNLIKELY (profile == NULL))
    {
1144
      cmsHPROFILE lcms_profile = gimp_color_profile_new_rgb_srgb_internal ();
1145

1146
      profile = gimp_color_profile_new_from_lcms_profile (lcms_profile, NULL);
1147

1148
      cmsCloseProfile (lcms_profile);
1149 1150
    }

1151 1152 1153
  data = gimp_color_profile_get_icc_profile (profile, &length);

  return gimp_color_profile_new_from_icc_profile (data, length, NULL);
1154
}
1155

1156
static cmsHPROFILE
1157
gimp_color_profile_new_rgb_srgb_linear_internal (void)
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
{
  cmsHPROFILE profile;

  /* white point is D65 from the sRGB specs */
  cmsCIExyY whitepoint = { 0.3127, 0.3290, 1.0 };

  /* primaries are ITU‐R BT.709‐5 (xYY), which are also the primaries
   * from the sRGB specs, modified to properly account for hexadecimal
   * quantization during the profile making process.
   */
  cmsCIExyYTRIPLE primaries =
    {
      /* R { 0.6400, 0.3300, 1.0 }, */
      /* G { 0.3000, 0.6000, 1.0 }, */
      /* B { 0.1500, 0.0600, 1.0 }  */
      /* R */ { 0.639998686, 0.330010138, 1.0 },
      /* G */ { 0.300003784, 0.600003357, 1.0 },
      /* B */ { 0.150002046, 0.059997204, 1.0 }
    };

1178 1179
  cmsToneCurve *curve[3];

1180
  /* linear light */
1181
  curve[0] = curve[1] = curve[2] = cmsBuildGamma (NULL, 1.0);
1182

1183
  profile = cmsCreateRGBProfile (&whitepoint, &primaries, curve);
1184

1185
  cmsFreeToneCurve (curve[0]);
1186

1187
  gimp_color_profile_set_tag (profile, cmsSigProfileDescriptionTag,
1188
                              "GIMP built-in Linear sRGB");
1189 1190 1191
  gimp_color_profile_set_tag (profile, cmsSigDeviceMfgDescTag,
                              "GIMP");
  gimp_color_profile_set_tag (profile, cmsSigDeviceModelDescTag,
1192
                              "Linear sRGB");
1193 1194
  gimp_color_profile_set_tag (profile, cmsSigCopyrightTag,
                              "Public Domain");
1195 1196 1197 1198 1199

  return profile;
}

/**
1200
 * gimp_color_profile_new_rgb_srgb_linear:
1201 1202
 *
 * This function creates a profile for babl_model("RGB"). Please
1203
 * somebody write something smarter here.
1204
 *
1205
 * Returns: the linear RGB #GimpColorProfile.
1206
 *
1207
 * Since: 2.10
1208
 **/
1209
GimpColorProfile *
1210
gimp_color_profile_new_rgb_srgb_linear (void)
1211
{
1212
  static GimpColorProfile *profile = NULL;
1213

1214 1215
  const guint8 *data;
  gsize         length;
1216

1217 1218
  if (G_UNLIKELY (profile == NULL))
    {
1219
      cmsHPROFILE lcms_profile = gimp_color_profile_new_rgb_srgb_linear_internal ();
1220

1221
      profile = gimp_color_profile_new_from_lcms_profile (lcms_profile, NULL);
1222

1223
      cmsCloseProfile (lcms_profile);
1224 1225
    }

1226 1227 1228
  data = gimp_color_profile_get_icc_profile (profile, &length);

  return gimp_color_profile_new_from_icc_profile (data, length, NULL);
1229 1230
}

1231
static cmsHPROFILE *
1232
gimp_color_profile_new_rgb_adobe_internal (void)
1233 1234
{
  cmsHPROFILE profile;
1235 1236 1237

  /* white point is D65 from the sRGB specs */
  cmsCIExyY whitepoint = { 0.3127, 0.3290, 1.0 };
1238 1239 1240 1241 1242 1243 1244

  /* AdobeRGB1998 and sRGB have the same white point.
   *
   * The primaries below are technically correct, but because of
   * hexadecimal rounding these primaries don't make a profile that
   * matches the original.
   *
1245
   *  cmsCIExyYTRIPLE primaries = {
1246 1247 1248 1249 1250
   *    { 0.6400, 0.3300, 1.0 },
   *    { 0.2100, 0.7100, 1.0 },
   *    { 0.1500, 0.0600, 1.0 }
   *  };
   */
1251
  cmsCIExyYTRIPLE primaries =
1252
    {
1253 1254 1255
      { 0.639996511, 0.329996864, 1.0 },
      { 0.210005295, 0.710004866, 1.0 },
      { 0.149997606, 0.060003644, 1.0 }
1256 1257
    };

1258 1259 1260 1261
  cmsToneCurve *curve[3];

  /* gamma 2.2 */
  curve[0] = curve[1] = curve[2] = cmsBuildGamma (NULL, 2.19921875);
1262

1263
  profile = cmsCreateRGBProfile (&whitepoint, &primaries, curve);
1264

1265
  cmsFreeToneCurve (curve[0]);
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279

  gimp_color_profile_set_tag (profile, cmsSigProfileDescriptionTag,
                              "Compatible with Adobe RGB (1998)");
  gimp_color_profile_set_tag (profile, cmsSigDeviceMfgDescTag,
                              "GIMP");
  gimp_color_profile_set_tag (profile, cmsSigDeviceModelDescTag,
                              "Compatible with Adobe RGB (1998)");
  gimp_color_profile_set_tag (profile, cmsSigCopyrightTag,
                              "Public Domain");

  return profile;
}

/**
1280
 * gimp_color_profile_new_rgb_adobe:
1281 1282 1283
 *
 * This function creates a profile compatible with AbobeRGB (1998).
 *
1284
 * Returns: the AdobeRGB-compatible #GimpColorProfile.
1285 1286 1287 1288
 *
 * Since: 2.10
 **/
GimpColorProfile *
1289
gimp_color_profile_new_rgb_adobe (void)
1290 1291 1292 1293 1294 1295 1296 1297
{
  static GimpColorProfile *profile = NULL;

  const guint8 *data;
  gsize         length;

  if (G_UNLIKELY (profile == NULL))
    {
1298
      cmsHPROFILE lcms_profile = gimp_color_profile_new_rgb_adobe_internal ();
1299 1300

      profile = gimp_color_profile_new_from_lcms_profile (lcms_profile, NULL);
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310

      cmsCloseProfile (lcms_profile);
    }

  data = gimp_color_profile_get_icc_profile (profile, &length);

  return gimp_color_profile_new_from_icc_profile (data, length, NULL);
}

static cmsHPROFILE *
1311
gimp_color_profile_new_d65_gray_srgb_trc_internal (void)
1312 1313 1314
{
  cmsHPROFILE profile;

1315 1316
  /* white point is D65 from the sRGB specs */
  cmsCIExyY whitepoint = { 0.3127, 0.3290, 1.0 };
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328

  cmsFloat64Number srgb_parameters[5] =
    { 2.4, 1.0 / 1.055,  0.055 / 1.055, 1.0 / 12.92, 0.04045 };

  cmsToneCurve *curve = cmsBuildParametricToneCurve (NULL, 4,
                                                     srgb_parameters);

  profile = cmsCreateGrayProfile (&whitepoint, curve);

  cmsFreeToneCurve (curve);

  gimp_color_profile_set_tag (profile, cmsSigProfileDescriptionTag,
1329
                              "GIMP built-in D65 Grayscale with sRGB TRC");
1330 1331 1332
  gimp_color_profile_set_tag (profile, cmsSigDeviceMfgDescTag,
                              "GIMP");
  gimp_color_profile_set_tag (profile, cmsSigDeviceModelDescTag,
1333
                              "D65 Grayscale with sRGB TRC");
1334 1335 1336 1337 1338 1339 1340
  gimp_color_profile_set_tag (profile, cmsSigCopyrightTag,
                              "Public Domain");

  return profile;
}

/**
1341
 * gimp_color_profile_new_d65_gray_srgb_trc
1342 1343
 *
 * This function creates a grayscale #GimpColorProfile with an
1344
 * sRGB TRC. See gimp_color_profile_new_rgb_srgb().
1345
 *
1346
 * Returns: the sRGB-gamma grayscale #GimpColorProfile.
1347 1348 1349 1350
 *
 * Since: 2.10
 **/
GimpColorProfile *
1351
gimp_color_profile_new_d65_gray_srgb_trc (void)
1352 1353 1354 1355 1356 1357 1358 1359
{
  static GimpColorProfile *profile = NULL;

  const guint8 *data;
  gsize         length;

  if (G_UNLIKELY (profile == NULL))
    {
1360
      cmsHPROFILE lcms_profile = gimp_color_profile_new_d65_gray_srgb_trc_internal ();
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372

      profile = gimp_color_profile_new_from_lcms_profile (lcms_profile, NULL);

      cmsCloseProfile (lcms_profile);
    }

  data = gimp_color_profile_get_icc_profile (profile, &length);

  return gimp_color_profile_new_from_icc_profile (data, length, NULL);
}

static cmsHPROFILE
1373
gimp_color_profile_new_d65_gray_linear_internal (void)
1374 1375 1376
{
  cmsHPROFILE profile;

1377 1378
  /* white point is D65 from the sRGB specs */
  cmsCIExyY whitepoint = { 0.3127, 0.3290, 1.0 };
1379 1380 1381 1382 1383 1384 1385 1386

  cmsToneCurve *curve = cmsBuildGamma (NULL, 1.0);

  profile = cmsCreateGrayProfile (&whitepoint, curve);

  cmsFreeToneCurve (curve);

  gimp_color_profile_set_tag (profile, cmsSigProfileDescriptionTag,
1387