Blame operations/common/color-to-alpha.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
 * Color To Alpha plug-in v1.0 by Seth Burgess, sjburges@gimp.org 1999/05/14
Packit bc1512
 *  with algorithm by clahey
Packit bc1512
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
Packit bc1512
 * Copyright (C) 2011 Robert Sasu <sasu.robert@gmail.com>
Packit bc1512
 */
Packit bc1512
Packit bc1512
#include "config.h"
Packit bc1512
#include <glib/gi18n-lib.h>
Packit bc1512
Packit bc1512
#ifdef GEGL_CHANT_PROPERTIES
Packit bc1512
Packit bc1512
gegl_chant_color (color, _("Color"), "black",
Packit bc1512
                  _("The color to render (defaults to 'black')"))
Packit bc1512
Packit bc1512
#else
Packit bc1512
Packit bc1512
#define GEGL_CHANT_TYPE_FILTER
Packit bc1512
#define GEGL_CHANT_C_FILE       "color-to-alpha.c"
Packit bc1512
Packit bc1512
#include "gegl-chant.h"
Packit bc1512
#include <stdio.h>
Packit bc1512
#include <math.h>
Packit bc1512
Packit bc1512
static void prepare (GeglOperation *operation)
Packit bc1512
{
Packit bc1512
  gegl_operation_set_format (operation, "input",
Packit bc1512
                             babl_format ("RGBA float"));
Packit bc1512
  gegl_operation_set_format (operation, "output",
Packit bc1512
                             babl_format ("RGBA float"));
Packit bc1512
}
Packit bc1512
Packit bc1512
/*
Packit bc1512
 * An excerpt from a discussion on #gimp that sheds some light on the ideas
Packit bc1512
 * behind the algorithm that is being used here.
Packit bc1512
 *
Packit bc1512
 <clahey>   so if a1 > c1, a2 > c2, and a3 > c2 and a1 - c1 > a2-c2, a3-c3,
Packit bc1512
 then a1 = b1 * alpha + c1 * (1-alpha)
Packit bc1512
 So, maximizing alpha without taking b1 above 1 gives
Packit bc1512
 a1 = alpha + c1(1-alpha) and therefore alpha = (a1-c1) / (1-c1).
Packit bc1512
 <sjburges> clahey: btw, the ordering of that a2, a3 in the white->alpha didn't
Packit bc1512
 matter
Packit bc1512
 <clahey>   sjburges: You mean that it could be either a1, a2, a3 or
Packit bc1512
 a1, a3, a2?
Packit bc1512
 <sjburges> yeah
Packit bc1512
 <sjburges> because neither one uses the other
Packit bc1512
 <clahey>   sjburges: That's exactly as it should be.  They are both just
Packit bc1512
 getting reduced to the same amount, limited by the the darkest
Packit bc1512
 color.
Packit bc1512
 <clahey>   Then a2 = b2 * alpha + c2 * (1- alpha).  Solving for b2 gives
Packit bc1512
 b2 = (a1-c2)/alpha + c2.
Packit bc1512
 <sjburges> yeah
Packit bc1512
 <clahey>   That gives us are formula for if the background is darker than the
Packit bc1512
 foreground? Yep.
Packit bc1512
 <clahey>   Next if a1 < c1, a2 < c2, a3 < c3, and c1-a1 > c2-a2, c3-a3, and
Packit bc1512
 by our desired result a1 = b1 * alpha + c1 * (1-alpha),
Packit bc1512
 we maximize alpha without taking b1 negative gives
Packit bc1512
 alpha = 1 - a1 / c1.
Packit bc1512
 <clahey>   And then again, b2 = (a2-c2) / alpha + c2 by the same formula.
Packit bc1512
 (Actually, I think we can use that formula for all cases, though
Packit bc1512
 it may possibly introduce rounding error.
Packit bc1512
 <clahey>   sjburges: I like the idea of using floats to avoid rounding error.
Packit bc1512
 Good call.
Packit bc1512
*/
Packit bc1512
Packit bc1512
static void
Packit bc1512
color_to_alpha (gfloat     *color,
Packit bc1512
                gfloat     *src,
Packit bc1512
                gint        offset)
