| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU Lesser General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
| 21 | ** packaging of this file. Please review the following information to | 
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
| 24 | ** | 
| 25 | ** GNU General Public License Usage | 
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
| 31 | ** included in the packaging of this file. Please review the following | 
| 32 | ** information to ensure the GNU General Public License requirements will | 
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
| 35 | ** | 
| 36 | ** $QT_END_LICENSE$ | 
| 37 | ** | 
| 38 | ****************************************************************************/ | 
| 39 |  | 
| 40 | #include "qcameraimageprocessing.h" | 
| 41 | #include "qmediaobject_p.h" | 
| 42 |  | 
| 43 | #include <qcameracontrol.h> | 
| 44 | #include <qcameraexposurecontrol.h> | 
| 45 | #include <qcamerafocuscontrol.h> | 
| 46 | #include <qmediarecordercontrol.h> | 
| 47 | #include <qcameraimageprocessingcontrol.h> | 
| 48 | #include <qcameraimagecapturecontrol.h> | 
| 49 | #include <qvideodeviceselectorcontrol.h> | 
| 50 |  | 
| 51 | #include <QtCore/QDebug> | 
| 52 |  | 
| 53 | QT_BEGIN_NAMESPACE | 
| 54 |  | 
| 55 | static void qRegisterCameraImageProcessingMetaTypes() | 
| 56 |         { | 
| 57 |             qRegisterMetaType<QCameraImageProcessing::WhiteBalanceMode>(); | 
| 58 |             qRegisterMetaType<QCameraImageProcessing::ColorFilter>(); | 
| 59 |         } | 
| 60 |  | 
| 61 | Q_CONSTRUCTOR_FUNCTION(qRegisterCameraImageProcessingMetaTypes) | 
| 62 |  | 
| 63 |  | 
| 64 | /*! | 
| 65 |     \class QCameraImageProcessing | 
| 66 |  | 
| 67 |     \brief The QCameraImageProcessing class provides an interface for | 
| 68 |     image processing related camera settings. | 
| 69 |  | 
| 70 |     \inmodule QtMultimedia | 
| 71 |     \ingroup multimedia | 
| 72 |     \ingroup multimedia_camera | 
| 73 |  | 
| 74 |     After capturing the data for a camera frame, the camera hardware and | 
| 75 |     software performs various image processing tasks to produce a final | 
| 76 |     image.  This includes compensating for ambient light color, reducing | 
| 77 |     noise, as well as making some other adjustments to the image. | 
| 78 |  | 
| 79 |     You can retrieve this class from an instance of a \l QCamera object. | 
| 80 |  | 
| 81 |     For example, you can set the white balance (or color temperature) used | 
| 82 |     for processing images: | 
| 83 |  | 
| 84 |     \snippet multimedia-snippets/camera.cpp Camera image whitebalance | 
| 85 |  | 
| 86 |     Or adjust the amount of denoising performed: | 
| 87 |  | 
| 88 |     \snippet multimedia-snippets/camera.cpp Camera image denoising | 
| 89 |  | 
| 90 |     In some cases changing these settings may result in a longer delay | 
| 91 |     before an image is ready. | 
| 92 |  | 
| 93 |     For more information on image processing of camera frames, see \l {camera_image_processing}{Camera Image Processing}. | 
| 94 |  | 
| 95 |     \sa QCameraImageProcessingControl | 
| 96 | */ | 
| 97 |  | 
| 98 | class QCameraImageProcessingFakeControl : public QCameraImageProcessingControl { | 
| 99 | public: | 
| 100 |     QCameraImageProcessingFakeControl(QObject *parent) : | 
| 101 |         QCameraImageProcessingControl(parent) | 
| 102 |     {} | 
| 103 |  | 
| 104 |     bool isParameterSupported(ProcessingParameter) const override { return false; } | 
| 105 |     bool isParameterValueSupported(ProcessingParameter, const QVariant &) const override { return false; } | 
| 106 |     QVariant parameter(ProcessingParameter) const override { return QVariant(); } | 
| 107 |     void setParameter(ProcessingParameter, const QVariant &) override {} | 
| 108 | }; | 
| 109 |  | 
| 110 |  | 
| 111 | class QCameraImageProcessingPrivate : public QMediaObjectPrivate | 
| 112 | { | 
| 113 |     Q_DECLARE_NON_CONST_PUBLIC(QCameraImageProcessing) | 
| 114 | public: | 
| 115 |     void initControls(); | 
| 116 |  | 
| 117 |     QCamera *camera; | 
| 118 |     QCameraImageProcessingControl *imageControl; | 
| 119 |     bool available; | 
| 120 | }; | 
| 121 |  | 
| 122 |  | 
| 123 | void QCameraImageProcessingPrivate::initControls() | 
| 124 | { | 
| 125 |     imageControl = 0; | 
| 126 |  | 
| 127 |     QMediaService *service = camera->service(); | 
| 128 |     if (service) | 
| 129 |         imageControl = qobject_cast<QCameraImageProcessingControl *>(object: service->requestControl(QCameraImageProcessingControl_iid)); | 
| 130 |  | 
| 131 |     available = (imageControl != nullptr); | 
| 132 |  | 
| 133 |     if (!imageControl) | 
| 134 |         imageControl = new QCameraImageProcessingFakeControl(q_ptr); | 
| 135 | } | 
| 136 |  | 
| 137 | /*! | 
| 138 |     Construct a QCameraImageProcessing for \a camera. | 
| 139 | */ | 
| 140 |  | 
| 141 | QCameraImageProcessing::QCameraImageProcessing(QCamera *camera) | 
| 142 |     : QObject(*new QCameraImageProcessingPrivate, camera) | 
| 143 | { | 
| 144 |     Q_D(QCameraImageProcessing); | 
| 145 |     d->camera = camera; | 
| 146 |     d->initControls(); | 
| 147 | } | 
| 148 |  | 
| 149 |  | 
| 150 | /*! | 
| 151 |     Destroys the camera focus object. | 
| 152 | */ | 
| 153 |  | 
| 154 | QCameraImageProcessing::~QCameraImageProcessing() | 
| 155 | { | 
| 156 | } | 
| 157 |  | 
| 158 |  | 
| 159 | /*! | 
| 160 |     Returns true if image processing related settings are supported by this camera. | 
| 161 | */ | 
| 162 | bool QCameraImageProcessing::isAvailable() const | 
| 163 | { | 
| 164 |     return d_func()->available; | 
| 165 | } | 
| 166 |  | 
| 167 |  | 
| 168 | /*! | 
| 169 |     Returns the white balance mode being used. | 
| 170 | */ | 
| 171 |  | 
| 172 | QCameraImageProcessing::WhiteBalanceMode QCameraImageProcessing::whiteBalanceMode() const | 
| 173 | { | 
| 174 |     return d_func()->imageControl->parameter(parameter: QCameraImageProcessingControl::WhiteBalancePreset) | 
| 175 |             .value<QCameraImageProcessing::WhiteBalanceMode>(); | 
| 176 | } | 
| 177 |  | 
| 178 | /*! | 
| 179 |     Sets the white balance to \a mode. | 
| 180 | */ | 
| 181 |  | 
| 182 | void QCameraImageProcessing::setWhiteBalanceMode(QCameraImageProcessing::WhiteBalanceMode mode) | 
| 183 | { | 
| 184 |     d_func()->imageControl->setParameter( | 
| 185 |                 parameter: QCameraImageProcessingControl::WhiteBalancePreset, | 
| 186 |                 value: QVariant::fromValue<QCameraImageProcessing::WhiteBalanceMode>(value: mode)); | 
| 187 | } | 
| 188 |  | 
| 189 | /*! | 
| 190 |     Returns true if the white balance \a mode is supported. | 
| 191 | */ | 
| 192 |  | 
| 193 | bool QCameraImageProcessing::isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceMode mode) const | 
| 194 | { | 
| 195 |     return d_func()->imageControl->isParameterValueSupported( | 
| 196 |                 parameter: QCameraImageProcessingControl::WhiteBalancePreset, | 
| 197 |                 value: QVariant::fromValue<QCameraImageProcessing::WhiteBalanceMode>(value: mode)); | 
| 198 |  | 
| 199 | } | 
| 200 |  | 
| 201 | /*! | 
| 202 |     Returns the current color temperature if the | 
| 203 |     current white balance mode is \c WhiteBalanceManual.  For other modes the | 
| 204 |     return value is undefined. | 
| 205 | */ | 
| 206 |  | 
| 207 | qreal QCameraImageProcessing::manualWhiteBalance() const | 
| 208 | { | 
| 209 |     return d_func()->imageControl->parameter(parameter: QCameraImageProcessingControl::ColorTemperature).toReal(); | 
| 210 | } | 
| 211 |  | 
| 212 | /*! | 
| 213 |     Sets manual white balance to \a colorTemperature.  This is used | 
| 214 |     when whiteBalanceMode() is set to \c WhiteBalanceManual.  The units are Kelvin. | 
| 215 | */ | 
| 216 |  | 
| 217 | void QCameraImageProcessing::setManualWhiteBalance(qreal colorTemperature) | 
| 218 | { | 
| 219 |     d_func()->imageControl->setParameter( | 
| 220 |                 parameter: QCameraImageProcessingControl::ColorTemperature, | 
| 221 |                 value: QVariant(colorTemperature)); | 
| 222 | } | 
| 223 |  | 
| 224 | /*! | 
| 225 |     Returns the brightness adjustment setting. | 
| 226 |  */ | 
| 227 | qreal QCameraImageProcessing::brightness() const | 
| 228 | { | 
| 229 |     return d_func()->imageControl->parameter(parameter: QCameraImageProcessingControl::BrightnessAdjustment).toReal(); | 
| 230 | } | 
| 231 |  | 
| 232 | /*! | 
| 233 |     Set the brightness adjustment to \a value. | 
| 234 |  | 
| 235 |     Valid brightness adjustment values range between -1.0 and 1.0, with a default of 0. | 
| 236 |  */ | 
| 237 | void QCameraImageProcessing::setBrightness(qreal value) | 
| 238 | { | 
| 239 |     d_func()->imageControl->setParameter(parameter: QCameraImageProcessingControl::BrightnessAdjustment, | 
| 240 |                                          value: QVariant(value)); | 
| 241 | } | 
| 242 |  | 
| 243 | /*! | 
| 244 |     Returns the contrast adjustment setting. | 
| 245 | */ | 
| 246 | qreal QCameraImageProcessing::contrast() const | 
| 247 | { | 
| 248 |     return d_func()->imageControl->parameter(parameter: QCameraImageProcessingControl::ContrastAdjustment).toReal(); | 
| 249 | } | 
| 250 |  | 
| 251 | /*! | 
| 252 |     Set the contrast adjustment to \a value. | 
| 253 |  | 
| 254 |     Valid contrast adjustment values range between -1.0 and 1.0, with a default of 0. | 
| 255 | */ | 
| 256 | void QCameraImageProcessing::setContrast(qreal value) | 
| 257 | { | 
| 258 |     d_func()->imageControl->setParameter(parameter: QCameraImageProcessingControl::ContrastAdjustment, | 
| 259 |                                          value: QVariant(value)); | 
| 260 | } | 
| 261 |  | 
| 262 | /*! | 
| 263 |     Returns the saturation adjustment value. | 
| 264 | */ | 
| 265 | qreal QCameraImageProcessing::saturation() const | 
| 266 | { | 
| 267 |     return d_func()->imageControl->parameter(parameter: QCameraImageProcessingControl::SaturationAdjustment).toReal(); | 
| 268 | } | 
| 269 |  | 
| 270 | /*! | 
| 271 |     Sets the saturation adjustment value to \a value. | 
| 272 |  | 
| 273 |     Valid saturation values range between -1.0 and 1.0, with a default of 0. | 
| 274 | */ | 
| 275 |  | 
| 276 | void QCameraImageProcessing::setSaturation(qreal value) | 
| 277 | { | 
| 278 |     d_func()->imageControl->setParameter(parameter: QCameraImageProcessingControl::SaturationAdjustment, | 
| 279 |                                          value: QVariant(value)); | 
| 280 | } | 
| 281 |  | 
| 282 | /*! | 
| 283 |     Returns the sharpening adjustment level. | 
| 284 | */ | 
| 285 | qreal QCameraImageProcessing::sharpeningLevel() const | 
| 286 | { | 
| 287 |     return d_func()->imageControl->parameter(parameter: QCameraImageProcessingControl::SharpeningAdjustment).toReal(); | 
| 288 | } | 
| 289 |  | 
| 290 | /*! | 
| 291 |     Sets the sharpening adjustment \a level. | 
| 292 |  | 
| 293 |     Valid sharpening values range between -1.0 and 1.0, with a default of 0. | 
| 294 | */ | 
| 295 |  | 
| 296 | void QCameraImageProcessing::setSharpeningLevel(qreal level) | 
| 297 | { | 
| 298 |     d_func()->imageControl->setParameter(parameter: QCameraImageProcessingControl::SharpeningAdjustment, | 
| 299 |                                          value: QVariant(level)); | 
| 300 | } | 
| 301 |  | 
| 302 | /*! | 
| 303 |     Returns the denoising adjustment level. | 
| 304 | */ | 
| 305 | qreal QCameraImageProcessing::denoisingLevel() const | 
| 306 | { | 
| 307 |     return d_func()->imageControl->parameter(parameter: QCameraImageProcessingControl::DenoisingAdjustment).toReal(); | 
| 308 | } | 
| 309 |  | 
| 310 | /*! | 
| 311 |     Sets the denoising adjustment \a level. | 
| 312 |  | 
| 313 |     Valid denoising values range between -1.0 and 1.0, with a default of 0. | 
| 314 |  | 
| 315 |     If the parameter value is set to 0, the amount of denoising applied | 
| 316 |     is selected by camera and depends on camera capabilities and settings. | 
| 317 |     Changing value in -1.0..1.0 range adjusts the amount of denoising applied | 
| 318 |     within the supported range. | 
| 319 | */ | 
| 320 | void QCameraImageProcessing::setDenoisingLevel(qreal level) | 
| 321 | { | 
| 322 |     d_func()->imageControl->setParameter(parameter: QCameraImageProcessingControl::DenoisingAdjustment, | 
| 323 |                                          value: QVariant(level)); | 
| 324 | } | 
| 325 |  | 
| 326 | /*! | 
| 327 |     \enum QCameraImageProcessing::WhiteBalanceMode | 
| 328 |  | 
| 329 |     \value WhiteBalanceAuto         Auto white balance mode. | 
| 330 |     \value WhiteBalanceManual       Manual white balance. In this mode the white balance should be set with | 
| 331 |                                     setManualWhiteBalance() | 
| 332 |     \value WhiteBalanceSunlight     Sunlight white balance mode. | 
| 333 |     \value WhiteBalanceCloudy       Cloudy white balance mode. | 
| 334 |     \value WhiteBalanceShade        Shade white balance mode. | 
| 335 |     \value WhiteBalanceTungsten     Tungsten (incandescent) white balance mode. | 
| 336 |     \value WhiteBalanceFluorescent  Fluorescent white balance mode. | 
| 337 |     \value WhiteBalanceFlash        Flash white balance mode. | 
| 338 |     \value WhiteBalanceSunset       Sunset white balance mode. | 
| 339 |     \value WhiteBalanceVendor       Base value for vendor defined white balance modes. | 
| 340 | */ | 
| 341 |  | 
| 342 | /*! | 
| 343 |     \enum QCameraImageProcessing::ColorFilter | 
| 344 |  | 
| 345 |     \value ColorFilterNone               No filter is applied to images. | 
| 346 |     \value ColorFilterGrayscale          A grayscale filter. | 
| 347 |     \value ColorFilterNegative           A negative filter. | 
| 348 |     \value ColorFilterSolarize           A solarize filter. | 
| 349 |     \value ColorFilterSepia              A sepia filter. | 
| 350 |     \value ColorFilterPosterize          A posterize filter. | 
| 351 |     \value ColorFilterWhiteboard         A whiteboard filter. | 
| 352 |     \value ColorFilterBlackboard         A blackboard filter. | 
| 353 |     \value ColorFilterAqua               An aqua filter. | 
| 354 |     \value ColorFilterVendor             The base value for vendor defined filters. | 
| 355 |  | 
| 356 |     \since 5.5 | 
| 357 | */ | 
| 358 |  | 
| 359 | /*! | 
| 360 |     Returns the color filter which will be applied to image data captured by the camera. | 
| 361 |  | 
| 362 |     \since 5.5 | 
| 363 | */ | 
| 364 |  | 
| 365 | QCameraImageProcessing::ColorFilter QCameraImageProcessing::colorFilter() const | 
| 366 | { | 
| 367 |     return d_func()->imageControl->parameter(parameter: QCameraImageProcessingControl::ColorFilter) | 
| 368 |             .value<QCameraImageProcessing::ColorFilter>(); | 
| 369 | } | 
| 370 |  | 
| 371 |  | 
| 372 | /*! | 
| 373 |     Sets the color \a filter which will be applied to image data captured by the camera. | 
| 374 |  | 
| 375 |     \since 5.5 | 
| 376 | */ | 
| 377 |  | 
| 378 | void QCameraImageProcessing::setColorFilter(QCameraImageProcessing::ColorFilter filter) | 
| 379 | { | 
| 380 |     d_func()->imageControl->setParameter( | 
| 381 |                 parameter: QCameraImageProcessingControl::ColorFilter, | 
| 382 |                 value: QVariant::fromValue<QCameraImageProcessing::ColorFilter>(value: filter)); | 
| 383 | } | 
| 384 |  | 
| 385 | /*! | 
| 386 |     Returns true if a color \a filter is supported. | 
| 387 |  | 
| 388 |     \since 5.5 | 
| 389 | */ | 
| 390 |  | 
| 391 | bool QCameraImageProcessing::isColorFilterSupported(QCameraImageProcessing::ColorFilter filter) const | 
| 392 | { | 
| 393 |     return d_func()->imageControl->isParameterValueSupported( | 
| 394 |                 parameter: QCameraImageProcessingControl::ColorFilter, | 
| 395 |                 value: QVariant::fromValue<QCameraImageProcessing::ColorFilter>(value: filter)); | 
| 396 |  | 
| 397 | } | 
| 398 |  | 
| 399 | QT_END_NAMESPACE | 
| 400 |  | 
| 401 | #include "moc_qcameraimageprocessing.cpp" | 
| 402 |  |