RGB rotation

This commit is contained in:
Jonas Kulla 2016-10-04 15:16:57 +02:00
parent 0ec1fce4ac
commit 89bb3e85fd
5 changed files with 31 additions and 55 deletions

View File

@ -1,48 +1,12 @@
uniform sampler2D inputTexture;
uniform float hueAdjust;
uniform sampler2D texture;
uniform lowp mat3 rotationMat;
varying vec2 v_texCoord;
void main ()
{
const vec4 kRGBToYPrime = vec4 (0.299, 0.587, 0.114, 0.0);
const vec4 kRGBToI = vec4 (0.596, -0.275, -0.321, 0.0);
const vec4 kRGBToQ = vec4 (0.212, -0.523, 0.311, 0.0);
vec4 color = texture2D (texture, v_texCoord.xy);
const vec4 kYIQToR = vec4 (1.0, 0.956, 0.621, 0.0);
const vec4 kYIQToG = vec4 (1.0, -0.272, -0.647, 0.0);
const vec4 kYIQToB = vec4 (1.0, -1.107, 1.704, 0.0);
/* Sample the input pixel */
vec4 color = texture2D (inputTexture, v_texCoord.xy);
/* Convert to YIQ */
float YPrime = dot (color, kRGBToYPrime);
float I = dot (color, kRGBToI);
float Q = dot (color, kRGBToQ);
/* Calculate the hue and chroma */
float hue = atan (Q, I);
float chroma = sqrt (I * I + Q * Q);
/* Make the user's adjustments */
hue += hueAdjust;
/* Remember old I and color */
float IOriginal = I;
vec4 coOriginal = color;
/* Convert back to YIQ */
Q = chroma * sin (hue);
I = chroma * cos (hue);
/* Convert back to RGB */
vec4 yIQ = vec4 (YPrime, I, Q, 0.0);
color.r = dot (yIQ, kYIQToR);
color.g = dot (yIQ, kYIQToG);
color.b = dot (yIQ, kYIQToB);
/* Save the result */
gl_FragColor = (IOriginal == 0.0) ? coOriginal : color;
gl_FragColor = vec4(rotationMat * color.rgb, color.a);
}

View File

@ -29,6 +29,8 @@
#include <pixman.h>
#include <math.h>
#include "gl-util.h"
#include "gl-meta.h"
#include "quad.h"
@ -854,13 +856,28 @@ void Bitmap::hueChange(int hue)
quad.setTexPosRect(texRect, texRect);
quad.setColor(Vec4(1, 1, 1, 1));
/* Calculate hue parameter */
/* Calculate hue rotation matrix */
hue = wrapRange(hue, 0, 359);
float hueAdj = -((M_PI * 2) / 360) * hue;
float hueRad = (M_PI / 180.0f) * hue;
float x = cos(hueRad);
float y = sin(hueRad);
float a = (1.0f - x) / 3.0f;
float b = sqrt(1.0f / 3.0f) * y;
float mat[9];
mat[0] = x + a;
mat[1] = a + b;
mat[2] = a - b;
mat[3] = mat[2];
mat[4] = mat[0];
mat[5] = mat[1];
mat[6] = mat[1];
mat[7] = mat[2];
mat[8] = mat[0];
HueShader &shader = shState->shaders().hue;
shader.bind();
shader.setHueAdjust(hueAdj);
shader.setRotationMat(mat);
FBO::bind(newTex.fbo);
p->pushSetViewport(shader);

View File

@ -90,6 +90,7 @@ typedef void (APIENTRYP _PFNGLUNIFORM1FPROC) (GLint location, GLfloat v0);
typedef void (APIENTRYP _PFNGLUNIFORM2FPROC) (GLint location, GLfloat v0, GLfloat v1);
typedef void (APIENTRYP _PFNGLUNIFORM4FPROC) (GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);
typedef void (APIENTRYP _PFNGLUNIFORM1IPROC) (GLint location, GLint v0);
typedef void (APIENTRYP _PFNGLUNIFORMMATRIX3FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value);
typedef void (APIENTRYP _PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value);
/* Vertex attribute */
@ -174,6 +175,7 @@ typedef void (APIENTRYP _PFNGLRELEASESHADERCOMPILERPROC) (void);
GL_FUN(Uniform2f, _PFNGLUNIFORM2FPROC) \
GL_FUN(Uniform4f, _PFNGLUNIFORM4FPROC) \
GL_FUN(Uniform1i, _PFNGLUNIFORM1IPROC) \
GL_FUN(UniformMatrix3fv, _PFNGLUNIFORMMATRIX3FVPROC) \
GL_FUN(UniformMatrix4fv, _PFNGLUNIFORMMATRIX4FVPROC) \
/* Vertex attribute */ \
GL_FUN(BindAttribLocation, _PFNGLBINDATTRIBLOCATIONPROC) \

View File

@ -550,18 +550,12 @@ HueShader::HueShader()
ShaderBase::init();
GET_U(hueAdjust);
GET_U(inputTexture);
GET_U(rotationMat);
}
void HueShader::setHueAdjust(float value)
void HueShader::setRotationMat(const float value[9])
{
gl.Uniform1f(u_hueAdjust, value);
}
void HueShader::setInputTexture(TEX::ID tex)
{
setTexUniform(u_inputTexture, 0, tex);
gl.UniformMatrix3fv(u_rotationMat, 1, GL_FALSE, value);
}

View File

@ -240,11 +240,10 @@ class HueShader : public ShaderBase
public:
HueShader();
void setHueAdjust(float value);
void setInputTexture(TEX::ID tex);
void setRotationMat(const float value[9]);
private:
GLint u_hueAdjust, u_inputTexture;
GLint u_rotationMat;
};
class SimpleMatrixShader : public ShaderBase