channel_mixer.c 39.2 KB
Newer Older
1 2 3 4 5
/****************************************************************************
 * This is a plugin for the GIMP v 2.0 or later.
 *
 * Copyright (C) 2002 Martin Guldahl <mguldahl@xmission.com>
 * Based on GTK code from:
6
 *    homomorphic (Copyright (C) 2001 Valter Marcus Hilden)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *    rand-noted  (Copyright (C) 1998 Miles O'Neal)
 *    nlfilt      (Copyright (C) 1997 Eric L. Hernes)
 *    pagecurl    (Copyright (C) 1996 Federico Mena Quintero)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 ****************************************************************************/

#include "config.h"

29
#include <errno.h>
30
#include <string.h>
31

32
#include <glib/gstdio.h>
33 34 35 36 37 38 39

#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>

#include "libgimp/stdplugins-intl.h"


40 41
#define PLUG_IN_PROC        "plug-in-colors-channel-mixer"
#define PLUG_IN_BINARY      "channel_mixer"
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#define PLUG_IN_VERSION     "Channel Mixer 0.8"
#define PROGRESS_UPDATE_NUM 20
#define CM_LINE_SIZE        1024

typedef enum
{
  CM_RED_CHANNEL,
  CM_GREEN_CHANNEL,
  CM_BLUE_CHANNEL
} CmModeType;

typedef struct
{
  gdouble red_gain;
  gdouble green_gain;
  gdouble blue_gain;
} CmChannelType;

typedef struct
{
  CmChannelType  red;
  CmChannelType  green;
  CmChannelType  blue;
  CmChannelType  black;

  gboolean       monochrome_flag;
  gboolean       preserve_luminosity_flag;

  CmModeType     output_channel;
71
  gboolean       preview;
72 73 74 75 76

  GtkAdjustment *red_data;
  GtkAdjustment *green_data;
  GtkAdjustment *blue_data;

77
  GtkWidget     *combo;
78 79 80 81 82 83 84 85

  CmModeType     old_output_channel;

  GtkWidget     *monochrome_toggle;
  GtkWidget     *preserve_luminosity_toggle;
} CmParamsType;


86 87 88 89 90 91
static void     query (void);
static void     run   (const gchar      *name,
                       gint              nparams,
                       const GimpParam  *param,
                       gint             *nreturn_vals,
                       GimpParam       **return_vals);
92

93 94
static void cm_set_defaults (CmParamsType *mix);

95
static void     channel_mixer (GimpDrawable *drawable);
96
static gboolean cm_dialog     (GimpDrawable *drawable);
97 98 99 100 101 102 103 104 105 106 107 108 109

static void cm_red_scale_callback           (GtkAdjustment    *adjustment,
                                             CmParamsType     *mix);
static void cm_green_scale_callback         (GtkAdjustment    *adjustment,
                                             CmParamsType     *mix);
static void cm_blue_scale_callback          (GtkAdjustment    *adjustment,
                                             CmParamsType     *mix);
static void cm_monochrome_callback          (GtkWidget        *widget,
                                             CmParamsType     *mix);
static void cm_preserve_luminosity_callback (GtkWidget        *widget,
                                             CmParamsType     *mix);
static void cm_load_file_callback           (GtkWidget        *widget,
                                             CmParamsType     *mix);
110
static void cm_load_file_response_callback  (GtkWidget        *dialog,
111 112 113 114
                                             gint              response_id,
                                             CmParamsType     *mix);
static void cm_save_file_callback           (GtkWidget        *widget,
                                             CmParamsType     *mix);
115
static void cm_save_file_response_callback  (GtkWidget        *dialog,
116 117
                                             gint              response_id,
                                             CmParamsType     *mix);
118 119
static void cm_reset_callback               (GtkWidget        *widget,
                                             CmParamsType     *mix);
120
static void cm_combo_callback               (GtkWidget        *widget,
121 122 123 124 125 126 127 128 129 130 131 132 133 134
                                             CmParamsType     *mix);

static gboolean cm_force_overwrite (const gchar *filename,
                                    GtkWidget   *parent);

static gdouble cm_calculate_norm (CmParamsType  *mix,
                                  CmChannelType *ch);

static inline guchar cm_mix_pixel (CmChannelType *ch,
                                   guchar         r,
                                   guchar         g,
                                   guchar         b,
                                   gdouble        norm);

135 136
static void cm_preview       (CmParamsType *mix,
                              GimpPreview  *preview);
137
static void cm_set_adjusters (CmParamsType *mix);
138
static void cm_update_ui     (CmParamsType *mix);
139 140 141 142

static void cm_save_file (CmParamsType *mix,
                          FILE         *fp);

143

