Gpuimage Custom Filters

Source: Internet
Author: User

Gpuimage Custom Filters

Gpuimage is an open source IOS framework based on GPU image and video processing. Because the GPU was used to process images and videos, it was very fast, and its author, Bradlarson, said that the processing speed on iPhone4 was 100 times times that of CPU processing (Coreimage can also use the GPU to process images, but I think coreimage is still slow )。 In addition to the speed advantage, Gpuimage also offers a lot of great image processing filters, but sometimes these basic features still do not meet the needs of actual development, do not worry about gpuimage support custom filters.

Gpuimage Custom filters need to write Fragment Shader (fragment shaders) using OpenGL Shading Language (GLSL), and you may need a little bit of knowledge about image processing. I'll try to explain how it works through the Gpuimagecolorinvertfilter (inverse color filter) in Gpuimage.

Look at the. h file First:

#import "GPUImageFilter.h"@interface GPUImageColorInvertFilter : GPUImageFilter{}@end

It's easy to see that Gpuimagecolorinvertfilter inherited Gpuimagefilter.

And look at the previous code in the. m file @implementation

NSString *const kGPUImageInvertFragmentShaderString = SHADER_STRING( varying highp vec2 textureCoordinate; uniform sampler2D inputImageTexture; void main() {    lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);    gl_FragColor = vec4((1.0 - textureColor.rgb), textureColor.a); });                                                                    

Line 1th, you can see that the SHADER_STRING macro contains our Shader (shader) code, our shader string is assigned to a const NSString object (This constant will be used to set the filter during initialization of Gpuimagefilter and its subclasses).

Lines 2nd and 3 declare two variables.

varyingVariables are data passing between Vertex and Fragment Shader (vertex shader and fragment shader), General Vertex Shader (vertex shader) modifies the value of the varying variable, and then Fragment The Shader (fragment shader) uses the value of the varying variable. Therefore, the varying variable must be consistent in Vertex and Fragment Shader declarations. Put it here, that is to say textureCoordinate the name cannot be changed.

highptextureCoordinatethe declaration precision (and the corresponding mediump and lowp ).

vec2textureCoordinateA declaration is a two-dimensional vector.

uniforminputImageTextureA declaration is a variable passed to the Shader by an external program, which can only be used inside a Shader program and cannot be changed. sampler2Ddeclaring a variable is a 2D texture.

Line 4th, I believe you are not unfamiliar, yes Shader is also starting from the main () function to execute.

Line 5th, the texture2D texture sampler, returns the value of the texture cell based on the texture coordinates.

The 6th line, (1.0 - textureColor.rgb) go to textureColor the original image of the RGB value, do a vector subtraction, which is the inverse color algorithm, and then the inverse color of the RGB value and the original Alpha value into a new VEC4 (four-dimensional vector) value to assign gl_FragColor . gl_FragColoris a Fragment Shader predefined variable, and the value assigned to it is the final color value of the fragment.

Shader here has been explained, maybe you still foggy, I say my understanding of this part of the function may not be correct, but now it is working, convenient for you to understand: Gpuimage should have a Vertex Shader, it on the image pixel-by-scan, through textureCoordinate The variable passes the current scan coordinates to our Fragment Shader, which inputImageTexture contains all the information we want to process the image, and in the Shader program we texture2D inputImageTexture use the textureCoordinate image processing knowledge to figure out what we want to do by getting the RGBA value at the current location RGBA value, assign the result value to gl_FragColor even if it is done.

Now we continue to look at the code, after Shader is the implementation of Gpuimagecolorinvertfilter:

@implementation GPUImageColorInvertFilter- (id)init;{    if (!(self = [super initWithFragmentShaderFromString:kGPUImageInvertFragmentShaderString]))    {        return nil;    }    return self;}@end     

The simple thing is to use the shader code just now to set the filter. So a new filter was born ~

The information on the internet about Gpuimage custom filters and GLSL is not particularly much, I venture to find myself in the understanding here and you share, hope to help you, if there is a mistake to point out the welcome point, in addition, you can go here to see the OpenGL GLSL document, have fun (°u°)? 」

From:http://www.itiger.me/?p=143&utm_source=tuicool

Gpuimage Custom Filters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.