gimplayer.c 11.7 KB
Newer Older
1 2 3 4 5
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-2000 Peter Mattis and Spencer Kimball
 *
 * gimplayer.c
 *
6
 * This library is free software: you can redistribute it and/or
7 8
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
9
 * version 3 of the License, or (at your option) any later version.
10 11 12 13 14 15 16
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17 18
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
19 20
 */

Sven Neumann's avatar
Sven Neumann committed
21 22
#include "config.h"

23 24
#include <string.h>

25 26
#define GIMP_DISABLE_DEPRECATION_WARNINGS

27
#include "gimp.h"
Sven Neumann's avatar
Sven Neumann committed
28

29

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/**
 * gimp_layer_new:
 * @image_ID: The image to which to add the layer.
 * @name: The layer name.
 * @width: The layer width.
 * @height: The layer height.
 * @type: The layer type.
 * @opacity: The layer opacity.
 * @mode: The layer combination mode.
 *
 * Create a new layer.
 *
 * This procedure creates a new layer with the specified width, height,
 * and type. Name, opacity, and mode are also supplied parameters. The
 * new layer still needs to be added to the image, as this is not
45
 * automatic. Add the new layer with the gimp_image_insert_layer()
46 47 48 49 50
 * command. Other attributes such as layer mask modes, and offsets
 * should be set with explicit procedure calls.
 *
 * Returns: The newly created layer.
 */
51
gint32
52 53 54 55 56 57 58
gimp_layer_new (gint32         image_ID,
                const gchar   *name,
                gint           width,
                gint           height,
                GimpImageType  type,
                gdouble        opacity,
                GimpLayerMode  mode)
59 60
{
  return _gimp_layer_new (image_ID,
Sven Neumann's avatar
Sven Neumann committed
61 62 63 64 65 66
                          width,
                          height,
                          type,
                          name,
                          opacity,
                          mode);
67 68
}

69 70 71 72 73 74 75 76
/**
 * gimp_layer_copy:
 * @layer_ID: The layer to copy.
 *
 * Copy a layer.
 *
 * This procedure copies the specified layer and returns the copy. The
 * newly copied layer is for use within the original layer's image. It
77
 * should not be subsequently added to any other image.
78 79 80
 *
 * Returns: The newly copied layer.
 */
81 82 83 84 85
gint32
gimp_layer_copy (gint32  layer_ID)
{
  return _gimp_layer_copy (layer_ID, FALSE);
}
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
/**
 * gimp_layer_new_from_pixbuf:
 * @image_ID:       The RGB image to which to add the layer.
 * @name:           The layer name.
 * @pixbuf:         A GdkPixbuf.
 * @opacity:        The layer opacity.
 * @mode:           The layer combination mode.
 * @progress_start: start of progress
 * @progress_end:   end of progress
 *
 * Create a new layer from a %GdkPixbuf.
 *
 * This procedure creates a new layer from the given %GdkPixbuf.  The
 * image has to be an RGB image and just like with gimp_layer_new()
 * you will still need to add the layer to it.
 *
 * If you pass @progress_end > @progress_start to this function,
 * gimp_progress_update() will be called for. You have to call
 * gimp_progress_init() beforehand then.
 *
 * Returns: The newly created layer.
 *
109
 * Since: 2.4
110 111
 */
gint32
112 113 114 115 116 117 118
gimp_layer_new_from_pixbuf (gint32         image_ID,
                            const gchar   *name,
                            GdkPixbuf     *pixbuf,
                            gdouble        opacity,
                            GimpLayerMode  mode,
                            gdouble        progress_start,
                            gdouble        progress_end)
119
{
120 121 122 123 124
  gint32  layer;
  gint    width;
  gint    height;
  gint    bpp;
  gdouble range = progress_end - progress_start;
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

  g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);

  if (gimp_image_base_type (image_ID) != GIMP_RGB)
    {
      g_warning ("gimp_layer_new_from_pixbuf() needs an RGB image");
      return -1;
    }

  if (gdk_pixbuf_get_colorspace (pixbuf) != GDK_COLORSPACE_RGB)
    {
      g_warning ("gimp_layer_new_from_pixbuf() assumes that GdkPixbuf is RGB");
      return -1;
    }

140 141 142
  width     = gdk_pixbuf_get_width (pixbuf);
  height    = gdk_pixbuf_get_height (pixbuf);
  bpp       = gdk_pixbuf_get_n_channels (pixbuf);
