Skip to content

Instantly share code, notes, and snippets.

@wjlafrance
Last active December 15, 2015 08:29
Show Gist options
  • Save wjlafrance/5231009 to your computer and use it in GitHub Desktop.
Save wjlafrance/5231009 to your computer and use it in GitHub Desktop.
//
// WJLSideBySideToAnaglyphFilter.h
//
// Copyright (c) 2013 William LaFrance
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#import <QuartzCore/CoreImage.h>
@interface WJLSideBySideToAnaglyphFilter : CIFilter {
CIImage *inputImage;
}
@end
//
// WJLSideBySideToAnaglyphFilter.m
//
// Copyright (c) 2013 William LaFrance
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#define STRINGIZE(x) #x
#define STRINGIZE2(x) STRINGIZE(x)
#define KERNEL_STRING(text) @STRINGIZE2(text)
#import "WJLSideBySideToAnaglyphFilter.h"
static CIKernel *filterKernel = nil;
NSString *kernelSource() {
return KERNEL_STRING(
kernel vec4 filterKernel(sampler image) {
vec2 size = samplerSize(image);
vec2 coord = samplerCoord(image);
// Without these two lines, the anaglyph will be centered in the
// frame at 1/2 width. I played with numbers until it worked, to be
// honest.
coord.x += size.x / 2.0;
coord.x /= 2.0;
vec4 leftPixel = sample(image, vec2(coord.x - size.x / 4.0, coord.y));
vec4 rightPixel = sample(image, vec2(coord.x + size.x / 4.0, coord.y));
float red = 0.7 * leftPixel.g + 0.3 * leftPixel.b;
return vec4(red, rightPixel.g, rightPixel.b, 1);
}
);
}
@implementation WJLSideBySideToAnaglyphFilter
+ (void)initialize
{
[CIFilter registerFilterName:@"WJLSideBySideToAnaglyphFilter"
constructor:(id<CIFilterConstructor>) self
classAttributes:@{
kCIAttributeFilterDisplayName:@"3D side by side to anaglyph conversion filter",
kCIAttributeFilterCategories:@[kCICategoryVideo, kCICategoryColorAdjustment]
}];
}
- (id)init
{
self = [super init];
if (self) {
if (!filterKernel) {
NSArray *kernels = [CIKernel kernelsWithString:kernelSource()];
filterKernel = kernels[0];
}
}
return self;
}
- (CIImage *)outputImage
{
CIImage *image = [self apply:filterKernel, inputImage, nil];
return image;
}
+ (CIFilter *)filterWithName:(NSString *)name
{
return self.new;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment