border-average.c 13.8 KB
Newer Older
1
/* borderaverage 0.01 - image processing plug-in for GIMP.
2
3
4
5
 *
 * Copyright (C) 1998 Philipp Klaus (webmaster@access.ch)
 *
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
10
11
12
13
14
15
16
 * (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
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19

20
#include "config.h"
21
22
23
24
25
26
27

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

#include "libgimp/stdplugins-intl.h"


28
#define PLUG_IN_PROC   "plug-in-borderaverage"
29
#define PLUG_IN_BINARY "border-average"
30
#define PLUG_IN_ROLE   "gimp-border-average"
31
32


33
34
35
/* Declare local functions.
 */
static void      query  (void);
36
static void      run    (const gchar      *name,
37
38
39
40
                         gint              nparams,
                         const GimpParam  *param,
                         gint             *nreturn_vals,
                         GimpParam       **return_vals);
41
42


David Odin's avatar
David Odin committed
43
44
static void      borderaverage        (GimpDrawable *drawable,
                                       GimpRGB      *result);
45

David Odin's avatar
David Odin committed
46
47
static gboolean  borderaverage_dialog (gint32        image_ID,
                                       GimpDrawable *drawable);
48

David Odin's avatar
David Odin committed
49
50
51
52
53
54
55
static void      add_new_color        (gint          bytes,
                                       const guchar *buffer,
                                       gint         *cube,
                                       gint          bucket_expo);

static void      thickness_callback   (GtkWidget    *widget,
                                       gpointer      data);
56

57
const GimpPlugInInfo PLUG_IN_INFO =
58
{
59
60
61
62
  NULL,  /* init  */
  NULL,  /* quit  */
  query, /* query */
  run,   /* run   */
63
64
};

65
static gint  borderaverage_thickness       = 3;
66
static gint  borderaverage_bucket_exponent = 4;
67

68
69
struct borderaverage_data
{
70
71
  gint  thickness;
  gint  bucket_exponent;
72
}
73
74

static borderaverage_data =
75
76
77
78
{
  3,
  4
};
79
80
81
82

MAIN ()

