LoginSignup
21
20

More than 5 years have passed since last update.

GPUImageの連結をわかりやすくするオペレータを定義する

Posted at

GPUImageの問題点

addTargetというメソッドがなんか分かりにくい。
具体的には以下のようになっている。

GPUImageVideoCamera *videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;

GPUImageFilter *customFilter = [[GPUImageFilter alloc] initWithFragmentShaderFromFile:@"CustomShader"];
GPUImageView *filteredVideoView = [[GPUImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, viewWidth, viewHeight)];

// Add the view somewhere so it's visible

[videoCamera addTarget:customFilter];
[customFilter addTarget:filteredVideoView];

[videoCamera startCameraCapture];

https://github.com/BradLarson/GPUImage#filtering-live-video より引用

videoCameraに対してcustomFilterをaddTargetしてそのcustomFilterに対してfilteredVideoViewをaddTargetしている。
わかればどうってことはなくて、

スクリーンショット 2016-01-28 12.34.57.png

という感じになっている。
ただ、この図をコード上で非常に想像しにくい。
特に途中で分岐した場合や合流した場合に非常にごちゃごちゃする

そこで…


infix operator => { associativity left precedence 80 }

func => <T where T:GPUImageInput, T:GPUImageOutput>(left: GPUImageOutput,right: T) -> GPUImageOutput {
  left.addTarget(right)
  return right
}
func => <T:GPUImageInput>(left: GPUImageOutput,right: T) {
  left.addTarget(right)
}

を定義する。
仕組みは簡単で、=>オペレータでaddTargetし、GPUImageOutputが返される場合は連結出来るようにするというもの。これさえあれば、

スクリーンショット 2016-01-28 12.45.00.png
http://sssslide.com/www.slideshare.net/kasajei/for-saminar123mtliphonecameran より引用

のような複雑なフィルタリングも

スクリーンショット 2016-01-28 12.45.25.png

このように図のまんまコードを組む事ができる。

最高

21
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
20