gimpimage-new.c 12.3 KB
Newer Older
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 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17 18 19
 */

#include "config.h"

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 31
#include "config/gimpcoreconfig.h"

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 44
#include "gimpimage-undo.h"
#include "gimplayer.h"
45
#include "gimplayer-new.h"
46
#include "gimptemplate.h"
47

48
#include "gimp-intl.h"
49 50


51
GimpTemplate *
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 65 66 67
    {
      gimp_config_sync (G_OBJECT (gimp->config->default_image),
                        G_OBJECT (template), 0);
      gimp_template_set_from_image (template, image);
    }
68
  else
69 70 71 72
    {
      gimp_config_sync (G_OBJECT (gimp->image_new_last_template),
                        G_OBJECT (template), 0);
    }
73

74
  return template;
75 76 77
}

void
78 79
gimp_image_new_set_last_template (Gimp         *gimp,
                                  GimpTemplate *template)
80
{
81 82
  g_return_if_fail (GIMP_IS_GIMP (gimp));
  g_return_if_fail (GIMP_IS_TEMPLATE (template));
83

84 85
  gimp_config_sync (G_OBJECT (template),
                    G_OBJECT (gimp->image_new_last_template), 0);
86
}
87

88 89 90 91 92
GimpImage *
gimp_image_new_from_template (Gimp         *gimp,
                              GimpTemplate *template,
                              GimpContext  *context)
{
93 94 95 96 97 98
  GimpImage         *image;
  GimpLayer         *layer;
  GimpColorProfile  *profile;
  gint               width, height;
  gboolean           has_alpha;
  const gchar       *comment;
99 100 101 102 103 104

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (GIMP_IS_TEMPLATE (template), NULL);
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);

  image = gimp_create_image (gimp,
105 106
                             gimp_template_get_width (template),
                             gimp_template_get_height (template),
107
                             gimp_template_get_base_type (template),
108
                             gimp_template_get_precision (template),
109 110 111 112
                             FALSE);

  gimp_image_undo_disable (image);

113 114 115
  comment = gimp_template_get_comment (template);

  if (comment)
116 117 118 119 120
    {
      GimpParasite *parasite;

      parasite = gimp_parasite_new ("gimp-comment",
                                    GIMP_PARASITE_PERSISTENT,
121 122
                                    strlen (comment) + 1,
                                    comment);
123 124 125 126 127
      gimp_image_parasite_attach (image, parasite);
      gimp_parasite_free (parasite);
    }

  gimp_image_set_resolution (image,
128 129 130
                             gimp_template_get_resolution_x (template),
                             gimp_template_get_resolution_y (template));
  gimp_image_set_unit (image, gimp_template_get_resolution_unit (template));
131

132 133 134 135 136
  profile = gimp_template_get_color_profile (template);
  gimp_image_set_color_profile (image, profile, NULL);
  if (profile)
    g_object_unref (profile);

137 138 139
  width  = gimp_image_get_width (image);
  height = gimp_image_get_height (image);

140
  if (gimp_template_get_fill_type (template) == GIMP_FILL_TRANSPARENT)
141 142 143
    has_alpha = TRUE;
  else
    has_alpha = FALSE;
144

145
  layer = gimp_layer_new (image, width, height,
146
                          gimp_image_get_layer_format (image, has_alpha),
147
                          _("Background"),
148
                          GIMP_OPACITY_OPAQUE,
149
                          gimp_image_get_default_new_layer_mode (image));
150

151 152
  gimp_drawable_fill (GIMP_DRAWABLE (layer),
                      context, gimp_template_get_fill_type (template));
153 154 155 156 157 158 159 160 161

  gimp_image_add_layer (image, layer, NULL, 0, FALSE);

  gimp_image_undo_enable (image);
  gimp_image_clean_all (image);

  return image;
}