144 145 146 147 148 149 150 151
GimpPlugInInfo PLUG_IN_INFO =
{
  NULL,  /* init_proc  */
  NULL,  /* quit_proc  */
  query, /* query_proc */
  run    /* run_proc   */
};

152 153 154
static CmParamsType  mix;
static GtkWidget    *preview;

155 156 157 158 159 160 161 162

MAIN ()

static void
query (void)
{
  static GimpParamDef args[] =
  {
163 164 165 166 167 168 169 170 171 172 173 174 175
    { GIMP_PDB_INT32,    "run-mode",   "Interactive, non-interactive" },
    { GIMP_PDB_IMAGE,    "image",      "Input image (unused)" },
    { GIMP_PDB_DRAWABLE, "drawable",   "Input drawable" },
    { GIMP_PDB_INT32,    "monochrome", "Monochrome (TRUE or FALSE)" },
    { GIMP_PDB_FLOAT,    "rr-gain",    "Set the red gain for the red channel" },
    { GIMP_PDB_FLOAT,    "rg-gain",    "Set the green gain for the red channel" },
    { GIMP_PDB_FLOAT,    "rb-gain",    "Set the blue gain for the red channel" },
    { GIMP_PDB_FLOAT,    "gr-gain",    "Set the red gain for the green channel" },
    { GIMP_PDB_FLOAT,    "gg-gain",    "Set the green gain for the green channel" },
    { GIMP_PDB_FLOAT,    "gb-gain",    "Set the blue gain for the green channel" },
    { GIMP_PDB_FLOAT,    "br-gain",    "Set the red gain for the blue channel" },
    { GIMP_PDB_FLOAT,    "bg-gain",    "Set the green gain for the blue channel" },
    { GIMP_PDB_FLOAT,    "bb-gain",    "Set the blue gain for the blue channel" }
176 177
  };

178
  gimp_install_procedure (PLUG_IN_PROC,
179 180 181 182 183
                          "Mix RGB Channels.",
                          "This plug-in mixes the RGB channels.",
                          "Martin Guldahl <mguldahl@xmission.com>",
                          "Martin Guldahl <mguldahl@xmission.com>",
                          "2002",
184
                          N_("Channel Mi_xer..."),
185 186 187 188
                          "RGB*",
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (args), 0,
                          args, NULL);
189

190
  gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Colors/Modify");
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
}

/*----------------------------------------------------------------------
 *  run() - main routine
 *
 *  This handles the main interaction with the GIMP itself,
 *  and invokes the routine that actually does the work.
 *--------------------------------------------------------------------*/
static void
run (const gchar      *name,
     gint              nparams,
     const GimpParam  *param,
     gint             *nreturn_vals,
     GimpParam       **return_vals)
{
  static GimpParam   values[1];
  GimpDrawable      *drawable;
  GimpRunMode        run_mode;
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;

  run_mode = param[0].data.d_int32;

  INIT_I18N ();

  *nreturn_vals = 1;
  *return_vals  = values;

  values[0].type          = GIMP_PDB_STATUS;
  values[0].data.d_status = status;

  drawable = gimp_drawable_get (param[2].data.d_drawable);

  if (gimp_drawable_is_rgb (drawable->drawable_id))
    {
225 226 227
      cm_set_defaults (&mix);
      mix.preview = TRUE;

228 229 230
      switch (run_mode)
        {
        case GIMP_RUN_INTERACTIVE:
231
          gimp_get_data (PLUG_IN_PROC, &mix);
232

233
          if (! cm_dialog (drawable))
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
            {
              gimp_drawable_detach (drawable);
              return;
            }

          break;

        case GIMP_RUN_NONINTERACTIVE:
          mix.monochrome_flag = param[3].data.d_int32;

          if (mix.monochrome_flag == 1)
            {
              mix.black.red_gain   = param[4].data.d_float;
              mix.black.green_gain = param[5].data.d_float;
              mix.black.blue_gain  = param[6].data.d_float;
            }
          else
            {
              mix.red.red_gain     = param[4].data.d_float;
              mix.red.green_gain   = param[5].data.d_float;
              mix.red.blue_gain    = param[6].data.d_float;
              mix.green.red_gain   = param[7].data.d_float;
              mix.green.green_gain = param[8].data.d_float;
              mix.green.blue_gain  = param[9].data.d_float;
              mix.blue.red_gain    = param[10].data.d_float;
              mix.blue.green_gain  = param[11].data.d_float;
              mix.blue.blue_gain   = param[12].data.d_float;
            }
          break;

        case GIMP_RUN_WITH_LAST_VALS:
265
          gimp_get_data (PLUG_IN_PROC, &mix);
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
          break;

        default:
          break;
        }

      if (status == GIMP_PDB_SUCCESS)
        {
          /* printf("Channel Mixer:: Mode:%d  r %f  g %f  b %f\n ",
                 param[3].data.d_int32, mix.black.red_gain,
                 mix.black.green_gain, mix.black.blue_gain); */

          gimp_progress_init (_(PLUG_IN_VERSION));

          channel_mixer (drawable);

          if (run_mode != GIMP_RUN_NONINTERACTIVE)
            gimp_displays_flush ();

          if (run_mode == GIMP_RUN_INTERACTIVE)
286
            gimp_set_data (PLUG_IN_PROC, &mix, sizeof (CmParamsType));
287 288 289 290 291 292 293 294 295 296 297 298
        }
    }
  else
    {
      status = GIMP_PDB_EXECUTION_ERROR;
    }

  values[0].data.d_status = status;

  gimp_drawable_detach (drawable);
}

