f3d58f39c7682574fd40a92c44f613df8f7d379a
[platform/upstream/gst-plugins-good.git] / gst / videomixer / blend_bgra.c
1 /* 
2  * Copyright (C) 2009 Alex Ugarte <augarte@vicomtech.org>
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 #include <gst/gst.h>
21
22 #define BLEND_NORMAL(B1,G1,R1,B2,G2,R2,B,G,R,alpha)     \
23         B = ((B1*(255-alpha))+(B2*alpha))>>8;           \
24         G = ((G1*(255-alpha))+(G2*alpha))>>8;           \
25         R = ((R1*(255-alpha))+(R2*alpha))>>8;
26
27 #define BLEND_MODE BLEND_NORMAL
28
29 void
30 gst_videomixer_blend_bgra_bgra (guint8 * src, gint xpos, gint ypos,
31     gint src_width, gint src_height, gdouble src_alpha,
32     guint8 * dest, gint dest_width, gint dest_height)
33 {
34   gint alpha, b_alpha;
35   gint i, j;
36   gint src_stride, dest_stride;
37   gint src_add, dest_add;
38   gint B, G, R;
39
40   src_stride = src_width * 4;
41   dest_stride = dest_width * 4;
42
43   b_alpha = (gint) (src_alpha * 255);
44
45   /* adjust src pointers for negative sizes */
46   if (xpos < 0) {
47     src += -xpos * 4;
48     src_width -= -xpos;
49     xpos = 0;
50   }
51   if (ypos < 0) {
52     src += -ypos * src_stride;
53     src_height -= -ypos;
54     ypos = 0;
55   }
56   /* adjust width/height if the src is bigger than dest */
57   if (xpos + src_width > dest_width) {
58     src_width = dest_width - xpos;
59   }
60   if (ypos + src_height > dest_height) {
61     src_height = dest_height - ypos;
62   }
63
64   src_add = src_stride - (4 * src_width);
65   dest_add = dest_stride - (4 * src_width);
66
67   dest = dest + 4 * xpos + (ypos * dest_stride);
68
69   /* we convert a square of 2x2 samples to generate 4 Luma and 2 chroma samples */
70   for (i = 0; i < src_height; i++) {
71     for (j = 0; j < src_width; j++) {
72       alpha = (src[3] * b_alpha) >> 8;
73       BLEND_MODE (dest[1], dest[2], dest[3], src[1], src[2], src[3],
74           B, G, R, alpha);
75       dest[0] = B;
76       dest[1] = G;
77       dest[2] = R;
78       dest[3] = 0xff;
79
80       src += 4;
81       dest += 4;
82     }
83     src += src_add;
84     dest += dest_add;
85   }
86 }
87
88 #undef BLEND_MODE
89
90 /* fill a buffer with a checkerboard pattern */
91 void
92 gst_videomixer_fill_bgra_checker (guint8 * dest, gint width, gint height)
93 {
94   gint i, j;
95   static int tab[] = { 80, 160, 80, 160 };
96
97   for (i = 0; i < height; i++) {
98     for (j = 0; j < width; j++) {
99       *dest++ = tab[((i & 0x8) >> 3) + ((j & 0x8) >> 3)];       //blue
100       *dest++ = tab[((i & 0x8) >> 3) + ((j & 0x8) >> 3)];       //green
101       *dest++ = tab[((i & 0x8) >> 3) + ((j & 0x8) >> 3)];       //red    
102       *dest++ = 0xFF;           //alpha
103     }
104   }
105 }
106
107 void
108 gst_videomixer_fill_bgra_color (guint8 * dest, gint width, gint height,
109     gint colY, gint colU, gint colV)
110 {
111   gint red, green, blue;
112   gint i, j;
113
114 //check this conversion 
115   red = 1.164 * (colY - 16) + 1.596 * (colV - 128);
116   green = 1.164 * (colY - 16) - 0.813 * (colV - 128) - 0.391 * (colU - 128);
117   blue = 1.164 * (colY - 16) + 2.018 * (colU - 128);
118
119
120   for (i = 0; i < height; i++) {
121     for (j = 0; j < width; j++) {
122       *dest++ = 0xff;
123       *dest++ = colY;
124       *dest++ = colU;
125       *dest++ = colV;
126     }
127   }
128 }