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

« back to all changes in this revision

Viewing changes to plug-ins/common/threshold_alpha.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:
24
24
 
25
25
#include "config.h"
26
26
 
27
 
#include <stdio.h>
28
 
#include <stdlib.h>
29
 
 
30
 
#include <gtk/gtk.h>
31
 
 
32
27
#include <libgimp/gimp.h>
33
28
#include <libgimp/gimpui.h>
34
29
 
35
30
#include "libgimp/stdplugins-intl.h"
36
31
 
37
32
 
38
 
#define PLUG_IN_NAME        "plug_in_threshold_alpha"
39
 
#define SHORT_NAME          "threshold_alpha"
40
 
#define HELP_ID             "plug-in-threshold-alpha"
41
 
#define PROGRESS_UPDATE_NUM 100
42
 
#define SCALE_WIDTH         120
 
33
#define PLUG_IN_PROC    "plug-in-threshold-alpha"
 
34
#define PLUG_IN_BINARY  "threshold_alpha"
 
35
 
 
36
#define SCALE_WIDTH     120
43
37
 
44
38
 
45
39
static void              query (void);
49
43
                                gint             *nreturn_vals,
50
44
                                GimpParam       **return_vals);
51
45
 
52
 
static GimpPDBStatusType threshold_alpha        (gint32     drawable_id);
53
 
 
54
 
static gboolean          threshold_alpha_dialog (void);
55
 
 
56
 
 
57
 
GimpPlugInInfo PLUG_IN_INFO =
 
46
static void      threshold_alpha        (GimpDrawable *drawable,
 
47
                                         GimpPreview  *preview);
 
48
 
 
49
static gboolean  threshold_alpha_dialog (GimpDrawable *drawable);
 
50
 
 
51
 
 
52
const GimpPlugInInfo PLUG_IN_INFO =
58
53
{
59
54
  NULL,  /* init_proc  */
60
55
  NULL,  /* quit_proc  */
64
59
 
65
60
typedef struct
66
61
{
67
 
  gint  threshold;
 
62
  gint  threshold;
68
63
} ValueType;
69
64
 
70
65
static ValueType VALS =
71
66
{
72
 
  127
 
67
  127 /* threshold */
73
68
};
74
69
 
75
70
 
78
73
static void
79
74
query (void)
80
75
{
81
 
  static GimpParamDef args [] =
 
76
  static const GimpParamDef args [] =
82
77
  {
83
 
    { GIMP_PDB_INT32,    "run_mode",  "Interactive, non-interactive" },
 
78
    { GIMP_PDB_INT32,    "run-mode",  "Interactive, non-interactive" },
84
79
    { GIMP_PDB_IMAGE,    "image",     "Input image (not used)"       },
85
80
    { GIMP_PDB_DRAWABLE, "drawable",  "Input drawable"               },
86
81
    { GIMP_PDB_INT32,    "threshold", "Threshold"                    }
87
82
  };
88
83
 
89
 
  gimp_install_procedure (PLUG_IN_NAME,
90
 
                          "",
91
 
                          "",
92
 
                          "Shuji Narazaki (narazaki@InetQ.or.jp)",
93
 
                          "Shuji Narazaki",
94
 
                          "1997",
95
 
                          N_("_Threshold Alpha..."),
96
 
                          "RGBA,GRAYA",
97
 
                          GIMP_PLUGIN,
98
 
                          G_N_ELEMENTS (args), 0,
99
 
                          args, NULL);
 
84
  gimp_install_procedure (PLUG_IN_PROC,
 
85
                          N_("Make transparency all-or-nothing"),
 
86
                          "",
 
87
                          "Shuji Narazaki (narazaki@InetQ.or.jp)",
 
88
                          "Shuji Narazaki",
 
89
                          "1997",
 
90
                          N_("_Threshold Alpha..."),
 
91
                          "RGBA,GRAYA",
 
92
                          GIMP_PLUGIN,
 
93
                          G_N_ELEMENTS (args), 0,
 
94
                          args, NULL);
100
95
 
101
 
  gimp_plugin_menu_register (PLUG_IN_NAME,
 
96
  gimp_plugin_menu_register (PLUG_IN_PROC,
102
97
                             "<Image>/Layer/Transparency/Modify");
103
98
}
104
99
 
112
107
  static GimpParam   values[1];
113
108
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
114
109
  GimpRunMode        run_mode;
115
 
  gint               drawable_id;
116
 
 
117
 
  run_mode    = param[0].data.d_int32;
118
 
  drawable_id = param[2].data.d_int32;
 
110
  GimpDrawable      *drawable;
 
111
 
 
112
  run_mode = param[0].data.d_int32;
 
113
  drawable = gimp_drawable_get (param[2].data.d_drawable);
 
114
 
 
115
  gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1));
