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

« back to all changes in this revision

Viewing changes to plug-ins/common/channel_mixer.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:
27
27
#include "config.h"
28
28
 
29
29
#include <errno.h>
30
 
#include <stdio.h>
31
30
#include <string.h>
32
 
#include <sys/types.h>
33
 
#include <sys/stat.h>
34
 
 
35
 
#include <gtk/gtk.h>
36
 
 
37
 
#ifdef G_OS_WIN32
38
 
#include <libgimpbase/gimpwin32-io.h>
39
 
#endif
40
 
 
41
 
#include <libgimpmath/gimpmath.h>
 
31
 
 
32
#include <glib/gstdio.h>
 
33
 
42
34
#include <libgimp/gimp.h>
43
35
#include <libgimp/gimpui.h>
44
36
 
45
37
#include "libgimp/stdplugins-intl.h"
46
38
 
47
39
 
48
 
#define PLUG_IN_NAME        "plug_in_colors_channel_mixer"
49
 
#define PLUG_IN_VERSION     "Channel Mixer 0.8"
50
 
#define HELP_ID             "plug-in-colors-channel-mixer"
51
 
#define PROGRESS_UPDATE_NUM 20
52
 
#define CM_LINE_SIZE        1024
 
40
#define PLUG_IN_PROC    "plug-in-colors-channel-mixer"
 
41
#define PLUG_IN_BINARY  "channel_mixer"
 
42
#define CM_LINE_SIZE    1024
53
43
 
54
44
typedef enum
55
45
{
60
50
 
61
51
typedef struct
62
52
{
63
 
  gdouble red_gain;
64
 
  gdouble green_gain;
65
 
  gdouble blue_gain;
 
53
  gdouble        red_gain;
 
54
  gdouble        green_gain;
 
55
  gdouble        blue_gain;
66
56
} CmChannelType;
67
57
 
68
58
typedef struct
72
62
  CmChannelType  blue;
73
63
  CmChannelType  black;
74
64
 
75
 
  gboolean       monochrome_flag;
76
 
  gboolean       preserve_luminosity_flag;
 
65
  gboolean       monochrome;
 
66
  gboolean       preserve_luminosity;
77
67
 
78
68
  CmModeType     output_channel;
79
69
  gboolean       preview;
91
81
} CmParamsType;
92
82
 
93
83
 
94
 
static GtkWidget *preview;
95
 
 
96
84
static void     query (void);
97
85
static void     run   (const gchar      *name,
98
86
                       gint              nparams,
100
88
                       gint             *nreturn_vals,
101
89
                       GimpParam       **return_vals);
102
90
 
103
 
static void     channel_mixer (GimpDrawable *drawable);
104
 
static gboolean cm_dialog     (GimpDrawable *drawable);
105
 
 
106
 
static void cm_red_scale_callback           (GtkAdjustment    *adjustment,
107
 
                                             CmParamsType     *mix);
108
 
static void cm_green_scale_callback         (GtkAdjustment    *adjustment,
109
 
                                             CmParamsType     *mix);
110
 
static void cm_blue_scale_callback          (GtkAdjustment    *adjustment,
111
 
                                             CmParamsType     *mix);
112
 
static void cm_monochrome_callback          (GtkWidget        *widget,
113
 
                                             CmParamsType     *mix);
114
 
static void cm_preserve_luminosity_callback (GtkWidget        *widget,
115
 
                                             CmParamsType     *mix);
116
 
static void cm_load_file_callback           (GtkWidget        *widget,
117
 
                                             CmParamsType     *mix);
118
 
static void cm_load_file_response_callback  (GtkWidget        *dialog,
119
 
                                             gint              response_id,
120
 
                                             CmParamsType     *mix);
121
 
static void cm_save_file_callback           (GtkWidget        *widget,
122
 
                                             CmParamsType     *mix);
123
 
static void cm_save_file_response_callback  (GtkWidget        *dialog,
124
 
                                             gint              response_id,
125
 
                                             CmParamsType     *mix);
126
 
static void cm_combo_callback               (GtkWidget        *widget,
127
 
                                             CmParamsType     *mix);
128
 
 
129
 
static gboolean cm_force_overwrite (const gchar *filename,
130
 
                                    GtkWidget   *parent);
131
 
 
132
 
static gdouble cm_calculate_norm (CmParamsType  *mix,
133
 
                                  CmChannelType *ch);
134
 
 
135
 
static inline guchar cm_mix_pixel (CmChannelType *ch,
136
 
                                   guchar         r,
137
 
                                   guchar         g,
138
 
                                   guchar         b,
139
 
                                   gdouble        norm);
140
 
 
141
 
static void cm_preview       (CmParamsType *mix,
142
 
                              GimpPreview  *preview);
143
 
static void cm_set_adjusters (CmParamsType *mix);
144
 
 
145
 
static void cm_save_file (CmParamsType *mix,
146
 
                          FILE         *fp);
147
 
 
148
 
 
149
 
GimpPlugInInfo PLUG_IN_INFO =
 
91
static void     cm_set_defaults                 (CmParamsType     *mix);
 
92
 
 
93
static void     channel_mixer                   (CmParamsType     *mix,
 
94
                                                 GimpDrawable     *drawable);
 
95
static gboolean cm_dialog                       (CmParamsType     *mix,
 
96
                                                 GimpDrawable     *drawable);
 
97
 
 
98
static void     cm_red_scale_callback           (GtkAdjustment    *adjustment,
 
99
                                                 CmParamsType     *mix);
 
100
static void     cm_green_scale_callback         (GtkAdjustment    *adjustment,
 
101
                                                 CmParamsType     *mix);
 
102
static void     cm_blue_scale_callback          (GtkAdjustment    *adjustment,
 
103
                                                 CmParamsType     *mix);
 
104
static void     cm_monochrome_callback          (GtkWidget        *widget,
 
105
                                                 CmParamsType     *mix);
 
106
static void     cm_preserve_luminosity_callback (GtkWidget        *widget,
 
107
                                                 CmParamsType     *mix);
 
108
static void     cm_load_file_callback           (GtkWidget        *widget,
 
109
                                                 CmParamsType     *mix);
 
110
static void     cm_load_file_response_callback  (GtkWidget        *dialog,
 
111
                                                 gint              response_id,
 
112
                                                 CmParamsType     *mix);
 
113
static void     cm_save_file_callback           (GtkWidget        *widget,
 
114
                                                 CmParamsType     *mix);
 
115
static void     cm_save_file_response_callback  (GtkWidget        *dialog,
 
116
                                                 gint              response_id,
 
117
                                                 CmParamsType     *mix);
 
118
static void     cm_reset_callback               (GtkWidget        *widget,
 
119
                                                 CmParamsType     *mix);
 
120
static void     cm_combo_callback               (GtkWidget        *widget,
 
121
                                                 CmParamsType     *mix);
 
122
 
 
123
static gdouble  cm_calculate_norm               (CmParamsType     *mix,
 
124
                                                 CmChannelType    *ch);
 
