~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to modules/colorsel_cmyk_lcms.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GIMP CMYK ColorSelector using littleCMS
 
2
 * Copyright (C) 2006  Sven Neumann <sven@gimp.org>
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <glib.h>  /* lcms.h uses the "inline" keyword */
 
22
 
 
23
#ifdef HAVE_LCMS_LCMS_H
 
24
#include <lcms/lcms.h>
 
25
#else
 
26
#include <lcms.h>
 
27
#endif
 
28
 
 
29
#include <gtk/gtk.h>
 
30
 
 
31
#include "libgimpcolor/gimpcolor.h"
 
32
#include "libgimpconfig/gimpconfig.h"
 
33
#include "libgimpmodule/gimpmodule.h"
 
34
#include "libgimpwidgets/gimpwidgets.h"
 
35
 
 
36
#include "libgimp/libgimp-intl.h"
 
37
 
 
38
 
 
39
/* definitions and variables */
 
40
 
 
41
#define COLORSEL_TYPE_CMYK            (colorsel_cmyk_type)
 
42
#define COLORSEL_CMYK(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), COLORSEL_TYPE_CMYK, ColorselCmyk))
 
43
#define COLORSEL_CMYK_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), COLORSEL_TYPE_CMYK, ColorselCmykClass))
 
44
#define COLORSEL_IS_CMYK(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), COLORSEL_TYPE_CMYK))
 
45
#define COLORSEL_IS_CMYK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), COLORSEL_TYPE_CMYK))
 
46
 
 
47
 
 
48
typedef struct _ColorselCmyk      ColorselCmyk;
 
49
typedef struct _ColorselCmykClass ColorselCmykClass;
 
50
 
 
51
struct _ColorselCmyk
 
52
{
 
53
  GimpColorSelector  parent_instance;
 
54
 
 
55
  GimpColorConfig   *config;
 
56
  cmsHTRANSFORM      rgb2cmyk;
 
57
  cmsHTRANSFORM      cmyk2rgb;
 
58
 
 
59
  GimpCMYK           cmyk;
 
60
  GtkAdjustment     *adj[4];
 
61
  GtkWidget         *name_label;
 
62
 
 
63
  gboolean           in_destruction;
 
64
};
 
65
 
 
66
struct _ColorselCmykClass
 
67
{
 
68
  GimpColorSelectorClass  parent_class;
 
69
};
 
70
 
 
71
 
 
72
static GType  colorsel_cmyk_get_type       (GTypeModule       *module);
 
73
static void   colorsel_cmyk_class_init     (ColorselCmykClass *klass);
 
74
static void   colorsel_cmyk_init           (ColorselCmyk      *cmyk);
 
75
static void   colorsel_cmyk_dispose        (GObject           *object);
 
76
 
 
77
static void   colorsel_cmyk_set_color      (GimpColorSelector *selector,
 
78
                                            const GimpRGB     *rgb,
 
79
                                            const GimpHSV     *hsv);
 
80
static void   colorsel_cmyk_set_config     (GimpColorSelector *selector,
 
81
                                            GimpColorConfig   *config);
 
82
 
 
83
static void   colorsel_cmyk_adj_update     (GtkAdjustment     *adj,
 
84
                                            ColorselCmyk      *module);
 
85
static void   colorsel_cmyk_config_changed (ColorselCmyk      *module);
 
86
 
 
87
 
 
88
static const GimpModuleInfo colorsel_cmyk_info =
 
89
{
 
90
  GIMP_MODULE_ABI_VERSION,
 
91
  N_("CMYK color selector (using color profile)"),
 
92
  "Sven Neumann <sven@gimp.org>",
 
93
  "v0.1",
 
94
  "(c) 2006, released under the GPL",
 
95
  "September 2006"
 
96
};
 
97
 
 
98
static GType         colorsel_cmyk_type = 0;
 
99
static GObjectClass *parent_class       = NULL;
 
100
 
 
101
 
 
102
G_MODULE_EXPORT const GimpModuleInfo *
 
103
gimp_module_query (GTypeModule *module)
 
104
{
 
105
  return &colorsel_cmyk_info;
 
106
}
 
107
 
 
108
G_MODULE_EXPORT gboolean
 
109
gimp_module_register (GTypeModule *module)
 
110
{
 
111
  colorsel_cmyk_get_type (module);
 
112
 
 
113
  return TRUE;
 
114
}
 
115
 
 
116
static GType
 
117
colorsel_cmyk_get_type (GTypeModule *module)
 
