|
Packit Service |
2781ba |
/* This file is an image processing operation for GEGL
|
|
Packit Service |
2781ba |
*
|
|
Packit Service |
2781ba |
* GEGL is free software; you can redistribute it and/or
|
|
Packit Service |
2781ba |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
2781ba |
* License as published by the Free Software Foundation; either
|
|
Packit Service |
2781ba |
* version 3 of the License, or (at your option) any later version.
|
|
Packit Service |
2781ba |
*
|
|
Packit Service |
2781ba |
* GEGL is distributed in the hope that it will be useful,
|
|
Packit Service |
2781ba |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
2781ba |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
2781ba |
* Lesser General Public License for more details.
|
|
Packit Service |
2781ba |
*
|
|
Packit Service |
2781ba |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
2781ba |
* License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
|
|
Packit Service |
2781ba |
*
|
|
Packit Service |
2781ba |
* Color To Alpha plug-in v1.0 by Seth Burgess, sjburges@gimp.org 1999/05/14
|
|
Packit Service |
2781ba |
* with algorithm by clahey
|
|
Packit Service |
2781ba |
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
Packit Service |
2781ba |
* Copyright (C) 2011 Robert Sasu <sasu.robert@gmail.com>
|
|
Packit Service |
2781ba |
*/
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#include "config.h"
|
|
Packit Service |
2781ba |
#include <glib/gi18n-lib.h>
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#ifdef GEGL_CHANT_PROPERTIES
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
gegl_chant_color (color, _("Color"), "black",
|
|
Packit Service |
2781ba |
_("The color to render (defaults to 'black')"))
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#else
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#define GEGL_CHANT_TYPE_FILTER
|
|
Packit Service |
2781ba |
#define GEGL_CHANT_C_FILE "color-to-alpha.c"
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#include "gegl-chant.h"
|
|
Packit Service |
2781ba |
#include <stdio.h>
|
|
Packit Service |
2781ba |
#include <math.h>
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
static void prepare (GeglOperation *operation)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
gegl_operation_set_format (operation, "input",
|
|
Packit Service |
2781ba |
babl_format ("RGBA float"));
|
|
Packit Service |
2781ba |
gegl_operation_set_format (operation, "output",
|
|
Packit Service |
2781ba |
babl_format ("RGBA float"));
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
/*
|
|
Packit Service |
2781ba |
* An excerpt from a discussion on #gimp that sheds some light on the ideas
|
|
Packit Service |
2781ba |
* behind the algorithm that is being used here.
|
|
Packit Service |
2781ba |
*
|
|
Packit Service |
2781ba |
<clahey> so if a1 > c1, a2 > c2, and a3 > c2 and a1 - c1 > a2-c2, a3-c3,
|
|
Packit Service |
2781ba |
then a1 = b1 * alpha + c1 * (1-alpha)
|
|
Packit Service |
2781ba |
So, maximizing alpha without taking b1 above 1 gives
|
|
Packit Service |
2781ba |
a1 = alpha + c1(1-alpha) and therefore alpha = (a1-c1) / (1-c1).
|
|
Packit Service |
2781ba |
<sjburges> clahey: btw, the ordering of that a2, a3 in the white->alpha didn't
|
|
Packit Service |
2781ba |
matter
|
|
Packit Service |
2781ba |
<clahey> sjburges: You mean that it could be either a1, a2, a3 or
|
|
Packit Service |
2781ba |
a1, a3, a2?
|
|
Packit Service |
2781ba |
<sjburges> yeah
|
|
Packit Service |
2781ba |
<sjburges> because neither one uses the other
|
|
Packit Service |
2781ba |
<clahey> sjburges: That's exactly as it should be. They are both just
|
|
Packit Service |
2781ba |
getting reduced to the same amount, limited by the the darkest
|
|
Packit Service |
2781ba |
color.
|
|
Packit Service |
2781ba |
<clahey> Then a2 = b2 * alpha + c2 * (1- alpha). Solving for b2 gives
|
|
Packit Service |
2781ba |
b2 = (a1-c2)/alpha + c2.
|
|
Packit Service |
2781ba |
<sjburges> yeah
|
|
Packit Service |
2781ba |
<clahey> That gives us are formula for if the background is darker than the
|
|
Packit Service |
2781ba |
foreground? Yep.
|
|
Packit Service |
2781ba |
<clahey> Next if a1 < c1, a2 < c2, a3 < c3, and c1-a1 > c2-a2, c3-a3, and
|
|
Packit Service |
2781ba |
by our desired result a1 = b1 * alpha + c1 * (1-alpha),
|
|
Packit Service |
2781ba |
we maximize alpha without taking b1 negative gives
|
|
Packit Service |
2781ba |
alpha = 1 - a1 / c1.
|
|
Packit Service |
2781ba |
<clahey> And then again, b2 = (a2-c2) / alpha + c2 by the same formula.
|
|
Packit Service |
2781ba |
(Actually, I think we can use that formula for all cases, though
|
|
Packit Service |
2781ba |
it may possibly introduce rounding error.
|
|
Packit Service |
2781ba |
<clahey> sjburges: I like the idea of using floats to avoid rounding error.
|
|
Packit Service |
2781ba |
Good call.
|
|
Packit Service |
2781ba |
*/
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
static void
|
|
Packit Service |
2781ba |
color_to_alpha (gfloat *color,
|
|
Packit Service |
2781ba |
gfloat *src,
|
|
Packit Service |
2781ba |
gint offset)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
gint i;
|
|
Packit Service |
2781ba |
gfloat temp[4];
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
temp[3] = src[offset + 3];
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
for (i=0; i<3; i++)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
if (color[i] < 1.e-4f)
|
|
Packit Service |
2781ba |
temp[i] = src[offset+i];
|
|
Packit Service |
2781ba |
else if (src[offset+i] > color[i])
|
|
Packit Service |
2781ba |
temp[i] = (src[offset+i] - color[i]) / (1.0f - color[i]);
|
|
Packit Service |
2781ba |
else if (src[offset+i] < color[i])
|
|
Packit Service |
2781ba |
temp[i] = (color[i] - src[offset+i]) / color[i];
|
|
Packit Service |
2781ba |
else
|
|
Packit Service |
2781ba |
temp[i] = 0.0f;
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
if (temp[0] > temp[1])
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
if (temp[0] > temp[2])
|
|
Packit Service |
2781ba |
src[offset+3] = temp[0];
|
|
Packit Service |
2781ba |
else
|
|
Packit Service |
2781ba |
src[offset+3] = temp[2];
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
else if (temp[1] > temp[2])
|
|
Packit Service |
2781ba |
src[offset+3] = temp[1];
|
|
Packit Service |
2781ba |
else
|
|
Packit Service |
2781ba |
src[offset+3] = temp[2];
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
if (src[offset+3] < 1.e-4f)
|
|
Packit Service |
2781ba |
return;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
for (i=0; i<3; i++)
|
|
Packit Service |
2781ba |
src[offset+i] = (src[offset+i] - color[i]) / src[offset+3] + color[i];
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
src[offset+3] *= temp[3];
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
static gboolean
|
|
Packit Service |
2781ba |
process (GeglOperation *operation,
|
|
Packit Service |
2781ba |
GeglBuffer *input,
|
|
Packit Service |
2781ba |
GeglBuffer *output,
|
|
Packit Service |
2781ba |
const GeglRectangle *result,
|
|
Packit Service |
2781ba |
gint level)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
|
|
Packit Service |
2781ba |
const Babl *format = babl_format ("RGBA float");
|
|
Packit Service |
2781ba |
gfloat *src_buf, color[4];
|
|
Packit Service |
2781ba |
gint x;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
src_buf = g_new0 (gfloat, result->width * result->height * 4);
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
gegl_buffer_get (input, result, 1.0, format, src_buf,
|
|
Packit Service |
2781ba |
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
gegl_color_get_pixel (o->color, babl_format ("RGBA float"), color);
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
for (x = 0; x < result->width * result->height; x++)
|
|
Packit Service |
2781ba |
color_to_alpha (color, src_buf, 4 * x);
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
gegl_buffer_set (output, result, 0, format, src_buf, GEGL_AUTO_ROWSTRIDE);
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
g_free (src_buf);
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
return TRUE;
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
static void
|
|
Packit Service |
2781ba |
gegl_chant_class_init (GeglChantClass *klass)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
GeglOperationClass *operation_class;
|
|
Packit Service |
2781ba |
GeglOperationFilterClass *filter_class;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
operation_class = GEGL_OPERATION_CLASS (klass);
|
|
Packit Service |
2781ba |
filter_class = GEGL_OPERATION_FILTER_CLASS (klass);
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
filter_class->process = process;
|
|
Packit Service |
2781ba |
operation_class->prepare = prepare;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
gegl_operation_class_set_keys (operation_class,
|
|
Packit Service |
2781ba |
"name" , "gegl:color-to-alpha",
|
|
Packit Service |
2781ba |
"categories" , "color",
|
|
Packit Service |
2781ba |
"description", _("Performs color-to-alpha on the image."),
|
|
Packit Service |
2781ba |
NULL);
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#endif
|