143 144 145 146 147 148 149 150

  layer = gimp_layer_new (image_ID, name, width, height,
                          bpp == 3 ? GIMP_RGB_IMAGE : GIMP_RGBA_IMAGE,
                          opacity, mode);

  if (layer == -1)
    return -1;

151 152 153
  if (gimp_plugin_precision_enabled ())
    {
      GeglBuffer *dest_buffer;
154

155
      dest_buffer = gimp_drawable_get_buffer (layer);
156

157 158 159 160
      gegl_buffer_set (dest_buffer, GEGL_RECTANGLE (0, 0, width, height), 0,
                       gimp_pixbuf_get_format (pixbuf),
                       gdk_pixbuf_get_pixels (pixbuf),
                       gdk_pixbuf_get_rowstride (pixbuf));
161

162 163 164
      g_object_unref (dest_buffer);
    }
  else
165
    {
166 167 168 169 170 171 172
      GimpDrawable *drawable;
      GimpPixelRgn  rgn;
      gpointer      pr;
      const guchar *pixels;
      gint          rowstride;
      guint         done  = 0;
      guint         count = 0;
173

174
      drawable = gimp_drawable_get (layer);
175

176
      gimp_pixel_rgn_init (&rgn, drawable, 0, 0, width, height, TRUE, FALSE);
177

178 179 180 181
      g_assert (bpp == rgn.bpp);

      rowstride = gdk_pixbuf_get_rowstride (pixbuf);
      pixels    = gdk_pixbuf_get_pixels (pixbuf);
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
      for (pr = gimp_pixel_rgns_register (1, &rgn);
           pr != NULL;
           pr = gimp_pixel_rgns_process (pr))
        {
          const guchar *src  = pixels + rgn.y * rowstride + rgn.x * bpp;
          guchar       *dest = rgn.data;
          gint          y;

          for (y = 0; y < rgn.h; y++)
            {
              memcpy (dest, src, rgn.w * rgn.bpp);

              src  += rowstride;
              dest += rgn.rowstride;
            }

          if (range > 0.0)
            {
              done += rgn.h * rgn.w;

              if (count++ % 32 == 0)
                gimp_progress_update (progress_start +
                                      (gdouble) done / (width * height) * range);
            }
207
        }
208 209

      gimp_drawable_detach (drawable);
210 211 212 213 214 215 216 217 218 219 220 221
    }

  if (range > 0.0)
    gimp_progress_update (progress_end);

  return layer;
}

/**
 * gimp_layer_new_from_surface:
 * @image_ID:        The RGB image to which to add the layer.
 * @name:            The layer name.
222
 * @surface:         A Cairo image surface.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
 * @progress_start:  start of progress
 * @progress_end:    end of progress
 *
 * Create a new layer from a #cairo_surface_t.
 *
 * This procedure creates a new layer from the given
 * #cairo_surface_t. The image has to be an RGB image and just like
 * with gimp_layer_new() you will still need to add the layer to it.
 *
 * If you pass @progress_end > @progress_start to this function,
 * gimp_progress_update() will be called for. You have to call
 * gimp_progress_init() beforehand then.
 *
 * Returns: The newly created layer.
 *
238
 * Since: 2.8
239 240 241 242 243 244 245 246
 */
