cast-space.c 4.07 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 23 24
 *
 * Copyright 2014 Michael Natterer <mitch@gimp.org>
 *
 */

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

#ifdef GEGL_PROPERTIES

25 26 27 28 29 30
property_string (name, _("Name"), "sRGB")
   description (_("One of: sRGB, Adobish, Rec2020, ProPhoto, Apple, ACEScg, ACES2065-1"))
property_format (pointer, _("Pointer"), NULL)
   description (_("pointer to a const * Babl space"))
property_file_path (path, _("Path"), "")
  description (_("File system path to ICC matrix profile to load"))
31 32 33

#else

34
#define GEGL_OP_COMPOSER
35 36 37 38 39 40 41 42 43 44 45
#define GEGL_OP_NAME     cast_space
#define GEGL_OP_C_SOURCE cast-space.c

#include "gegl-op.h"
#include <stdio.h>

static void
prepare (GeglOperation *operation)
{
  const Babl *in_format = gegl_operation_get_source_format (operation,
                                                            "input");
46 47
  const Babl *aux_format = gegl_operation_get_source_format (operation,
                                                            "aux");
48
  GeglProperties *o = GEGL_PROPERTIES (operation);
49 50 51 52
  const Babl *space = babl_space (o->name);
  if (o->pointer)
    space = o->pointer;
  if (o->path)
53 54 55
  {
    gchar *icc_data = NULL;
    gsize icc_length;
56
    g_file_get_contents (o->path, &icc_data, &icc_length, NULL);
57 58 59
    if (icc_data)
    {
      const char *error = NULL;
60
      const Babl *s = babl_space_from_icc (icc_data, (gint)icc_length,
61 62 63 64 65
                                 BABL_ICC_INTENT_RELATIVE_COLORIMETRIC, &error);
      if (s) space = s;
      g_free (icc_data);
    }
  }
66 67 68 69
  if (aux_format)
  {
    space = babl_format_get_space (aux_format);
  }
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

  gegl_operation_set_format (operation, "input",
                             babl_format_with_space ("R'G'B'A float", in_format));
  gegl_operation_set_format (operation, "output",
                             babl_format_with_space ("R'G'B'A float", space));
}

static gboolean
process (GeglOperation        *operation,
         GeglOperationContext *context,
         const gchar          *output_prop,
         const GeglRectangle  *roi,
         gint                  level)
{
  //GeglProperties *o = GEGL_PROPERTIES (operation);
  const Babl *in_format = gegl_operation_get_format (operation, "input");
  const Babl *out_format = gegl_operation_get_format (operation, "output");
  GeglBuffer *input;
  GeglBuffer *output;

  if (strcmp (output_prop, "output"))
    {
      g_warning ("cast-format: requested processing of %s pad", output_prop);
      return FALSE;
    }

  input = (GeglBuffer*) gegl_operation_context_dup_object (context, "input");
  if (! input)
    {
      g_warning ("cast: received NULL input");
      return FALSE;
    }

  output = gegl_buffer_new (roi, in_format);

  gegl_buffer_copy (input,  roi, GEGL_ABYSS_NONE,
                    output, roi);


  gegl_buffer_set_format (output, out_format);

  g_object_unref (input);

  gegl_operation_context_take_object (context, "output", G_OBJECT (output));

  return TRUE;
}

static void
gegl_op_class_init (GeglOpClass *klass)
{
  GeglOperationClass *operation_class = GEGL_OPERATION_CLASS (klass);

123 124
  operation_class->prepare = prepare;
  operation_class->process = process;
125 126 127

  gegl_operation_class_set_keys (operation_class,
    "name",       "gegl:cast-space",
128
    "title",      _("Cast color space"),
129
    "categories", "core:color",
130
    "description", _("Override the specified color space setting a pointer to a format override the string property and setting an aux pad overrides both. "),
131 132 133 134
    NULL);
}

#endif