layer.c 7.52 KB
Newer Older
Øyvind Kolås's avatar
Øyvind Kolås committed
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.
Øyvind Kolås's avatar
Øyvind Kolås committed
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/>.
Øyvind Kolås's avatar
Øyvind Kolås committed
15 16 17
 *
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
 */
18 19 20 21 22

/* FIXME: need to make this OpenRaster inspired layer integrate better
 * with the newer caching system
 */

23 24 25 26 27

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


28 29
#ifdef GEGL_PROPERTIES

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
property_string(composite_op, _("Operation"), "gegl:over")
    description (_("Composite operation to use"))

property_double(opacity, _("Opacity"), 1.0)
    value_range (0.0, 1.0)

property_double(x, _("X"), 0.0)
    description (_("Horizontal position in pixels"))
    ui_meta     ("axis", "x")
    ui_meta     ("unit", "pixel-coordinate")

property_double(y, _("Y"), 0.0)
    description (_("Vertical position in pixels"))
    ui_meta     ("axis", "y")
    ui_meta     ("unit", "pixel-coordinate")

property_double(scale, _("Scale"), 1.0)
    description (_("Scale 1:1 size"))

property_file_path(src, _("Source"), "")
    description (_("Source image file path (png, jpg, raw, svg, bmp, tif, ...)"))
Øyvind Kolås's avatar
Øyvind Kolås committed
51 52 53

#else

54
#include <gegl-plugin.h>
55
struct _GeglOp
Øyvind Kolås's avatar
Øyvind Kolås committed
56
{
57 58
  GeglOperationMeta parent_instance;
  gpointer          properties;
59

Øyvind Kolås's avatar
Øyvind Kolås committed
60 61 62 63 64 65
  GeglNode *self;
  GeglNode *input;
  GeglNode *aux;
  GeglNode *output;

  GeglNode *composite_op;
66
  GeglNode *translate;
67
  GeglNode *opacity;
68
  GeglNode *scale;
69
  GeglNode *load;
70

71
  gchar *cached_path;
72 73

  gdouble p_opacity;
74
  gdouble p_scale;
75 76
  gdouble p_x;
  gdouble p_y;
77
  gchar  *p_composite_op;
Øyvind Kolås's avatar
Øyvind Kolås committed
78 79
};

80 81 82
typedef struct
{
  GeglOperationMetaClass parent_class;
83
} GeglOpClass;
84

85 86
#define GEGL_OP_C_FILE "layer.c"
#include "gegl-op.h"
87
GEGL_DEFINE_DYNAMIC_OPERATION(GEGL_TYPE_OPERATION_META)
88 89 90

#include <glib/gprintf.h>

Øyvind Kolås's avatar
Øyvind Kolås committed
91
static void
92
do_setup (GeglOperation *operation)
Øyvind Kolås's avatar
Øyvind Kolås committed
93
{
94 95
  GeglProperties *o = GEGL_PROPERTIES (operation);
  GeglOp         *self = GEGL_OP (operation);
96

97 98 99 100 101 102 103 104 105
  /* If the src is NULL, and we previously used a source, clear what we have
   * cached and directly link the input and output. We don't need a composite
   * operation if we don't have a source, so don't continue preparing.
   */
  if (o->src[0] == 0)
    {
      if (self->cached_path != NULL)
        {
          gegl_node_link (self->input, self->output);
106
          g_free (self->cached_path);
107 108 109 110 111 112 113 114 115
          self->cached_path = NULL;
        }

      return;
    }

  /* Check if the composite operation we're using has changed from that which
   * is already in use.
   */
116
  if (!self->p_composite_op || strcmp (self->p_composite_op, o->composite_op))
Øyvind Kolås's avatar
Øyvind Kolås committed
117
    {
118 119
      gegl_node_set (self->composite_op,
                     "operation", o->composite_op,
Øyvind Kolås's avatar
Øyvind Kolås committed
120
                     NULL);
121 122 123
      if (self->p_composite_op)
        g_free (self->p_composite_op);
      self->p_composite_op = g_strdup (o->composite_op);
124
    }
125

126 127 128 129 130 131
  /* Load a src image, and relink the input/composite/output chain, as it
   * will currently be set to an input/output chain without a composite
   * source.
   */

  if (self->cached_path == NULL || strcmp (o->src, self->cached_path))
132
    {
133 134 135 136 137 138
      gegl_node_set (self->load,
          "operation", "gegl:load",
          NULL);
      gegl_node_set (self->load,
          "path",  o->src,
          NULL);
139

140 141 142 143 144 145 146
      /* Currently not using the composite op, reinsert it */
      if (!self->cached_path)
        gegl_node_link_many (self->input, self->composite_op, self->output, NULL);

      if (self->cached_path)
        g_free (self->cached_path);
      self->cached_path = g_strdup (o->src);
Øyvind Kolås's avatar
Øyvind Kolås committed
147 148
    }

149 150 151 152 153 154 155 156 157
  if (o->scale != self->p_scale)
    {
      gegl_node_set (self->scale,
                     "x",  o->scale,
                     "y",  o->scale,
                     NULL);
      self->p_scale= o->scale;
    }

158
  if (o->opacity != self->p_opacity)
159
    {
160 161
      gegl_node_set (self->opacity,
                     "value",  o->opacity,
162
                     NULL);
163
      self->p_opacity = o->opacity;
164 165
    }

166 167
  if (o->x != self->p_x ||
      o->y != self->p_y)
168
    {
169
      gegl_node_set (self->translate,
170 171
                     "x",  o->x,
                     "y",  o->y,
172
                     NULL);
173 174
      self->p_x = o->x;
      self->p_y = o->y;
175
    }
176 177


Øyvind Kolås's avatar
Øyvind Kolås committed
178 179
}

