2015-06-17 19:21:01 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <cairo.h>
|
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
|
|
|
#include <gegl.h>
|
|
|
|
|
|
|
|
#include "libgimpcolor/gimpcolor.h"
|
2015-08-14 06:08:51 +08:00
|
|
|
#include "libgimpconfig/gimpconfig.h"
|
2015-06-17 19:21:01 +08:00
|
|
|
|
|
|
|
#include "core-types.h"
|
|
|
|
|
2015-08-14 06:08:51 +08:00
|
|
|
#include "config/gimpcoreconfig.h"
|
|
|
|
|
2015-08-15 00:03:31 +08:00
|
|
|
#include "gegl/gimp-babl.h"
|
2015-08-15 04:43:49 +08:00
|
|
|
#include "gegl/gimp-gegl-loops.h"
|
2015-08-15 00:03:31 +08:00
|
|
|
|
2015-08-14 06:08:51 +08:00
|
|
|
#include "gimp.h"
|
2015-06-20 06:02:11 +08:00
|
|
|
#include "gimpbuffer.h"
|
2015-06-17 19:21:01 +08:00
|
|
|
#include "gimpimage.h"
|
|
|
|
#include "gimplayer.h"
|
|
|
|
#include "gimplayer-new.h"
|
|
|
|
|
|
|
|
|
2015-06-20 06:33:22 +08:00
|
|
|
/* local function prototypes */
|
|
|
|
|
2015-08-14 06:08:51 +08:00
|
|
|
static gboolean gimp_layer_new_convert_profile (GimpLayer *layer,
|
2015-08-15 00:03:31 +08:00
|
|
|
GeglBuffer *src_buffer,
|
2015-08-14 06:08:51 +08:00
|
|
|
const guint8 *icc_data,
|
|
|
|
gsize icc_length,
|
|
|
|
GError **error);
|
2015-06-20 06:33:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* public functions */
|
|
|
|
|
2015-06-17 19:21:01 +08:00
|
|
|
GimpLayer *
|
|
|
|
gimp_layer_new (GimpImage *image,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
|
|
|
const Babl *format,
|
|
|
|
const gchar *name,
|
|
|
|
gdouble opacity,
|
|
|
|
GimpLayerModeEffects mode)
|
|
|
|
{
|
|
|
|
GimpLayer *layer;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
|
|
|
|
g_return_val_if_fail (width > 0, NULL);
|
|
|
|
g_return_val_if_fail (height > 0, NULL);
|
|
|
|
g_return_val_if_fail (format != NULL, NULL);
|
|
|
|
|
|
|
|
layer = GIMP_LAYER (gimp_drawable_new (GIMP_TYPE_LAYER,
|
|
|
|
image, name,
|
|
|
|
0, 0, width, height,
|
|
|
|
format));
|
|
|
|
|
|
|
|
opacity = CLAMP (opacity, GIMP_OPACITY_TRANSPARENT, GIMP_OPACITY_OPAQUE);
|
|
|
|
|
|
|
|
layer->opacity = opacity;
|
|
|
|
layer->mode = mode;
|
|
|
|
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_layer_new_from_buffer:
|
|
|
|
* @buffer: The buffer to make the new layer from.
|
|
|
|
* @dest_image: The image the new layer will be added to.
|
|
|
|
* @format: The #Babl format of the new layer.
|
|
|
|
* @name: The new layer's name.
|
|
|
|
* @opacity: The new layer's opacity.
|
|
|
|
* @mode: The new layer's mode.
|
|
|
|
*
|
|
|
|
* Copies %buffer to a layer taking into consideration the
|
|
|
|
* possibility of transforming the contents to meet the requirements
|
|
|
|
* of the target image type
|
|
|
|
*
|
|
|
|
* Return value: The new layer.
|
|
|
|
**/
|
|
|
|
GimpLayer *
|
2015-06-20 06:02:11 +08:00
|
|
|
gimp_layer_new_from_buffer (GimpBuffer *buffer,
|
2015-06-17 19:21:01 +08:00
|
|
|
GimpImage *dest_image,
|
|
|
|
const Babl *format,
|
|
|
|
const gchar *name,
|
|
|
|
gdouble opacity,
|
|
|
|
GimpLayerModeEffects mode)
|
2015-06-20 06:02:11 +08:00
|
|
|
{
|
2015-06-20 06:33:22 +08:00
|
|
|
const guint8 *icc_data;
|
|
|
|
gsize icc_len;
|
|
|
|
|
2015-06-20 06:02:11 +08:00
|
|
|
g_return_val_if_fail (GIMP_IS_BUFFER (buffer), NULL);
|
|
|
|
g_return_val_if_fail (GIMP_IS_IMAGE (dest_image), NULL);
|
|
|
|
g_return_val_if_fail (format != NULL, NULL);
|
|
|
|
|
2015-06-20 06:33:22 +08:00
|
|
|
icc_data = gimp_buffer_get_icc_profile (buffer, &icc_len);
|
|
|
|
|
2015-06-20 06:02:11 +08:00
|
|
|
return gimp_layer_new_from_gegl_buffer (gimp_buffer_get_buffer (buffer),
|
|
|
|
dest_image, format,
|
2015-06-20 06:33:22 +08:00
|
|
|
name, opacity, mode,
|
|
|
|
icc_data, icc_len);
|
2015-06-20 06:02:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_layer_new_from_gegl_buffer:
|
|
|
|
* @buffer: The buffer to make the new layer from.
|
|
|
|
* @dest_image: The image the new layer will be added to.
|
|
|
|
* @format: The #Babl format of the new layer.
|
|
|
|
* @name: The new layer's name.
|
|
|
|
* @opacity: The new layer's opacity.
|
|
|
|
* @mode: The new layer's mode.
|
|
|
|
*
|
|
|
|
* Copies %buffer to a layer taking into consideration the
|
|
|
|
* possibility of transforming the contents to meet the requirements
|
|
|
|
* of the target image type
|
|
|
|
*
|
|
|
|
* Return value: The new layer.
|
|
|
|
**/
|
|
|
|
GimpLayer *
|
|
|
|
gimp_layer_new_from_gegl_buffer (GeglBuffer *buffer,
|
|
|
|
GimpImage *dest_image,
|
|
|
|
const Babl *format,
|
|
|
|
const gchar *name,
|
|
|
|
gdouble opacity,
|
2015-06-20 06:33:22 +08:00
|
|
|
GimpLayerModeEffects mode,
|
|
|
|
const guint8 *buffer_icc_data,
|
|
|
|
gsize buffer_icc_length)
|
2015-06-17 19:21:01 +08:00
|
|
|
{
|
2015-08-15 00:03:31 +08:00
|
|
|
GimpLayer *layer;
|
2015-06-17 19:21:01 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (GEGL_IS_BUFFER (buffer), NULL);
|
|
|
|
g_return_val_if_fail (GIMP_IS_IMAGE (dest_image), NULL);
|
|
|
|
g_return_val_if_fail (format != NULL, NULL);
|
2015-06-20 06:33:22 +08:00
|
|
|
g_return_val_if_fail (buffer_icc_data != NULL || buffer_icc_length == 0, NULL);
|
2015-06-17 19:21:01 +08:00
|
|
|
|
|
|
|
/* do *not* use the buffer's format because this function gets
|
|
|
|
* buffers of any format passed, and converts them
|
|
|
|
*/
|
|
|
|
layer = gimp_layer_new (dest_image,
|
|
|
|
gegl_buffer_get_width (buffer),
|
|
|
|
gegl_buffer_get_height (buffer),
|
|
|
|
format,
|
|
|
|
name, opacity, mode);
|
|
|
|
|
2015-06-20 06:33:22 +08:00
|
|
|
if (buffer_icc_data)
|
2015-08-15 00:03:31 +08:00
|
|
|
{
|
|
|
|
gimp_layer_new_convert_profile (layer, buffer,
|
|
|
|
buffer_icc_data, buffer_icc_length,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gegl_buffer_copy (buffer, NULL, GEGL_ABYSS_NONE,
|
|
|
|
gimp_drawable_get_buffer (GIMP_DRAWABLE (layer)), NULL);
|
|
|
|
}
|
2015-06-20 06:33:22 +08:00
|
|
|
|
2015-06-17 19:21:01 +08:00
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_layer_new_from_pixbuf:
|
|
|
|
* @pixbuf: The pixbuf to make the new layer from.
|
|
|
|
* @dest_image: The image the new layer will be added to.
|
|
|
|
* @format: The #Babl format of the new layer.
|
|
|
|
* @name: The new layer's name.
|
|
|
|
* @opacity: The new layer's opacity.
|
|
|
|
* @mode: The new layer's mode.
|
|
|
|
*
|
|
|
|
* Copies %pixbuf to a layer taking into consideration the
|
|
|
|
* possibility of transforming the contents to meet the requirements
|
|
|
|
* of the target image type
|
|
|
|
*
|
|
|
|
* Return value: The new layer.
|
|
|
|
**/
|
|
|
|
GimpLayer *
|
|
|
|
gimp_layer_new_from_pixbuf (GdkPixbuf *pixbuf,
|
|
|
|
GimpImage *dest_image,
|
|
|
|
const Babl *format,
|
|
|
|
const gchar *name,
|
|
|
|
gdouble opacity,
|
|
|
|
GimpLayerModeEffects mode)
|
|
|
|
{
|
|
|
|
GimpLayer *layer;
|
2015-08-15 00:03:31 +08:00
|
|
|
GeglBuffer *buffer;
|
2015-06-20 06:33:22 +08:00
|
|
|
guint8 *icc_data;
|
|
|
|
gsize icc_len;
|
2015-06-17 19:21:01 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
|
|
|
|
g_return_val_if_fail (GIMP_IS_IMAGE (dest_image), NULL);
|
|
|
|
g_return_val_if_fail (format != NULL, NULL);
|
|
|
|
|
2015-08-15 00:03:31 +08:00
|
|
|
layer = gimp_layer_new (dest_image,
|
|
|
|
gdk_pixbuf_get_width (pixbuf),
|
|
|
|
gdk_pixbuf_get_height (pixbuf),
|
2015-06-17 19:21:01 +08:00
|
|
|
format, name, opacity, mode);
|
|
|
|
|
2015-08-15 00:03:31 +08:00
|
|
|
buffer = gimp_pixbuf_create_buffer (pixbuf);
|
2015-06-17 19:21:01 +08:00
|
|
|
|
2015-06-20 06:33:22 +08:00
|
|
|
icc_data = gimp_pixbuf_get_icc_profile (pixbuf, &icc_len);
|
2015-08-15 00:03:31 +08:00
|
|
|
|
2015-06-20 06:33:22 +08:00
|
|
|
if (icc_data)
|
|
|
|
{
|
2015-08-15 00:03:31 +08:00
|
|
|
gimp_layer_new_convert_profile (layer, buffer, icc_data, icc_len, NULL);
|
2015-06-20 06:33:22 +08:00
|
|
|
g_free (icc_data);
|
|
|
|
}
|
2015-08-16 01:15:14 +08:00
|
|
|
else if (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB)
|
|
|
|
{
|
|
|
|
GimpColorProfile *profile = gimp_color_profile_new_srgb ();
|
|
|
|
const guint8 *icc_data;
|
|
|
|
|
|
|
|
icc_data = gimp_color_profile_get_icc_profile (profile, &icc_len);
|
|
|
|
gimp_layer_new_convert_profile (layer, buffer, icc_data, icc_len, NULL);
|
|
|
|
|
|
|
|
g_object_unref (profile);
|
|
|
|
}
|
2015-08-15 00:03:31 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
gegl_buffer_copy (buffer, NULL, GEGL_ABYSS_NONE,
|
|
|
|
gimp_drawable_get_buffer (GIMP_DRAWABLE (layer)), NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref (buffer);
|
2015-06-20 06:33:22 +08:00
|
|
|
|
2015-06-17 19:21:01 +08:00
|
|
|
return layer;
|
|
|
|
}
|
2015-06-20 06:33:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* private functions */
|
|
|
|
|
2015-08-14 06:08:51 +08:00
|
|
|
static gboolean
|
|
|
|
gimp_layer_new_convert_profile (GimpLayer *layer,
|
2015-08-15 00:03:31 +08:00
|
|
|
GeglBuffer *src_buffer,
|
2015-08-14 06:08:51 +08:00
|
|
|
const guint8 *icc_data,
|
|
|
|
gsize icc_length,
|
|
|
|
GError **error)
|
2015-06-20 06:33:22 +08:00
|
|
|
{
|
2015-08-15 00:03:31 +08:00
|
|
|
GimpDrawable *drawable = GIMP_DRAWABLE (layer);
|
|
|
|
GimpImage *image = gimp_item_get_image (GIMP_ITEM (layer));
|
|
|
|
GimpColorConfig *config = image->gimp->config->color_management;
|
|
|
|
GeglBuffer *dest_buffer = gimp_drawable_get_buffer (drawable);
|
2015-08-14 06:08:51 +08:00
|
|
|
GimpColorProfile *src_profile;
|
|
|
|
GimpColorProfile *dest_profile;
|
|
|
|
|
2015-08-15 00:03:31 +08:00
|
|
|
/* FIXME: this is the wrong check, need something like file import
|
|
|
|
* conversion config
|
|
|
|
*/
|
2015-08-14 06:08:51 +08:00
|
|
|
if (config->mode == GIMP_COLOR_MANAGEMENT_OFF)
|
2015-08-15 04:43:49 +08:00
|
|
|
{
|
|
|
|
gegl_buffer_copy (src_buffer, NULL, GEGL_ABYSS_NONE, dest_buffer, NULL);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2015-08-15 00:03:31 +08:00
|
|
|
|
2015-08-14 06:08:51 +08:00
|
|
|
src_profile = gimp_color_profile_new_from_icc_profile (icc_data, icc_length,
|
|
|
|
error);
|
|
|
|
if (! src_profile)
|
|
|
|
{
|
2015-08-15 04:43:49 +08:00
|
|
|
gegl_buffer_copy (src_buffer, NULL, GEGL_ABYSS_NONE, dest_buffer, NULL);
|
|
|
|
return FALSE;
|
2015-08-14 06:08:51 +08:00
|
|
|
}
|
|
|
|
|
2015-08-15 04:43:49 +08:00
|
|
|
dest_profile = gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (image));
|
2015-08-14 06:08:51 +08:00
|
|
|
|
2015-08-15 04:43:49 +08:00
|
|
|
gimp_gegl_convert_color_profile (src_buffer, NULL, src_profile,
|
|
|
|
dest_buffer, NULL, dest_profile,
|
|
|
|
GIMP_COLOR_RENDERING_INTENT_PERCEPTUAL,
|
|
|
|
FALSE);
|
2015-08-14 06:08:51 +08:00
|
|
|
|
|
|
|
g_object_unref (src_profile);
|
|
|
|
g_object_unref (dest_profile);
|
|
|
|
|
|
|
|
return TRUE;
|
2015-06-20 06:33:22 +08:00
|
|
|
}
|