summaryrefslogtreecommitdiffstats
path: root/app/core/gimplayermask.c
blob: ec0dbfb539911ecad39363e89728c9577217aac5 (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
/* 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 <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "core-types.h"

#include "gegl/gimp-babl.h"

#include "gimperror.h"
#include "gimpimage.h"
#include "gimplayer.h"
#include "gimplayermask.h"

#include "gimp-intl.h"


static void            gimp_layer_mask_preview_freeze       (GimpViewable      *viewable);
static void            gimp_layer_mask_preview_thaw         (GimpViewable      *viewable);

static gboolean        gimp_layer_mask_is_attached          (GimpItem          *item);
static gboolean        gimp_layer_mask_is_content_locked    (GimpItem          *item);
static gboolean        gimp_layer_mask_is_position_locked   (GimpItem          *item);
static GimpItemTree  * gimp_layer_mask_get_tree             (GimpItem          *item);
static GimpItem      * gimp_layer_mask_duplicate            (GimpItem          *item,
                                                             GType              new_type);
static gboolean        gimp_layer_mask_rename               (GimpItem          *item,
                                                             const gchar       *new_name,
                                                             const gchar       *undo_desc,
                                                             GError           **error);

static void            gimp_layer_mask_bounding_box_changed (GimpDrawable      *drawable);
static void            gimp_layer_mask_convert_type         (GimpDrawable      *drawable,
                                                             GimpImage         *dest_image,
                                                             const Babl        *new_format,
                                                             GimpColorProfile  *dest_profile,
                                                             GeglDitherMethod   layer_dither_type,
                                                             GeglDitherMethod   mask_dither_type,
                                                             gboolean           push_undo,
                                                             GimpProgress      *progress);


G_DEFINE_TYPE (GimpLayerMask, gimp_layer_mask, GIMP_TYPE_CHANNEL)

#define parent_class gimp_layer_mask_parent_class


static void
gimp_layer_mask_class_init (GimpLayerMaskClass *klass)
{
  GimpViewableClass *viewable_class = GIMP_VIEWABLE_CLASS (klass);
  GimpItemClass     *item_class     = GIMP_ITEM_CLASS (klass);
  GimpDrawableClass *drawable_class = GIMP_DRAWABLE_CLASS (klass);

  viewable_class->default_icon_name = "gimp-layer-mask";

  viewable_class->preview_freeze       = gimp_layer_mask_preview_freeze;
  viewable_class->preview_thaw         = gimp_layer_mask_preview_thaw;

  item_class->is_attached              = gimp_layer_mask_is_attached;
  item_class->is_content_locked        = gimp_layer_mask_is_content_locked;
  item_class->is_position_locked       = gimp_layer_mask_is_position_locked;
  item_class->get_tree                 = gimp_layer_mask_get_tree;
  item_class->duplicate                = gimp_layer_mask_duplicate;
  item_class->rename                   = gimp_layer_mask_rename;
  item_class->translate_desc           = C_("undo-type", "Move Layer Mask");
  item_class->to_selection_desc        = C_("undo-type", "Layer Mask to Selection");

  drawable_class->bounding_box_changed = gimp_layer_mask_bounding_box_changed;
  drawable_class->convert_type         = gimp_layer_mask_convert_type;
}

static void
gimp_layer_mask_init (GimpLayerMask *layer_mask)
{
  layer_mask->layer = NULL;
}

static void
gimp_layer_mask_preview_freeze (GimpViewable *viewable)
{
  GimpLayerMask *mask  = GIMP_LAYER_MASK (viewable);
  GimpLayer     *layer = gimp_layer_mask_get_layer (mask);

  if (layer)
    {
      GimpViewable *parent = gimp_viewable_get_parent (GIMP_VIEWABLE (layer));

      if (! parent && gimp_item_is_attached (GIMP_ITEM (layer)))
        parent = GIMP_VIEWABLE (gimp_item_get_image (GIMP_ITEM (layer)));

      if (parent)
        gimp_viewable_preview_freeze (parent);
    }
}

static void
gimp_layer_mask_preview_thaw (GimpViewable *viewable)
{
  GimpLayerMask *mask  = GIMP_LAYER_MASK (viewable);
  GimpLayer     *layer = gimp_layer_mask_get_layer (mask);

  if (layer)
    {
      GimpViewable *parent = gimp_viewable_get_parent (GIMP_VIEWABLE (layer));

      if (! parent && gimp_item_is_attached (GIMP_ITEM (layer)))
        parent = GIMP_VIEWABLE (gimp_item_get_image (GIMP_ITEM (layer)));

      if (parent)
        gimp_viewable_preview_thaw (parent);
    }
}

static gboolean
gimp_layer_mask_is_content_locked (GimpItem *item)
{
  GimpLayerMask *mask  = GIMP_LAYER_MASK (item);
  GimpLayer     *layer = gimp_layer_mask_get_layer (mask);

  if (layer)
    return gimp_item_is_content_locked (GIMP_ITEM (layer));

  return FALSE;
}

static gboolean
gimp_layer_mask_is_position_locked (GimpItem *item)
{
  GimpLayerMask *mask  = GIMP_LAYER_MASK (item);
  GimpLayer     *layer = gimp_layer_mask_get_layer (mask);

  if (layer)
    return gimp_item_is_position_locked (GIMP_ITEM (layer));

  return FALSE;
}

static gboolean
gimp_layer_mask_is_attached (GimpItem *item)
{
  GimpLayerMask *mask  = GIMP_LAYER_MASK (item);
  GimpLayer     *layer = gimp_layer_mask_get_layer (mask);

  return (GIMP_IS_IMAGE (gimp_item_get_image (item)) &&
          GIMP_IS_LAYER (layer)                      &&
          gimp_layer_get_mask (layer) == mask        &&
          gimp_item_is_attached (GIMP_ITEM (layer)));
}

static GimpItemTree *
gimp_layer_mask_get_tree (GimpItem *item)
{
  return NULL;
}

static GimpItem *
gimp_layer_mask_duplicate (GimpItem *item,
                           GType     new_type)
{
  GimpItem *new_item;

  g_return_val_if_fail (g_type_is_a (new_type, GIMP_TYPE_DRAWABLE), NULL);

  new_item = GIMP_ITEM_CLASS (parent_class)->duplicate (item, new_type);

  return new_item;
}

static gboolean
gimp_layer_mask_rename (GimpItem     *item,
                        const gchar  *new_name,
                        const gchar  *undo_desc,
                        GError      **error)
{
  /* reject renaming, layer masks are always named "<layer name> mask"  */

  g_set_error (error, GIMP_ERROR, GIMP_FAILED,
               _("Cannot rename layer masks."));

  return FALSE;
}

