1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995-1999 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 <cairo.h>
21 #include <gdk-pixbuf/gdk-pixbuf.h>
22 #include <gegl.h>
23 
24 #include "libgimpbase/gimpbase.h"
25 #include "libgimpcolor/gimpcolor.h"
26 #include "libgimpconfig/gimpconfig.h"
27 
28 #include "core-types.h"
29 
30 #include "config/gimpcoreconfig.h"
31 
32 #include "gegl/gimp-babl.h"
33 
34 #include "gimp.h"
35 #include "gimpbuffer.h"
36 #include "gimpchannel.h"
37 #include "gimpcontext.h"
38 #include "gimpdrawable-fill.h"
39 #include "gimpimage.h"
40 #include "gimpimage-color-profile.h"
41 #include "gimpimage-colormap.h"
42 #include "gimpimage-new.h"
43 #include "gimpimage-undo.h"
44 #include "gimplayer.h"
45 #include "gimplayer-new.h"
46 #include "gimptemplate.h"
47 
48 #include "gimp-intl.h"
49 
50 
51 GimpTemplate *
gimp_image_new_get_last_template(Gimp * gimp,GimpImage * image)52 gimp_image_new_get_last_template (Gimp      *gimp,
53                                   GimpImage *image)
54 {
55   GimpTemplate *template;
56 
57   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
58   g_return_val_if_fail (image == NULL || GIMP_IS_IMAGE (image), NULL);
59 
60   template = gimp_template_new ("image new values");
61 
62   if (image)
63     {
64       gimp_config_sync (G_OBJECT (gimp->config->default_image),
65                         G_OBJECT (template), 0);
66       gimp_template_set_from_image (template, image);
67     }
68   else
69     {
70       gimp_config_sync (G_OBJECT (gimp->image_new_last_template),
71                         G_OBJECT (template), 0);
72     }
73 
74   return template;
75 }
76 
77 void
gimp_image_new_set_last_template(Gimp * gimp,GimpTemplate * template)78 gimp_image_new_set_last_template (Gimp         *gimp,
79                                   GimpTemplate *template)
80 {
81   g_return_if_fail (GIMP_IS_GIMP (gimp));
82   g_return_if_fail (GIMP_IS_TEMPLATE (template));
83 
84   gimp_config_sync (G_OBJECT (template),
85                     G_OBJECT (gimp->image_new_last_template), 0);
86 }
87 
88 GimpImage *
gimp_image_new_from_template(Gimp * gimp,GimpTemplate * template,GimpContext * context)89 gimp_image_new_from_template (Gimp         *gimp,
90                               GimpTemplate *template,
91                               GimpContext  *context)
92 {
93   GimpImage         *image;
94   GimpLayer         *layer;
95   GimpColorProfile  *profile;
96   gint               width, height;
97   gboolean           has_alpha;
98   const gchar       *comment;
99 
100   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
101   g_return_val_if_fail (GIMP_IS_TEMPLATE (template), NULL);
102   g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
103 
104   image = gimp_create_image (gimp,
105                              gimp_template_get_width (template),
106                              gimp_template_get_height (template),
107                              gimp_template_get_base_type (template),
108                              gimp_template_get_precision (template),
109                              FALSE);
110 
111   gimp_image_undo_disable (image);
112 
113   comment = gimp_template_get_comment (template);
114 
115   if (comment)
116     {
117       GimpParasite *parasite;
118 
119       parasite = gimp_parasite_new ("gimp-comment",
120                                     GIMP_PARASITE_PERSISTENT,
121                                     strlen (comment) + 1,
122                                     comment);
123       gimp_image_parasite_attach (image, parasite, FALSE);
124       gimp_parasite_free (parasite);
125     }
126 
127   gimp_image_set_resolution (image,
128                              gimp_template_get_resolution_x (template),
129                              gimp_template_get_resolution_y (template));
130   gimp_image_set_unit (image, gimp_template_get_resolution_unit (template));
131 
132   gimp_image_set_is_color_managed (image,
133                                    gimp_template_get_color_managed (template),
134                                    FALSE);
135   profile = gimp_template_get_color_profile (template);
136   gimp_image_set_color_profile (image, profile, NULL);
137   if (profile)
138     g_object_unref (profile);
139 
140   width  = gimp_image_get_width (image);
141   height = gimp_image_get_height (image);
142 
143   if (gimp_template_get_fill_type (template) == GIMP_FILL_TRANSPARENT)
144     has_alpha = TRUE;
145   else
146     has_alpha = FALSE;
147 
148   layer = gimp_layer_new (image, width, height,
149                           gimp_image_get_layer_format (image, has_alpha),
150                           _("Background"),
151                           GIMP_OPACITY_OPAQUE,
152                           gimp_image_get_default_new_layer_mode (image));
153 
154   gimp_drawable_fill (GIMP_DRAWABLE (layer),
155                       context, gimp_template_get_fill_type (template));
156 
157   gimp_image_add_layer (image, layer, NULL, 0, FALSE);
158 
159   gimp_image_undo_enable (image);
160   gimp_image_clean_all (image);
161 
162   return image;
163 }
164 
165 GimpImage *
gimp_image_new_from_drawable(Gimp * gimp,GimpDrawable * drawable)166 gimp_image_new_from_drawable (Gimp         *gimp,
167                               GimpDrawable *drawable)
168 {
169   GimpItem          *item;
170   GimpImage         *image;
171   GimpImage         *new_image;
172   GimpLayer         *new_layer;
173   GType              new_type;
174   gint               off_x, off_y;
175   GimpImageBaseType  type;
176   gdouble            xres;
177   gdouble            yres;
178   GimpColorProfile  *profile;
179 
180   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
181   g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
182 
183   item  = GIMP_ITEM (drawable);
184   image = gimp_item_get_image (item);
185 
186   type = gimp_drawable_get_base_type (drawable);
187 
188   new_image = gimp_create_image (gimp,
189                                  gimp_item_get_width  (item),
190                                  gimp_item_get_height (item),
191                                  type,
192                                  gimp_drawable_get_precision (drawable),
193                                  TRUE);
194   gimp_image_undo_disable (new_image);
195 
196   if (type == GIMP_INDEXED)
197     gimp_image_set_colormap (new_image,
198                              gimp_image_get_colormap (image),
199                              gimp_image_get_colormap_size (image),
200                              FALSE);
201 
202   gimp_image_get_resolution (image, &xres, &yres);
203   gimp_image_set_resolution (new_image, xres, yres);
204   gimp_image_set_unit (new_image, gimp_image_get_unit (image));
205 
206   gimp_image_set_is_color_managed (new_image,
207                                    gimp_image_get_is_color_managed (image),
208                                    FALSE);
209   profile = gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (drawable));
210   gimp_image_set_color_profile (new_image, profile, NULL);
211 
212   if (GIMP_IS_LAYER (drawable))
213     new_type = G_TYPE_FROM_INSTANCE (drawable);
214   else
215     new_type = GIMP_TYPE_LAYER;
216 
217   new_layer = GIMP_LAYER (gimp_item_convert (GIMP_ITEM (drawable),
218                                              new_image, new_type));
219 
220   gimp_object_set_name (GIMP_OBJECT (new_layer),
221                         gimp_object_get_name (drawable));
222 
223   gimp_item_get_offset (GIMP_ITEM (new_layer), &off_x, &off_y);
224   gimp_item_translate (GIMP_ITEM (new_layer), -off_x, -off_y, FALSE);
225   gimp_item_set_visible (GIMP_ITEM (new_layer), TRUE, FALSE);
226   gimp_item_set_linked (GIMP_ITEM (new_layer), FALSE, FALSE);
227   gimp_layer_set_mode (new_layer,
228                        gimp_image_get_default_new_layer_mode (new_image),
229                        FALSE);
230   gimp_layer_set_opacity (new_layer, GIMP_OPACITY_OPAQUE, FALSE);
231   if (gimp_layer_can_lock_alpha (new_layer))
232     gimp_layer_set_lock_alpha (new_layer, FALSE, FALSE);
233 
234   gimp_image_add_layer (new_image, new_layer, NULL, 0, TRUE);
235 
236   gimp_image_undo_enable (new_image);
237 
238   return new_image;
239 }
240 
241 GimpImage *
gimp_image_new_from_component(Gimp * gimp,GimpImage * image,GimpChannelType component)242 gimp_image_new_from_component (Gimp            *gimp,
243                                GimpImage       *image,
244                                GimpChannelType  component)
245 {
246   GimpImage   *new_image;
247   GimpChannel *channel;
248   GimpLayer   *layer;
249   const gchar *desc;
250   gdouble      xres;
251   gdouble      yres;
252 
253   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
254   g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
255 
256   new_image = gimp_create_image (gimp,
257                                  gimp_image_get_width  (image),
258                                  gimp_image_get_height (image),
259                                  GIMP_GRAY,
260                                  gimp_image_get_precision (image),
261                                  TRUE);
262 
263   gimp_image_undo_disable (new_image);
264 
265   gimp_image_get_resolution (image, &xres, &yres);
266   gimp_image_set_resolution (new_image, xres, yres);
267   gimp_image_set_unit (new_image, gimp_image_get_unit (image));
268 
269   channel = gimp_channel_new_from_component (image, component, NULL, NULL);
270 
271   layer = GIMP_LAYER (gimp_item_convert (GIMP_ITEM (channel),
272                                          new_image, GIMP_TYPE_LAYER));
273   g_object_unref (channel);
274 
275   gimp_enum_get_value (GIMP_TYPE_CHANNEL_TYPE, component,
276                        NULL, NULL, &desc, NULL);
277   gimp_object_take_name (GIMP_OBJECT (layer),
278                          g_strdup_printf (_("%s Channel Copy"), desc));
279 
280   gimp_image_add_layer (new_image, layer, NULL, 0, TRUE);
281 
282   gimp_image_undo_enable (new_image);
283 
284   return new_image;
285 }
286 
287 GimpImage *
gimp_image_new_from_buffer(Gimp * gimp,GimpBuffer * buffer)288 gimp_image_new_from_buffer (Gimp       *gimp,
289                             GimpBuffer *buffer)
290 {
291   GimpImage        *image;
292   GimpLayer        *layer;
293   const Babl       *format;
294   gboolean          has_alpha;
295   gdouble           res_x;
296   gdouble           res_y;
297   GimpColorProfile *profile;
298 
299   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
300   g_return_val_if_fail (GIMP_IS_BUFFER (buffer), NULL);
301 
302   format    = gimp_buffer_get_format (buffer);
303   has_alpha = babl_format_has_alpha (format);
304 
305   image = gimp_create_image (gimp,
306                              gimp_buffer_get_width  (buffer),
307                              gimp_buffer_get_height (buffer),
308                              gimp_babl_format_get_base_type (format),
309                              gimp_babl_format_get_precision (format),
310                              TRUE);
311   gimp_image_undo_disable (image);
312 
313   if (gimp_buffer_get_resolution (buffer, &res_x, &res_y))
314     {
315       gimp_image_set_resolution (image, res_x, res_y);
316       gimp_image_set_unit (image, gimp_buffer_get_unit (buffer));
317     }
318 
319   profile = gimp_buffer_get_color_profile (buffer);
320   gimp_image_set_color_profile (image, profile, NULL);
321 
322   layer = gimp_layer_new_from_buffer (buffer, image,
323                                       gimp_image_get_layer_format (image,
324                                                                    has_alpha),
325                                       _("Pasted Layer"),
326                                       GIMP_OPACITY_OPAQUE,
327                                       gimp_image_get_default_new_layer_mode (image));
328 
329   gimp_image_add_layer (image, layer, NULL, 0, TRUE);
330 
331   gimp_image_undo_enable (image);
332 
333   return image;
334 }
335 
336 GimpImage *
gimp_image_new_from_pixbuf(Gimp * gimp,GdkPixbuf * pixbuf,const gchar * layer_name)337 gimp_image_new_from_pixbuf (Gimp        *gimp,
338                             GdkPixbuf   *pixbuf,
339                             const gchar *layer_name)
340 {
341   GimpImage         *new_image;
342   GimpLayer         *layer;
343   GimpImageBaseType  base_type;
344   gboolean           has_alpha = FALSE;
345   guint8            *icc_data;
346   gsize              icc_len;
347 
348   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
349   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
350 
351   switch (gdk_pixbuf_get_n_channels (pixbuf))
352     {
353     case 2: has_alpha = TRUE;
354     case 1: base_type = GIMP_GRAY;
355       break;
356 
357     case 4: has_alpha = TRUE;
358     case 3: base_type = GIMP_RGB;
359       break;
360 
361     default:
362       g_return_val_if_reached (NULL);
363     }
364 
365   new_image = gimp_create_image (gimp,
366                                  gdk_pixbuf_get_width  (pixbuf),
367                                  gdk_pixbuf_get_height (pixbuf),
368                                  base_type,
369                                  GIMP_PRECISION_U8_GAMMA,
370                                  FALSE);
371 
372   gimp_image_undo_disable (new_image);
373 
374   icc_data = gimp_pixbuf_get_icc_profile (pixbuf, &icc_len);
375   if (icc_data)
376     {
377       gimp_image_set_icc_profile (new_image, icc_data, icc_len, NULL);
378       g_free (icc_data);
379     }
380 
381   layer = gimp_layer_new_from_pixbuf (pixbuf, new_image,
382                                       gimp_image_get_layer_format (new_image,
383                                                                    has_alpha),
384                                       layer_name,
385                                       GIMP_OPACITY_OPAQUE,
386                                       gimp_image_get_default_new_layer_mode (new_image));
387 
388   gimp_image_add_layer (new_image, layer, NULL, 0, TRUE);
389 
390   gimp_image_undo_enable (new_image);
391 
392   return new_image;
393 }
394