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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
 * Autocrop plug-in version 1.00
 * by Tim Newsome <drz@froody.bloke.com>
 */

/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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.
 */

#include "config.h"

#include <string.h>

#include <libgimp/gimp.h>

#include "libgimp/stdplugins-intl.h"


#define AUTOCROP_PROC       "plug-in-autocrop"
#define AUTOCROP_LAYER_PROC "plug-in-autocrop-layer"


/* Declare local functions. */
static void      query         (void);
static void      run           (const gchar      *name,
                                gint              nparams,
                                const GimpParam  *param,
                                gint             *nreturn_vals,
                                GimpParam       **return_vals);

static gboolean  colors_equal  (const guchar *col1,
                                const guchar *col2,
                                gint          bytes);
static gint      guess_bgcolor (GimpPixelRgn *pr,
                                gint          width,
                                gint          height,
                                gint          bytes,
                                guchar       *color);

static void      autocrop      (GimpDrawable *drawable,
                                gint32        image_id,
                                gboolean      show_progress,
                                gboolean      layer_only);


const GimpPlugInInfo PLUG_IN_INFO =
{
  NULL,   /* init_proc  */
  NULL,   /* quit_proc  */
  query,  /* query_proc */
  run,    /* run_proc   */
};

static gint bytes;


MAIN ()

static void
query (void)
{
  static const GimpParamDef args[] =
  {
    { GIMP_PDB_INT32,    "run-mode", "Interactive, non-interactive" },
    { GIMP_PDB_IMAGE,    "image",    "Input image"                  },
    { GIMP_PDB_DRAWABLE, "drawable", "Input drawable"               }
  };

  gimp_install_procedure (AUTOCROP_PROC,
                          N_("Remove empty borders from the image"),
                          "",
                          "Tim Newsome",
                          "Tim Newsome",
                          "1997",
                          N_("Autocrop Imag_e"),
                          "RGB*, GRAY*, INDEXED*",
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (args), 0,
                          args, NULL);

  gimp_plugin_menu_register (AUTOCROP_PROC, "<Image>/Image/Crop");

  gimp_install_procedure (AUTOCROP_LAYER_PROC,
                          N_("Remove empty borders from the layer"),
                          "",
                          "Tim Newsome",
                          "Tim Newsome",
                          "1997",
                          N_("Autocrop Lay_er"),
                          "RGB*, GRAY*, INDEXED*",
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (args), 0,
                          args, NULL);

  gimp_plugin_menu_register (AUTOCROP_LAYER_PROC, "<Image>/Layer/Crop");
}

static void
run (const gchar      *name,
     gint              n_params,
     const GimpParam  *param,
     gint             *nreturn_vals,
     GimpParam       **return_vals)
{
  static GimpParam   values[1];
  GimpDrawable      *drawable;
  GimpRunMode        run_mode;
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
  gint32             image_id;
  gboolean           interactive;

  *nreturn_vals = 1;
  *return_vals = values;

  run_mode = param[0].data.d_int32;
  interactive = (run_mode != GIMP_RUN_NONINTERACTIVE);

  INIT_I18N();

  if (n_params != 3)
    {
      status = GIMP_PDB_CALLING_ERROR;
      goto out;
    }

  /*  Get the specified drawable  */
  drawable = gimp_drawable_get (param[2].data.d_drawable);
  image_id = param[1].data.d_image;

  /*  Make sure that the drawable is gray or RGB color  */
  if (gimp_drawable_is_rgb (drawable->drawable_id) ||
      gimp_drawable_is_gray (drawable->drawable_id)  ||
      gimp_drawable_is_indexed (drawable->drawable_id))
    {
      if (interactive)
        gimp_progress_init (_("Cropping"));

      gimp_tile_cache_ntiles (MAX (drawable->width / gimp_tile_width (),
                                   drawable->height / gimp_tile_height ()) + 1);

      autocrop (drawable, image_id, interactive,
                strcmp (name, AUTOCROP_LAYER_PROC) == 0);

      if (interactive)
        gimp_displays_flush ();
    }
  else
    {
      status = GIMP_PDB_EXECUTION_ERROR;
    }

 out:
  values[0].type = GIMP_PDB_STATUS;
  values[0].data.d_status = status;
}