299 300 301
static void
cm_set_defaults (CmParamsType *mix)
{
302 303
  static CmParamsType defaults =
  {
304 305 306 307 308 309 310 311 312 313 314 315
    { 1.0, 0.0, 0.0 },
    { 0.0, 1.0, 0.0 },
    { 0.0, 0.0, 1.0 },
    { 1.0, 0.0, 0.0 },
    FALSE,
    FALSE,
    CM_RED_CHANNEL
  };

  memcpy (mix, &defaults, G_STRUCT_OFFSET (CmParamsType, preview));
}

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
static gdouble
cm_calculate_norm (CmParamsType  *mix,
                   CmChannelType *ch)
{
  gdouble sum;

  sum = ch->red_gain + ch->green_gain + ch->blue_gain;

  if (sum == 0.0 || mix->preserve_luminosity_flag == FALSE)
    return 1.0;

  return fabs (1 / sum);
}

static inline guchar
cm_mix_pixel (CmChannelType *ch,
              guchar         r,
              guchar         g,
              guchar         b,
              gdouble        norm)
{
  gdouble c = ch->red_gain * r + ch->green_gain * g + ch->blue_gain * b;

  c *= norm;

341
  return (guchar) CLAMP0255 (c);
342 343 344 345 346 347 348 349 350 351 352 353
}

static void
channel_mixer (GimpDrawable *drawable)
{
  GimpPixelRgn  src_rgn, dest_rgn;
  guchar       *src, *dest;
  gpointer      pr;
  gint          x, y;
  gint          total, processed = 0;
  gboolean      has_alpha;
  gdouble       red_norm, green_norm, blue_norm, black_norm;
354 355 356 357 358 359 360 361
  gint          x1, y1, x2, y2;
  gint          width, height;

  gimp_drawable_mask_bounds (drawable->drawable_id,
                             &x1, &y1, &x2, &y2);

  width  = x2 - x1;
  height = y2 - y1;
362 363 364

  has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);

365
  total = width * height;
366 367 368 369

  gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1));

  gimp_pixel_rgn_init (&src_rgn, drawable,
370
                       x1, y1, width, height, FALSE, FALSE);
371
  gimp_pixel_rgn_init (&dest_rgn, drawable,
372
                       x1, y1, width, height, TRUE, TRUE);
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

  red_norm   = cm_calculate_norm (&mix, &mix.red);
  green_norm = cm_calculate_norm (&mix, &mix.green);
  blue_norm  = cm_calculate_norm (&mix, &mix.blue);
  black_norm = cm_calculate_norm (&mix, &mix.black);

  for (pr = gimp_pixel_rgns_register (2, &src_rgn, &dest_rgn);
       pr != NULL; pr = gimp_pixel_rgns_process (pr))
    {
      gint offset;

      for (y = 0; y < src_rgn.h; y++)
        {
          src  = src_rgn.data  + y * src_rgn.rowstride;
          dest = dest_rgn.data + y * dest_rgn.rowstride;

          offset = 0;

          for (x = 0; x < src_rgn.w; x++)
            {
              guchar r, g, b;

395 396 397
              r = src[offset + 0];
              g = src[offset + 1];
              b = src[offset + 2];
398 399 400

              if (mix.monochrome_flag == TRUE)
                {
401 402 403
                  dest[offset + 0] =
                  dest[offset + 1] =
                  dest[offset + 2] =
404
                    cm_mix_pixel (&mix.black, r, g, b, black_norm);
405 406 407
                }
              else
                {
408
                  dest[offset + 0] =
409
                    cm_mix_pixel (&mix.red, r, g, b, red_norm);
410
                  dest[offset + 1] =
411
                    cm_mix_pixel (&mix.green, r, g, b, green_norm);
412
                  dest[offset + 2] =
413 414 415 416 417 418 419
                    cm_mix_pixel (&mix.blue, r, g, b, blue_norm);
                }

              offset += 3;

              if (has_alpha)
                {
420
                  dest[offset] = src[offset];
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
                  offset++;
                }

              /* update progress */
              if ((++processed % (total / PROGRESS_UPDATE_NUM + 1)) == 0)
                gimp_progress_update ((gdouble) processed / (gdouble) total);
            }
        }
    }

  gimp_progress_update (1.0);

  gimp_drawable_flush (drawable);
  gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
  gimp_drawable_update (drawable->drawable_id,
436
                        x1, y1, width, height);