gint32
gimp_layer_new_from_surface (gint32                image_ID,
                             const gchar          *name,
                             cairo_surface_t      *surface,
                             gdouble               progress_start,
                             gdouble               progress_end)
{
247 248 249 250 251
  gint32         layer;
  gint           width;
  gint           height;
  cairo_format_t format;
  gdouble        range = progress_end - progress_start;
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

  g_return_val_if_fail (surface != NULL, -1);
  g_return_val_if_fail (cairo_surface_get_type (surface) ==
                        CAIRO_SURFACE_TYPE_IMAGE, -1);

  if (gimp_image_base_type (image_ID) != GIMP_RGB)
    {
      g_warning ("gimp_layer_new_from_surface() needs an RGB image");
      return -1;
    }

  width  = cairo_image_surface_get_width (surface);
  height = cairo_image_surface_get_height (surface);
  format = cairo_image_surface_get_format (surface);

  if (format != CAIRO_FORMAT_ARGB32 &&
      format != CAIRO_FORMAT_RGB24)
    {
      g_warning ("gimp_layer_new_from_surface() assumes that surface is RGB");
      return -1;
    }

  layer = gimp_layer_new (image_ID, name, width, height,
                          format == CAIRO_FORMAT_RGB24 ?
                          GIMP_RGB_IMAGE : GIMP_RGBA_IMAGE,
277 278
                          100.0,
                          gimp_image_get_default_new_layer_mode (image_ID));
279 280 281 282

  if (layer == -1)
    return -1;

283 284 285 286
  if (gimp_plugin_precision_enabled ())
    {
      GeglBuffer *src_buffer;
      GeglBuffer *dest_buffer;
287

288 289
      src_buffer = gimp_cairo_surface_create_buffer (surface);
      dest_buffer = gimp_drawable_get_buffer (layer);
290

291
      gegl_buffer_copy (src_buffer, NULL, GEGL_ABYSS_NONE,
292
                        dest_buffer, NULL);
293

294 295 296 297
      g_object_unref (src_buffer);
      g_object_unref (dest_buffer);
    }
  else
298
    {
299 300 301 302
      GimpDrawable   *drawable;
      GimpPixelRgn    rgn;
      const guchar   *pixels;
      gpointer        pr;
303
      gsize           rowstride;
304 305 306 307
      guint           count = 0;
      guint           done  = 0;

      drawable = gimp_drawable_get (layer);
308

309 310 311 312 313 314 315 316
      gimp_pixel_rgn_init (&rgn, drawable, 0, 0, width, height, TRUE, FALSE);

      rowstride = cairo_image_surface_get_stride (surface);
      pixels    = cairo_image_surface_get_data (surface);

      for (pr = gimp_pixel_rgns_register (1, &rgn);
           pr != NULL;
           pr = gimp_pixel_rgns_process (pr))
317
        {
318 319 320
          const guchar *src  = pixels + rgn.y * rowstride + rgn.x * 4;
          guchar       *dest = rgn.data;
          gint          y;
321

322 323 324 325
          switch (format)
            {
            case CAIRO_FORMAT_RGB24:
              for (y = 0; y < rgn.h; y++)
326
                {
327 328 329
                  const guchar *s = src;
                  guchar       *d = dest;
                  gint          w = rgn.w;
330

331 332 333
                  while (w--)
                    {
                      GIMP_CAIRO_RGB24_GET_PIXEL (s, d[0], d[1], d[2]);
334

335 336 337
                      s += 4;
                      d += 3;
                    }
338

339 340 341 342
                  src  += rowstride;
                  dest += rgn.rowstride;
                }
              break;
343

344 345
            case CAIRO_FORMAT_ARGB32:
              for (y = 0; y < rgn.h; y++)
346
                {
347 348 349
                  const guchar *s = src;
                  guchar       *d = dest;
                  gint          w = rgn.w;
350

351 352 353 354 355 356 357 358 359 360
                  while (w--)
                    {
                      GIMP_CAIRO_ARGB32_GET_PIXEL (s, d[0], d[1], d[2], d[3]);

                      s += 4;
                      d += 4;
                    }

                  src  += rowstride;
                  dest += rgn.rowstride;
361
                }
362
              break;
363

364 365
            default:
              break;
366 367
            }

368 369 370
          if (range > 0.0)
            {
              done += rgn.h * rgn.w;
371

372 373 374 375
              if (count++ % 32 == 0)
                gimp_progress_update (progress_start +
                                      (gdouble) done / (width * height) * range);
            }
376
        }
377 378 379

      gimp_drawable_detach (drawable);
   }
380 381 382 383 384 385 386

  if (range > 0.0)
    gimp_progress_update (progress_end);

  return layer;
}

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
/**
 * gimp_layer_get_preserve_trans:
 * @layer_ID: The layer.
 *
 * This procedure is deprecated! Use gimp_layer_get_lock_alpha() instead.
 *
 * Returns: The layer's preserve transperancy setting.
 */
gboolean
gimp_layer_get_preserve_trans (gint32 layer_ID)
{
  return gimp_layer_get_lock_alpha (layer_ID);
}

/**
 * gimp_layer_set_preserve_trans:
 * @layer_ID: The layer.
 * @preserve_trans: The new layer's preserve transperancy setting.
 *
 * This procedure is deprecated! Use gimp_layer_set_lock_alpha() instead.
 *
 * Returns: TRUE on success.
 */
gboolean
gimp_layer_set_preserve_trans (gint32   layer_ID,
                               gboolean preserve_trans)
{
Manish Singh's avatar
Manish Singh committed
414
  return gimp_layer_set_lock_alpha (layer_ID, preserve_trans);
415
}