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

« back to all changes in this revision

Viewing changes to plug-ins/common/checkerboard.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:
21
21
 
22
22
#include "config.h"
23
23
 
24
 
#include <gtk/gtk.h>
25
 
 
26
24
#include <libgimp/gimp.h>
27
25
#include <libgimp/gimpui.h>
28
26
 
29
27
#include "libgimp/stdplugins-intl.h"
30
28
 
31
29
 
 
30
#define PLUG_IN_PROC      "plug-in-checkerboard"
 
31
#define PLUG_IN_BINARY    "checkerboard"
32
32
#define SPIN_BUTTON_WIDTH 8
33
33
 
 
34
 
34
35
/* Variables set in dialog box */
35
36
typedef struct data
36
37
{
37
 
  gint   mode;
38
 
  gint   size;
 
38
  gboolean mode;
 
39
  gint     size;
39
40
} CheckVals;
40
41
 
41
42
 
42
43
static void      query  (void);
43
44
static void      run    (const gchar       *name,
44
 
                         gint               nparams,
45
 
                         const GimpParam   *param,
46
 
                         gint              *nreturn_vals,
47
 
                         GimpParam        **return_vals);
 
45
                         gint               nparams,
 
46
                         const GimpParam   *param,
 
47
                         gint              *nreturn_vals,
 
48
                         GimpParam        **return_vals);
48
49
 
49
 
static void      do_checkerboard_pattern    (GimpDrawable *drawable);
 
50
static void      do_checkerboard_pattern    (GimpDrawable *drawable,
 
51
                                             GimpPreview  *preview);
50
52
static gint      inblock                    (gint          pos,
51
53
                                             gint          size);
52
54
 
53
 
