ICC Color Correction Effect for 5.26.4

icc-effect-5.26.90
Vitaliy Filippov 2023-01-20 02:24:16 +03:00
parent 0e0b9dea12
commit 19de9e97cf
15 changed files with 660 additions and 0 deletions

View File

@ -96,6 +96,7 @@ add_subdirectory(blendchanges)
add_subdirectory(blur)
add_subdirectory(backgroundcontrast)
add_subdirectory(glide)
add_subdirectory(icc)
add_subdirectory(invert)
add_subdirectory(magnifier)
add_subdirectory(mouseclick)

View File

@ -0,0 +1,36 @@
#######################################
# Effect
find_package(PkgConfig)
pkg_check_modules(LCMS2 REQUIRED lcms2)
set_package_properties(LCMS2 PROPERTIES TYPE REQUIRED PURPOSE "Required for ICC color correction.")
set(icc_SOURCES icc.cpp icc.qrc main.cpp)
kwin4_add_effect_module(kwin4_effect_icc ${icc_SOURCES})
target_link_libraries(kwin4_effect_icc PUBLIC ${LCMS2_LIBRARIES})
target_include_directories(kwin4_effect_icc PUBLIC ${LCMS2_INCLUDE_DIRS})
target_compile_options(kwin4_effect_icc PUBLIC ${LCMS2_CFLAGS_OTHER})
#######################################
# Config
if (KWIN_BUILD_KCMS)
set(kwin_icc_config_SRCS icc_config.cpp)
kwin_add_effect_config(kwin_icc_config ${kwin_icc_config_SRCS})
ki18n_wrap_ui(kwin_icc_config_SRCS icc_config.ui)
qt5_add_dbus_interface(kwin_icc_config_SRCS ${kwin_effects_dbus_xml} kwineffects_interface)
kconfig_add_kcfg_files(kwin_icc_config_SRCS iccconfig.kcfgc)
target_link_libraries(kwin_icc_config
KF5::ConfigWidgets
KF5::I18n
KF5::Service
KF5::XmlGui
)
kcoreaddons_desktop_to_json(kwin_icc_config icc_config.desktop SERVICE_TYPES kcmodule.desktop)
install(
TARGETS
kwin_icc_config
DESTINATION
${PLUGIN_INSTALL_DIR}/kwin/effects/configs
)
endif()

242
src/effects/icc/icc.cpp Normal file
View File