125
 
 
126
static inline guchar cm_mix_pixel               (CmChannelType    *ch,
 
127
                                                 guchar            r,
 
128
                                                 guchar            g,
 
129
                                                 guchar            b,
 
130
                                                 gdouble           norm);
 
131
 
 
132
static void     cm_preview                      (CmParamsType      *mix,
 
133
                                                 GimpPreview       *preview);
 
134
static void     cm_set_adjusters                (CmParamsType      *mix);
 
135
static void     cm_update_ui                    (CmParamsType      *mix);
 
136
 
 
137
static void     cm_save_file                    (CmParamsType      *mix,
 
138
                                                 FILE              *fp);
 
139
 
 
140
 
 
141
const GimpPlugInInfo PLUG_IN_INFO =
150
142
{
151
143
  NULL,  /* init_proc  */
152
144
  NULL,  /* quit_proc  */
154
146
  run    /* run_proc   */
155
147
};
156
148
 
157
 
static CmParamsType mix =
158
 
{
159
 
  { 1.0, 0.0, 0.0 },
160
 
  { 0.0, 1.0, 0.0 },
161
 
  { 0.0, 0.0, 1.0 },
162
 
  { 1.0, 0.0, 0.0 },
163
 
  FALSE,
164
 
  FALSE,
165
 
  CM_RED_CHANNEL,
166
 
  TRUE
167
 
};
 
149
static GtkWidget    *preview;
 
150
 
168
151
 
169
152
MAIN ()
170
153
 
