/* PIKA - Photo and Image Kooker Application * a rebranding of The GNU Image Manipulation Program (created with heckimp) * A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio * * Original copyright, applying to most contents (license remains unchanged): * Copyright (C) 1995 Spencer Kimball and Peter Mattis * * pikabrushclipboard.c * Copyright (C) 2006 Michael Natterer * * 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 . */ #include "config.h" #include #include #include "libpikabase/pikabase.h" #include "core-types.h" #include "pika.h" #include "pikabuffer.h" #include "pikabrush-private.h" #include "pikabrushclipboard.h" #include "pikaimage.h" #include "pikapickable.h" #include "pikatempbuf.h" #include "pika-intl.h" #define BRUSH_MAX_SIZE 1024 enum { PROP_0, PROP_PIKA, PROP_MASK_ONLY }; /* local function prototypes */ static void pika_brush_clipboard_constructed (GObject *object); static void pika_brush_clipboard_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec); static void pika_brush_clipboard_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec); static PikaData * pika_brush_clipboard_duplicate (PikaData *data); static void pika_brush_clipboard_changed (Pika *pika, PikaBrush *brush); G_DEFINE_TYPE (PikaBrushClipboard, pika_brush_clipboard, PIKA_TYPE_BRUSH) #define parent_class pika_brush_clipboard_parent_class static void pika_brush_clipboard_class_init (PikaBrushClipboardClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); PikaDataClass *data_class = PIKA_DATA_CLASS (klass); object_class->constructed = pika_brush_clipboard_constructed; object_class->set_property = pika_brush_clipboard_set_property; object_class->get_property = pika_brush_clipboard_get_property; data_class->duplicate = pika_brush_clipboard_duplicate; g_object_class_install_property (object_class, PROP_PIKA, g_param_spec_object ("pika", NULL, NULL, PIKA_TYPE_PIKA, PIKA_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); g_object_class_install_property (object_class, PROP_MASK_ONLY, g_param_spec_boolean ("mask-only", NULL, NULL, FALSE, PIKA_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); } static void pika_brush_clipboard_init (PikaBrushClipboard *brush) { } static void pika_brush_clipboard_constructed (GObject *object) { PikaBrushClipboard *brush = PIKA_BRUSH_CLIPBOARD (object); G_OBJECT_CLASS (parent_class)->constructed (object); pika_assert (PIKA_IS_PIKA (brush->pika)); g_signal_connect_object (brush->pika, "clipboard-changed", G_CALLBACK (pika_brush_clipboard_changed), brush, 0); pika_brush_clipboard_changed (brush->pika, PIKA_BRUSH (brush)); } static void pika_brush_clipboard_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { PikaBrushClipboard *brush = PIKA_BRUSH_CLIPBOARD (object); switch (property_id) { case PROP_PIKA: brush->pika = g_value_get_object (value); break; case PROP_MASK_ONLY: brush->mask_only = g_value_get_boolean (value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; } } static void pika_brush_clipboard_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { PikaBrushClipboard *brush = PIKA_BRUSH_CLIPBOARD (object); switch (property_id) { case PROP_PIKA: g_value_set_object (value, brush->pika); break; case PROP_MASK_ONLY: g_value_set_boolean (value, brush->mask_only); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; } } static PikaData * pika_brush_clipboard_duplicate (PikaData *data) { PikaData *new = g_object_new (PIKA_TYPE_BRUSH, NULL); pika_data_copy (new, data); return new; } PikaData * pika_brush_clipboard_new (Pika *pika, gboolean mask_only) { const gchar *name; g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL); if (mask_only) name = _("Clipboard Mask"); else name = _("Clipboard Image"); return g_object_new (PIKA_TYPE_BRUSH_CLIPBOARD, "name", name, "pika", pika, "mask-only", mask_only, NULL); } /* private functions */ static void pika_brush_clipboard_changed (Pika *pika, PikaBrush *brush) { PikaObject *paste; GeglBuffer *buffer = NULL; gint width; gint height; g_clear_pointer (&brush->priv->mask, pika_temp_buf_unref); g_clear_pointer (&brush->priv->pixmap, pika_temp_buf_unref); paste = pika_get_clipboard_object (pika); if (PIKA_IS_IMAGE (paste)) { pika_pickable_flush (PIKA_PICKABLE (paste)); buffer = pika_pickable_get_buffer (PIKA_PICKABLE (paste)); } else if (PIKA_IS_BUFFER (paste)) { buffer = pika_buffer_get_buffer (PIKA_BUFFER (paste)); } if (buffer) { const Babl *format = gegl_buffer_get_format (buffer); width = MIN (gegl_buffer_get_width (buffer), BRUSH_MAX_SIZE); height = MIN (gegl_buffer_get_height (buffer), BRUSH_MAX_SIZE); brush->priv->mask = pika_temp_buf_new (width, height, babl_format ("Y u8")); if (PIKA_BRUSH_CLIPBOARD (brush)->mask_only) { guchar *p; gint i; gegl_buffer_get (buffer, GEGL_RECTANGLE (0, 0, width, height), 1.0, babl_format ("Y u8"), pika_temp_buf_get_data (brush->priv->mask), GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE); /* invert the mask, it's more intuitive to think * "black on white" than the other way around */ for (i = 0, p = pika_temp_buf_get_data (brush->priv->mask); i < width * height; i++, p++) { *p = 255 - *p; } } else { brush->priv->pixmap = pika_temp_buf_new (width, height, babl_format ("R'G'B' u8")); /* copy the alpha channel into the brush's mask */ if (babl_format_has_alpha (format)) { gegl_buffer_get (buffer, GEGL_RECTANGLE (0, 0, width, height), 1.0, babl_format ("A u8"), pika_temp_buf_get_data (brush->priv->mask), GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE); } else { memset (pika_temp_buf_get_data (brush->priv->mask), 255, width * height); } /* copy the color channels into the brush's pixmap */ gegl_buffer_get (buffer, GEGL_RECTANGLE (0, 0, width, height), 1.0, babl_format ("R'G'B' u8"), pika_temp_buf_get_data (brush->priv->pixmap), GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE); } } else { width = 17; height = 17; brush->priv->mask = pika_temp_buf_new (width, height, babl_format ("Y u8")); pika_temp_buf_data_clear (brush->priv->mask); } brush->priv->x_axis.x = width / 2; brush->priv->x_axis.y = 0; brush->priv->y_axis.x = 0; brush->priv->y_axis.y = height / 2; pika_data_dirty (PIKA_DATA (brush)); }