raw-load.c 5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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 2 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
 * License along with GEGL; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
 */
20 21 22
#ifdef GEGL_CHANT_SELF
gegl_chant_string (path, "/tmp/test.raw", "path to file to load")
gegl_chant_pointer (cached, "private")
23 24
#else

25 26 27
#define GEGL_CHANT_SOURCE
#define GEGL_CHANT_NAME            raw_load
#define GEGL_CHANT_DESCRIPTION     "Loads a raw file, by communicating with dcraw through pipes."
28

29 30 31
#define GEGL_CHANT_SELF            "raw-load.c"
#define GEGL_CHANT_CATEGORIES      "hidden"
#define GEGL_CHANT_CLASS_CONSTRUCT
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include "gegl-chant.h"
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define PIPE_MODE "r"
#define MAX_SAMPLE 65535
#define ERROR -1

static void load_cache (ChantInstance *op_raw_load);

static gboolean
evaluate (GeglOperation *operation,
          const gchar   *output_prop)
{
  GeglOperationSource *op_source = GEGL_OPERATION_SOURCE(operation);
49
  ChantInstance       *self      = GEGL_CHANT_INSTANCE (operation);
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

  if(strcmp("output", output_prop))
    return FALSE;

  if (op_source->output)
    g_object_unref (op_source->output);
  op_source->output=NULL;

  g_assert (self->cached);
  {
    GeglRect *result = gegl_operation_result_rect (operation);

    op_source->output = g_object_new (GEGL_TYPE_BUFFER,
                        "source", self->cached,
                        "x",      result->x,
                        "y",      result->y,
                        "width",  result->w,
                        "height", result->h,
                        NULL);
69 70
    g_object_unref (self->cached);
    self->cached = NULL;
71 72 73 74 75 76 77 78
  }
  return  TRUE;
}


static gboolean
calc_have_rect (GeglOperation *operation)
{
79
  ChantInstance *self = GEGL_CHANT_INSTANCE (operation);
80 81 82 83 84 85 86 87 88 89 90 91 92 93
  gint width, height;

  load_cache (self);

  width  = GEGL_BUFFER (self->cached)->width;
  height = GEGL_BUFFER (self->cached)->height;

  gegl_operation_set_have_rect (operation, 0, 0, width, height);

  return TRUE;
}

static void dispose (GObject *gobject)
{
94
  ChantInstance *self = GEGL_CHANT_INSTANCE (gobject);
95 96 97 98 99 100 101 102 103 104
  if (self->cached)
    {
      g_object_unref (self->cached);
      self->cached = NULL;
    }
  G_OBJECT_CLASS (g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)))->dispose (gobject);
}

static void class_init (GeglOperationClass *klass)
{
105
  static gboolean done=FALSE;
106
  G_OBJECT_CLASS (klass)->dispose = dispose;
107 108 109 110 111 112 113 114 115 116

  if (done)
    return;
  gegl_extension_handler_register (".raw", "raw-load");
  gegl_extension_handler_register (".RAW", "raw-load");
  gegl_extension_handler_register (".raf", "raw-load");
  gegl_extension_handler_register (".RAF", "raw-load");
  gegl_extension_handler_register (".nef", "raw-load");
  gegl_extension_handler_register (".NEF", "raw-load");
  done = TRUE;
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
}

static void
load_cache (ChantInstance *op_raw_load)
{
  if (!op_raw_load->cached)
    {
      FILE *pfp;
      gchar *command;

      gint width, height, val_max;
      char newline;

      command = g_malloc (strlen (op_raw_load->path) + 128);
      if (!command)
        return;
      sprintf (command, "dcraw -4 -c '%s'\n", op_raw_load->path);
      pfp = popen (command, PIPE_MODE);
      free (command);

      if (fscanf (pfp, "P6 %d %d %d %c",
         &width, &height, &val_max, &newline) != 4)
        {
          pclose (pfp);
          g_warning ("not able to aquire raw data");
          return;
        }

       op_raw_load->cached = g_object_new (GEGL_TYPE_BUFFER,
                                      "format", babl_format_new (
                                        babl_model ("RGB"),
                                        babl_type ("u16"),
                                        babl_component ("G"),
                                        babl_component ("B"),
                                        babl_component ("R"),
                                        NULL),
                                      "x",      0,
                                      "y",      0,
                                      "width",  width,
                                      "height", height,
                                      NULL);
         {
           
           guchar *buf=g_malloc (width * height * 3 * 2);
           fread (buf, 1, width * height * 3 * 2, pfp);
           gegl_buffer_set (op_raw_load->cached, buf);
           g_free (buf);
         }
       fclose (pfp);
    }
}

#endif