Search code examples
iosswiftimage-processinggpuimage

Generate image with GPUImagePerlinNoiseFilter


I'm new in GPUImage. I want to create a noisy background at given size. Seems like I should use GPUImagePerlinNoiseFilter, but I have no idea how to generate an image.

let noiseFilter = GPUImagePerlinNoiseFilter()
noiseFilter.colorStart = GPUVector4(1, 1, 1, 0)
noiseFilter.colorStart = GPUVector4(1, 1, 1, 1)
noiseFilter.scale = 0.5

What should I do next?


Solution

  • In provided noise generator they use input image too (you set size and color), so I think I'm correct. Here is my solution:

    import UIKit
    
    import GPUImage
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let imageView = UIImageView(frame: CGRectMake(10, 50, 100, 100))
            imageView.image = createNoiseImage(CGSizeMake(100, 100), color: UIColor.whiteColor())
            view.addSubview(imageView)
        }
    
        private func createNoiseImage(size: CGSize, color: UIColor) -> UIImage {
            UIGraphicsBeginImageContext(size)
            let context = UIGraphicsGetCurrentContext()
    
            CGContextSetFillColorWithColor(context, color.CGColor)
            CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height))
    
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext();
    
            let filter = GPUImagePerlinNoiseFilter()
            return filter.imageByFilteringImage(image)
        }
    }
    

    With default filter parameters it produces this:

    enter image description here