2000-06-01 22:59:22 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
|
|
|
* Copyright (C) 1995-2000 Peter Mattis and Spencer Kimball
|
|
|
|
*
|
|
|
|
* gimplayer.c
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This library is free software: you can redistribute it and/or
|
2000-06-01 22:59:22 +08:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2009-01-18 06:28:01 +08:00
|
|
|
* version 3 of the License, or (at your option) any later version.
|
2000-06-01 22:59:22 +08:00
|
|
|
*
|
|
|
|
* 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
|
2009-01-18 06:28:01 +08:00
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
2000-06-01 22:59:22 +08:00
|
|
|
*/
|
|
|
|
|
2002-05-14 07:30:23 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-04-21 02:04:35 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2012-05-03 10:13:39 +08:00
|
|
|
#define GIMP_DISABLE_DEPRECATION_WARNINGS
|
|
|
|
|
2000-06-01 22:59:22 +08:00
|
|
|
#include "gimp.h"
|
2002-05-14 07:30:23 +08:00
|
|
|
|
2011-04-21 05:58:00 +08:00
|
|
|
|
2000-08-25 08:37:35 +08:00
|
|
|
/**
|
|
|
|
* 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
|
2010-09-06 06:03:29 +08:00
|
|
|
* automatic. Add the new layer with the gimp_image_insert_layer()
|
2000-08-25 08:37:35 +08:00
|
|
|
* command. Other attributes such as layer mask modes, and offsets
|
|
|
|
* should be set with explicit procedure calls.
|
|
|
|
*
|
|
|
|
* Returns: The newly created layer.
|
|
|
|
*/
|
2000-06-01 22:59:22 +08:00
|
|
|
gint32
|
|
|
|
gimp_layer_new (gint32 image_ID,
|
2006-04-12 18:53:28 +08:00
|
|
|
const gchar *name,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
|
|
|
GimpImageType type,
|
|
|
|
gdouble opacity,
|
|
|
|
GimpLayerModeEffects mode)
|
2000-06-01 22:59:22 +08:00
|
|
|
{
|
|
|
|
return _gimp_layer_new (image_ID,
|
2006-04-12 18:53:28 +08:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
type,
|
|
|
|
name,
|
|
|
|
opacity,
|
|
|
|
mode);
|
2000-06-01 22:59:22 +08:00
|
|
|
}
|
|
|
|
|
2000-08-25 08:37:35 +08:00
|
|
|
/**
|
|
|
|
* 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
|
2007-10-16 14:02:18 +08:00
|
|
|
* should not be subsequently added to any other image.
|
2000-08-25 08:37:35 +08:00
|
|
|
*
|
|
|
|
* Returns: The newly copied layer.
|
|
|
|
*/
|
2000-06-01 22:59:22 +08:00
|
|
|
gint32
|
|
|
|
gimp_layer_copy (gint32 layer_ID)
|
|
|
|
{
|
|
|
|
return _gimp_layer_copy (layer_ID, FALSE);
|
|
|
|
}
|
2005-07-11 05:17:22 +08:00
|
|
|
|
2011-04-21 02:04:35 +08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Since: GIMP 2.4
|
|
|
|
*/
|
|
|
|
gint32
|
|
|
|
gimp_layer_new_from_pixbuf (gint32 image_ID,
|
|
|
|
const gchar *name,
|
|
|
|
GdkPixbuf *pixbuf,
|
|
|
|
gdouble opacity,
|
|
|
|
GimpLayerModeEffects mode,
|
|
|
|
gdouble progress_start,
|
|
|
|
gdouble progress_end)
|
|
|
|
{
|
2012-05-03 09:48:49 +08:00
|
|
|
gint32 layer;
|
|
|
|
gint width;
|
|
|
|
gint height;
|
|
|
|
gint bpp;
|
|
|
|
gdouble range = progress_end - progress_start;
|
2011-04-21 02:04:35 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-05-03 09:48:49 +08:00
|
|
|
width = gdk_pixbuf_get_width (pixbuf);
|
|
|
|
height = gdk_pixbuf_get_height (pixbuf);
|
|
|
|
bpp = gdk_pixbuf_get_n_channels (pixbuf);
|
2011-04-21 02:04:35 +08:00
|
|
|
|
|
|
|
layer = gimp_layer_new (image_ID, name, width, height,
|
|
|
|
bpp == 3 ? GIMP_RGB_IMAGE : GIMP_RGBA_IMAGE,
|
|
|
|
opacity, mode);
|
|
|
|
|
|
|
|
if (layer == -1)
|
|
|
|
return -1;
|
|
|
|
|
2012-05-03 09:48:49 +08:00
|
|
|
if (gimp_plugin_precision_enabled ())
|
|
|
|
{
|
|
|
|
GeglBuffer *dest_buffer;
|
2011-04-21 02:04:35 +08:00
|
|
|
|
2012-05-03 09:48:49 +08:00
|
|
|
dest_buffer = gimp_drawable_get_buffer (layer);
|
2011-04-21 02:04:35 +08:00
|
|
|
|
2014-02-21 01:20:17 +08:00
|
|
|
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));
|
2011-04-21 02:04:35 +08:00
|
|
|
|
2012-05-03 09:48:49 +08:00
|
|
|
g_object_unref (dest_buffer);
|
|
|
|
}
|
|
|
|
else
|
2011-04-21 02:04:35 +08:00
|
|
|
{
|
2012-05-03 09:48:49 +08:00
|
|
|
GimpDrawable *drawable;
|
|
|
|
GimpPixelRgn rgn;
|
|
|
|
gpointer pr;
|
|
|
|
const guchar *pixels;
|
|
|
|
gint rowstride;
|
|
|
|
guint done = 0;
|
|
|
|
guint count = 0;
|
2011-04-21 02:04:35 +08:00
|
|
|
|
2012-05-03 09:48:49 +08:00
|
|
|
drawable = gimp_drawable_get (layer);
|
2011-04-21 02:04:35 +08:00
|
|
|
|
2012-05-03 09:48:49 +08:00
|
|
|
gimp_pixel_rgn_init (&rgn, drawable, 0, 0, width, height, TRUE, FALSE);
|
2011-04-21 02:04:35 +08:00
|
|
|
|
2012-05-03 09:48:49 +08:00
|
|
|
g_assert (bpp == rgn.bpp);
|
|
|
|
|
|
|
|
rowstride = gdk_pixbuf_get_rowstride (pixbuf);
|
|
|
|
pixels = gdk_pixbuf_get_pixels (pixbuf);
|
2011-04-21 02:04:35 +08:00
|
|
|
|
2012-05-03 09:48:49 +08:00
|
|
|
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);
|
|
|
|
}
|
2011-04-21 05:58:00 +08:00
|
|
|
}
|
2012-05-03 09:48:49 +08:00
|
|
|
|
|
|
|
gimp_drawable_detach (drawable);
|
2011-04-21 05:58:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2011-04-27 23:05:56 +08:00
|
|
|
* @surface: A Cairo image surface.
|
2011-04-21 05:58:00 +08:00
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* Since: GIMP 2.8
|
|
|
|
*/
|
|
|
|
gint32
|
|
|
|
gimp_layer_new_from_surface (gint32 image_ID,
|
|
|
|
const gchar *name,
|
|
|
|
cairo_surface_t *surface,
|
|
|
|
gdouble progress_start,
|
|
|
|
gdouble progress_end)
|
|
|
|
{
|
2012-05-03 10:13:39 +08:00
|
|
|
gint32 layer;
|
|
|
|
gint width;
|
|
|
|
gint height;
|
|
|
|
cairo_format_t format;
|
|
|
|
gdouble range = progress_end - progress_start;
|
2011-04-21 05:58:00 +08:00
|
|
|
|
|
|
|
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,
|
2011-04-22 03:07:50 +08:00
|
|
|
100.0, GIMP_NORMAL_MODE);
|
2011-04-21 05:58:00 +08:00
|
|
|
|
|
|
|
if (layer == -1)
|
|
|
|
return -1;
|
|
|
|
|
2012-05-03 10:13:39 +08:00
|
|
|
if (gimp_plugin_precision_enabled ())
|
|
|
|
{
|
|
|
|
GeglBuffer *src_buffer;
|
|
|
|
GeglBuffer *dest_buffer;
|
2011-04-21 05:58:00 +08:00
|
|
|
|
2012-05-03 10:13:39 +08:00
|
|
|
src_buffer = gimp_cairo_surface_create_buffer (surface);
|
|
|
|
dest_buffer = gimp_drawable_get_buffer (layer);
|
2011-04-21 05:58:00 +08:00
|
|
|
|
2012-05-03 10:13:39 +08:00
|
|
|
gegl_buffer_copy (src_buffer, NULL, dest_buffer, NULL);
|
2011-04-21 05:58:00 +08:00
|
|
|
|
2012-05-03 10:13:39 +08:00
|
|
|
g_object_unref (src_buffer);
|
|
|
|
g_object_unref (dest_buffer);
|
|
|
|
}
|
|
|
|
else
|
2011-04-21 05:58:00 +08:00
|
|
|
{
|
2012-05-03 10:13:39 +08:00
|
|
|
GimpDrawable *drawable;
|
|
|
|
GimpPixelRgn rgn;
|
|
|
|
const guchar *pixels;
|
|
|
|
gpointer pr;
|
|
|
|
gint rowstride;
|
|
|
|
guint count = 0;
|
|
|
|
guint done = 0;
|
|
|
|
|
|
|
|
drawable = gimp_drawable_get (layer);
|
2011-04-21 05:58:00 +08:00
|
|
|
|
2012-05-03 10:13:39 +08:00
|
|
|
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))
|
2011-04-21 05:58:00 +08:00
|
|
|
{
|
2012-05-03 10:13:39 +08:00
|
|
|
const guchar *src = pixels + rgn.y * rowstride + rgn.x * 4;
|
|
|
|
guchar *dest = rgn.data;
|
|
|
|
gint y;
|
2011-04-21 05:58:00 +08:00
|
|
|
|
2012-05-03 10:13:39 +08:00
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
case CAIRO_FORMAT_RGB24:
|
|
|
|
for (y = 0; y < rgn.h; y++)
|
2011-04-21 05:58:00 +08:00
|
|
|
{
|
2012-05-03 10:13:39 +08:00
|
|
|
const guchar *s = src;
|
|
|
|
guchar *d = dest;
|
|
|
|
gint w = rgn.w;
|
2011-04-21 05:58:00 +08:00
|
|
|
|
2012-05-03 10:13:39 +08:00
|
|
|
while (w--)
|
|
|
|
{
|
|
|
|
GIMP_CAIRO_RGB24_GET_PIXEL (s, d[0], d[1], d[2]);
|
2011-04-21 05:58:00 +08:00
|
|
|
|
2012-05-03 10:13:39 +08:00
|
|
|
s += 4;
|
|
|
|
d += 3;
|
|
|
|
}
|
2011-04-21 05:58:00 +08:00
|
|
|
|
2012-05-03 10:13:39 +08:00
|
|
|
src += rowstride;
|
|
|
|
dest += rgn.rowstride;
|
|
|
|
}
|
|
|
|
break;
|
2011-04-21 05:58:00 +08:00
|
|
|
|
2012-05-03 10:13:39 +08:00
|
|
|
case CAIRO_FORMAT_ARGB32:
|
|
|
|
for (y = 0; y < rgn.h; y++)
|
2011-04-21 05:58:00 +08:00
|
|
|
{
|
2012-05-03 10:13:39 +08:00
|
|
|
const guchar *s = src;
|
|
|
|
guchar *d = dest;
|
|
|
|
gint w = rgn.w;
|
2011-04-21 05:58:00 +08:00
|
|
|
|
2012-05-03 10:13:39 +08:00
|
|
|
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;
|
2011-04-21 05:58:00 +08:00
|
|
|
}
|
2012-05-03 10:13:39 +08:00
|
|
|
break;
|
2011-04-21 05:58:00 +08:00
|
|
|
|
2012-05-03 10:13:39 +08:00
|
|
|
default:
|
|
|
|
break;
|
2011-04-21 05:58:00 +08:00
|
|
|
}
|
|
|
|
|
2012-05-03 10:13:39 +08:00
|
|
|
if (range > 0.0)
|
|
|
|
{
|
|
|
|
done += rgn.h * rgn.w;
|
2011-04-21 05:58:00 +08:00
|
|
|
|
2012-05-03 10:13:39 +08:00
|
|
|
if (count++ % 32 == 0)
|
|
|
|
gimp_progress_update (progress_start +
|
|
|
|
(gdouble) done / (width * height) * range);
|
|
|
|
}
|
2011-04-21 02:04:35 +08:00
|
|
|
}
|
2012-05-03 10:13:39 +08:00
|
|
|
|
|
|
|
gimp_drawable_detach (drawable);
|
|
|
|
}
|
2011-04-21 02:04:35 +08:00
|
|
|
|
|
|
|
if (range > 0.0)
|
|
|
|
gimp_progress_update (progress_end);
|
|
|
|
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2005-07-11 05:17:22 +08:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2005-07-11 06:05:11 +08:00
|
|
|
return gimp_layer_set_lock_alpha (layer_ID, preserve_trans);
|
2005-07-11 05:17:22 +08:00
|
|
|
}
|