]> git.sesse.net Git - mlt/blob - src/modules/motion_est/arrow_code.c
Merge ../mlt++
[mlt] / src / modules / motion_est / arrow_code.c
1 /*
2  *      /brief Draw arrows
3  *      /author Zachary Drew, Copyright 2004
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <framework/mlt_frame.h>
21 #include "arrow_code.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <math.h>
26 #include <string.h>
27
28 #define MIN(a,b) ((a) > (b) ? (b) : (a))
29
30 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
31 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
32
33
34 static int w;
35 static int h;
36 static int xstride;
37 static int ystride;
38 static mlt_image_format format;
39
40 int init_arrows( mlt_image_format *image_format, int width, int height )
41 {
42         w = width;
43         h = height;
44         format = *image_format;
45         switch( *image_format ) {
46                 case mlt_image_yuv422:
47                         xstride = 2;
48                         ystride = xstride * w;
49                         break; 
50                 default:
51                         // I don't know
52                         return 0;
53         }
54         return 1;
55
56 }
57
58 // ffmpeg borrowed
59 static inline int clip(int a, int amin, int amax)
60 {
61     if (a < amin)
62         return amin;
63     else if (a > amax)
64         return amax;
65     else
66         return a;
67 }
68
69
70 /**
71  * draws an line from (ex, ey) -> (sx, sy).
72  * Credits: modified from ffmpeg project
73  * @param ystride stride/linesize of the image
74  * @param xstride stride/element size of the image
75  * @param color color of the arrow
76  */
77 void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int color)
78 {
79     int t, x, y, fr, f;
80
81
82     sx= clip(sx, 0, w-1);
83     sy= clip(sy, 0, h-1);
84     ex= clip(ex, 0, w-1);
85     ey= clip(ey, 0, h-1);
86
87     buf[sy*ystride + sx*xstride]+= color;
88
89     if(ABS(ex - sx) > ABS(ey - sy)){
90         if(sx > ex){
91             t=sx; sx=ex; ex=t;
92             t=sy; sy=ey; ey=t;
93         }
94         buf+= sx*xstride + sy*ystride;
95         ex-= sx;
96         f= ((ey-sy)<<16)/ex;
97         for(x= 0; x <= ex; x++){
98             y = (x*f)>>16;
99             fr= (x*f)&0xFFFF;
100             buf[ y   *ystride + x*xstride]+= (color*(0x10000-fr))>>16;
101             buf[(y+1)*ystride + x*xstride]+= (color*         fr )>>16;
102         }
103     }else{
104         if(sy > ey){
105             t=sx; sx=ex; ex=t;
106             t=sy; sy=ey; ey=t;
107         }
108         buf+= sx*xstride + sy*ystride;
109         ey-= sy;
110         if(ey) f= ((ex-sx)<<16)/ey;
111         else   f= 0;
112         for(y= 0; y <= ey; y++){
113             x = (y*f)>>16;
114             fr= (y*f)&0xFFFF;
115             buf[y*ystride + x    *xstride]+= (color*(0x10000-fr))>>16;;
116             buf[y*ystride + (x+1)*xstride]+= (color*         fr )>>16;;
117         }
118     }
119 }
120
121 void draw_rectangle_fill(uint8_t *buf, int x, int y, int w, int h, int color)
122 {
123         int i,j;
124         for ( i = 0; i < w; i++ ) 
125                 for ( j = 0; j < h; j++ )
126                         buf[ (y+j)*ystride + (x+i)*xstride] = color; 
127 }
128
129 void draw_rectangle_outline(uint8_t *buf, int x, int y, int w, int h, int color)
130 {
131         int i,j;
132         for ( i = 0; i < w; i++ ) {
133                 buf[ y*ystride + (x+i)*xstride ] += color; 
134                 buf[ (y+h)*ystride + (x+i)*xstride ] += color; 
135         }
136         for ( j = 1; j < h+1; j++ ) {
137                 buf[ (y+j)*ystride + x*xstride ] += color;
138                 buf[ (y+j)*ystride + (x+w)*xstride ] += color; 
139         }
140 }
141 /**
142  * draws an arrow from (ex, ey) -> (sx, sy).
143  * Credits: modified from ffmpeg project
144  * @param stride stride/linesize of the image
145  * @param color color of the arrow
146  */
147 void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int color){
148
149         int dx,dy;
150         dx= ex - sx;
151         dy= ey - sy;
152
153         if(dx*dx + dy*dy > 3*3){
154                 int rx=  dx + dy;
155                 int ry= -dx + dy;
156                 int length= sqrt((rx*rx + ry*ry)<<8);
157
158                 rx= ROUNDED_DIV(rx*3<<4, length);
159                 ry= ROUNDED_DIV(ry*3<<4, length);
160
161                 draw_line(buf, sx, sy, sx + rx, sy + ry, color);
162                 draw_line(buf, sx, sy, sx - ry, sy + rx, color);
163         }
164         draw_line(buf, sx, sy, ex, ey, color);
165 }