@ -0,0 +1,242 @@
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2019 Vitaliy Filippov <vitalif@yourcmc.ru>
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, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "icc.h"
#include "iccconfig.h"
#include "kwinglplatform.h"
#include "lcms2.h"
#include <QAction>
#include <QFile>
#include <KGlobalAccel>
#include <KLocalizedString>
#include <QStandardPaths>
static const int LUT_POINTS = 64;
namespace KWin
{
ICCEffect::ICCEffect()
: m_valid(false),
m_shader(NULL),
m_texture(0),
m_clut(NULL)
{
initConfig<ICCConfig>();
reconfigure(ReconfigureAll);
}
ICCEffect::~ICCEffect()
{
if (m_shader)
delete m_shader;
if (m_clut)
delete[] m_clut;
if (m_texture != 0)
glDeleteTextures(1, &m_texture);
}
bool ICCEffect::supported()
{
return effects->compositingType() == OpenGL2Compositing;
}
void ICCEffect::reconfigure(ReconfigureFlags flags)
{
Q_UNUSED(flags)
ICCConfig::self()->read();
m_sourceICC = ICCConfig::self()->sourceICC().trimmed();
m_targetICC = ICCConfig::self()->targetICC().trimmed();
loadData();
effects->addRepaintFull();
}
bool ICCEffect::loadData()
{
m_valid = false;
if (m_shader)
delete m_shader;
if (m_clut)
delete[] m_clut;
if (m_texture != 0)
glDeleteTextures(1, &m_texture);
m_shader = ShaderManager::instance()->generateShaderFromFile(ShaderTrait::MapTexture, QString(), QStringLiteral(":/effects/icc/shaders/icc.frag"));
if (!m_shader->isValid())
{
qCCritical(KWINEFFECTS) << "The shader failed to load!";
return false;
}
ShaderManager::instance()->pushShader(m_shader);
m_shader->setUniform("clut", 3); // GL_TEXTURE3
ShaderManager::instance()->popShader();
m_clut = makeCLUT(m_sourceICC.trimmed().isEmpty() ? NULL : m_sourceICC.toLocal8Bit().data(), m_targetICC.toLocal8Bit().data());
if (m_clut)
{
m_texture = setupCCTexture(m_clut);
if (m_texture)
{
m_valid = true;
return true;
}
}
return false;
}
uint8_t *ICCEffect::makeCLUT(const char* source_icc, const char* target_icc)
{
uint8_t *clut = NULL, *clut_source = NULL;
cmsHPROFILE source, target;
cmsHTRANSFORM transform;
source = source_icc ? cmsOpenProfileFromFile(source_icc, "r") : cmsCreate_sRGBProfile();
if (!source)
goto free_nothing;
target = cmsOpenProfileFromFile(target_icc, "r");
if (!target)
goto free_source;
transform = cmsCreateTransform(
source, TYPE_RGB_8, target, TYPE_RGB_8,
INTENT_RELATIVE_COLORIMETRIC, cmsFLAGS_BLACKPOINTCOMPENSATION
);
if (!transform)
goto free_target;
clut_source = new uint8_t[LUT_POINTS*LUT_POINTS*LUT_POINTS*3];
if (!clut_source)
goto free_transform;
for (int b = 0, addr = 0; b < LUT_POINTS; b++)
{
for (int g = 0; g < LUT_POINTS; g++)
{
for (int r = 0; r < LUT_POINTS; r++, addr += 3)
{
clut_source[addr] = 255*r/(LUT_POINTS-1);
clut_source[addr+1] = 255*g/(LUT_POINTS-1);
clut_source[addr+2] = 255*b/(LUT_POINTS-1);
}
}
}
clut = new uint8_t[LUT_POINTS*LUT_POINTS*LUT_POINTS*3];
if (!clut)
goto free_clut_source;
cmsDoTransform(transform, clut_source, clut, LUT_POINTS*LUT_POINTS*LUT_POINTS);
/*for (int b = 0, addr = 0; b < LUT_POINTS; b++)
{
for (int g = 0; g < LUT_POINTS; g++)
{
for (int r = 0; r < LUT_POINTS; r++, addr += 3)
{
printf("cLUT %02x%02x%02x -> %02x%02x%02x\n", 255*r/(LUT_POINTS-1), 255*g/(LUT_POINTS-1), 255*b/(LUT_POINTS-1), clut[addr], clut[addr+1], clut[addr+2]);
}
}
}*/
free_clut_source:
delete[] clut_source;
free_transform:
cmsDeleteTransform(transform);
free_target:
cmsCloseProfile(target);
free_source:
cmsCloseProfile(source);
free_nothing:
return clut;
}
GLuint ICCEffect::setupCCTexture(uint8_t *clut)
{
GLenum err = glGetError();
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_3D, texture);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB8,
LUT_POINTS, LUT_POINTS, LUT_POINTS,
0, GL_RGB, GL_UNSIGNED_BYTE, clut);
if ((err = glGetError()) != GL_NO_ERROR)
{
glDeleteTextures(1, &texture);
return 0;
}
return texture;
}
void ICCEffect::drawWindow(EffectWindow* w, int mask, const QRegion &region, WindowPaintData& data)
{
if (m_valid)
{
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_3D, m_texture);
glActiveTexture(GL_TEXTURE0);
ShaderManager *shaderManager = ShaderManager::instance();
shaderManager->pushShader(m_shader);
data.shader = m_shader;
}
effects->drawWindow(w, mask, region, data);
if (m_valid)
{
ShaderManager::instance()->popShader();
}
}
void ICCEffect::paintEffectFrame(KWin::EffectFrame* frame, const QRegion &region, double opacity, double frameOpacity)
{
if (m_valid)
{
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_3D, m_texture);
glActiveTexture(GL_TEXTURE0);
frame->setShader(m_shader);
ShaderBinder binder(m_shader);
effects->paintEffectFrame(frame, region, opacity, frameOpacity);
}
else
{
effects->paintEffectFrame(frame, region, opacity, frameOpacity);
}
}
bool ICCEffect::isActive() const
{
return m_valid;
}
} // namespace

