Blame operations/common/load.c

Packit bc1512
/* This file is an image processing operation for GEGL
Packit bc1512
 *
Packit bc1512
 * GEGL is free software; you can redistribute it and/or
Packit bc1512
 * modify it under the terms of the GNU Lesser General Public
Packit bc1512
 * License as published by the Free Software Foundation; either
Packit bc1512
 * version 3 of the License, or (at your option) any later version.
Packit bc1512
 *
Packit bc1512
 * GEGL is distributed in the hope that it will be useful,
Packit bc1512
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit bc1512
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit bc1512
 * Lesser General Public License for more details.
Packit bc1512
 *
Packit bc1512
 * You should have received a copy of the GNU Lesser General Public
Packit bc1512
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
Packit bc1512
 *
Packit bc1512
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
Packit bc1512
 */
Packit bc1512
Packit bc1512
#include "config.h"
Packit bc1512
#include <glib/gi18n-lib.h>
Packit bc1512
Packit bc1512
Packit bc1512
#ifdef GEGL_CHANT_PROPERTIES
Packit bc1512
Packit bc1512
gegl_chant_file_path (path, _("File"), "", _("Path of file to load."))
Packit bc1512
Packit bc1512
#else
Packit bc1512
Packit bc1512
#define GEGL_CHANT_C_FILE       "load.c"
Packit bc1512
Packit bc1512
#include "gegl-plugin.h"
Packit bc1512
Packit bc1512
struct _GeglChant
Packit bc1512
{
Packit bc1512
  GeglOperationMeta parent_instance;
Packit bc1512
  gpointer          properties;
Packit bc1512
Packit bc1512
  GeglNode *output;
Packit bc1512
  GeglNode *load;
Packit bc1512
  gchar    *cached_path;
Packit bc1512
};
Packit bc1512
Packit bc1512
typedef struct
Packit bc1512
{
Packit bc1512
  GeglOperationMetaClass parent_class;
Packit bc1512
} GeglChantClass;
Packit bc1512
Packit bc1512
#include "gegl-chant.h"
Packit bc1512
GEGL_DEFINE_DYNAMIC_OPERATION(GEGL_TYPE_OPERATION_META)
Packit bc1512
Packit bc1512
#include <stdio.h>
Packit bc1512
Packit bc1512
static void attach (GeglOperation *operation)
Packit bc1512
{
Packit bc1512
  GeglChant *self = GEGL_CHANT (operation);
Packit bc1512
Packit bc1512
  self->output = gegl_node_get_output_proxy (operation->node, "output");
Packit bc1512
Packit bc1512
  self->load = gegl_node_new_child (operation->node,
Packit bc1512
                                    "operation", "gegl:text",
Packit bc1512
                                    "string",    "foo",
Packit bc1512
                                    NULL);
Packit bc1512
Packit bc1512
  gegl_node_link (self->load, self->output);
Packit bc1512
}
Packit bc1512
Packit bc1512
static GeglNode *
Packit bc1512
detect (GeglOperation *operation,
Packit bc1512
        gint           x,
Packit bc1512
        gint           y)
