koher / swift-image

SwiftImage: an image library in Swift with Swifty APIs and value semantics
MIT License
529 stars 57 forks source link

enhancement - filters Dither Atkinson, Floyd Steinberg, Burkes, Sierra, Sierra Two Row, Sierra Lite, Stucki, Jarvis Judice Ninke, Binary, Grayscale, Tint, Shading, Solarize, Invert, Gamma, Brightness, Contrast, Sepia #29

Open johndpope opened 6 years ago

johndpope commented 6 years ago

I came across this code base FilterPlay from @AfricanSwift which applies various filters to nsimage. It maybe nice to some to incorporate some of these into this library. https://github.com/AfricanSwift/FilterPlay

It maybe something where community can pitch in and add / extend

johndpope commented 6 years ago

related https://github.com/BradLarson/GPUImage2/tree/master/examples/Mac/FilterShowcase/FilterShowcase

johndpope commented 6 years ago

filters - https://github.com/johndpope/LightRoom/blob/master/LightRoom/Classes/ColorEffect.swift

also worth checking out https://github.com/muukii/Pixel

koher commented 4 years ago

Sorry for leaving this issue non-replied for a long time.

SwiftImage is designed to handle images as a data structure with value semantics rather than a convenient library with various filters.

SwiftImage provides some APIs to collaborate with other libraries/frameworks. For example, it is possible to collaborate with Core Image or Core Graphics like the code below.

// with Core Image
let image = Image<RGB<UInt8>>(uiImage: inputImage)
let outputImage: UIImage = image.withCGImage { cgImage in
    let ciImage = CIImage(cgImage: cgImage)

    let filter = CIFilter(name: "CILanczosScaleTransform")!
    filter.setValue(ciImage, forKey: kCIInputImageKey)
    let scale = NSNumber(value: 2736.0 / Double(cropped.width))
    filter.setValue(scale, forKey: kCIInputScaleKey)
    filter.setValue(NSNumber(value: 1.0 as Float), forKey: kCIInputAspectRatioKey)
    let output = filter.value(forKey: kCIOutputImageKey) as! CIImage
    let context = CIContext(options: [.useSoftwareRenderer: false])
    return UIImage(cgImage: context.createCGImage(output, from: output.extent)!)
}
// with Core Graphics
var image = Image<PremultipliedRGBA<UInt8>>(uiImage: imageView.image!)
image.withCGContext { context in
    context.setLineWidth(1)
    context.setStrokeColor(UIColor.red.cgColor)
    context.move(to: CGPoint(x: -1, y: -1))
    context.addLine(to: CGPoint(x: 640, y: 480))
    context.strokePath()
}
imageView.image = image.uiImage

Also you can access to raw buffers using the withUnsafeBufferPointer or withUnsafeMutableBufferPointer methods.

image.withUnsafeBufferPointer { pointer in
    ...
}

It would make it possible to collaborate with various libraries/frameworks.