• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
3  * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Alternatively, the contents of this file may be used under the
24  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
25  * which case the following provisions apply instead of the ones
26  * mentioned above:
27  *
28  * This library is free software; you can redistribute it and/or
29  * modify it under the terms of the GNU Library General Public
30  * License as published by the Free Software Foundation; either
31  * version 2 of the License, or (at your option) any later version.
32  *
33  * This library is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
36  * Library General Public License for more details.
37  *
38  * You should have received a copy of the GNU Library General Public
39  * License along with this library; if not, write to the
40  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
41  * Boston, MA 02110-1301, USA.
42  */
43 
44 /*
45  * Thanks to Jerry Huxtable <http://www.jhlabs.com> work on its java
46  * image editor and filters. The algorithms here were extracted from
47  * his code.
48  */
49 
50 #include "geometricmath.h"
51 #include <math.h>
52 
53 #define N  0x1000
54 #define B  0x100
55 #define BM 0xff
56 
57 struct _GstGMNoise
58 {
59   gdouble p[2 * B + 2];
60   gdouble g2[2 * B + 2][2];
61 };
62 
63 static void
normalize_2(gdouble * v)64 normalize_2 (gdouble * v)
65 {
66   gdouble s = sqrt (v[0] * v[0] + v[1] * v[1]);
67 
68   v[0] = v[0] / s;
69   v[1] = v[1] / s;
70 }
71 
72 GstGMNoise *
gst_gm_noise_new(void)73 gst_gm_noise_new (void)
74 {
75   GstGMNoise *noise = g_new0 (GstGMNoise, 1);
76   gint i, j, k;
77 
78   for (i = 0; i < B; i++) {
79     noise->p[i] = i;
80     for (j = 0; j < 2; j++) {
81       noise->g2[i][j] = ((g_random_int () % (2 * B))
82           - B) / (gdouble) B;
83     }
84     normalize_2 (noise->g2[i]);
85   }
86 
87   for (i = B - 1; i >= 0; i--) {
88     k = noise->p[i];
89     j = g_random_int () % B;
90     noise->p[i] = noise->p[j];
91     noise->p[j] = k;
92   }
93 
94   for (i = 0; i < B + 2; i++) {
95     noise->p[B + i] = noise->p[i];
96     for (j = 0; j < 2; j++) {
97       noise->g2[B + i][j] = noise->g2[i][j];
98     }
99   }
100 
101   return noise;
102 }
103 
104 void
gst_gm_noise_free(GstGMNoise * noise)105 gst_gm_noise_free (GstGMNoise * noise)
106 {
107   g_free (noise);
108 }
109 
110 static gdouble
s_curve(gdouble x)111 s_curve (gdouble x)
112 {
113   return x * x * (3.0 - 2.0 * x);
114 }
115 
116 static gdouble
lerp(gdouble t,gdouble a,gdouble b)117 lerp (gdouble t, gdouble a, gdouble b)
118 {
119   return a + t * (b - a);
120 }
121 
122 gdouble
gst_gm_noise_2(GstGMNoise * noise,gdouble x,gdouble y)123 gst_gm_noise_2 (GstGMNoise * noise, gdouble x, gdouble y)
124 {
125   gint bx0, bx1, by0, by1, b00, b10, b01, b11;
126   gdouble rx0, rx1, ry0, ry1, sx, sy, a, b, t, u, v;
127   gdouble *q;
128   gint i, j;
129 
130   t = x + N;
131   bx0 = ((gint) t) & BM;
132   bx1 = (bx0 + 1) & BM;
133   rx0 = t - (gint) t;
134   rx1 = rx0 - 1.0;
135 
136   t = y + N;
137   by0 = ((gint) t) & BM;
138   by1 = (by0 + 1) & BM;
139   ry0 = t - (gint) t;
140   ry1 = ry0 - 1.0;
141 
142   i = noise->p[bx0];
143   j = noise->p[bx1];
144 
145   b00 = noise->p[i + by0];
146   b10 = noise->p[j + by0];
147   b01 = noise->p[i + by1];
148   b11 = noise->p[j + by1];
149 
150   sx = s_curve (rx0);
151   sy = s_curve (ry0);
152 
153   q = noise->g2[b00];
154   u = rx0 * q[0] + ry0 * q[1];
155   q = noise->g2[b10];
156   v = rx1 * q[0] + ry0 * q[1];
157   a = lerp (sx, u, v);
158 
159   q = noise->g2[b01];
160   u = rx0 * q[0] + ry1 * q[1];
161   q = noise->g2[b11];
162   v = rx1 * q[0] + ry1 * q[1];
163   b = lerp (sx, u, v);
164 
165   return 1.5 * lerp (sy, a, b);
166 }
167 
168 /*
169  * This differs from the % operator with respect to negative numbers
170  */
171 gdouble
gst_gm_mod_float(gdouble a,gdouble b)172 gst_gm_mod_float (gdouble a, gdouble b)
173 {
174   gint n = (gint) (a / b);
175 
176   a -= n * b;
177   if (a < 0)
178     return a + b;
179   return a;
180 }
181 
182 /*
183  * Returns a repeating triangle shape in the range 0..1 with wavelength 1.0
184  */
185 gdouble
gst_gm_triangle(gdouble x)186 gst_gm_triangle (gdouble x)
187 {
188   gdouble r = gst_gm_mod_float (x, 1.0);
189 
190   return 2.0 * (r < 0.5 ? r : 1 - r);
191 }
192 
193 /*
194  * Hermite interpolation
195  */
196 gdouble
gst_gm_smoothstep(gdouble edge0,gdouble edge1,gdouble x)197 gst_gm_smoothstep (gdouble edge0, gdouble edge1, gdouble x)
198 {
199   gdouble t = CLAMP ((x - edge0) / (edge1 - edge0), 0.0, 1.0);
200   return t * t * (3.0 - 2.0 * t);
201 }
202