171
154
static void
172
155
query (void)
173
156
{
174
 
  static GimpParamDef args[] =
 
157
  static const GimpParamDef args[] =
175
158
  {
176
 
    { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
177
 
    { GIMP_PDB_IMAGE, "image", "Input image (unused)" },
178
 
    { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
179
 
    { GIMP_PDB_INT32, "monochrome", "Monochrome (TRUE or FALSE)" },
180
 
    { GIMP_PDB_FLOAT, "rr_gain", "Set the red gain for the red channel" },
181
 
    { GIMP_PDB_FLOAT, "rg_gain", "Set the green gain for the red channel" },
182
 
    { GIMP_PDB_FLOAT, "rb_gain", "Set the blue gain for the red channel" },
183
 
    { GIMP_PDB_FLOAT, "gr_gain", "Set the red gain for the green channel" },
184
 
    { GIMP_PDB_FLOAT, "gg_gain", "Set the green gain for the green channel" },
185
 
    { GIMP_PDB_FLOAT, "gb_gain", "Set the blue gain for the green channel" },
186
 
    { GIMP_PDB_FLOAT, "br_gain", "Set the red gain for the blue channel" },
187
 
    { GIMP_PDB_FLOAT, "bg_gain", "Set the green gain for the blue channel" },
188
 
    { GIMP_PDB_FLOAT, "bb_gain", "Set the blue gain for the blue channel" }
 
159
    { GIMP_PDB_INT32,    "run-mode",   "Interactive, non-interactive" },
 
160
    { GIMP_PDB_IMAGE,    "image",      "Input image (unused)" },
 
161
    { GIMP_PDB_DRAWABLE, "drawable",   "Input drawable" },
 
162
    { GIMP_PDB_INT32,    "monochrome", "Monochrome (TRUE or FALSE)" },
 
163
    { GIMP_PDB_FLOAT,    "rr-gain",    "Set the red gain for the red channel" },
 
164
    { GIMP_PDB_FLOAT,    "rg-gain",    "Set the green gain for the red channel" },
 
165
    { GIMP_PDB_FLOAT,    "rb-gain",    "Set the blue gain for the red channel" },
 
166
    { GIMP_PDB_FLOAT,    "gr-gain",    "Set the red gain for the green channel" },
 
167
    { GIMP_PDB_FLOAT,    "gg-gain",    "Set the green gain for the green channel" },
 
168
    { GIMP_PDB_FLOAT,    "gb-gain",    "Set the blue gain for the green channel" },
 
169
    { GIMP_PDB_FLOAT,    "br-gain",    "Set the red gain for the blue channel" },
 
170
    { GIMP_PDB_FLOAT,    "bg-gain",    "Set the green gain for the blue channel" },
 
171
    { GIMP_PDB_FLOAT,    "bb-gain",    "Set the blue gain for the blue channel" }
189
172
  };
190
173
 
191
 
  gimp_install_procedure (PLUG_IN_NAME,
192
 
                          "Mix RGB Channels.",
 
174
  gimp_install_procedure (PLUG_IN_PROC,
 
175
                          N_("Alter colors by mixing RGB Channels"),
193
176
                          "This plug-in mixes the RGB channels.",
194
177
                          "Martin Guldahl <mguldahl@xmission.com>",
195
178
                          "Martin Guldahl <mguldahl@xmission.com>",
200
183
                          G_N_ELEMENTS (args), 0,
201
184
                          args, NULL);
202
185
 
203
 
  gimp_plugin_menu_register (PLUG_IN_NAME, "<Image>/Filters/Colors");
 
186
  gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Colors/Components");
204
187
}
205
188
 
206
 
/*----------------------------------------------------------------------
207
 
 *  run() - main routine
208
 
 *
209
 
 *  This handles the main interaction with the GIMP itself,
210
 
 *  and invokes the routine that actually does the work.
211
 
 *--------------------------------------------------------------------*/
212
189
static void
213
190
run (const gchar      *name,
214
191
     gint              nparams,
219
196
  static GimpParam   values[1];
220
197
  GimpDrawable      *drawable;
221
198
  GimpRunMode        run_mode;
 
199
  CmParamsType       mix;
222
200
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
223
201
 
224
202
  run_mode = param[0].data.d_int32;
235
213
 
236
214
  if (gimp_drawable_is_rgb (drawable->drawable_id))
237
215
    {
 
216
      cm_set_defaults (&mix);
 
217
      mix.preview = TRUE;
 
218
 
238
219
      switch (run_mode)
239
220
        {
240
221
        case GIMP_RUN_INTERACTIVE:
241
 
          gimp_get_data (PLUG_IN_NAME, &mix);
 
222
          gimp_get_data (PLUG_IN_PROC, &mix);
242
223
 
243
 
          if (! cm_dialog (drawable))
 
224
          if (! cm_dialog (&mix, drawable))
244
225
            {
245
226
              gimp_drawable_detach (drawable);
246
227
              return;
249
230
          break;
250
231
 
251
232
        case GIMP_RUN_NONINTERACTIVE:
252
 
          mix.monochrome_flag = param[3].data.d_int32;
 
233
          mix.monochrome = param[3].data.d_int32;
253
234
 
254
 
          if (mix.monochrome_flag == 1)
 
235
          if (mix.monochrome)
255
236
            {
256
237
              mix.black.red_gain   = param[4].data.d_float;
257
238
              mix.black.green_gain = param[5].data.d_float;
262
243
              mix.red.red_gain     = param[4].data.d_float;
263
244
              mix.red.green_gain   = param[5].data.d_float;
264
245
              mix.red.blue_gain    = param[6].data.d_float;
 
246
 
265
247
              mix.green.red_gain   = param[7].data.d_float;
266
248
              mix.green.green_gain = param[8].data.d_float;
267
249
              mix.green.blue_gain  = param[9].data.d_float;
 
250
 
268
251
              mix.blue.red_gain    = param[10].data.d_float;
269
252
              mix.blue.green_gain  = param[11].data.d_float;
270
253
              mix.blue.blue_gain   = param[12].data.d_float;
272
255
          break;
273
256
 
274
257
        case GIMP_RUN_WITH_LAST_VALS:
275
 
          gimp_get_data (PLUG_IN_NAME, &mix);
 
258
          gimp_get_data (PLUG_IN_PROC, &mix);
276
259
          break;
277
260
 
278
261
        default:
285
268
                 param[3].data.d_int32, mix.black.red_gain,
286
269
                 mix.black.green_gain, mix.black.blue_gain); */
287
270
 
288
 
          gimp_progress_init (_(PLUG_IN_VERSION));
 
271
          gimp_progress_init (_("Channel Mixer"));
289
272
 
290
 
          channel_mixer (drawable);
 
273
          channel_mixer (&mix, drawable);
291
274
 
292
275
          if (run_mode != GIMP_RUN_NONINTERACTIVE)
293
276
            gimp_displays_flush ();
294
277
 
295
278
          if (run_mode == GIMP_RUN_INTERACTIVE)
296
 
            gimp_set_data (PLUG_IN_NAME, &mix, sizeof (CmParamsType));
 
279
            gimp_set_data (PLUG_IN_PROC, &mix, sizeof (CmParamsType));
297
280
        }
298
281
    }
299
282
  else
306
289
  gimp_drawable_detach (drawable);
307
290
}
308
291
 
 
292
static void
 
293
cm_set_defaults (CmParamsType *mix)
 
294
{
 
295
  const CmParamsType defaults =
 
296
  {
 
297
    { 1.0, 0.0, 0.0 },
 
298
    { 0.0, 1.0, 0.0 },
 
299
    { 0.0, 0.0, 1.0 },
 
300
    { 1.0, 0.0, 0.0 },
 
301
    FALSE,
 
302
    FALSE,
 
303
    CM_RED_CHANNEL
 
304
  };
 
305
 
 
306
  memcpy (mix, &defaults, G_STRUCT_OFFSET (CmParamsType, preview));
 
307
}
 
308
 
309
309
static gdouble
310
310
cm_calculate_norm (CmParamsType  *mix,
311
311
                   CmChannelType *ch)
312
312
{
313
 
  gdouble sum;
314
 
 
315
 
  sum = ch->red_gain + ch->green_gain + ch->blue_gain;
316
 
 
317
 
  if (sum == 0.0 || mix->preserve_luminosity_flag == FALSE)
 
313
  gdouble sum = ch->red_gain + ch->green_gain + ch->blue_gain;
 
314
 
 
315
  if (sum == 0.0 || ! mix->preserve_luminosity)
318
316
    return 1.0;
319
317
 
320
318
  return fabs (1 / sum);
334
332
  return (guchar) CLAMP0255 (c);
335
333
}
336
334
 
 
335
static inline void
 
336
cm_process_pixel (CmParamsType  *mix,
 
337
                  const guchar  *s,
 
338
                  guchar        *d,
 
339
                  const gdouble  red_norm,
 
340
                  const gdouble  green_norm,
 
341
                  const gdouble  blue_norm,
 
342
                  const gdouble  black_norm)
 
343
{
 
344
  if (mix->monochrome)
 
345
    {
 
346
      d[0] = d[1] = d[2] =
 
347
        cm_mix_pixel (&mix->black, s[0], s[1], s[2], black_norm);
 
348
    }
 
349
  else
 
350
    {
 
351
      d[0] = cm_mix_pixel (&mix->red,   s[0], s[1], s[2], red_norm);
 
352
      d[1] = cm_mix_pixel (&mix->green, s[0], s[1], s[2], green_norm);
 
353
      d[2] = cm_mix_pixel (&mix->blue,  s[0], s[1], s[2], blue_norm);
 
354
    }
 
355
}
 
356
 
337
357
static void
338
 
channel_mixer (GimpDrawable *drawable)
 
358
channel_mixer (CmParamsType *mix,
 
359
               GimpDrawable *drawable)
339
360
{
340
361
  GimpPixelRgn  src_rgn, dest_rgn;
341
 
  guchar       *src, *dest;
342
362
  gpointer      pr;
343
 
  gint          x, y;
344
 
  gint          total, processed = 0;
345
363
  gboolean      has_alpha;
346
364
  gdouble       red_norm, green_norm, blue_norm, black_norm;
347
 
  gint          x1, y1, x2, y2;
 
365
  gint          i, total, processed = 0;
 
366
  gint          x1, y1;
348
367
  gint          width, height;
349
368
 
350
 
  gimp_drawable_mask_bounds (drawable->drawable_id,
351
 
                             &x1, &y1, &x2, &y2);
 
369
  if (! gimp_drawable_mask_intersect (drawable->drawable_id,
 
370
                                      &x1, &y1, &width, &height))
 
371
    return;
352
372
 
353
 
  width  = x2 - x1;
354
 
  height = y2 - y1;
 
373
  red_norm   = cm_calculate_norm (mix, &mix->red);
 
374
  green_norm = cm_calculate_norm (mix, &mix->green);
 
375
  blue_norm  = cm_calculate_norm (mix, &mix->blue);
 
376
  black_norm = cm_calculate_norm (mix, &mix->black);
355
377
 
356
378
  has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
357
379
 
358
 
  total = width * height;
359
 
 
360
 
  gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1));
361
 
 
362
380
  gimp_pixel_rgn_init (&src_rgn, drawable,
363
381
                       x1, y1, width, height, FALSE, FALSE);
364
382
  gimp_pixel_rgn_init (&dest_rgn, drawable,
365
383
                       x1, y1, width, height, TRUE, TRUE);
366
384
 
367
 
  red_norm   = cm_calculate_norm (&mix, &mix.red);
368
 
  green_norm = cm_calculate_norm (&mix, &mix.green);
369
 
  blue_norm  = cm_calculate_norm (&mix, &mix.blue);
370
 
  black_norm = cm_calculate_norm (&mix, &mix.black);
 
385
  total = width * height;
371
386
 
372
 
  for (pr = gimp_pixel_rgns_register (2, &src_rgn, &dest_rgn);
373
 
       pr != NULL; pr = gimp_pixel_rgns_process (pr))
 
387
  for (pr = gimp_pixel_rgns_register (2, &src_rgn, &dest_rgn), i = 0;
 
388
       pr != NULL;
 
389
       pr = gimp_pixel_rgns_process (pr), i++)
374
390
    {
375
 
      gint offset;
 
391
      const guchar *src  = src_rgn.data;
 
392
      guchar       *dest = dest_rgn.data;
 
393
      gint          x, y;
376
394
 
377
395
      for (y = 0; y < src_rgn.h; y++)
378
396
        {
379
 
          src  = src_rgn.data  + y * src_rgn.rowstride;
380
 
          dest = dest_rgn.data + y * dest_rgn.rowstride;
381
 
 
382
 
          offset = 0;
383
 
 
384
 
          for (x = 0; x < src_rgn.w; x++)
385
 
            {
386
 
              guchar r, g, b;
387
 
 
388
 
              r = src[offset + 0];
389
 
              g = src[offset + 1];
390
 
              b = src[offset + 2];
391
 
 
392
 
              if (mix.monochrome_flag == TRUE)
393
 
                {
394
 
                  dest[offset + 0] =
395
 
                  dest[offset + 1] =
396
 
                  dest[offset + 2] =
397
 
                    cm_mix_pixel (&mix.black, r, g, b, black_norm);
398
 
                }
399
 
              else
400
 
                {
401
 
                  dest[offset + 0] =
402
 
                    cm_mix_pixel (&mix.red, r, g, b, red_norm);
403
 
                  dest[offset + 1] =
404
 
                    cm_mix_pixel (&mix.green, r, g, b, green_norm);
405
 
                  dest[offset + 2] =
406
 
                    cm_mix_pixel (&mix.blue, r, g, b, blue_norm);
407
 
                }
408
 
 
409
 
              offset += 3;
410
 
 
411
 
              if (has_alpha)
412
 
                {
413
 
                  dest[offset] = src[offset];
414
 
                  offset++;
415
 
                }
416
 
 
417
 
              /* update progress */
418
 
              if ((++processed % (total / PROGRESS_UPDATE_NUM + 1)) == 0)
419
 
                gimp_progress_update ((gdouble) processed / (gdouble) total);
420
 
            }
 
397
          const guchar *s = src;
 
398
          guchar       *d = dest;
 
399
 
 
400
          if (has_alpha)
 
401
            {
 
402
              for (x = 0; x < src_rgn.w; x++, s += 4, d += 4)
 
403
                {
 
404
                  cm_process_pixel (mix, s, d,
 
405
                                    red_norm, green_norm, blue_norm,
 
406
                                    black_norm);
 
407
                  d[3] = s[3];
 
408
                }
 
409
            }
 
410
          else
 
411
            {
 
412
              for (x = 0; x < src_rgn.w; x++, s += 3, d += 3)
 
413
                {
 
414
                  cm_process_pixel (mix, s, d,
 
415
                                    red_norm, green_norm, blue_norm,
 
416
                                    black_norm);
 
417
                }
 
418
            }
 
419
 
 
420
          src += src_rgn.rowstride;
 
421
          dest += dest_rgn.rowstride;
421
422
        }
 
423
 
 
424
      processed += src_rgn.w * src_rgn.h;
 
425
 
 
426
      if (i % 16 == 0)
 
427
        gimp_progress_update ((gdouble) processed / (gdouble) total);
422
428
    }
423
429
 
424
430
  gimp_progress_update (1.0);
425
431
 
426
432
  gimp_drawable_flush (drawable);
427
433
  gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
428
 
  gimp_drawable_update (drawable->drawable_id,
429
 
                        x1, y1, width, height);
 
434
  gimp_drawable_update (drawable->drawable_id, x1, y1, width, height);
430
435
}
431
436
 
432
437
static gboolean
433
 
cm_dialog (GimpDrawable *drawable)
 
438
cm_dialog (CmParamsType *mix,
 
439
           GimpDrawable *drawable)
434
440
{
435
441
  GtkWidget *dialog;
436
442
  GtkWidget *main_vbox;
443
449
  gdouble    red_value, green_value, blue_value;
444
450
  gboolean   run;
445
451
 
446
 
  gimp_ui_init ("channel_mixer", FALSE);
 
452
  gimp_ui_init (PLUG_IN_BINARY, FALSE);
447
453
 
448
454
  /* get values */
449
 
  if (mix.monochrome_flag == TRUE)
 
455
  if (mix->monochrome)
450
456
    {
451
 
      red_value   = mix.black.red_gain   * 100;
452
 
      green_value = mix.black.green_gain * 100;
453
 
      blue_value  = mix.black.blue_gain  * 100;
 
457
      red_value   = mix->black.red_gain   * 100;
 
458
      green_value = mix->black.green_gain * 100;
 
459
      blue_value  = mix->black.blue_gain  * 100;
454
460
    }
455
461
  else
456
462
    {
457
 
      switch (mix.output_channel)
 
463
      switch (mix->output_channel)
458
464
        {
459
465
        case CM_RED_CHANNEL:
460
 
          red_value   = mix.red.red_gain   * 100;
461
 
          green_value = mix.red.green_gain * 100;
462
 
          blue_value  = mix.red.blue_gain  * 100;
 
466
          red_value   = mix->red.red_gain   * 100;
 
467
          green_value = mix->red.green_gain * 100;
 
468
          blue_value  = mix->red.blue_gain  * 100;
463
469
          break;
464
470
 
465
471
        case CM_GREEN_CHANNEL:
466
 
          red_value   = mix.green.red_gain   * 100;
467
 
          green_value = mix.green.green_gain * 100;
468
 
          blue_value  = mix.green.blue_gain  * 100;
 
472
          red_value   = mix->green.red_gain   * 100;
 
473
          green_value = mix->green.green_gain * 100;
 
474
          blue_value  = mix->green.blue_gain  * 100;
469
475
          break;
470
476
 
471
477
        case CM_BLUE_CHANNEL:
472
 
          red_value   = mix.blue.red_gain   * 100;
473
 
          green_value = mix.blue.green_gain * 100;
474
 
          blue_value  = mix.blue.blue_gain  * 100;
 
478
          red_value   = mix->blue.red_gain   * 100;
 
479
          green_value = mix->blue.green_gain * 100;
 
480
          blue_value  = mix->blue.blue_gain  * 100;
475
481
          break;
476
482
 
477
483
        default:
482
488
        }
483
489
    }
484
490
 
485
 
  dialog = gimp_dialog_new (_("Channel Mixer"), "mixer",
 
491
  dialog = gimp_dialog_new (_("Channel Mixer"), PLUG_IN_BINARY,
486
492
                            NULL, 0,
487
 
                            gimp_standard_help_func, HELP_ID,
 
493
                            gimp_standard_help_func, PLUG_IN_PROC,
488
494
 
489
495
                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
490
496
                            GTK_STOCK_OK,     GTK_RESPONSE_OK,
491
497
 
492
498
                            NULL);
493
499
 
 
500
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
 
501
                                           GTK_RESPONSE_OK,
 
502
                                           GTK_RESPONSE_CANCEL,
 
503
                                           -1);
 
504
 
 
505
  gimp_window_set_transient (GTK_WINDOW (dialog));
 
506
 
494
507
  main_vbox = gtk_vbox_new (FALSE, 12);
495
508
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
496
509
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), main_vbox);
497
510
  gtk_widget_show (main_vbox);
498
511
 
499
 
  preview = gimp_aspect_preview_new (drawable, &mix.preview);
 
512
  preview = gimp_zoom_preview_new (drawable);
500
513
  gtk_box_pack_start_defaults (GTK_BOX (main_vbox), preview);
501
514
  gtk_widget_show (preview);
502
515
  g_signal_connect_swapped (preview, "invalidated",
503
516
                            G_CALLBACK (cm_preview),
504
 
                            &mix);
 
517
                            mix);
