app: s/sprintf/g_snprintf/ in xcf_save_image()
[gimp.git] / libgimpcolor / gimpbilinear.c
blob4cec47b058490bc1d660f5490853b6df0231e553
1 /* LIBGIMP - The GIMP Library
2 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
4 * This library is free software: you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <https://www.gnu.org/licenses/>.
19 #include "config.h"
21 #include <glib-object.h>
23 #include "libgimpmath/gimpmath.h"
25 #include "gimpcolortypes.h"
27 #include "gimpbilinear.h"
30 /**
31 * SECTION: gimpbilinear
32 * @title: GimpBilinear
33 * @short_description: Utility functions for bilinear interpolation.
35 * Utility functions for bilinear interpolation.
36 **/
39 gdouble
40 gimp_bilinear (gdouble x,
41 gdouble y,
42 gdouble *values)
44 gdouble m0, m1;
46 g_return_val_if_fail (values != NULL, 0.0);
48 x = fmod (x, 1.0);
49 y = fmod (y, 1.0);
51 if (x < 0.0)
52 x += 1.0;
53 if (y < 0.0)
54 y += 1.0;
56 m0 = (1.0 - x) * values[0] + x * values[1];
57 m1 = (1.0 - x) * values[2] + x * values[3];
59 return (1.0 - y) * m0 + y * m1;
62 guchar
63 gimp_bilinear_8 (gdouble x,
64 gdouble y,
65 guchar *values)
67 gdouble m0, m1;
69 g_return_val_if_fail (values != NULL, 0);
71 x = fmod (x, 1.0);
72 y = fmod (y, 1.0);
74 if (x < 0.0)
75 x += 1.0;
76 if (y < 0.0)
77 y += 1.0;
79 m0 = (1.0 - x) * values[0] + x * values[1];
80 m1 = (1.0 - x) * values[2] + x * values[3];
82 return (guchar) ((1.0 - y) * m0 + y * m1);
85 guint16
86 gimp_bilinear_16 (gdouble x,
87 gdouble y,
88 guint16 *values)
90 gdouble m0, m1;
92 g_return_val_if_fail (values != NULL, 0);
94 x = fmod (x, 1.0);
95 y = fmod (y, 1.0);
97 if (x < 0.0)
98 x += 1.0;
99 if (y < 0.0)
100 y += 1.0;
102 m0 = (1.0 - x) * values[0] + x * values[1];
103 m1 = (1.0 - x) * values[2] + x * values[3];
105 return (guint16) ((1.0 - y) * m0 + y * m1);
108 guint32
109 gimp_bilinear_32 (gdouble x,
110 gdouble y,
111 guint32 *values)
113 gdouble m0, m1;
115 g_return_val_if_fail (values != NULL, 0);
117 x = fmod (x, 1.0);
118 y = fmod (y, 1.0);
120 if (x < 0.0)
121 x += 1.0;
122 if (y < 0.0)
123 y += 1.0;
125 m0 = (1.0 - x) * values[0] + x * values[1];
126 m1 = (1.0 - x) * values[2] + x * values[3];
128 return (guint32) ((1.0 - y) * m0 + y * m1);
131 GimpRGB
132 gimp_bilinear_rgb (gdouble x,
133 gdouble y,
134 GimpRGB *values)
136 gdouble m0, m1;
137 gdouble ix, iy;
138 GimpRGB v = { 0, };
140 g_return_val_if_fail (values != NULL, v);
142 x = fmod(x, 1.0);
143 y = fmod(y, 1.0);
145 if (x < 0)
146 x += 1.0;
147 if (y < 0)
148 y += 1.0;
150 ix = 1.0 - x;
151 iy = 1.0 - y;
153 /* Red */
155 m0 = ix * values[0].r + x * values[1].r;
156 m1 = ix * values[2].r + x * values[3].r;
158 v.r = iy * m0 + y * m1;
160 /* Green */
162 m0 = ix * values[0].g + x * values[1].g;
163 m1 = ix * values[2].g + x * values[3].g;
165 v.g = iy * m0 + y * m1;
167 /* Blue */
169 m0 = ix * values[0].b + x * values[1].b;
170 m1 = ix * values[2].b + x * values[3].b;
172 v.b = iy * m0 + y * m1;
174 return v;
177 GimpRGB
178 gimp_bilinear_rgba (gdouble x,
179 gdouble y,
180 GimpRGB *values)
182 gdouble m0, m1;
183 gdouble ix, iy;
184 gdouble a0, a1, a2, a3, alpha;
185 GimpRGB v = { 0, };
187 g_return_val_if_fail (values != NULL, v);
189 x = fmod (x, 1.0);
190 y = fmod (y, 1.0);
192 if (x < 0)
193 x += 1.0;
194 if (y < 0)
195 y += 1.0;
197 ix = 1.0 - x;
198 iy = 1.0 - y;
200 a0 = values[0].a;
201 a1 = values[1].a;
202 a2 = values[2].a;
203 a3 = values[3].a;
205 /* Alpha */
207 m0 = ix * a0 + x * a1;
208 m1 = ix * a2 + x * a3;
210 alpha = v.a = iy * m0 + y * m1;
212 if (alpha > 0)
214 /* Red */
216 m0 = ix * a0 * values[0].r + x * a1 * values[1].r;
217 m1 = ix * a2 * values[2].r + x * a3 * values[3].r;
219 v.r = (iy * m0 + y * m1)/alpha;
221 /* Green */
223 m0 = ix * a0 * values[0].g + x * a1 * values[1].g;
224 m1 = ix * a2 * values[2].g + x * a3 * values[3].g;
226 v.g = (iy * m0 + y * m1)/alpha;
228 /* Blue */
230 m0 = ix * a0 * values[0].b + x * a1 * values[1].b;
231 m1 = ix * a2 * values[2].b + x * a3 * values[3].b;
233 v.b = (iy * m0 + y * m1)/alpha;
236 return v;