Added QtCamAutoFocus::setRegionOfInterest()
[harmattan/cameraplus] / lib / qtcamautofocus.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "qtcamautofocus.h"
22 #include "qtcamautofocus_p.h"
23 #include <QRectF>
24
25 #ifndef G_VALUE_INIT
26 #define G_VALUE_INIT  { 0, { { 0 } } }
27 #endif /* G_VALUE_INIT */
28
29 QtCamAutoFocus::QtCamAutoFocus(QtCamDevice *dev, QObject *parent) :
30   QObject(parent),
31   d_ptr(new QtCamAutoFocusPrivate(dev, this, this)) {
32
33 }
34
35 QtCamAutoFocus::~QtCamAutoFocus() {
36   delete d_ptr; d_ptr = 0;
37 }
38
39 QtCamAutoFocus::Status QtCamAutoFocus::status() {
40   return d_ptr->status;
41 }
42
43 QtCamAutoFocus::Status QtCamAutoFocus::cafStatus() {
44   return d_ptr->cafStatus;
45 }
46
47 bool QtCamAutoFocus::startAutoFocus() {
48   return d_ptr->setEnabled(true);
49 }
50
51 bool QtCamAutoFocus::stopAutoFocus() {
52   return d_ptr->setEnabled(false);
53 }
54
55 bool QtCamAutoFocus::canFocus(const QtCamScene::SceneMode& mode) {
56   if (mode == QtCamScene::Landscape || mode == QtCamScene::Sport) {
57     return false;
58   }
59
60   return true;
61 }
62
63 void QtCamAutoFocus::setRegionOfInterest(const QRectF& roi) {
64   if (!d_ptr->dev || !d_ptr->dev->viewfinder()) {
65     return;
66   }
67
68   QSizeF vf = d_ptr->dev->viewfinder()->videoResolution();
69   if (vf.isEmpty()) {
70     return;
71   }
72
73   int frameWidth = vf.width();
74   int frameHeight = vf.height();
75   int x = roi.x() * frameWidth;
76   int y = roi.y() * frameHeight;
77   int width = roi.width() * frameWidth;
78   int height = roi.height() * frameHeight;
79
80   // if we have an empty roi then we reset:
81   int priority = roi.isEmpty() ? 0 : 1;
82
83   GstStructure *region = gst_structure_new("region0",
84                                            "region-x", G_TYPE_UINT, x,
85                                            "region-y", G_TYPE_UINT, y,
86                                            "region-w", G_TYPE_UINT, width,
87                                            "region-h", G_TYPE_UINT, height,
88                                            "region-priority", G_TYPE_UINT, priority,
89                                            "region-id", G_TYPE_UINT, 0,
90                                            NULL);
91
92   GValue regionValue = G_VALUE_INIT;
93   GValue regionList = G_VALUE_INIT;
94
95   g_value_init(&regionValue, GST_TYPE_STRUCTURE);
96   g_value_init(&regionList, GST_TYPE_LIST);
97
98   gst_value_set_structure(&regionValue, region);
99   gst_value_list_append_value(&regionList, &regionValue);
100
101   GstStructure *s = gst_structure_new("regions-of-interest",
102                                       "frame-width", G_TYPE_UINT, frameWidth,
103                                       "frame-height", G_TYPE_UINT, frameHeight,
104                                       NULL);
105   gst_structure_set_value(s, "regions", &regionList);
106
107   GstEvent *event = gst_event_new_custom(GST_EVENT_CUSTOM_UPSTREAM, s);
108   gst_structure_free(region);
109   g_value_unset(&regionValue);
110   g_value_unset(&regionList);
111
112   if (!d_ptr->sendEventToSource(event)) {
113     qWarning() << "Failed to send ROI event";
114   }
115 }