static void
83
query (void)
84
{
85
  static const GimpParamDef args[] =
86
  {
87
    { GIMP_PDB_INT32,    "run-mode",        "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
88
89
90
    { GIMP_PDB_IMAGE,    "image",           "Input image (unused)" },
    { GIMP_PDB_DRAWABLE, "drawable",        "Input drawable" },
    { GIMP_PDB_INT32,    "thickness",       "Border size to take in count" },
91
    { GIMP_PDB_INT32,    "bucket-exponent", "Bits for bucket size (default=4: 16 Levels)" },
92
  };
93
  static const GimpParamDef return_vals[] =
94
  {
95
    { GIMP_PDB_COLOR,    "borderaverage",   "The average color of the specified border." },
96
  };
97

98
  gimp_install_procedure (PLUG_IN_PROC,
99
                          N_("Set foreground to the average color of the image border"),
100
101
102
103
                          "",
                          "Philipp Klaus",
                          "Internet Access AG",
                          "1998",
104
                          N_("_Border Average..."),
105
106
107
                          "RGB*",
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (args),
108
                          G_N_ELEMENTS (return_vals),
109
                          args, return_vals);
110

111
  gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Colors/Info");
112
113
114
}

static void
115
116
117
118
119
run (const gchar      *name,
     gint              nparams,
     const GimpParam  *param,
     gint             *nreturn_vals,
     GimpParam       **return_vals)
120
{
121
122
  static GimpParam   values[3];
  GimpDrawable      *drawable;
123
  gint32             image_ID;
124
125
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
  GimpRGB            result_color;
126
  GimpRunMode        run_mode;
127

128
  INIT_I18N ();
129

130
  run_mode = param[0].data.d_int32;
131
  image_ID = param[1].data.d_int32;
132

133
  /*    Get the specified drawable      */
134
135
136
137
  drawable = gimp_drawable_get (param[2].data.d_drawable);

  switch (run_mode)
    {
Sven Neumann's avatar
Sven Neumann committed
138
    case GIMP_RUN_INTERACTIVE:
139
      gimp_get_data (PLUG_IN_PROC, &borderaverage_data);
140
      borderaverage_thickness       = borderaverage_data.thickness;
141
      borderaverage_bucket_exponent = borderaverage_data.bucket_exponent;
142
      if (! borderaverage_dialog (image_ID, drawable))
143
        status = GIMP_PDB_EXECUTION_ERROR;
144
145
      break;

Sven Neumann's avatar
Sven Neumann committed
146
    case GIMP_RUN_NONINTERACTIVE:
147
      if (nparams != 5)
148
        status = GIMP_PDB_CALLING_ERROR;
Sven Neumann's avatar
Sven Neumann committed
149
      if (status == GIMP_PDB_SUCCESS)
150
151
152
153
        {
          borderaverage_thickness       = param[3].data.d_int32;
          borderaverage_bucket_exponent = param[4].data.d_int32;
        }
154
155
      break;

Sven Neumann's avatar
Sven Neumann committed
156
    case GIMP_RUN_WITH_LAST_VALS:
157
      gimp_get_data (PLUG_IN_PROC, &borderaverage_data);
158
      borderaverage_thickness       = borderaverage_data.thickness;
159
160
161
162
163
164
165
      borderaverage_bucket_exponent = borderaverage_data.bucket_exponent;
      break;

    default:
      break;
    }

Sven Neumann's avatar
Sven Neumann committed
166
  if (status == GIMP_PDB_SUCCESS)
167
    {
168
      /*  Make sure that the drawable is RGB color  */
169
      if (gimp_drawable_is_rgb (drawable->drawable_id))
170
        {
171
          gimp_progress_init ( _("Border Average"));
172
173
174
175
          borderaverage (drawable, &result_color);

          if (run_mode != GIMP_RUN_NONINTERACTIVE)
            {
176
              gimp_context_set_foreground (&result_color);
177
178
179
180
181
            }
          if (run_mode == GIMP_RUN_INTERACTIVE)
            {
              borderaverage_data.thickness       = borderaverage_thickness;
              borderaverage_data.bucket_exponent = borderaverage_bucket_exponent;
182
              gimp_set_data (PLUG_IN_PROC,
183
184
185
                             &borderaverage_data, sizeof (borderaverage_data));
            }
        }
186
      else
187
188
189
        {
          status = GIMP_PDB_EXECUTION_ERROR;
        }
190
191
    }
  *nreturn_vals = 3;
192
  *return_vals  = values;
193

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

197
  values[1].type         = GIMP_PDB_COLOR;
198
  values[1].data.d_color = result_color;
199

200
  gimp_drawable_detach (drawable);
201
202
}

203
204
205
206
207
208
209
210
typedef struct {
  gint x1, x2, y1, y2;
  gint bucket_expo;
  gint *cube;
} BorderAverageParam_t;

static void
borderaverage_func (gint x,
David Odin's avatar
David Odin committed
211
212
213
214
                    gint y,
                    const guchar *src,
                    gint bpp,
                    gpointer data)
215
216
217
218
219
220
221
222
223
224
225
226
{
  BorderAverageParam_t *param = (BorderAverageParam_t*) data;

  if (x <  param->x1 + borderaverage_thickness ||
      x >= param->x2 - borderaverage_thickness ||
      y <  param->y1 + borderaverage_thickness ||
      y >= param->y2 - borderaverage_thickness)
    {
      add_new_color (bpp, src, param->cube, param->bucket_expo);
    }
}

227
static void
228
borderaverage (GimpDrawable *drawable,
229
               GimpRGB      *result)
230
{
231
232
233
234
235
236
237
238
  gint    x1, x2, y1, y2;
  gint    bytes;
  gint    max;
  guchar  r, g, b;
  guchar *buffer;
  gint    bucket_num, bucket_expo, bucket_rexpo;
  gint   *cube;
  gint    i, j, k; /* index variables */
239
  GimpRgnIterator *iter;
240

241
  BorderAverageParam_t     param;
242
243
244
245

  /* allocate and clear the cube before */
  bucket_expo = borderaverage_bucket_exponent;
  bucket_rexpo = 8 - bucket_expo;
246
  param.cube = cube = g_new (gint, 1 << (bucket_rexpo * 3));
247
  bucket_num = 1 << bucket_rexpo;
248

249
250
251
  for (i = 0; i < bucket_num; i++)
    {
      for (j = 0; j < bucket_num; j++)
252
253
254
255
256
257
        {
          for (k = 0; k < bucket_num; k++)
            {
              cube[(i << (bucket_rexpo << 1)) + (j << bucket_rexpo) + k] = 0;
            }
        }
258
259
    }

260
  gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
261
262
263
264
265
  param.x1 = x1;
  param.y1 = y1;
  param.x2 = x2;
  param.y2 = y2;
  param.bucket_expo = bucket_expo;
266
267
268
269
270
271

  /*  Get the size of the input image. (This will/must be the same
   *  as the size of the output image.
   */
  bytes = drawable->bpp;

272
273
  gimp_tile_cache_ntiles (2 * ((x2 - x1) / gimp_tile_width () + 1));

274
275
276
  /*  allocate row buffer  */
  buffer = g_new (guchar, (x2 - x1) * bytes);

277
  iter = gimp_rgn_iterator_new (drawable, 0);
278
279
  gimp_rgn_iterator_src (iter, borderaverage_func, &param);
  gimp_rgn_iterator_free (iter);
280

281
  max = 0; r = 0; g = 0; b = 0;
282

283
284
285
286
  /* get max of cube */
  for (i = 0; i < bucket_num; i++)
    {
      for (j = 0; j < bucket_num; j++)
287
288
289
290
291
292
293
294
295
296
297
298
299
300
        {
          for (k = 0; k < bucket_num; k++)
            {
              if (cube[(i << (bucket_rexpo << 1)) +
                      (j << bucket_rexpo) + k] > max)
                {
                  max = cube[(i << (bucket_rexpo << 1)) +
                            (j << bucket_rexpo) + k];
                  r = (i<<bucket_expo) + (1<<(bucket_expo - 1));
                  g = (j<<bucket_expo) + (1<<(bucket_expo - 1));
                  b = (k<<bucket_expo) + (1<<(bucket_expo - 1));
                }
            }
        }
301
    }
302

303
  /* return the color */
304
  gimp_rgb_set_uchar (result, r, g, b);
305

306
307
308
  g_free (buffer);
  g_free (cube);
}
309

310
static void
David Odin's avatar
David Odin committed
311
add_new_color (gint          bytes,
312
               const guchar *buffer,
David Odin's avatar
David Odin committed
313
               gint         *cube,
314
               gint          bucket_expo)
315
{
316
  guchar r, g, b;
317
  gint   bucket_rexpo;
318
319

  bucket_rexpo = 8 - bucket_expo;
320
321
322
  r = buffer[0] >> bucket_expo;
  g = (bytes > 1) ? buffer[1] >> bucket_expo : 0;
  b = (bytes > 2) ? buffer[2] >> bucket_expo : 0;
323
324
325
  cube[(r << (bucket_rexpo << 1)) + (g << bucket_rexpo) + b]++;
}

326
static gboolean
David Odin's avatar
David Odin committed
327
328
borderaverage_dialog (gint32        image_ID,
                      GimpDrawable *drawable)
329
{
David Odin's avatar
David Odin committed
330
  GtkWidget    *dialog;
331
  GtkWidget    *frame;
David Odin's avatar
David Odin committed
332
  GtkWidget    *main_vbox;
333
334
  GtkWidget    *hbox;
  GtkWidget    *label;
335
336
  GtkWidget    *size_entry;
  GimpUnit      unit;
337
338
339
  GtkWidget    *combo;
  GtkSizeGroup *group;
  gboolean      run;
David Odin's avatar
David Odin committed
340
  gdouble       xres, yres;
341

342
343
344
  const gchar *labels[] =
    { "1", "2", "4", "8", "16", "32", "64", "128", "256" };

345
  gimp_ui_init (PLUG_IN_BINARY, FALSE);
346

347
  dialog = gimp_dialog_new (_("Borderaverage"), PLUG_IN_ROLE,
David Odin's avatar
David Odin committed
348
                            NULL, 0,
349
                            gimp_standard_help_func, PLUG_IN_PROC,
350

David Odin's avatar
David Odin committed
351
352
                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                            GTK_STOCK_OK,     GTK_RESPONSE_OK,
Sven Neumann's avatar
Sven Neumann committed
353

David Odin's avatar
David Odin committed
354
                            NULL);
355

356
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
357
358
359
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);
360

361
  gimp_window_set_transient (GTK_WINDOW (dialog));
362

Michael Natterer's avatar
Michael Natterer committed
363
  main_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
David Odin's avatar
David Odin committed
364
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
365
366
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
                      main_vbox, TRUE, TRUE, 0);
