2003-12-05 23:55:15 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
2001-01-23 08:53:12 +08:00
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This library is free software: you can redistribute it and/or
|
2001-01-23 08:53:12 +08:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2009-01-18 06:28:01 +08:00
|
|
|
* version 3 of the License, or (at your option) any later version.
|
2003-12-05 23:55:15 +08:00
|
|
|
*
|
|
|
|
* This library 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
|
2001-01-23 08:53:12 +08:00
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-01-18 06:28:01 +08:00
|
|
|
* License along with this library. If not, see
|
2018-07-12 05:27:07 +08:00
|
|
|
* <https://www.gnu.org/licenses/>.
|
2001-01-23 08:53:12 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2005-01-22 08:43:31 +08:00
|
|
|
#include <glib-object.h>
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2001-01-24 07:56:18 +08:00
|
|
|
#include "libgimpmath/gimpmath.h"
|
2001-01-24 02:49:44 +08:00
|
|
|
|
2001-05-10 06:34:59 +08:00
|
|
|
#include "gimpcolortypes.h"
|
|
|
|
|
2001-01-23 08:53:12 +08:00
|
|
|
#include "gimpbilinear.h"
|
|
|
|
|
|
|
|
|
2010-06-30 01:46:37 +08:00
|
|
|
/**
|
2010-06-30 02:08:39 +08:00
|
|
|
* SECTION: gimpbilinear
|
2010-06-30 01:46:37 +08:00
|
|
|
* @title: GimpBilinear
|
|
|
|
* @short_description: Utility functions for bilinear interpolation.
|
|
|
|
*
|
|
|
|
* Utility functions for bilinear interpolation.
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
2020-05-06 01:37:18 +08:00
|
|
|
/**
|
|
|
|
* gimp_bilinear:
|
|
|
|
* @x:
|
|
|
|
* @y:
|
|
|
|
* @values: (array fixed-size=4):
|
|
|
|
*/
|
2001-01-23 08:53:12 +08:00
|
|
|
gdouble
|
|
|
|
gimp_bilinear (gdouble x,
|
2006-04-12 18:53:28 +08:00
|
|
|
gdouble y,
|
|
|
|
gdouble *values)
|
2001-01-23 08:53:12 +08:00
|
|
|
{
|
|
|
|
gdouble m0, m1;
|
|
|
|
|
2003-03-03 00:27:01 +08:00
|
|
|
g_return_val_if_fail (values != NULL, 0.0);
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2002-11-21 05:29:16 +08:00
|
|
|
x = fmod (x, 1.0);
|
|
|
|
y = fmod (y, 1.0);
|
2001-01-23 08:53:12 +08:00
|
|
|
|
|
|
|
if (x < 0.0)
|
|
|
|
x += 1.0;
|
|
|
|
if (y < 0.0)
|
|
|
|
y += 1.0;
|
|
|
|
|
2002-11-21 05:29:16 +08:00
|
|
|
m0 = (1.0 - x) * values[0] + x * values[1];
|
|
|
|
m1 = (1.0 - x) * values[2] + x * values[3];
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2002-11-21 05:29:16 +08:00
|
|
|
return (1.0 - y) * m0 + y * m1;
|
2001-01-23 08:53:12 +08:00
|
|
|
}
|
|
|
|
|
2020-05-06 01:37:18 +08:00
|
|
|
/**
|
|
|
|
* gimp_bilinear_8:
|
|
|
|
* @x:
|
|
|
|
* @y:
|
|
|
|
* @values: (array fixed-size=4):
|
|
|
|
*/
|
2001-01-23 08:53:12 +08:00
|
|
|
guchar
|
|
|
|
gimp_bilinear_8 (gdouble x,
|
2006-04-12 18:53:28 +08:00
|
|
|
gdouble y,
|
|
|
|
guchar *values)
|
2001-01-23 08:53:12 +08:00
|
|
|
{
|
|
|
|
gdouble m0, m1;
|
|
|
|
|
2003-03-03 00:27:01 +08:00
|
|
|
g_return_val_if_fail (values != NULL, 0);
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2002-11-21 05:29:16 +08:00
|
|
|
x = fmod (x, 1.0);
|
|
|
|
y = fmod (y, 1.0);
|
2001-01-23 08:53:12 +08:00
|
|
|
|
|
|
|
if (x < 0.0)
|
|
|
|
x += 1.0;
|
|
|
|
if (y < 0.0)
|
|
|
|
y += 1.0;
|
|
|
|
|
2002-11-21 05:29:16 +08:00
|
|
|
m0 = (1.0 - x) * values[0] + x * values[1];
|
|
|
|
m1 = (1.0 - x) * values[2] + x * values[3];
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2002-11-21 05:29:16 +08:00
|
|
|
return (guchar) ((1.0 - y) * m0 + y * m1);
|
2001-01-23 08:53:12 +08:00
|
|
|
}
|
|
|
|
|
2020-05-06 01:37:18 +08:00
|
|
|
/**
|
|
|
|
* gimp_bilinear_16:
|
|
|
|
* @x:
|
|
|
|
* @y:
|
|
|
|
* @values: (array fixed-size=4):
|
|
|
|
*/
|
2001-01-23 08:53:12 +08:00
|
|
|
guint16
|
|
|
|
gimp_bilinear_16 (gdouble x,
|
2006-04-12 18:53:28 +08:00
|
|
|
gdouble y,
|
|
|
|
guint16 *values)
|
2001-01-23 08:53:12 +08:00
|
|
|
{
|
|
|
|
gdouble m0, m1;
|
|
|
|
|
2003-03-03 00:27:01 +08:00
|
|
|
g_return_val_if_fail (values != NULL, 0);
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2002-11-21 05:29:16 +08:00
|
|
|
x = fmod (x, 1.0);
|
|
|
|
y = fmod (y, 1.0);
|
2001-01-23 08:53:12 +08:00
|
|
|
|
|
|
|
if (x < 0.0)
|
|
|
|
x += 1.0;
|
|
|
|
if (y < 0.0)
|
|
|
|
y += 1.0;
|
|
|
|
|
2002-11-21 05:29:16 +08:00
|
|
|
m0 = (1.0 - x) * values[0] + x * values[1];
|
|
|
|
m1 = (1.0 - x) * values[2] + x * values[3];
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2002-11-21 05:29:16 +08:00
|
|
|
return (guint16) ((1.0 - y) * m0 + y * m1);
|
2001-01-23 08:53:12 +08:00
|
|
|
}
|
|
|
|
|
2020-05-06 01:37:18 +08:00
|
|
|
/**
|
|
|
|
* gimp_bilinear_32:
|
|
|
|
* @x:
|
|
|
|
* @y:
|
|
|
|
* @values: (array fixed-size=4):
|
|
|
|
*/
|
2001-01-23 08:53:12 +08:00
|
|
|
guint32
|
|
|
|
gimp_bilinear_32 (gdouble x,
|
2006-04-12 18:53:28 +08:00
|
|
|
gdouble y,
|
|
|
|
guint32 *values)
|
2001-01-23 08:53:12 +08:00
|
|
|
{
|
|
|
|
gdouble m0, m1;
|
|
|
|
|
2003-03-03 00:27:01 +08:00
|
|
|
g_return_val_if_fail (values != NULL, 0);
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2002-11-21 05:29:16 +08:00
|
|
|
x = fmod (x, 1.0);
|
|
|
|
y = fmod (y, 1.0);
|
2001-01-23 08:53:12 +08:00
|
|
|
|
|
|
|
if (x < 0.0)
|
|
|
|
x += 1.0;
|
|
|
|
if (y < 0.0)
|
|
|
|
y += 1.0;
|
|
|
|
|
2002-11-21 05:29:16 +08:00
|
|
|
m0 = (1.0 - x) * values[0] + x * values[1];
|
|
|
|
m1 = (1.0 - x) * values[2] + x * values[3];
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2002-11-21 05:29:16 +08:00
|
|
|
return (guint32) ((1.0 - y) * m0 + y * m1);
|
2001-01-23 08:53:12 +08:00
|
|
|
}
|
|
|
|
|
2020-05-06 01:37:18 +08:00
|
|
|
/**
|
|
|
|
* gimp_bilinear_rgb:
|
|
|
|
* @x:
|
|
|
|
* @y:
|
|
|
|
* @values: (array fixed-size=4):
|
|
|
|
*/
|
2001-01-23 08:53:12 +08:00
|
|
|
GimpRGB
|
|
|
|
gimp_bilinear_rgb (gdouble x,
|
2006-04-12 18:53:28 +08:00
|
|
|
gdouble y,
|
|
|
|
GimpRGB *values)
|
2001-01-23 08:53:12 +08:00
|
|
|
{
|
|
|
|
gdouble m0, m1;
|
|
|
|
gdouble ix, iy;
|
2005-07-09 02:18:00 +08:00
|
|
|
GimpRGB v = { 0, };
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2003-03-03 00:27:01 +08:00
|
|
|
g_return_val_if_fail (values != NULL, v);
|
2001-01-23 08:53:12 +08:00
|
|
|
|
|
|
|
x = fmod(x, 1.0);
|
|
|
|
y = fmod(y, 1.0);
|
|
|
|
|
|
|
|
if (x < 0)
|
|
|
|
x += 1.0;
|
|
|
|
if (y < 0)
|
|
|
|
y += 1.0;
|
|
|
|
|
|
|
|
ix = 1.0 - x;
|
|
|
|
iy = 1.0 - y;
|
|
|
|
|
|
|
|
/* Red */
|
|
|
|
|
|
|
|
m0 = ix * values[0].r + x * values[1].r;
|
|
|
|
m1 = ix * values[2].r + x * values[3].r;
|
|
|
|
|
|
|
|
v.r = iy * m0 + y * m1;
|
|
|
|
|
|
|
|
/* Green */
|
|
|
|
|
|
|
|
m0 = ix * values[0].g + x * values[1].g;
|
|
|
|
m1 = ix * values[2].g + x * values[3].g;
|
|
|
|
|
|
|
|
v.g = iy * m0 + y * m1;
|
|
|
|
|
|
|
|
/* Blue */
|
|
|
|
|
|
|
|
m0 = ix * values[0].b + x * values[1].b;
|
|
|
|
m1 = ix * values[2].b + x * values[3].b;
|
|
|
|
|
|
|
|
v.b = iy * m0 + y * m1;
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2020-05-06 01:37:18 +08:00
|
|
|
/**
|
|
|
|
* gimp_bilinear_rgba:
|
|
|
|
* @x:
|
|
|
|
* @y:
|
|
|
|
* @values: (array fixed-size=4):
|
|
|
|
*/
|
2001-01-23 08:53:12 +08:00
|
|
|
GimpRGB
|
|
|
|
gimp_bilinear_rgba (gdouble x,
|
2006-04-12 18:53:28 +08:00
|
|
|
gdouble y,
|
|
|
|
GimpRGB *values)
|
2001-01-23 08:53:12 +08:00
|
|
|
{
|
|
|
|
gdouble m0, m1;
|
|
|
|
gdouble ix, iy;
|
2003-02-19 01:54:02 +08:00
|
|
|
gdouble a0, a1, a2, a3, alpha;
|
2005-07-09 02:18:00 +08:00
|
|
|
GimpRGB v = { 0, };
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2003-03-03 00:27:01 +08:00
|
|
|
g_return_val_if_fail (values != NULL, v);
|
2001-01-23 08:53:12 +08:00
|
|
|
|
|
|
|
x = fmod (x, 1.0);
|
|
|
|
y = fmod (y, 1.0);
|
|
|
|
|
|
|
|
if (x < 0)
|
|
|
|
x += 1.0;
|
|
|
|
if (y < 0)
|
|
|
|
y += 1.0;
|
|
|
|
|
|
|
|
ix = 1.0 - x;
|
|
|
|
iy = 1.0 - y;
|
|
|
|
|
2003-02-19 01:54:02 +08:00
|
|
|
a0 = values[0].a;
|
|
|
|
a1 = values[1].a;
|
|
|
|
a2 = values[2].a;
|
|
|
|
a3 = values[3].a;
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2003-02-19 01:54:02 +08:00
|
|
|
/* Alpha */
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2003-02-19 01:54:02 +08:00
|
|
|
m0 = ix * a0 + x * a1;
|
|
|
|
m1 = ix * a2 + x * a3;
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2003-02-19 01:54:02 +08:00
|
|
|
alpha = v.a = iy * m0 + y * m1;
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2003-02-19 01:54:02 +08:00
|
|
|
if (alpha > 0)
|
|
|
|
{
|
|
|
|
/* Red */
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2003-02-19 01:54:02 +08:00
|
|
|
m0 = ix * a0 * values[0].r + x * a1 * values[1].r;
|
|
|
|
m1 = ix * a2 * values[2].r + x * a3 * values[3].r;
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2003-02-19 01:54:02 +08:00
|
|
|
v.r = (iy * m0 + y * m1)/alpha;
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2003-02-19 01:54:02 +08:00
|
|
|
/* Green */
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2003-02-19 01:54:02 +08:00
|
|
|
m0 = ix * a0 * values[0].g + x * a1 * values[1].g;
|
|
|
|
m1 = ix * a2 * values[2].g + x * a3 * values[3].g;
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2003-02-19 01:54:02 +08:00
|
|
|
v.g = (iy * m0 + y * m1)/alpha;
|
|
|
|
|
|
|
|
/* Blue */
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2003-02-19 01:54:02 +08:00
|
|
|
m0 = ix * a0 * values[0].b + x * a1 * values[1].b;
|
|
|
|
m1 = ix * a2 * values[2].b + x * a3 * values[3].b;
|
2001-01-23 08:53:12 +08:00
|
|
|
|
2003-02-19 01:54:02 +08:00
|
|
|
v.b = (iy * m0 + y * m1)/alpha;
|
|
|
|
}
|
2001-01-23 08:53:12 +08:00
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|