使用CIFilter校正Photoshop LUT滤镜的颜色

问题描述 投票:1回答:1

使用photoshop创建LUT过滤器并使用iOS CIFilter读取LUT图像,iOS创建的过滤器图像与photoshop创建的文件图像不对应。

如何查找问题?“在此处输入图像描述”

这是原始图像“在此处输入图像描述”

这是我从photoshop中创建的带有滤镜的图像“在此处输入图像描述”

这是我从iPhone创建的带有过滤器的图像“在此处输入图像描述”

这是我正在使用的LUT图片“在此处输入图像描述”

ios swift image-processing pixel core-image
1个回答
0
投票

请尝试一下,这对我有用

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
        }
© www.soinside.com 2019 - 2024. All rights reserved.