David Odin's avatar
David Odin committed
367
  gtk_widget_show (main_vbox);
368

369
  frame = gimp_frame_new (_("Border Size"));
David Odin's avatar
David Odin committed
370
  gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
371
  gtk_widget_show (frame);
372

Michael Natterer's avatar
Michael Natterer committed
373
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
374
375
376
  gtk_container_add (GTK_CONTAINER (frame), hbox);
  gtk_widget_show (hbox);

377
  label = gtk_label_new_with_mnemonic (_("_Thickness:"));
378
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
379
380
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);
381

382
383
384
385
  group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
  gtk_size_group_add_widget (group, label);
  g_object_unref (group);

386
387
388
  /*  Get the image resolution and unit  */
  gimp_image_get_resolution (image_ID, &xres, &yres);
  unit = gimp_image_get_unit (image_ID);
389

390
391
392
393
394
395
396
397
  size_entry = gimp_size_entry_new (1, unit, "%a", TRUE, TRUE, FALSE, 4,
                                    GIMP_SIZE_ENTRY_UPDATE_SIZE);
  gtk_box_pack_start (GTK_BOX (hbox), size_entry, FALSE, FALSE, 0);

  gimp_size_entry_set_unit (GIMP_SIZE_ENTRY (size_entry), GIMP_UNIT_PIXEL);
  gimp_size_entry_set_resolution (GIMP_SIZE_ENTRY (size_entry), 0, xres, TRUE);

  /*  set the size (in pixels) that will be treated as 0% and 100%  */
