mono-mixer.c 3.11 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 <http://www.gnu.org/licenses/>.
15 16 17 18
 *
 * Copyright 2006 Mark Probst <mark.probst@gmail.com>
 */

19 20 21 22 23

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


Øyvind Kolås's avatar
Øyvind Kolås committed
24
#ifdef GEGL_CHANT_PROPERTIES
25

26 27 28
gegl_chant_double (red,   _("Red"),   -10.0, 10.0, 0.5,  _("Amount of red"))
gegl_chant_double (green, _("Green"), -10.0, 10.0, 0.25, _("Amount of green"))
gegl_chant_double (blue,  _("Blue"),  -10.0, 10.0, 0.25, _("Amount of blue"))
29

30 31
#else

Øyvind Kolås's avatar
Øyvind Kolås committed
32
#define GEGL_CHANT_TYPE_FILTER
33 34
#define GEGL_CHANT_C_FILE      "mono-mixer.c"

Øyvind Kolås's avatar
Øyvind Kolås committed
35
#include "gegl-chant.h"
36

37 38 39
static void prepare (GeglOperation *operation)
{
  /* set the babl format this operation prefers to work on */
40 41
  gegl_operation_set_format (operation, "input", babl_format ("RGBA float"));
  gegl_operation_set_format (operation, "output", babl_format ("YA float"));
42 43
}

44
static gboolean
45 46 47
process (GeglOperation       *operation,
         GeglBuffer          *input,
         GeglBuffer          *output,
48 49
         const GeglRectangle *result,
         gint                 level)
50
{
51
  GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
52 53 54 55 56
  gfloat      red   = o->red;
  gfloat      green = o->green;
  gfloat      blue  = o->blue;
  gfloat     *in_buf;
  gfloat     *out_buf;
57

58
 if ((result->width > 0) && (result->height > 0))
59
 {
60
     gint num_pixels = result->width * result->height;
61 62 63
     gint i;
     gfloat *in_pixel, *out_pixel;

64 65
     in_buf = g_new (gfloat, 4 * num_pixels);
     out_buf = g_new (gfloat, 2 * num_pixels);
66

67
     gegl_buffer_get (input, result, 1.0, babl_format ("RGBA float"), in_buf, GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
68 69 70 71 72 73 74 75 76 77 78 79

     in_pixel = in_buf;
     out_pixel = out_buf;
     for (i = 0; i < num_pixels; ++i)
     {
         out_pixel[0] = in_pixel[0] * red + in_pixel[1] * green + in_pixel[2] * blue;
         out_pixel[1] = in_pixel[3];

         in_pixel += 4;
         out_pixel += 2;
     }

80
     gegl_buffer_set (output, result, 0, babl_format ("YA float"), out_buf,
81
                      GEGL_AUTO_ROWSTRIDE);
82 83 84 85 86 87 88 89

     g_free (in_buf);
     g_free (out_buf);
 }

 return TRUE;
}

Øyvind Kolås's avatar
Øyvind Kolås committed
90
static void
91
gegl_chant_class_init (GeglChantClass *klass)
Øyvind Kolås's avatar
Øyvind Kolås committed
92 93 94 95
{
  GeglOperationClass       *operation_class;
  GeglOperationFilterClass *filter_class;

96 97
  operation_class = GEGL_OPERATION_CLASS (klass);
  filter_class    = GEGL_OPERATION_FILTER_CLASS (klass);
Øyvind Kolås's avatar
Øyvind Kolås committed
98 99 100 101

  filter_class->process = process;
  operation_class->prepare = prepare;

102
  operation_class->name        = "gegl:mono-mixer";
Øyvind Kolås's avatar
Øyvind Kolås committed
103
  operation_class->categories  = "color";
104
  operation_class->description = _("Monochrome channel mixer");
Øyvind Kolås's avatar
Øyvind Kolås committed
105 106
}

107
#endif