78
src/effects/icc/icc.h Normal file
View File

@ -0,0 +1,78 @@
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2019 Vitaliy Filippov <vitalif@yourcmc.ru>
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, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#ifndef KWIN_ICC_H
#define KWIN_ICC_H
#include <kwineffects.h>
#include "kwinglutils.h"
namespace KWin
{
class GLShader;
/**
* Applies cLUT (32x32x32 RGB LUT)-based color correction to the whole desktop
* cLUT is generated from a pair of ICC profiles using LCMS2
*/
class ICCEffect
: public Effect
{
Q_OBJECT
public:
ICCEffect();
~ICCEffect();
void reconfigure(ReconfigureFlags flags) override;
void drawWindow(EffectWindow* w, int mask, const QRegion &region, WindowPaintData& data) override;
void paintEffectFrame(KWin::EffectFrame* frame, const QRegion &region, double opacity, double frameOpacity) override;
bool isActive() const override;
int requestedEffectChainPosition() const override;
static bool supported();
public Q_SLOTS:
protected:
bool loadData();
private:
bool m_inited;
bool m_valid;
QString m_sourceICC;
QString m_targetICC;
GLShader* m_shader;
GLuint m_texture;
uint8_t *m_clut;
uint8_t *makeCLUT(const char* source_icc, const char* target_icc);
GLuint setupCCTexture(uint8_t *clut);
};
inline int ICCEffect::requestedEffectChainPosition() const
{
return 98;
}
} // namespace
#endif

15
src/effects/icc/icc.kcfg Normal file
View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
http://www.kde.org/standards/kcfg/1.0/kcfg.xsd" >
<kcfgfile arg="true"/>
<group name="Effect-ICC">
<entry name="SourceICC" type="String">
<default></default>
</entry>
<entry name="TargetICC" type="String">
<default></default>
</entry>
</group>
</kcfg>

6
src/effects/icc/icc.qrc Normal file
View File

@ -0,0 +1,6 @@
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/effects/icc/">
<file>shaders/icc.frag</file>
<file>shaders/icc_core.frag</file>
</qresource>
</RCC>

View File

@ -0,0 +1,82 @@
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2019 Vitaliy Filippov <vitalif@yourcmc.ru>
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, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "icc_config.h"
#include <kwineffects_interface.h>
#include "iccconfig.h"
#include <config-kwin.h>
#include <QAction>
#include <KLocalizedString>
#include <KActionCollection>
#include <KAboutData>
#include <KPluginFactory>
#include <QVBoxLayout>
K_PLUGIN_FACTORY_WITH_JSON(ICCEffectConfigFactory,
"icc_config.json",
registerPlugin<KWin::ICCEffectConfig>();)
namespace KWin
{
ICCEffectConfig::ICCEffectConfig(QWidget* parent, const QVariantList& args) :
KCModule(KAboutData::pluginData(QStringLiteral("icc")), parent, args)
{
ui.setupUi(this);
ICCConfig::instance(KWIN_CONFIG);
addConfig(ICCConfig::self(), this);
load();
}
ICCEffectConfig::~ICCEffectConfig()
{
// Undo unsaved changes
}
void ICCEffectConfig::load()
{
KCModule::load();
emit changed(false);
}
void ICCEffectConfig::save()
{
KCModule::save();
emit changed(false);
OrgKdeKwinEffectsInterface interface(QStringLiteral("org.kde.KWin"),
QStringLiteral("/Effects"),
QDBusConnection::sessionBus());
interface.reconfigureEffect(QStringLiteral("icc"));
}
void ICCEffectConfig::defaults()
{
emit changed(true);
}
} // namespace
#include "icc_config.moc"