118
{
 
119
  if (! colorsel_cmyk_type)
 
120
    {
 
121
      const GTypeInfo select_info =
 
122
      {
 
123
        sizeof (ColorselCmykClass),
 
124
        (GBaseInitFunc) NULL,
 
125
        (GBaseFinalizeFunc) NULL,
 
126
        (GClassInitFunc) colorsel_cmyk_class_init,
 
127
        NULL,           /* class_finalize */
 
128
        NULL,           /* class_data     */
 
129
        sizeof (ColorselCmyk),
 
130
        0,              /* n_preallocs    */
 
131
        (GInstanceInitFunc) colorsel_cmyk_init,
 
132
      };
 
133
 
 
134
      colorsel_cmyk_type =
 
135
        g_type_module_register_type (module,
 
136
                                     GIMP_TYPE_COLOR_SELECTOR,
 
137
                                     "ColorselCmyk",
 
138
                                     &select_info, 0);
 
139
    }
 
140
 
 
141
  return colorsel_cmyk_type;
 
142
}
 
143
 
 
144
static void
 
145
colorsel_cmyk_class_init (ColorselCmykClass *klass)
 
146
{
 
147
  GObjectClass           *object_class   = G_OBJECT_CLASS (klass);
 
148
  GimpColorSelectorClass *selector_class = GIMP_COLOR_SELECTOR_CLASS (klass);
 
149
 
 
150
  parent_class = g_type_class_peek_parent (klass);
 
151
 
 
152
  object_class->dispose      = colorsel_cmyk_dispose;
 
153
 
 
154
  selector_class->name       = _("CMYK");
 
155
  selector_class->help_id    = "gimp-colorselector-cmyk";
 
156
  selector_class->stock_id   = GTK_STOCK_PRINT;  /* FIXME */
 
157
  selector_class->set_color  = colorsel_cmyk_set_color;
 
158
  selector_class->set_config = colorsel_cmyk_set_config;
 
159
}
 
160
 
 
161
static void
 
162
colorsel_cmyk_init (ColorselCmyk *module)
 
163
{
 
164
  GtkWidget *table;
 
165
  GtkObject *adj;
 
166
  gint       i;
 
167
 
 
168
  static const gchar * const cmyk_labels[] =
 
169
  {
 
170
    /* Cyan        */
 
171
    N_("_C"),
 
172
    /* Magenta     */
 
173
    N_("_M"),
 
174
    /* Yellow      */
 
175
    N_("_Y"),
 
176
    /* Key (Black) */
 
177
    N_("_K")
 
178
  };
 
179
  static const gchar * const cmyk_tips[] =
 
180
  {
 
181
    N_("Cyan"),
 
182
    N_("Magenta"),
 
183
    N_("Yellow"),
 
184
    N_("Black")
 
185
  };
 
186
 
 
187
  module->config   = NULL;
 
188
  module->rgb2cmyk = NULL;
 
189
  module->cmyk2rgb = NULL;
 
190
 
 
191
  gtk_box_set_spacing (GTK_BOX (module), 6);
 
192
 
 
193
  table = gtk_table_new (4, 4, FALSE);
 
194
 
 
195
  gtk_table_set_row_spacings (GTK_TABLE (table), 1);
 
196
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
 
197
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 0);
 
198
 
 
199
  gtk_box_pack_start (GTK_BOX (module), table, FALSE, FALSE, 0);
 
200
  gtk_widget_show (table);
 
201
 
 
202
  for (i = 0; i < 4; i++)
 
203
    {
 
204
      adj = gimp_scale_entry_new (GTK_TABLE (table), 1, i,
 
205
                                  gettext (cmyk_labels[i]),
 
206
                                  -1, -1,
 
207
                                  0.0,
 
208
                                  0.0, 100.0,
 
209
                                  1.0, 10.0,
 
210
                                  0,
 
211
                                  TRUE, 0.0, 0.0,
 
212
                                  gettext (cmyk_tips[i]),
 
213
                                  NULL);
 
214
 
 
215
      g_signal_connect (adj, "value-changed",
 
216
                        G_CALLBACK (colorsel_cmyk_adj_update),
 
217
                        module);
 
218
 
 
219
      module->adj[i] = GTK_ADJUSTMENT (adj);
 
220
    }
 
221
 
 
222
  module->name_label = gtk_label_new (NULL);
 
223
  gtk_misc_set_alignment (GTK_MISC (module->name_label), 0.0, 0.5);
 
