Skip to content

Commit

Permalink
Added a zoom blur.
Browse files Browse the repository at this point in the history
  • Loading branch information
BradLarson committed Feb 12, 2013
1 parent 28c0e08 commit e64d638
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
case GPUIMAGE_MEDIAN: cell.textLabel.text = @"Median (3x3)"; break;
case GPUIMAGE_BILATERAL: cell.textLabel.text = @"Bilateral blur"; break;
case GPUIMAGE_MOTIONBLUR: cell.textLabel.text = @"Motion blur"; break;
case GPUIMAGE_ZOOMBLUR: cell.textLabel.text = @"Zoom blur"; break;
case GPUIMAGE_BOXBLUR: cell.textLabel.text = @"Box blur"; break;
case GPUIMAGE_GAUSSIAN_SELECTIVE: cell.textLabel.text = @"Gaussian selective blur"; break;
case GPUIMAGE_GAUSSIAN_POSITION: cell.textLabel.text = @"Gaussian (centered)"; break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ typedef enum {
GPUIMAGE_MEDIAN,
GPUIMAGE_BILATERAL,
GPUIMAGE_MOTIONBLUR,
GPUIMAGE_ZOOMBLUR,
GPUIMAGE_SWIRL,
GPUIMAGE_BULGE,
GPUIMAGE_PINCH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,17 @@ - (void)setupFilter;

filter = [[GPUImageMotionBlurFilter alloc] init];
}; break;
case GPUIMAGE_ZOOMBLUR:
{
self.title = @"Zoom Blur";
self.filterSettingsSlider.hidden = NO;

[self.filterSettingsSlider setMinimumValue:0.0];
[self.filterSettingsSlider setMaximumValue:2.5f];
[self.filterSettingsSlider setValue:1.0];

filter = [[GPUImageZoomBlurFilter alloc] init];
}; break;
case GPUIMAGE_UIELEMENT:
{
self.title = @"UI Element";
Expand Down Expand Up @@ -1566,7 +1577,8 @@ - (IBAction)updateFilterFromSlider:(id)sender;
case GPUIMAGE_BILATERAL: [(GPUImageBilateralFilter *)filter setDistanceNormalizationFactor:[(UISlider*)sender value]]; break;
case GPUIMAGE_FASTBLUR: [(GPUImageFastBlurFilter *)filter setBlurPasses:round([(UISlider*)sender value])]; break;
// case GPUIMAGE_FASTBLUR: [(GPUImageFastBlurFilter *)filter setBlurSize:[(UISlider*)sender value]]; break;
case GPUIMAGE_MOTIONBLUR: [(GPUImageMotionBlurFilter *)filter setBlurAngle:round([(UISlider*)sender value])]; break;
case GPUIMAGE_MOTIONBLUR: [(GPUImageMotionBlurFilter *)filter setBlurAngle:[(UISlider*)sender value]]; break;
case GPUIMAGE_ZOOMBLUR: [(GPUImageZoomBlurFilter *)filter setBlurSize:[(UISlider*)sender value]]; break;
case GPUIMAGE_OPACITY: [(GPUImageOpacityFilter *)filter setOpacity:[(UISlider *)sender value]]; break;
case GPUIMAGE_GAUSSIAN_SELECTIVE: [(GPUImageGaussianSelectiveBlurFilter *)filter setExcludeCircleRadius:[(UISlider*)sender value]]; break;
case GPUIMAGE_GAUSSIAN_POSITION: [(GPUImageGaussianBlurPositionFilter *)filter setBlurRadius:[(UISlider *)sender value]]; break;
Expand Down
8 changes: 8 additions & 0 deletions framework/GPUImage.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@
BCB79E7B15EC2A8400965D92 /* GPUImageAverageLuminanceThresholdFilter.m in Sources */ = {isa = PBXBuildFile; fileRef = BCB79E7915EC2A8300965D92 /* GPUImageAverageLuminanceThresholdFilter.m */; };
BCBC604D16C58B0900B11741 /* GPUImageMotionBlurFilter.h in Headers */ = {isa = PBXBuildFile; fileRef = BCBC604B16C58B0900B11741 /* GPUImageMotionBlurFilter.h */; };
BCBC604E16C58B0900B11741 /* GPUImageMotionBlurFilter.m in Sources */ = {isa = PBXBuildFile; fileRef = BCBC604C16C58B0900B11741 /* GPUImageMotionBlurFilter.m */; };
BCBC605716C8527C00B11741 /* GPUImageZoomBlurFilter.h in Headers */ = {isa = PBXBuildFile; fileRef = BCBC605516C8527C00B11741 /* GPUImageZoomBlurFilter.h */; };
BCBC605816C8527C00B11741 /* GPUImageZoomBlurFilter.m in Sources */ = {isa = PBXBuildFile; fileRef = BCBC605616C8527C00B11741 /* GPUImageZoomBlurFilter.m */; };
BCBCE9991595021B00E0ED33 /* GPUImageMonochromeFilter.h in Headers */ = {isa = PBXBuildFile; fileRef = BCBCE9971595021B00E0ED33 /* GPUImageMonochromeFilter.h */; };
BCBCE99A1595021B00E0ED33 /* GPUImageMonochromeFilter.m in Sources */ = {isa = PBXBuildFile; fileRef = BCBCE9981595021B00E0ED33 /* GPUImageMonochromeFilter.m */; };
BCBCE9D5159944AC00E0ED33 /* GPUImageBuffer.h in Headers */ = {isa = PBXBuildFile; fileRef = BCBCE9D3159944AB00E0ED33 /* GPUImageBuffer.h */; };
Expand Down Expand Up @@ -587,6 +589,8 @@
BCB79E7915EC2A8300965D92 /* GPUImageAverageLuminanceThresholdFilter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = GPUImageAverageLuminanceThresholdFilter.m; path = Source/GPUImageAverageLuminanceThresholdFilter.m; sourceTree = SOURCE_ROOT; };
BCBC604B16C58B0900B11741 /* GPUImageMotionBlurFilter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = GPUImageMotionBlurFilter.h; path = Source/GPUImageMotionBlurFilter.h; sourceTree = SOURCE_ROOT; };
BCBC604C16C58B0900B11741 /* GPUImageMotionBlurFilter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = GPUImageMotionBlurFilter.m; path = Source/GPUImageMotionBlurFilter.m; sourceTree = SOURCE_ROOT; };
BCBC605516C8527C00B11741 /* GPUImageZoomBlurFilter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = GPUImageZoomBlurFilter.h; path = Source/GPUImageZoomBlurFilter.h; sourceTree = SOURCE_ROOT; };
BCBC605616C8527C00B11741 /* GPUImageZoomBlurFilter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = GPUImageZoomBlurFilter.m; path = Source/GPUImageZoomBlurFilter.m; sourceTree = SOURCE_ROOT; };
BCBCE9971595021B00E0ED33 /* GPUImageMonochromeFilter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = GPUImageMonochromeFilter.h; path = Source/GPUImageMonochromeFilter.h; sourceTree = SOURCE_ROOT; };
BCBCE9981595021B00E0ED33 /* GPUImageMonochromeFilter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = GPUImageMonochromeFilter.m; path = Source/GPUImageMonochromeFilter.m; sourceTree = SOURCE_ROOT; };
BCBCE9D3159944AB00E0ED33 /* GPUImageBuffer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = GPUImageBuffer.h; path = Source/GPUImageBuffer.h; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -1060,6 +1064,8 @@
BCA464CB1627C1BC00C0F68E /* GPUImageLineGenerator.m */,
BCBC604B16C58B0900B11741 /* GPUImageMotionBlurFilter.h */,
BCBC604C16C58B0900B11741 /* GPUImageMotionBlurFilter.m */,
BCBC605516C8527C00B11741 /* GPUImageZoomBlurFilter.h */,
BCBC605616C8527C00B11741 /* GPUImageZoomBlurFilter.m */,
);
name = "Image processing";
sourceTree = "<group>";
Expand Down Expand Up @@ -1304,6 +1310,7 @@
BC185E8216B866F000EA01AD /* GPUImagePixellatePositionFilter.h in Headers */,
46A8097816B8A48E000C29ED /* GPUImageTwoInputCrossTextureSamplingFilter.h in Headers */,
BCBC604D16C58B0900B11741 /* GPUImageMotionBlurFilter.h in Headers */,
BCBC605716C8527C00B11741 /* GPUImageZoomBlurFilter.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1572,6 +1579,7 @@
46A8097916B8A48E000C29ED /* GPUImageTwoInputCrossTextureSamplingFilter.m in Sources */,
BC61F4B116B9CAEB009F6234 /* GPUImagePoissonBlendFilter.m in Sources */,
BCBC604E16C58B0900B11741 /* GPUImageMotionBlurFilter.m in Sources */,
BCBC605816C8527C00B11741 /* GPUImageZoomBlurFilter.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
3 changes: 2 additions & 1 deletion framework/Source/GPUImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,5 @@
#import "GPUImagePixellatePositionFilter.h"
#import "GPUImageTwoInputCrossTextureSamplingFilter.h"
#import "GPUImagePoissonBlendFilter.h"
#import "GPUImageMotionBlurFilter.h"
#import "GPUImageMotionBlurFilter.h"
#import "GPUImageZoomBlurFilter.h"
14 changes: 12 additions & 2 deletions framework/Source/GPUImage3x3TextureSamplingFilter.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ @implementation GPUImage3x3TextureSamplingFilter
#pragma mark -
#pragma mark Initialization and teardown

- (id)initWithFragmentShaderFromString:(NSString *)fragmentShaderString;
- (id)initWithVertexShaderFromString:(NSString *)vertexShaderString fragmentShaderFromString:(NSString *)fragmentShaderString;
{
if (!(self = [super initWithVertexShaderFromString:kGPUImageNearbyTexelSamplingVertexShaderString fragmentShaderFromString:fragmentShaderString]))
if (!(self = [super initWithVertexShaderFromString:vertexShaderString fragmentShaderFromString:fragmentShaderString]))
{
return nil;
}
Expand All @@ -66,6 +66,16 @@ - (id)initWithFragmentShaderFromString:(NSString *)fragmentShaderString;
return self;
}

- (id)initWithFragmentShaderFromString:(NSString *)fragmentShaderString;
{
if (!(self = [self initWithVertexShaderFromString:kGPUImageNearbyTexelSamplingVertexShaderString fragmentShaderFromString:fragmentShaderString]))
{
return nil;
}

return self;
}

- (void)setupFilterForSize:(CGSize)filterFrameSize;
{
if (!hasOverriddenImageSizeFactor)
Expand Down
8 changes: 0 additions & 8 deletions framework/Source/GPUImagePoissonBlendFilter.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// GPUImagePoissonBlendFilter.h
// GPUImage
//
// Created by Ian Simon on 1/29/13.
// Copyright (c) 2013 Brad Larson. All rights reserved.
//

#import "GPUImageTwoInputCrossTextureSamplingFilter.h"
#import "GPUImageFilterGroup.h"

Expand Down
8 changes: 0 additions & 8 deletions framework/Source/GPUImagePoissonBlendFilter.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// GPUImagePoissonBlendFilter.m
// GPUImage
//
// Created by Ian Simon on 1/29/13.
// Copyright (c) 2013 Brad Larson. All rights reserved.
//

#import "GPUImagePoissonBlendFilter.h"

NSString *const kGPUImagePoissonBlendFragmentShaderString = SHADER_STRING
Expand Down
8 changes: 0 additions & 8 deletions framework/Source/GPUImageTwoInputCrossTextureSamplingFilter.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// GPUImageTwoInputCrossTextureSamplingFilter.h
// GPUImage
//
// Created by Ian Simon on 1/29/13.
// Copyright (c) 2013 Brad Larson. All rights reserved.
//

#import "GPUImageTwoInputFilter.h"

@interface GPUImageTwoInputCrossTextureSamplingFilter : GPUImageTwoInputFilter
Expand Down
8 changes: 0 additions & 8 deletions framework/Source/GPUImageTwoInputCrossTextureSamplingFilter.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// GPUImageTwoInputCrossTextureSamplingFilter.m
// GPUImage
//
// Created by Ian Simon on 1/29/13.
// Copyright (c) 2013 Brad Larson. All rights reserved.
//

#import "GPUImageTwoInputCrossTextureSamplingFilter.h"

NSString *const kGPUImageTwoInputNearbyTexelSamplingVertexShaderString = SHADER_STRING
Expand Down
13 changes: 13 additions & 0 deletions framework/Source/GPUImageZoomBlurFilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#import "GPUImageFilter.h"

@interface GPUImageZoomBlurFilter : GPUImageFilter

/** A multiplier for the blur size, ranging from 0.0 on up, with a default of 1.0
*/
@property (readwrite, nonatomic) CGFloat blurSize;

