introspect.c 4.7 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 <https://www.gnu.org/licenses/>.
15 16 17
 *
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
 */
18 19

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


24
#ifdef GEGL_PROPERTIES
25

26
property_object(node, _("Node"), GEGL_TYPE_NODE)
27 28 29

#else

30
#define GEGL_OP_SOURCE
31
#define GEGL_OP_NAME     introspect
32
#define GEGL_OP_C_SOURCE introspect.c
33

34
#include "gegl-op.h"
35
gchar *gegl_to_dot                       (GeglNode       *node);
36
#include <stdio.h>
37 38

static void
39
gegl_introspect_load_cache (GeglProperties *op_introspect)
40
{
41 42 43 44 45 46 47
  GeglBuffer *new_buffer   = NULL;
  GeglNode   *png_load     = NULL;
  GeglNode   *buffer_sink  = NULL;
  gchar      *dot_string   = NULL;
  gchar      *png_filename = NULL;
  gchar      *dot_filename = NULL;
  gchar      *dot_cmd      = NULL;
48

49
  if (op_introspect->user_data || op_introspect->node == NULL)
50 51 52 53 54 55 56
    return;

  /* Construct temp filenames */
  dot_filename = g_build_filename (g_get_tmp_dir (), "gegl-introspect.dot", NULL);
  png_filename = g_build_filename (g_get_tmp_dir (), "gegl-introspect.png", NULL);

  /* Construct the .dot source */
57
  dot_string = gegl_to_dot (GEGL_NODE (op_introspect->node));
58 59 60 61
  g_file_set_contents (dot_filename, dot_string, -1, NULL);

  /* Process the .dot to a .png */
  dot_cmd = g_strdup_printf ("dot -o %s -Tpng %s", png_filename, dot_filename);
62 63
  if (system (dot_cmd) == -1)
    g_warning ("Error executing GraphViz dot program");
64

65 66
  /* Create a graph that loads the png into a GeglBuffer and process
   * it
67
   */
68 69 70 71 72 73 74 75 76 77 78
  png_load = gegl_node_new_child (NULL,
                                  "operation", "gegl:png-load",
                                  "path",      png_filename,
                                  NULL);
  buffer_sink = gegl_node_new_child (NULL,
                                     "operation", "gegl:buffer-sink",
                                     "buffer",    &new_buffer,
                                     NULL);
  gegl_node_link_many (png_load, buffer_sink, NULL);
  gegl_node_process (buffer_sink);

79
  op_introspect->user_data= new_buffer;
80 81

  /* Cleanup */
82 83
  g_object_unref (buffer_sink);
  g_object_unref (png_load);
84 85 86 87 88
  g_free (dot_string);
  g_free (dot_cmd);
  g_free (dot_filename);
  g_free (png_filename);
}
89

90 91 92
static void
gegl_introspect_dispose (GObject *object)
{
93
  GeglProperties *o = GEGL_PROPERTIES (object);
94

Debarshi Ray's avatar
Debarshi Ray committed
95
  g_clear_object (&o->user_data);
96

97
  G_OBJECT_CLASS (gegl_op_parent_class)->dispose (object);
98 99
}

100
static GeglRectangle
101
gegl_introspect_get_bounding_box (GeglOperation *operation)
102
{
103 104
  GeglRectangle   result = {0,0,0,0};
  GeglProperties *o = GEGL_PROPERTIES (operation);
105 106 107
  gint width, height;

  gegl_introspect_load_cache (o);
108

109
  g_object_get (o->user_data, "width", &width,
110 111 112 113
                               "height", &height, NULL);

  result.width  = width;
  result.height = height;
114 115 116

  return result;
}
117 118

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

127 128
  gegl_introspect_load_cache (o);

129 130 131
  if (!o->user_data)
    return FALSE;

132
  /* gegl_operation_context_take_object() takes the reference we have,
133 134
   * so we must increase it since we want to keep the object
   */
135
  g_object_ref (o->user_data);
136

137
  gegl_operation_context_take_object (context, output_pad, G_OBJECT (o->user_data));
138

139 140 141
  return  TRUE;
}

142
static void
143
gegl_op_class_init (GeglOpClass *klass)
144
{
145
  GObjectClass             *object_class;
146 147
  GeglOperationClass       *operation_class;

148
  object_class    = G_OBJECT_CLASS (klass);
149 150
  operation_class = GEGL_OPERATION_CLASS (klass);

151 152
  object_class->dispose             = gegl_introspect_dispose;

153 154
  operation_class->process          = gegl_introspect_process;
  operation_class->get_bounding_box = gegl_introspect_get_bounding_box;
155

156 157 158 159 160
  gegl_operation_class_set_keys (operation_class,
    "name"       , "gegl:introspect",
    "categories" , "render",
    "description", _("GEGL graph visualizer."),
    NULL);
161 162 163
}

#endif