David Odin's avatar
David Odin committed
398
399
  gimp_size_entry_set_size (GIMP_SIZE_ENTRY (size_entry), 0, 0.0,
                            drawable->width);
400
401
402
403
404
405
406

  gimp_size_entry_set_refval_boundaries (GIMP_SIZE_ENTRY (size_entry), 0,
                                         1.0, 256.0);
  gtk_table_set_col_spacing (GTK_TABLE (size_entry), 0, 4);
  gtk_table_set_col_spacing (GTK_TABLE (size_entry), 2, 12);
  gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (size_entry), 0,
                              (gdouble) borderaverage_thickness);
407
  g_signal_connect (size_entry, "value-changed",
408
409
410
                    G_CALLBACK (thickness_callback),
                    NULL);
  gtk_widget_show (size_entry);
411

412
  frame = gimp_frame_new (_("Number of Colors"));
David Odin's avatar
David Odin committed
413
  gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
414
415
  gtk_widget_show (frame);

Michael Natterer's avatar
Michael Natterer committed
416
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
417
418
419
  gtk_container_add (GTK_CONTAINER (frame), hbox);
  gtk_widget_show (hbox);

420
  label = gtk_label_new_with_mnemonic (_("_Bucket size:"));
421
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
422
423
424
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

425
426
  gtk_size_group_add_widget (group, label);

427
428
429
430
431
432
433
434
435
436
437
  combo = gimp_int_combo_box_new_array (G_N_ELEMENTS (labels), labels);
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (combo),
                                 borderaverage_bucket_exponent);

  g_signal_connect (combo, "changed",
                    G_CALLBACK (gimp_int_combo_box_get_active),
                    &borderaverage_bucket_exponent);

  gtk_box_pack_start (GTK_BOX (hbox), combo, FALSE, FALSE, 0);
  gtk_widget_show (combo);
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
438

David Odin's avatar
David Odin committed
439
  gtk_widget_show (dialog);
440

David Odin's avatar
David Odin committed
441
  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
442

David Odin's avatar
David Odin committed
443
  gtk_widget_destroy (dialog);
444

445
  return run;
446
}
447
448
449

static void
thickness_callback (GtkWidget *widget,
David Odin's avatar
David Odin committed
450
                    gpointer   data)
451
{
David Odin's avatar
David Odin committed
452
  borderaverage_thickness =
453
454
    gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (widget), 0);
}