ios - 使用 CIFilter 对 Photoshop LUT 滤镜进行颜色校正

标签 ios swift image-processing pixel core-image

使用photoshop创建LUT滤镜,使用iOS CIFilter读取LUT图像,iOS创建的滤镜图像与photoshop创建的filer图像不对应。

我如何追踪问题?
enter image description here

这是原图
enter image description here

这是我从 Photoshop 创建的带有滤镜的图像
enter image description here

这是我从 iPhone 创建的带有过滤器的图像
enter image description here

这是我正在使用的 LUT 图像
enter image description here

最佳答案

请试试这对我有用

public class LUTsHelper {

public static func applyLUTsFilter(lutImage: String, dimension: Int, colorSpace: CGColorSpace) -> CIFilter? {

    guard let image = UIImage(named: lutImage) else {
        return nil
    }

    guard let cgImage = image.cgImage else {
        return nil
    }

    guard let bitmap = createBitmap(image: cgImage, colorSpace: colorSpace) else {
        return nil
    }

    let width = cgImage.width
    let height = cgImage.height
    let rowNum = width / dimension
    let columnNum = height / dimension

    let dataSize = dimension * dimension * dimension * MemoryLayout<Float>.size * 4

    var array = Array<Float>(repeating: 0, count: dataSize)

    var bitmapOffest: Int = 0
    var z: Int = 0

    for _ in stride(from: 0, to: rowNum, by: 1) {
        for y in stride(from: 0, to: dimension, by: 1) {
            let tmp = z
            for _ in stride(from: 0, to: columnNum, by: 1) {
                for x in stride(from: 0, to: dimension, by: 1) {

                    let dataOffset = (z * dimension * dimension + y * dimension + x) * 4

                    let position = bitmap
                        .advanced(by: bitmapOffest)

                    array[dataOffset + 0] = Float(position
                        .advanced(by: 0)
                        .pointee) / 255

                    array[dataOffset + 1] = Float(position
                        .advanced(by: 1)
                        .pointee) / 255

                    array[dataOffset + 2] = Float(position
                        .advanced(by: 2)
                        .pointee) / 255

                    array[dataOffset + 3] = Float(position
                        .advanced(by: 3)
                        .pointee) / 255

                    bitmapOffest += 4

                }
                z += 1
            }
            z = tmp
        }
        z += columnNum
    }

    free(bitmap)

    let data = Data.init(bytes: array, count: dataSize)

    guard
    let cubeFilter = CIFilter(name: "CIColorCubeWithColorSpace")
    else {
        return nil
    }

    cubeFilter.setValue(dimension, forKey: "inputCubeDimension")
    cubeFilter.setValue(data, forKey: "inputCubeData")
    cubeFilter.setValue(colorSpace, forKey: "inputColorSpace")

    return cubeFilter

}

private static func createBitmap(image: CGImage, colorSpace: CGColorSpace) -> UnsafeMutablePointer<UInt8>? {

    let width = image.width
    let height = image.height

    let bitsPerComponent = 8
    let bytesPerRow = width * 4

    let bitmapSize = bytesPerRow * height

    guard let data = malloc(bitmapSize) else {
        return nil
    }

    guard let context = CGContext(
        data: data,
        width: width,
        height: height,
        bitsPerComponent: bitsPerComponent,
        bytesPerRow: bytesPerRow,
        space: colorSpace,
        bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue,
        releaseCallback: nil,
        releaseInfo: nil) else {
            return nil
    }

    context.draw(image, in: CGRect(x: 0, y: 0, width: width, height: height))

    return data.bindMemory(to: UInt8.self, capacity: bitmapSize)
}}

现在我们这个类
        let colorSpace: CGColorSpace = CGColorSpace.init(name: CGColorSpace.sRGB) ?? CGColorSpaceCreateDeviceRGB()
        let lutFilter = LUTsHelper.applyLUTsFilter(lutImage: "demo.png", dimension: 64, colorSpace: colorSpace)
        lutFilter?.setValue(outputImage, forKey: "inputImage")

        let lutOutputImage = lutFilter?.outputImage

        if let output = lutOutputImage {
            outputImage = output
        }

关于ios - 使用 CIFilter 对 Photoshop LUT 滤镜进行颜色校正,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31110593/

相关文章:

ios - 从 Xcode 构建中删除命令

java - 渐进式图像渲染,逐像素加载图像并在 imageLoader 中更新 ImageView

java - 一堆图像的 ImageJ 亮度和对比度

ios - 多次将同一个 TableViewController 插入导航堆栈

ios - 在 swift 3 中将多个 NSMutableArray 数据追加到另一个 NSMutableArray

ios - Swift 自定义 uitableViewCell

iOS/Swift/CoreBluetooth Ble 通知不工作

python - opencv 通过关键点与拉伸(stretch)对齐两个图像

javascript - ios 上的位置警报

ios - SpriteKit 是否支持 sprite/texture/shape 的密集镶嵌,以便可以自由变形?