505
518
 
506
519
  frame = gimp_frame_new (NULL);
507
520
  gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
516
529
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
517
530
  gtk_widget_show (label);
518
531
 
519
 
  mix.combo = gimp_int_combo_box_new (NULL, 0);
 
532
  mix->combo = g_object_new (GIMP_TYPE_INT_COMBO_BOX, NULL);
520
533
 
521
 
  gimp_int_combo_box_append (GIMP_INT_COMBO_BOX (mix.combo),
 
534
  gimp_int_combo_box_append (GIMP_INT_COMBO_BOX (mix->combo),
522
535
                             GIMP_INT_STORE_VALUE,    CM_RED_CHANNEL,
523
536
                             GIMP_INT_STORE_LABEL,    _("Red"),
524
537
                             GIMP_INT_STORE_STOCK_ID, GIMP_STOCK_CHANNEL_RED,
525
538
                             -1);
526
 
  gimp_int_combo_box_append (GIMP_INT_COMBO_BOX (mix.combo),
 
539
  gimp_int_combo_box_append (GIMP_INT_COMBO_BOX (mix->combo),
527
540
                             GIMP_INT_STORE_VALUE,    CM_GREEN_CHANNEL,
528
541
                             GIMP_INT_STORE_LABEL,    _("Green"),
529
542
                             GIMP_INT_STORE_STOCK_ID, GIMP_STOCK_CHANNEL_GREEN,
530
543
                             -1);
531
 
  gimp_int_combo_box_append (GIMP_INT_COMBO_BOX (mix.combo),
 
544
  gimp_int_combo_box_append (GIMP_INT_COMBO_BOX (mix->combo),
532
545
                             GIMP_INT_STORE_VALUE,    CM_BLUE_CHANNEL,
533
546
                             GIMP_INT_STORE_LABEL,    _("Blue"),
534
547
                             GIMP_INT_STORE_STOCK_ID, GIMP_STOCK_CHANNEL_BLUE,
535
548
                             -1);
536
549
 
537
 
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (mix.combo),
538
 
                                 mix.output_channel);
 
550
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (mix->combo),
 
551
                                 mix->output_channel);
539
552
 
540
 
  g_signal_connect (mix.combo, "changed",
 
553
  g_signal_connect (mix->combo, "changed",
541
554
                    G_CALLBACK (cm_combo_callback),
542
 
                    &mix);
543
 
 
544
 
  gtk_box_pack_start (GTK_BOX (hbox), mix.combo, TRUE, TRUE, 0);
545
 
  gtk_widget_show (mix.combo);
546
 
 
547
 
  if (mix.monochrome_flag)
548
 
    gtk_widget_set_sensitive (mix.combo, FALSE);
549
 
 
550
 
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), mix.combo);
 
