vlc | branch: master | Prince Gupta <guptaprince8...@gmail.com> | Tue Jan  5 
22:38:15 2021 +0530| [f6a37cc3fda9017f9a1ac7ae4099495a84129d58] | committer: 
Pierre Lamot

qt: add ImageLuminanceExtractor util class

Signed-off-by: Pierre Lamot <pie...@videolabs.io>

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=f6a37cc3fda9017f9a1ac7ae4099495a84129d58
---

 modules/gui/qt/Makefile.am                      |   2 +
 modules/gui/qt/maininterface/mainui.cpp         |   2 +
 modules/gui/qt/util/imageluminanceextractor.cpp | 123 ++++++++++++++++++++++++
 modules/gui/qt/util/imageluminanceextractor.hpp |  76 +++++++++++++++
 4 files changed, 203 insertions(+)

diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am
index ca4fc8756d..7a2665d0a1 100644
--- a/modules/gui/qt/Makefile.am
+++ b/modules/gui/qt/Makefile.am
@@ -201,6 +201,7 @@ libqt_plugin_la_SOURCES = \
        gui/qt/util/audio_device_model.cpp  \
        gui/qt/util/audio_device_model.hpp \
        gui/qt/util/color_scheme_model.cpp gui/qt/util/color_scheme_model.hpp \
+       gui/qt/util/imageluminanceextractor.cpp 
gui/qt/util/imageluminanceextractor.hpp \
        gui/qt/util/imagehelper.cpp gui/qt/util/imagehelper.hpp \
        gui/qt/util/i18n.cpp gui/qt/util/i18n.hpp \
        gui/qt/util/listcache.hpp \
@@ -345,6 +346,7 @@ nodist_libqt_plugin_la_SOURCES = \
        gui/qt/util/asynctask.moc.cpp \
        gui/qt/util/audio_device_model.moc.cpp \
        gui/qt/util/color_scheme_model.moc.cpp \
+       gui/qt/util/imageluminanceextractor.moc.cpp \
        gui/qt/util/i18n.moc.cpp \
        gui/qt/util/listcache.moc.cpp \
        gui/qt/util/navigation_history.moc.cpp \
diff --git a/modules/gui/qt/maininterface/mainui.cpp 
b/modules/gui/qt/maininterface/mainui.cpp
index a91dcd6c8a..02d9cab54b 100644
--- a/modules/gui/qt/maininterface/mainui.cpp
+++ b/modules/gui/qt/maininterface/mainui.cpp
@@ -22,6 +22,7 @@
 
 #include "util/qml_main_context.hpp"
 #include "util/qmleventfilter.hpp"
+#include "util/imageluminanceextractor.hpp"
 #include "util/i18n.hpp"
 #include "util/systempalette.hpp"
 #include "util/sortfilterproxymodel.hpp"
@@ -197,6 +198,7 @@ void MainUI::registerQMLTypes()
     qmlRegisterType<NetworkSourcesModel>( "org.videolan.vlc", 0, 1, 
"NetworkSourcesModel");
     qmlRegisterType<ServicesDiscoveryModel>( "org.videolan.vlc", 0, 1, 
"ServicesDiscoveryModel");
     qmlRegisterType<MlFoldersModel>( "org.videolan.vlc", 0, 1, 
"MLFolderModel");
+    qmlRegisterType<ImageLuminanceExtractor>( "org.videolan.vlc", 0, 1, 
"ImageLuminanceExtractor");
 
     qmlRegisterUncreatableType<NavigationHistory>("org.videolan.vlc", 0, 1, 
"History", "Type of global variable history" );
 