437 438
}

439
static gboolean
440
cm_dialog (GimpDrawable *drawable)
441 442
{
  GtkWidget *dialog;
443
  GtkWidget *main_vbox;
444 445 446 447
  GtkWidget *frame;
  GtkWidget *hbox;
  GtkWidget *button;
  GtkWidget *label;
Sven Neumann's avatar
Sven Neumann committed
448
  GtkWidget *image;
449 450 451 452
  GtkWidget *table;
  gdouble    red_value, green_value, blue_value;
  gboolean   run;

453
  gimp_ui_init (PLUG_IN_BINARY, FALSE);
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486

  /* get values */
  if (mix.monochrome_flag == TRUE)
    {
      red_value   = mix.black.red_gain   * 100;
      green_value = mix.black.green_gain * 100;
      blue_value  = mix.black.blue_gain  * 100;
    }
  else
    {
      switch (mix.output_channel)
        {
        case CM_RED_CHANNEL:
          red_value   = mix.red.red_gain   * 100;
          green_value = mix.red.green_gain * 100;
          blue_value  = mix.red.blue_gain  * 100;
          break;

        case CM_GREEN_CHANNEL:
          red_value   = mix.green.red_gain   * 100;
          green_value = mix.green.green_gain * 100;
          blue_value  = mix.green.blue_gain  * 100;
          break;

        case CM_BLUE_CHANNEL:
          red_value   = mix.blue.red_gain   * 100;
          green_value = mix.blue.green_gain * 100;
          blue_value  = mix.blue.blue_gain  * 100;
          break;

        default:
          g_assert_not_reached ();

487
          red_value = green_value = blue_value = 0.0;
488 489 490 491
          break;
        }
    }

492
  dialog = gimp_dialog_new (_("Channel Mixer"), PLUG_IN_BINARY,
493
                            NULL, 0,
494
                            gimp_standard_help_func, PLUG_IN_PROC,
495 496 497 498

                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                            GTK_STOCK_OK,     GTK_RESPONSE_OK,

499
                            NULL);
500

501
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
502 503 504
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);
505

506
  gimp_window_set_transient (GTK_WINDOW (dialog));
507

508 509 510 511
  main_vbox = gtk_vbox_new (FALSE, 12);
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), main_vbox);
  gtk_widget_show (main_vbox);
512

513
  preview = gimp_zoom_preview_new (drawable);
514 515 516 517 518
  gtk_box_pack_start_defaults (GTK_BOX (main_vbox), preview);
  gtk_widget_show (preview);
  g_signal_connect_swapped (preview, "invalidated",
                            G_CALLBACK (cm_preview),
                            &mix);
519

520
  frame = gimp_frame_new (NULL);
521
  gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
522 523
  gtk_widget_show (frame);

524
  hbox = gtk_hbox_new (FALSE, 6);
525 526
  gtk_frame_set_label_widget (GTK_FRAME (frame), hbox);
  gtk_widget_show (hbox);
527

528
  label = gtk_label_new_with_mnemonic (_("O_utput channel:"));
Sven Neumann's avatar
Sven Neumann committed
529

530 531 532
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

Sven Neumann's avatar
Sven Neumann committed
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
  mix.combo = gimp_int_combo_box_new (NULL, 0);

  gimp_int_combo_box_append (GIMP_INT_COMBO_BOX (mix.combo),
                             GIMP_INT_STORE_VALUE,    CM_RED_CHANNEL,
                             GIMP_INT_STORE_LABEL,    _("Red"),
                             GIMP_INT_STORE_STOCK_ID, GIMP_STOCK_CHANNEL_RED,
                             -1);
  gimp_int_combo_box_append (GIMP_INT_COMBO_BOX (mix.combo),
                             GIMP_INT_STORE_VALUE,    CM_GREEN_CHANNEL,
                             GIMP_INT_STORE_LABEL,    _("Green"),
                             GIMP_INT_STORE_STOCK_ID, GIMP_STOCK_CHANNEL_GREEN,
                             -1);
  gimp_int_combo_box_append (GIMP_INT_COMBO_BOX (mix.combo),
                             GIMP_INT_STORE_VALUE,    CM_BLUE_CHANNEL,
                             GIMP_INT_STORE_LABEL,    _("Blue"),
                             GIMP_INT_STORE_STOCK_ID, GIMP_STOCK_CHANNEL_BLUE,
                             -1);

551 552
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (mix.combo),
                                 mix.output_channel);
553