555
                    mix);
 
556
 
 
557
  gtk_box_pack_start (GTK_BOX (hbox), mix->combo, TRUE, TRUE, 0);
 
558
  gtk_widget_show (mix->combo);
 
559
 
 
560
  if (mix->monochrome)
 
561
    gtk_widget_set_sensitive (mix->combo, FALSE);
 
562
 
 
563
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), mix->combo);
551
564
 
552
565
  /*........................................................... */
553
566
 
563
576
                    0, 1, 0, 1, GTK_FILL, GTK_FILL, 0, 0);
564
577
  gtk_widget_show (image);
565
578
 
566
 
  mix.red_data =
 
579
  mix->red_data =
567
580
    GTK_ADJUSTMENT (gimp_scale_entry_new (GTK_TABLE (table), 1, 0,
568
581
                                          _("_Red:"), 150, -1,
569
582
                                          red_value, -200.0, 200.0,
571
584
                                          TRUE, 0.0, 0.0,
572
585
                                          NULL, NULL));
573
586
 
574
 
  g_signal_connect (mix.red_data, "value_changed",
 
587
  g_signal_connect (mix->red_data, "value-changed",
575
588
                    G_CALLBACK (cm_red_scale_callback),
576
 
                    &mix);
 
589
                    mix);
577
590
 
578
591
  image = gtk_image_new_from_stock (GIMP_STOCK_CHANNEL_GREEN,
579
592
                                    GTK_ICON_SIZE_BUTTON);
581
594
                    0, 1, 1, 2, GTK_FILL, GTK_FILL, 0, 0);
582
595
  gtk_widget_show (image);
583
596
 
584
 
  mix.green_data =
 
597
  mix->green_data =
585
598
    GTK_ADJUSTMENT (gimp_scale_entry_new (GTK_TABLE (table), 1, 1,
586
599
                                          _("_Green:"), 150, -1,
587
600
                                          green_value, -200.0, 200.0,
589
602
                                          TRUE, 0.0, 0.0,
590
603
                                          NULL, NULL));
591
604
 
592
 
  g_signal_connect (mix.green_data, "value_changed",
 
605
  g_signal_connect (mix->green_data, "value-changed",
593
606
                    G_CALLBACK (cm_green_scale_callback),
594
 
                    &mix);
 
607
                    mix);
595
608
 
596
609
 
597
610
  image = gtk_image_new_from_stock (GIMP_STOCK_CHANNEL_BLUE,
600
613
                    0, 1, 2, 3, GTK_FILL, GTK_FILL, 0, 0);
601
614
  gtk_widget_show (image);
602
615
 
603
 
  mix.blue_data =
 
616
  mix->blue_data =
604
617
    GTK_ADJUSTMENT (gimp_scale_entry_new (GTK_TABLE (table), 1, 2,
605
618
                                          _("_Blue:"), 150, -1,
606
619
                                          blue_value, -200.0, 200.0,
608
621
                                          TRUE, 0.0, 0.0,
609
622
                                          NULL, NULL));
610
623
 
611
 
  g_signal_connect (mix.blue_data, "value_changed",
 
624
  g_signal_connect (mix->blue_data, "value-changed",
612
625
                    G_CALLBACK (cm_blue_scale_callback),
613
 
                    &mix);
 
626
                    mix);
614
627
 
615
628
  /*  The monochrome toggle  */
616
 
  mix.monochrome_toggle = gtk_check_button_new_with_mnemonic (_("_Monochrome"));
617
 
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mix.monochrome_toggle),
618
 
                                mix.monochrome_flag);
619
 
  gtk_box_pack_start (GTK_BOX (main_vbox), mix.monochrome_toggle, FALSE, FALSE, 0);
620
 
  gtk_widget_show (mix.monochrome_toggle);
 
