typo fixes
[mplayer/greg.git] / libmpcodecs / vf_rectangle.c
blob361734369c9b1163bf372e95f0859942516f45a5
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "mp_image.h"
5 #include "mp_msg.h"
6 #include "vf.h"
8 #include "libvo/fastmemcpy.h"
10 struct vf_priv_s {
11 int x, y, w, h;
14 static int
15 config(struct vf_instance_s* vf,
16 int width, int height, int d_width, int d_height,
17 unsigned int flags, unsigned int outfmt)
19 if (vf->priv->w < 0 || width < vf->priv->w)
20 vf->priv->w = width;
21 if (vf->priv->h < 0 || height < vf->priv->h)
22 vf->priv->h = height;
23 if (vf->priv->x < 0)
24 vf->priv->x = (width - vf->priv->w) / 2;
25 if (vf->priv->y < 0)
26 vf->priv->y = (height - vf->priv->h) / 2;
27 if (vf->priv->w + vf->priv->x > width
28 || vf->priv->h + vf->priv->y > height) {
29 mp_msg(MSGT_VFILTER,MSGL_WARN,"rectangle: bad position/width/height - rectangle area is out of the original!\n");
30 return 0;
32 return vf_next_config(vf, width, height, d_width, d_height, flags, outfmt);
35 static int
36 control(struct vf_instance_s* vf, int request, void *data)
38 const int *const tmp = data;
39 switch(request){
40 case VFCTRL_CHANGE_RECTANGLE:
41 switch (tmp[0]){
42 case 0:
43 vf->priv->w += tmp[1];
44 return 1;
45 break;
46 case 1:
47 vf->priv->h += tmp[1];
48 return 1;
49 break;
50 case 2:
51 vf->priv->x += tmp[1];
52 return 1;
53 break;
54 case 3:
55 vf->priv->y += tmp[1];
56 return 1;
57 break;
58 default:
59 mp_msg(MSGT_VFILTER,MSGL_FATAL,"Unknown param %d \n", tmp[0]);
60 return 0;
63 return vf_next_control(vf, request, data);
64 return 0;
66 static int
67 put_image(struct vf_instance_s* vf, mp_image_t* mpi, double pts){
68 mp_image_t* dmpi;
69 unsigned int bpp = mpi->bpp / 8;
70 unsigned int x, y, w, h;
71 dmpi = vf_get_image(vf->next, mpi->imgfmt, MP_IMGTYPE_TEMP,
72 MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
73 mpi->w, mpi->h);
75 memcpy_pic(dmpi->planes[0],mpi->planes[0],mpi->w*bpp, mpi->h,
76 dmpi->stride[0],mpi->stride[0]);
77 if(mpi->flags&MP_IMGFLAG_PLANAR && mpi->flags&MP_IMGFLAG_YUV){
78 memcpy_pic(dmpi->planes[1],mpi->planes[1],
79 mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift,
80 dmpi->stride[1],mpi->stride[1]);
81 memcpy_pic(dmpi->planes[2],mpi->planes[2],
82 mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift,
83 dmpi->stride[2],mpi->stride[2]);
86 /* Draw the rectangle */
88 mp_msg(MSGT_VFILTER,MSGL_INFO, "rectangle: -vf rectangle=%d:%d:%d:%d \n", vf->priv->w, vf->priv->h, vf->priv->x, vf->priv->y);
90 if (vf->priv->x < 0)
91 x = 0;
92 else if (dmpi->width < vf->priv->x)
93 x = dmpi->width;
94 else
95 x = vf->priv->x;
96 if (vf->priv->x + vf->priv->w - 1 < 0)
97 w = vf->priv->x + vf->priv->w - 1 - x;
98 else if (dmpi->width < vf->priv->x + vf->priv->w - 1)
99 w = dmpi->width - x;
100 else
101 w = vf->priv->x + vf->priv->w - 1 - x;
102 if (vf->priv->y < 0)
103 y = 0;
104 else if (dmpi->height < vf->priv->y)
105 y = dmpi->height;
106 else
107 y = vf->priv->y;
108 if (vf->priv->y + vf->priv->h - 1 < 0)
109 h = vf->priv->y + vf->priv->h - 1 - y;
110 else if (dmpi->height < vf->priv->y + vf->priv->h - 1)
111 h = dmpi->height - y;
112 else
113 h = vf->priv->y + vf->priv->h - 1 - y;
115 if (0 <= vf->priv->y && vf->priv->y <= dmpi->height) {
116 unsigned char *p = dmpi->planes[0] + y * dmpi->stride[0] + x * bpp;
117 unsigned int count = w * bpp;
118 while (count--)
119 p[count] = 0xff - p[count];
121 if (h != 1 && vf->priv->y + vf->priv->h - 1 <= mpi->height) {
122 unsigned char *p = dmpi->planes[0] + (vf->priv->y + vf->priv->h - 1) * dmpi->stride[0] + x * bpp;
123 unsigned int count = w * bpp;
124 while (count--)
125 p[count] = 0xff - p[count];
127 if (0 <= vf->priv->x && vf->priv->x <= dmpi->width) {
128 unsigned char *p = dmpi->planes[0] + y * dmpi->stride[0] + x * bpp;
129 unsigned int count = h;
130 while (count--) {
131 unsigned int i = bpp;
132 while (i--)
133 p[i] = 0xff - p[i];
134 p += dmpi->stride[0];
137 if (w != 1 && vf->priv->x + vf->priv->w - 1 <= mpi->width) {
138 unsigned char *p = dmpi->planes[0] + y * dmpi->stride[0] + (vf->priv->x + vf->priv->w - 1) * bpp;
139 unsigned int count = h;
140 while (count--) {
141 unsigned int i = bpp;
142 while (i--)
143 p[i] = 0xff - p[i];
144 p += dmpi->stride[0];
147 return vf_next_put_image(vf, dmpi, pts);
150 static int
151 open(vf_instance_t* vf, char* args) {
152 vf->config = config;
153 vf->control = control;
154 vf->put_image = put_image;
155 vf->priv = malloc(sizeof(struct vf_priv_s));
156 vf->priv->x = -1;
157 vf->priv->y = -1;
158 vf->priv->w = -1;
159 vf->priv->h = -1;
160 if (args)
161 sscanf(args, "%d:%d:%d:%d",
162 &vf->priv->w, &vf->priv->h, &vf->priv->x, &vf->priv->y);
163 return 1;
166 vf_info_t vf_info_rectangle = {
167 "draw rectangle",
168 "rectangle",
169 "Kim Minh Kaplan",
171 open,
172 NULL