gimpdrawable-shadow.c 3.41 KB
Newer Older
1 2 3
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19
 */

#include "config.h"

20
#include <gdk-pixbuf/gdk-pixbuf.h>
21
#include <gegl.h>
22
#include <cairo.h>
23 24 25

#include "core-types.h"

26
#include "gegl/gimp-gegl-utils.h"
27 28

#include "gimpdrawable.h"
29
#include "gimpdrawable-private.h"
30 31 32
#include "gimpdrawable-shadow.h"


33 34
GeglBuffer *
gimp_drawable_get_shadow_buffer (GimpDrawable *drawable)
35
{
36 37 38 39
  GimpItem   *item;
  gint        width;
  gint        height;
  const Babl *format;
40 41 42 43 44

  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);

  item = GIMP_ITEM (drawable);

45 46 47 48
  width  = gimp_item_get_width  (item);
  height = gimp_item_get_height (item);
  format = gimp_drawable_get_format (drawable);

49
  if (drawable->private->shadow)
50
    {
51 52 53
      if ((width  != gegl_buffer_get_width  (drawable->private->shadow)) ||
          (height != gegl_buffer_get_height (drawable->private->shadow)) ||
          (format != gegl_buffer_get_format (drawable->private->shadow)))
54
        {
55
          gimp_drawable_free_shadow_buffer (drawable);
56 57 58
        }
      else
        {
59
          return drawable->private->shadow;
60 61 62
        }
    }

63 64 65
  drawable->private->shadow = gegl_buffer_new (GEGL_RECTANGLE (0, 0,
                                                               width, height),
                                               format);
66

67
  return drawable->private->shadow;
68 69 70
}

void
71
gimp_drawable_free_shadow_buffer (GimpDrawable *drawable)
72 73 74
{
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));

75
  g_clear_object (&drawable->private->shadow);
76 77 78
}

void
79 80 81
gimp_drawable_merge_shadow_buffer (GimpDrawable *drawable,
                                   gboolean      push_undo,
                                   const gchar  *undo_desc)
82 83 84 85 86 87
{
  gint x, y;
  gint width, height;

  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)));
88
  g_return_if_fail (GEGL_IS_BUFFER (drawable->private->shadow));
89 90 91 92 93

  /*  A useful optimization here is to limit the update to the
   *  extents of the selection mask, as it cannot extend beyond
   *  them.
   */
94
  if (gimp_item_mask_intersect (GIMP_ITEM (drawable), &x, &y, &width, &height))
95
    {
96
      GeglBuffer *buffer = g_object_ref (drawable->private->shadow);
Sven Neumann's avatar
Sven Neumann committed
97

98
      gimp_drawable_apply_buffer (drawable, buffer,
99
                                  GEGL_RECTANGLE (x, y, width, height),
100
                                  push_undo, undo_desc,
101 102
                                  GIMP_OPACITY_OPAQUE,
                                  GIMP_LAYER_MODE_REPLACE,
103 104
                                  GIMP_LAYER_COLOR_SPACE_AUTO,
                                  GIMP_LAYER_COLOR_SPACE_AUTO,
105
                                  GIMP_LAYER_COMPOSITE_AUTO,
106
                                  NULL, x, y);
107

108
      g_object_unref (buffer);
109 110
    }
}