629
  mix->monochrome_toggle =
 
630
    gtk_check_button_new_with_mnemonic (_("_Monochrome"));
 
631
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mix->monochrome_toggle),
 
632
                                mix->monochrome);
 
633
  gtk_box_pack_start (GTK_BOX (main_vbox), mix->monochrome_toggle,
 
634
                      FALSE, FALSE, 0);
 
635
  gtk_widget_show (mix->monochrome_toggle);
621
636
 
622
 
  g_signal_connect (mix.monochrome_toggle, "toggled",
 
637
  g_signal_connect (mix->monochrome_toggle, "toggled",
623
638
                    G_CALLBACK (cm_monochrome_callback),
624
 
                    &mix);
 
639
                    mix);
625
640
 
626
641
  /*  The preserve luminosity toggle  */
627
 
  mix.preserve_luminosity_toggle =
 
642
  mix->preserve_luminosity_toggle =
628
643
    gtk_check_button_new_with_mnemonic (_("Preserve _luminosity"));
629
644
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
630
 
                                (mix.preserve_luminosity_toggle),
631
 
                                mix.preserve_luminosity_flag);
632
 
  gtk_box_pack_start (GTK_BOX (main_vbox), mix.preserve_luminosity_toggle,
 
645
                                (mix->preserve_luminosity_toggle),
 
646
                                mix->preserve_luminosity);
 
647
  gtk_box_pack_start (GTK_BOX (main_vbox), mix->preserve_luminosity_toggle,
633
648
                      FALSE, FALSE, 0);
634
 
  gtk_widget_show (mix.preserve_luminosity_toggle);
 
649
  gtk_widget_show (mix->preserve_luminosity_toggle);
635
650
 
636
 
  g_signal_connect (mix.preserve_luminosity_toggle, "toggled",
 
651
  g_signal_connect (mix->preserve_luminosity_toggle, "toggled",
637
652
                    G_CALLBACK (cm_preserve_luminosity_callback),
638
 
                    &mix);
 
653
                    mix);
639
654
 
640
655
  /*........................................................... */
641
656
  /*  Horizontal box for file i/o  */
649
664
 
650
665
  g_signal_connect (button, "clicked",
651
666
                    G_CALLBACK (cm_load_file_callback),
652
 
                    &mix);
 
667
                    mix);
653
668
 
654
669
  button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
655
670
  gtk_container_add (GTK_CONTAINER (hbox), button);
657
672
 
658
673
  g_signal_connect (button, "clicked",
659
674
                    G_CALLBACK (cm_save_file_callback),
660
 
                    &mix);
 
675
                    mix);
 
676
 
 
677
  button = gtk_button_new_from_stock (GIMP_STOCK_RESET);
 
678
  gtk_container_add (GTK_CONTAINER (hbox), button);
 
679
  gtk_widget_show (button);
 
680
 
 
681
  g_signal_connect (button, "clicked",
 
682
                    G_CALLBACK (cm_reset_callback),
 
683
                    mix);
661
684
 
662
685
  gtk_widget_show (dialog);
663
686
 
672
695
cm_red_scale_callback (GtkAdjustment *adjustment,
673
696
                       CmParamsType  *mix)
674
697
{
675
 
  if (mix->monochrome_flag == TRUE)
676
 
    mix->black.red_gain = adjustment->value / 100.0;
 
698
  if (mix->monochrome)
 
699
    {
 
700
      mix->black.red_gain = adjustment->value / 100.0;
 
701
    }
677
702
  else
678
 
    switch (mix->output_channel)
679
 
      {
680
 
      case CM_RED_CHANNEL:
681
 
        mix->red.red_gain = adjustment->value / 100.0;
682
 
        break;
683
 
      case CM_GREEN_CHANNEL:
684
 
        mix->green.red_gain = adjustment->value / 100.0;
685
 
        break;
686
 
      case CM_BLUE_CHANNEL:
687
 
        mix->blue.red_gain = adjustment->value / 100.0;
688
 
        break;
689
 
      }
 
703
    {
 
704
      switch (mix->output_channel)
 
705
        {
 
706
        case CM_RED_CHANNEL:
 
707
          mix->red.red_gain = adjustment->value / 100.0;
 
708
          break;
 
709
        case CM_GREEN_CHANNEL:
 
710
          mix->green.red_gain = adjustment->value / 100.0;
 
711
          break;
 
712
        case CM_BLUE_CHANNEL:
 
713
          mix->blue.red_gain = adjustment->value / 100.0;
 
714
          break;
 
715
        }
 
716
    }
690
717
 
691
718
  gimp_preview_invalidate (GIMP_PREVIEW (preview));
692
719
}
695
722
cm_green_scale_callback (GtkAdjustment *adjustment,
696
723
                         CmParamsType  *mix)
697
724
{
698
 
  if (mix->monochrome_flag == TRUE)
699
 
    mix->black.green_gain = adjustment->value / 100.0;
 
725
  if (mix->monochrome)
 
726
    {
 
727
      mix->black.green_gain = adjustment->value / 100.0;
 
728
    }
700
729
  else
701
 
    switch (mix->output_channel)
702
 
      {
703
 
      case CM_RED_CHANNEL:
704
 
        mix->red.green_gain = adjustment->value / 100.0;
705
 
        break;
706
 
      case CM_GREEN_CHANNEL:
707
 
        mix->green.green_gain = adjustment->value / 100.0;
708
 
        break;
709
 
      case CM_BLUE_CHANNEL:
710
 
        mix->blue.green_gain = adjustment->value / 100.0;
711
 
        break;
712
 
      }
 
730
    {
 
731
      switch (mix->output_channel)
 
732
        {
 
733
        case CM_RED_CHANNEL:
 
734
          mix->red.green_gain = adjustment->value / 100.0;
 
735
          break;
 
736
        case CM_GREEN_CHANNEL:
 
737
          mix->green.green_gain = adjustment->value / 100.0;
 
738
          break;
 
739
        case CM_BLUE_CHANNEL:
 
740
          mix->blue.green_gain = adjustment->value / 100.0;
 
741
          break;
 
742
        }
 
743
    }
713
744
 
714
745
  gimp_preview_invalidate (GIMP_PREVIEW (preview));
715
746
}
718
749
cm_blue_scale_callback (GtkAdjustment *adjustment,
719
750
                        CmParamsType  *mix)
