libgimpwidgets: add gimp_preview_area_set_color_config()

If a config is set, apply its settings when drawing the preview's
pixels. This is preparation for fixing:

Bug 467930 - color selectors are not color managed
Bug 556608 - Monitor color profile is not applied to filter preview
This commit is contained in:
Michael Natterer 2016-05-24 21:43:20 +02:00
parent 3a458d1756
commit 9c99d61feb
3 changed files with 246 additions and 84 deletions

View File

@ -19,14 +19,19 @@
#include "config.h" #include "config.h"
#include <string.h> #include <string.h>
#include <lcms2.h>
#include <gegl.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include "libgimpbase/gimpbase.h" #include "libgimpbase/gimpbase.h"
#include "libgimpconfig/gimpconfig.h"
#include "libgimpcolor/gimpcolor.h"
#include "gimpwidgetstypes.h" #include "gimpwidgetstypes.h"
#include "gimppreviewarea.h" #include "gimppreviewarea.h"
#include "gimpwidgetsutils.h"
#include "libgimp/libgimp-intl.h" #include "libgimp/libgimp-intl.h"
@ -57,27 +62,48 @@ enum
(((area)->offset_x + (col)) & size)) ? dark : light) (((area)->offset_x + (col)) & size)) ? dark : light)
static void gimp_preview_area_finalize (GObject *object); typedef struct _GimpPreviewAreaPrivate GimpPreviewAreaPrivate;
static void gimp_preview_area_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void gimp_preview_area_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static void gimp_preview_area_size_allocate (GtkWidget *widget, struct _GimpPreviewAreaPrivate
GtkAllocation *allocation); {
static gboolean gimp_preview_area_expose (GtkWidget *widget, GimpColorConfig *config;
GdkEventExpose *event); GimpColorTransform transform;
};
static void gimp_preview_area_queue_draw (GimpPreviewArea *area, #define GET_PRIVATE(obj) \
gint x, G_TYPE_INSTANCE_GET_PRIVATE (obj, \
gint y, GIMP_TYPE_PREVIEW_AREA, \
gint width, GimpPreviewAreaPrivate)
gint height);
static gint gimp_preview_area_image_type_bytes (GimpImageType type);
static void gimp_preview_area_dispose (GObject *object);
static void gimp_preview_area_finalize (GObject *object);
static void gimp_preview_area_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void gimp_preview_area_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static void gimp_preview_area_size_allocate (GtkWidget *widget,
GtkAllocation *allocation);
static gboolean gimp_preview_area_expose (GtkWidget *widget,
GdkEventExpose *event);
static void gimp_preview_area_queue_draw (GimpPreviewArea *area,
gint x,
gint y,
gint width,
gint height);
static gint gimp_preview_area_image_type_bytes (GimpImageType type);
static void gimp_preview_area_config_notify (GimpColorConfig *config,
const GParamSpec *pspec,
GimpPreviewArea *area);
static void gimp_preview_area_create_transform (GimpPreviewArea *area);
G_DEFINE_TYPE (GimpPreviewArea, gimp_preview_area, GTK_TYPE_DRAWING_AREA) G_DEFINE_TYPE (GimpPreviewArea, gimp_preview_area, GTK_TYPE_DRAWING_AREA)
@ -91,6 +117,7 @@ gimp_preview_area_class_init (GimpPreviewAreaClass *klass)
GObjectClass *object_class = G_OBJECT_CLASS (klass); GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->dispose = gimp_preview_area_dispose;
object_class->finalize = gimp_preview_area_finalize; object_class->finalize = gimp_preview_area_finalize;
object_class->set_property = gimp_preview_area_set_property; object_class->set_property = gimp_preview_area_set_property;
object_class->get_property = gimp_preview_area_get_property; object_class->get_property = gimp_preview_area_get_property;
@ -111,6 +138,8 @@ gimp_preview_area_class_init (GimpPreviewAreaClass *klass)
GIMP_TYPE_CHECK_TYPE, GIMP_TYPE_CHECK_TYPE,
DEFAULT_CHECK_TYPE, DEFAULT_CHECK_TYPE,
GIMP_PARAM_READWRITE)); GIMP_PARAM_READWRITE));
g_type_class_add_private (object_class, sizeof (GimpPreviewAreaPrivate));
} }
static void static void
@ -129,6 +158,16 @@ gimp_preview_area_init (GimpPreviewArea *area)
area->max_height = -1; area->max_height = -1;
} }
static void
gimp_preview_area_dispose (GObject *object)
{
GimpPreviewArea *area = GIMP_PREVIEW_AREA (object);
gimp_preview_area_set_color_config (area, NULL);
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void static void
gimp_preview_area_finalize (GObject *object) gimp_preview_area_finalize (GObject *object)
{ {
@ -139,6 +178,7 @@ gimp_preview_area_finalize (GObject *object)
g_free (area->buf); g_free (area->buf);
area->buf = NULL; area->buf = NULL;
} }
if (area->colormap) if (area->colormap)
{ {
g_free (area->colormap); g_free (area->colormap);
@ -229,11 +269,12 @@ static gboolean
gimp_preview_area_expose (GtkWidget *widget, gimp_preview_area_expose (GtkWidget *widget,
GdkEventExpose *event) GdkEventExpose *event)
{ {
GimpPreviewArea *area = GIMP_PREVIEW_AREA (widget); GimpPreviewArea *area = GIMP_PREVIEW_AREA (widget);
GtkAllocation allocation; GimpPreviewAreaPrivate *priv = GET_PRIVATE (area);
GdkPixbuf *pixbuf; GtkAllocation allocation;
GdkRectangle rect; GdkPixbuf *pixbuf;
cairo_t *cr; GdkRectangle rect;
cairo_t *cr;
if (! area->buf) if (! area->buf)
return FALSE; return FALSE;
@ -245,14 +286,46 @@ gimp_preview_area_expose (GtkWidget *widget,
rect.width = area->width; rect.width = area->width;
rect.height = area->height; rect.height = area->height;
pixbuf = gdk_pixbuf_new_from_data (area->buf, if (! priv->transform)
GDK_COLORSPACE_RGB, gimp_preview_area_create_transform (area);
FALSE,
8, if (priv->transform)
rect.width, {
rect.height, gint rowstride = ((area->width * 3) + 3) & ~3;
area->rowstride, guchar *buf = g_new (guchar, rowstride * area->height);
NULL, NULL); guchar *src = area->buf;
guchar *dest = buf;
gint i;
for (i = 0; i < area->height; i++)
{
cmsDoTransform (priv->transform, src, dest, area->width);
src += area->rowstride;
dest += rowstride;
}
pixbuf = gdk_pixbuf_new_from_data (buf,
GDK_COLORSPACE_RGB,
FALSE,
8,
rect.width,
rect.height,
rowstride,
(GdkPixbufDestroyNotify) g_free, NULL);
}
else
{
pixbuf = gdk_pixbuf_new_from_data (area->buf,
GDK_COLORSPACE_RGB,
FALSE,
8,
rect.width,
rect.height,
area->rowstride,
NULL, NULL);
}
cr = gdk_cairo_create (gtk_widget_get_window (widget)); cr = gdk_cairo_create (gtk_widget_get_window (widget));
gdk_cairo_region (cr, event->region); gdk_cairo_region (cr, event->region);
@ -310,6 +383,45 @@ gimp_preview_area_image_type_bytes (GimpImageType type)
} }
} }
static void
gimp_preview_area_config_notify (GimpColorConfig *config,
const GParamSpec *pspec,
GimpPreviewArea *area)
{
GimpPreviewAreaPrivate *priv = GET_PRIVATE (area);
if (priv->transform)
{
cmsDeleteTransform (priv->transform);
priv->transform = NULL;
}
gtk_widget_queue_draw (GTK_WIDGET (area));
}
static void
gimp_preview_area_create_transform (GimpPreviewArea *area)
{
GimpPreviewAreaPrivate *priv = GET_PRIVATE (area);
if (priv->config)
{
static GimpColorProfile *profile = NULL;
const Babl *format = babl_format ("R'G'B' u8");
if (G_UNLIKELY (! profile))
profile = gimp_color_profile_new_rgb_srgb ();
priv->transform = gimp_widget_get_color_transform (GTK_WIDGET (area),
priv->config,
profile,
&format,
&format);
}
}
/** /**
* gimp_preview_area_new: * gimp_preview_area_new:
* *
@ -1664,6 +1776,52 @@ gimp_preview_area_set_colormap (GimpPreviewArea *area,
} }
} }
/**
* gimp_preview_area_set_color_config:
* @area: a #GimpPreviewArea widget.
* @config: a #GimpColorConfig object.
*
* Sets the color management configuration to use with this preview area.
*
* Since: 2.10
*/
void
gimp_preview_area_set_color_config (GimpPreviewArea *area,
GimpColorConfig *config)
{
GimpPreviewAreaPrivate *priv;
g_return_if_fail (GIMP_IS_PREVIEW_AREA (area));
g_return_if_fail (config == NULL || GIMP_IS_COLOR_CONFIG (config));
priv = GET_PRIVATE (area);
if (priv->config)
{
g_signal_handlers_disconnect_by_func (priv->config,
gimp_preview_area_config_notify,
area);
g_object_unref (priv->config);
if (priv->transform)
{
cmsDeleteTransform (priv->transform);
priv->transform = NULL;
}
}
priv->config = config;
if (priv->config)
{
g_object_ref (priv->config);
g_signal_connect (priv->config, "notify",
G_CALLBACK (gimp_preview_area_config_notify),
area);
}
}
/** /**
* gimp_preview_area_set_max_size: * gimp_preview_area_set_max_size:
* @area: a #GimpPreviewArea widget * @area: a #GimpPreviewArea widget

View File

@ -65,64 +65,67 @@ struct _GimpPreviewAreaClass
}; };
GType gimp_preview_area_get_type (void) G_GNUC_CONST; GType gimp_preview_area_get_type (void) G_GNUC_CONST;
GtkWidget * gimp_preview_area_new (void); GtkWidget * gimp_preview_area_new (void);
void gimp_preview_area_draw (GimpPreviewArea *area, void gimp_preview_area_draw (GimpPreviewArea *area,
gint x, gint x,
gint y, gint y,
gint width, gint width,
gint height, gint height,
GimpImageType type, GimpImageType type,
const guchar *buf, const guchar *buf,
gint rowstride); gint rowstride);
void gimp_preview_area_blend (GimpPreviewArea *area, void gimp_preview_area_blend (GimpPreviewArea *area,
gint x, gint x,
gint y, gint y,
gint width, gint width,
gint height, gint height,
GimpImageType type, GimpImageType type,
const guchar *buf1, const guchar *buf1,
gint rowstride1, gint rowstride1,
const guchar *buf2, const guchar *buf2,
gint rowstride2, gint rowstride2,
guchar opacity); guchar opacity);
void gimp_preview_area_mask (GimpPreviewArea *area, void gimp_preview_area_mask (GimpPreviewArea *area,
gint x, gint x,
gint y, gint y,
gint width, gint width,
gint height, gint height,
GimpImageType type, GimpImageType type,
const guchar *buf1, const guchar *buf1,
gint rowstride1, gint rowstride1,
const guchar *buf2, const guchar *buf2,
gint rowstride2, gint rowstride2,
const guchar *mask, const guchar *mask,
gint rowstride_mask); gint rowstride_mask);
void gimp_preview_area_fill (GimpPreviewArea *area, void gimp_preview_area_fill (GimpPreviewArea *area,
gint x, gint x,
gint y, gint y,
gint width, gint width,
gint height, gint height,
guchar red, guchar red,
guchar green, guchar green,
guchar blue); guchar blue);
void gimp_preview_area_set_offsets (GimpPreviewArea *area, void gimp_preview_area_set_offsets (GimpPreviewArea *area,
gint x, gint x,
gint y); gint y);
void gimp_preview_area_set_colormap (GimpPreviewArea *area, void gimp_preview_area_set_colormap (GimpPreviewArea *area,
const guchar *colormap, const guchar *colormap,
gint num_colors); gint num_colors);
void gimp_preview_area_set_max_size (GimpPreviewArea *area, void gimp_preview_area_set_color_config (GimpPreviewArea *area,
gint width, GimpColorConfig *config);
gint height);
void gimp_preview_area_menu_popup (GimpPreviewArea *area, void gimp_preview_area_set_max_size (GimpPreviewArea *area,
GdkEventButton *event); gint width,
gint height);
void gimp_preview_area_menu_popup (GimpPreviewArea *area,
GdkEventButton *event);
G_END_DECLS G_END_DECLS

View File

@ -276,6 +276,7 @@ EXPORTS
gimp_preview_area_mask gimp_preview_area_mask
gimp_preview_area_menu_popup gimp_preview_area_menu_popup
gimp_preview_area_new gimp_preview_area_new
gimp_preview_area_set_color_config
gimp_preview_area_set_colormap gimp_preview_area_set_colormap
gimp_preview_area_set_max_size gimp_preview_area_set_max_size
gimp_preview_area_set_offsets gimp_preview_area_set_offsets