View File

@ -0,0 +1,9 @@
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=KCModule
X-KDE-Library=kwin_icc_config
X-KDE-ParentComponents=icc
Name=ICC color correction
Name[ru]=ICC цветокоррекция

View File

@ -0,0 +1,48 @@
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2019 Vitaliy Filippov <vitalif@yourcmc.ru>
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, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#ifndef KWIN_ICC_CONFIG_H
#define KWIN_ICC_CONFIG_H
#include <kcmodule.h>
#include "ui_icc_config.h"
namespace KWin
{
class ICCEffectConfig : public KCModule
{
Q_OBJECT
public:
explicit ICCEffectConfig(QWidget* parent = 0, const QVariantList& args = QVariantList());
~ICCEffectConfig();
public Q_SLOTS:
virtual void save();
virtual void load();
virtual void defaults();
private:
::Ui::ICCEffectConfig ui;
};
} // namespace
#endif

View File

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ICCEffectConfig</class>
<widget class="QWidget" name="ICCEffectConfig">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>642</width>
<height>107</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QFormLayout" name="iccForm">
<property name="labelAlignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<item row="0" column="0">
<widget class="QLabel" name="sourceICCProfileLabel">
<property name="text">
<string>Source ICC profile file:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="kcfg_SourceICC">
<property name="text">
<string/>
</property>
<property name="placeholderText">
<string>leave empty to use sRGB (default color space)</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="targetICCProfileLabel">
<property name="text">
<string>Target ICC profile file:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="kcfg_TargetICC">
<property name="placeholderText">
<string>select your display's ICC profile here</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -0,0 +1,5 @@
File=icc.kcfg
ClassName=ICCConfig
NameSpace=KWin
Singleton=true
Mutators=true

11
src/effects/icc/main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "icc.h"
namespace KWin
{
KWIN_EFFECT_FACTORY(ICCEffect,
"metadata.json.stripped")
} // namespace KWin
#include "main.moc"

View File

@ -0,0 +1,13 @@
{
"KPlugin": {
"Category": "Accessibility",
"Description": "ICC Color Conversion",
"EnabledByDefault": false,
"Id": "icc",
"License": "GPL",
"Name": "ICC"
},
"X-KDE-ConfigModule": "kwin_icc_config",
"org.kde.kwin.effect": {
}
}

View File

@ -0,0 +1,26 @@
uniform sampler2D sampler;
varying vec2 texcoord0;
uniform sampler3D clut;
uniform vec4 modulation;
uniform float saturation;
void main()
{
vec4 tex = texture2D(sampler, texcoord0);
if (saturation != 1.0) {
vec3 desaturated = tex.rgb * vec3( 0.30, 0.59, 0.11 );
desaturated = vec3( dot( desaturated, tex.rgb ));
tex.rgb = tex.rgb * vec3( saturation ) + desaturated * vec3( 1.0 - saturation );
}
tex.rgb = texture3D(clut, tex.rgb).rgb;
tex *= modulation;
tex.rgb *= tex.a;
gl_FragColor = tex;
}

View File

@ -0,0 +1,29 @@
#version 140
uniform sampler2D sampler;
in vec2 texcoord0;
uniform sampler3D clut;
out vec4 fragColor;
uniform vec4 modulation;
uniform float saturation;
void main()
{
vec4 tex = texture(sampler, texcoord0);
if (saturation != 1.0) {
vec3 desaturated = tex.rgb * vec3( 0.30, 0.59, 0.11 );
desaturated = vec3( dot( desaturated, tex.rgb ));
tex.rgb = tex.rgb * vec3( saturation ) + desaturated * vec3( 1.0 - saturation );
}
tex.rgb = texture(clut, tex.rgb).rgb;
tex *= modulation;
tex.rgb *= tex.a;
fragColor = tex;
}