2013-08-16 41 views
0

我试图链接共混层并将其过滤如何用GPUImage框架实现这些过滤器链接?

(产地 - > Texture1(不透明度30%)/ HARDLIGHT - >纹理2 /柔光)=> 水平(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]; 
+0

只是检查样品。因为他们完成了链式过滤器。运行它你会明白它。 – Rafeek

回答

3

我看到一对夫妇的问题:

1)有可能是一个错字链接文本1的过滤器:

[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]/

编辑:

与整数商设置的最小值和最大值这里当心:

[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 
+0

我尝试了你所说的,但结果真的是越野车。你认为这个链接可能被窃听吗?我编辑了上面的代码。 – klefevre

+0

你的结果是什么意思真的是越野车?编辑过的代码是否崩溃? –

+0

它不会崩溃,谢谢。结果是在显示屏上出现错误。结果图像上有一个奇怪的形状。我只是找到了原因,它是SoftLight搅拌器过滤器。无法弄清楚为什么它不起作用......我已经接受你的答案了。 – klefevre