/** The normalized center of the blur. (0.5, 0.5) by default
*/
@property (readwrite, nonatomic) CGPoint blurCenter;

@end
85 changes: 85 additions & 0 deletions framework/Source/GPUImageZoomBlurFilter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#import "GPUImageZoomBlurFilter.h"

NSString *const kGPUImageZoomBlurFragmentShaderString = SHADER_STRING
(
varying highp vec2 textureCoordinate;

uniform sampler2D inputImageTexture;

uniform highp vec2 blurCenter;
uniform highp float blurSize;

void main()
{
// TODO: Do a more intelligent scaling based on resolution here
highp vec2 samplingOffset = 1.0/100.0 * (blurCenter - textureCoordinate) * blurSize;

lowp vec4 fragmentColor = texture2D(inputImageTexture, textureCoordinate) * 0.18;
fragmentColor += texture2D(inputImageTexture, textureCoordinate + samplingOffset) * 0.15;
fragmentColor += texture2D(inputImageTexture, textureCoordinate + (2.0 * samplingOffset)) * 0.12;
fragmentColor += texture2D(inputImageTexture, textureCoordinate + (3.0 * samplingOffset)) * 0.09;
fragmentColor += texture2D(inputImageTexture, textureCoordinate + (4.0 * samplingOffset)) * 0.05;
fragmentColor += texture2D(inputImageTexture, textureCoordinate - samplingOffset) * 0.15;
fragmentColor += texture2D(inputImageTexture, textureCoordinate - (2.0 * samplingOffset)) * 0.12;
fragmentColor += texture2D(inputImageTexture, textureCoordinate - (3.0 * samplingOffset)) * 0.09;
fragmentColor += texture2D(inputImageTexture, textureCoordinate - (4.0 * samplingOffset)) * 0.05;

gl_FragColor = fragmentColor;
}
);