224
  gimp_label_set_attributes (GTK_LABEL (module->name_label),
 
225
                             PANGO_ATTR_SCALE, PANGO_SCALE_SMALL,
 
226
                             PANGO_ATTR_STYLE, PANGO_STYLE_ITALIC,
 
227
                             -1);
 
228
  gtk_box_pack_start (GTK_BOX (module), module->name_label, FALSE, FALSE, 0);
 
229
  gtk_widget_show (module->name_label);
 
230
}
 
231
 
 
232
static void
 
233
colorsel_cmyk_dispose (GObject *object)
 
234
{
 
235
  ColorselCmyk *module = COLORSEL_CMYK (object);
 
236
 
 
237
  module->in_destruction = TRUE;
 
238
 
 
239
  colorsel_cmyk_set_config (GIMP_COLOR_SELECTOR (object), NULL);
 
240
 
 
241
  G_OBJECT_CLASS (parent_class)->dispose (object);
 
242
}
 
243
 
 
244
static void
 
245
colorsel_cmyk_set_color (GimpColorSelector *selector,
 
246
                         const GimpRGB     *rgb,
 
247
                         const GimpHSV     *hsv)
 
248
{
 
249
  ColorselCmyk *module = COLORSEL_CMYK (selector);
 
250
 
 
251
  if (module->rgb2cmyk)
 
252
    {
 
253
      gdouble rgb_values[3];
 
254
      gdouble cmyk_values[4];
 
255
 
 
256
      rgb_values[0] = rgb->r;
 
257
      rgb_values[1] = rgb->g;
 
258
      rgb_values[2] = rgb->b;
 
259
 
 
260
      cmsDoTransform (module->rgb2cmyk, rgb_values, cmyk_values, 1);
 
261
 
 
262
      module->cmyk.c = cmyk_values[0] / 100.0;
 
263
      module->cmyk.m = cmyk_values[1] / 100.0;
 
264
      module->cmyk.y = cmyk_values[2] / 100.0;
 
265
      module->cmyk.k = cmyk_values[3] / 100.0;
 
266
    }
 
267
  else
 
268
    {
 
269
      gimp_rgb_to_cmyk (rgb, 1.0, &module->cmyk);
 
270
    }
 
271
 
 
272
  gtk_adjustment_set_value (module->adj[0], module->cmyk.c * 100.0);
 
273
  gtk_adjustment_set_value (module->adj[1], module->cmyk.m * 100.0);
 
274
  gtk_adjustment_set_value (module->adj[2], module->cmyk.y * 100.0);
 
275
  gtk_adjustment_set_value (module->adj[3], module->cmyk.k * 100.0);
 
276
}
 
277
 
 
278
static void
 
279
colorsel_cmyk_set_config (GimpColorSelector *selector,
 
280
                          GimpColorConfig   *config)
 
281
{
 
282
  ColorselCmyk *module = COLORSEL_CMYK (selector);
 
283
 
 
284
  if (config == module->config)
 
285
    return;
 
286
 
 
287
  if (module->config)
 
288
    {
 
289
      g_signal_handlers_disconnect_by_func (module->config,
 
290
                                            G_CALLBACK (colorsel_cmyk_config_changed),
 
291
                                            module);
 
292
      g_object_unref (module->config);
 
293
    }
 
294
 
 
295
  module->config = config;
 
296
 
 
297
  if (module->config)
 
298
    {
 
299
      g_object_ref (module->config);
 
300
      g_signal_connect_swapped (module->config, "notify",
 
301
                                G_CALLBACK (colorsel_cmyk_config_changed),
 
302
                                module);
 
303
    }
 
304
 
 
305
  colorsel_cmyk_config_changed (module);
 
306
}
 
307
 
 
308
static void
 
309
colorsel_cmyk_adj_update (GtkAdjustment *adj,
 
310
                          ColorselCmyk  *module)
 