554 555 556
  g_signal_connect (mix.combo, "changed",
                    G_CALLBACK (cm_combo_callback),
                    &mix);
557

Sven Neumann's avatar
Sven Neumann committed
558
  gtk_box_pack_start (GTK_BOX (hbox), mix.combo, TRUE, TRUE, 0);
559
  gtk_widget_show (mix.combo);
560

561 562
  if (mix.monochrome_flag)
    gtk_widget_set_sensitive (mix.combo, FALSE);
563

564
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), mix.combo);
565 566 567

  /*........................................................... */

Sven Neumann's avatar
Sven Neumann committed
568
  table = gtk_table_new (3, 4, FALSE);
569 570
  gtk_table_set_row_spacings (GTK_TABLE (table), 6);
  gtk_table_set_col_spacings (GTK_TABLE (table), 6);
571 572
  gtk_container_add (GTK_CONTAINER (frame), table);
  gtk_widget_show (table);
573

Sven Neumann's avatar
Sven Neumann committed
574 575 576 577 578 579
  image = gtk_image_new_from_stock (GIMP_STOCK_CHANNEL_RED,
                                    GTK_ICON_SIZE_BUTTON);
  gtk_table_attach (GTK_TABLE (table), image,
                    0, 1, 0, 1, GTK_FILL, GTK_FILL, 0, 0);
  gtk_widget_show (image);

580
  mix.red_data =
Sven Neumann's avatar
Sven Neumann committed
581
    GTK_ADJUSTMENT (gimp_scale_entry_new (GTK_TABLE (table), 1, 0,
582 583 584 585 586
                                          _("_Red:"), 150, -1,
                                          red_value, -200.0, 200.0,
                                          1.0, 10.0, 1,
                                          TRUE, 0.0, 0.0,
                                          NULL, NULL));
587

588
  g_signal_connect (mix.red_data, "value-changed",
589 590
                    G_CALLBACK (cm_red_scale_callback),
                    &mix);
591

Sven Neumann's avatar
Sven Neumann committed
592 593 594 595 596 597
  image = gtk_image_new_from_stock (GIMP_STOCK_CHANNEL_GREEN,
                                    GTK_ICON_SIZE_BUTTON);
  gtk_table_attach (GTK_TABLE (table), image,
                    0, 1, 1, 2, GTK_FILL, GTK_FILL, 0, 0);
  gtk_widget_show (image);

598
  mix.green_data =
Sven Neumann's avatar
Sven Neumann committed
599
    GTK_ADJUSTMENT (gimp_scale_entry_new (GTK_TABLE (table), 1, 1,
600 601 602 603 604
                                          _("_Green:"), 150, -1,
                                          green_value, -200.0, 200.0,
                                          1.0, 10.0, 1,
                                          TRUE, 0.0, 0.0,
                                          NULL, NULL));
605

606
  g_signal_connect (mix.green_data, "value-changed",
607 608
                    G_CALLBACK (cm_green_scale_callback),
                    &mix);
609 610


Sven Neumann's avatar
Sven Neumann committed
611 612 613 614 615 616
  image = gtk_image_new_from_stock (GIMP_STOCK_CHANNEL_BLUE,
                                    GTK_ICON_SIZE_BUTTON);
  gtk_table_attach (GTK_TABLE (table), image,
                    0, 1, 2, 3, GTK_FILL, GTK_FILL, 0, 0);
  gtk_widget_show (image);

617
  mix.blue_data =
Sven Neumann's avatar
Sven Neumann committed
618
    GTK_ADJUSTMENT (gimp_scale_entry_new (GTK_TABLE (table), 1, 2,
619 620 621 622 623
                                          _("_Blue:"), 150, -1,
                                          blue_value, -200.0, 200.0,
                                          1.0, 10.0, 1,
                                          TRUE, 0.0, 0.0,
                                          NULL, NULL));
624

625
  g_signal_connect (mix.blue_data, "value-changed",
626 627
                    G_CALLBACK (cm_blue_scale_callback),
                    &mix);
628 629

  /*  The monochrome toggle  */
630
  mix.monochrome_toggle = gtk_check_button_new_with_mnemonic (_("_Monochrome"));
631 632
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mix.monochrome_toggle),
                                mix.monochrome_flag);
633
  gtk_box_pack_start (GTK_BOX (main_vbox), mix.monochrome_toggle, FALSE, FALSE, 0);
634 635
  gtk_widget_show (mix.monochrome_toggle);

636 637 638 639
  g_signal_connect (mix.monochrome_toggle, "toggled",
                    G_CALLBACK (cm_monochrome_callback),
                    &mix);

640 641
  /*  The preserve luminosity toggle  */
  mix.preserve_luminosity_toggle =
642
    gtk_check_button_new_with_mnemonic (_("Preserve _luminosity"));