119
116
 
120
117
  INIT_I18N ();
121
118
 
129
126
    {
130
127
    case GIMP_RUN_INTERACTIVE:
131
128
      /* Since a channel might be selected, we must check wheter RGB or not. */
132
 
      if (gimp_layer_get_preserve_trans (drawable_id))
133
 
        {
134
 
          g_message (_("The layer preserves transparency."));
135
 
          return;
136
 
        }
137
 
      if (!gimp_drawable_is_rgb (drawable_id) &&
138
 
          !gimp_drawable_is_gray (drawable_id))
139
 
        {
140
 
          g_message (_("RGBA/GRAYA drawable is not selected."));
141
 
          return;
142
 
        }
143
 
      gimp_get_data (PLUG_IN_NAME, &VALS);
144
 
      if (! threshold_alpha_dialog ())
145
 
        return;
 
129
      if (gimp_layer_get_lock_alpha (drawable->drawable_id))
 
130
        {
 
131
          g_message (_("The layer has its alpha channel locked."));
 
132
          return;
 
133
        }
 
134
      if (!gimp_drawable_is_rgb (drawable->drawable_id) &&
 
135
          !gimp_drawable_is_gray (drawable->drawable_id))
 
136
        {
 
137
          g_message (_("RGBA/GRAYA drawable is not selected."));
 
138
          return;
 
139
        }
 
140
      gimp_get_data (PLUG_IN_PROC, &VALS);
 
141
      if (! threshold_alpha_dialog (drawable))
 
142
        return;
146
143
      break;
147
144
 
148
145
    case GIMP_RUN_NONINTERACTIVE:
149
146
      if (nparams != 4)
150
 
        {
151
 
          status = GIMP_PDB_CALLING_ERROR;
152
 
        }
 
147
        {
 
148
          status = GIMP_PDB_CALLING_ERROR;
 
149
        }
153
150
      else
154
 
        {
155
 
          VALS.threshold = param[3].data.d_int32;
156
 
        }
 
151
        {
 
152
          VALS.threshold = param[3].data.d_int32;
 
153
        }
157
154
      break;
158
155
 
159
156
    case GIMP_RUN_WITH_LAST_VALS:
160
 
      gimp_get_data (PLUG_IN_NAME, &VALS);
 
157
      gimp_get_data (PLUG_IN_PROC, &VALS);
161
158
      break;
162
159
    }
163
160
 
164
161
  if (status == GIMP_PDB_SUCCESS)
165
162
    {
166
 
      status = threshold_alpha (drawable_id);
167
 
 
168
 
      if (run_mode != GIMP_RUN_NONINTERACTIVE)
169
 
        gimp_displays_flush ();
170
 
      if (run_mode == GIMP_RUN_INTERACTIVE && status == GIMP_PDB_SUCCESS)
171
 
        gimp_set_data (PLUG_IN_NAME, &VALS, sizeof (ValueType));
 
163
      if (gimp_drawable_has_alpha (drawable->drawable_id))
 
164
        {
 
165
          gimp_progress_init (_("Coloring transparency"));
 
166
 
 
167
          threshold_alpha (drawable, NULL);
 
168
 
 
169
          if (run_mode != GIMP_RUN_NONINTERACTIVE)
 
170
            gimp_displays_flush ();
 
171
          if (run_mode == GIMP_RUN_INTERACTIVE && status == GIMP_PDB_SUCCESS)
 
172
            gimp_set_data (PLUG_IN_PROC, &VALS, sizeof (ValueType));
 
173
        }
 
174
      else
 
175
        {
 
176
          status = GIMP_PDB_EXECUTION_ERROR;
 
177
        }
 
178
      gimp_drawable_detach (drawable);
172
179
    }
