Skip to content

Instantly share code, notes, and snippets.

@yogox
Created October 6, 2020 06:44
Show Gist options
  • Save yogox/2405050f262ef362a378328792129135 to your computer and use it in GitHub Desktop.
Save yogox/2405050f262ef362a378328792129135 to your computer and use it in GitHub Desktop.
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