@interface GPUImageZoomBlurFilter()
{
GLint blurSizeUniform, blurCenterUniform;
}
@end

@implementation GPUImageZoomBlurFilter

@synthesize blurSize = _blurSize;
@synthesize blurCenter = _blurCenter;

#pragma mark -
#pragma mark Initialization and teardown

- (id)init;
{
if (!(self = [super initWithFragmentShaderFromString:kGPUImageZoomBlurFragmentShaderString]))
{
return nil;
}

blurSizeUniform = [filterProgram uniformIndex:@"blurSize"];
blurCenterUniform = [filterProgram uniformIndex:@"blurCenter"];

self.blurSize = 1.0;
self.blurCenter = CGPointMake(0.5, 0.5);

return self;
}

#pragma mark -
#pragma mark Accessors

- (void)setInputRotation:(GPUImageRotationMode)newInputRotation atIndex:(NSInteger)textureIndex;
{
[super setInputRotation:newInputRotation atIndex:textureIndex];
[self setBlurCenter:self.blurCenter];
}

- (void)setBlurSize:(CGFloat)newValue;
{
_blurSize = newValue;

[self setFloat:_blurSize forUniform:blurSizeUniform program:filterProgram];
}

- (void)setBlurCenter:(CGPoint)newValue;
{
_blurCenter = newValue;

CGPoint rotatedPoint = [self rotatedPoint:_blurCenter forRotation:inputRotation];
[self setPoint:rotatedPoint forUniform:blurCenterUniform program:filterProgram];
}

@end

0 comments on commit e64d638

Please sign in to comment.