643 644 645
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
                                (mix.preserve_luminosity_toggle),
                                mix.preserve_luminosity_flag);
646
  gtk_box_pack_start (GTK_BOX (main_vbox), mix.preserve_luminosity_toggle,
647
                      FALSE, FALSE, 0);
648 649
  gtk_widget_show (mix.preserve_luminosity_toggle);

650 651 652
  g_signal_connect (mix.preserve_luminosity_toggle, "toggled",
                    G_CALLBACK (cm_preserve_luminosity_callback),
                    &mix);
653 654 655

  /*........................................................... */
  /*  Horizontal box for file i/o  */
656
  hbox = gtk_hbox_new (FALSE, 6);
657
  gtk_box_pack_end (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
658
  gtk_widget_show (hbox);
659 660 661 662 663 664

  button = gtk_button_new_from_stock (GTK_STOCK_OPEN);
  gtk_container_add (GTK_CONTAINER (hbox), button);
  gtk_widget_show (button);

  g_signal_connect (button, "clicked",
665 666 667 668
                    G_CALLBACK (cm_load_file_callback),
                    &mix);

  button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
669 670 671
  gtk_container_add (GTK_CONTAINER (hbox), button);
  gtk_widget_show (button);

672 673 674
  g_signal_connect (button, "clicked",
                    G_CALLBACK (cm_save_file_callback),
                    &mix);
675

676 677 678 679 680 681 682 683
  button = gtk_button_new_from_stock (GIMP_STOCK_RESET);
  gtk_container_add (GTK_CONTAINER (hbox), button);
  gtk_widget_show (button);

  g_signal_connect (button, "clicked",
                    G_CALLBACK (cm_reset_callback),
                    &mix);

684 685
  gtk_widget_show (dialog);

Manish Singh's avatar
Manish Singh committed
686
  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712

  gtk_widget_destroy (dialog);

  return run;
}

static void
cm_red_scale_callback (GtkAdjustment *adjustment,
                       CmParamsType  *mix)
{
  if (mix->monochrome_flag == TRUE)
    mix->black.red_gain = adjustment->value / 100.0;
  else
    switch (mix->output_channel)
      {
      case CM_RED_CHANNEL:
        mix->red.red_gain = adjustment->value / 100.0;
        break;
      case CM_GREEN_CHANNEL:
        mix->green.red_gain = adjustment->value / 100.0;
        break;
      case CM_BLUE_CHANNEL:
        mix->blue.red_gain = adjustment->value / 100.0;
        break;
      }

713
  gimp_preview_invalidate (GIMP_PREVIEW (preview));
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
}

static void
cm_green_scale_callback (GtkAdjustment *adjustment,
                         CmParamsType  *mix)
{
  if (mix->monochrome_flag == TRUE)
    mix->black.green_gain = adjustment->value / 100.0;
  else
    switch (mix->output_channel)
      {
      case CM_RED_CHANNEL:
        mix->red.green_gain = adjustment->value / 100.0;
        break;
      case CM_GREEN_CHANNEL:
        mix->green.green_gain = adjustment->value / 100.0;
        break;
      case CM_BLUE_CHANNEL:
        mix->blue.green_gain = adjustment->value / 100.0;
        break;
      }

736
  gimp_preview_invalidate (GIMP_PREVIEW (preview));
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
}

static void
cm_blue_scale_callback (GtkAdjustment *adjustment,
                        CmParamsType  *mix)
{
  if (mix->monochrome_flag == TRUE)
    mix->black.blue_gain = adjustment->value / 100.0;
  else
    switch (mix->output_channel)
      {
      case CM_RED_CHANNEL:
        mix->red.blue_gain = adjustment->value / 100.0;
        break;
      case CM_GREEN_CHANNEL:
        mix->green.blue_gain = adjustment->value / 100.0;
        break;
      case CM_BLUE_CHANNEL:
        mix->blue.blue_gain = adjustment->value / 100.0;
        break;
      }

759
  gimp_preview_invalidate (GIMP_PREVIEW (preview));
760 761 762
}

static void
763 764
cm_preview (CmParamsType *mix,
            GimpPreview  *preview)