162 163 164 165 166 167 168 169 170 171 172 173 174
GimpImage *
gimp_image_new_from_drawable (Gimp         *gimp,
                              GimpDrawable *drawable)
{
  GimpItem          *item;
  GimpImage         *image;
  GimpImage         *new_image;
  GimpLayer         *new_layer;
  GType              new_type;
  gint               off_x, off_y;
  GimpImageBaseType  type;
  gdouble            xres;
  gdouble            yres;
175
  GimpColorProfile  *profile;
176 177 178 179 180 181 182

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);

  item  = GIMP_ITEM (drawable);
  image = gimp_item_get_image (item);

183
  type = gimp_drawable_get_base_type (drawable);
184 185 186 187

  new_image = gimp_create_image (gimp,
                                 gimp_item_get_width  (item),
                                 gimp_item_get_height (item),
188 189 190
                                 type,
                                 gimp_drawable_get_precision (drawable),
                                 TRUE);
191 192 193 194 195 196 197 198 199 200 201 202
  gimp_image_undo_disable (new_image);

  if (type == GIMP_INDEXED)
    gimp_image_set_colormap (new_image,
                             gimp_image_get_colormap (image),
                             gimp_image_get_colormap_size (image),
                             FALSE);

  gimp_image_get_resolution (image, &xres, &yres);
  gimp_image_set_resolution (new_image, xres, yres);
  gimp_image_set_unit (new_image, gimp_image_get_unit (image));

203 204
  profile = gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (drawable));
  gimp_image_set_color_profile (new_image, profile, NULL);
205

206 207
  if (GIMP_IS_LAYER (drawable))
    new_type = G_TYPE_FROM_INSTANCE (drawable);
208
  else
209
    new_type = GIMP_TYPE_LAYER;
210 211 212 213 214 215 216 217 218 219 220

  new_layer = GIMP_LAYER (gimp_item_convert (GIMP_ITEM (drawable),
                                             new_image, new_type));

  gimp_object_set_name (GIMP_OBJECT (new_layer),
                        gimp_object_get_name (drawable));

  gimp_item_get_offset (GIMP_ITEM (new_layer), &off_x, &off_y);
  gimp_item_translate (GIMP_ITEM (new_layer), -off_x, -off_y, FALSE);
  gimp_item_set_visible (GIMP_ITEM (new_layer), TRUE, FALSE);
  gimp_item_set_linked (GIMP_ITEM (new_layer), FALSE, FALSE);
221
  gimp_layer_set_mode (new_layer,
222
                       gimp_image_get_default_new_layer_mode (new_image),
223
                       FALSE);
224
  gimp_layer_set_opacity (new_layer, GIMP_OPACITY_OPAQUE, FALSE);
225 226
  if (gimp_layer_can_lock_alpha (new_layer))
    gimp_layer_set_lock_alpha (new_layer, FALSE, FALSE);
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

  gimp_image_add_layer (new_image, new_layer, NULL, 0, TRUE);

  gimp_image_undo_enable (new_image);

  return new_image;
}

GimpImage *
gimp_image_new_from_component (Gimp            *gimp,
                               GimpImage       *image,
                               GimpChannelType  component)
{
  GimpImage   *new_image;
  GimpChannel *channel;
  GimpLayer   *layer;
  const gchar *desc;
  gdouble      xres;
  gdouble      yres;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);

  new_image = gimp_create_image (gimp,
                                 gimp_image_get_width  (image),
                                 gimp_image_get_height (image),
253 254 255
                                 GIMP_GRAY,
                                 gimp_image_get_precision (image),
                                 TRUE);
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

  gimp_image_undo_disable (new_image);

  gimp_image_get_resolution (image, &xres, &yres);
  gimp_image_set_resolution (new_image, xres, yres);
  gimp_image_set_unit (new_image, gimp_image_get_unit (image));

  channel = gimp_channel_new_from_component (image, component, NULL, NULL);

  layer = GIMP_LAYER (gimp_item_convert (GIMP_ITEM (channel),
                                         new_image, GIMP_TYPE_LAYER));
  g_object_unref (channel);

  gimp_enum_get_value (GIMP_TYPE_CHANNEL_TYPE, component,
                       NULL, NULL, &desc, NULL);
  gimp_object_take_name (GIMP_OBJECT (layer),
                         g_strdup_printf (_("%s Channel Copy"), desc));

  gimp_image_add_layer (new_image, layer, NULL, 0, TRUE);

  gimp_image_undo_enable (new_image);

  return new_image;
}

