如何用GPUImage框架实现那些过滤器链接?

时间:2013-08-16 03:19:37

标签: iphone ios objective-c ipad gpuimage

我正在尝试链接混合层并过滤它

  

(原点 - > Texture1(不透明度30%)/ HardLight - > Texture2 / SoftLight)=>   水平(45,0.95,238)+饱和度(-100)+色调(+42)

以下是我的尝试:

已编辑:此代码适用于以下内容,感谢您的回答

// 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 个答案:

答案 0 :(得分:3)

我看到了几个问题:

1)可能有一个拼写错误的text1过滤器:

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

应该是

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

2)您正在将过滤器链接到text1text2 GPUImagePictures但忘记处理它们:

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

3)UIImage *output = [blendFilter2 imageFromCurrentlyProcessedOutput];

您应该在链的最后一个过滤器上调用imageFromCurrentlyProcessedOutput,在您的情况下是group过滤器。我不必使用GPUImageFilterGroup这里通常用于创建使用现有过滤器的过滤器子类,但我只是将最后3个过滤器链接到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];

完整链将是:

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

修改

注意这里设置min和max的整数除法:

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

最小值和最大值为0!

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