gst-indent
[platform/upstream/gst-plugins-good.git] / gst / smpte / paint.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
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.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <math.h>
25 #include <stdlib.h>
26 #include "paint.h"
27
28 void
29 gst_smpte_paint_vbox (guint32 * dest, gint stride,
30     gint x0, gint y0, gint c0, gint x1, gint y1, gint c1)
31 {
32   gint i, j;
33   gint width, height;
34
35   width = x1 - x0;
36   height = y1 - y0;
37
38   g_assert (width > 0);
39   g_assert (height > 0);
40
41   dest = dest + y0 * stride + x0;
42
43   for (i = 0; i < height; i++) {
44     for (j = 0; j < width; j++) {
45       dest[j] = (c1 * j + c0 * (width - j)) / width;
46     }
47     dest += stride;
48   }
49 }
50
51 void
52 gst_smpte_paint_hbox (guint32 * dest, gint stride,
53     gint x0, gint y0, gint c0, gint x1, gint y1, gint c1)
54 {
55   gint i, j;
56   gint width, height;
57
58   width = x1 - x0;
59   height = y1 - y0;
60
61   g_assert (width > 0);
62   g_assert (height > 0);
63
64   g_print ("vbox: %d %d %d %d %d %d\n", x0, y0, c0, x1, y1, c1);
65
66   dest = dest + y0 * stride + x0;
67
68   for (i = 0; i < height; i++) {
69     guint32 value = (c1 * i + c0 * (height - i)) / height;
70
71     for (j = 0; j < width; j++) {
72       dest[j] = value;
73     }
74     dest += stride;
75   }
76 }
77
78 #define STEP_3D_LINE(dxabs,dyabs,dzabs,sdx,sdy,sdz,xr,yr,zr,px,py,pz)           \
79 G_STMT_START {                                          \
80   if (dxabs >= dyabs && dxabs >= dzabs) {               \
81     yr += dyabs;                                        \
82     zr += dzabs;                                        \
83     if (yr >= dxabs) {                                  \
84       py += sdy;                                        \
85       yr -= dxabs;                                      \
86     }                                                   \
87     if (zr >= dzabs) {                                  \
88       pz += sdz;                                        \
89       zr -= dxabs;                                      \
90     }                                                   \
91     px += sdx;                                          \
92   } else if (dyabs >= dxabs && dyabs >= dzabs) {        \
93     xr += dxabs;                                        \
94     zr += dzabs;                                        \
95     if (xr >= dyabs) {                                  \
96       px += sdx;                                        \
97       xr -= dyabs;                                      \
98     }                                                   \
99     if (zr >= dzabs) {                                  \
100       pz += sdz;                                        \
101       zr -= dyabs;                                      \
102     }                                                   \
103     py += sdy;                                          \
104   } else {                                              \
105     yr += dyabs;                                        \
106     xr += dxabs;                                        \
107     if (yr >= dyabs) {                                  \
108       py += sdy;                                        \
109       yr -= dzabs;                                      \
110     }                                                   \
111     if (xr >= dyabs) {                                  \
112       px += sdx;                                        \
113       xr -= dzabs;                                      \
114     }                                                   \
115     pz += sdz;                                          \
116   }                                                     \
117 } G_STMT_END
118
119 #define SWAP_INT(a,b)           \
120 G_STMT_START {                  \
121   gint tmp;                     \
122   tmp = (a);                    \
123   (a) = (b);                    \
124   (b) = (tmp);                  \
125 } G_STMT_END
126
127 #define SIGN(a) ((a) < 0 ? -1 : 1)
128
129 #define PREPARE_3D_LINE(x0,y0,z0,x1,y1,z1,dxabs,dyabs,dzabs,sdx,sdy,sdz,xr,yr,zr,px,py,pz)\
130 G_STMT_START {                  \
131   gint dx, dy, dz;              \
132   dx = x1 - x0;                 \
133   dy = y1 - y0;                 \
134   dz = z1 - z0;                 \
135   dxabs = abs (dx);             \
136   dyabs = abs (dy);             \
137   dzabs = abs (dz);             \
138   sdx = SIGN (dx);              \
139   sdy = SIGN (dy);              \
140   sdz = SIGN (dz);              \
141   xr = dxabs >> 1;              \
142   yr = dyabs >> 1;              \
143   zr = dzabs >> 1;              \
144   px = x0;                      \
145   py = y0;                      \
146   pz = z0;                      \
147 } G_STMT_END
148
149 void
150 gst_smpte_paint_triangle_linear (guint32 * dest, gint stride,
151     gint x0, gint y0, gint c0,
152     gint x1, gint y1, gint c1, gint x2, gint y2, gint c2)
153 {
154   gint sdxl, sdyl, sdcl, dxlabs, dylabs, dclabs, xrl, yrl, crl, pxl, pyl, pcl;
155   gint sdxr, sdyr, sdcr, dxrabs, dyrabs, dcrabs, xrr, yrr, crr, pxr, pyr, pcr;
156   gint i, j, k, seg_start, seg_end;
157
158   if (y0 > y1) {
159     SWAP_INT (x0, x1);
160     SWAP_INT (y0, y1);
161     SWAP_INT (c0, c1);
162   }
163   if (y0 > y2) {
164     SWAP_INT (x0, x2);
165     SWAP_INT (y0, y2);
166     SWAP_INT (c0, c2);
167   }
168   if (y1 > y2) {
169     SWAP_INT (x1, x2);
170     SWAP_INT (y1, y2);
171     SWAP_INT (c1, c2);
172   }
173
174   PREPARE_3D_LINE (x0, y0, c0, x2, y2, c2,
175       dxlabs, dylabs, dclabs, sdxl, sdyl, sdcl, xrl, yrl, crl, pxl, pyl, pcl);
176
177   PREPARE_3D_LINE (x0, y0, c0, x1, y1, c1,
178       dxrabs, dyrabs, dcrabs, sdxr, sdyr, sdcr, xrr, yrr, crr, pxr, pyr, pcr);
179
180   dest = dest + stride * y0;
181   seg_start = y0;
182   seg_end = y1;
183
184   /* do two passes */
185   for (k = 0; k < 2; k++) {
186     for (i = seg_start; i < seg_end; i++) {
187       gint s = pxl, e = pxr, sc = pcl, ec = pcr;
188       gint sign = SIGN (e - s);
189
190       e += sign;
191       for (j = s; j != e; j += sign) {
192         dest[j] = (ec * (j - s) + sc * (e - j)) / (e - s);
193       }
194
195       while (pyr == i) {
196         STEP_3D_LINE (dxrabs, dyrabs, dcrabs, sdxr, sdyr, sdcr,
197             xrr, yrr, crr, pxr, pyr, pcr);
198       }
199       while (pyl == i) {
200         STEP_3D_LINE (dxlabs, dylabs, dclabs, sdxl, sdyl, sdcl,
201             xrl, yrl, crl, pxl, pyl, pcl);
202       }
203       dest += stride;
204     }
205
206     PREPARE_3D_LINE (x1, y1, c1, x2, y2, c2,
207         dxrabs, dyrabs, dcrabs, sdxr, sdyr, sdcr, xrr, yrr, crr, pxr, pyr, pcr);
208
209     seg_start = y1;
210     seg_end = y2;
211   }
212 }
213
214 static void
215 draw_bresenham_line (guint32 * dest, gint stride,
216     gint x0, gint y0, gint x1, gint y1, guint32 col)
217 {
218   gint dx = abs (x1 - x0);
219   gint dy = abs (y1 - y0);
220   gint x_incr, y_incr;
221   gint i, dpr, dpru, P, indep;
222
223   dest = dest + y0 * stride + x0;
224
225   x_incr = SIGN (x1 - x0);
226   y_incr = SIGN (y1 - y0) * stride;
227
228   if (dx >= dy) {
229     dpr = dy << 1;
230     i = dx;
231     indep = x_incr;
232   } else {
233     dpr = dx << 1;
234     i = dy;
235     indep = y_incr;
236   }
237
238   dpru = dpr - (i << 1);
239   P = dpr - i;
240
241   for (; i >= 0; i--) {
242     *dest = col;
243
244     if (P > 0) {
245       dest += x_incr;
246       dest += y_incr;
247       P += dpru;
248     } else {
249       dest += indep;
250       P += dpr;
251     }
252   }
253 }
254
255 void
256 gst_smpte_paint_triangle_clock (guint32 * dest, gint stride,
257     gint x0, gint y0, gint c0,
258     gint x1, gint y1, gint c1, gint x2, gint y2, gint c2)
259 {
260   gint i;
261   gint sign;
262   gfloat angle, angle_s, angle_e;
263   gfloat len1;
264
265   angle_s = 0.0;
266   angle_e = acos (((x1 - x0) * (x2 - x0) + (y1 - y0) * (y2 - y0)) /
267       (sqrt ((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)) *
268           sqrt ((x2 - x0) * (x2 - x0) + (y2 - y0) * (y2 - y0))));
269
270   len1 = sqrt ((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
271
272   if (x1 == x2) {
273     sign = SIGN (y2 - y1);
274
275     for (i = y1; i != (y2 + sign); i += sign) {
276       if (y1 == i)
277         angle = 0;
278       else
279         angle = acos (((x1 - x0) * (x2 - x0) + (y1 - y0) * (i - y0)) /
280             (len1 * sqrt ((x1 - x0) * (x1 - x0) + (i - y0) * (i -
281                         y0)))) / angle_e;
282
283       draw_bresenham_line (dest, stride,
284           x0, y0, x1, i, (c2 * angle + c1 * (1.0 - angle)));
285     }
286   } else if (y1 == y2) {
287     sign = SIGN (x2 - x1);
288
289     for (i = x1; i != (x2 + sign); i += sign) {
290       if (x1 == i)
291         angle = 0;
292       else
293         angle = acos (((x1 - x0) * (i - x0) + (y1 - y0) * (y2 - y0)) /
294             (len1 * sqrt ((i - x0) * (i - x0) + (y2 - y0) * (y2 -
295                         y0)))) / angle_e;
296
297       draw_bresenham_line (dest, stride,
298           x0, y0, i, y1, (c2 * angle + c1 * (1.0 - angle)));
299     }
300   }
301 }
302
303 void
304 gst_smpte_paint_box_clock (guint32 * dest, gint stride,
305     gint x0, gint y0, gint c0,
306     gint x1, gint y1, gint c1, gint x2, gint y2, gint c2)
307 {
308   gfloat angle_m, col_m;
309   gint xv, yv;
310
311   if (x1 == x0) {
312     xv = x2;
313     yv = y1;
314   } else if (y1 == y0) {
315     xv = x1;
316     yv = y2;
317   } else {
318     g_warning ("paint box clock: not supported");
319     return;
320   }
321
322   angle_m = 2 * acos (((x1 - x0) * (xv - x0) + (y1 - y0) * (yv - y0)) /
323       (sqrt ((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)) *
324           sqrt ((xv - x0) * (xv - x0) + (yv - y0) * (yv - y0)))) / M_PI;
325
326   col_m = c2 * angle_m + c1 * (1.0 - angle_m);
327
328   gst_smpte_paint_triangle_clock (dest, stride,
329       x0, y0, c0, x1, y1, c1, xv, yv, col_m);
330   gst_smpte_paint_triangle_clock (dest, stride,
331       x0, y0, c0, xv, yv, col_m, x2, y2, c2);
332 }