summaryrefslogtreecommitdiffstats
path: root/app/core/gimppickable-auto-shrink.c
blob: 7ddbee227ca7c4060e8626c50a375ad0a742e278 (plain)
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
/* 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 3 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, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>

#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "core-types.h"

#include "gimp.h"
#include "gimpimage.h"
#include "gimppickable.h"
#include "gimppickable-auto-shrink.h"


typedef enum
{
  AUTO_SHRINK_NOTHING = 0,
  AUTO_SHRINK_ALPHA   = 1,
  AUTO_SHRINK_COLOR   = 2
} AutoShrinkType;


typedef gboolean (* ColorsEqualFunc) (guchar *col1,
                                      guchar *col2);


/*  local function prototypes  */

static AutoShrinkType   gimp_pickable_guess_bgcolor (GimpPickable *pickable,
                                                     guchar       *color,
                                                     gint          x1,
                                                     gint          x2,
                                                     gint          y1,
                                                     gint          y2);
static gboolean         gimp_pickable_colors_equal  (guchar       *col1,
                                                     guchar       *col2);
static gboolean         gimp_pickable_colors_alpha  (guchar       *col1,
                                                     guchar       *col2);


/*  public functions  */

GimpAutoShrink
gimp_pickable_auto_shrink (GimpPickable *pickable,
                           gint          start_x,
                           gint          start_y,
                           gint          start_width,
                           gint          start_height,
                           gint         *shrunk_x,
                           gint         *shrunk_y,
                           gint         *shrunk_width,
                           gint         *shrunk_height)
{
  GeglBuffer      *buffer;
  GeglRectangle    rect;
  ColorsEqualFunc  colors_equal_func;
  guchar           bgcolor[MAX_CHANNELS] = { 0, 0, 0, 0 };
  guchar          *buf = NULL;
  gint             x1, y1, x2, y2;
  gint             width, height;
  const Babl      *format;
  gint             x, y, abort;
  GimpAutoShrink   retval = GIMP_AUTO_SHRINK_UNSHRINKABLE;

  g_return_val_if_fail (GIMP_IS_PICKABLE (pickable), FALSE);
  g_return_val_if_fail (shrunk_x != NULL, FALSE);
  g_return_val_if_fail (shrunk_y != NULL, FALSE);
  g_return_val_if_fail (shrunk_width != NULL, FALSE);
  g_return_val_if_fail (shrunk_height != NULL, FALSE);

  gimp_set_busy (gimp_pickable_get_image (pickable)->gimp);

  /* You should always keep in mind that x2 and y2 are the NOT the
   * coordinates of the bottomright corner of the area to be
   * cropped. They point at the pixel located one to the right and one
   * to the bottom.
   */

  gimp_pickable_flush (pickable);

  buffer = gimp_pickable_get_buffer (pickable);

  x1 = MAX (start_x, 0);
  y1 = MAX (start_y, 0);
  x2 = MIN (start_x + start_width,  gegl_buffer_get_width  (buffer));
  y2 = MIN (start_y + start_height, gegl_buffer_get_height (buffer));

  /* By default, return the start values */
  *shrunk_x      = x1;
  *shrunk_y      = y1;
  *shrunk_width  = x2 - x1;
  *shrunk_height = y2 - y1;

  format = babl_format ("R'G'B'A u8");

  switch (gimp_pickable_guess_bgcolor (pickable, bgcolor,
                                       x1, x2 - 1, y1, y2 - 1))
    {
    case AUTO_SHRINK_ALPHA:
      colors_equal_func = gimp_pickable_colors_alpha;
      break;
    case AUTO_SHRINK_COLOR:
      colors_equal_func = gimp_pickable_colors_equal;
      break;
    default:
      goto FINISH;
      break;
    }

  width  = x2 - x1;
  height = y2 - y1;

  /* The following could be optimized further by processing
   * the smaller side first instead of defaulting to width    --Sven
   */

  buf = g_malloc (MAX (width, height) * 4);

  /* Check how many of the top lines are uniform/transparent. */
  rect.x      = x1;
  rect.y      = 0;
  rect.width  = width;
  rect.height = 1;

  abort = FALSE;
  for (y = y1; y < y2 && !abort; y++)
    {
      rect.y = y;
      gegl_buffer_get (buffer, &rect, 1.0, format, buf,
                       GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
      for (x = 0; x < width && !abort; x++)
        abort = ! colors_equal_func (bgcolor, buf + x * 4);
    }
  if (y == y2 && !abort)
    {
      retval = GIMP_AUTO_SHRINK_EMPTY;
      goto FINISH;
    }
  y1 = y - 1;

  /* Check how many of the bottom lines are uniform/transparent. */
  rect.x      = x1;
  rect.y      = 0;
  rect.width  = width;
  rect.height = 1;

  abort = FALSE;
  for (y = y2; y > y1 && !abort; y--)
    {
      rect.y = y - 1;
      gegl_buffer_get (buffer, &rect, 1.0, format, buf,
                       GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
      for (x = 0; x < width && !abort; x++)
        abort = ! colors_equal_func  (bgcolor, buf + x * 4);
    }
  y2 = y + 1;

  /* compute a new height for the next operations */
  height = y2 - y1;

  /* Check how many of the left lines are uniform/transparent. */
  rect.x      = 0;
  rect.y      = y1;
  rect.width  = 1;
  rect.height = height;

  abort = FALSE;
  for (x = x1; x < x2 && !abort; x++)
    {
      rect.x = x;
      gegl_buffer_get (buffer, &rect, 1.0, format, buf,
                       GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
      for (y = 0; y < height && !abort; y++)
        abort = ! colors_equal_func (bgcolor, buf + y * 4);
    }
  x1 = x - 1;

  /* Check how many of the right lines are uniform/transparent. */
  rect.x      = 0;
  rect.y      = y1;
  rect.width  = 1;
  rect.height = height;

  abort = FALSE;
  for (x = x2; x > x1 && !abort; x--)
    {
      rect.x = x - 1;
      gegl_buffer_get (buffer, &rect, 1.0, format, buf,
                       GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
      for (y = 0; y < height && !abort; y++)
        abort = ! colors_equal_func (bgcolor, buf + y * 4);
    }
  x2 = x + 1;

  if (x1      != start_x     ||
      y1      != start_y     ||
      x2 - x1 != start_width ||
      y2 - y1 != start_height)
    {
      *shrunk_x      = x1;
      *shrunk_y      = y1;
      *shrunk_width  = x2 - x1;
      *shrunk_height = y2 - y1;

      retval = GIMP_AUTO_SHRINK_SHRINK;
    }

 FINISH:

  g_free (buf);
  gimp_unset_busy (gimp_pickable_get_image (pickable)->gimp);

  return retval;
}


/*  private functions  */

static AutoShrinkType
gimp_pickable_guess_bgcolor (GimpPickable *pickable,
                             guchar       *color,
                             gint          x1,
                             gint          x2,
                             gint          y1,
                             gint          y2)
{
  const Babl *format = babl_format ("R'G'B'A u8");
  guchar      tl[4];
  guchar      tr[4];
  guchar      bl[4];
  guchar      br[4];
  gint        i;

  for (i = 0; i < 4; i++)
    color[i] = 0;

  /* First check if there's transparency to crop. If not, guess the
   * background-color to see if at least 2 corners are equal.
   */

  if (! gimp_pickable_get_pixel_at (pickable, x1, y1, format, tl) ||
      ! gimp_pickable_get_pixel_at (pickable, x1, y2, format, tr) ||
      ! gimp_pickable_get_pixel_at (pickable, x2, y1, format, bl) ||
      ! gimp_pickable_get_pixel_at (pickable, x2, y2, format, br))
    {
      return AUTO_SHRINK_NOTHING;
    }

  if ((tl[ALPHA] == 0 && tr[ALPHA] == 0) ||
      (tl[ALPHA] == 0 && bl[ALPHA] == 0) ||
      (tr[ALPHA] == 0 && br[ALPHA] == 0) ||
      (bl[ALPHA] == 0 && br[ALPHA] == 0))
    {
      return AUTO_SHRINK_ALPHA;
    }

  if (gimp_pickable_colors_equal (tl, tr) ||
      gimp_pickable_colors_equal (tl, bl))
    {
      memcpy (color, tl, 4);
      return AUTO_SHRINK_COLOR;
    }

  if (gimp_pickable_colors_equal (br, bl) ||
      gimp_pickable_colors_equal (br, tr))
    {
      memcpy (color, br, 4);
      return AUTO_SHRINK_COLOR;
    }

  return AUTO_SHRINK_NOTHING;
}

static gboolean
gimp_pickable_colors_equal (guchar *col1,
                            guchar *col2)
{
  gint b;

  for (b = 0; b < 4; b++)
    {
      if (col1[b] != col2[b])
        return FALSE;
    }

  return TRUE;
}

static gboolean
gimp_pickable_colors_alpha (guchar *dummy,
                            guchar *col)
{
  return (col[ALPHA] == 0);
}