gimp/app/core/gimplayer-new.c

252 lines
8.1 KiB
C
Raw Normal View History

/* 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 <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <cairo.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>
#include "libgimpcolor/gimpcolor.h"
#include "libgimpconfig/gimpconfig.h"
#include "core-types.h"
#include "gegl/gimp-babl.h"
#include "gegl/gimp-gegl-loops.h"
#include "gimpbuffer.h"
#include "gimpimage.h"
#include "gimpimage-color-profile.h"
#include "gimplayer.h"
#include "gimplayer-new.h"
/* local function prototypes */
static void gimp_layer_new_convert_buffer (GimpLayer *layer,
GeglBuffer *src_buffer,
GimpColorProfile *src_profile,
GError **error);
/* public functions */
GimpLayer *
gimp_layer_new (GimpImage *image,
gint width,
gint height,
const Babl *format,
const gchar *name,
gdouble opacity,
GimpLayerMode 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));
gimp_layer_set_opacity (layer, opacity, FALSE);
gimp_layer_set_mode (layer, mode, FALSE);
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
*
* Returns: The new layer.
**/
GimpLayer *
gimp_layer_new_from_buffer (GimpBuffer *buffer,
GimpImage *dest_image,
const Babl *format,
const gchar *name,
gdouble opacity,
GimpLayerMode mode)
{
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);
return gimp_layer_new_from_gegl_buffer (gimp_buffer_get_buffer (buffer),
dest_image, format,
name, opacity, mode,
gimp_buffer_get_color_profile (buffer));
}
/**
* 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
*
* Returns: The new layer.
**/
GimpLayer *
gimp_layer_new_from_gegl_buffer (GeglBuffer *buffer,
GimpImage *dest_image,
const Babl *format,
const gchar *name,
gdouble opacity,
GimpLayerMode mode,
GimpColorProfile *buffer_profile)
{
GimpLayer *layer;
const GeglRectangle *extent;
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);
g_return_val_if_fail (buffer_profile == NULL ||
GIMP_IS_COLOR_PROFILE (buffer_profile), NULL);
extent = gegl_buffer_get_extent (buffer);
/* 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,
extent->width, extent->height,
format,
name, opacity, mode);
if (extent->x != 0 || extent->y != 0)
gimp_item_translate (GIMP_ITEM (layer), extent->x, extent->y, FALSE);
gimp_layer_new_convert_buffer (layer, buffer, buffer_profile, NULL);
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
*
* Returns: The new layer.
**/
GimpLayer *
gimp_layer_new_from_pixbuf (GdkPixbuf *pixbuf,
GimpImage *dest_image,
const Babl *format,
const gchar *name,
gdouble opacity,
GimpLayerMode mode)
{
GimpLayer *layer;
GeglBuffer *buffer;
guint8 *icc_data;
gsize icc_len;
GimpColorProfile *profile = NULL;
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);
layer = gimp_layer_new (dest_image,
gdk_pixbuf_get_width (pixbuf),
gdk_pixbuf_get_height (pixbuf),
format, name, opacity, mode);
buffer = gimp_pixbuf_create_buffer (pixbuf);
icc_data = gimp_pixbuf_get_icc_profile (pixbuf, &icc_len);
if (icc_data)
{
profile = gimp_color_profile_new_from_icc_profile (icc_data, icc_len,
NULL);
g_free (icc_data);
}
gimp_layer_new_convert_buffer (layer, buffer, profile, NULL);
if (profile)
g_object_unref (profile);
g_object_unref (buffer);
return layer;
}
/* private functions */
static void
gimp_layer_new_convert_buffer (GimpLayer *layer,
GeglBuffer *src_buffer,
GimpColorProfile *src_profile,
GError **error)
{
GimpDrawable *drawable = GIMP_DRAWABLE (layer);
GeglBuffer *dest_buffer = gimp_drawable_get_buffer (drawable);
GimpColorProfile *dest_profile;
if (! src_profile)
{
const Babl *src_format = gegl_buffer_get_format (src_buffer);
src_profile = gimp_babl_format_get_color_profile (src_format);
}
Initial space invasion commit in GIMP All babl formats now have a space equivalent to a color profile, determining the format's primaries and TRCs. This commit makes GIMP aware of this. libgimp: - enum GimpPrecision: rename GAMMA values to NON_LINEAR and keep GAMMA as deprecated aliases, add PERCEPTUAL values so we now have LINEAR, NON_LINEAR and PERCPTUAL for each encoding, matching the babl encoding variants RGB, R'G'B' and R~G~B~. - gimp_color_transform_can_gegl_copy() now returns TRUE if both profiles can return a babl space, increasing the amount of fast babl color conversions significantly. - TODO: no solution yet for getting libgimp drawable proxy buffers in the right format with space. plug-ins: - follow the GimpPrecision change. - TODO: everything else unchanged and partly broken or sub-optimal, like setting a new image's color profile too late. app: - add enum GimpTRCType { LINEAR, NON_LINEAR, PERCEPTUAL } as replacement for all "linear" booleans. - change gimp-babl functions to take babl spaces and GimpTRCType parameters and support all sorts of new perceptual ~ formats. - a lot of places changed in the early days of goat invasion didn't take advantage of gimp-babl utility functions and constructed formats manually. They all needed revisiting and many now use much simpler code calling gimp-babl API. - change gimp_babl_format_get_color_profile() to really extract a newly allocated color profile from the format, and add gimp_babl_get_builtin_color_profile() which does the same as gimp_babl_format_get_color_profile() did before. Visited all callers to decide whether they are looking for the format's actual profile, or for one of the builtin profiles, simplifying code that only needs builtin profiles. - drawables have a new get_space_api(), get_linear() is now get_trc(). - images now have a "layer space" and an API to get it, gimp_image_get_layer_format() returns formats in that space. - an image's layer space is created from the image's color profile, change gimpimage-color-profile to deal with that correctly - change many babl_format() calls to babl_format_with_space() and take the space from passed formats or drawables - add function gimp_layer_fix_format_space() which replaces the layer's buffer with one that has the image's layer format, but doesn't change pixel values - use gimp_layer_fix_format_space() to make sure layers loaded from XCF and created by plug-ins have the right space when added to the image, because it's impossible to always assign the right space upon layer creation - "assign color profile" and "discard color profile" now require use of gimp_layer_fix_format_space() too because the profile is now embedded in all formats via the space. Add gimp_image_assign_color_profile() which does all that and call it instead of a simple gimp_image_set_color_profile(), also from the PDB set-color-profile functions, which are essentially "assign" and "discard" calls. - generally, make sure a new image's color profile is set before adding layers to it, gimp_image_set_color_profile() is more than before considered know-what-you-are-doing API. - take special precaution in all places that call gimp_drawable_convert_type(), we now must pass a new_profile from all callers that convert layers within the same image (such as image_convert_type, image_convert_precision), because the layer's new space can't be determined from the image's layer format during the call. - change all "linear" properties to "trc", in all config objects like for levels and curves, in the histogram, in the widgets. This results in some GUI that now has three choices instead of two. TODO: we might want to reduce that back to two later. - keep "linear" boolean properties around as compat if needed for file pasring, but always convert the parsed parsed boolean to GimpTRCType. - TODO: the image's "enable color management" switch is currently broken, will fix that in another commit.
2018-07-21 20:23:01 +08:00
else
{
g_object_ref (src_profile);
}
dest_profile =
gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (layer));
gimp_gegl_convert_color_profile (src_buffer, NULL, src_profile,
dest_buffer, NULL, dest_profile,
GIMP_COLOR_RENDERING_INTENT_PERCEPTUAL,
TRUE, NULL);
Initial space invasion commit in GIMP All babl formats now have a space equivalent to a color profile, determining the format's primaries and TRCs. This commit makes GIMP aware of this. libgimp: - enum GimpPrecision: rename GAMMA values to NON_LINEAR and keep GAMMA as deprecated aliases, add PERCEPTUAL values so we now have LINEAR, NON_LINEAR and PERCPTUAL for each encoding, matching the babl encoding variants RGB, R'G'B' and R~G~B~. - gimp_color_transform_can_gegl_copy() now returns TRUE if both profiles can return a babl space, increasing the amount of fast babl color conversions significantly. - TODO: no solution yet for getting libgimp drawable proxy buffers in the right format with space. plug-ins: - follow the GimpPrecision change. - TODO: everything else unchanged and partly broken or sub-optimal, like setting a new image's color profile too late. app: - add enum GimpTRCType { LINEAR, NON_LINEAR, PERCEPTUAL } as replacement for all "linear" booleans. - change gimp-babl functions to take babl spaces and GimpTRCType parameters and support all sorts of new perceptual ~ formats. - a lot of places changed in the early days of goat invasion didn't take advantage of gimp-babl utility functions and constructed formats manually. They all needed revisiting and many now use much simpler code calling gimp-babl API. - change gimp_babl_format_get_color_profile() to really extract a newly allocated color profile from the format, and add gimp_babl_get_builtin_color_profile() which does the same as gimp_babl_format_get_color_profile() did before. Visited all callers to decide whether they are looking for the format's actual profile, or for one of the builtin profiles, simplifying code that only needs builtin profiles. - drawables have a new get_space_api(), get_linear() is now get_trc(). - images now have a "layer space" and an API to get it, gimp_image_get_layer_format() returns formats in that space. - an image's layer space is created from the image's color profile, change gimpimage-color-profile to deal with that correctly - change many babl_format() calls to babl_format_with_space() and take the space from passed formats or drawables - add function gimp_layer_fix_format_space() which replaces the layer's buffer with one that has the image's layer format, but doesn't change pixel values - use gimp_layer_fix_format_space() to make sure layers loaded from XCF and created by plug-ins have the right space when added to the image, because it's impossible to always assign the right space upon layer creation - "assign color profile" and "discard color profile" now require use of gimp_layer_fix_format_space() too because the profile is now embedded in all formats via the space. Add gimp_image_assign_color_profile() which does all that and call it instead of a simple gimp_image_set_color_profile(), also from the PDB set-color-profile functions, which are essentially "assign" and "discard" calls. - generally, make sure a new image's color profile is set before adding layers to it, gimp_image_set_color_profile() is more than before considered know-what-you-are-doing API. - take special precaution in all places that call gimp_drawable_convert_type(), we now must pass a new_profile from all callers that convert layers within the same image (such as image_convert_type, image_convert_precision), because the layer's new space can't be determined from the image's layer format during the call. - change all "linear" properties to "trc", in all config objects like for levels and curves, in the histogram, in the widgets. This results in some GUI that now has three choices instead of two. TODO: we might want to reduce that back to two later. - keep "linear" boolean properties around as compat if needed for file pasring, but always convert the parsed parsed boolean to GimpTRCType. - TODO: the image's "enable color management" switch is currently broken, will fix that in another commit.
2018-07-21 20:23:01 +08:00
g_object_unref (src_profile);
}