static void
gimp_layer_mask_bounding_box_changed (GimpDrawable *drawable)
{
  GimpLayerMask *mask  = GIMP_LAYER_MASK (drawable);
  GimpLayer     *layer = gimp_layer_mask_get_layer (mask);

  if (GIMP_DRAWABLE_CLASS (parent_class)->bounding_box_changed)
    GIMP_DRAWABLE_CLASS (parent_class)->bounding_box_changed (drawable);

  if (layer)
    gimp_drawable_update_bounding_box (GIMP_DRAWABLE (layer));
}

static void
gimp_layer_mask_convert_type (GimpDrawable      *drawable,
                              GimpImage         *dest_image,
                              const Babl        *new_format,
                              GimpColorProfile  *dest_profile,
                              GeglDitherMethod   layer_dither_type,
                              GeglDitherMethod   mask_dither_type,
                              gboolean           push_undo,
                              GimpProgress      *progress)
{
  new_format =
    gimp_babl_mask_format (gimp_babl_format_get_precision (new_format));

  GIMP_DRAWABLE_CLASS (parent_class)->convert_type (drawable, dest_image,
                                                    new_format,
                                                    dest_profile,
                                                    layer_dither_type,
                                                    mask_dither_type,
                                                    push_undo,
                                                    progress);
}

GimpLayerMask *
gimp_layer_mask_new (GimpImage     *image,
                     gint           width,
                     gint           height,
                     const gchar   *name,
                     const GimpRGB *color)
{
  GimpLayerMask *layer_mask;

  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
  g_return_val_if_fail (width > 0, NULL);
  g_return_val_if_fail (height > 0, NULL);
  g_return_val_if_fail (color != NULL, NULL);

  layer_mask =
    GIMP_LAYER_MASK (gimp_drawable_new (GIMP_TYPE_LAYER_MASK,
                                        image, name,
                                        0, 0, width, height,
                                        gimp_image_get_mask_format (image)));

  /*  set the layer_mask color and opacity  */
  gimp_channel_set_color (GIMP_CHANNEL (layer_mask), color, FALSE);
  gimp_channel_set_show_masked (GIMP_CHANNEL (layer_mask), TRUE);

  /*  selection mask variables  */
  GIMP_CHANNEL (layer_mask)->x2 = width;
  GIMP_CHANNEL (layer_mask)->y2 = height;

  return layer_mask;
}

void
gimp_layer_mask_set_layer (GimpLayerMask *layer_mask,
                           GimpLayer     *layer)
{
  g_return_if_fail (GIMP_IS_LAYER_MASK (layer_mask));
  g_return_if_fail (layer == NULL || GIMP_IS_LAYER (layer));

  layer_mask->layer = layer;

  if (layer)
    {
      gchar *mask_name;
      gint   offset_x;
      gint   offset_y;

      gimp_item_get_offset (GIMP_ITEM (layer), &offset_x, &offset_y);
      gimp_item_set_offset (GIMP_ITEM (layer_mask), offset_x, offset_y);

      mask_name = g_strdup_printf (_("%s mask"), gimp_object_get_name (layer));

      gimp_object_take_name (GIMP_OBJECT (layer_mask), mask_name);
    }
}

GimpLayer *
gimp_layer_mask_get_layer (GimpLayerMask *layer_mask)
{
  g_return_val_if_fail (GIMP_IS_LAYER_MASK (layer_mask), NULL);

  return layer_mask->layer;
}