static void
autocrop (GimpDrawable *drawable,
          gint32        image_id,
          gboolean      show_progress,
          gboolean      layer_only)
{
  GimpPixelRgn  srcPR;
  gint          width, height;
  gint          off_x, off_y;
  gint          x1, y1, x2, y2, i;
  gboolean      abort;
  guchar       *buffer;
  guchar        color[4] = {0, 0, 0, 0};
  gint32        layer_id = 0;

  width  = drawable->width;
  height = drawable->height;
  bytes  = drawable->bpp;
  gimp_drawable_offsets (drawable->drawable_id, &off_x, &off_y);

  if (layer_only)
    {
      layer_id = gimp_image_get_active_layer (image_id);
      if (layer_id == -1)
        {
          gimp_drawable_detach (drawable);
          return;
        }
    }

  /*  initialize the pixel regions  */
  gimp_pixel_rgn_init (&srcPR, drawable, 0, 0, width, height, FALSE, FALSE);

  /* First, let's figure out what exactly to crop. */
  buffer = g_malloc ((width > height ? width : height) * bytes);

  guess_bgcolor (&srcPR, width, height, bytes, color);

  /* Check how many of the top lines are uniform. */
  abort = FALSE;
  for (y1 = 0; y1 < height && !abort; y1++)
    {
      gimp_pixel_rgn_get_row (&srcPR, buffer, 0, y1, width);

      for (i = 0; i < width && !abort; i++)
        abort = !colors_equal (color, buffer + i * bytes, bytes);
    }

  if (y1 == height && !abort)
    {
      /* whee - a plain color drawable. Do nothing. */
      g_free (buffer);
      gimp_drawable_detach (drawable);
      return;
    }

  if (show_progress)
    gimp_progress_update (0.25);

  /* Check how many of the bottom lines are uniform. */
  abort = FALSE;
  for (y2 = height - 1; y2 >= 0 && !abort; y2--)
    {
      gimp_pixel_rgn_get_row (&srcPR, buffer, 0, y2, width);

      for (i = 0; i < width && !abort; i++)
        abort = !colors_equal (color, buffer + i * bytes, bytes);
    }

  y2 += 1; /* to make y2 - y1 == height */

  /* The coordinates are now the first rows which DON'T match
   * the color. Crop instead to one row larger:
   */
  if (y1 > 0)
    y1--;

  if (y2 < height)
    y2++;

  if (show_progress)
    gimp_progress_update (0.5);

  /* Check how many of the left lines are uniform. */
  abort = FALSE;
  for (x1 = 0; x1 < width && !abort; x1++)
    {
      gimp_pixel_rgn_get_col (&srcPR, buffer, x1, y1, y2 - y1);

      for (i = 0; i < y2 - y1 && !abort; i++)
        abort = !colors_equal (color, buffer + i * bytes, bytes);
    }

  if (show_progress)
    gimp_progress_update (0.75);

  /* Check how many of the right lines are uniform. */
  abort = FALSE;
  for (x2 = width - 1; x2 >= 0 && !abort; x2--)
    {
      gimp_pixel_rgn_get_col (&srcPR, buffer, x2, y1, y2 - y1);

      for (i = 0; i < y2 - y1 && !abort; i++)
        abort = !colors_equal (color, buffer + i * bytes, bytes);
    }

  x2 += 1; /* to make x2 - x1 == width */

  /* The coordinates are now the first columns which DON'T match
   * the color. Crop instead to one column larger:
   */
  if (x1 > 0)
    x1--;

  if (x2 < width)
    x2++;

  g_free (buffer);

  gimp_drawable_detach (drawable);

  if (x2 - x1 != width || y2 - y1 != height)
    {
      if (layer_only)
        {
          gimp_layer_resize (layer_id, x2 - x1, y2 - y1, -x1, -y1);
        }
      else
        {
          /* convert to image coordinates */
          x1 += off_x; x2 += off_x;
          y1 += off_y; y2 += off_y;

          gimp_image_undo_group_start (image_id);

          if (x1 < 0 || y1 < 0 ||
              x2 > gimp_image_width (image_id) ||
              y2 > gimp_image_height (image_id))
            {
              /*
               * partially outside the image area, we need to
               * resize the image to be able to crop properly.
               */
              gimp_image_resize (image_id, x2 - x1, y2 - y1, -x1, -y1);

              x2 -= x1;
              y2 -= y1;

              x1 = y1 = 0;
            }

          gimp_image_crop (image_id, x2 - x1, y2 - y1, x1, y1);

          gimp_image_undo_group_end (image_id);
        }
    }

  if (show_progress)
    gimp_progress_update (1.00);
}

static gint
guess_bgcolor (GimpPixelRgn *pr,
               gint          width,
               gint          height,
               gint          bytes,
               guchar       *color)
{
  guchar tl[4], tr[4], bl[4], br[4];

  gimp_pixel_rgn_get_pixel (pr, tl, 0, 0);
  gimp_pixel_rgn_get_pixel (pr, tr, width - 1, 0);
  gimp_pixel_rgn_get_pixel (pr, bl, 0, height - 1);
  gimp_pixel_rgn_get_pixel (pr, br, width - 1, height - 1);

  /* Algorithm pinched from pnmcrop.
   * To guess the background, first see if 3 corners are equal.
   * Then if two are equal.
   * Otherwise average the colors.
   */

  if (colors_equal (tr, bl, bytes) && colors_equal (tr, br, bytes))
    {
      memcpy (color, tr, bytes);
      return 3;
    }
  else if (colors_equal (tl, bl, bytes) && colors_equal (tl, br, bytes))
    {
      memcpy (color, tl, bytes);
      return 3;
    }
  else if (colors_equal (tl, tr, bytes) && colors_equal (tl, br, bytes))
    {
      memcpy (color, tl, bytes);
      return 3;
    }
  else if (colors_equal (tl, tr, bytes) && colors_equal (tl, bl, bytes))
    {
      memcpy (color, tl, bytes);
      return 3;
    }
  else if (colors_equal (tl, tr, bytes) || colors_equal (tl, bl, bytes) ||
           colors_equal (tl, br, bytes))
    {
      memcpy (color, tl, bytes);
      return 2;
    }
  else if (colors_equal (tr, bl, bytes) || colors_equal (tr, bl, bytes))
    {
      memcpy (color, tr, bytes);
      return 2;
    }
  else if (colors_equal (br, bl, bytes))
    {
      memcpy (color, br, bytes);
      return 2;
    }
  else
    {
      while (bytes--)
        {
          color[bytes] = (tl[bytes] + tr[bytes] + bl[bytes] + br[bytes]) / 4;
        }
      return 0;
    }
}

static gboolean
colors_equal (const guchar 	*col1,
              const guchar	*col2,
              gint  		 bytes)
{
  gboolean equal = TRUE;
  gint b;

  if ((bytes == 2 || bytes == 4) &&     /* HACK! */
      col1[bytes-1] == 0 &&
      col2[bytes-1] == 0)
    {
      return TRUE;                      /* handle zero alpha */
    }

  for (b = 0; b < bytes; b++)
    {
      if (col1[b] != col2[b])
        {
          equal = FALSE;
          break;
        }
    }

  return equal;
}