765
{
766 767 768 769 770
  guchar       *dst, *src;
  gint          x, y;
  gint          offset, rowsize;
  gdouble       red_norm, green_norm, blue_norm, black_norm;
  gint          width, height, bpp;
771 772 773
  GimpDrawable *drawable;

  drawable = gimp_zoom_preview_get_drawable (GIMP_ZOOM_PREVIEW (preview));
774 775 776 777 778 779

  red_norm   = cm_calculate_norm (mix, &mix->red);
  green_norm = cm_calculate_norm (mix, &mix->green);
  blue_norm  = cm_calculate_norm (mix, &mix->blue);
  black_norm = cm_calculate_norm (mix, &mix->black);

780 781
  src = gimp_zoom_preview_get_source (GIMP_ZOOM_PREVIEW (preview),
                                      &width, &height, &bpp);
782 783 784

  rowsize = width * bpp;
  dst = g_new (guchar, rowsize * height);
785

786
  offset = 0;
787
  for (y = 0; y < height; y++)
788
    {
789
      for (x = 0; x < width; x++)
790 791 792
        {
          guchar r, g, b;

793 794 795
          r = src[offset + 0];
          g = src[offset + 1];
          b = src[offset + 2];
796 797 798

          if (mix->monochrome_flag == TRUE)
            {
799 800 801
              dst[offset + 0] =
              dst[offset + 1] =
              dst[offset + 2] =
802 803 804 805
                cm_mix_pixel (&mix->black, r, g, b, black_norm);
            }
          else
            {
806
              dst[offset + 0] =
807
                cm_mix_pixel (&mix->red, r, g, b, red_norm);
808
              dst[offset + 1] =
809
                cm_mix_pixel (&mix->green, r, g, b, green_norm);
810
              dst[offset + 2] =
811 812
                cm_mix_pixel (&mix->blue, r, g, b, blue_norm);
            }
813 814
          if (bpp == 4)
            dst[offset + 3] = src[offset + 3];
815

816
          offset += bpp;
817 818
        }
    }
819
  gimp_preview_draw_buffer (GIMP_PREVIEW (preview), dst, bpp * width);
820 821 822 823 824 825 826 827 828 829 830 831

  g_free (dst);
}

static void
cm_monochrome_callback (GtkWidget    *widget,
                        CmParamsType *mix)
{
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
    {
      mix->old_output_channel = mix->output_channel;
      mix->monochrome_flag = TRUE;
832
      gtk_widget_set_sensitive (mix->combo, FALSE);
833 834 835 836 837
    }
  else
    {
      mix->output_channel = mix->old_output_channel;
      mix->monochrome_flag = FALSE;
838
      gtk_widget_set_sensitive (mix->combo, TRUE);
839 840 841 842
    }

  cm_set_adjusters (mix);

843
  gimp_preview_invalidate (GIMP_PREVIEW (preview));
844 845 846 847 848 849 850 851 852 853 854
}

static void
cm_preserve_luminosity_callback (GtkWidget    *widget,
                                 CmParamsType *mix)
{
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
    mix->preserve_luminosity_flag = TRUE;
  else
    mix->preserve_luminosity_flag = FALSE;

855
  gimp_preview_invalidate (GIMP_PREVIEW (preview));
856 857
}

858
static gchar *
859
cm_settings_filename (void)
860
{
861 862 863 864
  return g_build_filename (gimp_directory (),
                           "channel-mixer",
                           "settings",
                           NULL);
865 866
}

867 868 869 870
static void
cm_load_file_callback (GtkWidget    *widget,
                       CmParamsType *mix)
{
871 872 873
  static GtkWidget *dialog = NULL;

  if (! dialog)
874
    {
875
      GtkWidget *parent = gtk_widget_get_toplevel (widget);
876
      gchar     *name;
877 878 879 880 881

      dialog =
        gtk_file_chooser_dialog_new (_("Load Channel Mixer Settings"),
                                     GTK_WINDOW (parent),
                                     GTK_FILE_CHOOSER_ACTION_OPEN,
882

883 884
                                     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                     GTK_STOCK_OPEN,   GTK_RESPONSE_OK,
885

886
                                     NULL);
887

888 889 890 891 892
      gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                               GTK_RESPONSE_OK,
                                               GTK_RESPONSE_CANCEL,
                                               -1);

893
      gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
894 895

      g_signal_connect (dialog, "response",
896
                        G_CALLBACK (cm_load_file_response_callback),
897
                        mix);
898
      g_signal_connect (dialog, "delete-event",
899 900 901
                        G_CALLBACK (gtk_true),
                        NULL);

902 903 904 905
      name = cm_settings_filename ();
      gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (dialog), name);
      g_free (name);
    }
906

907
  gtk_window_present (GTK_WINDOW (dialog));
908 909 910
}

static void
911 912 913
cm_load_file_response_callback (GtkWidget    *dialog,
                                gint          response_id,
                                CmParamsType *mix)