720
751
{
721
 
  if (mix->monochrome_flag == TRUE)
722
 
    mix->black.blue_gain = adjustment->value / 100.0;
 
752
  if (mix->monochrome)
 
753
    {
 
754
      mix->black.blue_gain = adjustment->value / 100.0;
 
755
    }
723
756
  else
724
 
    switch (mix->output_channel)
725
 
      {
726
 
      case CM_RED_CHANNEL:
727
 
        mix->red.blue_gain = adjustment->value / 100.0;
728
 
        break;
729
 
      case CM_GREEN_CHANNEL:
730
 
        mix->green.blue_gain = adjustment->value / 100.0;
731
 
        break;
732
 
      case CM_BLUE_CHANNEL:
733
 
        mix->blue.blue_gain = adjustment->value / 100.0;
734
 
        break;
735
 
      }
 
757
    {
 
758
      switch (mix->output_channel)
 
759
        {
 
760
        case CM_RED_CHANNEL:
 
761
          mix->red.blue_gain = adjustment->value / 100.0;
 
762
          break;
 
763
        case CM_GREEN_CHANNEL:
 
764
          mix->green.blue_gain = adjustment->value / 100.0;
 
765
          break;
 
766
        case CM_BLUE_CHANNEL:
 
767
          mix->blue.blue_gain = adjustment->value / 100.0;
 
768
          break;
 
769
        }
 
770
    }
736
771
 
737
772
  gimp_preview_invalidate (GIMP_PREVIEW (preview));
738
773
}
741
776
cm_preview (CmParamsType *mix,
742
777
            GimpPreview  *preview)
743
778
{
744
 
  guchar       *dst, *src;
 
779
  guchar       *src, *s;
 
780
  guchar       *dst, *d;
745
781
  gint          x, y;
746
 
  gint          offset, rowsize;
747
782
  gdouble       red_norm, green_norm, blue_norm, black_norm;
748
783
  gint          width, height, bpp;
749
 
  GimpDrawable *drawable = GIMP_ASPECT_PREVIEW (preview)->drawable;
 
784
  GimpDrawable *drawable;
750
785
 
751
786
  red_norm   = cm_calculate_norm (mix, &mix->red);
752
787
  green_norm = cm_calculate_norm (mix, &mix->green);
753
788
  blue_norm  = cm_calculate_norm (mix, &mix->blue);
754
789
  black_norm = cm_calculate_norm (mix, &mix->black);
755
790
 
756
 
  gimp_preview_get_size (GIMP_PREVIEW (preview), &width, &height);
757
 
  bpp = gimp_drawable_bpp (drawable->drawable_id);
758
 
  src = gimp_drawable_get_thumbnail_data (drawable->drawable_id,
 
791
  drawable = gimp_zoom_preview_get_drawable (GIMP_ZOOM_PREVIEW (preview));
 
792
 
 
793
  src = s = gimp_zoom_preview_get_source (GIMP_ZOOM_PREVIEW (preview),
759
794
                                          &width, &height, &bpp);
760
795
 
761
 
  rowsize = width * bpp;
762
 
  dst = g_new (guchar, rowsize * height);
 
796
  dst = d = g_new (guchar, width * height * bpp);
763
797
 
764
 
  offset = 0;
765
798
  for (y = 0; y < height; y++)
766
799
    {
767
 
      for (x = 0; x < width; x++)
 
800
      for (x = 0; x < width; x++, s += bpp, d += bpp)
768
801
        {
769
 
          guchar r, g, b;
770
 
 
771
 
          r = src[offset + 0];
772
 
          g = src[offset + 1];
773
 
          b = src[offset + 2];
774
 
 
775
 
          if (mix->monochrome_flag == TRUE)
776
 
            {
777
 
              dst[offset + 0] =
778
 
              dst[offset + 1] =
779
 
              dst[offset + 2] =
780
 
                cm_mix_pixel (&mix->black, r, g, b, black_norm);
781
 
            }
782
 
          else
783
 
            {
784
 
              dst[offset + 0] =
785
 
                cm_mix_pixel (&mix->red, r, g, b, red_norm);
786
 
              dst[offset + 1] =
787
 
                cm_mix_pixel (&mix->green, r, g, b, green_norm);
788
 
              dst[offset + 2] =
789
 
                cm_mix_pixel (&mix->blue, r, g, b, blue_norm);
790
 
            }
 
802
          cm_process_pixel (mix, s, d,
 
803
                            red_norm, green_norm, blue_norm,
 
804
                            black_norm);
 
805
 
791
806
          if (bpp == 4)
792
 
            dst[offset + 3] = src[offset + 3];
793
 
 
794
 
          offset += bpp;
 
807
            d[3] = s[3];
795
808
        }
796
809
    }
 
810
 
797
811
  gimp_preview_draw_buffer (GIMP_PREVIEW (preview), dst, bpp * width);
798
812
 
 
813
  g_free (src);
799
814
  g_free (dst);
800
815
}
801
816
 
806
821
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
807
822
    {
808
823
      mix->old_output_channel = mix->output_channel;
809
 
      mix->monochrome_flag = TRUE;
 
824
      mix->monochrome = TRUE;
810
825
      gtk_widget_set_sensitive (mix->combo, FALSE);
811
826
    }
812
827
  else
813
828
    {
814
829
      mix->output_channel = mix->old_output_channel;
815
 
      mix->monochrome_flag = FALSE;
 
830
      mix->monochrome = FALSE;
816
831
      gtk_widget_set_sensitive (mix->combo, TRUE);
817
832
    }
818
833
 
826
841
                                 CmParamsType *mix)
827
842
{
828
843
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
829
 
    mix->preserve_luminosity_flag = TRUE;
 
844
    mix->preserve_luminosity = TRUE;
830
845
  else
831
 
    mix->preserve_luminosity_flag = FALSE;
 
846
    mix->preserve_luminosity = FALSE;
832
847
 
833
848
  gimp_preview_invalidate (GIMP_PREVIEW (preview));
834
849
}
863
878
 
864
879
                                     NULL);
865
880
 
 
881
      gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
 
882
                                               GTK_RESPONSE_OK,
 
883
                                               GTK_RESPONSE_CANCEL,
 
884
                                               -1);
 
885
 
866
886
      gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
867
887
 
868
888
      g_signal_connect (dialog, "response",
869
889
                        G_CALLBACK (cm_load_file_response_callback),
870
890
                        mix);
871
 
      g_signal_connect (dialog, "delete_event",
 
891
      g_signal_connect (dialog, "delete-event",
872
892
                        G_CALLBACK (gtk_true),
873
893
                        NULL);
874
894
 
893
913
 
894
914
      filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
895
915
 
896
 
      fp = fopen (filename, "r");
 
916
      fp = g_fopen (filename, "r");
897
917
 
898
918
      if (fp)
899
919
        {
917
937
 
918
938
          fscanf (fp, "%*s %s", buf[0]);
919
939
          if (strcmp (buf[0], "TRUE") == 0)
920
 
            mix->monochrome_flag = TRUE;
 
940
            mix->monochrome = TRUE;
921
941
          else
922
 
            mix->monochrome_flag = FALSE;
923
 
 
924
 
          gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mix->monochrome_toggle),
925
 
                                        mix->monochrome_flag);
 
942
            mix->monochrome = FALSE;
926
943
 
927
944
          fscanf (fp, "%*s %s", buf[0]);
928
945
          if (strcmp (buf[0], "TRUE") == 0)
929
 
            mix->preserve_luminosity_flag = TRUE;
 
946
            mix->preserve_luminosity = TRUE;
930
947
          else
931
 
            mix->preserve_luminosity_flag = FALSE;
