gimp/plug-ins/common/channel-mixer.c

1213 lines
38 KiB
C
Raw Normal View History

/****************************************************************************
* This is a plugin for GIMP v 2.0 or later.
*
* Copyright (C) 2002 Martin Guldahl <mguldahl@xmission.com>
* Based on GTK code from:
* homomorphic (Copyright (C) 2001 Valter Marcus Hilden)
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
****************************************************************************/
#include "config.h"
#include <errno.h>
#include <string.h>
#include <glib/gstdio.h>
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include "libgimp/stdplugins-intl.h"
#define PLUG_IN_PROC "plug-in-colors-channel-mixer"
Renamed tons of plug-ins to make more sense and to be consistent: 2008-03-24 Michael Natterer <mitch@gimp.org> Renamed tons of plug-ins to make more sense and to be consistent: * plug-ins/common/AlienMap2.c -> alien-map.c * plug-ins/common/CEL.c -> cel.c * plug-ins/common/CML_explorer.c -> cml-explorer.c * plug-ins/common/align_layers.c -> align-layers.c * plug-ins/common/animationplay.c -> animation-play.c * plug-ins/common/animoptimize.c -> animation-optimize.c * plug-ins/common/apply_lens.c -> lens-apply.c * plug-ins/common/autocrop.c -> crop-auto.c * plug-ins/common/autostretch_hsv.c -> contrast-stretch-hsv.c * plug-ins/common/borderaverage.c -> border-average.c * plug-ins/common/bumpmap.c -> bump-map.c * plug-ins/common/c_astretch.c -> contrast-stretch.c * plug-ins/common/ccanalyze.c -> color-cube-analyze.c * plug-ins/common/channel_mixer.c -> channel-mixer.c * plug-ins/common/color_enhance.c -> color-enhance.c * plug-ins/common/colortoalpha.c -> color-to-alpha.c * plug-ins/common/convmatrix.c -> convolution-matrix.c * plug-ins/common/curve_bend.c -> curve-bend.c * plug-ins/common/depthmerge.c -> depth-merge.c * plug-ins/common/dog.c -> edge-dog.c * plug-ins/common/exchange.c -> color-exchange.c * plug-ins/common/flarefx.c -> lens-flare.c * plug-ins/common/fp.c -> filter-pack.c * plug-ins/common/fractaltrace.c -> fractal-trace.c * plug-ins/common/gauss.c -> blur-gauss.c * plug-ins/common/gee_zoom.c -> gee-zoom.c * plug-ins/common/glasstile.c -> tile-glass.c * plug-ins/common/gqbist.c -> qbist.c * plug-ins/common/gradmap.c -> gradient-map.c * plug-ins/common/laplace.c -> edge-laplace.c * plug-ins/common/lens.c -> lens-distortion.c * plug-ins/common/lic.c -> van-gogh-lic.c * plug-ins/common/max_rgb.c -> max-rgb.c * plug-ins/common/mblur.c -> blur-motion.c * plug-ins/common/nlfilt.c -> nl-filter.c * plug-ins/common/noisify.c -> noise-rgb.c * plug-ins/common/normalize.c -> contrast-normalize.c * plug-ins/common/papertile.c -> tile-paper.c * plug-ins/common/polar.c -> polar-coords.c * plug-ins/common/randomize.c -> noise-randomize.c * plug-ins/common/redeye.c -> red-eye-removal.c * plug-ins/common/retinex.c -> contrast-retinex.c * plug-ins/common/sample_colorize.c -> sample-colorize.c * plug-ins/common/scatter_hsv.c -> noise-hsv.c * plug-ins/common/sel_gauss.c -> blur-gauss-selective.c * plug-ins/common/semiflatten.c -> semi-flatten.c * plug-ins/common/smooth_palette.c -> smooth-palette.c * plug-ins/common/snoise.c -> noise-solid.c * plug-ins/common/sobel.c -> edge-sobel.c * plug-ins/common/spheredesigner.c -> sphere-designer.c * plug-ins/common/spread.c -> noise-spread.c * plug-ins/common/struc.c -> apply-canvas.c * plug-ins/common/threshold_alpha.c -> threshold-alpha.c * plug-ins/common/tileit.c -> tile-small.c * plug-ins/common/tiler.c -> tile-seamless.c * plug-ins/common/uniteditor.c -> unit-editor.c * plug-ins/common/unsharp.c -> unsharp-mask.c * plug-ins/common/vinvert.c -> value-invert.c * plug-ins/common/vpropagate.c -> value-propagate.c * plug-ins/common/webbrowser.c -> web-browser.c * plug-ins/common/whirlpinch.c -> whirl-pinch.c * plug-ins/common/zealouscrop.c -> crop-zealous.c * plug-ins/common/plugin-defs.pl: changed accordingly. * plug-ins/common/Makefile.am: regenerated. svn path=/trunk/; revision=25192
2008-03-24 23:29:55 +08:00
#define PLUG_IN_BINARY "channel-mixer"
#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;
gboolean preserve_luminosity;
CmModeType output_channel;
gboolean preview;
GtkAdjustment *red_data;
GtkAdjustment *green_data;
GtkAdjustment *blue_data;
GtkWidget *combo;
CmModeType old_output_channel;
GtkWidget *monochrome_toggle;
GtkWidget *preserve_luminosity_toggle;
} CmParamsType;
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static void cm_set_defaults (CmParamsType *mix);
static void channel_mixer (CmParamsType *mix,
GimpDrawable *drawable);
static gboolean cm_dialog (CmParamsType *mix,
GimpDrawable *drawable);
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);
static void cm_load_file_response_callback (GtkWidget *dialog,
gint response_id,
CmParamsType *mix);
static void cm_save_file_callback (GtkWidget *widget,
CmParamsType *mix);
static void cm_save_file_response_callback (GtkWidget *dialog,
gint response_id,
CmParamsType *mix);
static void cm_reset_callback (GtkWidget *widget,
CmParamsType *mix);
static void cm_combo_callback (GtkWidget *widget,
CmParamsType *mix);
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);
static void cm_preview (CmParamsType *mix,
GimpPreview *preview);
static void cm_set_adjusters (CmParamsType *mix);
static void cm_update_ui (CmParamsType *mix);
static void cm_save_file (CmParamsType *mix,
FILE *fp);
const GimpPlugInInfo PLUG_IN_INFO =
{
NULL, /* init_proc */
NULL, /* quit_proc */
query, /* query_proc */
run /* run_proc */
};
static GtkWidget *preview;
MAIN ()
static void
query (void)
{
static const GimpParamDef args[] =
{
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
{ GIMP_PDB_IMAGE, "image", "Input image (unused)" },
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
{ GIMP_PDB_INT32, "monochrome", "Monochrome { TRUE, 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" }
};
gimp_install_procedure (PLUG_IN_PROC,
N_("Alter colors by mixing RGB Channels"),
"This plug-in mixes the RGB channels.",
"Martin Guldahl <mguldahl@xmission.com>",
"Martin Guldahl <mguldahl@xmission.com>",
"2002",
N_("Channel Mi_xer..."),
"RGB*",
GIMP_PLUGIN,
G_N_ELEMENTS (args), 0,
args, NULL);
Changed plug-in menu registration again to allow passing just the menu 2004-05-07 Michael Natterer <mitch@gimp.org> Changed plug-in menu registration again to allow passing just the menu item's label (not the full path) in gimp_install_procedure() and only the path (excluding the item's label) in gimp_plugin_menu_register(). Matches the internal action system better and makes translating the menu paths much easier. (Of yourse it's still possible to use the old syntax for backward compatibility). * app/plug-in/plug-in-proc.[ch]: added "gchar *menu_label". * app/plug-in/plug-in-params.[ch]: added new functions plug_in_param_defs_check() and plug_in_proc_args_check() which check if a procedure's parameters match its menu location (e.g. <Image> needs RUN-MODE, IMAGE, DRAWABLE). * app/plug-in/plug-in-message.c (plug_in_handle_proc_install): if registering an old-style (full) menu_path, use plug_in_param_defs_check(), set proc_def->menu_label otherwise. * tools/pdbgen/pdb/plug_in.pdb (plugin_menu_register): use plug_in_proc_args_check() on the passed menu_path and make sugre old and new style menu registration are not mixed. * app/pdb/plug_in_cmds.c: regenerated. * app/plug-in/plug-in-rc.c: save/restore "menu_label". * app/actions/file-dialog-actions.c * app/actions/plug-in-actions.c * app/menus/plug-in-menus.c: changed action/menu creation accordingly. Some hacks needed to allow both old and new style menu_label/menu_paths. * app/plug-in/plug-in.c * app/widgets/gimpfiledialog.c * app/xcf/xcf.c: changed accordingly. * plug-ins/common/align_layers.c * plug-ins/common/animationplay.c * plug-ins/common/animoptimize.c * plug-ins/common/apply_lens.c * plug-ins/common/autocrop.c * plug-ins/common/autostretch_hsv.c * plug-ins/common/blinds.c * plug-ins/common/blur.c * plug-ins/common/borderaverage.c * plug-ins/common/bumpmap.c * plug-ins/common/c_astretch.c * plug-ins/common/ccanalyze.c * plug-ins/common/channel_mixer.c * plug-ins/common/checkerboard.c * plug-ins/common/color_enhance.c * plug-ins/common/colorify.c * plug-ins/common/colortoalpha.c * plug-ins/common/compose.c * plug-ins/common/convmatrix.c * plug-ins/common/cubism.c * plug-ins/common/curve_bend.c * plug-ins/common/decompose.c * plug-ins/common/deinterlace.c * plug-ins/common/depthmerge.c * plug-ins/common/destripe.c * plug-ins/common/diffraction.c * plug-ins/common/displace.c * plug-ins/common/edge.c * plug-ins/common/emboss.c * plug-ins/common/engrave.c * plug-ins/common/exchange.c * plug-ins/common/film.c * plug-ins/common/flarefx.c * plug-ins/common/fractaltrace.c * plug-ins/common/screenshot.c: ported the first few plug-ins to the new registration scheme.
2004-05-07 08:30:24 +08:00
gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Colors/Components");
}
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;
CmParamsType mix;
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))
{
cm_set_defaults (&mix);
mix.preview = TRUE;
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
gimp_get_data (PLUG_IN_PROC, &mix);
if (! cm_dialog (&mix, drawable))
{
gimp_drawable_detach (drawable);
return;
}
break;
case GIMP_RUN_NONINTERACTIVE:
mix.monochrome = param[3].data.d_int32;
if (mix.monochrome)
{
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:
gimp_get_data (PLUG_IN_PROC, &mix);
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 (_("Channel Mixer"));
channel_mixer (&mix, drawable);
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
if (run_mode == GIMP_RUN_INTERACTIVE)
gimp_set_data (PLUG_IN_PROC, &mix, sizeof (CmParamsType));
}
}
else
{
status = GIMP_PDB_EXECUTION_ERROR;
}
values[0].data.d_status = status;
gimp_drawable_detach (drawable);
}
static void
cm_set_defaults (CmParamsType *mix)
{
const CmParamsType defaults =
{
{ 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));
}
static gdouble
cm_calculate_norm (CmParamsType *mix,
CmChannelType *ch)
{
gdouble sum = ch->red_gain + ch->green_gain + ch->blue_gain;
if (sum == 0.0 || ! mix->preserve_luminosity)
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;
return (guchar) CLAMP0255 (c);
}
static inline void
cm_process_pixel (CmParamsType *mix,
const guchar *s,
guchar *d,
const gdouble red_norm,
const gdouble green_norm,
const gdouble blue_norm,
const gdouble black_norm)
{
if (mix->monochrome)
{
d[0] = d[1] = d[2] =
cm_mix_pixel (&mix->black, s[0], s[1], s[2], black_norm);
}
else
{
d[0] = cm_mix_pixel (&mix->red, s[0], s[1], s[2], red_norm);
d[1] = cm_mix_pixel (&mix->green, s[0], s[1], s[2], green_norm);
d[2] = cm_mix_pixel (&mix->blue, s[0], s[1], s[2], blue_norm);
}
}
static void
channel_mixer (CmParamsType *mix,
GimpDrawable *drawable)
{
GimpPixelRgn src_rgn, dest_rgn;
gpointer pr;
gboolean has_alpha;
gdouble red_norm, green_norm, blue_norm, black_norm;
gint i, total, processed = 0;
gint x1, y1;
gint width, height;
if (! gimp_drawable_mask_intersect (drawable->drawable_id,
&x1, &y1, &width, &height))
return;
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);
has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
gimp_pixel_rgn_init (&src_rgn, drawable,
x1, y1, width, height, FALSE, FALSE);
gimp_pixel_rgn_init (&dest_rgn, drawable,
x1, y1, width, height, TRUE, TRUE);
total = width * height;
for (pr = gimp_pixel_rgns_register (2, &src_rgn, &dest_rgn), i = 0;
pr != NULL;
pr = gimp_pixel_rgns_process (pr), i++)
{
const guchar *src = src_rgn.data;
guchar *dest = dest_rgn.data;
gint x, y;
for (y = 0; y < src_rgn.h; y++)
{
const guchar *s = src;
guchar *d = dest;
if (has_alpha)
{
for (x = 0; x < src_rgn.w; x++, s += 4, d += 4)
{
cm_process_pixel (mix, s, d,
red_norm, green_norm, blue_norm,
black_norm);
d[3] = s[3];
}
}
else
{
for (x = 0; x < src_rgn.w; x++, s += 3, d += 3)
{
cm_process_pixel (mix, s, d,
red_norm, green_norm, blue_norm,
black_norm);
}
}
src += src_rgn.rowstride;
dest += dest_rgn.rowstride;
}
processed += src_rgn.w * src_rgn.h;
if (i % 16 == 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, x1, y1, width, height);
}
static gboolean
cm_dialog (CmParamsType *mix,
GimpDrawable *drawable)
{
GtkWidget *dialog;
GtkWidget *main_vbox;
GtkWidget *vbox;
GtkWidget *frame;
GtkWidget *hbox;
GtkWidget *button;
GtkWidget *label;
GtkWidget *image;
GtkWidget *table;
gdouble red_value, green_value, blue_value;
gboolean run;
gimp_ui_init (PLUG_IN_BINARY, FALSE);
/* get values */
if (mix->monochrome)
{
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 ();
red_value = green_value = blue_value = 0.0;
break;
}
}
dialog = gimp_dialog_new (_("Channel Mixer"), PLUG_IN_BINARY,
NULL, 0,
gimp_standard_help_func, PLUG_IN_PROC,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OK, GTK_RESPONSE_OK,
NULL);
gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
GTK_RESPONSE_OK,
GTK_RESPONSE_CANCEL,
-1);
Added parent window API to the GimpProgress interface and to the libgimp 2005-09-09 Michael Natterer <mitch@gimp.org> Added parent window API to the GimpProgress interface and to the libgimp progress stuff. Might look strange, but does the right thing in almost all cases (image window, file dialog, script-fu dialog etc). Fixes bug #62988. * app/core/gimpprogress.[ch]: added GimpProgress::get_window() which should return a toplevel window ID if the progress is in a window that wants to be the transient parent of plug-in dialogs. * app/widgets/gimpwidgets-utils.[ch] (gimp_window_get_native): new function which returns the window handle of a GtkWindow's GdkWindow. * app/widgets/gimpfiledialog.c: implement ::get_window(). * app/display/gimpdisplay.[ch]: ditto. Removed window handle API. * app/gui/gui-vtable.c: changed accordingly. * libgimpbase/gimpbaseenums.[ch] (enum GimpProgressCommand): added GIMP_PROGRESS_COMMAND_GET_WINDOW. * app/plug-in/plug-in-progress.[ch] (plug_in_progress_get_window): new function. Also renamed some functions to match the GimpProgress interface, and not the legacy PDB procedure names. * tools/pdbgen/pdb/progress.pdb * app/core/gimppdbprogress.c: implement get_window() on both sides of the wire, keeping backward compatibility (hopefully). * libgimp/gimpprogress.[ch]: deprecated gimp_progress_install() and added gimp_progress_install_vtable() which takes a vtable with padding to be extensible. Added get_window() vtable entry and dispatch it accordingly. Also added pulse() which was implemented in a hackish way before. Everything is of course backward compatible. * libgimp/gimpprogressbar.c: inmplement the get_window() stuff so a plug-in dialog containing a progress can be the transient parent of another dialog in another plug-in. * libgimp/gimpui.[ch] (gimp_ui_get_progress_window): new function which returns a foreign GdkWindow of this plug-ins progress window. Renamed gimp_window_set_transient_for_default_display() to gimp_window_set_transient() and make it use the progress' window handle instead of the display's (which is the right thing to do in almost all cases). * libgimp/gimp.def * libgimp/gimpui.def: add the new functions. * tools/pdbgen/enums.pl * app/pdb/internal_procs.c * app/pdb/progress_cmds.c * libgimp/gimpprogress_pdb.[ch]: regenerated. * libgimp/gimpexport.c * plug-ins/*/*.c: follow API change.
2005-09-10 02:07:31 +08:00
gimp_window_set_transient (GTK_WINDOW (dialog));
main_vbox = gtk_vbox_new (FALSE, 12);
gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
main_vbox);
gtk_widget_show (main_vbox);
preview = gimp_zoom_preview_new (drawable);
gtk_box_pack_start (GTK_BOX (main_vbox), preview, TRUE, TRUE, 0);
gtk_widget_show (preview);
g_signal_connect_swapped (preview, "invalidated",
G_CALLBACK (cm_preview),
mix);
frame = gimp_frame_new (NULL);
gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
gtk_widget_show (frame);
hbox = gtk_hbox_new (FALSE, 6);
gtk_frame_set_label_widget (GTK_FRAME (frame), hbox);
gtk_widget_show (hbox);
label = gtk_label_new_with_mnemonic (_("O_utput channel:"));
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
gtk_widget_show (label);
mix->combo = g_object_new (GIMP_TYPE_INT_COMBO_BOX, NULL);
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);
gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (mix->combo),
mix->output_channel);
g_signal_connect (mix->combo, "changed",
G_CALLBACK (cm_combo_callback),
mix);
gtk_box_pack_start (GTK_BOX (hbox), mix->combo, TRUE, TRUE, 0);
gtk_widget_show (mix->combo);
if (mix->monochrome)
gtk_widget_set_sensitive (mix->combo, FALSE);
gtk_label_set_mnemonic_widget (GTK_LABEL (label), mix->combo);
/*........................................................... */
table = gtk_table_new (3, 4, FALSE);
gtk_table_set_row_spacings (GTK_TABLE (table), 6);
gtk_table_set_col_spacings (GTK_TABLE (table), 6);
gtk_container_add (GTK_CONTAINER (frame), table);
gtk_widget_show (table);
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);
mix->red_data =
GTK_ADJUSTMENT (gimp_scale_entry_new (GTK_TABLE (table), 1, 0,
_("_Red:"), 150, -1,
red_value, -200.0, 200.0,
1.0, 10.0, 1,
TRUE, 0.0, 0.0,
NULL, NULL));
g_signal_connect (mix->red_data, "value-changed",
G_CALLBACK (cm_red_scale_callback),
mix);
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);
mix->green_data =
GTK_ADJUSTMENT (gimp_scale_entry_new (GTK_TABLE (table), 1, 1,
_("_Green:"), 150, -1,
green_value, -200.0, 200.0,
1.0, 10.0, 1,
TRUE, 0.0, 0.0,
NULL, NULL));
g_signal_connect (mix->green_data, "value-changed",
G_CALLBACK (cm_green_scale_callback),
mix);
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);
mix->blue_data =
GTK_ADJUSTMENT (gimp_scale_entry_new (GTK_TABLE (table), 1, 2,
_("_Blue:"), 150, -1,
blue_value, -200.0, 200.0,
1.0, 10.0, 1,
TRUE, 0.0, 0.0,
NULL, NULL));
g_signal_connect (mix->blue_data, "value-changed",
G_CALLBACK (cm_blue_scale_callback),
mix);
vbox = gtk_vbox_new (6, FALSE);
gtk_box_pack_start (GTK_BOX (main_vbox), vbox, FALSE, FALSE, 0);
gtk_widget_show (vbox);
/* The monochrome toggle */
mix->monochrome_toggle =
gtk_check_button_new_with_mnemonic (_("_Monochrome"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mix->monochrome_toggle),
mix->monochrome);
gtk_box_pack_start (GTK_BOX (vbox), mix->monochrome_toggle,
FALSE, FALSE, 0);
gtk_widget_show (mix->monochrome_toggle);
g_signal_connect (mix->monochrome_toggle, "toggled",
G_CALLBACK (cm_monochrome_callback),
mix);
/* The preserve luminosity toggle */
mix->preserve_luminosity_toggle =
gtk_check_button_new_with_mnemonic (_("Preserve _luminosity"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
(mix->preserve_luminosity_toggle),
mix->preserve_luminosity);
gtk_box_pack_start (GTK_BOX (vbox), mix->preserve_luminosity_toggle,
FALSE, FALSE, 0);
gtk_widget_show (mix->preserve_luminosity_toggle);
g_signal_connect (mix->preserve_luminosity_toggle, "toggled",
G_CALLBACK (cm_preserve_luminosity_callback),
mix);
/*........................................................... */
/* Horizontal box for file i/o */
hbox = gtk_hbox_new (FALSE, 6);
gtk_box_pack_end (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
gtk_widget_show (hbox);
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",
G_CALLBACK (cm_load_file_callback),
mix);
button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
gtk_container_add (GTK_CONTAINER (hbox), button);
gtk_widget_show (button);
g_signal_connect (button, "clicked",
G_CALLBACK (cm_save_file_callback),
mix);
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);
gtk_widget_show (dialog);
run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
gtk_widget_destroy (dialog);
return run;
}
static void
cm_red_scale_callback (GtkAdjustment *adjustment,
CmParamsType *mix)
{
if (mix->monochrome)
{
mix->black.red_gain = gtk_adjustment_get_value (adjustment) / 100.0;
}
else
{
switch (mix->output_channel)
{
case CM_RED_CHANNEL:
mix->red.red_gain = gtk_adjustment_get_value (adjustment) / 100.0;
break;
case CM_GREEN_CHANNEL:
mix->green.red_gain = gtk_adjustment_get_value (adjustment) / 100.0;
break;
case CM_BLUE_CHANNEL:
mix->blue.red_gain = gtk_adjustment_get_value (adjustment) / 100.0;
break;
}
}
gimp_preview_invalidate (GIMP_PREVIEW (preview));
}
static void
cm_green_scale_callback (GtkAdjustment *adjustment,
CmParamsType *mix)
{
if (mix->monochrome)
{
mix->black.green_gain = gtk_adjustment_get_value (adjustment) / 100.0;
}
else
{
switch (mix->output_channel)
{
case CM_RED_CHANNEL:
mix->red.green_gain = gtk_adjustment_get_value (adjustment) / 100.0;
break;
case CM_GREEN_CHANNEL:
mix->green.green_gain = gtk_adjustment_get_value (adjustment) / 100.0;
break;
case CM_BLUE_CHANNEL:
mix->blue.green_gain = gtk_adjustment_get_value (adjustment) / 100.0;
break;
}
}
gimp_preview_invalidate (GIMP_PREVIEW (preview));
}
static void
cm_blue_scale_callback (GtkAdjustment *adjustment,
CmParamsType *mix)
{
if (mix->monochrome)
{
mix->black.blue_gain = gtk_adjustment_get_value (adjustment) / 100.0;
}
else
{
switch (mix->output_channel)
{
case CM_RED_CHANNEL:
mix->red.blue_gain = gtk_adjustment_get_value (adjustment) / 100.0;
break;
case CM_GREEN_CHANNEL:
mix->green.blue_gain = gtk_adjustment_get_value (adjustment) / 100.0;
break;
case CM_BLUE_CHANNEL:
mix->blue.blue_gain = gtk_adjustment_get_value (adjustment) / 100.0;
break;
}
}
gimp_preview_invalidate (GIMP_PREVIEW (preview));
}
static void
cm_preview (CmParamsType *mix,
GimpPreview *preview)
{
guchar *src, *s;
guchar *dst, *d;
gint x, y;
gdouble red_norm, green_norm, blue_norm, black_norm;
gint width, height, bpp;
GimpDrawable *drawable;
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);
drawable = gimp_zoom_preview_get_drawable (GIMP_ZOOM_PREVIEW (preview));
src = s = gimp_zoom_preview_get_source (GIMP_ZOOM_PREVIEW (preview),
&width, &height, &bpp);
dst = d = g_new (guchar, width * height * bpp);
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++, s += bpp, d += bpp)
{
cm_process_pixel (mix, s, d,
red_norm, green_norm, blue_norm,
black_norm);
if (bpp == 4)
d[3] = s[3];
}
}
gimp_preview_draw_buffer (GIMP_PREVIEW (preview), dst, bpp * width);
g_free (src);
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 = TRUE;
gtk_widget_set_sensitive (mix->combo, FALSE);
}
else
{
mix->output_channel = mix->old_output_channel;
mix->monochrome = FALSE;
gtk_widget_set_sensitive (mix->combo, TRUE);
}
cm_set_adjusters (mix);
gimp_preview_invalidate (GIMP_PREVIEW (preview));
}
static void
cm_preserve_luminosity_callback (GtkWidget *widget,
CmParamsType *mix)
{
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
mix->preserve_luminosity = TRUE;
else
mix->preserve_luminosity = FALSE;
gimp_preview_invalidate (GIMP_PREVIEW (preview));
}
static gchar *
cm_settings_filename (void)
{
return g_build_filename (gimp_directory (),
"channel-mixer",
"settings",
NULL);
}
static void
cm_load_file_callback (GtkWidget *widget,
CmParamsType *mix)
{
static GtkWidget *dialog = NULL;
if (! dialog)
{
GtkWidget *parent = gtk_widget_get_toplevel (widget);
gchar *name;
dialog =
gtk_file_chooser_dialog_new (_("Load Channel Mixer Settings"),
GTK_WINDOW (parent),
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_OK,
NULL);
gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
GTK_RESPONSE_OK,
GTK_RESPONSE_CANCEL,
-1);
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
g_signal_connect (dialog, "response",
G_CALLBACK (cm_load_file_response_callback),
mix);
g_signal_connect (dialog, "delete-event",
G_CALLBACK (gtk_true),
NULL);
name = cm_settings_filename ();
gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (dialog), name);
g_free (name);
}
gtk_window_present (GTK_WINDOW (dialog));
}
static void
cm_load_file_response_callback (GtkWidget *dialog,
gint response_id,
CmParamsType *mix)
{
FILE *fp;
if (response_id == GTK_RESPONSE_OK)
{
gchar *filename;
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
fp = g_fopen (filename, "rb");
if (fp)
{
gchar buf[3][CM_LINE_SIZE];
buf[0][0] = '\0';
buf[1][0] = '\0';
buf[2][0] = '\0';
fgets (buf[0], CM_LINE_SIZE - 1, fp);
fscanf (fp, "%*s %1023s", buf[0]);
if (strcmp (buf[0], "RED") == 0)
mix->output_channel = CM_RED_CHANNEL;
else if (strcmp (buf[0], "GREEN") == 0)
mix->output_channel = CM_GREEN_CHANNEL;
else if (strcmp (buf[0], "BLUE") == 0)
mix->output_channel = CM_BLUE_CHANNEL;
fscanf (fp, "%*s %1023s", buf[0]); /* preview flag, preserved for compatibility */
fscanf (fp, "%*s %1023s", buf[0]);
if (strcmp (buf[0], "TRUE") == 0)
mix->monochrome = TRUE;
else
mix->monochrome = FALSE;
fscanf (fp, "%*s %1023s", buf[0]);
if (strcmp (buf[0], "TRUE") == 0)
mix->preserve_luminosity = TRUE;
else
mix->preserve_luminosity = FALSE;
fscanf (fp, "%*s %1023s %1023s %1023s", 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);
fscanf (fp, "%*s %1023s %1023s %1023s", 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);
fscanf (fp, "%*s %1023s %1023s %1023s", 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);
fscanf (fp, "%*s %1023s %1023s %1023s", 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);
fclose (fp);
cm_update_ui (mix);
}
else
{
g_message (_("Could not open '%s' for reading: %s"),
gimp_filename_to_utf8 (filename),
g_strerror (errno));
}
g_free (filename);
}
gtk_widget_hide (dialog);
}
static void
cm_save_file_callback (GtkWidget *widget,
CmParamsType *mix)
{
static GtkWidget *dialog = NULL;
if (! dialog)
{
GtkWidget *parent = gtk_widget_get_toplevel (widget);
gchar *name;
dialog =
gtk_file_chooser_dialog_new (_("Save Channel Mixer Settings"),
GTK_WINDOW (parent),
GTK_FILE_CHOOSER_ACTION_SAVE,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_SAVE, GTK_RESPONSE_OK,
NULL);
gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
GTK_RESPONSE_OK,
GTK_RESPONSE_CANCEL,
-1);
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
TRUE);
g_signal_connect (dialog, "response",
G_CALLBACK (cm_save_file_response_callback),
mix);
g_signal_connect (dialog, "delete-event",
G_CALLBACK (gtk_true),
NULL);
name = cm_settings_filename ();
gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (dialog), name);
g_free (name);
}
gtk_window_present (GTK_WINDOW (dialog));
}
static void
cm_save_file_response_callback (GtkWidget *dialog,
gint response_id,
CmParamsType *mix)
{
gchar *filename;
FILE *file = NULL;
if (response_id != GTK_RESPONSE_OK)
{
gtk_widget_hide (dialog);
return;
}
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
if (! filename)
return;
file = g_fopen (filename, "wb");
if (! file)
{
g_message (_("Could not open '%s' for writing: %s"),
2004-01-19 11:06:04 +08:00
gimp_filename_to_utf8 (filename), g_strerror (errno));
g_free (filename);
return;
}
cm_save_file (mix, file);
g_message (_("Parameters were saved to '%s'"),
2004-01-19 11:06:04 +08:00
gimp_filename_to_utf8 (filename));
gtk_widget_hide (dialog);
}
static void
cm_save_file (CmParamsType *mix,
FILE *fp)
{
const gchar *str = NULL;
gchar buf[3][G_ASCII_DTOSTR_BUF_SIZE];
switch (mix->output_channel)
{
case CM_RED_CHANNEL:
str = "RED";
break;
case CM_GREEN_CHANNEL:
str = "GREEN";
break;
case CM_BLUE_CHANNEL:
str = "BLUE";
break;
default:
g_assert_not_reached ();
break;
}
fprintf (fp, "# Channel Mixer Configuration File\n");
fprintf (fp, "CHANNEL: %s\n", str);
fprintf (fp, "PREVIEW: %s\n", "TRUE"); /* preserved for compatibility */
fprintf (fp, "MONOCHROME: %s\n",
mix->monochrome ? "TRUE" : "FALSE");
fprintf (fp, "PRESERVE_LUMINOSITY: %s\n",
mix->preserve_luminosity ? "TRUE" : "FALSE");
fprintf (fp, "RED: %s %s %s\n",
g_ascii_formatd (buf[0], sizeof (buf[0]), "%5.3f",
mix->red.red_gain),
g_ascii_formatd (buf[1], sizeof (buf[1]), "%5.3f",
mix->red.green_gain),
g_ascii_formatd (buf[2], sizeof (buf[2]), "%5.3f",
mix->red.blue_gain));
fprintf (fp, "GREEN: %s %s %s\n",
g_ascii_formatd (buf[0], sizeof (buf[0]), "%5.3f",
mix->green.red_gain),
g_ascii_formatd (buf[1], sizeof (buf[1]), "%5.3f",
mix->green.green_gain),
g_ascii_formatd (buf[2], sizeof (buf[2]), "%5.3f",
mix->green.blue_gain));
fprintf (fp, "BLUE: %s %s %s\n",
g_ascii_formatd (buf[0], sizeof (buf[0]), "%5.3f",
mix->blue.red_gain),
g_ascii_formatd (buf[1], sizeof (buf[1]), "%5.3f",
mix->blue.green_gain),
g_ascii_formatd (buf[2], sizeof (buf[2]), "%5.3f",
mix->blue.blue_gain));
fprintf (fp, "BLACK: %s %s %s\n",
g_ascii_formatd (buf[0], sizeof (buf[0]), "%5.3f",
mix->black.red_gain),
g_ascii_formatd (buf[1], sizeof (buf[1]), "%5.3f",
mix->black.green_gain),
g_ascii_formatd (buf[2], sizeof (buf[2]), "%5.3f",
mix->black.blue_gain));
fclose (fp);
}
static void
cm_reset_callback (GtkWidget *widget,
CmParamsType *mix)
{
cm_set_defaults (mix);
cm_update_ui (mix);
}
static void
cm_combo_callback (GtkWidget *widget,
CmParamsType *mix)
{
gint value;
if (gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget), &value))
{
mix->output_channel = value;
cm_set_adjusters (mix);
}
}
static void
cm_set_adjusters (CmParamsType *mix)
{
if (mix->monochrome)
{
gtk_adjustment_set_value (mix->red_data, mix->black.red_gain * 100.0);
gtk_adjustment_set_value (mix->green_data, mix->black.green_gain * 100.0);
gtk_adjustment_set_value (mix->blue_data, mix->black.blue_gain * 100.0);
return;
}
switch (mix->output_channel)
{
case CM_RED_CHANNEL:
gtk_adjustment_set_value (mix->red_data, mix->red.red_gain * 100.0);
gtk_adjustment_set_value (mix->green_data, mix->red.green_gain * 100.0);
gtk_adjustment_set_value (mix->blue_data, mix->red.blue_gain * 100.0);
break;
case CM_GREEN_CHANNEL:
gtk_adjustment_set_value (mix->red_data, mix->green.red_gain * 100.0);
gtk_adjustment_set_value (mix->green_data, mix->green.green_gain * 100.0);
gtk_adjustment_set_value (mix->blue_data, mix->green.blue_gain * 100.0);
break;
case CM_BLUE_CHANNEL:
gtk_adjustment_set_value (mix->red_data, mix->blue.red_gain * 100.0);
gtk_adjustment_set_value (mix->green_data, mix->blue.green_gain * 100.0);
gtk_adjustment_set_value (mix->blue_data, mix->blue.blue_gain * 100.0);
break;
}
}
static void
cm_update_ui (CmParamsType *mix)
{
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mix->monochrome_toggle),
mix->monochrome);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mix->preserve_luminosity_toggle),
mix->preserve_luminosity);
gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (mix->combo),
mix->output_channel);
cm_set_adjusters (mix);
gimp_preview_invalidate (GIMP_PREVIEW (preview));
}