914 915 916 917 918
{
  FILE *fp;

  if (response_id == GTK_RESPONSE_OK)
    {
919
      gchar *filename;
920

921
      filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
922

923
      fp = g_fopen (filename, "r");
924 925

      if (fp)
926 927
        {
          gchar buf[3][CM_LINE_SIZE];
928

929 930 931
          buf[0][0] = '\0';
          buf[1][0] = '\0';
          buf[2][0] = '\0';
932

933
          fgets (buf[0], CM_LINE_SIZE - 1, fp);
934

935 936
          fscanf (fp, "%*s %s", buf[0]);
          if (strcmp (buf[0], "RED") == 0)
937
            mix->output_channel = CM_RED_CHANNEL;
938
          else if (strcmp (buf[0], "GREEN") == 0)
939
            mix->output_channel = CM_GREEN_CHANNEL;
940
          else if (strcmp (buf[0], "BLUE") == 0)
941 942
            mix->output_channel = CM_BLUE_CHANNEL;

943
          fscanf (fp, "%*s %s", buf[0]); /* preview flag, preserved for compatibility */
944

945 946
          fscanf (fp, "%*s %s", buf[0]);
          if (strcmp (buf[0], "TRUE") == 0)
947 948 949 950
            mix->monochrome_flag = TRUE;
          else
            mix->monochrome_flag = FALSE;

951 952
          fscanf (fp, "%*s %s", buf[0]);
          if (strcmp (buf[0], "TRUE") == 0)
953 954 955 956
            mix->preserve_luminosity_flag = TRUE;
          else
            mix->preserve_luminosity_flag = FALSE;

957 958 959 960
          fscanf (fp, "%*s %s %s %s", buf[0], buf[1], buf[2]);
          mix->red.red_gain   = g_ascii_strtod (buf[0], NULL);
          mix->red.green_gain = g_ascii_strtod (buf[1], NULL);
          mix->red.blue_gain  = g_ascii_strtod (buf[2], NULL);
961

962 963 964 965
          fscanf (fp, "%*s %s %s %s", buf[0], buf[1], buf[2]);
          mix->green.red_gain   = g_ascii_strtod (buf[0], NULL);
          mix->green.green_gain = g_ascii_strtod (buf[1], NULL);
          mix->green.blue_gain  = g_ascii_strtod (buf[2], NULL);
966

967 968 969 970
          fscanf (fp, "%*s %s %s %s", buf[0], buf[1], buf[2]);
          mix->blue.red_gain   = g_ascii_strtod (buf[0], NULL);
          mix->blue.green_gain = g_ascii_strtod (buf[1], NULL);
          mix->blue.blue_gain  = g_ascii_strtod (buf[2], NULL);
971

972 973 974 975
          fscanf (fp, "%*s %s %s %s", buf[0], buf[1], buf[2]);
          mix->black.red_gain   = g_ascii_strtod (buf[0], NULL);
          mix->black.green_gain = g_ascii_strtod (buf[1], NULL);
          mix->black.blue_gain  = g_ascii_strtod (buf[2], NULL);
976 977 978

          fclose (fp);

979
          cm_update_ui (mix);
980 981
        }
      else
982 983
        {
          g_message (_("Could not open '%s' for reading: %s"),
984
                     gimp_filename_to_utf8 (filename),
985
                     g_strerror (errno));
986
        }
987 988

      g_free (filename);
989 990
    }

991
  gtk_widget_hide (dialog);
992 993 994 995 996 997
}

static void
cm_save_file_callback (GtkWidget    *widget,
                       CmParamsType *mix)
{
998 999 1000
  static GtkWidget *dialog = NULL;

  if (! dialog)
1001
    {
1002
      GtkWidget *parent = gtk_widget_get_toplevel (widget);
1003
      gchar     *name;
1004

1005 1006 1007 1008
      dialog =
        gtk_file_chooser_dialog_new (_("Save Channel Mixer Settings"),
                                     GTK_WINDOW (parent),
                                     GTK_FILE_CHOOSER_ACTION_SAVE,
1009

1010 1011
                                     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                     GTK_STOCK_SAVE,   GTK_RESPONSE_OK,
1012

1013 1014
                                     NULL);

1015 1016 1017 1018 1019
      gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                               GTK_RESPONSE_OK,
                                               GTK_RESPONSE_CANCEL,
                                               -1);

1020
      gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
1021 1022

      g_signal_connect (dialog, "response",
1023
                        G_CALLBACK (cm_save_file_response_callback),
1024
                        mix);
1025
      g_signal_connect (dialog, "delete-event",
1026 1027 1028
                        G_CALLBACK (gtk_true),
                        NULL);

1029 1030 1031 1032
      name = cm_settings_filename ();
      gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (dialog), name);
      g_free (name);
    }
1033

1034
  gtk_window_present (GTK_WINDOW (dialog));
1035 1036 1037
}

static void
1038 1039 1040
cm_save_file_response_callback (GtkWidget    *dialog,
                                gint          response_id,
                                CmParamsType *mix)
1041
{
1042 1043
  gchar *filename;
  FILE  *file = NULL;
1044 1045 1046

  if (response_id != GTK_RESPONSE_OK)
    {
1047
      gtk_widget_hide (dialog);