Skip to content

Instantly share code, notes, and snippets.

@soffes
Last active July 7, 2024 08:51
Show Gist options
  • Save soffes/9361972 to your computer and use it in GitHub Desktop.
Save soffes/9361972 to your computer and use it in GitHub Desktop.
GPUImage + Core Image
//
// GPUImageTextureInput+CIImage.h
// GPUImage
//
// Created by Sam Soffes on 3/4/14.
// Copyright (c) 2014 Sam Soffes. All rights reserved.
//
#import "GPUImageTextureInput.h"
@class CIImage;
@interface GPUImageTextureInput (CIImage)
+ (instancetype)textureInputWithCIImage:(CIImage *)ciImage;
@end
//
// GPUImageTextureInput+CIImage.m
// GPUImage
//
// Created by Sam Soffes on 3/4/14.
// Copyright (c) 2014 Sam Soffes. All rights reserved.
//
#import "GPUImageTextureInput+CIImage.h"
#import <CoreImage/CoreImage.h>
@implementation GPUImageTextureInput (CIImage)
+ (instancetype)textureInputWithCIImage:(CIImage *)ciImage {
EAGLSharegroup *sharegroup = [[[GPUImageContext sharedImageProcessingContext] context] sharegroup];
EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup:sharegroup];
[EAGLContext setCurrentContext:context];
GLsizei backingWidth = ciImage.extent.size.width;
GLsizei backingHeight = ciImage.extent.size.height;
GLuint outputTexture, defaultFramebuffer;
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &outputTexture);
glBindTexture(GL_TEXTURE_2D, outputTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE1);
glGenFramebuffers(1, &defaultFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
glBindTexture(GL_TEXTURE_2D, outputTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, backingWidth, backingHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, outputTexture, 0);
NSAssert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, @"Incomplete filter FBO: %d", glCheckFramebufferStatus(GL_FRAMEBUFFER));
glBindTexture(GL_TEXTURE_2D, 0);
ciImage = [ciImage imageByApplyingTransform:CGAffineTransformConcat(CGAffineTransformMakeScale(1.0f, -1.0f), CGAffineTransformMakeTranslation(0.0f, ciImage.extent.size.height))];
CIContext *ciContext = [CIContext contextWithEAGLContext:context options:@{kCIContextWorkingColorSpace: [NSNull null]}];
[ciContext drawImage:ciImage inRect:ciImage.extent fromRect:ciImage.extent];
return [[GPUImageTextureInput alloc] initWithTexture:outputTexture size:ciImage.extent.size];
}
@end
//
// GPUImageTextureOutput+CIImage.h
// GPUImage
//
// Created by Sam Soffes on 3/4/14.
// Copyright (c) 2014 Sam Soffes. All rights reserved.
//
#import "GPUImageTextureOutput.h"
@class CIImage;
@interface GPUImageTextureOutput (CIImage)
- (CIImage *)CIImageWithSize:(CGSize)size;
@end
//
// GPUImageTextureOutput+CIImage.m
// GPUImage
//
// Created by Sam Soffes on 3/4/14.
// Copyright (c) 2014 Sam Soffes. All rights reserved.
//
#import "GPUImageTextureOutput+CIImage.h"
#import <CoreImage/CoreImage.h>
@implementation GPUImageTextureOutput (CIImage)
- (CIImage *)CIImageWithSize:(CGSize)size {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CIImage *image = [[CIImage alloc] initWithTexture:self.texture size:size flipped:YES colorSpace:colorSpace];
CGColorSpaceRelease(colorSpace);
return image;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment