buffer-source.c 5.28 KB
Newer Older
1 2 3 4 5
/* This file is an image processing operation for GEGL
 *
 * GEGL 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
6
 * version 3 of the License, or (at your option) any later version.
7 8 9 10 11 12 13
 *
 * GEGL 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
14
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
15 16 17
 *
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
 */
18 19 20 21 22

#include "config.h"
#include <glib/gi18n-lib.h>


23
#ifdef GEGL_PROPERTIES
24

25 26
property_object(buffer, _("Input buffer"), GEGL_TYPE_BUFFER)
	  description (_("The GeglBuffer to load into the pipeline"))
27

28 29
#else

30 31
#define GEGL_OP_SOURCE
#define GEGL_OP_C_FILE "buffer-source.c"
32

33
#include "gegl-op.h"
34

35 36 37 38 39 40
typedef struct
{
  gulong buffer_changed_handler;
} Priv;

static Priv *
41
get_priv (GeglProperties *o)
42
{
43
  Priv *priv = (Priv*)o->user_data;
44 45
  if (priv == NULL) {
    priv = g_new0 (Priv, 1);
46
    o->user_data = (void*) priv;
47 48 49 50 51 52

    priv->buffer_changed_handler = 0;
  }
  return priv;
}

53 54 55 56 57 58 59 60
static void buffer_changed (GeglBuffer          *buffer,
                            const GeglRectangle *rect,
                            gpointer             userdata)
{
  gegl_operation_invalidate (GEGL_OPERATION (userdata), rect, FALSE);
}


61 62 63 64
static void
gegl_buffer_source_prepare (GeglOperation *operation)
{
  const Babl *format = NULL;
65
  GeglProperties *o = GEGL_PROPERTIES (operation);
66 67 68 69 70 71 72

  if (o->buffer)
    format = gegl_buffer_get_format (GEGL_BUFFER (o->buffer));

  gegl_operation_set_format (operation, "output", format);
}

73 74
static GeglRectangle
get_bounding_box (GeglOperation *operation)
75
{
76
  GeglRectangle result = {0,0,0,0};
77
  GeglProperties   *o = GEGL_PROPERTIES (operation);
78

79
  if (!o->buffer)
80
    {
81
      return result;
82
    }
83
  result = *gegl_buffer_get_extent (GEGL_BUFFER (o->buffer));
84
  return result;
85 86
}

87 88 89 90 91 92 93
static void
my_set_property (GObject  *gobject,
                          guint         property_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  GeglOperation *operation = GEGL_OPERATION (gobject);
94
  GeglProperties *o = GEGL_PROPERTIES (operation);
95 96 97 98 99 100 101
  Priv *p = get_priv(o);
  GObject *buffer = NULL;

  switch (property_id)
  {
    case PROP_buffer:
      if (o->buffer) {
102 103
        // Invariant: valid buffer should always have valid signal handler
        g_assert(p->buffer_changed_handler > 0);
104
        g_signal_handler_disconnect (o->buffer, p->buffer_changed_handler);
105
        /* XXX: should decrement signal connected count */
106 107 108
      }
      buffer = G_OBJECT (g_value_get_object (value));
      if (buffer) {
109
        p->buffer_changed_handler = gegl_buffer_signal_connect (buffer, "changed", G_CALLBACK(buffer_changed), operation);
110 111 112 113 114 115 116 117 118 119 120
      }
      break;
    default:
      break;
  }

  /* The set_property provided by the chant system does the
   * storing and reffing/unreffing of the input properties */
  set_property(gobject, property_id, value, pspec);
}

121
static gboolean
122 123 124 125 126
process (GeglOperation        *operation,
         GeglOperationContext *context,
         const gchar          *output_pad,
         const GeglRectangle  *result,
         gint                  level)
127
{
128
  GeglProperties *o = GEGL_PROPERTIES (operation);
129

130
  if (o->buffer)
131
    {
132 133 134 135
      g_object_ref (o->buffer); /* Add an extra reference, since
				     * gegl_operation_set_data is
				     * stealing one.
				     */
136

137
      /* override core behaviour, by resetting the buffer in the operation_context */
138 139
      gegl_operation_context_take_object (context, "output",
                                          G_OBJECT (o->buffer));
140 141 142
      /* mark that this buffer should not be used for in-place
       * processing.
       */
143
      gegl_object_set_has_forked (G_OBJECT (o->buffer));
144
    }
145 146 147
  return TRUE;
}

148 149
static void
dispose (GObject *object)
150
{
151
  GeglProperties *o = GEGL_PROPERTIES (object);
152
  Priv *p = get_priv(o);
153

154
  if (o->buffer)
155
    {
156 157
      // Invariant: valid buffer should always have valid signal handler
      g_assert(p->buffer_changed_handler > 0);
158
      g_signal_handler_disconnect (o->buffer, p->buffer_changed_handler);
159
        /* XXX: should decrement signal connected count */
160 161
      g_object_unref (o->buffer);
      o->buffer = NULL;
162
    }
163

164 165
  if (p) {
    g_free(p);
166
    o->user_data = NULL;
167 168
  }

169
  G_OBJECT_CLASS (gegl_op_parent_class)->dispose (object);
170 171
}

172 173

static void
174
gegl_op_class_init (GeglOpClass *klass)
175
{
176 177 178 179
  GeglOperationClass       *operation_class;

  operation_class = GEGL_OPERATION_CLASS (klass);

180
  operation_class->prepare = gegl_buffer_source_prepare;
181
  operation_class->process = process;
182 183
  operation_class->get_bounding_box = get_bounding_box;

184
  G_OBJECT_CLASS (klass)->set_property = my_set_property;
185 186
  G_OBJECT_CLASS (klass)->dispose = dispose;

187 188
  gegl_operation_class_set_keys (operation_class,
      "name",       "gegl:buffer-source",
189
      "title",      _("Buffer Source"),
190
      "categories", "programming:input",
191
      "description", _("Use an existing in-memory GeglBuffer as image source."),
192
      NULL);
193

194
  operation_class->no_cache = TRUE;
195 196
}

197
#endif
198