forked from BradLarson/GPUImage
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Cameron
committed
Jul 21, 2012
1 parent
8c351ac
commit 6566040
Showing
6 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
#import "GPUImageFilter.h" | ||
|
||
@interface GPUImageHueFilter : GPUImageFilter | ||
{ | ||
GLfloat hueAdjustUniform; | ||
|
||
} | ||
@property (nonatomic, readwrite) CGFloat hue; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
|
||
#import "GPUImageHueFilter.h" | ||
|
||
// Adapted from http://stackoverflow.com/questions/9234724/how-to-change-hue-of-a-texture-with-glsl - see for code and discussion | ||
NSString *const kGPUImageHueFragmentShaderString = SHADER_STRING | ||
( | ||
precision highp float; | ||
varying highp vec2 textureCoordinate; | ||
|
||
uniform sampler2D inputImageTexture; | ||
uniform mediump float hueAdjust; | ||
const highp vec4 kRGBToYPrime = vec4 (0.299, 0.587, 0.114, 0.0); | ||
const highp vec4 kRGBToI = vec4 (0.595716, -0.274453, -0.321263, 0.0); | ||
const highp vec4 kRGBToQ = vec4 (0.211456, -0.522591, 0.31135, 0.0); | ||
|
||
const highp vec4 kYIQToR = vec4 (1.0, 0.9563, 0.6210, 0.0); | ||
const highp vec4 kYIQToG = vec4 (1.0, -0.2721, -0.6474, 0.0); | ||
const highp vec4 kYIQToB = vec4 (1.0, -1.1070, 1.7046, 0.0); | ||
|
||
void main () | ||
{ | ||
// Sample the input pixel | ||
highp vec4 color = texture2D(inputImageTexture, textureCoordinate); | ||
|
||
// Convert to YIQ | ||
highp float YPrime = dot (color, kRGBToYPrime); | ||
highp float I = dot (color, kRGBToI); | ||
highp float Q = dot (color, kRGBToQ); | ||
|
||
// Calculate the hue and chroma | ||
highp float hue = atan (Q, I); | ||
highp float chroma = sqrt (I * I + Q * Q); | ||
|
||
// Make the user's adjustments | ||
hue += (-hueAdjust); //why negative rotation? | ||
|
||
// Convert back to YIQ | ||
Q = chroma * sin (hue); | ||
I = chroma * cos (hue); | ||
|
||
// Convert back to RGB | ||
highp 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 = color; | ||
} | ||
); | ||
|
||
|
||
@implementation GPUImageHueFilter | ||
@synthesize hue; | ||
|
||
-(id) init { | ||
if(! (self = [super initWithFragmentShaderFromString:kGPUImageHueFragmentShaderString]) ){ | ||
return nil; | ||
} | ||
|
||
hueAdjustUniform = [filterProgram uniformIndex:@"hueAdjust"]; | ||
self.hue = 90; | ||
|
||
return self; | ||
} | ||
|
||
-(void) setHue:(CGFloat)newHue { | ||
hue = fmodf(newHue, 360.0); | ||
[GPUImageOpenGLESContext useImageProcessingContext]; | ||
[filterProgram use]; | ||
glUniform1f(hueAdjustUniform, hue); | ||
|
||
} | ||
|
||
|
||
@end |