dae3c6f199246332fdba10fe8381b7113fa4ce7a
[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, 
31                       gint x1, gint y1, gint c1)
32 {
33   gint i, j;
34   gint width, height;
35
36   width = x1 - x0;
37   height = y1 - y0;
38   
39   g_assert (width > 0);
40   g_assert (height > 0);
41
42   dest = dest + y0 * stride + x0;
43         
44   for (i = 0; i < height; i++) {
45     for (j = 0; j < width; j++) {
46       dest[j] = (c1 * j + c0 * (width - j)) / width;
47     }
48     dest += stride;
49   }
50 }
51
52 void
53 gst_smpte_paint_hbox (guint32 *dest, gint stride, 
54                       gint x0, gint y0, gint c0, 
55                       gint x1, gint y1, gint c1)
56 {
57   gint i, j;
58   gint width, height;
59
60   width = x1 - x0;
61   height = y1 - y0;
62   
63   g_assert (width > 0);
64   g_assert (height > 0);
65
66   g_print ("vbox: %d %d %d %d %d %d\n", x0, y0, c0, x1, y1, c1);
67
68   dest = dest + y0 * stride + x0;
69
70   for (i = 0; i < height; i++) {
71     guint32 value  = (c1 * i + c0 * (height - i)) / height;
72     for (j = 0; j < width; j++) {
73       dest[j] = value;
74     }
75     dest += stride;
76   }
77 }
78
79 #define STEP_3D_LINE(dxabs,dyabs,dzabs,sdx,sdy,sdz,xr,yr,zr,px,py,pz)           \
80 G_STMT_START {                                          \
81   if (dxabs >= dyabs && dxabs >= dzabs) {               \
82     yr += dyabs;                                        \
83     zr += dzabs;                                        \
84     if (yr >= dxabs) {                                  \
85       py += sdy;                                        \
86       yr -= dxabs;                                      \
87     }                                                   \
88     if (zr >= dzabs) {                                  \
89       pz += sdz;                                        \
90       zr -= dxabs;                                      \
91     }                                                   \
92     px += sdx;                                          \
93   } else if (dyabs >= dxabs && dyabs >= dzabs) {        \
94     xr += dxabs;                                        \
95     zr += dzabs;                                        \
96     if (xr >= dyabs) {                                  \
97       px += sdx;                                        \
98       xr -= dyabs;                                      \
99     }                                                   \
100     if (zr >= dzabs) {                                  \
101       pz += sdz;                                        \
102       zr -= dyabs;                                      \
103     }                                                   \
104     py += sdy;                                          \
105   } else {                                              \
106     yr += dyabs;                                        \
107     xr += dxabs;                                        \
108     if (yr >= dyabs) {                                  \
109       py += sdy;                                        \
110       yr -= dzabs;                                      \
111     }                                                   \
112     if (xr >= dyabs) {                                  \
113       px += sdx;                                        \
114       xr -= dzabs;                                      \
115     }                                                   \
116     pz += sdz;                                          \
117   }                                                     \
118 } G_STMT_END
119
120 #define SWAP_INT(a,b)           \
121 G_STMT_START {                  \
122   gint tmp;                     \
123   tmp = (a);                    \
124   (a) = (b);                    \
125   (b) = (tmp);                  \
126 } G_STMT_END
127
128 #define SIGN(a) ((a) < 0 ? -1 : 1)
129
130 #define PREPARE_3D_LINE(x0,y0,z0,x1,y1,z1,dxabs,dyabs,dzabs,sdx,sdy,sdz,xr,yr,zr,px,py,pz)\
131 G_STMT_START {                  \
132   gint dx, dy, dz;              \
133   dx = x1 - x0;                 \
134   dy = y1 - y0;                 \
135   dz = z1 - z0;                 \
136   dxabs = abs (dx);             \
137   dyabs = abs (dy);             \
138   dzabs = abs (dz);             \
139   sdx = SIGN (dx);              \
140   sdy = SIGN (dy);              \
141   sdz = SIGN (dz);              \
142   xr = dxabs >> 1;              \
143   yr = dyabs >> 1;              \
144   zr = dzabs >> 1;              \
145   px = x0;                      \
146   py = y0;                      \
147   pz = z0;                      \
148 } G_STMT_END
149
150 void
151 gst_smpte_paint_triangle_linear (guint32 *dest, gint stride,
152                                  gint x0, gint y0, gint c0,
153                                  gint x1, gint y1, gint c1, gint x2, gint y2, gint c2)
154 {
155   gint sdxl, sdyl, sdcl, dxlabs, dylabs, dclabs, xrl, yrl, crl, pxl, pyl, pcl;
156   gint sdxr, sdyr, sdcr, dxrabs, dyrabs, dcrabs, xrr, yrr, crr, pxr, pyr, pcr;
157   gint i, j, k, seg_start, seg_end;
158
159   if (y0 > y1) { SWAP_INT (x0, x1); SWAP_INT (y0, y1); SWAP_INT (c0, c1); }
160   if (y0 > y2) { SWAP_INT (x0, x2); SWAP_INT (y0, y2); SWAP_INT (c0, c2); }
161   if (y1 > y2) { SWAP_INT (x1, x2); SWAP_INT (y1, y2); SWAP_INT (c1, c2); }
162   
163   PREPARE_3D_LINE (x0,y0,c0,x2,y2,c2,
164                    dxlabs,dylabs,dclabs,
165                    sdxl, sdyl,sdcl,
166                    xrl,yrl,crl,
167                    pxl,pyl,pcl);
168
169   PREPARE_3D_LINE (x0,y0,c0,x1,y1,c1,
170                    dxrabs,dyrabs,dcrabs,
171                    sdxr, sdyr,sdcr,
172                    xrr,yrr,crr,
173                    pxr,pyr,pcr);
174
175   dest = dest + stride * y0;
176   seg_start = y0;
177   seg_end = y1;
178
179   /* do two passes */
180   for (k = 0; k < 2; k++) {
181     for (i = seg_start; i < seg_end; i++) {
182       gint s = pxl, e = pxr, sc = pcl, ec = pcr;
183       gint sign = SIGN (e - s);
184
185       e += sign;
186       for (j = s; j != e; j+=sign) {
187        dest[j] = (ec * (j - s) + sc * (e - j)) / (e - s);
188       }
189
190       while (pyr == i) {
191         STEP_3D_LINE (dxrabs, dyrabs, dcrabs, sdxr, sdyr, sdcr, 
192                       xrr, yrr, crr, pxr, pyr, pcr);
193       }
194       while (pyl == i) {
195         STEP_3D_LINE (dxlabs, dylabs, dclabs, sdxl, sdyl, sdcl, 
196                       xrl, yrl, crl, pxl, pyl, pcl);
197       }
198       dest += stride;
199     }
200
201     PREPARE_3D_LINE (x1,y1,c1,x2,y2,c2,
202                      dxrabs,dyrabs,dcrabs,
203                      sdxr, sdyr,sdcr,
204                      xrr,yrr,crr,
205                      pxr,pyr,pcr);
206
207     seg_start = y1;
208     seg_end = y2;
209   }
210 }
211
212 static void 
213 draw_bresenham_line (guint32 *dest, gint stride, 
214                      gint x0, gint y0, 
215                      gint x1, gint y1, 
216                      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   }
233   else {
234     dpr = dx << 1;
235     i = dy;
236     indep = y_incr;
237   }
238
239   dpru = dpr - (i << 1);
240   P = dpr - i;
241
242   for (; i >= 0; i--) {
243     *dest = col;
244
245     if (P > 0) { 
246       dest += x_incr;
247       dest += y_incr;
248       P += dpru;
249     } else {
250       dest += indep;
251       P += dpr;
252     }
253   }
254 }
255
256 void
257 gst_smpte_paint_triangle_clock (guint32 *dest, gint stride,
258                                 gint x0, gint y0, gint c0,
259                                 gint x1, gint y1, gint c1, 
260                                 gint x2, gint y2, gint c2)
261 {
262   gint i;
263   gint sign;
264   gfloat angle, angle_s, angle_e;
265   gfloat len1;
266
267   angle_s = 0.0;
268   angle_e = acos (((x1-x0) * (x2-x0) + (y1-y0) * (y2-y0))/
269                     (sqrt ((x1-x0) * (x1-x0) + (y1-y0) * (y1-y0)) * 
270                      sqrt ((x2-x0) * (x2-x0) + (y2-y0) * (y2-y0))));
271
272   len1 = sqrt ((x1-x0) * (x1-x0) + (y1-y0) * (y1-y0));
273
274   if (x1 == x2) {
275     sign = SIGN (y2 - y1);
276
277     for (i = y1; i != (y2 + sign); i += sign) {
278       if (y1 == i)
279         angle = 0;
280       else
281         angle = acos (((x1-x0) * (x2-x0) + (y1-y0) * (i-y0)) / 
282                         (len1 * sqrt ((x1-x0) * (x1-x0) + (i-y0) * (i-y0)))) / angle_e;
283
284       draw_bresenham_line (dest, stride,
285                     x0, y0, x1, i, 
286                     (c2 * angle + c1 * (1.0-angle)));
287     }
288   }
289   else if (y1 == y2) {
290     sign = SIGN (x2 - x1);
291
292     for (i = x1; i != (x2 + sign); i += sign) {
293       if (x1 == i)
294         angle = 0;
295       else
296         angle = acos (((x1-x0) * (i-x0) + (y1-y0) * (y2-y0)) / 
297                         (len1 * sqrt ((i-x0) * (i-x0) + (y2-y0) * (y2-y0)))) / angle_e;
298
299       draw_bresenham_line (dest, stride,
300                     x0, y0, i, y1, 
301                     (c2 * angle + c1 * (1.0-angle)));
302     }
303   }
304 }
305
306 void
307 gst_smpte_paint_box_clock (guint32 *dest, gint stride,
308                            gint x0, gint y0, gint c0,
309                            gint x1, gint y1, gint c1, 
310                            gint x2, gint y2, gint c2)
311 {
312   gfloat angle_m, col_m;
313   gint xv, yv;
314
315   if (x1 == x0) { 
316     xv = x2;
317     yv = y1;
318   } else if (y1 == y0) {
319     xv = x1;
320     yv = y2;
321   }
322   else {
323     g_warning ("paint box clock: not supported");
324     return;
325   }
326
327   angle_m = 2 * acos (((x1-x0) * (xv-x0) + (y1-y0) * (yv-y0))/
328                     (sqrt ((x1-x0) * (x1-x0) + (y1-y0) * (y1-y0)) * 
329                      sqrt ((xv-x0) * (xv-x0) + (yv-y0) * (yv-y0)))) / M_PI;
330     
331   col_m = c2 * angle_m + c1 * (1.0-angle_m);
332
333   gst_smpte_paint_triangle_clock (dest, stride,
334                                   x0, y0, c0,
335                                   x1, y1, c1,
336                                   xv, yv, col_m);
337   gst_smpte_paint_triangle_clock (dest, stride,
338                                   x0, y0, c0,
339                                   xv, yv, col_m,
340                                   x2, y2, c2);
341 }
342