Gimp/plug-ins/common/threshold_alpha.c

274 lines
6.7 KiB
C
Raw Normal View History

1997-11-24 14:05:25 -08:00
/* threshold_alpha.c -- This is a plug-in for the GIMP (1.0's API)
* Author: Shuji Narazaki <narazaki@InetQ.or.jp>
* Time-stamp: <2000-01-09 13:25:30 yasuhiro>
* Version: 0.13A (the 'A' is for Adam who hacked in greyscale
* support - don't know if there's a more recent official
* version)
1997-11-24 14:05:25 -08:00
*
* Copyright (C) 1997 Shuji Narazaki <narazaki@InetQ.or.jp>
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1997-11-24 14:05:25 -08:00
*/
#include "config.h"
1997-11-24 14:05:25 -08:00
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include "libgimp/stdplugins-intl.h"
#define PLUG_IN_NAME "plug_in_threshold_alpha"
#define SHORT_NAME "threshold_alpha"
#define PROGRESS_UPDATE_NUM 100
#define SCALE_WIDTH 120
use the passed Gimp pointer instead of using "the_gimp". 2001-12-03 Michael Natterer <mitch@gimp.org> * app/devices.c: use the passed Gimp pointer instead of using "the_gimp". * app/base/temp-buf.c: indentation. * app/gui/preferences-dialog.c: prefs_toggle_callback(): fixed segfault when trying to find the prefs_dlg widget from a menu item callback (Fixes #65757). * app/gui/offset-dialog.[ch]: fixed public prototype, include the header in the .c file. * app/gui/menus.c: some menu cleanup: moved all functions which operate on the active layer/drawable to <Image>/Layer. Renamed "Layers" to "Layer". * app/display/gimpdisplayshell.c: changed menu update function accordingly. * app/gui/image-commands.[ch] * app/gui/layers-commands.[ch]: moved stuff from image-commands.* to layers-commads.*- * app/tools/gimpblendtool.c * app/tools/gimpbrightnesscontrasttool.c * app/tools/gimpcolorbalancetool.c * app/tools/gimpcurvestool.c * app/tools/gimphistogramtool.c * app/tools/gimphuesaturationtool.c * app/tools/gimplevelstool.c * app/tools/gimpposterizetool.c * app/tools/gimpthresholdtool.c * app/tools/paint_options.c * app/tools/transform_options.c * plug-ins/common/align_layers.c * plug-ins/common/autocrop.c * plug-ins/common/autostretch_hsv.c * plug-ins/common/c_astretch.c * plug-ins/common/color_enhance.c * plug-ins/common/guillotine.c * plug-ins/common/normalize.c * plug-ins/common/rotate.c * plug-ins/common/threshold_alpha.c * plug-ins/common/zealouscrop.c * plug-ins/rcm/rcm.c * plug-ins/fp/fp.c: register under <Image>/Layer, some cosmetic fixes.
2001-12-03 09:59:48 -08:00
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static GimpPDBStatusType threshold_alpha (gint32 drawable_id);
static gint threshold_alpha_dialog (void);
static void threshold_alpha_ok_callback (GtkWidget *widget,
gpointer data);
1997-11-24 14:05:25 -08:00
use the passed Gimp pointer instead of using "the_gimp". 2001-12-03 Michael Natterer <mitch@gimp.org> * app/devices.c: use the passed Gimp pointer instead of using "the_gimp". * app/base/temp-buf.c: indentation. * app/gui/preferences-dialog.c: prefs_toggle_callback(): fixed segfault when trying to find the prefs_dlg widget from a menu item callback (Fixes #65757). * app/gui/offset-dialog.[ch]: fixed public prototype, include the header in the .c file. * app/gui/menus.c: some menu cleanup: moved all functions which operate on the active layer/drawable to <Image>/Layer. Renamed "Layers" to "Layer". * app/display/gimpdisplayshell.c: changed menu update function accordingly. * app/gui/image-commands.[ch] * app/gui/layers-commands.[ch]: moved stuff from image-commands.* to layers-commads.*- * app/tools/gimpblendtool.c * app/tools/gimpbrightnesscontrasttool.c * app/tools/gimpcolorbalancetool.c * app/tools/gimpcurvestool.c * app/tools/gimphistogramtool.c * app/tools/gimphuesaturationtool.c * app/tools/gimplevelstool.c * app/tools/gimpposterizetool.c * app/tools/gimpthresholdtool.c * app/tools/paint_options.c * app/tools/transform_options.c * plug-ins/common/align_layers.c * plug-ins/common/autocrop.c * plug-ins/common/autostretch_hsv.c * plug-ins/common/c_astretch.c * plug-ins/common/color_enhance.c * plug-ins/common/guillotine.c * plug-ins/common/normalize.c * plug-ins/common/rotate.c * plug-ins/common/threshold_alpha.c * plug-ins/common/zealouscrop.c * plug-ins/rcm/rcm.c * plug-ins/fp/fp.c: register under <Image>/Layer, some cosmetic fixes.
2001-12-03 09:59:48 -08:00
static GimpRunMode run_mode;
GimpPlugInInfo PLUG_IN_INFO =
1997-11-24 14:05:25 -08:00
{
NULL, /* init_proc */
NULL, /* quit_proc */
query, /* query_proc */
run, /* run_proc */
1997-11-24 14:05:25 -08:00
};
typedef struct
{
gint threshold;
} ValueType;
static ValueType VALS =
{
127
};
typedef struct
{
gboolean run;
1997-11-24 14:05:25 -08:00
} Interface;
static Interface INTERFACE =
{
FALSE
};
1997-11-24 14:05:25 -08:00
MAIN ()
static void
query (void)
1997-11-24 14:05:25 -08:00
{
static GimpParamDef args [] =
1997-11-24 14:05:25 -08:00
{
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive"},
{ GIMP_PDB_IMAGE, "image", "Input image (not used)"},
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
{ GIMP_PDB_INT32, "threshold", "Threshold" }
1997-11-24 14:05:25 -08:00
};
1997-11-24 14:05:25 -08:00
gimp_install_procedure (PLUG_IN_NAME,
"",
"",
"Shuji Narazaki (narazaki@InetQ.or.jp)",
"Shuji Narazaki",
"1997",
N_("<Image>/Layer/Transparency/_Threshold Alpha..."),
1997-11-24 14:05:25 -08:00
"RGBA,GRAYA",
GIMP_PLUGIN,
use the passed Gimp pointer instead of using "the_gimp". 2001-12-03 Michael Natterer <mitch@gimp.org> * app/devices.c: use the passed Gimp pointer instead of using "the_gimp". * app/base/temp-buf.c: indentation. * app/gui/preferences-dialog.c: prefs_toggle_callback(): fixed segfault when trying to find the prefs_dlg widget from a menu item callback (Fixes #65757). * app/gui/offset-dialog.[ch]: fixed public prototype, include the header in the .c file. * app/gui/menus.c: some menu cleanup: moved all functions which operate on the active layer/drawable to <Image>/Layer. Renamed "Layers" to "Layer". * app/display/gimpdisplayshell.c: changed menu update function accordingly. * app/gui/image-commands.[ch] * app/gui/layers-commands.[ch]: moved stuff from image-commands.* to layers-commads.*- * app/tools/gimpblendtool.c * app/tools/gimpbrightnesscontrasttool.c * app/tools/gimpcolorbalancetool.c * app/tools/gimpcurvestool.c * app/tools/gimphistogramtool.c * app/tools/gimphuesaturationtool.c * app/tools/gimplevelstool.c * app/tools/gimpposterizetool.c * app/tools/gimpthresholdtool.c * app/tools/paint_options.c * app/tools/transform_options.c * plug-ins/common/align_layers.c * plug-ins/common/autocrop.c * plug-ins/common/autostretch_hsv.c * plug-ins/common/c_astretch.c * plug-ins/common/color_enhance.c * plug-ins/common/guillotine.c * plug-ins/common/normalize.c * plug-ins/common/rotate.c * plug-ins/common/threshold_alpha.c * plug-ins/common/zealouscrop.c * plug-ins/rcm/rcm.c * plug-ins/fp/fp.c: register under <Image>/Layer, some cosmetic fixes.
2001-12-03 09:59:48 -08:00
G_N_ELEMENTS (args), 0,
args, NULL);
1997-11-24 14:05:25 -08:00
}
static void
run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals)
1997-11-24 14:05:25 -08:00
{
static GimpParam values[1];
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
gint drawable_id;
1997-11-24 14:05:25 -08:00
run_mode = param[0].data.d_int32;
1997-11-24 14:05:25 -08:00
drawable_id = param[2].data.d_int32;
INIT_I18N ();
1997-11-24 14:05:25 -08:00
*nreturn_vals = 1;
*return_vals = values;
values[0].type = GIMP_PDB_STATUS;
1997-11-24 14:05:25 -08:00
values[0].data.d_status = status;
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
1997-11-24 14:05:25 -08:00
/* Since a channel might be selected, we must check wheter RGB or not. */
if (gimp_layer_get_preserve_transparency (drawable_id))
{
g_message (_("The layer preserves transparency."));
1997-11-24 14:05:25 -08:00
return;
}
1999-10-24 13:49:09 -07:00
if (!gimp_drawable_is_rgb (drawable_id) &&
1999-10-16 17:07:55 -07:00
!gimp_drawable_is_gray (drawable_id))
1997-11-24 14:05:25 -08:00
{
g_message (_("RGBA/GRAYA drawable is not selected."));
1997-11-24 14:05:25 -08:00
return;
}
gimp_get_data (PLUG_IN_NAME, &VALS);
if (! threshold_alpha_dialog ())
1997-11-24 14:05:25 -08:00
return;
break;
case GIMP_RUN_NONINTERACTIVE:
if (nparams != 4)
{
status = GIMP_PDB_CALLING_ERROR;
}
else
{
VALS.threshold = param[3].data.d_int32;
}
1997-11-24 14:05:25 -08:00
break;
case GIMP_RUN_WITH_LAST_VALS:
1997-11-24 14:05:25 -08:00
gimp_get_data (PLUG_IN_NAME, &VALS);
break;
}
if (status == GIMP_PDB_SUCCESS)
{
status = threshold_alpha (drawable_id);
1997-11-24 14:05:25 -08:00
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
if (run_mode == GIMP_RUN_INTERACTIVE && status == GIMP_PDB_SUCCESS)
gimp_set_data (PLUG_IN_NAME, &VALS, sizeof (ValueType));
}
1997-11-24 14:05:25 -08:00
values[0].type = GIMP_PDB_STATUS;
1997-11-24 14:05:25 -08:00
values[0].data.d_status = status;
}
static void
threshold_alpha_func (const guchar *src,
guchar *dest,
gint bpp,
gpointer data)
{
gint gap;
for (gap = GPOINTER_TO_INT(data); gap; gap--)
*dest++ = *src++;
*dest = (VALS.threshold < *src) ? 255 : 0;
}
static GimpPDBStatusType
threshold_alpha (gint32 drawable_id)
1997-11-24 14:05:25 -08:00
{
GimpDrawable *drawable;
gint gap;
1997-11-24 14:05:25 -08:00
drawable = gimp_drawable_get (drawable_id);
if (! gimp_drawable_has_alpha (drawable_id))
return GIMP_PDB_EXECUTION_ERROR;
1997-11-24 14:05:25 -08:00
gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1));
gimp_progress_init (_("Threshold Alpha: Coloring Transparency..."));
gap = (gimp_drawable_is_rgb (drawable_id)) ? 3 : 1;
gimp_rgn_iterate2 (drawable, run_mode, threshold_alpha_func,
GINT_TO_POINTER(gap));
1997-11-24 14:05:25 -08:00
gimp_drawable_detach (drawable);
return GIMP_PDB_SUCCESS;
1997-11-24 14:05:25 -08:00
}
static gint
threshold_alpha_dialog (void)
1997-11-24 14:05:25 -08:00
{
GtkWidget *dlg;
GtkWidget *table;
GtkObject *adj;
gimp_ui_init ("threshold_alpha", FALSE);
1997-11-24 14:05:25 -08:00
dlg = gimp_dialog_new (_("Threshold Alpha"), "threshold_alpha",
gimp_standard_help_func, "filters/threshold_alpha.html",
GTK_WIN_POS_MOUSE,
FALSE, TRUE, FALSE,
2001-08-03 12:52:08 -07:00
GTK_STOCK_CANCEL, gtk_widget_destroy,
NULL, 1, NULL, FALSE, TRUE,
GTK_STOCK_OK, threshold_alpha_ok_callback,
NULL, NULL, NULL, TRUE, FALSE,
NULL);
g_signal_connect (dlg, "destroy",
G_CALLBACK (gtk_main_quit),
NULL);
table = gimp_parameter_settings_new (GTK_DIALOG (dlg)->vbox, 1, 3);
1997-11-24 14:05:25 -08:00
adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
_("Threshold:"), SCALE_WIDTH, 0,
VALS.threshold, 0, 255, 1, 8, 0,
TRUE, 0, 0,
NULL, NULL);
g_signal_connect (adj, "value_changed",
G_CALLBACK (gimp_int_adjustment_update),
&VALS.threshold);
1997-11-24 14:05:25 -08:00
gtk_widget_show (dlg);
1997-11-24 14:05:25 -08:00
gtk_main ();
gdk_flush ();
1997-11-24 14:05:25 -08:00
return INTERFACE.run;
1997-11-24 14:05:25 -08:00
}
static void
threshold_alpha_ok_callback (GtkWidget *widget,
gpointer data)
1997-11-24 14:05:25 -08:00
{
INTERFACE.run = TRUE;
1997-11-24 14:05:25 -08:00
gtk_widget_destroy (GTK_WIDGET (data));
1997-11-24 14:05:25 -08:00
}