-
-
Save yogox/2405050f262ef362a378328792129135 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CIBlendWithMatte : CIFilter { | |
var inputImage: CIImage? | |
var backgroundImage: CIImage? | |
var color: CIVector? | |
override var outputImage: CIImage? { | |
guard let inputImage = inputImage | |
, let backgroundImage = backgroundImage | |
, let color = color else { return nil} | |
// 写真に合わせてMatte画像のスケールを拡大 | |
let scaleFilter = CIFilter.lanczosScaleTransform() | |
let matteHeight = inputImage.extent.height | |
let photoHeight = backgroundImage.extent.height | |
scaleFilter.inputImage = inputImage | |
scaleFilter.scale = Float(photoHeight / matteHeight) | |
scaleFilter.aspectRatio = 1.0 | |
// マット画像の色・アルファを変更 | |
let colorFilter = CIFilter.colorClamp() | |
colorFilter.inputImage = scaleFilter.outputImage! | |
colorFilter.maxComponents = color | |
// Matte画像自身をマスクにして写真と合成 | |
let blendFilter = CIFilter.blendWithMask() | |
blendFilter.inputImage = colorFilter.outputImage! | |
blendFilter.backgroundImage = backgroundImage | |
blendFilter.maskImage = scaleFilter.outputImage! | |
return blendFilter.outputImage! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment