1e4fb51a2446fc7881e3b7a097f36ea963aedf9b
[platform/upstream/gst-plugins-good.git] / gst / videofilter / gstvideobalance.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2003> David Schleef <ds@schleef.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * This file was (probably) generated from gstvideobalance.c,
23  * gstvideobalance.c,v 1.7 2003/11/08 02:48:59 dschleef Exp 
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 /*#define DEBUG_ENABLED */
31 #include <gstvideobalance.h>
32 #include <string.h>
33 #include <math.h>
34
35 /* GstVideobalance signals and args */
36 enum {
37   /* FILL ME */
38   LAST_SIGNAL
39 };
40
41 enum {
42   ARG_0,
43   ARG_CONTRAST,
44   ARG_BRIGHTNESS,
45   ARG_HUE,
46   ARG_SATURATION,
47   /* FILL ME */
48 };
49
50 static void     gst_videobalance_base_init      (gpointer g_class);
51 static void     gst_videobalance_class_init     (gpointer g_class, gpointer class_data);
52 static void     gst_videobalance_init           (GTypeInstance *instance, gpointer g_class);
53
54 static void     gst_videobalance_set_property           (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
55 static void     gst_videobalance_get_property           (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
56
57 static void gst_videobalance_planar411(GstVideofilter *videofilter, void *dest, void *src);
58 static void gst_videobalance_setup(GstVideofilter *videofilter);
59
60 GType
61 gst_videobalance_get_type (void)
62 {
63   static GType videobalance_type = 0;
64
65   if (!videobalance_type) {
66     static const GTypeInfo videobalance_info = {
67       sizeof(GstVideobalanceClass),
68       gst_videobalance_base_init,
69       NULL,
70       gst_videobalance_class_init,
71       NULL,
72       NULL,
73       sizeof(GstVideobalance),
74       0,
75       gst_videobalance_init,
76     };
77     videobalance_type = g_type_register_static(GST_TYPE_VIDEOFILTER,
78         "GstVideobalance", &videobalance_info, 0);
79   }
80   return videobalance_type;
81 }
82
83 static GstVideofilterFormat gst_videobalance_formats[] = {
84   { "I420", 12, gst_videobalance_planar411, },
85 };
86
87   
88 static void
89 gst_videobalance_base_init (gpointer g_class)
90 {
91   static GstElementDetails videobalance_details = GST_ELEMENT_DETAILS (
92     "Video Balance Control",
93     "Filter/Effect/Video",
94     "Adjusts brightness, contrast, hue, saturation on a video stream",
95     "David Schleef <ds@schleef.org>"
96   );
97   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
98   GstVideofilterClass *videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
99   int i;
100   
101   gst_element_class_set_details (element_class, &videobalance_details);
102
103   for(i=0;i<G_N_ELEMENTS(gst_videobalance_formats);i++){
104     gst_videofilter_class_add_format(videofilter_class,
105         gst_videobalance_formats + i);
106   }
107
108   gst_videofilter_class_add_pad_templates (GST_VIDEOFILTER_CLASS (g_class));
109 }
110
111 static void
112 gst_videobalance_class_init (gpointer g_class, gpointer class_data)
113 {
114   GObjectClass *gobject_class;
115   GstVideofilterClass *videofilter_class;
116
117   gobject_class = G_OBJECT_CLASS (g_class);
118   videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
119
120   g_object_class_install_property(gobject_class, ARG_CONTRAST,
121       g_param_spec_double("contrast","Contrast","contrast",
122       0, 2, 1, G_PARAM_READWRITE));
123   g_object_class_install_property(gobject_class, ARG_BRIGHTNESS,
124       g_param_spec_double("brightness","Brightness","brightness",
125       -1, 1, 0, G_PARAM_READWRITE));
126   g_object_class_install_property(gobject_class, ARG_HUE,
127       g_param_spec_double("hue","Hue","hue",
128       -1, 1, 0, G_PARAM_READWRITE));
129   g_object_class_install_property(gobject_class, ARG_SATURATION,
130       g_param_spec_double("saturation","Saturation","saturation",
131       0, 2, 1, G_PARAM_READWRITE));
132
133   gobject_class->set_property = gst_videobalance_set_property;
134   gobject_class->get_property = gst_videobalance_get_property;
135
136   videofilter_class->setup = gst_videobalance_setup;
137 }
138
139 static void
140 gst_videobalance_init (GTypeInstance *instance, gpointer g_class)
141 {
142   GstVideobalance *videobalance = GST_VIDEOBALANCE (instance);
143   GstVideofilter *videofilter;
144
145   GST_DEBUG("gst_videobalance_init");
146
147   videofilter = GST_VIDEOFILTER(videobalance);
148
149   /* do stuff */
150   videobalance->contrast = 1;
151   videobalance->brightness = 0;
152   videobalance->saturation = 1;
153   videobalance->hue = 0;
154
155 }
156
157 static void
158 gst_videobalance_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
159 {
160   GstVideobalance *src;
161
162   /* it's not null if we got it, but it might not be ours */
163   g_return_if_fail(GST_IS_VIDEOBALANCE(object));
164   src = GST_VIDEOBALANCE(object);
165
166   GST_DEBUG("gst_videobalance_set_property");
167   switch (prop_id) {
168     case ARG_CONTRAST:
169       src->contrast = g_value_get_double (value);
170       break;
171     case ARG_BRIGHTNESS:
172       src->brightness = g_value_get_double (value);
173       break;
174     case ARG_HUE:
175       src->hue = g_value_get_double (value);
176       break;
177     case ARG_SATURATION:
178       src->saturation = g_value_get_double (value);
179       break;
180     default:
181       break;
182   }
183 }
184
185 static void
186 gst_videobalance_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
187 {
188   GstVideobalance *src;
189
190   /* it's not null if we got it, but it might not be ours */
191   g_return_if_fail(GST_IS_VIDEOBALANCE(object));
192   src = GST_VIDEOBALANCE(object);
193
194   switch (prop_id) {
195     case ARG_CONTRAST:
196       g_value_set_double (value, src->contrast);
197       break;
198     case ARG_BRIGHTNESS:
199       g_value_set_double (value, src->brightness);
200       break;
201     case ARG_HUE:
202       g_value_set_double (value, src->hue);
203       break;
204     case ARG_SATURATION:
205       g_value_set_double (value, src->saturation);
206       break;
207     default:
208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209       break;
210   }
211 }
212
213 static gboolean plugin_init (GstPlugin *plugin)
214 {
215   if(!gst_library_load("gstvideofilter"))
216     return FALSE;
217
218   return gst_element_register (plugin, "videobalance", GST_RANK_NONE,
219       GST_TYPE_VIDEOBALANCE);
220 }
221
222 GST_PLUGIN_DEFINE (
223   GST_VERSION_MAJOR,
224   GST_VERSION_MINOR,
225   "videobalance",
226   "Changes hue, saturation, brightness etc. on video images",
227   plugin_init,
228   VERSION,
229   GST_LICENSE,
230   GST_COPYRIGHT,
231   GST_PACKAGE,
232   GST_ORIGIN
233 )
234
235 static void gst_videobalance_setup(GstVideofilter *videofilter)
236 {
237   GstVideobalance *videobalance;
238
239   g_return_if_fail(GST_IS_VIDEOBALANCE(videofilter));
240   videobalance = GST_VIDEOBALANCE(videofilter);
241
242   /* if any setup needs to be done, do it here */
243
244 }
245
246 static void gst_videobalance_planar411(GstVideofilter *videofilter,
247     void *dest, void *src)
248 {
249   GstVideobalance *videobalance;
250   int width;
251   int height;
252   int x,y;
253
254   g_return_if_fail(GST_IS_VIDEOBALANCE(videofilter));
255   videobalance = GST_VIDEOBALANCE(videofilter);
256
257   width = videofilter->from_width;
258   height = videofilter->from_height;
259
260   {
261     double Y;
262     double contrast;
263     double brightness;
264     guint8 *cdest = dest;
265     guint8 *csrc = src;
266
267     contrast = videobalance->contrast;
268     brightness = videobalance->brightness;
269
270     for(y=0;y<height;y++){
271       for(x=0;x<width;x++){
272         Y = csrc[y*width + x];
273         Y = 16 + ((Y-16) * contrast + brightness*255);
274         if(Y<0)Y=0;
275         if(Y>255)Y=255;
276         cdest[y*width + x] = rint(Y);
277       }
278     }
279   }
280
281   {
282     double u, v;
283     double u1, v1;
284     double saturation;
285     double hue_cos, hue_sin;
286     guint8 *usrc, *vsrc;
287     guint8 *udest, *vdest;
288
289     saturation = videobalance->saturation;
290
291     usrc = src + width*height;
292     udest = dest + width*height;
293     vsrc = src + width*height + (width/2)*(height/2);
294     vdest = dest + width*height + (width/2)*(height/2);
295
296     /* FIXME this is a bogus transformation for hue, but you get
297      * the idea */
298     hue_cos = cos(M_PI*videobalance->hue);
299     hue_sin = sin(M_PI*videobalance->hue);
300   
301     for(y=0;y<height/2;y++){
302       for(x=0;x<width/2;x++){
303         u1 = usrc[y*(width/2) + x] - 128;
304         v1 = vsrc[y*(width/2) + x] - 128;
305         u = 128 + (( u1 * hue_cos + v1 * hue_sin) * saturation);
306         v = 128 + ((-u1 * hue_sin + v1 * hue_cos) * saturation);
307         if(u<0)u=0;
308         if(u>255)u=255;
309         if(v<0)v=0;
310         if(v>255)v=255;
311         udest[y*(width/2) + x] = rint(u);
312         vdest[y*(width/2) + x] = rint(v);
313       }
314     }
315   }
316
317 }
318