311
{
 
312
  GimpColorSelector *selector = GIMP_COLOR_SELECTOR (module);
 
313
  gint               i;
 
314
 
 
315
  for (i = 0; i < 4; i++)
 
316
    if (module->adj[i] == adj)
 
317
      break;
 
318
 
 
319
  switch (i)
 
320
    {
 
321
    case 0:
 
322
      module->cmyk.c = adj->value / 100.0;
 
323
      break;
 
324
    case 1:
 
325
      module->cmyk.m = adj->value / 100.0;
 
326
      break;
 
327
    case 2:
 
328
      module->cmyk.y = adj->value / 100.0;
 
329
      break;
 
330
    case 3:
 
331
      module->cmyk.k = adj->value / 100.0;
 
332
      break;
 
333
    default:
 
334
      return;
 
335
    }
 
336
 
 
337
  if (module->cmyk2rgb)
 
338
    {
 
339
      gdouble cmyk_values[4];
 
340
      gdouble rgb_values[3];
 
341
 
 
342
      cmyk_values[0] = module->cmyk.c * 100.0;
 
343
      cmyk_values[1] = module->cmyk.m * 100.0;
 
344
      cmyk_values[2] = module->cmyk.y * 100.0;
 
345
      cmyk_values[3] = module->cmyk.k * 100.0;
 
346
 
 
347
      cmsDoTransform (module->cmyk2rgb, cmyk_values, rgb_values, 1);
 
348
 
 
349
      selector->rgb.r = rgb_values[0];
 
350
      selector->rgb.g = rgb_values[1];
 
351
      selector->rgb.b = rgb_values[2];
 
352
    }
 
353
  else
 
354
    {
 
355
      gimp_cmyk_to_rgb (&module->cmyk, &selector->rgb);
 
356
    }
 
357
 
 
358
  gimp_rgb_to_hsv (&selector->rgb, &selector->hsv);
 
359
 
 
360
  gimp_color_selector_color_changed (selector);
 
361
}
 
362
 
 
363
static cmsHPROFILE
 
364
color_config_get_rgb_profile (GimpColorConfig *config)
 
365
{
 
366
  cmsHPROFILE  profile = NULL;
 
367
 
 
368
  if (config->rgb_profile)
 
369
    profile = cmsOpenProfileFromFile (config->rgb_profile, "r");
 
370
 
 
371
  return profile ? profile : cmsCreate_sRGBProfile ();
 
372
}
 
373
 
 
374
static void
 
375
colorsel_cmyk_config_changed (ColorselCmyk *module)
 
376
{
 
377
  GimpColorSelector *selector = GIMP_COLOR_SELECTOR (module);
 
378
  GimpColorConfig   *config   = module->config;
 
379
  cmsHPROFILE        rgb_profile;
 
380
  cmsHPROFILE        cmyk_profile;
 
381
  const gchar       *name;
 
382
  gchar             *text;
 
383
 
 
384
  if (module->rgb2cmyk)
 
385
    {
 
386
      cmsDeleteTransform (module->rgb2cmyk);
 
387
      module->rgb2cmyk = NULL;
 
388
    }
 
389
 
 
390
  if (module->cmyk2rgb)
 
391
    {
 
392
      cmsDeleteTransform (module->cmyk2rgb);
 
393
      module->cmyk2rgb = NULL;
 
394
    }
 
395
 
 
396
  gtk_label_set_text (GTK_LABEL (module->name_label), _("Profile: (none)"));
 
397
 
 
398
  if (! config || config->mode == GIMP_COLOR_MANAGEMENT_OFF)
 
399
    goto out;
 
400
 
 
401
  if (! config->cmyk_profile ||
 
402
      ! (cmyk_profile = cmsOpenProfileFromFile (config->cmyk_profile, "r")))
 
403
    goto out;
 
404
 
 
405
  name = cmsTakeProductName (cmyk_profile);
 
406
  if (! g_utf8_validate (name, -1, NULL))
 
407
    name = _("(invalid UTF-8 string)");
 
408
 
 
409
  text = g_strdup_printf (_("Profile: %s"), name);
 
410
  gtk_label_set_text (GTK_LABEL (module->name_label), text);
 
411
  g_free (text);
 
412
 
 
413
  rgb_profile = color_config_get_rgb_profile (config);
 
414
 
 
415
  module->rgb2cmyk = cmsCreateTransform (rgb_profile,  TYPE_RGB_DBL,
 
416
                                         cmyk_profile, TYPE_CMYK_DBL,
 
417
                                         INTENT_PERCEPTUAL,
 
418
                                          0);
 
419
  module->cmyk2rgb = cmsCreateTransform (cmyk_profile, TYPE_CMYK_DBL,
 
420
                                         rgb_profile,  TYPE_RGB_DBL,
 
421
                                         INTENT_PERCEPTUAL,
 
422
                                          0);
 
423
 
 
424
  cmsCloseProfile (rgb_profile);
 
425
  cmsCloseProfile (cmyk_profile);
 
426
 
 
427
 out:
 
428
  if (! module->in_destruction)
 
429
    gimp_color_selector_set_color (selector, &selector->rgb, &selector->hsv);
 
430
}