2 * This file is part of CameraPlus.
4 * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "qtcamroi_p.h"
25 #define G_VALUE_INIT { 0, { { 0 } } }
26 #endif /* G_VALUE_INIT */
28 QtCamRoi::QtCamRoi(QtCamDevice *dev, QObject *parent) :
30 d_ptr(new QtCamRoiPrivate(dev, this, this)) {
33 d_ptr->installHandler();
36 QtCamRoi::~QtCamRoi() {
37 delete d_ptr; d_ptr = 0;
40 void QtCamRoi::setRegionOfInterest(const QRectF& roi) {
41 if (!d_ptr->dev || !d_ptr->dev->viewfinder()) {
45 QSizeF vf = d_ptr->dev->viewfinder()->videoResolution();
50 int frameWidth = vf.width();
51 int frameHeight = vf.height();
52 int x = roi.x() * frameWidth;
53 int y = roi.y() * frameHeight;
54 int width = roi.width() * frameWidth;
55 int height = roi.height() * frameHeight;
57 // if we have an empty roi then we reset:
58 int priority = roi.isEmpty() ? 0 : 1;
60 GstStructure *region = gst_structure_new("region0",
61 "region-x", G_TYPE_UINT, x,
62 "region-y", G_TYPE_UINT, y,
63 "region-w", G_TYPE_UINT, width,
64 "region-h", G_TYPE_UINT, height,
65 "region-priority", G_TYPE_UINT, priority,
66 "region-id", G_TYPE_UINT, 0,
69 GValue regionValue = G_VALUE_INIT;
70 GValue regionList = G_VALUE_INIT;
72 g_value_init(®ionValue, GST_TYPE_STRUCTURE);
73 g_value_init(®ionList, GST_TYPE_LIST);
75 gst_value_set_structure(®ionValue, region);
76 gst_value_list_append_value(®ionList, ®ionValue);
78 GstStructure *s = gst_structure_new("regions-of-interest",
79 "frame-width", G_TYPE_UINT, frameWidth,
80 "frame-height", G_TYPE_UINT, frameHeight,
82 gst_structure_set_value(s, "regions", ®ionList);
84 GstEvent *event = gst_event_new_custom(GST_EVENT_CUSTOM_UPSTREAM, s);
85 gst_structure_free(region);
86 g_value_unset(®ionValue);
87 g_value_unset(®ionList);
89 if (!d_ptr->sendEventToSource(event)) {
90 qWarning() << "Failed to send ROI event";
94 void QtCamRoi::resetRegionOfInterest() {
95 setRegionOfInterest(QRectF());
98 void QtCamRoi::setEnabled(bool enabled) {
99 if (enabled == isEnabled()) {
107 gboolean val = enabled ? TRUE : FALSE;
109 g_object_set(d_ptr->roi, d_ptr->dev->config()->roiEnableProperty().toUtf8().constData(),
113 bool QtCamRoi::isEnabled() {
118 gboolean val = FALSE;
120 g_object_get(d_ptr->roi, d_ptr->dev->config()->roiEnableProperty().toUtf8().constData(),
126 QtCamDevice *QtCamRoi::device() {