180 181 182 183 184 185 186
static void
my_set_property (GObject      *gobject,
                 guint         property_id,
                 const GValue *value,
                 GParamSpec   *pspec)
{
  GeglOperation *operation = GEGL_OPERATION (gobject);
187
  GeglOp        *self      = GEGL_OP (operation);
188 189 190 191 192 193 194 195 196

  /* 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);

  if (self->load)
    do_setup (operation);
}

197
static void attach (GeglOperation *operation)
Øyvind Kolås's avatar
Øyvind Kolås committed
198
{
199 200
  GeglOp         *self = GEGL_OP (operation);
  GeglProperties *o    = GEGL_PROPERTIES (operation);
201
  GeglNode *gegl;
Øyvind Kolås's avatar
Øyvind Kolås committed
202

203 204
  self->self = GEGL_OPERATION (self)->node;
  gegl = self->self;
Øyvind Kolås's avatar
Øyvind Kolås committed
205

206 207 208
  self->input = gegl_node_get_input_proxy (gegl, "input");
  self->aux = gegl_node_get_input_proxy (gegl, "aux");
  self->output = gegl_node_get_output_proxy (gegl, "output");
Øyvind Kolås's avatar
Øyvind Kolås committed
209

210 211
  self->composite_op = gegl_node_new_child (gegl,
                                         "operation", o->composite_op,
Øyvind Kolås's avatar
Øyvind Kolås committed
212 213
                                         NULL);

214
  self->translate = gegl_node_new_child (gegl, "operation", "gegl:translate", NULL);
215
  self->scale = gegl_node_new_child (gegl, "operation", "gegl:scale-ratio", NULL);
216
  self->opacity = gegl_node_new_child (gegl, "operation", "gegl:opacity", NULL);
217

218
  self->load = gegl_node_new_child (gegl,
219
                                    "operation", "gegl:text",
220
                                    "string", "Load operation placeholder",
221
                                    NULL);
Øyvind Kolås's avatar
Øyvind Kolås committed
222

223
  gegl_node_link_many (self->load, self->scale, self->opacity, self->translate,
224
                       NULL);
225
  gegl_node_link_many (self->input, self->composite_op, self->output, NULL);
226
  gegl_node_connect_from (self->composite_op, "aux", self->translate, "output");
227

228 229 230 231 232 233 234 235
  gegl_operation_meta_watch_nodes (operation,
                                   self->composite_op,
                                   self->translate,
                                   self->scale,
                                   self->opacity,
                                   self->load,
                                   NULL);

236
  do_setup (operation);
237 238
}

239

240 241 242
static void
finalize (GObject *object)
{
243
  GeglOp *self = GEGL_OP (object);
244

245
  if (self->cached_path)
246
    g_free (self->cached_path);
247
  if (self->p_composite_op)
248
    g_free (self->p_composite_op);
249

250
  G_OBJECT_CLASS (gegl_op_parent_class)->finalize (object);
Øyvind Kolås's avatar
Øyvind Kolås committed
251 252
}

253
static void
254
gegl_op_class_init (GeglOpClass *klass)
Øyvind Kolås's avatar
Øyvind Kolås committed
255
{
256 257
  GObjectClass       *object_class;
  GeglOperationClass *operation_class;
258

259 260 261
  object_class    = G_OBJECT_CLASS (klass);
  operation_class = GEGL_OPERATION_CLASS (klass);

262 263
  object_class->finalize     = finalize;
  object_class->set_property = my_set_property;
264 265

  operation_class->attach = attach;
266 267 268 269

  gegl_operation_class_set_keys (operation_class,
    "name"       , "gegl:layer",
    "categories" , "meta",
270
    "description", _("A layer in the traditional sense"),
271
    NULL);
272 273
}

Øyvind Kolås's avatar
Øyvind Kolås committed
274
#endif