173
180
 
174
181
  values[0].type = GIMP_PDB_STATUS;
177
184
 
178
185
static void
179
186
threshold_alpha_func (const guchar *src,
180
 
                      guchar       *dest,
181
 
                      gint          bpp,
182
 
                      gpointer      data)
 
187
                      guchar       *dest,
 
188
                      gint          bpp)
183
189
{
184
 
  gint gap;
185
 
 
186
 
  for (gap = GPOINTER_TO_INT(data); gap; gap--)
 
190
  for (bpp--; bpp; bpp--)
187
191
    *dest++ = *src++;
188
192
  *dest = (VALS.threshold < *src) ? 255 : 0;
189
193
}
190
194
 
191
 
static GimpPDBStatusType
192
 
threshold_alpha (gint32 drawable_id)
 
195
static void
 
196
threshold_alpha (GimpDrawable *drawable,
 
197
                 GimpPreview  *preview)
193
198
{
194
 
  GimpDrawable *drawable;
195
 
  gint gap;
196
 
 
197
 
  drawable = gimp_drawable_get (drawable_id);
198
 
  if (! gimp_drawable_has_alpha (drawable_id))
199
 
    return GIMP_PDB_EXECUTION_ERROR;
200
 
 
201
 
  gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1));
202
 
  gimp_progress_init (_("Threshold Alpha: Coloring Transparency..."));
203
 
 
204
 
  gap = (gimp_drawable_is_rgb (drawable_id)) ? 3 : 1;
205
 
 
206
 
  gimp_rgn_iterate2 (drawable, 0 /* unused */, threshold_alpha_func,
207
 
                     GINT_TO_POINTER(gap));
208
 
 
209
 
  gimp_drawable_detach (drawable);
210
 
 
211
 
  return GIMP_PDB_SUCCESS;
 
199
  if (preview)
 
200
    {
 
201
      GimpPixelRgn  src_rgn;
 
202
      guchar       *src, *dst;
 
203
      gint          i;
 
204
      gint          x1, y1;
 
205
      gint          width, height;
 
206
      gint          bpp;
 
207
 
 
208
      gimp_preview_get_position (preview, &x1, &y1);
 
209
      gimp_preview_get_size (preview, &width, &height);
 
210
 
 
211
      bpp = drawable->bpp;
 
212
 
 
213
      src = g_new (guchar, width * height * bpp);
 
214
      dst = g_new (guchar, width * height * bpp);
 
215
 
 
216
      gimp_pixel_rgn_init (&src_rgn, drawable,
 
217
                           x1, y1, width, height,
 
218
                           FALSE, FALSE);
 
219
      gimp_pixel_rgn_get_rect (&src_rgn, src, x1, y1, width, height);
 
220
 
 
221
      for (i = 0; i < width * height; i++)
 
222
        threshold_alpha_func (src + i * bpp, dst + i * bpp, bpp);
 
223
 
 
224
      gimp_preview_draw_buffer (preview, dst, width * bpp);
 
225
 
 
226
      g_free (src);
 
227
      g_free (dst);
 
228
    }
 
229
  else
 
230
    {
 
231
      gimp_rgn_iterate2 (drawable, 0 /* unused */,
 
232
                         (GimpRgnFunc2)threshold_alpha_func, NULL);
 
233
    }
212
234
}
213
235
 
214
236
static gboolean
215
 
threshold_alpha_dialog (void)
 
