How to achieve those filter chaining with GPUImage framework?

纵然是瞬间 提交于 2020-01-11 07:49:08

问题


I'm trying to chaining blended layer and filter it

(Origin -> Texture1(opacity 30%)/HardLight -> Texture2/SoftLight) => level(45, 0.95, 238) + saturation(-100) + hue(+42)

Here is what I tried :

Edited: This code works below, thanks for answer

// Textures
GPUImagePicture *origin = [[GPUImagePicture alloc] initWithImage:originImage smoothlyScaleOutput:NO];
GPUImagePicture *text1 = [[GPUImagePicture alloc] initWithImage:[UIImage imageNamed:@"filter_landscape_vintage_1.png"] smoothlyScaleOutput:NO];
GPUImagePicture *text2 = [[GPUImagePicture alloc] initWithImage:[UIImage imageNamed:@"filter_landscape_vintage_2.png"] smoothlyScaleOutput:NO];

// Blend filters
GPUImageHardLightBlendFilter *blendFilter1 = [[GPUImageHardLightBlendFilter alloc] init];
GPUImageSoftLightBlendFilter *blendFilter2 = [[GPUImageSoftLightBlendFilter alloc] init];

// Color filters
GPUImageOpacityFilter *filter1 = [[GPUImageOpacityFilter alloc] init];
[filter1 setOpacity:0.3];
GPUImageLevelsFilter *filter2 = [[GPUImageLevelsFilter alloc] init];
[filter2 setMin:45.0/255.0 gamma:0.95 max:238.0/255.0]; // 45, 0.95, 238
GPUImageSaturationFilter *filter3 = [[GPUImageSaturationFilter alloc] init];
[filter3 setSaturation:0.0];
GPUImageHueFilter *filter4 = [[GPUImageHueFilter alloc] init];
[filter4 setHue:42.0];

// Texture1(opacity 30%)/HardLight
[text1 addTarget:filter1]; // Opacity
[filter1 addTarget:blendFilter1]; // HardLight Blend

// Texture2/SoftLight
[text2 addTarget:blendFilter2]; // SoftLight Blend

// Chain Origin + Texture1 + Texture2
[origin addTarget:blendFilter1];
[blendFilter1 addTarget:blendFilter2];

// Result => level + saturation + hue
[blendFilter2 addTarget:filter2];
[filter2 addTarget:filter3];
[filter3 addTarget:filter4];

// Processing
[origin processImage];
[text1 processImage];
[text2 processImage];

UIImage *output = [filter4 imageFromCurrentlyProcessedOutput];

回答1:


I see a couple of problems:

1) There's probably a typo chaining text1's filters:

[text1 addTarget:filter1]; // Opacity
[text1 addTarget:blendFilter1]; // HardLight Blend

should instead be

[text1 addTarget:filter1]; // Opacity
[filter1 addTarget:blendFilter1]; // HardLight Blend

2) You're chaining filters to text1 and text2 GPUImagePictures but forgot to process them:

// Processing
[origin processImage];
[text1 processImage];
[text2 processImage];

3) UIImage *output = [blendFilter2 imageFromCurrentlyProcessedOutput];

You should call imageFromCurrentlyProcessedOutput on the last filter of the chain which in your case is the group filter. I wouldn't necessary use the GPUImageFilterGroup here which is usually used to create filter subclasses that use existing filters, but instead I simply would chain the last 3 filters to blendFilter2 like this:

...
// Result => level + saturation + hue
[blendFilter2 addTarget:filter2];
[filter2 addTarget:filter3];
[filter3 addTarget:filter4];

// Processing
[origin processImage];
[text1 processImage];
[text2 processImage];

UIImage *output = [filter4 imageFromCurrentlyProcessedOutput];

The full chain would be:

[text1] -> [filter1] \ 
                      +->  [blend1]  \
            [origin] /                +-> [blend2] -> [filter2] -> [filter3] -> [filter4]
                             [text2] / 

EDIT:

Watch out with those integer divisions setting min and max here:

[filter2 setMin:45/255 gamma:0.95 max:238/255]; // 45, 0.95, 238

min and max are 0!

[filter2 setMin:45 / 255.0 gamma:0.95 max:238 / 255.0]; // 45, 0.95, 238


来源:https://stackoverflow.com/questions/18265292/how-to-achieve-those-filter-chaining-with-gpuimage-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!