static gboolean  checkerboard_dialog        (gint32        image_ID,
 
55
static gboolean  checkerboard_dialog        (gint32        image_ID,
54
56
                                             GimpDrawable *drawable);
55
 
static void      check_size_update_callback (GtkWidget    *widget,
56
 
                                             gpointer       data);
57
 
 
58
 
 
59
 
GimpPlugInInfo PLUG_IN_INFO =
 
57
static void      check_size_update_callback (GtkWidget    *widget);
 
58
 
 
59
 
 
60
const GimpPlugInInfo PLUG_IN_INFO =
60
61
{
61
62
  NULL,  /* init_proc  */
62
63
  NULL,  /* quit_proc  */
66
67
 
67
68
static CheckVals cvals =
68
69
{
69
 
  0,
70
 
  10
 
70
  FALSE,     /* mode */
 
71
  10         /* size */
71
72
};
72
73
 
73
74
MAIN ()
75
76
static void
76
77
query (void)
77
78
{
78
 
  static GimpParamDef args[] =
 
79
  static const GimpParamDef args[] =
79
80
  {
80
 
    { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
81
 
    { GIMP_PDB_IMAGE, "image", "Input image (unused)" },
82
 
    { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
83
 
    { GIMP_PDB_INT32, "check_mode", "Regular or Psychobilly" },
84
 
    { GIMP_PDB_INT32, "check_size", "Size of the checks" }
 
81
    { GIMP_PDB_INT32,    "run-mode",   "Interactive, non-interactive" },
 
82
    { GIMP_PDB_IMAGE,    "image",      "Input image (unused)"         },
 
83
    { GIMP_PDB_DRAWABLE, "drawable",   "Input drawable"               },
 
84
    { GIMP_PDB_INT32,    "check-mode", "Regular or Psychobilly"       },
 
85
    { GIMP_PDB_INT32,    "check-size", "Size of the checks"           }
85
86
  };
86
87
 
87
 
  gimp_install_procedure ("plug_in_checkerboard",
88
 
                          "Adds a checkerboard pattern to an image",
89
 
                          "More here later",
90
 
                          "Brent Burton & the Edward Blevins",
91
 
                          "Brent Burton & the Edward Blevins",
92
 
                          "1997",
93
 
                          N_("_Checkerboard..."),
94
 
                          "RGB*, GRAY*",
95
 
                          GIMP_PLUGIN,
96
 
                          G_N_ELEMENTS (args), 0,
97
 
                          args, NULL);
 
88
  gimp_install_procedure (PLUG_IN_PROC,
 
89
                          N_("Create a checkerboard pattern"),
 
90
                          "More here later",
 
91
                          "Brent Burton & the Edward Blevins",
 
92
                          "Brent Burton & the Edward Blevins",
 
93
                          "1997",
 
94
                          N_("_Checkerboard..."),
 
95
                          "RGB*, GRAY*",
 
96
                          GIMP_PLUGIN,
 
97
                          G_N_ELEMENTS (args), 0,
 
98
                          args, NULL);
98
99
 
99
 
  gimp_plugin_menu_register ("plug_in_checkerboard",
100
 
                             "<Image>/Filters/Render/Pattern");
 
100
  gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Filters/Render/Pattern");
101
101
}
102
102
 
103
103
static void
129
129
  switch (run_mode)
130
130
    {
131
131
    case GIMP_RUN_INTERACTIVE:
132
 
      gimp_get_data ("plug_in_checkerboard", &cvals);
 
132
      gimp_get_data (PLUG_IN_PROC, &cvals);
133
133
      if (! checkerboard_dialog (image_ID, drawable))
134
 
        {
135
 
          gimp_drawable_detach (drawable);
136
 
          return;
137
 
        }
 
134
        {
 
135
          gimp_drawable_detach (drawable);
 
136
          return;
 
137
        }
138
138
      break;
139
139
 
140
140
    case GIMP_RUN_NONINTERACTIVE:
141
141
      if (nparams != 5)
142
 
        status = GIMP_PDB_CALLING_ERROR;
 
142
        status = GIMP_PDB_CALLING_ERROR;
143
143
      if (status == GIMP_PDB_SUCCESS)
144
 
        {
145
 
          cvals.mode = param[3].data.d_int32;
146
 
          cvals.size = param[4].data.d_int32;
147
 
        }
 
144
        {
 
145
          cvals.mode = param[3].data.d_int32;
 
146
          cvals.size = param[4].data.d_int32;
 
147
        }
148
148
      break;
149
149
 
150
150
    case GIMP_RUN_WITH_LAST_VALS:
151
 
      gimp_get_data ("plug_in_checkerboard", &cvals);
 
151
      gimp_get_data (PLUG_IN_PROC, &cvals);
152
152
      break;
153
153
 
154
154
    default:
158
158
  if (gimp_drawable_is_rgb (drawable->drawable_id) ||
159
159
      gimp_drawable_is_gray (drawable->drawable_id))
160
160
    {
161
 
      gimp_progress_init (_("Adding Checkerboard..."));
 
161
      gimp_progress_init (_("Adding checkerboard"));
162
162
 
163
 
      do_checkerboard_pattern(drawable);
 
163
      do_checkerboard_pattern (drawable, NULL);
164
164
 
165
165
      if (run_mode != GIMP_RUN_NONINTERACTIVE)
166
 
        gimp_displays_flush ();
 
166
        gimp_displays_flush ();
167
167
 
168
168
      if (run_mode == GIMP_RUN_INTERACTIVE)
169
 
        gimp_set_data ("plug_in_checkerboard", &cvals, sizeof (CheckVals));
 
169
        gimp_set_data (PLUG_IN_PROC, &cvals, sizeof (CheckVals));
170
170
    }
171
171
  else
172
172
    {
178
178
  gimp_drawable_detach (drawable);
179
179
}
180
180
 
181
 
typedef struct {
 
181
typedef struct
 
182
{
182
183
  guchar fg[4];
183
184
  guchar bg[4];
184
185
} CheckerboardParam_t;
185
186
 
186
187
static void
187
 
checkerboard_func (gint x,
188
 
                   gint y,
189
 
                   guchar *dest,
190
 
                   gint bpp,
191
 
                   gpointer data)
 
188
checkerboard_func (gint      x,
 
189
                   gint      y,
 
190
                   guchar   *dest,
 
191
                   gint      bpp,
 
192
                   gpointer  data)
192
193
{
193
194
  CheckerboardParam_t *param = (CheckerboardParam_t*) data;
 
195
 
194
196
  gint val, xp, yp;
195
197
  gint b;
196
198
 
217
219
}
218
220
 
219
221
static void
220
 
do_checkerboard_pattern (GimpDrawable *drawable)
 
222
do_checkerboard_pattern (GimpDrawable *drawable,
 
223
                         GimpPreview  *preview)
221
224
{
222
225
  CheckerboardParam_t  param;
223
226
  GimpRgnIterator     *iter;
235
238
      cvals.size = 1;
236
239
    }
237
240
 
238
 
  iter = gimp_rgn_iterator_new (drawable, 0);
239
 
  gimp_rgn_iterator_dest (iter, checkerboard_func, &param);
240
 
  gimp_rgn_iterator_free (iter);
 
241
  if (preview)
 
242
    {
 
243
      gint    x1, y1;
 
244
      gint    width, height;
 
245
      gint    i;
 
246
      gint    bpp;
 
247
      guchar *buffer;
 
248
 
 
249
      gimp_preview_get_position (preview, &x1, &y1);
 
250
      gimp_preview_get_size (preview, &width, &height);
 
251
      bpp = drawable->bpp;
 
252
      buffer = g_new (guchar, width * height * bpp);
 
253
 
 
254
      for (i = 0; i < width * height; i++)
 
255
        {
 
256
          checkerboard_func (x1 + i % width,
 
257
                             y1 + i / width,
 
258
                             buffer + i * bpp,
 
259
                             bpp, &param);
 
260
        }
 
261
      gimp_preview_draw_buffer (preview, buffer, width * bpp);
 
262
      g_free (buffer);
 
263
    }
 
264
  else
 
265
    {
 
266
      iter = gimp_rgn_iterator_new (drawable, 0);
 
267
      gimp_rgn_iterator_dest (iter, checkerboard_func, &param);
 
268
      gimp_rgn_iterator_free (iter);
 
269
    }
241
270
}
242
271
 
243
272
static gint
244
273
inblock (gint pos,
245
 
         gint size)
 
274
         gint size)
246
275
{
247
 
  static gint *in = NULL;       /* initialized first time */
248
 
  gint         len;
 
276
  static gint *in  = NULL;       /* initialized first time */
 
277
  static gint  len = -1;
249
278
 
250
279
  /* avoid a FP exception */
251
280
  if (size == 1)
252
281
    size = 2;
253
282
 
254
 
  len = size*size;
 
283
  if (in && len != size * size)
 
284
    {
 
285
      g_free (in);
 
286
      in = NULL;
 
287
    }
 
288
  len = size * size;
255
289
  /*
256
290
   * Initialize the array; since we'll be called thousands of
257
291
   * times with the same size value, precompute the array.
258
292
   */
259
293
  if (in == NULL)
260
294
    {
261
 
      gint cell = 1;    /* cell value */
 
295
      gint cell = 1;    /* cell value */
262
296
      gint i, j, k;
263
297
 
264
298
      in = g_new (gint, len);
268
302
       * j is current number of blocks to fill in with a 1 or 0.
269
303
       * k is just counter for the j cells.
270
304
       */
271
 
      i=0;
272
 
      for (j=1; j<=size; j++ )
273
 
        { /* first half */
274
 
          for (k=0; k<j; k++ )
275
 
            {
276
 
              in[i++] = cell;
277
 
            }
278
 
          cell = !cell;
279
 
        }
280
 
      for ( j=size-1; j>=1; j--)
281
 
        { /* second half */
282
 
          for (k=0; k<j; k++ )
283
 
            {
284
 
              in[i++] = cell;
285
 
            }
286
 
          cell = !cell;
287
 
        }
 
305
      i = 0;
 
306
      for (j = 1; j <= size; j++)
 
307
        { /* first half */
 
308
          for (k = 0; k < j; k++)
 
309
            {
 
310
              in[i++] = cell;
 
311
            }
 
312
          cell = !cell;
 
313
        }
 
314
      for (j = size - 1; j >= 1; j--)
 
315
        { /* second half */
 
316
          for (k = 0; k < j; k++)
 
317
            {
 
318
              in[i++] = cell;
 
319
            }
 
320
          cell = !cell;
 
321
        }
288
322
    }
289
323
 
290
324
  /* place pos within 0..(len-1) grid and return the value. */
291
 
  return in[ pos % (len-1) ];
 
325
  return in[pos % (len - 1)];
292
326
}
293
327
 
294
328
static gboolean
295
329
checkerboard_dialog (gint32        image_ID,
296
330
                     GimpDrawable *drawable)
297
331
{
298
 
  GtkWidget *dlg;
 
332
  GtkWidget *dialog;
299
333
  GtkWidget *vbox;
 
334
  GtkWidget *preview;
300
335
  GtkWidget *toggle;
301
336
  GtkWidget *size_entry;
302
 
  gint       size, width, height;
 
337
  gint       size, width, height;
303
338
  GimpUnit   unit;
304
339
  gdouble    xres;
305
340
  gdouble    yres;
306
341
  gboolean   run;
307
342
 
308
 
  gimp_ui_init ("checkerboard", FALSE);
309
 
 
310
 
  dlg = gimp_dialog_new (_("Checkerboard"), "checkerboard",
311
 
                         NULL, 0,
312
 
                         gimp_standard_help_func, "plug-in-checkerboard",
313
 
 
314
 
                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
315
 
                         GTK_STOCK_OK,     GTK_RESPONSE_OK,
316
 
 
317
 
                         NULL);
318
 
 
319
 
  /*  Get the image resolution and unit  */
320
 
  gimp_image_get_resolution (image_ID, &xres, &yres);
321
 
  unit = gimp_image_get_unit (image_ID);
322
 
 
323
 
  width  = gimp_drawable_width (drawable->drawable_id);
324
 
  height = gimp_drawable_height (drawable->drawable_id);
325
 
  size   = MIN (width, height);
 
343
  gimp_ui_init (PLUG_IN_BINARY, FALSE);
 
344
 
 
345
  dialog = gimp_dialog_new (_("Checkerboard"), PLUG_IN_BINARY,
 
346
                            NULL, 0,
 
347
                            gimp_standard_help_func, PLUG_IN_PROC,
 
348
 
 
349
                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
350
                            GTK_STOCK_OK,     GTK_RESPONSE_OK,
 
351
 
 
352
                            NULL);
 
353
 
 
354
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
 
355
                                           GTK_RESPONSE_OK,
 
356
                                           GTK_RESPONSE_CANCEL,
 
357
                                           -1);
 
358
 
 
359
  gimp_window_set_transient (GTK_WINDOW (dialog));
326
360
 
327
361
  vbox = gtk_vbox_new (FALSE, 12);
328
362
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
329
 
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), vbox, TRUE, TRUE, 0);
 
363
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), vbox);
330
364
  gtk_widget_show (vbox);