237
threshold_alpha_dialog (GimpDrawable *drawable)
216
238
{
217
 
  GtkWidget *dlg;
 
239
  GtkWidget *dialog;
 
240
  GtkWidget *main_vbox;
 
241
  GtkWidget *preview;
218
242
  GtkWidget *table;
219
243
  GtkObject *adj;
220
244
  gboolean   run;
221
245
 
222
 
  gimp_ui_init (SHORT_NAME, FALSE);
223
 
 
224
 
  dlg = gimp_dialog_new (_("Threshold Alpha"), SHORT_NAME,
225
 
                         NULL, 0,
226
 
                         gimp_standard_help_func, HELP_ID,
227
 
 
228
 
                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
229
 
                         GTK_STOCK_OK,     GTK_RESPONSE_OK,
230
 
 
231
 
                         NULL);
 
246
  gimp_ui_init (PLUG_IN_BINARY, FALSE);
 
247
 
 
248
  dialog = gimp_dialog_new (_("Threshold Alpha"), PLUG_IN_BINARY,
 
249
                            NULL, 0,
 
250
                            gimp_standard_help_func, PLUG_IN_PROC,
 
251
 
 
252
                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
253
                            GTK_STOCK_OK,     GTK_RESPONSE_OK,
 
254
 
 
255
                            NULL);
 
256
 
 
257
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
 
258
                                           GTK_RESPONSE_OK,
 
259
                                           GTK_RESPONSE_CANCEL,
 
260
                                           -1);
 
261
 
 
262
  gimp_window_set_transient (GTK_WINDOW (dialog));
 
263
 
 
264
  main_vbox = gtk_vbox_new (FALSE, 12);
 
265
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
 
266
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), main_vbox);
 
267
  gtk_widget_show (main_vbox);
 
268
 
 
269
  preview = gimp_drawable_preview_new (drawable, NULL);
 
270
  gtk_box_pack_start (GTK_BOX (main_vbox), preview, TRUE, TRUE, 0);
 
271
  gtk_widget_show (preview);
 
272
  g_signal_connect_swapped (preview, "invalidated",
 
273
                            G_CALLBACK (threshold_alpha),
 
274
                            drawable);
232
275
 
233
276
  table = gtk_table_new (1 ,3, FALSE);
234
277
  gtk_table_set_col_spacings (GTK_TABLE (table), 6);
235
278
  gtk_container_set_border_width (GTK_CONTAINER (table), 12);
236
 
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), table, FALSE, FALSE, 0);  gtk_widget_show (table);
 
279
  gtk_box_pack_start (GTK_BOX (main_vbox), table, FALSE, FALSE, 0);
 
280
  gtk_widget_show (table);
237
281
 
238
282
  adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
239
 
                              _("Threshold:"), SCALE_WIDTH, 0,
240
 
                              VALS.threshold, 0, 255, 1, 8, 0,
241
 
                              TRUE, 0, 0,
242
 
                              NULL, NULL);
243
 
  g_signal_connect (adj, "value_changed",
 
283
                              _("Threshold:"), SCALE_WIDTH, 0,
 
284
                              VALS.threshold, 0, 255, 1, 8, 0,
 
285
                              TRUE, 0, 0,
 
286
                              NULL, NULL);
 
287
  g_signal_connect (adj, "value-changed",
244
288
                    G_CALLBACK (gimp_int_adjustment_update),
245
289
                    &VALS.threshold);
246
 
 
247
 
  gtk_widget_show (dlg);
248
 
 
249
 
  run = (gimp_dialog_run (GIMP_DIALOG (dlg)) == GTK_RESPONSE_OK);
250
 
 
251
 
  gtk_widget_destroy (dlg);
 
290
  g_signal_connect_swapped (adj, "value-changed",
 
291
                            G_CALLBACK (gimp_preview_invalidate),
 
292
                            preview);
 
293
 
 
294
  gtk_widget_show (dialog);
 
295
 
 
296
  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
 
297
 
 
298
  gtk_widget_destroy (dialog);
252
299
 
253
300
  return run;
254
301
}