281 282
GimpImage *
gimp_image_new_from_buffer (Gimp       *gimp,
283
                            GimpBuffer *buffer)
284
{
285 286 287 288
  GimpImage        *image;
  GimpLayer        *layer;
  const Babl       *format;
  gboolean          has_alpha;
289 290
  gdouble           res_x;
  gdouble           res_y;
291
  GimpColorProfile *profile;
292 293

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
294
  g_return_val_if_fail (GIMP_IS_BUFFER (buffer), NULL);
295

296
  format    = gimp_buffer_get_format (buffer);
297
  has_alpha = babl_format_has_alpha (format);
298 299

  image = gimp_create_image (gimp,
300 301
                             gimp_buffer_get_width  (buffer),
                             gimp_buffer_get_height (buffer),
302
                             gimp_babl_format_get_base_type (format),
303
                             gimp_babl_format_get_precision (format),
304 305 306
                             TRUE);
  gimp_image_undo_disable (image);

307
  if (gimp_buffer_get_resolution (buffer, &res_x, &res_y))
308
    {
309 310
      gimp_image_set_resolution (image, res_x, res_y);
      gimp_image_set_unit (image, gimp_buffer_get_unit (buffer));
311 312
    }

313
  profile = gimp_buffer_get_color_profile (buffer);
314
  gimp_image_set_color_profile (image, profile, NULL);
315

316
  layer = gimp_layer_new_from_buffer (buffer, image,
317 318
                                      gimp_image_get_layer_format (image,
                                                                   has_alpha),
319
                                      _("Pasted Layer"),
320
                                      GIMP_OPACITY_OPAQUE,
321
                                      gimp_image_get_default_new_layer_mode (image));
322 323 324 325 326 327 328 329

  gimp_image_add_layer (image, layer, NULL, 0, TRUE);

  gimp_image_undo_enable (image);

  return image;
}

330 331 332 333 334
GimpImage *
gimp_image_new_from_pixbuf (Gimp        *gimp,
                            GdkPixbuf   *pixbuf,
                            const gchar *layer_name)
{
335 336 337
  GimpImage         *new_image;
  GimpLayer         *layer;
  GimpImageBaseType  base_type;
338
  gboolean           has_alpha = FALSE;
339 340
  guint8            *icc_data;
  gsize              icc_len;
341 342 343 344 345 346

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);

  switch (gdk_pixbuf_get_n_channels (pixbuf))
    {
347 348 349 350 351 352
    case 2: has_alpha = TRUE;
    case 1: base_type = GIMP_GRAY;
      break;

    case 4: has_alpha = TRUE;
    case 3: base_type = GIMP_RGB;
353 354 355 356 357 358 359 360 361
      break;

    default:
      g_return_val_if_reached (NULL);
    }

  new_image = gimp_create_image (gimp,
                                 gdk_pixbuf_get_width  (pixbuf),
                                 gdk_pixbuf_get_height (pixbuf),
362
                                 base_type,
363
                                 GIMP_PRECISION_U8_NON_LINEAR,
364 365 366 367
                                 FALSE);

  gimp_image_undo_disable (new_image);

368 369 370 371 372 373 374
  icc_data = gimp_pixbuf_get_icc_profile (pixbuf, &icc_len);
  if (icc_data)
    {
      gimp_image_set_icc_profile (new_image, icc_data, icc_len, NULL);
      g_free (icc_data);
    }

375
  layer = gimp_layer_new_from_pixbuf (pixbuf, new_image,
376 377
                                      gimp_image_get_layer_format (new_image,
                                                                   has_alpha),
378
                                      layer_name,
379
                                      GIMP_OPACITY_OPAQUE,
380
                                      gimp_image_get_default_new_layer_mode (new_image));
381 382 383 384 385 386 387

  gimp_image_add_layer (new_image, layer, NULL, 0, TRUE);

  gimp_image_undo_enable (new_image);

  return new_image;
}