932
 
 
933
 
          gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mix->preserve_luminosity_toggle),
934
 
                                        mix->preserve_luminosity_flag);
 
948
            mix->preserve_luminosity = FALSE;
935
949
 
936
950
          fscanf (fp, "%*s %s %s %s", buf[0], buf[1], buf[2]);
937
951
          mix->red.red_gain   = g_ascii_strtod (buf[0], NULL);
955
969
 
956
970
          fclose (fp);
957
971
 
958
 
          gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (mix->combo),
959
 
                                         mix->output_channel);
960
 
          cm_set_adjusters (mix);
961
 
 
962
 
          gimp_preview_invalidate (GIMP_PREVIEW (preview));
 
972
          cm_update_ui (mix);
963
973
        }
964
974
      else
965
975
        {
995
1005
 
996
1006
                                     NULL);
997
1007
 
 
1008
      gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
 
1009
                                               GTK_RESPONSE_OK,
 
1010
                                               GTK_RESPONSE_CANCEL,
 
1011
                                               -1);
998
1012
      gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
999
1013
 
 
1014
      gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
 
1015
                                                      TRUE);
 
1016
 
1000
1017
      g_signal_connect (dialog, "response",
1001
1018
                        G_CALLBACK (cm_save_file_response_callback),
1002
1019
                        mix);
1003
 
      g_signal_connect (dialog, "delete_event",
 
1020
      g_signal_connect (dialog, "delete-event",
1004
1021
                        G_CALLBACK (gtk_true),
1005
1022
                        NULL);
1006
1023
 
1030
1047
  if (! filename)
1031
1048
    return;
1032
1049
 
1033
 
  if (g_file_test (filename, G_FILE_TEST_EXISTS) &&
1034
 
      ! cm_force_overwrite (filename, dialog))
1035
 
    {
1036
 
      g_free (filename);
1037
 
      return;
1038
 
    }
1039
 
 
1040
 
  file = fopen (filename, "w");
 
1050
  file = g_fopen (filename, "w");
1041
1051
 
1042
1052
  if (! file)
1043
1053
    {
1055
1065
  gtk_widget_hide (dialog);
1056
1066
}
1057
1067
 
1058
 
static gboolean
1059
 
cm_force_overwrite (const gchar *filename,
1060
 
                    GtkWidget   *parent)
1061
 
{
1062
 
  GtkWidget *dlg;
1063
 
  GtkWidget *label;
1064
 
  GtkWidget *hbox;
1065
 
  gchar     *buffer;
1066
 
  gboolean   overwrite;
1067
 
 
1068
 
  dlg = gimp_dialog_new (_("Channel Mixer File Operation Warning"),
1069
 
                         "channel_mixer",
1070
 
                         parent, GTK_DIALOG_MODAL,
1071
 
                         gimp_standard_help_func, HELP_ID,
1072
 
 
1073
 
                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1074
 
                         GTK_STOCK_OK,     GTK_RESPONSE_OK,
1075
 
 
1076
 
                         NULL);
1077
 
 
1078
 
  hbox = gtk_hbox_new (FALSE, 0);
1079
 
  gtk_container_set_border_width (GTK_CONTAINER (hbox), 12);
1080
 
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), hbox, FALSE, FALSE, 0);
1081
 
  gtk_widget_show (hbox);
1082
 
 
1083
 
  buffer = g_strdup_printf (_("File '%s' exists.\n"
1084
 
                              "Overwrite it?"), filename);
1085
 
  label = gtk_label_new (buffer);
1086
 
  g_free (buffer);
1087
 
 
1088
 
  gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
1089
 
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
1090
 
  gtk_widget_show (label);
1091
 
 
1092
 
  gtk_widget_show (dlg);
1093
 
 
1094
 
  overwrite = (gimp_dialog_run (GIMP_DIALOG (dlg)) == GTK_RESPONSE_OK);
1095
 
 
1096
 
  gtk_widget_destroy (dlg);
1097
 
 
1098
 
  return overwrite;
1099
 
}
1100
 
 
1101
1068
static void
1102
1069
cm_save_file (CmParamsType *mix,
1103
1070
              FILE         *fp)
1126
1093
  fprintf (fp, "CHANNEL: %s\n", str);
1127
1094
  fprintf (fp, "PREVIEW: %s\n", "TRUE"); /* preserved for compatibility */
1128
1095
  fprintf (fp, "MONOCHROME: %s\n",
1129
 
           mix->monochrome_flag ? "TRUE" : "FALSE");
 
1096
           mix->monochrome ? "TRUE" : "FALSE");
1130
1097
  fprintf (fp, "PRESERVE_LUMINOSITY: %s\n",
1131
 
           mix->preserve_luminosity_flag ? "TRUE" : "FALSE");
 
1098
           mix->preserve_luminosity ? "TRUE" : "FALSE");
1132
1099
 
1133
1100
  fprintf (fp, "RED: %s %s %s\n",
1134
1101
           g_ascii_formatd (buf[0], sizeof (buf[0]), "%5.3f",
1166
1133
}
1167
1134
 
1168
1135
static void
 
1136
cm_reset_callback (GtkWidget    *widget,
 
1137
                   CmParamsType *mix)
 
1138
{
 
1139
  cm_set_defaults (mix);
 
1140
  cm_update_ui (mix);
 
1141
}
 
1142
 
 
1143
static void
1169
1144
cm_combo_callback (GtkWidget    *widget,
1170
1145
                   CmParamsType *mix)
1171
1146
{
1172
 
  gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget),
1173
 
                                 (gint *) &mix->output_channel);
1174
 
 
1175
 
  cm_set_adjusters (mix);
 
1147
  gint value;
 
1148
 
 
1149
  if (gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget), &value))
 
1150
    {
 
1151
      mix->output_channel = value;
 
1152
 
 
1153
      cm_set_adjusters (mix);
 
1154
    }
1176
1155
}
1177
1156
 
1178
1157
static void
1179
1158
cm_set_adjusters (CmParamsType *mix)
1180
1159
{
1181
 
  if (mix->monochrome_flag == TRUE)
 
1160
  if (mix->monochrome)
1182
1161
    {
1183
1162
      gtk_adjustment_set_value (mix->red_data,   mix->black.red_gain   * 100.0);
1184
1163
      gtk_adjustment_set_value (mix->green_data, mix->black.green_gain * 100.0);
1208
1187
      break;
1209
1188
    }
1210
1189
}
 
1190
 
 
1191
static void
 
1192
cm_update_ui (CmParamsType *mix)
 
1193
{
 
1194
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mix->monochrome_toggle),
 
1195
                                mix->monochrome);
 
1196
 
 
1197
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mix->preserve_luminosity_toggle),
 
1198
                                mix->preserve_luminosity);
 
1199
 
 
1200
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (mix->combo),
 
1201
                                 mix->output_channel);
 
1202
 
 
1203
  cm_set_adjusters (mix);
 
1204
 
 
1205
  gimp_preview_invalidate (GIMP_PREVIEW (preview));
 
1206
}