Packit bc1512
{
Packit bc1512
  GeglChant *self = GEGL_CHANT (operation);
Packit bc1512
  GeglNode *output = self->output;
Packit bc1512
  GeglRectangle bounds;
Packit bc1512
Packit bc1512
  bounds = gegl_node_get_bounding_box (output); /* hopefully this is
Packit bc1512
                                                   as correct as original
Packit bc1512
                                                   which was peeking
Packit bc1512
                                                   directly into output->have_rect
Packit bc1512
                                                   */
Packit bc1512
Packit bc1512
  if (x >= bounds.x &&
Packit bc1512
      y >= bounds.y &&
Packit bc1512
      x  < bounds.x + bounds.width &&
Packit bc1512
      y  < bounds.y + bounds.height)
Packit bc1512
    return operation->node;
Packit bc1512
Packit bc1512
  return NULL;
Packit bc1512
}
Packit bc1512
Packit bc1512
static void
Packit bc1512
prepare (GeglOperation *operation)
Packit bc1512
{
Packit bc1512
  GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
Packit bc1512
  GeglChant  *self = GEGL_CHANT (operation);
Packit bc1512
  /* warning: this might trigger regeneration of the graph,
Packit bc1512
   *          for now this is evaded by just ignoring additional
Packit bc1512
   *          requests to be made into members of the graph
Packit bc1512
   */
Packit bc1512
Packit bc1512
    if (o->path[0]==0 && self->cached_path == NULL)
Packit bc1512
      {
Packit bc1512
          gegl_node_set (self->load,
Packit bc1512
                         "operation", "gegl:text",
Packit bc1512
                         "size", 20.0,
Packit bc1512
                         "string", "Eeeeek!",
Packit bc1512
                         NULL);
Packit bc1512
      }
Packit bc1512
    else
Packit bc1512
    {
Packit bc1512
      if (o->path[0] &&
Packit bc1512
          (self->cached_path == NULL || strcmp (o->path, self->cached_path)))
Packit bc1512
        {
Packit bc1512
          const gchar *extension = strrchr (o->path, '.');
Packit bc1512
          const gchar *handler   = NULL;
Packit bc1512
Packit bc1512
          if (!g_file_test (o->path, G_FILE_TEST_EXISTS))
Packit bc1512
            {
Packit bc1512
              gchar *name = g_filename_display_name (o->path);
Packit bc1512
              gchar *tmp  = g_strdup_printf ("File '%s' does not exist", name);
Packit bc1512
              g_free (name);
Packit bc1512
Packit bc1512
              g_warning ("load: %s", tmp);
Packit bc1512
              gegl_node_set (self->load,
Packit bc1512
                             "operation", "gegl:text",
Packit bc1512
                             "size", 12.0,
Packit bc1512
                             "string", tmp,
Packit bc1512
                             NULL);
Packit bc1512
              g_free (tmp);
Packit bc1512
            }
Packit bc1512
          else
Packit bc1512
            {
Packit bc1512
              if (extension)
Packit bc1512
                handler = gegl_extension_handler_get (extension);
Packit bc1512
              gegl_node_set (self->load,
Packit bc1512
                             "operation", handler,
Packit bc1512
                             NULL);
Packit bc1512
              gegl_node_set (self->load,
Packit bc1512
                             "path",  o->path,
Packit bc1512
                             NULL);
Packit bc1512
            }
Packit bc1512
          if (self->cached_path)
Packit bc1512
            g_free (self->cached_path);
Packit bc1512
          self->cached_path = g_strdup (o->path);
Packit bc1512
        }
Packit bc1512
    }
Packit bc1512
}
Packit bc1512
Packit bc1512
static void
Packit bc1512
dispose (GObject *object)
Packit bc1512
{
Packit bc1512
  GeglChant *self = GEGL_CHANT (object);
Packit bc1512
Packit bc1512
  if (self->cached_path)
Packit bc1512
    {
Packit bc1512
      g_free (self->cached_path);
Packit bc1512
      self->cached_path = NULL;
Packit bc1512
    }
Packit bc1512
Packit bc1512
  G_OBJECT_CLASS (gegl_chant_parent_class)->dispose (object);
Packit bc1512
}
Packit bc1512
Packit bc1512
static void
Packit bc1512
gegl_chant_class_init (GeglChantClass *klass)
Packit bc1512
{
Packit bc1512
  GObjectClass       *object_class    = G_OBJECT_CLASS (klass);
Packit bc1512
  GeglOperationClass *operation_class = GEGL_OPERATION_CLASS (klass);
Packit bc1512
Packit bc1512
  object_class->dispose = dispose;
Packit bc1512
Packit bc1512
  operation_class->attach = attach;
Packit bc1512
  operation_class->detect = detect;
Packit bc1512
  operation_class->prepare = prepare;
Packit bc1512
  operation_class->no_cache = TRUE;
Packit bc1512
Packit bc1512
  gegl_operation_class_set_keys (operation_class,
Packit bc1512
    "name"       , "gegl:load",
Packit bc1512
    "categories" , "meta:input",
Packit bc1512
    "description",
Packit bc1512
          _("Multipurpose file loader, that uses other native handlers, and "
Packit bc1512
            "fallback conversion using image magick's convert."),
Packit bc1512
    NULL);
Packit bc1512
Packit bc1512
}
Packit bc1512
Packit bc1512
#endif