mirror of
https://code.videolan.org/videolan/vlc
synced 2024-09-24 20:48:52 +02:00
qt: add ImageLuminanceExtractor util class
Signed-off-by: Pierre Lamot <pierre@videolabs.io>
This commit is contained in:
parent
c8bb709d08
commit
f6a37cc3fd
@ -201,6 +201,7 @@ libqt_plugin_la_SOURCES = \
|
|||||||
gui/qt/util/audio_device_model.cpp \
|
gui/qt/util/audio_device_model.cpp \
|
||||||
gui/qt/util/audio_device_model.hpp \
|
gui/qt/util/audio_device_model.hpp \
|
||||||
gui/qt/util/color_scheme_model.cpp gui/qt/util/color_scheme_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/imagehelper.cpp gui/qt/util/imagehelper.hpp \
|
||||||
gui/qt/util/i18n.cpp gui/qt/util/i18n.hpp \
|
gui/qt/util/i18n.cpp gui/qt/util/i18n.hpp \
|
||||||
gui/qt/util/listcache.hpp \
|
gui/qt/util/listcache.hpp \
|
||||||
@ -345,6 +346,7 @@ nodist_libqt_plugin_la_SOURCES = \
|
|||||||
gui/qt/util/asynctask.moc.cpp \
|
gui/qt/util/asynctask.moc.cpp \
|
||||||
gui/qt/util/audio_device_model.moc.cpp \
|
gui/qt/util/audio_device_model.moc.cpp \
|
||||||
gui/qt/util/color_scheme_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/i18n.moc.cpp \
|
||||||
gui/qt/util/listcache.moc.cpp \
|
gui/qt/util/listcache.moc.cpp \
|
||||||
gui/qt/util/navigation_history.moc.cpp \
|
gui/qt/util/navigation_history.moc.cpp \
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include "util/qml_main_context.hpp"
|
#include "util/qml_main_context.hpp"
|
||||||
#include "util/qmleventfilter.hpp"
|
#include "util/qmleventfilter.hpp"
|
||||||
|
#include "util/imageluminanceextractor.hpp"
|
||||||
#include "util/i18n.hpp"
|
#include "util/i18n.hpp"
|
||||||
#include "util/systempalette.hpp"
|
#include "util/systempalette.hpp"
|
||||||
#include "util/sortfilterproxymodel.hpp"
|
#include "util/sortfilterproxymodel.hpp"
|
||||||
@ -197,6 +198,7 @@ void MainUI::registerQMLTypes()
|
|||||||
qmlRegisterType<NetworkSourcesModel>( "org.videolan.vlc", 0, 1, "NetworkSourcesModel");
|
qmlRegisterType<NetworkSourcesModel>( "org.videolan.vlc", 0, 1, "NetworkSourcesModel");
|
||||||
qmlRegisterType<ServicesDiscoveryModel>( "org.videolan.vlc", 0, 1, "ServicesDiscoveryModel");
|
qmlRegisterType<ServicesDiscoveryModel>( "org.videolan.vlc", 0, 1, "ServicesDiscoveryModel");
|
||||||
qmlRegisterType<MlFoldersModel>( "org.videolan.vlc", 0, 1, "MLFolderModel");
|
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" );
|
qmlRegisterUncreatableType<NavigationHistory>("org.videolan.vlc", 0, 1, "History", "Type of global variable history" );
|
||||||
|
|
||||||
|
123
modules/gui/qt/util/imageluminanceextractor.cpp
Normal file
123
modules/gui/qt/util/imageluminanceextractor.cpp
Normal file
@ -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;
|
||||||
|
}
|
76
modules/gui/qt/util/imageluminanceextractor.hpp
Normal file
76
modules/gui/qt/util/imageluminanceextractor.hpp
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user