LOMO追求鮮豔色彩,隨意、自由的態度,是一種常用的濾鏡,今天介紹一下iOS 中LOMO濾鏡的實現ios
首先它有3張輸入圖像git
1.咱們要處理的圖像。即咱們要應用LOMO濾鏡的圖像github
2 app
3ui
在gpuimage中多張輸入圖像的濾鏡須要本身寫。在這裏我參照自己提供的GPUImageTwoInputFilter,本身寫了GPUImageThreeInputFilter,用於接收3張輸入圖像的濾鏡。它們都是經過濾鏡組的繼承來實現,多重濾鏡。spa
片斷着色器3d
NSString *const kFWLomofiShaderString = SHADER_STRING ( precision lowp float; varying highp vec2 textureCoordinate; uniform sampler2D inputImageTexture; uniform sampler2D inputImageTexture2; uniform sampler2D inputImageTexture3; void main() { vec3 texel = texture2D(inputImageTexture, textureCoordinate).rgb;//獲取要處理圖像的rgb值向量 vec2 red = vec2(texel.r, 0.16666); vec2 green = vec2(texel.g, 0.5); vec2 blue = vec2(texel.b, 0.83333);//使要處理的圖像和柔光混合,生成新的像素 texel.rgb = vec3( texture2D(inputImageTexture2, red).r, texture2D(inputImageTexture2, green).g, texture2D(inputImageTexture2, blue).b); //前面生成新的像素再與第二個輸入圖像的像素進行混合 //使用第三個圖像做爲暗角模板與前面的像素混合 vec2 tc = (2.0 * textureCoordinate) - 1.0; float d = dot(tc, tc); vec2 lookup = vec2(d, texel.r); texel.r = texture2D(inputImageTexture3, lookup).r; lookup.y = texel.g; texel.g = texture2D(inputImageTexture3, lookup).g; lookup.y = texel.b; texel.b = texture2D(inputImageTexture3, lookup).b;
//生成最終的LOMO效果
gl_FragColor = vec4(texel,1.0); } );
@implementation FWLomofiFilter - (id)init { if (!(self = [super init])) { return nil; } FWFilter6 *filter = [[FWFilter6 alloc] init]; [self addFilter:filter]; //設置第二個輸入圖像 UIImage *image = [UIImage imageNamed:@"lomoMap"]; imageSource1 = [[GPUImagePicture alloc] initWithImage:image]; [imageSource1 addTarget:filter atTextureLocation:1]; [imageSource1 processImage];
//設置第三個輸入圖像
UIImage *image1 = [UIImage imageNamed:@"vignetteMap"]; imageSource2 = [[GPUImagePicture alloc] initWithImage:image1]; [imageSource2 addTarget:filter atTextureLocation:2]; [imageSource2 processImage]; self.initialFilters = [NSArray arrayWithObjects:filter, nil]; self.terminalFilter = filter; return self; }
+ (UIImage *)applyLomofiFilter:(UIImage *)imagerest
{code
FWLomofiFilter *filter = [[FWLomofiFilter alloc] init];orm
[filter forceProcessingAtSize:image.size];
//第一個輸入圖像
GPUImagePicture *pic = [[GPUImagePicture alloc] initWithImage:image];
[pic addTarget:filter];
[pic processImage];
[filter useNextFrameForImageCapture];
//獲得效果圖
return [filter imageFromCurrentFramebuffer];
}
原圖
lomo效果圖
完整代碼能夠在本人的GITHUB上下載源碼!
下面是廢話
不善言辭的人進博客園首頁就這麼難,我得寫多少廢話才能進?這篇能夠嗎? 無論文字多少,你上網查查ios實現LOMO濾鏡的源碼,本濾鏡純本身琢磨,在上一代GPUImage中我經過1個多月的摸索,將多圖像濾鏡實現的。