save.c 5.93 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 21 22
 *
 * Copyright 2010 Danny Robson <danny@blubinc.net>
 */

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


23
#ifdef GEGL_PROPERTIES
24

25 26
property_file_path (path, _("File"), "")
    description(_("Path of file to save."))
27 28 29

#else

30
#define GEGL_OP_NAME     save
31
#define GEGL_OP_C_SOURCE save.c
32 33 34

#include "gegl-plugin.h"

35
struct _GeglOp
36 37 38 39 40 41 42 43 44 45 46 47
{
  GeglOperationSink  parent_instance;
  gpointer           properties;

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

typedef struct
{
  GeglOperationSinkClass parent_class;
48
} GeglOpClass;
49

50
#include "gegl-op.h"
51 52 53 54 55
GEGL_DEFINE_DYNAMIC_OPERATION(GEGL_TYPE_OPERATION_SINK)

#include <stdio.h>


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

  /* If prepare has already been invoked, bail out early */
64
  if (self->cached_path && o->path && !strcmp (o->path, self->cached_path))
65
      return;
66
  if (o->path == NULL || *o->path == '\0')
67
    return;
68 69 70 71 72 73 74 75 76
  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, '.');
77
  handler   = extension ? gegl_operation_handlers_get_saver (extension) : NULL;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

  if (handler)
    {
      gegl_node_set (self->save,
                     "operation", handler,
                     "path",      o->path,
                     NULL);
    }
  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);
95
  return;
96 97 98 99 100 101 102 103 104
}


/* 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)
{
105
  GeglOp   *self = GEGL_OP (operation);
106 107
  /* const gchar *nodename; */
  /* gchar       *childname; */
108 109 110 111 112 113 114 115 116 117 118 119

  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 */
120
  /*  nodename = gegl_node_get_debug_name (operation->node);
121 122 123 124 125 126 127 128
  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);
129
  g_free (childname);*/
130 131 132 133 134 135 136 137 138

  /* 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
139 140 141 142
gegl_save_set_property (GObject      *gobject,
                        guint         property_id,
                        const GValue *value,
                        GParamSpec   *pspec)
143
{
144
  GeglOperation *operation = GEGL_OPERATION (gobject);
145
  GeglOp     *self = GEGL_OP (operation);
146 147 148 149 150 151 152

  /* 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);
153 154 155 156 157 158 159
}


static gboolean
gegl_save_process (GeglOperation        *operation,
                   GeglOperationContext *context,
                   const gchar          *output_pad,
160 161
                   const GeglRectangle  *roi,
                   gint                  level)
162
{
163
  GeglOp *self = GEGL_OP (operation);
164

165
  return gegl_operation_process (gegl_node_get_gegl_operation (self->save),
166 167
                                 context,
                                 output_pad,
168 169
                                 roi,
                                 level);
170 171 172 173 174
}

static void
gegl_save_dispose (GObject *object)
{
175
  GeglOp *self = GEGL_OP (object);
176

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

179
  G_OBJECT_CLASS (gegl_op_parent_class)->dispose (object);
180 181 182 183
}


static void
184
gegl_op_class_init (GeglOpClass *klass)
185 186 187 188 189
{
  GObjectClass           *object_class    = G_OBJECT_CLASS (klass);
  GeglOperationSinkClass *sink_class      = GEGL_OPERATION_SINK_CLASS (klass);
  GeglOperationClass     *operation_class = GEGL_OPERATION_CLASS (klass);

190 191
  object_class->dispose      = gegl_save_dispose;
  object_class->set_property = gegl_save_set_property;
192 193 194 195 196 197

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

  sink_class->needs_full = TRUE;

198 199
  gegl_operation_class_set_keys (operation_class,
    "name"       , "gegl:save",
200
    "title",       _("Save"),
201 202
    "categories" , "meta:output",
    "description",
203
        _("Multipurpose file saver, that uses other native save handlers depending on extension, use the format specific save ops to specify additional parameters."),
204
        NULL);
205 206 207 208
}

#endif