Skip to content

Commit

Permalink
Merge pull request #146 from taj-ny/colors
Browse files Browse the repository at this point in the history
blur: add ability to set brightness, saturation and contrast
  • Loading branch information
taj-ny authored Dec 19, 2024
2 parents 0d8563f + d0e61cb commit 2167a3f
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 5 deletions.
41 changes: 41 additions & 0 deletions src/blur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ BlurEffect::BlurEffect()
m_downsamplePass.mvpMatrixLocation = m_downsamplePass.shader->uniformLocation("modelViewProjectionMatrix");
m_downsamplePass.offsetLocation = m_downsamplePass.shader->uniformLocation("offset");
m_downsamplePass.halfpixelLocation = m_downsamplePass.shader->uniformLocation("halfpixel");
m_downsamplePass.transformColorsLocation = m_downsamplePass.shader->uniformLocation("transformColors");
m_downsamplePass.colorMatrixLocation = m_downsamplePass.shader->uniformLocation("colorMatrix");
}

m_upsamplePass.shader = ShaderManager::instance()->generateShaderFromFile(ShaderTrait::MapTexture,
Expand Down Expand Up @@ -238,6 +240,7 @@ void BlurEffect::reconfigure(ReconfigureFlags flags)
m_offset = blurStrengthValues[m_settings.general.blurStrength].offset;
m_expandSize = blurOffsets[m_iterationCount - 1].expandSize;
m_fakeBlurTextures.clear();
m_colorMatrix = colorMatrix(m_settings.general.brightness, m_settings.general.saturation, m_settings.general.contrast);

for (EffectWindow *w : effects->stackingOrder()) {
updateBlurRegion(w);
Expand Down Expand Up @@ -1005,6 +1008,8 @@ void BlurEffect::blur(BlurRenderData &renderInfo, const RenderTarget &renderTarg

m_downsamplePass.shader->setUniform(m_downsamplePass.mvpMatrixLocation, projectionMatrix);
m_downsamplePass.shader->setUniform(m_downsamplePass.offsetLocation, float(m_offset));
m_downsamplePass.shader->setUniform(m_downsamplePass.colorMatrixLocation, m_colorMatrix);
m_downsamplePass.shader->setUniform(m_downsamplePass.transformColorsLocation, true);

for (size_t i = 1; i < renderInfo.framebuffers.size(); ++i) {
const auto &read = renderInfo.framebuffers[i - 1];
Expand All @@ -1018,6 +1023,10 @@ void BlurEffect::blur(BlurRenderData &renderInfo, const RenderTarget &renderTarg

GLFramebuffer::pushFramebuffer(draw.get());
vbo->draw(GL_TRIANGLES, 0, 6);

if (i == 1) {
m_downsamplePass.shader->setUniform(m_downsamplePass.transformColorsLocation, false);
}
}

ShaderManager::instance()->popShader();
Expand Down Expand Up @@ -1252,6 +1261,38 @@ GLTexture *BlurEffect::createFakeBlurTextureX11(const GLenum &textureFormat)
return compositeTexture.release();
}

QMatrix4x4 BlurEffect::colorMatrix(const float &brightness, const float &saturation, const float &contrast) const
{
QMatrix4x4 saturationMatrix;
if (saturation != 1.0) {
const qreal r = (1.0 - saturation) * .2126;
const qreal g = (1.0 - saturation) * .7152;
const qreal b = (1.0 - saturation) * .0722;

saturationMatrix = QMatrix4x4(r + saturation, r, r, 0.0,
g, g + saturation, g, 0.0,
b, b, b + saturation, 0.0,
0, 0, 0, 1.0);
}

QMatrix4x4 brightnessMatrix;
if (brightness != 1.0) {
brightnessMatrix.scale(brightness, brightness, brightness);
}

QMatrix4x4 contrastMatrix;
if (contrast != 1.0) {
const float transl = (1.0 - contrast) / 2.0;

contrastMatrix = QMatrix4x4(contrast, 0, 0, 0.0,
0, contrast, 0, 0.0,
0, 0, contrast, 0.0,
transl, transl, transl, 1.0);
}

return contrastMatrix * saturationMatrix * brightnessMatrix;
}

bool BlurEffect::isActive() const
{
return m_valid && !effects->isScreenLocked();
Expand Down
7 changes: 5 additions & 2 deletions src/blur.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public Q_SLOTS:
bool shouldForceBlur(const EffectWindow *w) const;
void updateBlurRegion(EffectWindow *w, bool geometryChanged = false);
bool hasFakeBlur(EffectWindow *w);
QMatrix4x4 colorMatrix(const float &brightness, const float &saturation, const float &contrast) const;

/*
* @param w The pointer to the window being blurred, nullptr if an image is being blurred.
Expand Down Expand Up @@ -139,6 +140,8 @@ public Q_SLOTS:
int mvpMatrixLocation;
int offsetLocation;
int halfpixelLocation;
int transformColorsLocation;
int colorMatrixLocation;
} m_downsamplePass;

struct
Expand All @@ -158,8 +161,6 @@ public Q_SLOTS:
int antialiasingLocation;
int blurSizeLocation;
int opacityLocation;


} m_upsamplePass;

struct
Expand Down Expand Up @@ -215,6 +216,8 @@ public Q_SLOTS:
// Windows to blur even when transformed.
QList<const EffectWindow*> m_blurWhenTransformed;

QMatrix4x4 m_colorMatrix;

QMap<EffectWindow *, QMetaObject::Connection> windowBlurChangedConnections;
QMap<EffectWindow *, QMetaObject::Connection> windowExpandedGeometryChangedConnections;
QMap<Output *, QMetaObject::Connection> screenChangedConnections;
Expand Down
9 changes: 9 additions & 0 deletions src/blur.kcfg
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,14 @@ class3</default>
<entry name="FakeBlurDisableWhenWindowBehind" type="Bool">
<default>true</default>
</entry>
<entry name="Saturation" type="Double">
<default>1.0</default>
</entry>
<entry name="Brightness" type="Double">
<default>1.0</default>
</entry>
<entry name="Contrast" type="Double">
<default>1.0</default>
</entry>
</group>
</kcfg>
78 changes: 77 additions & 1 deletion src/kcm/blur_config.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>480</width>
<height>200</height>
<height>240</height>
</rect>
</property>
<layout class="QVBoxLayout">
Expand Down Expand Up @@ -179,6 +179,82 @@
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>10</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QLabel">
<property name="text">
<string>Brightness</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="kcfg_Brightness">
<property name="minimum">
<double>0.0</double>
</property>
<property name="singleStep">
<double>0.1</double>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QLabel">
<property name="text">
<string>Saturation</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="kcfg_Saturation">
<property name="minimum">
<double>0.0</double>
</property>
<property name="singleStep">
<double>0.1</double>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QLabel">
<property name="text">
<string>Contrast</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="kcfg_Contrast">
<property name="minimum">
<double>0.0</double>
</property>
<property name="singleStep">
<double>0.1</double>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QWidget">
<property name="sizePolicy">
Expand Down
3 changes: 3 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ void BlurSettings::read()
general.blurStrength = BlurConfig::blurStrength() - 1;
general.noiseStrength = BlurConfig::noiseStrength();
general.windowOpacityAffectsBlur = BlurConfig::transparentBlur();
general.brightness = BlurConfig::brightness();
general.saturation = BlurConfig::saturation();
general.contrast = BlurConfig::contrast();

forceBlur.windowClasses = BlurConfig::windowClasses().split("\n");
forceBlur.windowClassMatchingMode = BlurConfig::blurMatching() ? WindowClassMatchingMode::Whitelist : WindowClassMatchingMode::Blacklist;
Expand Down
3 changes: 3 additions & 0 deletions src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ struct GeneralSettings
int blurStrength;
int noiseStrength;
bool windowOpacityAffectsBlur;
float brightness;
float saturation;
float contrast;
};

struct ForceBlurSettings
Expand Down
10 changes: 9 additions & 1 deletion src/shaders/downsample.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ uniform sampler2D texUnit;
uniform float offset;
uniform vec2 halfpixel;

uniform bool transformColors;
uniform mat4 colorMatrix;

varying vec2 uv;

void main(void)
Expand All @@ -11,6 +14,11 @@ void main(void)
sum += texture2D(texUnit, uv + halfpixel.xy * offset);
sum += texture2D(texUnit, uv + vec2(halfpixel.x, -halfpixel.y) * offset);
sum += texture2D(texUnit, uv - vec2(halfpixel.x, -halfpixel.y) * offset);
sum /= 8.0;

if (transformColors) {
sum *= colorMatrix;
}

gl_FragColor = sum / 8.0;
gl_FragColor = sum;
}
10 changes: 9 additions & 1 deletion src/shaders/downsample_core.frag
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ uniform sampler2D texUnit;
uniform float offset;
uniform vec2 halfpixel;

uniform bool transformColors;
uniform mat4 colorMatrix;

in vec2 uv;

out vec4 fragColor;
Expand All @@ -15,6 +18,11 @@ void main(void)
sum += texture(texUnit, uv + halfpixel.xy * offset);
sum += texture(texUnit, uv + vec2(halfpixel.x, -halfpixel.y) * offset);
sum += texture(texUnit, uv - vec2(halfpixel.x, -halfpixel.y) * offset);
sum /= 8.0;

if (transformColors) {
sum *= colorMatrix;
}

fragColor = sum / 8.0;
fragColor = sum;
}

0 comments on commit 2167a3f

Please sign in to comment.