Skip to content

Commit

Permalink
OpenGL2: Add sharpness
Browse files Browse the repository at this point in the history
Issue #45
  • Loading branch information
zaps166 committed Jul 12, 2016
1 parent 4106cb3 commit ca2d6cf
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
47 changes: 38 additions & 9 deletions src/modules/OpenGL2/GLSL/YCbCr.frag
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
varying vec2 vTexCoord;
uniform vec4 uVideoEq;
uniform float uSharpness;
uniform vec2 uStep;
uniform sampler2D uY, uCb, uCr;

const mat3 YUVtoRGB = mat3(
1.16430, 1.16430, 1.16430,
0.00000, -0.39173, 2.01700,
1.59580, -0.81290, 0.00000
);

float getLumaAtOffset(float x, float y)
{
return texture2D(uY, vTexCoord + vec2(x, y))[0] - 0.0625;
}

void main()
{
float brightness = uVideoEq[0];
float contrast = uVideoEq[1];
float saturation = uVideoEq[2];
vec3 contrastSaturation = vec3(
uVideoEq[1],
uVideoEq[1] * uVideoEq[2],
uVideoEq[1] * uVideoEq[2]
);

vec3 YCbCr = vec3(
texture2D(uY , vTexCoord)[0] - 0.0625,
texture2D(uCb, vTexCoord)[0] - 0.5,
texture2D(uCr, vTexCoord)[0] - 0.5
);


if (uSharpness != 0.0)
{
// Kernel 3x3
// 1 2 1
// 2 4 2
// 1 2 1
float lumaBlur = (
getLumaAtOffset(-uStep.x, -uStep.y) / 16.0 + getLumaAtOffset(0.0, -uStep.y) / 8.0 + getLumaAtOffset(uStep.x, -uStep.y) / 16.0 +
getLumaAtOffset(-uStep.x, 0.0 ) / 8.0 + getLumaAtOffset(0.0, 0.0 ) / 4.0 + getLumaAtOffset(uStep.x, 0.0 ) / 8.0 +
getLumaAtOffset(-uStep.x, uStep.y) / 16.0 + getLumaAtOffset(0.0, uStep.y) / 8.0 + getLumaAtOffset(uStep.x, uStep.y) / 16.0
);
// Subtract blur from original image, multiply and then add it to the original image
YCbCr[0] = clamp(YCbCr[0] + (YCbCr[0] - lumaBlur) * uSharpness, 0.0, 1.0);
}

/* Hue
float hueAdj = uVideoEq[3];
if (hueAdj != 0.0)
Expand All @@ -22,11 +56,6 @@ void main()
YCbCr[2] = chroma * sin(hue);
}
Hue */
YCbCr.yz *= saturation;
vec3 rgb = mat3(
1.16430, 1.16430, 1.16430,
0.00000, -0.39173, 2.01700,
1.59580, -0.81290, 0.00000
) * YCbCr * contrast + brightness;
gl_FragColor = vec4(rgb, 1.0);

gl_FragColor = vec4(clamp(YUVtoRGB * (YCbCr * contrastSaturation), 0.0, 1.0) + brightness, 1.0);
}
6 changes: 5 additions & 1 deletion src/modules/OpenGL2/OpenGL2Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ OpenGL2Common::OpenGL2Common() :
#endif
shaderProgramYCbCr(NULL), shaderProgramOSD(NULL),
texCoordYCbCrLoc(-1), positionYCbCrLoc(-1), texCoordOSDLoc(-1), positionOSDLoc(-1),
Contrast(-1), Saturation(-1), Brightness(-1), Hue(-1),
Contrast(-1), Saturation(-1), Brightness(-1), Hue(-1), Sharpness(-1),
hasPbo(false),
isPaused(false), isOK(false), hasImage(false), doReset(true), setMatrix(true),
subsX(-1), subsY(-1), W(-1), H(-1), subsW(-1), subsH(-1), outW(-1), outH(-1), verticesIdx(0),
Expand Down Expand Up @@ -324,6 +324,8 @@ void OpenGL2Common::paintGL()
{
const GLsizei w = checkLinesize(p) ? videoFrame.linesize[p] : widths[p];
const GLsizei h = heights[p];
if (p == 0)
pixelStep = QVector2D(1.0f / w, 1.0f / h);
if (hasPbo)
{
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo[p + 1]);
Expand Down Expand Up @@ -417,6 +419,8 @@ void OpenGL2Common::paintGL()
if (doReset)
{
shaderProgramYCbCr->setUniformValue("uVideoEq", Brightness, Contrast, Saturation, Hue);
shaderProgramYCbCr->setUniformValue("uSharpness", Sharpness);
shaderProgramYCbCr->setUniformValue("uStep", pixelStep);
doReset = !resetDone;
setMatrix = true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/modules/OpenGL2/OpenGL2Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ class OpenGL2Common
QOpenGLShaderProgram *shaderProgramYCbCr, *shaderProgramOSD;

qint32 texCoordYCbCrLoc, positionYCbCrLoc, texCoordOSDLoc, positionOSDLoc;
float Contrast, Saturation, Brightness, Hue;
float Contrast, Saturation, Brightness, Hue, Sharpness;
float texCoordYCbCr[8];
QVector2D pixelStep;
quint32 textures[4];

quint32 pbo[4];
Expand Down
5 changes: 4 additions & 1 deletion src/modules/OpenGL2/OpenGL2Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ OpenGL2Writer::OpenGL2Writer(Module &module) :
addParam("Brightness");
addParam("Contrast");
addParam("Hue");
addParam("Sharpness");

SetModule(module);
}
Expand Down Expand Up @@ -91,8 +92,9 @@ bool OpenGL2Writer::processParams(bool *)
const float Saturation = (getParam("Saturation").toInt() + 100) / 100.0f;
const float Brightness = getParam("Brightness").toInt() / 100.0f;
const float Hue = getParam("Hue").toInt() / -31.831f;
const float Sharpness = getParam("Sharpness").toFloat();
const int verticesIdx = rotate90 * 4 + flip;
if (drawable->aspectRatio != aspectRatio || drawable->zoom != zoom || drawable->sphericalView != spherical || drawable->verticesIdx != verticesIdx || drawable->Contrast != Contrast || drawable->Brightness != Brightness || drawable->Saturation != Saturation || drawable->Hue != Hue)
if (drawable->aspectRatio != aspectRatio || drawable->zoom != zoom || drawable->sphericalView != spherical || drawable->verticesIdx != verticesIdx || drawable->Contrast != Contrast || drawable->Brightness != Brightness || drawable->Saturation != Saturation || drawable->Hue != Hue || drawable->Sharpness != Sharpness)
{
drawable->zoom = zoom;
drawable->aspectRatio = aspectRatio;
Expand All @@ -101,6 +103,7 @@ bool OpenGL2Writer::processParams(bool *)
drawable->Brightness = Brightness;
drawable->Saturation = Saturation;
drawable->Hue = Hue;
drawable->Sharpness = Sharpness;
drawable->setSpherical(spherical);
doResizeEvent = drawable->widget()->isVisible();
}
Expand Down

0 comments on commit ca2d6cf

Please sign in to comment.