1 /* GIMP - The GNU Image Manipulation Program
2 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19
20 #include <gdk-pixbuf/gdk-pixbuf.h>
21 #include <gegl.h>
22
23 #include "core-types.h"
24
25 #include "gegl/gimp-babl.h"
26
27 #include "gimperror.h"
28 #include "gimpimage.h"
29 #include "gimplayer.h"
30 #include "gimplayermask.h"
31
32 #include "gimp-intl.h"
33
34
35 static void gimp_layer_mask_preview_freeze (GimpViewable *viewable);
36 static void gimp_layer_mask_preview_thaw (GimpViewable *viewable);
37
38 static gboolean gimp_layer_mask_is_attached (GimpItem *item);
39 static gboolean gimp_layer_mask_is_content_locked (GimpItem *item);
40 static gboolean gimp_layer_mask_is_position_locked (GimpItem *item);
41 static GimpItemTree * gimp_layer_mask_get_tree (GimpItem *item);
42 static GimpItem * gimp_layer_mask_duplicate (GimpItem *item,
43 GType new_type);
44 static gboolean gimp_layer_mask_rename (GimpItem *item,
45 const gchar *new_name,
46 const gchar *undo_desc,
47 GError **error);
48
49 static void gimp_layer_mask_bounding_box_changed (GimpDrawable *drawable);
50 static void gimp_layer_mask_convert_type (GimpDrawable *drawable,
51 GimpImage *dest_image,
52 const Babl *new_format,
53 GimpColorProfile *dest_profile,
54 GeglDitherMethod layer_dither_type,
55 GeglDitherMethod mask_dither_type,
56 gboolean push_undo,
57 GimpProgress *progress);
58
59
G_DEFINE_TYPE(GimpLayerMask,gimp_layer_mask,GIMP_TYPE_CHANNEL)60 G_DEFINE_TYPE (GimpLayerMask, gimp_layer_mask, GIMP_TYPE_CHANNEL)
61
62 #define parent_class gimp_layer_mask_parent_class
63
64
65 static void
66 gimp_layer_mask_class_init (GimpLayerMaskClass *klass)
67 {
68 GimpViewableClass *viewable_class = GIMP_VIEWABLE_CLASS (klass);
69 GimpItemClass *item_class = GIMP_ITEM_CLASS (klass);
70 GimpDrawableClass *drawable_class = GIMP_DRAWABLE_CLASS (klass);
71
72 viewable_class->default_icon_name = "gimp-layer-mask";
73
74 viewable_class->preview_freeze = gimp_layer_mask_preview_freeze;
75 viewable_class->preview_thaw = gimp_layer_mask_preview_thaw;
76
77 item_class->is_attached = gimp_layer_mask_is_attached;
78 item_class->is_content_locked = gimp_layer_mask_is_content_locked;
79 item_class->is_position_locked = gimp_layer_mask_is_position_locked;
80 item_class->get_tree = gimp_layer_mask_get_tree;
81 item_class->duplicate = gimp_layer_mask_duplicate;
82 item_class->rename = gimp_layer_mask_rename;
83 item_class->translate_desc = C_("undo-type", "Move Layer Mask");
84 item_class->to_selection_desc = C_("undo-type", "Layer Mask to Selection");
85
86 drawable_class->bounding_box_changed = gimp_layer_mask_bounding_box_changed;
87 drawable_class->convert_type = gimp_layer_mask_convert_type;
88 }
89
90 static void
gimp_layer_mask_init(GimpLayerMask * layer_mask)91 gimp_layer_mask_init (GimpLayerMask *layer_mask)
92 {
93 layer_mask->layer = NULL;
94 }
95
96 static void
gimp_layer_mask_preview_freeze(GimpViewable * viewable)97 gimp_layer_mask_preview_freeze (GimpViewable *viewable)
98 {
99 GimpLayerMask *mask = GIMP_LAYER_MASK (viewable);
100 GimpLayer *layer = gimp_layer_mask_get_layer (mask);
101
102 if (layer)
103 {
104 GimpViewable *parent = gimp_viewable_get_parent (GIMP_VIEWABLE (layer));
105
106 if (! parent && gimp_item_is_attached (GIMP_ITEM (layer)))
107 parent = GIMP_VIEWABLE (gimp_item_get_image (GIMP_ITEM (layer)));
108
109 if (parent)
110 gimp_viewable_preview_freeze (parent);
111 }
112 }
113
114 static void
gimp_layer_mask_preview_thaw(GimpViewable * viewable)115 gimp_layer_mask_preview_thaw (GimpViewable *viewable)
116 {
117 GimpLayerMask *mask = GIMP_LAYER_MASK (viewable);
118 GimpLayer *layer = gimp_layer_mask_get_layer (mask);
119
120 if (layer)
121 {
122 GimpViewable *parent = gimp_viewable_get_parent (GIMP_VIEWABLE (layer));
123
124 if (! parent && gimp_item_is_attached (GIMP_ITEM (layer)))
125 parent = GIMP_VIEWABLE (gimp_item_get_image (GIMP_ITEM (layer)));
126
127 if (parent)
128 gimp_viewable_preview_thaw (parent);
129 }
130 }
131
132 static gboolean
gimp_layer_mask_is_content_locked(GimpItem * item)133 gimp_layer_mask_is_content_locked (GimpItem *item)
134 {
135 GimpLayerMask *mask = GIMP_LAYER_MASK (item);
136 GimpLayer *layer = gimp_layer_mask_get_layer (mask);
137
138 if (layer)
139 return gimp_item_is_content_locked (GIMP_ITEM (layer));
140
141 return FALSE;
142 }
143
144 static gboolean
gimp_layer_mask_is_position_locked(GimpItem * item)145 gimp_layer_mask_is_position_locked (GimpItem *item)
146 {
147 GimpLayerMask *mask = GIMP_LAYER_MASK (item);
148 GimpLayer *layer = gimp_layer_mask_get_layer (mask);
149
150 if (layer)
151 return gimp_item_is_position_locked (GIMP_ITEM (layer));
152
153 return FALSE;
154 }
155
156 static gboolean
gimp_layer_mask_is_attached(GimpItem * item)157 gimp_layer_mask_is_attached (GimpItem *item)
158 {
159 GimpLayerMask *mask = GIMP_LAYER_MASK (item);
160 GimpLayer *layer = gimp_layer_mask_get_layer (mask);
161
162 return (GIMP_IS_IMAGE (gimp_item_get_image (item)) &&
163 GIMP_IS_LAYER (layer) &&
164 gimp_layer_get_mask (layer) == mask &&
165 gimp_item_is_attached (GIMP_ITEM (layer)));
166 }
167
168 static GimpItemTree *
gimp_layer_mask_get_tree(GimpItem * item)169 gimp_layer_mask_get_tree (GimpItem *item)
170 {
171 return NULL;
172 }
173
174 static GimpItem *
gimp_layer_mask_duplicate(GimpItem * item,GType new_type)175 gimp_layer_mask_duplicate (GimpItem *item,
176 GType new_type)
177 {
178 GimpItem *new_item;
179
180 g_return_val_if_fail (g_type_is_a (new_type, GIMP_TYPE_DRAWABLE), NULL);
181
182 new_item = GIMP_ITEM_CLASS (parent_class)->duplicate (item, new_type);
183
184 return new_item;
185 }
186
187 static gboolean
gimp_layer_mask_rename(GimpItem * item,const gchar * new_name,const gchar * undo_desc,GError ** error)188 gimp_layer_mask_rename (GimpItem *item,
189 const gchar *new_name,
190 const gchar *undo_desc,
191 GError **error)
192 {
193 /* reject renaming, layer masks are always named "<layer name> mask" */
194
195 g_set_error (error, GIMP_ERROR, GIMP_FAILED,
196 _("Cannot rename layer masks."));
197
198 return FALSE;
199 }
200
201 static void
gimp_layer_mask_bounding_box_changed(GimpDrawable * drawable)202 gimp_layer_mask_bounding_box_changed (GimpDrawable *drawable)
203 {
204 GimpLayerMask *mask = GIMP_LAYER_MASK (drawable);
205 GimpLayer *layer = gimp_layer_mask_get_layer (mask);
206
207 if (GIMP_DRAWABLE_CLASS (parent_class)->bounding_box_changed)
208 GIMP_DRAWABLE_CLASS (parent_class)->bounding_box_changed (drawable);
209
210 if (layer)
211 gimp_drawable_update_bounding_box (GIMP_DRAWABLE (layer));
212 }
213
214 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)215 gimp_layer_mask_convert_type (GimpDrawable *drawable,
216 GimpImage *dest_image,
217 const Babl *new_format,
218 GimpColorProfile *dest_profile,
219 GeglDitherMethod layer_dither_type,
220 GeglDitherMethod mask_dither_type,
221 gboolean push_undo,
222 GimpProgress *progress)
223 {
224 new_format =
225 gimp_babl_mask_format (gimp_babl_format_get_precision (new_format));
226
227 GIMP_DRAWABLE_CLASS (parent_class)->convert_type (drawable, dest_image,
228 new_format,
229 dest_profile,
230 layer_dither_type,
231 mask_dither_type,
232 push_undo,
233 progress);
234 }
235
236 GimpLayerMask *
gimp_layer_mask_new(GimpImage * image,gint width,gint height,const gchar * name,const GimpRGB * color)237 gimp_layer_mask_new (GimpImage *image,
238 gint width,
239 gint height,
240 const gchar *name,
241 const GimpRGB *color)
242 {
243 GimpLayerMask *layer_mask;
244
245 g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
246 g_return_val_if_fail (width > 0, NULL);
247 g_return_val_if_fail (height > 0, NULL);
248 g_return_val_if_fail (color != NULL, NULL);
249
250 layer_mask =
251 GIMP_LAYER_MASK (gimp_drawable_new (GIMP_TYPE_LAYER_MASK,
252 image, name,
253 0, 0, width, height,
254 gimp_image_get_mask_format (image)));
255
256 /* set the layer_mask color and opacity */
257 gimp_channel_set_color (GIMP_CHANNEL (layer_mask), color, FALSE);
258 gimp_channel_set_show_masked (GIMP_CHANNEL (layer_mask), TRUE);
259
260 /* selection mask variables */
261 GIMP_CHANNEL (layer_mask)->x2 = width;
262 GIMP_CHANNEL (layer_mask)->y2 = height;
263
264 return layer_mask;
265 }
266
267 void
gimp_layer_mask_set_layer(GimpLayerMask * layer_mask,GimpLayer * layer)268 gimp_layer_mask_set_layer (GimpLayerMask *layer_mask,
269 GimpLayer *layer)
270 {
271 g_return_if_fail (GIMP_IS_LAYER_MASK (layer_mask));
272 g_return_if_fail (layer == NULL || GIMP_IS_LAYER (layer));
273
274 layer_mask->layer = layer;
275
276 if (layer)
277 {
278 gchar *mask_name;
279 gint offset_x;
280 gint offset_y;
281
282 gimp_item_get_offset (GIMP_ITEM (layer), &offset_x, &offset_y);
283 gimp_item_set_offset (GIMP_ITEM (layer_mask), offset_x, offset_y);
284
285 mask_name = g_strdup_printf (_("%s mask"), gimp_object_get_name (layer));
286
287 gimp_object_take_name (GIMP_OBJECT (layer_mask), mask_name);
288 }
289 }
290
291 GimpLayer *
gimp_layer_mask_get_layer(GimpLayerMask * layer_mask)292 gimp_layer_mask_get_layer (GimpLayerMask *layer_mask)
293 {
294 g_return_val_if_fail (GIMP_IS_LAYER_MASK (layer_mask), NULL);
295
296 return layer_mask->layer;
297 }
298