1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
|
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimpdrawable.c
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gimp.h"
#include <gobject/gvaluecollector.h>
#include "gimppixbuf.h"
#include "gimptilebackendplugin.h"
G_DEFINE_ABSTRACT_TYPE (GimpDrawable, gimp_drawable, GIMP_TYPE_ITEM)
#define parent_class gimp_drawable_parent_class
static void
gimp_drawable_class_init (GimpDrawableClass *klass)
{
}
static void
gimp_drawable_init (GimpDrawable *drawable)
{
}
/* Public API. */
/**
* gimp_drawable_get_by_id:
* @drawable_id: The drawable id.
*
* Returns a #GimpDrawable representing @drawable_id. This function
* calls gimp_item_get_by_id() and returns the item if it is drawable
* or %NULL otherwise.
*
* Returns: (nullable) (transfer none): a #GimpDrawable for
* @drawable_id or %NULL if @drawable_id does not represent a
* valid drawable. The object belongs to libgimp and you must
* not modify or unref it.
*
* Since: 3.0
**/
GimpDrawable *
gimp_drawable_get_by_id (gint32 drawable_id)
{
GimpItem *item = gimp_item_get_by_id (drawable_id);
if (GIMP_IS_DRAWABLE (item))
return (GimpDrawable *) item;
return NULL;
}
/**
* gimp_drawable_get_thumbnail_data:
* @drawable: the drawable
* @width: the requested thumbnail width (<= 1024 pixels)
* @height: the requested thumbnail height (<= 1024 pixels)
* @actual_width: (out): the resulting thumbnail's actual width
* @actual_height: (out): the resulting thumbnail's actual height
* @bpp: (out): the bytes per pixel of the returned thubmnail data
*
* Retrieves thumbnail data for the drawable identified by @drawable.
* The thumbnail will be not larger than the requested size.
*
* Returns: (transfer full) (nullable): thumbnail data or %NULL if
* @drawable is invalid.
**/
GBytes *
gimp_drawable_get_thumbnail_data (GimpDrawable *drawable,
gint width,
gint height,
gint *actual_width,
gint *actual_height,
gint *bpp)
{
GBytes *image_data;
g_return_val_if_fail (actual_width != NULL, NULL);
g_return_val_if_fail (actual_height != NULL, NULL);
g_return_val_if_fail (bpp != NULL, NULL);
_gimp_drawable_thumbnail (drawable,
width,
height,
actual_width,
actual_height,
bpp,
&image_data);
return image_data;
}
/**
* gimp_drawable_get_thumbnail:
* @drawable: the drawable
* @width: the requested thumbnail width (<= 1024 pixels)
* @height: the requested thumbnail height (<= 1024 pixels)
* @alpha: how to handle an alpha channel
*
* Retrieves a thumbnail pixbuf for the drawable identified by
* @drawable. The thumbnail will be not larger than the requested
* size.
*
* Returns: (transfer full): a new #GdkPixbuf
*
* Since: 2.2
**/
GdkPixbuf *
gimp_drawable_get_thumbnail (GimpDrawable *drawable,
gint width,
gint height,
GimpPixbufTransparency alpha)
{
gint thumb_width, thumb_height, thumb_bpp;
GBytes *data;
gsize data_size;
GdkPixbuf *pixbuf = NULL;
g_return_val_if_fail (width > 0 && width <= 1024, NULL);
g_return_val_if_fail (height > 0 && height <= 1024, NULL);
data = gimp_drawable_get_thumbnail_data (drawable,
width,
height,
&thumb_width,
&thumb_height,
&thumb_bpp);
if (data)
pixbuf = _gimp_pixbuf_from_data (g_bytes_unref_to_data (data, &data_size),
thumb_width, thumb_height, thumb_bpp,
alpha);
return pixbuf;
}
/**
* gimp_drawable_get_sub_thumbnail_data:
* @drawable: the drawable ID
* @src_x: the x coordinate of the area
* @src_y: the y coordinate of the area
* @src_width: the width of the area
* @src_height: the height of the area
* @dest_width: the requested thumbnail width (<= 1024 pixels)
* @dest_height: the requested thumbnail height (<= 1024 pixels)
* @actual_width: (out): the width of the returned thumbnail
* @actual_height: (out): the height of the returned thumbnail
* @bpp: (out): the bytes per pixel of the returned thumbnail data
*
* Retrieves thumbnail data for the drawable identified by @drawable.
* The thumbnail will be not larger than the requested size.
*
* Returns: (transfer full): thumbnail data or %NULL if
* @drawable is invalid.
**/
GBytes *
gimp_drawable_get_sub_thumbnail_data (GimpDrawable *drawable,
gint src_x,
gint src_y,
gint src_width,
gint src_height,
gint dest_width,
gint dest_height,
gint *actual_width,
gint *actual_height,
gint *bpp)
{
GBytes *image_bytes;
_gimp_drawable_sub_thumbnail (drawable,
src_x, src_y,
src_width, src_height,
dest_width,
dest_height,
actual_width,
actual_height,
bpp,
&image_bytes);
return image_bytes;
}
/**
* gimp_drawable_get_sub_thumbnail:
* @drawable: the drawable ID
* @src_x: the x coordinate of the area
* @src_y: the y coordinate of the area
* @src_width: the width of the area
* @src_height: the height of the area
* @dest_width: the requested thumbnail width (<= 1024 pixels)
* @dest_height: the requested thumbnail height (<= 1024 pixels)
* @alpha: how to handle an alpha channel
*
* Retrieves a thumbnail pixbuf for the drawable identified by
* @drawable. The thumbnail will be not larger than the requested
* size.
*
* Returns: (transfer full): a new #GdkPixbuf
*
* Since: 2.2
**/
GdkPixbuf *
gimp_drawable_get_sub_thumbnail (GimpDrawable *drawable,
gint src_x,
gint src_y,
gint src_width,
gint src_height,
gint dest_width,
gint dest_height,
GimpPixbufTransparency alpha)
{
gint thumb_width = dest_width;
gint thumb_height = dest_height;
gint thumb_bpp;
GBytes *data;
gsize data_size;
GdkPixbuf *pixbuf = NULL;
g_return_val_if_fail (src_x >= 0, NULL);
g_return_val_if_fail (src_y >= 0, NULL);
g_return_val_if_fail (src_width > 0, NULL);
g_return_val_if_fail (src_height > 0, NULL);
g_return_val_if_fail (dest_width > 0 && dest_width <= 1024, NULL);
g_return_val_if_fail (dest_height > 0 && dest_height <= 1024, NULL);
data = gimp_drawable_get_sub_thumbnail_data (drawable,
src_x, src_y,
src_width, src_height,
dest_width, dest_height,
&thumb_width,
&thumb_height,
&thumb_bpp);
if (data)
pixbuf = _gimp_pixbuf_from_data (g_bytes_unref_to_data (data, &data_size),
thumb_width, thumb_height, thumb_bpp,
alpha);
g_bytes_unref (data);
return pixbuf;
}
/**
* gimp_drawable_get_buffer:
* @drawable: the ID of the #GimpDrawable to get the buffer for.
*
* Returns a #GeglBuffer of a specified drawable. The buffer can be used
* like any other GEGL buffer. Its data will we synced back with the core
* drawable when the buffer gets destroyed, or when gegl_buffer_flush()
* is called.
*
* Returns: (transfer full): The #GeglBuffer.
*
* See Also: gimp_drawable_get_shadow_buffer()
*
* Since: 2.10
*/
GeglBuffer *
gimp_drawable_get_buffer (GimpDrawable *drawable)
{
if (gimp_item_is_valid (GIMP_ITEM (drawable)))
{
GeglTileBackend *backend;
GeglBuffer *buffer;
backend = _gimp_tile_backend_plugin_new (drawable, FALSE);
buffer = gegl_buffer_new_for_backend (NULL, backend);
g_object_unref (backend);
return buffer;
}
return NULL;
}
/**
* gimp_drawable_get_shadow_buffer:
* @drawable: the ID of the #GimpDrawable to get the buffer for.
*
* Returns a #GeglBuffer of a specified drawable's shadow tiles. The
* buffer can be used like any other GEGL buffer. Its data will we
* synced back with the core drawable's shadow tiles when the buffer
* gets destroyed, or when gegl_buffer_flush() is called.
*
* Returns: (transfer full): The #GeglBuffer.
*
* Since: 2.10
*/
GeglBuffer *
gimp_drawable_get_shadow_buffer (GimpDrawable *drawable)
{
if (gimp_item_is_valid (GIMP_ITEM (drawable)))
{
GeglTileBackend *backend;
GeglBuffer *buffer;
backend = _gimp_tile_backend_plugin_new (drawable, TRUE);
buffer = gegl_buffer_new_for_backend (NULL, backend);
g_object_unref (backend);
return buffer;
}
return NULL;
}
/**
* gimp_drawable_get_format:
* @drawable: the ID of the #GimpDrawable to get the format for.
*
* Returns the #Babl format of the drawable.
*
* Returns: The #Babl format.
*
* Since: 2.10
*/
const Babl *
gimp_drawable_get_format (GimpDrawable *drawable)
{
const Babl *format = NULL;
gchar *format_str = _gimp_drawable_get_format (drawable);
/* _gimp_drawable_get_format() only returns the encoding, so we
* create the actual space from the image's profile
*/
if (format_str)
{
const Babl *space = NULL;
GimpImage *image = gimp_item_get_image (GIMP_ITEM (drawable));
if (gimp_item_is_layer (GIMP_ITEM (drawable)))
{
GimpColorProfile *profile = gimp_image_get_color_profile (image);
if (profile)
{
GError *error = NULL;
space = gimp_color_profile_get_space
(profile,
GIMP_COLOR_RENDERING_INTENT_RELATIVE_COLORIMETRIC,
&error);
if (! space)
{
g_printerr ("%s: failed to create Babl space from "
"profile: %s\n",
G_STRFUNC, error->message);
g_clear_error (&error);
}
g_object_unref (profile);
}
}
if (gimp_drawable_is_indexed (drawable))
{
const Babl *palette;
const Babl *palette_alpha;
const Babl *color_format;
guchar *colormap;
gint n_colors;
babl_new_palette_with_space (format_str, space,
&palette, &palette_alpha);
if (gimp_drawable_has_alpha (drawable))
format = palette_alpha;
else
format = palette;
color_format = babl_format_with_space ("R'G'B' u8", space);
colormap = gimp_palette_get_colormap (gimp_image_get_palette (image), color_format, &n_colors, NULL);
if (colormap)
{
babl_palette_set_palette (format, color_format, colormap, n_colors);
g_free (colormap);
}
}
else
{
format = babl_format_with_space (format_str, space);
}
g_free (format_str);
}
return format;
}
/**
* gimp_drawable_get_thumbnail_format:
* @drawable: the ID of the #GimpDrawable to get the thumbnail format for.
*
* Returns the #Babl thumbnail format of the drawable.
*
* Returns: The #Babl thumbnail format.
*
* Since: 2.10.14
*/
const Babl *
gimp_drawable_get_thumbnail_format (GimpDrawable *drawable)
{
const Babl *format = NULL;
gchar *format_str = _gimp_drawable_get_thumbnail_format (drawable);
if (format_str)
{
format = babl_format (format_str);
g_free (format_str);
}
return format;
}
/**
* gimp_drawable_append_filter:
* @drawable: The drawable.
* @filter: The drawable filter to append.
*
* This procedure appends the specified drawable effect at the top of the
* effect list of @drawable.
*
* The @drawable argument must be the same as the one used when you
* created the effect with [ctor@Gimp.DrawableFilter.new].
* Some effects may be slower than others to render. In order to
* minimize processing time, it is preferred to customize the
* operation's arguments as received with
* [method@Gimp.DrawableFilter.get_config] before adding the effect.
*
* Since: 3.0
**/
void
gimp_drawable_append_filter (GimpDrawable *drawable,
GimpDrawableFilter *filter)
{
g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
g_return_if_fail (GIMP_IS_DRAWABLE_FILTER (filter));
gimp_drawable_filter_update (filter);
_gimp_drawable_append_filter_private (drawable, filter);
}
/**
* gimp_drawable_merge_filter:
* @drawable: The drawable.
* @filter: The drawable filter to merge.
*
* This procedure applies the specified drawable effect on @drawable
* and merge it (therefore before any non-destructive effects are
* computed).
*
* The @drawable argument must be the same as the one used when you
* created the effect with [ctor@Gimp.DrawableFilter.new].
* Once this is run, @filter is not valid anymore and you should not
* try to do anything with it. In particular, you must customize the
* operation's arguments as received with
* [method@Gimp.DrawableFilter.get_config] or set the filter's opacity
* and blend mode before merging the effect.
*
* Since: 3.0
**/
void
gimp_drawable_merge_filter (GimpDrawable *drawable,
GimpDrawableFilter *filter)
{
g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
g_return_if_fail (GIMP_IS_DRAWABLE_FILTER (filter));
gimp_drawable_filter_update (filter);
_gimp_drawable_merge_filter_private (drawable, filter);
}
/**
* gimp_drawable_append_new_filter: (skip)
* @drawable: The #GimpDrawable.
* @operation_name: The GEGL operation's name.
* @name: The effect name.
* @mode: The blend mode.
* @opacity: The opacity from 0.0 (transparent) to 1.0 (opaque).
* @...: a %NULL-terminated list of operation argument names
* and values.
*
* Utility function which combines [ctor@Gimp.DrawableFilter.new]
* followed by setting arguments for the
* [class@Gimp.DrawableFilterConfig] returned by
* [method@Gimp.DrawableFilter.get_config], and finally appending with
* [method@Gimp.Drawable.append_filter]
*
* The variable arguments are couples of an argument name followed by a
* value, NULL-terminated, such as:
*
* ```C
* filter = gimp_drawable_append_new_filter (drawable,
* GIMP_LAYER_MODE_REPLACE, 1.0,
* "gegl:gaussian-blur", "My Gaussian Blur",
* "std-dev-x", 2.5,
* "std-dev-y", 2.5,
* "abyss-policy", "clamp",
* NULL);
* ```
*
* Returns: (transfer none): The newly created filter.
*/
GimpDrawableFilter *
gimp_drawable_append_new_filter (GimpDrawable *drawable,
const gchar *operation_name,
const gchar *name,
GimpLayerMode mode,
gdouble opacity,
...)
{
GimpDrawableFilter *filter;
GimpDrawableFilterConfig *config;
const gchar *arg_name;
va_list va_args;
g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
g_return_val_if_fail (opacity >= 0.0 && opacity <= 1.0, NULL);
filter = gimp_drawable_filter_new (drawable, operation_name, name);
g_return_val_if_fail (filter != NULL, NULL);
config = gimp_drawable_filter_get_config (filter);
va_start (va_args, opacity);
while ((arg_name = va_arg (va_args, const gchar *)))
{
GParamSpec *pspec;
gchar *error = NULL;
GValue value = G_VALUE_INIT;
pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (config), arg_name);
if (pspec == NULL)
{
g_warning ("%s: %s has no property named '%s'",
G_STRFUNC,
g_type_name (G_TYPE_FROM_INSTANCE (config)),
arg_name);
break;
}
g_value_init (&value, pspec->value_type);
G_VALUE_COLLECT (&value, va_args, G_VALUE_NOCOPY_CONTENTS, &error);
if (error)
{
g_warning ("%s: %s", G_STRFUNC, error);
g_free (error);
break;
}
g_object_set_property (G_OBJECT (config), arg_name, &value);
g_value_unset (&value);
}
va_end (va_args);
gimp_drawable_filter_set_blend_mode (filter, mode);
gimp_drawable_filter_set_opacity (filter, opacity);
gimp_drawable_append_filter (drawable, filter);
return filter;
}
/**
* gimp_drawable_merge_new_filter: (skip)
* @drawable: The #GimpDrawable.
* @operation_name: The GEGL operation's name.
* @name: The effect name which will show in undo step.
* @mode: The blend mode.
* @opacity: The opacity from 0.0 (transparent) to 1.0 (opaque).
* @...: a %NULL-terminated list of operation argument names
* and values.
*
* Utility function which combines [ctor@Gimp.DrawableFilter.new]
* followed by setting arguments for the
* [class@Gimp.DrawableFilterConfig] returned by
* [method@Gimp.DrawableFilter.get_config], and finally applying the
* effect to @drawable with [method@Gimp.Drawable.merge_filter]
*
* The variable arguments are couples of an argument name followed by a
* value, NULL-terminated, such as:
*
* ```C
* filter = gimp_drawable_merge_new_filter (drawable,
* GIMP_LAYER_MODE_REPLACE, 1.0,
* "gegl:gaussian-blur", "My Gaussian Blur",
* "std-dev-x", 2.5,
* "std-dev-y", 2.5,
* "abyss-policy", "clamp",
* NULL);
* ```
*/
void
gimp_drawable_merge_new_filter (GimpDrawable *drawable,
const gchar *operation_name,
const gchar *name,
GimpLayerMode mode,
gdouble opacity,
...)
{
GimpDrawableFilter *filter;
GimpDrawableFilterConfig *config;
const gchar *arg_name;
va_list va_args;
g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
g_return_if_fail (opacity >= 0.0 && opacity <= 1.0);
filter = gimp_drawable_filter_new (drawable, operation_name, name);
g_return_if_fail (filter != NULL);
config = gimp_drawable_filter_get_config (filter);
va_start (va_args, opacity);
while ((arg_name = va_arg (va_args, const gchar *)))
{
GParamSpec *pspec;
gchar *error = NULL;
GValue value = G_VALUE_INIT;
pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (config), arg_name);
if (pspec == NULL)
{
g_warning ("%s: %s has no property named '%s'",
G_STRFUNC,
g_type_name (G_TYPE_FROM_INSTANCE (config)),
arg_name);
break;
}
g_value_init (&value, pspec->value_type);
G_VALUE_COLLECT (&value, va_args, G_VALUE_NOCOPY_CONTENTS, &error);
if (error)
{
g_warning ("%s: %s", G_STRFUNC, error);
g_free (error);
break;
}
g_object_set_property (G_OBJECT (config), arg_name, &value);
g_value_unset (&value);
}
va_end (va_args);
gimp_drawable_filter_set_blend_mode (filter, mode);
gimp_drawable_filter_set_opacity (filter, opacity);
gimp_drawable_merge_filter (drawable, filter);
}
|