b8e2be300aef6dea54fdc64e03023da1fc3a77b4
[platform/upstream/gstreamer.git] / ext / opencv / gsttextoverlay.cpp
1 /*
2  * GStreamer
3  * Copyright (C) 2005 Thomas Vander Stichele <thomas@apestaart.org>
4  * Copyright (C) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
5  * Copyright (C) 2010 Sreerenj Balachandran <bsreerenj@gmail.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Alternatively, the contents of this file may be used under the
26  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
27  * which case the following provisions apply instead of the ones
28  * mentioned above:
29  *
30  * This library is free software; you can redistribute it and/or
31  * modify it under the terms of the GNU Library General Public
32  * License as published by the Free Software Foundation; either
33  * version 2 of the License, or (at your option) any later version.
34  *
35  * This library is distributed in the hope that it will be useful,
36  * but WITHOUT ANY WARRANTY; without even the implied warranty of
37  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
38  * Library General Public License for more details.
39  *
40  * You should have received a copy of the GNU Library General Public
41  * License along with this library; if not, write to the
42  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
43  * Boston, MA 02110-1301, USA.
44  */
45
46 /**
47  * SECTION:element-opencvtextoverlay
48  *
49  * opencvtextoverlay renders the text on top of the video frames
50  *
51  * ## Example launch line
52  *
53  * |[
54  * gst-launch-1.0 videotestsrc ! videoconvert ! opencvtextoverlay text="Opencv Text Overlay " ! videoconvert ! xvimagesink
55  * ]|
56  */
57
58 #ifdef HAVE_CONFIG_H
59 #  include <config.h>
60 #endif
61
62 #include "gsttextoverlay.h"
63
64 GST_DEBUG_CATEGORY_STATIC (gst_opencv_text_overlay_debug);
65 #define GST_CAT_DEFAULT gst_opencv_opencv_text_overlay_debug
66
67 #define DEFAULT_PROP_TEXT       "Opencv Text Overlay"
68 #define DEFAULT_PROP_WIDTH      1
69 #define DEFAULT_PROP_HEIGHT     1
70 #define DEFAULT_PROP_XPOS       50
71 #define DEFAULT_PROP_YPOS       50
72 #define DEFAULT_PROP_THICKNESS  2
73 #define DEFAULT_PROP_COLOR      0
74
75
76 /* Filter signals and args */
77 enum
78 {
79   /* FILL ME */
80   LAST_SIGNAL
81 };
82 #define DEFAULT_WIDTH     1.0
83 #define DEFAULT_HEIGHT    1.0
84 enum
85 {
86   PROP_0,
87   PROP_XPOS,
88   PROP_YPOS,
89   PROP_THICKNESS,
90   PROP_COLOR_R,
91   PROP_COLOR_G,
92   PROP_COLOR_B,
93   PROP_TEXT,
94   PROP_HEIGHT,
95   PROP_WIDTH
96 };
97
98 /* the capabilities of the inputs and outputs.
99  *
100  * describe the real formats here.
101  */
102 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
103     GST_PAD_SINK,
104     GST_PAD_ALWAYS,
105     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
106     );
107
108 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
109     GST_PAD_SRC,
110     GST_PAD_ALWAYS,
111     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
112     );
113
114 G_DEFINE_TYPE (GstOpencvTextOverlay, gst_opencv_text_overlay,
115     GST_TYPE_OPENCV_VIDEO_FILTER);
116
117 static void gst_opencv_text_overlay_set_property (GObject * object,
118     guint prop_id, const GValue * value, GParamSpec * pspec);
119 static void gst_opencv_text_overlay_get_property (GObject * object,
120     guint prop_id, GValue * value, GParamSpec * pspec);
121
122 static GstFlowReturn gst_opencv_text_overlay_transform_ip (GstOpencvVideoFilter
123     * filter, GstBuffer * buf, cv::Mat img);
124
125 /* Clean up */
126 static void
127 gst_opencv_text_overlay_finalize (GObject * obj)
128 {
129   GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (obj);
130
131   g_free (filter->textbuf);
132
133   G_OBJECT_CLASS (gst_opencv_text_overlay_parent_class)->finalize (obj);
134 }
135
136 /* initialize the opencvtextoverlay's class */
137 static void
138 gst_opencv_text_overlay_class_init (GstOpencvTextOverlayClass * klass)
139 {
140   GObjectClass *gobject_class;
141   GstOpencvVideoFilterClass *gstopencvbasefilter_class;
142   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
143
144   gobject_class = (GObjectClass *) klass;
145   gobject_class->finalize =
146       GST_DEBUG_FUNCPTR (gst_opencv_text_overlay_finalize);
147   gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
148
149   gstopencvbasefilter_class->cv_trans_ip_func =
150       gst_opencv_text_overlay_transform_ip;
151
152   gobject_class->set_property = gst_opencv_text_overlay_set_property;
153   gobject_class->get_property = gst_opencv_text_overlay_get_property;
154
155   g_object_class_install_property (gobject_class, PROP_TEXT,
156       g_param_spec_string ("text", "text",
157           "Text to be display.", DEFAULT_PROP_TEXT,
158           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
159
160   g_object_class_install_property (gobject_class, PROP_XPOS,
161       g_param_spec_int ("xpos", "horizontal position",
162           "Sets the Horizontal position", 0, G_MAXINT,
163           DEFAULT_PROP_XPOS,
164           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
165
166   g_object_class_install_property (gobject_class, PROP_YPOS,
167       g_param_spec_int ("ypos", "vertical position",
168           "Sets the Vertical position", 0, G_MAXINT,
169           DEFAULT_PROP_YPOS,
170           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
171
172   g_object_class_install_property (gobject_class, PROP_THICKNESS,
173       g_param_spec_int ("thickness", "font thickness",
174           "Sets the Thickness of Font", 0, G_MAXINT,
175           DEFAULT_PROP_THICKNESS,
176           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
177
178   g_object_class_install_property (gobject_class, PROP_COLOR_R,
179       g_param_spec_int ("colorR", "color -Red ",
180           "Sets the color -R", 0, 255,
181           DEFAULT_PROP_COLOR,
182           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
183
184   g_object_class_install_property (gobject_class, PROP_COLOR_G,
185       g_param_spec_int ("colorG", "color -Green",
186           "Sets the color -G", 0, 255,
187           DEFAULT_PROP_COLOR,
188           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
189
190   g_object_class_install_property (gobject_class, PROP_COLOR_B,
191       g_param_spec_int ("colorB", "color -Blue",
192           "Sets the color -B", 0, 255,
193           DEFAULT_PROP_COLOR,
194           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
195
196   g_object_class_install_property (gobject_class, PROP_HEIGHT,
197       g_param_spec_double ("height", "Height",
198           "Sets the height of fonts", 1.0, 5.0,
199           DEFAULT_HEIGHT,
200           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
201
202   g_object_class_install_property (gobject_class, PROP_WIDTH,
203       g_param_spec_double ("width", "Width",
204           "Sets the width of fonts", 1.0, 5.0,
205           DEFAULT_WIDTH,
206           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
207
208   gst_element_class_set_static_metadata (element_class,
209       "opencvtextoverlay",
210       "Filter/Effect/Video",
211       "Write text on the top of video", "sreerenj<bsreerenj@gmail.com>");
212
213   gst_element_class_add_static_pad_template (element_class, &src_factory);
214   gst_element_class_add_static_pad_template (element_class, &sink_factory);
215
216 }
217
218 /* initialize the new element
219  * instantiate pads and add them to element
220  * set pad calback functions
221  * initialize instance structure
222  */
223 static void
224 gst_opencv_text_overlay_init (GstOpencvTextOverlay * filter)
225 {
226   filter->textbuf = g_strdup (DEFAULT_PROP_TEXT);
227   filter->width = DEFAULT_PROP_WIDTH;
228   filter->height = DEFAULT_PROP_HEIGHT;
229   filter->xpos = DEFAULT_PROP_XPOS;
230   filter->ypos = DEFAULT_PROP_YPOS;
231   filter->thickness = DEFAULT_PROP_THICKNESS;
232   filter->colorR = DEFAULT_PROP_COLOR;
233   filter->colorG = DEFAULT_PROP_COLOR;
234   filter->colorB = DEFAULT_PROP_COLOR;
235
236   gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
237       TRUE);
238 }
239
240 static void
241 gst_opencv_text_overlay_set_property (GObject * object, guint prop_id,
242     const GValue * value, GParamSpec * pspec)
243 {
244   GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (object);
245
246   switch (prop_id) {
247     case PROP_TEXT:
248       g_free (filter->textbuf);
249       filter->textbuf = g_value_dup_string (value);
250       break;
251     case PROP_XPOS:
252       filter->xpos = g_value_get_int (value);
253       break;
254     case PROP_YPOS:
255       filter->ypos = g_value_get_int (value);
256       break;
257     case PROP_THICKNESS:
258       filter->thickness = g_value_get_int (value);
259       break;
260
261     case PROP_COLOR_R:
262       filter->colorR = g_value_get_int (value);
263       break;
264     case PROP_COLOR_G:
265       filter->colorG = g_value_get_int (value);
266       break;
267     case PROP_COLOR_B:
268       filter->colorB = g_value_get_int (value);
269       break;
270
271     case PROP_HEIGHT:
272       filter->height = g_value_get_double (value);
273       break;
274     case PROP_WIDTH:
275       filter->width = g_value_get_double (value);
276       break;
277     default:
278       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
279       break;
280   }
281 }
282
283 static void
284 gst_opencv_text_overlay_get_property (GObject * object, guint prop_id,
285     GValue * value, GParamSpec * pspec)
286 {
287   GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (object);
288
289   switch (prop_id) {
290     case PROP_TEXT:
291       g_value_set_string (value, filter->textbuf);
292       break;
293     case PROP_XPOS:
294       g_value_set_int (value, filter->xpos);
295       break;
296     case PROP_YPOS:
297       g_value_set_int (value, filter->ypos);
298       break;
299     case PROP_THICKNESS:
300       g_value_set_int (value, filter->thickness);
301       break;
302     case PROP_COLOR_R:
303       g_value_set_int (value, filter->colorR);
304       break;
305     case PROP_COLOR_G:
306       g_value_set_int (value, filter->colorG);
307       break;
308     case PROP_COLOR_B:
309       g_value_set_int (value, filter->colorB);
310       break;
311     case PROP_HEIGHT:
312       g_value_set_double (value, filter->height);
313       break;
314     case PROP_WIDTH:
315       g_value_set_double (value, filter->width);
316       break;
317     default:
318       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
319       break;
320   }
321 }
322
323 /* chain function
324  * this function does the actual processing
325  */
326 static GstFlowReturn
327 gst_opencv_text_overlay_transform_ip (GstOpencvVideoFilter * base,
328     GstBuffer * buf, cv::Mat img)
329 {
330   GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (base);
331
332   cv::putText (img, filter->textbuf, cv::Point (filter->xpos,
333           filter->ypos), cv::FONT_HERSHEY_SIMPLEX,
334       (filter->width + filter->height) * 0.5, cv::Scalar (filter->colorR,
335           filter->colorG, filter->colorB), filter->thickness);
336
337   return GST_FLOW_OK;
338 }
339
340
341 /* entry point to initialize the plug-in
342  * initialize the plug-in itself
343  * register the element factories and other features
344  */
345 gboolean
346 gst_opencv_text_overlay_plugin_init (GstPlugin * plugin)
347 {
348   /* debug category for fltering log messages
349    *
350    * exchange the string 'Template opencvtextoverlay' with your description
351    */
352   GST_DEBUG_CATEGORY_INIT (gst_opencv_text_overlay_debug, "opencvtextoverlay",
353       0, "Template opencvtextoverlay");
354
355   return gst_element_register (plugin, "opencvtextoverlay", GST_RANK_NONE,
356       GST_TYPE_OPENCV_TEXT_OVERLAY);
357 }