Packit bc1512
{
Packit bc1512
  gint i;
Packit bc1512
  gfloat temp[4];
Packit bc1512
Packit bc1512
  temp[3] = src[offset + 3];
Packit bc1512
Packit bc1512
  for (i=0; i<3; i++)
Packit bc1512
    {
Packit bc1512
      if (color[i] < 1.e-4f)
Packit bc1512
        temp[i] = src[offset+i];
Packit bc1512
      else if (src[offset+i] > color[i])
Packit bc1512
        temp[i] = (src[offset+i] - color[i]) / (1.0f - color[i]);
Packit bc1512
      else if (src[offset+i] < color[i])
Packit bc1512
        temp[i] = (color[i] - src[offset+i]) / color[i];
Packit bc1512
      else
Packit bc1512
        temp[i] = 0.0f;
Packit bc1512
    }
Packit bc1512
Packit bc1512
  if (temp[0] > temp[1])
Packit bc1512
    {
Packit bc1512
      if (temp[0] > temp[2])
Packit bc1512
        src[offset+3] = temp[0];
Packit bc1512
      else
Packit bc1512
        src[offset+3] = temp[2];
Packit bc1512
    }
Packit bc1512
  else if (temp[1] > temp[2])
Packit bc1512
    src[offset+3] = temp[1];
Packit bc1512
  else
Packit bc1512
    src[offset+3] = temp[2];
Packit bc1512
Packit bc1512
  if (src[offset+3] < 1.e-4f)
Packit bc1512
    return;
Packit bc1512
Packit bc1512
  for (i=0; i<3; i++)
Packit bc1512
    src[offset+i] = (src[offset+i] - color[i]) / src[offset+3] + color[i];
Packit bc1512
Packit bc1512
  src[offset+3] *= temp[3];
Packit bc1512
}
Packit bc1512
Packit bc1512
Packit bc1512
Packit bc1512
static gboolean
Packit bc1512
process (GeglOperation       *operation,
Packit bc1512
         GeglBuffer          *input,
Packit bc1512
         GeglBuffer          *output,
Packit bc1512
         const GeglRectangle *result,
Packit bc1512
         gint                 level)
Packit bc1512
{
Packit bc1512
  GeglChantO *o      = GEGL_CHANT_PROPERTIES (operation);
Packit bc1512
  const Babl *format = babl_format ("RGBA float");
Packit bc1512
  gfloat     *src_buf, color[4];
Packit bc1512
  gint        x;
Packit bc1512
Packit bc1512
  src_buf = g_new0 (gfloat, result->width * result->height * 4);
Packit bc1512
Packit bc1512
  gegl_buffer_get (input, result, 1.0, format, src_buf,
Packit bc1512
                   GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
Packit bc1512
Packit bc1512
  gegl_color_get_pixel (o->color, babl_format ("RGBA float"), color);
Packit bc1512
Packit bc1512
  for (x = 0; x < result->width * result->height; x++)
Packit bc1512
    color_to_alpha (color, src_buf, 4 * x);
Packit bc1512
Packit bc1512
  gegl_buffer_set (output, result, 0, format, src_buf, GEGL_AUTO_ROWSTRIDE);
Packit bc1512
Packit bc1512
  g_free (src_buf);
Packit bc1512
Packit bc1512
  return TRUE;
Packit bc1512
}
Packit bc1512
Packit bc1512
Packit bc1512
static void
Packit bc1512
gegl_chant_class_init (GeglChantClass *klass)
Packit bc1512
{
Packit bc1512
  GeglOperationClass       *operation_class;
Packit bc1512
  GeglOperationFilterClass *filter_class;
Packit bc1512
Packit bc1512
  operation_class = GEGL_OPERATION_CLASS (klass);
Packit bc1512
  filter_class    = GEGL_OPERATION_FILTER_CLASS (klass);
Packit bc1512
Packit bc1512
  filter_class->process    = process;
Packit bc1512
  operation_class->prepare = prepare;
Packit bc1512
Packit bc1512
  gegl_operation_class_set_keys (operation_class,
Packit bc1512
    "name"       , "gegl:color-to-alpha",
Packit bc1512
    "categories" , "color",
Packit bc1512
    "description", _("Performs color-to-alpha on the image."),
Packit bc1512
    NULL);
Packit bc1512
}
Packit bc1512
Packit bc1512
#endif