1 /*
2  * GStreamer
3  * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Alternatively, the contents of this file may be used under the
24  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
25  * which case the following provisions apply instead of the ones
26  * mentioned above:
27  *
28  * This library is free software; you can redistribute it and/or
29  * modify it under the terms of the GNU Library General Public
30  * License as published by the Free Software Foundation; either
31  * version 2 of the License, or (at your option) any later version.
32  *
33  * This library is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
36  * Library General Public License for more details.
37  *
38  * You should have received a copy of the GNU Library General Public
39  * License along with this library; if not, write to the
40  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
41  * Boston, MA 02110-1301, USA.
42  */
43 
44 /**
45  * SECTION:element-cvequalizehist
46  *
47  * Equalizes the histogram of a grayscale image with the cvEqualizeHist OpenCV
48  * function.
49  *
50  * <refsect2>
51  * <title>Example launch line</title>
52  * |[
53  * gst-launch-1.0 videotestsrc pattern=23 ! cvequalizehist ! videoconvert ! autovideosink
54  * ]|
55  * </refsect2>
56  */
57 
58 
59 #ifdef HAVE_CONFIG_H
60 #  include <config.h>
61 #endif
62 
63 #include "gstcvequalizehist.h"
64 #include <opencv2/imgproc.hpp>
65 
66 GST_DEBUG_CATEGORY_STATIC (gst_cv_equalize_hist_debug);
67 #define GST_CAT_DEFAULT gst_cv_equalize_hist_debug
68 
69 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
70     GST_PAD_SINK,
71     GST_PAD_ALWAYS,
72     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("GRAY8")));
73 
74 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
75     GST_PAD_SRC,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("GRAY8")));
78 
79 G_DEFINE_TYPE (GstCvEqualizeHist, gst_cv_equalize_hist,
80     GST_TYPE_OPENCV_VIDEO_FILTER);
81 
82 static GstFlowReturn gst_cv_equalize_hist_transform (GstOpencvVideoFilter *
83     filter, GstBuffer * buf, cv::Mat img, GstBuffer * outbuf, cv::Mat outimg);
84 
85 
86 static void
gst_cv_equalize_hist_class_init(GstCvEqualizeHistClass * klass)87 gst_cv_equalize_hist_class_init (GstCvEqualizeHistClass * klass)
88 {
89   GstOpencvVideoFilterClass *gstopencvbasefilter_class;
90   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
91   gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
92 
93   gstopencvbasefilter_class->cv_trans_func = gst_cv_equalize_hist_transform;
94 
95   gst_element_class_add_static_pad_template (element_class, &src_factory);
96   gst_element_class_add_static_pad_template (element_class, &sink_factory);
97 
98   gst_element_class_set_static_metadata (element_class,
99       "cvequalizehist",
100       "Transform/Effect/Video",
101       "Applies cvEqualizeHist OpenCV function to the image",
102       "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
103 }
104 
105 static void
gst_cv_equalize_hist_init(GstCvEqualizeHist * filter)106 gst_cv_equalize_hist_init (GstCvEqualizeHist * filter)
107 {
108   gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
109       FALSE);
110 }
111 
112 static GstFlowReturn
gst_cv_equalize_hist_transform(GstOpencvVideoFilter * base,GstBuffer * buf,cv::Mat img,GstBuffer * outbuf,cv::Mat outimg)113 gst_cv_equalize_hist_transform (GstOpencvVideoFilter * base,
114     GstBuffer * buf, cv::Mat img, GstBuffer * outbuf, cv::Mat outimg)
115 {
116   cv::equalizeHist (img, outimg);
117   return GST_FLOW_OK;
118 }
119 
120 gboolean
gst_cv_equalize_hist_plugin_init(GstPlugin * plugin)121 gst_cv_equalize_hist_plugin_init (GstPlugin * plugin)
122 {
123   GST_DEBUG_CATEGORY_INIT (gst_cv_equalize_hist_debug, "cvequalizehist", 0,
124       "cvequalizehist");
125   return gst_element_register (plugin, "cvequalizehist", GST_RANK_NONE,
126       GST_TYPE_CV_EQUALIZE_HIST);
127 }
128