diff --git a/modules/gui/qt/util/imageluminanceextractor.cpp 
b/modules/gui/qt/util/imageluminanceextractor.cpp
new file mode 100644
index 0000000000..816819245a
--- /dev/null
+++ b/modules/gui/qt/util/imageluminanceextractor.cpp
@@ -0,0 +1,123 @@
+/*****************************************************************************
+ * Copyright (C) 2021 the VideoLAN team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 
USA.
+ *****************************************************************************/
+
+#include <QImage>
+#include <QColor>
+
+#include "imageluminanceextractor.hpp"
+
+ImageLuminanceExtractor::ImageLuminanceExtractor(QObject *parent) : 
QObject(parent) {}
+
+QUrl ImageLuminanceExtractor::source() const
+{
+    return m_source;
+}
+
+int ImageLuminanceExtractor::luminance() const
+{
+    return m_luminance;
+}
+
+bool ImageLuminanceExtractor::isEnabled() const
+{
+    return m_enabled;
+}
+
+void ImageLuminanceExtractor::setLuminance(const int luminance)
+{
+    m_luminance = luminance;
+    emit luminanceChanged(m_luminance);
+}
+
+void ImageLuminanceExtractor::setSource(const QUrl &source)
+{
+    if (m_source == source)
+        return;
+
+    m_source = source;
+    if (m_enabled)
+        startTask();
+    else
+        m_pendingUpdate = true;
+    emit sourceChanged(m_source);
+}
+
+void ImageLuminanceExtractor::setIsEnabled(const bool enabled)
+{
+    if (m_enabled == enabled)
+        return;
+
+    m_enabled = enabled;
+    if (m_enabled && m_pendingUpdate)
+        startTask();
+    emit enabledChanged(m_enabled);
+}
+
+void ImageLuminanceExtractor::startTask()
+{
+    m_pendingUpdate = false;
+    m_task.reset(new LuminanceCalculator(m_source));
+    connect(m_task.get(), &LuminanceCalculator::result, this, [this]()
+    {
+        LuminanceCalculator *task = static_cast<LuminanceCalculator 
*>(sender());
+        assert(task == m_task.get());
+
+        int result = task->takeResult();
+        if (result != Status::FAILED)
+            setLuminance(result);
+        else
+            qWarning("luminance extraction failed");
+
+        m_task.reset();
+    });
+
+    m_task->start(*QThreadPool::globalInstance());
+}
+
+ImageLuminanceExtractor::LuminanceCalculator::LuminanceCalculator(const QUrl 
&source) : m_source {source} {}
+
+int ImageLuminanceExtractor::LuminanceCalculator::execute()
+{
+    QString path = m_source.isLocalFile() ? m_source.toLocalFile() : 
m_source.toString();
+    if (path.startsWith("qrc:///"))
+        path.replace(0, strlen("qrc:///"), ":/");
+    else if (!m_source.isLocalFile())
+        return Status::FAILED;
+
+    const QImage image = QImage(path).scaled(128, 128, Qt::KeepAspectRatio);
+    if (image.isNull())
+        return Status::FAILED;
+
+    int lumimance = 0;
+    size_t count = 0;
+    for (int i = 0; i < image.width(); i++)
+    {
+        for (int j = 0; j < image.height(); j++)
+        {
+            const QColor pixelColor = image.pixelColor(i, j);
+            if (!pixelColor.isValid())
+                continue;
+
+            lumimance += 0.299*pixelColor.red() + 0.587*pixelColor.green() + 
0.144*pixelColor.blue();
+            count++;
+        }
+    }
+
+    lumimance = (lumimance / count);
+    return lumimance;
+}
diff --git a/modules/gui/qt/util/imageluminanceextractor.hpp 
b/modules/gui/qt/util/imageluminanceextractor.hpp
new file mode 100644
index 0000000000..5e553c5028
--- /dev/null
+++ b/modules/gui/qt/util/imageluminanceextractor.hpp
@@ -0,0 +1,76 @@
+/*****************************************************************************
+ * Copyright (C) 2021 the VideoLAN team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 
USA.
+ *****************************************************************************/
+
+#ifndef IMAGELUMINANCEXTRACTOR_HPP
+#define IMAGELUMINANCEXTRACTOR_HPP
+
+#include <QUrl>
+
+#include "util/asynctask.hpp"
+
+class ImageLuminanceExtractor : public QObject
+{
+    Q_OBJECT
+    Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
+    Q_PROPERTY(int luminance READ luminance NOTIFY luminanceChanged)
+    Q_PROPERTY(bool enabled READ isEnabled WRITE setIsEnabled NOTIFY 
enabledChanged)
+
+public:
+    enum Status
+    {
+        FAILED = -1
+    };
+
+    ImageLuminanceExtractor(QObject *parent = nullptr);
+
+    QUrl source() const;
+    int luminance() const;
+    bool isEnabled() const;
+
+public slots:
+    void setSource(const QUrl &source);
+    void setIsEnabled(bool enabled);
+
+signals:
+    void sourceChanged(QUrl source);
+    void luminanceChanged(int luminance);
+    void enabledChanged(bool enabled);
+
+private:
+    class LuminanceCalculator : public AsyncTask<int>
+    {
+    public:
+        LuminanceCalculator(const QUrl &source);
+
+        int execute() override;
+
+    private:
+        QUrl m_source;
+    };
+
+    void startTask();
+    void setLuminance(int luminance);
+
+    QUrl m_source;
+    TaskHandle<LuminanceCalculator> m_task;
+    int m_luminance = 0;
+    bool m_enabled = false;
+    bool m_pendingUpdate = false;
+};
+
+#endif // IMAGELUMINANCEXTRACTOR_HPP

_______________________________________________
vlc-commits mailing list
vlc-commits@videolan.org
https://mailman.videolan.org/listinfo/vlc-commits

Reply via email to