save.c 6.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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
 * version 3 of the License, or (at your option) any later version.
 *
 * 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 <https://www.gnu.org/licenses/>.
15 16 17 18 19 20
 *
 * Copyright 2010 Danny Robson <danny@blubinc.net>
 */

#include "config.h"
#include <glib/gi18n-lib.h>
21
#include <gegl-metadata.h>
22 23


24
#ifdef GEGL_PROPERTIES
25

26 27
property_file_path (path, _("File"), "")
    description(_("Path of file to save."))
28 29
property_object (metadata, _("Metadata"), GEGL_TYPE_METADATA)
    description (_("Object providing image metadata"))
30 31 32

#else

33
#define GEGL_OP_NAME     save
34
#define GEGL_OP_C_SOURCE save.c
35 36 37

#include "gegl-plugin.h"

38
struct _GeglOp
39 40 41 42 43 44 45 46 47 48 49 50
{
  GeglOperationSink  parent_instance;
  gpointer           properties;

  GeglNode          *input;
  GeglNode          *save;
  gchar             *cached_path;
};

typedef struct
{
  GeglOperationSinkClass parent_class;
51
} GeglOpClass;
52

53
#include "gegl-op.h"
54 55 56 57 58
GEGL_DEFINE_DYNAMIC_OPERATION(GEGL_TYPE_OPERATION_SINK)

#include <stdio.h>


59
static void
60 61
gegl_save_set_saver (GeglOperation *operation)
{
62 63
  GeglProperties  *o    = GEGL_PROPERTIES (operation);
  GeglOp   *self = GEGL_OP (operation);
64 65 66
  const gchar *extension, *handler;

  /* If prepare has already been invoked, bail out early */
67
  if (self->cached_path && o->path && !strcmp (o->path, self->cached_path))
68
      return;
69
  if (o->path == NULL || *o->path == '\0')
70
    return;
71 72 73 74 75 76 77 78 79
  g_free (self->cached_path);

  /* Find an extension handler and put it into the graph. The path can be
   * empty, which indicates an error, but not null. Ideally we'd like to
   * report an error here, but there's no way to pass it back to GEGL or the
   * user in prepare()...
   */
  g_assert (o->path);
  extension = strrchr (o->path, '.');
80
  handler   = extension ? gegl_operation_handlers_get_saver (extension) : NULL;
81 82 83 84 85 86 87

  if (handler)
    {
      gegl_node_set (self->save,
                     "operation", handler,
                     "path",      o->path,
                     NULL);
88 89 90 91 92

      if (o->metadata &&
          gegl_operation_find_property (handler, "metadata") != NULL)
        gegl_node_set (self->save, "metadata", o->metadata, NULL);

93 94 95 96 97 98 99 100 101 102
    }
  else
    {
      g_warning ("Unable to find suitable save handler for path '%s'", o->path);
      gegl_node_set (self->save,
                     "operation", "gegl:nop",
                     NULL);
    }

  self->cached_path = g_strdup (o->path);
103
  return;
104 105 106 107 108 109 110 111 112
}


/* Create an input proxy, and temporary save operation, and link together.
 * These will be passed control when gegl_save_process is called later.
 */
static void
gegl_save_attach (GeglOperation *operation)
{
113
  GeglOp   *self = GEGL_OP (operation);
114 115
  /* const gchar *nodename; */
  /* gchar       *childname; */
116 117 118 119 120 121 122 123 124 125 126 127

  g_assert (!self->input);
  g_assert (!self->save);
  g_assert (!self->cached_path);

  /* Initialise and attach child nodes */
  self->input  = gegl_node_get_input_proxy (operation->node, "input");
  self->save   = gegl_node_new_child (operation->node,
                                      "operation", "gegl:nop",
                                      NULL);

  /* Set some debug names for the child nodes */
128
  /*  nodename = gegl_node_get_debug_name (operation->node);
129 130 131 132 133 134 135 136
  g_assert (nodename);

  childname = g_strconcat (nodename, "-save", NULL);
  gegl_node_set_name (self->input, childname);
  g_free (childname);

  childname = g_strconcat (nodename, "-input", NULL);
  gegl_node_set_name (self->input, childname);
137
  g_free (childname);*/
138 139 140 141 142 143 144 145 146

  /* Link the saving node and attempt to set an appropriate save operation,
   * might as well at least try to do this before prepare.
   */
  gegl_node_link (self->input, self->save);
  gegl_save_set_saver (operation);
}

static void
147 148 149 150
gegl_save_set_property (GObject      *gobject,
                        guint         property_id,
                        const GValue *value,
                        GParamSpec   *pspec)
151
{
152
  GeglOperation *operation = GEGL_OPERATION (gobject);
153
  GeglOp     *self = GEGL_OP (operation);
154 155 156 157 158 159 160

  /* 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->save)
    gegl_save_set_saver (operation);
161 162 163 164 165 166 167
}


static gboolean
gegl_save_process (GeglOperation        *operation,
                   GeglOperationContext *context,
                   const gchar          *output_pad,
168 169
                   const GeglRectangle  *roi,
                   gint                  level)
170
{
171
  GeglOp *self = GEGL_OP (operation);
172

173
  return gegl_operation_process (gegl_node_get_gegl_operation (self->save),
174 175
                                 context,
                                 output_pad,
176 177
                                 roi,
                                 level);
178 179 180 181 182
}

static void
gegl_save_dispose (GObject *object)
{
183
  GeglOp *self = GEGL_OP (object);
184

Debarshi Ray's avatar
Debarshi Ray committed
185
  g_clear_pointer (&self->cached_path, g_free);
186

187
  G_OBJECT_CLASS (gegl_op_parent_class)->dispose (object);
188 189 190 191
}


static void
192
gegl_op_class_init (GeglOpClass *klass)
193 194 195 196 197
{
  GObjectClass           *object_class    = G_OBJECT_CLASS (klass);
  GeglOperationSinkClass *sink_class      = GEGL_OPERATION_SINK_CLASS (klass);
  GeglOperationClass     *operation_class = GEGL_OPERATION_CLASS (klass);

198 199
  object_class->dispose      = gegl_save_dispose;
  object_class->set_property = gegl_save_set_property;
200 201 202 203 204 205

  operation_class->attach  = gegl_save_attach;
  operation_class->process = gegl_save_process;

  sink_class->needs_full = TRUE;

206 207
  gegl_operation_class_set_keys (operation_class,
    "name"       , "gegl:save",
208
    "title",       _("Save"),
209 210
    "categories" , "meta:output",
    "description",
211
        _("Multipurpose file saver, that uses other native save handlers depending on extension, use the format specific save ops to specify additional parameters."),
212
        NULL);
213 214 215 216
}

#endif