1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
#include <glib.h> /* lcms.h uses the "inline" keyword */
26
#define LCMS_WIN_TYPES_ALREADY_DEFINED
29
#ifdef HAVE_LCMS_LCMS_H
30
#include <lcms/lcms.h>
37
#ifdef GDK_WINDOWING_QUARTZ
38
#include <Carbon/Carbon.h>
39
#include <ApplicationServices/ApplicationServices.h>
40
#include <CoreServices/CoreServices.h>
43
#include "libgimpcolor/gimpcolor.h"
44
#include "libgimpconfig/gimpconfig.h"
45
#include "libgimpmath/gimpmath.h"
46
#include "libgimpmodule/gimpmodule.h"
47
#include "libgimpwidgets/gimpwidgets.h"
49
#include "libgimp/libgimp-intl.h"
52
#define CDISPLAY_TYPE_LCMS (cdisplay_lcms_type)
53
#define CDISPLAY_LCMS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CDISPLAY_TYPE_LCMS, CdisplayLcms))
54
#define CDISPLAY_LCMS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CDISPLAY_TYPE_LCMS, CdisplayLcmsClass))
55
#define CDISPLAY_IS_LCMS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CDISPLAY_TYPE_LCMS))
56
#define CDISPLAY_IS_LCMS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CDISPLAY_TYPE_LCMS))
59
typedef struct _CdisplayLcms CdisplayLcms;
60
typedef struct _CdisplayLcmsClass CdisplayLcmsClass;
64
GimpColorDisplay parent_instance;
66
cmsHTRANSFORM transform;
69
struct _CdisplayLcmsClass
71
GimpColorDisplayClass parent_instance;
75
static GType cdisplay_lcms_get_type (GTypeModule *module);
76
static void cdisplay_lcms_class_init (CdisplayLcmsClass *klass);
77
static void cdisplay_lcms_init (CdisplayLcms *lcms);
78
static void cdisplay_lcms_finalize (GObject *object);
80
static GtkWidget * cdisplay_lcms_configure (GimpColorDisplay *display);
81
static void cdisplay_lcms_convert (GimpColorDisplay *display,
87
static void cdisplay_lcms_changed (GimpColorDisplay *display);
89
static cmsHPROFILE cdisplay_lcms_get_rgb_profile (CdisplayLcms *lcms);
90
static cmsHPROFILE cdisplay_lcms_get_display_profile (CdisplayLcms *lcms);
91
static cmsHPROFILE cdisplay_lcms_get_printer_profile (CdisplayLcms *lcms);
93
static void cdisplay_lcms_attach_labelled (GtkTable *table,
98
static void cdisplay_lcms_label_set_text (GtkLabel *label,
100
const gchar *tooltip);
101
static void cdisplay_lcms_update_profile_label (CdisplayLcms *lcms,
103
static void cdisplay_lcms_notify_profile (GObject *config,
108
static const GimpModuleInfo cdisplay_lcms_info =
110
GIMP_MODULE_ABI_VERSION,
111
N_("Color management display filter using ICC color profiles"),
114
"(c) 2005 - 2007, released under the GPL",
118
static GType cdisplay_lcms_type = 0;
119
static GimpColorDisplayClass *parent_class = NULL;
122
G_MODULE_EXPORT const GimpModuleInfo *
123
gimp_module_query (GTypeModule *module)
125
return &cdisplay_lcms_info;
128
G_MODULE_EXPORT gboolean
129
gimp_module_register (GTypeModule *module)
131
cdisplay_lcms_get_type (module);
137
cdisplay_lcms_get_type (GTypeModule *module)
139
if (! cdisplay_lcms_type)
141
const GTypeInfo display_info =
143
sizeof (CdisplayLcmsClass),
144
(GBaseInitFunc) NULL,
145
(GBaseFinalizeFunc) NULL,
146
(GClassInitFunc) cdisplay_lcms_class_init,
147
NULL, /* class_finalize */
148
NULL, /* class_data */
149
sizeof (CdisplayLcms),
151
(GInstanceInitFunc) cdisplay_lcms_init,
155
g_type_module_register_type (module, GIMP_TYPE_COLOR_DISPLAY,
156
"CdisplayLcms", &display_info, 0);
159
return cdisplay_lcms_type;
163
cdisplay_lcms_class_init (CdisplayLcmsClass *klass)
165
GObjectClass *object_class = G_OBJECT_CLASS (klass);
166
GimpColorDisplayClass *display_class = GIMP_COLOR_DISPLAY_CLASS (klass);
168
parent_class = g_type_class_peek_parent (klass);
170
object_class->finalize = cdisplay_lcms_finalize;
172
display_class->name = _("Color Management");
173
display_class->help_id = "gimp-colordisplay-lcms";
174
display_class->stock_id = GIMP_STOCK_DISPLAY_FILTER_LCMS;
176
display_class->configure = cdisplay_lcms_configure;
177
display_class->convert = cdisplay_lcms_convert;
178
display_class->changed = cdisplay_lcms_changed;
180
cmsErrorAction (LCMS_ERROR_IGNORE);
184
cdisplay_lcms_init (CdisplayLcms *lcms)
186
lcms->transform = NULL;
190
cdisplay_lcms_finalize (GObject *object)
192
CdisplayLcms *lcms = CDISPLAY_LCMS (object);
196
cmsDeleteTransform (lcms->transform);
197
lcms->transform = NULL;
200
G_OBJECT_CLASS (parent_class)->finalize (object);
204
cdisplay_lcms_profile_get_info (cmsHPROFILE profile,
210
*name = cmsTakeProductDesc (profile);
213
*name = cmsTakeProductName (profile);
215
if (*name && ! g_utf8_validate (*name, -1, NULL))
216
*name = _("(invalid UTF-8 string)");
218
*info = cmsTakeProductInfo (profile);
219
if (*name && ! g_utf8_validate (*info, -1, NULL))
230
cdisplay_lcms_configure (GimpColorDisplay *display)
232
CdisplayLcms *lcms = CDISPLAY_LCMS (display);
233
GObject *config = G_OBJECT (gimp_color_display_get_config (display));
243
vbox = gtk_vbox_new (FALSE, 12);
245
hint = gimp_hint_box_new (_("This filter takes its configuration "
246
"from the Color Management section "
247
"in the Preferences dialog."));
248
gtk_box_pack_start (GTK_BOX (vbox), hint, FALSE, FALSE, 0);
249
gtk_widget_show (hint);
251
table = gtk_table_new (5, 2, FALSE);
252
gtk_table_set_row_spacings (GTK_TABLE (table), 6);
253
gtk_table_set_row_spacing (GTK_TABLE (table), 0, 12);
254
gtk_table_set_col_spacings (GTK_TABLE (table), 6);
255
gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
256
gtk_widget_show (table);
258
cdisplay_lcms_attach_labelled (GTK_TABLE (table), row++,
259
_("Mode of operation:"),
260
gimp_prop_enum_label_new (config, "mode"),
263
label = gtk_label_new (NULL);
264
gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
265
g_object_set_data (G_OBJECT (lcms), "rgb-profile", label);
266
cdisplay_lcms_attach_labelled (GTK_TABLE (table), row++,
269
cdisplay_lcms_update_profile_label (lcms, "rgb-profile");
271
label = gtk_label_new (NULL);
272
gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
273
g_object_set_data (G_OBJECT (lcms), "display-profile", label);
274
cdisplay_lcms_attach_labelled (GTK_TABLE (table), row++,
275
_("Monitor profile:"),
277
cdisplay_lcms_update_profile_label (lcms, "display-profile");
279
label = gtk_label_new (NULL);
280
gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
281
g_object_set_data (G_OBJECT (lcms), "printer-profile", label);
282
cdisplay_lcms_attach_labelled (GTK_TABLE (table), row++,
283
_("Print simulation profile:"),
285
cdisplay_lcms_update_profile_label (lcms, "printer-profile");
287
g_signal_connect_object (config, "notify",
288
G_CALLBACK (cdisplay_lcms_notify_profile),
295
cdisplay_lcms_convert (GimpColorDisplay *display,
302
CdisplayLcms *lcms = CDISPLAY_LCMS (display);
308
if (! lcms->transform)
311
for (y = 0; y < height; y++, buf += bpl)
312
cmsDoTransform (lcms->transform, buf, buf, width);
316
cdisplay_lcms_changed (GimpColorDisplay *display)
318
CdisplayLcms *lcms = CDISPLAY_LCMS (display);
319
GimpColorConfig *config = gimp_color_display_get_config (display);
321
cmsHPROFILE src_profile = NULL;
322
cmsHPROFILE dest_profile = NULL;
323
cmsHPROFILE proof_profile = NULL;
328
cmsDeleteTransform (lcms->transform);
329
lcms->transform = NULL;
335
switch (config->mode)
337
case GIMP_COLOR_MANAGEMENT_OFF:
340
case GIMP_COLOR_MANAGEMENT_SOFTPROOF:
341
proof_profile = cdisplay_lcms_get_printer_profile (lcms);
344
case GIMP_COLOR_MANAGEMENT_DISPLAY:
345
src_profile = cdisplay_lcms_get_rgb_profile (lcms);
346
dest_profile = cdisplay_lcms_get_display_profile (lcms);
350
if (config->display_intent ==
351
GIMP_COLOR_RENDERING_INTENT_RELATIVE_COLORIMETRIC)
353
flags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
359
src_profile = cmsCreate_sRGBProfile ();
362
dest_profile = cmsCreate_sRGBProfile ();
364
flags |= cmsFLAGS_SOFTPROOFING;
366
if (config->simulation_gamut_check)
370
flags |= cmsFLAGS_GAMUTCHECK;
372
gimp_rgb_get_uchar (&config->out_of_gamut_color, &r, &g, &b);
374
cmsSetAlarmCodes (r, g, b);
377
lcms->transform = cmsCreateProofingTransform (src_profile, TYPE_RGB_8,
378
dest_profile, TYPE_RGB_8,
380
config->simulation_intent,
381
config->display_intent,
383
cmsCloseProfile (proof_profile);
385
else if (src_profile || dest_profile)
388
src_profile = cmsCreate_sRGBProfile ();
391
dest_profile = cmsCreate_sRGBProfile ();
393
lcms->transform = cmsCreateTransform (src_profile, TYPE_RGB_8,
394
dest_profile, TYPE_RGB_8,
395
config->display_intent,
400
cmsCloseProfile (dest_profile);
403
cmsCloseProfile (src_profile);
407
cdisplay_lcms_profile_is_rgb (cmsHPROFILE profile)
409
return (cmsGetColorSpace (profile) == icSigRgbData);
413
cdisplay_lcms_get_rgb_profile (CdisplayLcms *lcms)
415
GimpColorConfig *config;
416
GimpColorManaged *managed;
417
cmsHPROFILE profile = NULL;
419
managed = gimp_color_display_get_managed (GIMP_COLOR_DISPLAY (lcms));
424
const guint8 *data = gimp_color_managed_get_icc_profile (managed, &len);
427
profile = cmsOpenProfileFromMem ((gpointer) data, len);
430
! cdisplay_lcms_profile_is_rgb (profile))
432
cmsCloseProfile (profile);
439
config = gimp_color_display_get_config (GIMP_COLOR_DISPLAY (lcms));
441
if (config->rgb_profile)
442
profile = cmsOpenProfileFromFile (config->rgb_profile, "r");
449
cdisplay_lcms_get_screen (CdisplayLcms *lcms,
452
GimpColorManaged *managed;
455
managed = gimp_color_display_get_managed (GIMP_COLOR_DISPLAY (lcms));
457
if (GTK_IS_WIDGET (managed))
458
screen = gtk_widget_get_screen (GTK_WIDGET (managed));
460
screen = gdk_screen_get_default ();
462
g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
464
if (GTK_IS_WIDGET (managed) && GTK_WIDGET_DRAWABLE (managed))
466
GtkWidget *widget = GTK_WIDGET (managed);
468
*monitor = gdk_screen_get_monitor_at_window (screen, widget->window);
479
#ifdef GDK_WINDOWING_QUARTZ
489
openReadSpool = 1, /* start read data process */
490
openWriteSpool = 2, /* start write data process */
491
readSpool = 3, /* read specified number of bytes */
492
writeSpool = 4, /* write specified number of bytes */
493
closeSpool = 5 /* complete data transfer process */
497
lcms_cdisplay_lcms_flatten_profile (SInt32 command,
502
ProfileTransfer *transfer = refCon;
507
g_return_val_if_fail (transfer->data == NULL && transfer->len == 0, -1);
511
transfer->data = g_realloc (transfer->data, transfer->len + *size);
512
memcpy (transfer->data + transfer->len, data, *size);
513
transfer->len += *size;
523
#endif /* GDK_WINDOWING_QUARTZ */
527
cdisplay_lcms_get_display_profile (CdisplayLcms *lcms)
529
GimpColorConfig *config;
530
cmsHPROFILE profile = NULL;
532
config = gimp_color_display_get_config (GIMP_COLOR_DISPLAY (lcms));
534
#if defined GDK_WINDOWING_X11
535
if (config->display_profile_from_gdk)
538
GdkAtom type = GDK_NONE;
545
screen = cdisplay_lcms_get_screen (lcms, &monitor);
548
atom_name = g_strdup_printf ("_ICC_PROFILE_%d", monitor);
550
atom_name = g_strdup ("_ICC_PROFILE");
552
if (gdk_property_get (gdk_screen_get_root_window (screen),
553
gdk_atom_intern (atom_name, FALSE),
555
0, 64 * 1024 * 1024, FALSE,
556
&type, &format, &nitems, &data) && nitems > 0)
558
profile = cmsOpenProfileFromMem (data, nitems);
565
#elif defined GDK_WINDOWING_QUARTZ
566
if (config->display_profile_from_gdk)
568
CMProfileRef prof = NULL;
571
cdisplay_lcms_get_screen (lcms, &monitor);
573
CMGetProfileByAVID (monitor, &prof);
577
ProfileTransfer transfer = { NULL, 0 };
580
CMFlattenProfile (prof, 0,
581
lcms_cdisplay_lcms_flatten_profile, &transfer,
583
CMCloseProfile (prof);
587
profile = cmsOpenProfileFromMem (transfer.data, transfer.len);
588
g_free (transfer.data);
593
#elif defined G_OS_WIN32
594
if (config->display_profile_from_gdk)
596
HDC hdc = GetDC (NULL);
603
GetICMProfile (hdc, &len, NULL);
604
path = g_new (gchar, len);
606
if (GetICMProfile (hdc, &len, path))
607
profile = cmsOpenProfileFromFile (path, "r");
610
ReleaseDC (NULL, hdc);
615
if (! profile && config->display_profile)
616
profile = cmsOpenProfileFromFile (config->display_profile, "r");
622
cdisplay_lcms_get_printer_profile (CdisplayLcms *lcms)
624
GimpColorConfig *config;
626
config = gimp_color_display_get_config (GIMP_COLOR_DISPLAY (lcms));
628
if (config->printer_profile)
629
return cmsOpenProfileFromFile (config->printer_profile, "r");
635
cdisplay_lcms_attach_labelled (GtkTable *table,
642
GtkWidget *ebox = NULL;
644
label = g_object_new (GTK_TYPE_LABEL,
650
gimp_label_set_attributes (GTK_LABEL (label),
651
PANGO_ATTR_WEIGHT, PANGO_WEIGHT_BOLD,
653
gtk_table_attach (table, label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 0, 0);
654
gtk_widget_show (label);
658
ebox = gtk_event_box_new ();
659
gtk_table_attach (table, ebox, 1, 2, row, row + 1,
660
GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
661
gtk_widget_show (ebox);
663
g_object_set_data (G_OBJECT (label), "tooltip-widget", ebox);
666
if (GTK_IS_LABEL (widget))
667
gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
670
gtk_container_add (GTK_CONTAINER (ebox), widget);
672
gtk_table_attach (table, widget, 1, 2, row, row + 1,
673
GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
675
gtk_widget_show (widget);
679
cdisplay_lcms_label_set_text (GtkLabel *label,
681
const gchar *tooltip)
683
GtkWidget *tooltip_widget;
685
gtk_label_set_text (label, text);
687
tooltip_widget = g_object_get_data (G_OBJECT (label), "tooltip-widget");
690
gimp_help_set_help_data (tooltip_widget, tooltip, NULL);
694
cdisplay_lcms_update_profile_label (CdisplayLcms *lcms,
698
cmsHPROFILE profile = NULL;
700
const gchar *tooltip;
702
label = g_object_get_data (G_OBJECT (lcms), name);
707
if (strcmp (name, "rgb-profile") == 0)
709
profile = cdisplay_lcms_get_rgb_profile (lcms);
711
else if (g_str_has_prefix (name, "display-profile"))
713
profile = cdisplay_lcms_get_display_profile (lcms);
715
else if (strcmp (name, "printer-profile") == 0)
717
profile = cdisplay_lcms_get_printer_profile (lcms);
721
g_return_if_reached ();
724
cdisplay_lcms_profile_get_info (profile, &text, &tooltip);
725
cdisplay_lcms_label_set_text (GTK_LABEL (label), text, tooltip);
728
cmsCloseProfile (profile);
732
cdisplay_lcms_notify_profile (GObject *config,
736
cdisplay_lcms_update_profile_label (lcms, pspec->name);