1
1
mirror of https://gitlab.gnome.org/GNOME/gimp.git synced 2025-10-06 09:32:41 +02:00
Files
gimp/app/core/gimpimage-convert-precision.c

231 lines
7.4 KiB
C
Raw Permalink Normal View History

/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimpimage-convert-precision.c
* Copyright (C) 2012 Michael Natterer <mitch@gimp.org>
*
* 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>
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 14:23:01 +02:00
#include "libgimpbase/gimpbase.h"
#include "libgimpcolor/gimpcolor.h"
#include "core-types.h"
#include "gegl/gimp-babl.h"
#include "gegl/gimp-gegl-loops.h"
#include "gimpchannel.h"
#include "gimpdrawable.h"
#include "gimpdrawable-operation.h"
#include "gimpimage.h"
#include "gimpimage-color-profile.h"
#include "gimpimage-convert-precision.h"
#include "gimpimage-undo.h"
#include "gimpimage-undo-push.h"
#include "gimpobjectqueue.h"
#include "gimpprogress.h"
#include "text/gimptextlayer.h"
#include "gimp-intl.h"
void
gimp_image_convert_precision (GimpImage *image,
GimpPrecision precision,
GeglDitherMethod layer_dither_type,
GeglDitherMethod text_layer_dither_type,
GeglDitherMethod mask_dither_type,
GimpProgress *progress)
{
GimpColorProfile *profile;
GimpObjectQueue *queue;
GimpProgress *sub_progress;
GList *layers;
GimpDrawable *drawable;
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 14:23:01 +02:00
const gchar *enum_desc;
gchar *undo_desc = NULL;
g_return_if_fail (GIMP_IS_IMAGE (image));
g_return_if_fail (precision != gimp_image_get_precision (image));
g_return_if_fail (gimp_babl_is_valid (gimp_image_get_base_type (image),
precision));
g_return_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress));
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 14:23:01 +02:00
gimp_enum_get_value (GIMP_TYPE_PRECISION, precision,
NULL, NULL, &enum_desc, NULL);
undo_desc = g_strdup_printf (C_("undo-type", "Convert Image to %s"),
enum_desc);
if (progress)
gimp_progress_start (progress, FALSE, "%s", undo_desc);
queue = gimp_object_queue_new (progress);
sub_progress = GIMP_PROGRESS (queue);
layers = gimp_image_get_layer_list (image);
gimp_object_queue_push_list (queue, layers);
g_list_free (layers);
gimp_object_queue_push (queue, gimp_image_get_mask (image));
gimp_object_queue_push_container (queue, gimp_image_get_channels (image));
g_object_freeze_notify (G_OBJECT (image));
gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_CONVERT,
undo_desc);
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 14:23:01 +02:00
g_free (undo_desc);
/* Push the image precision to the stack */
gimp_image_undo_push_image_precision (image, NULL);
/* Use the actual image's profile, and in particular don't be clever
* by using a builtin profile. I.e. don't use gimp_color_managed_get_color_profile().
* We don't want a profile change, and the format is enough to take
* care of any TRC override if needed.
*/
profile = gimp_image_get_color_profile (image);
app: fix a crash when converting to higher precision. gimp_display_shell_render() writes to a GeglBuffer backed by allocated memory (shell->profile_data). Unfortunately while converting prevision in gimp_image_convert_precision(), we change the "precision" property (hence the source format) first, hence end up trying to write data in a too small buffer. This crash was hard to find as it was not showing up on my machine (though it did produce rendering artifacts!), unless I built both GIMP and babl with `b_sanitize=address`. Note that an alternate fix was to make sure that the profile_data buffer is big enough (by calling gimp_display_shell_profile_update() before rendering), but anyway the image is in an inconsistent state while conversion is in progress: whereas the `src_format` is the new one, the `src_profile` is still the old one (and cannot be changed before we finish converting). Moreover the render happen regularly on progress signals, once after each converted drawable. So each of these rendering step happens in an inconsistent state, with the wrong profile set, some of the drawables converted and others not yet. We could still render properly if each drawable's buffer used space-aware format (thus allowing different drawables to use different profiles/spaces), but it feels over-engineering the problem. It might be much better to ignore rendering steps while converting the image precision. Moreover it would obviously make a faster conversion. See discussions in #9136 for this crash, which didn't have dedicated report AFAIK. (cherry picked from commit de25be92104e6883cb35ea7bf0ea12408c8ca722) Note: on the `master` branch, even with sanitized code, I don't get the crash. Yet this change seems relevant enough that I'm adding it.
2023-02-19 13:54:03 +01:00
gimp_image_set_converting (image, TRUE);
/* Set the new precision */
g_object_set (image, "precision", precision, NULL);
while ((drawable = gimp_object_queue_pop (queue)))
{
if (drawable == GIMP_DRAWABLE (gimp_image_get_mask (image)))
{
GeglBuffer *buffer;
gimp_image_undo_push_mask_precision (image, NULL,
GIMP_CHANNEL (drawable));
buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0,
gimp_image_get_width (image),
gimp_image_get_height (image)),
gimp_image_get_mask_format (image));
gimp_gegl_buffer_copy (gimp_drawable_get_buffer (drawable), NULL,
GEGL_ABYSS_NONE,
buffer, NULL);
gimp_drawable_set_buffer (drawable, FALSE, NULL, buffer);
g_object_unref (buffer);
gimp_progress_set_value (sub_progress, 1.0);
}
else
{
GeglDitherMethod dither_type;
if (gimp_item_is_text_layer (GIMP_ITEM (drawable)))
dither_type = text_layer_dither_type;
else
dither_type = layer_dither_type;
gimp_drawable_convert_type (drawable, image,
gimp_drawable_get_base_type (drawable),
precision,
gimp_drawable_has_alpha (drawable),
profile, profile,
dither_type,
mask_dither_type,
TRUE, sub_progress);
}
}
gimp_color_managed_profile_changed (GIMP_COLOR_MANAGED (image));
app: fix a crash when converting to higher precision. gimp_display_shell_render() writes to a GeglBuffer backed by allocated memory (shell->profile_data). Unfortunately while converting prevision in gimp_image_convert_precision(), we change the "precision" property (hence the source format) first, hence end up trying to write data in a too small buffer. This crash was hard to find as it was not showing up on my machine (though it did produce rendering artifacts!), unless I built both GIMP and babl with `b_sanitize=address`. Note that an alternate fix was to make sure that the profile_data buffer is big enough (by calling gimp_display_shell_profile_update() before rendering), but anyway the image is in an inconsistent state while conversion is in progress: whereas the `src_format` is the new one, the `src_profile` is still the old one (and cannot be changed before we finish converting). Moreover the render happen regularly on progress signals, once after each converted drawable. So each of these rendering step happens in an inconsistent state, with the wrong profile set, some of the drawables converted and others not yet. We could still render properly if each drawable's buffer used space-aware format (thus allowing different drawables to use different profiles/spaces), but it feels over-engineering the problem. It might be much better to ignore rendering steps while converting the image precision. Moreover it would obviously make a faster conversion. See discussions in #9136 for this crash, which didn't have dedicated report AFAIK. (cherry picked from commit de25be92104e6883cb35ea7bf0ea12408c8ca722) Note: on the `master` branch, even with sanitized code, I don't get the crash. Yet this change seems relevant enough that I'm adding it.
2023-02-19 13:54:03 +01:00
gimp_image_set_converting (image, FALSE);
gimp_image_undo_group_end (image);
gimp_image_precision_changed (image);
g_object_thaw_notify (G_OBJECT (image));
g_object_unref (queue);
if (progress)
gimp_progress_end (progress);
}
void
gimp_image_convert_dither_u8 (GimpImage *image,
GimpProgress *progress)
{
GeglNode *dither;
g_return_if_fail (GIMP_IS_IMAGE (image));
g_return_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress));
dither = gegl_node_new_child (NULL,
"operation", "gegl:noise-rgb",
"red", 1.0 / 256.0,
"green", 1.0 / 256.0,
"blue", 1.0 / 256.0,
"linear", FALSE,
"gaussian", FALSE,
NULL);
if (dither)
{
GimpObjectQueue *queue;
GimpProgress *sub_progress;
GList *layers;
GList *list;
GimpDrawable *drawable;
if (progress)
gimp_progress_start (progress, FALSE, "%s", _("Dithering"));
queue = gimp_object_queue_new (progress);
sub_progress = GIMP_PROGRESS (queue);
layers = gimp_image_get_layer_list (image);
for (list = layers; list; list = g_list_next (list))
{
if (! gimp_viewable_get_children (list->data) &&
! gimp_item_is_text_layer (list->data))
{
gimp_object_queue_push (queue, list->data);
}
}
g_list_free (layers);
while ((drawable = gimp_object_queue_pop (queue)))
{
gimp_drawable_apply_operation (drawable, sub_progress,
_("Dithering"),
dither);
}
g_object_unref (queue);
if (progress)
gimp_progress_end (progress);
g_object_unref (dither);
}
}