331
365
 
 
366
  preview = gimp_drawable_preview_new (drawable, NULL);
 
367
  gtk_container_add (GTK_CONTAINER (vbox), preview);
 
368
  gtk_widget_show (preview);
 
369
  g_signal_connect_swapped (preview, "invalidated",
 
370
                            G_CALLBACK (do_checkerboard_pattern),
 
371
                            drawable);
 
372
 
332
373
  toggle = gtk_check_button_new_with_mnemonic (_("_Psychobilly"));
333
374
  gtk_box_pack_start (GTK_BOX (vbox), toggle, FALSE, FALSE, 0);
334
375
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), cvals.mode);
337
378
  g_signal_connect (toggle, "toggled",
338
379
                    G_CALLBACK (gimp_toggle_button_update),
339
380
                    &cvals.mode);
 
381
  g_signal_connect_swapped (toggle, "toggled",
 
382
                            G_CALLBACK (gimp_preview_invalidate),
 
383
                            preview);
 
384
 
 
385
  /*  Get the image resolution and unit  */
 
386
  gimp_image_get_resolution (image_ID, &xres, &yres);
 
387
  unit = gimp_image_get_unit (image_ID);
 
388
 
 
389
  width  = gimp_drawable_width (drawable->drawable_id);
 
390
  height = gimp_drawable_height (drawable->drawable_id);
 
