summaryrefslogtreecommitdiffstats
path: root/app/gegl/gimp-gegl-mask.c
blob: 325289cd41c67889f479cd799ea7738edbb739fe (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
/* 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 <gegl.h>

#include "gimp-gegl-types.h"

#include "gegl/gimp-gegl-mask.h"


gboolean
gimp_gegl_mask_bounds (GeglBuffer *buffer,
                       gint        *x1,
                       gint        *y1,
                       gint        *x2,
                       gint        *y2)
{
  GeglBufferIterator  *iter;
  const GeglRectangle *extent;
  const GeglRectangle *roi;
  const Babl          *format;
  gint                 bpp;
  gint                 tx1, tx2, ty1, ty2;

  g_return_val_if_fail (GEGL_IS_BUFFER (buffer), FALSE);
  g_return_val_if_fail (x1 != NULL, FALSE);
  g_return_val_if_fail (y1 != NULL, FALSE);
  g_return_val_if_fail (x2 != NULL, FALSE);
  g_return_val_if_fail (y2 != NULL, FALSE);

  extent = gegl_buffer_get_extent (buffer);

  /*  go through and calculate the bounds  */
  tx1 = extent->x + extent->width;
  ty1 = extent->y + extent->height;
  tx2 = extent->x;
  ty2 = extent->y;

  format = gegl_buffer_get_format (buffer);
  bpp    = babl_format_get_bytes_per_pixel (format);

  iter = gegl_buffer_iterator_new (buffer, NULL, 0, format,
                                   GEGL_ACCESS_READ, GEGL_ABYSS_NONE, 1);
  roi = &iter->items[0].roi;

  while (gegl_buffer_iterator_next (iter))
    {
      const guint8 *data_u8 = iter->items[0].data;
      gint          ex      = roi->x + roi->width;
      gint          ey      = roi->y + roi->height;

      /*  only check the pixels if this tile is not fully within the
       *  currently computed bounds
       */
      if (roi->x < tx1 || ex > tx2 ||
          roi->y < ty1 || ey > ty2)
        {
          /* Check upper left and lower right corners to see if we can
           * avoid checking the rest of the pixels in this tile
           */
          if (! gegl_memeq_zero (data_u8,                            bpp) &&
              ! gegl_memeq_zero (data_u8 + (iter->length - 1) * bpp, bpp))
            {
              /*  "ex/ey - 1" because the internal variables are the
               *  right/bottom pixel of the mask's contents, not one
               *  right/below it like the return values.
               */

              if (roi->x < tx1) tx1 = roi->x;
              if (ex > tx2)     tx2 = ex - 1;

              if (roi->y < ty1) ty1 = roi->y;
              if (ey > ty2)     ty2 = ey - 1;
            }
          else
            {
              #define FIND_BOUNDS(bpp, type)                                 \
                G_STMT_START                                                 \
                  {                                                          \
                    const type *data;                                        \
                    gint        y;                                           \
                                                                             \
                    if ((guintptr) data_u8 % bpp)                            \
                      goto generic;                                          \
                                                                             \
                    data = (const type *) data_u8;                           \
                                                                             \
                    for (y = roi->y; y < ey; y++)                            \
                      {                                                      \
                        gint x1;                                             \
                                                                             \
                        for (x1 = 0; x1 < roi->width; x1++)                  \
                          {                                                  \
                            if (data[x1])                                    \
                              {                                              \
                                gint x2;                                     \
                                gint x2_end = MAX (x1, tx2 - roi->x);        \
                                                                             \
                                for (x2 = roi->width - 1; x2 > x2_end; x2--) \
                                  {                                          \
                                    if (data[x2])                            \
                                      break;                                 \
                                  }                                          \
                                                                             \
                                x1 += roi->x;                                \
                                x2 += roi->x;                                \
                                                                             \
                                if (x1 < tx1) tx1 = x1;                      \
                                if (x2 > tx2) tx2 = x2;                      \
                                                                             \
                                if (y < ty1) ty1 = y;                        \
                                if (y > ty2) ty2 = y;                        \
                                                                             \
                                break;                                       \
                              }                                              \
                          }                                                  \
                                                                             \
                        data += roi->width;                                  \
                      }                                                      \
                  }                                                          \
                G_STMT_END

              switch (bpp)
                {
                case 1:
                  FIND_BOUNDS (1, guint8);
                  break;

                case 2:
                  FIND_BOUNDS (2, guint16);
                  break;

                case 4:
                  FIND_BOUNDS (4, guint32);
                  break;

                case 8:
                  FIND_BOUNDS (8, guint64);
                  break;

                default:
                generic:
                  {
                    const guint8 *data = data_u8;
                    gint          y;

                    for (y = roi->y; y < ey; y++)
                      {
                        gint x1;

                        for (x1 = 0; x1 < roi->width; x1++)
                          {
                            if (! gegl_memeq_zero (data + x1 * bpp, bpp))
                              {
                                gint x2;
                                gint x2_end = MAX (x1, tx2 - roi->x);

                                for (x2 = roi->width - 1; x2 > x2_end; x2--)
                                  {
                                    if (! gegl_memeq_zero (data + x2 * bpp,
                                                           bpp))
                                      {
                                        break;
                                      }
                                  }

                                x1 += roi->x;
                                x2 += roi->x;

                                if (x1 < tx1) tx1 = x1;
                                if (x2 > tx2) tx2 = x2;

                                if (y < ty1) ty1 = y;
                                if (y > ty2) ty2 = y;
                              }
                          }

                        data += roi->width * bpp;
                      }
                  }
                  break;
                }

              #undef FIND_BOUNDS
            }
        }
    }

  tx2 = CLAMP (tx2 + 1, 0, gegl_buffer_get_width  (buffer));
  ty2 = CLAMP (ty2 + 1, 0, gegl_buffer_get_height (buffer));

  if (tx1 == gegl_buffer_get_width  (buffer) &&
      ty1 == gegl_buffer_get_height (buffer))
    {
      *x1 = 0;
      *y1 = 0;
      *x2 = gegl_buffer_get_width  (buffer);
      *y2 = gegl_buffer_get_height (buffer);

      return FALSE;
    }

  *x1 = tx1;
  *y1 = ty1;
  *x2 = tx2;
  *y2 = ty2;

  return TRUE;
}

gboolean
gimp_gegl_mask_is_empty (GeglBuffer *buffer)
{
  GeglBufferIterator *iter;
  const Babl         *format;
  gint                bpp;

  g_return_val_if_fail (GEGL_IS_BUFFER (buffer), FALSE);

  format = gegl_buffer_get_format (buffer);
  bpp    = babl_format_get_bytes_per_pixel (format);

  iter = gegl_buffer_iterator_new (buffer, NULL, 0, format,
                                   GEGL_ACCESS_READ, GEGL_ABYSS_NONE, 1);

  while (gegl_buffer_iterator_next (iter))
    {
      if (! gegl_memeq_zero (iter->items[0].data, bpp * iter->length))
        {
          gegl_buffer_iterator_stop (iter);

          return FALSE;
        }
    }

  return TRUE;
}