391
  size   = MIN (width, height);
340
392
 
341
393
  size_entry = gimp_size_entry_new (1, unit, "%a",
342
394
                                    TRUE, TRUE, FALSE, SPIN_BUTTON_WIDTH,
364
416
  gimp_size_entry_attach_label (GIMP_SIZE_ENTRY (size_entry),
365
417
                                _("_Size:"), 1, 0, 0.0);
366
418
 
367
 
  g_signal_connect (size_entry, "value_changed",
 
419
  g_signal_connect (size_entry, "value-changed",
368
420
                    G_CALLBACK (check_size_update_callback),
369
421
                    &cvals.size);
 
422
  g_signal_connect_swapped (size_entry, "value-changed",
 
423
                            G_CALLBACK (gimp_preview_invalidate),
 
424
                            preview);
370
425
 
371
 
  gtk_container_add (GTK_CONTAINER (vbox), size_entry);
 
426
  gtk_box_pack_start (GTK_BOX (vbox), size_entry, FALSE, FALSE, 0);
372
427
  gtk_widget_show (size_entry);
373
428
 
374
 
  gtk_widget_show (dlg);
375
 
 
376
 
  run = (gimp_dialog_run (GIMP_DIALOG (dlg)) == GTK_RESPONSE_OK);
377
 
 
378
 
  gtk_widget_destroy (dlg);
 
429
  gtk_widget_show (dialog);
 
430
 
 
431
  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
 
432
 
 
433
  gtk_widget_destroy (dialog);
379
434
 
380
435
  return run;
381
436
}
382
437
 
383
438
static void
384
 
check_size_update_callback (GtkWidget *widget,
385
 
                            gpointer   data)
 
439
check_size_update_callback (GtkWidget *widget)
386
440
{
387
441
  cvals.size = gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (widget), 0);
388
442
}