如何将LUT png用于CIColorCube滤镜?

2024-05-05

我想使用查找表 png (example http://nghiatran.me/wp-content/uploads/2014/06/FilterMe_Part2_ProcessedLUT.png)作为颜色立方体数据CIColorCubeSwift 中的过滤器。到目前为止,我尝试(和发现)的所有示例都是计算颜色立方体的示例,如下所示这个例子 https://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_filer_recipes/ci_filter_recipes.html.

如何读取 png 作为查找数据?


我现在用的是this https://github.com/NghiaTranUIT/FeSlideFilter and this https://github.com/imgly/imgly-sdk-ios项目以适应 Swift 的 Objective-C 实现:

func colorCubeFilterFromLUT(imageName : NSString) -> CIFilter? {

    let kDimension : UInt = 64

    let lutImage    = UIImage(named: imageName)!.CGImage
    let lutWidth    = CGImageGetWidth(lutImage!)
    let lutHeight   = CGImageGetHeight(lutImage!)
    let rowCount    = lutHeight / kDimension
    let columnCount = lutWidth / kDimension

    if ((lutWidth % kDimension != 0) || (lutHeight % kDimension != 0) || (rowCount * columnCount != kDimension)) {

        NSLog("Invalid colorLUT %@", imageName);
        return nil
    }

    let bitmap  = self.createRGBABitmapFromImage(lutImage)
    let size    = Int(kDimension) * Int(kDimension) * Int(kDimension) * sizeof(Float) * 4
    let data    = UnsafeMutablePointer<Float>(malloc(UInt(size)))

    var bitmapOffset : Int = 0
    var z : UInt = 0


    for (var row: UInt = 0; row < rowCount; row++)
    {
        for (var y: UInt = 0; y < kDimension; y++)
        {
            var tmp = z
            for (var col: UInt = 0; col < columnCount; col++)
            {
                for (var x: UInt = 0; x < kDimension; x++) {

                    let alpha   = Float(bitmap[Int(bitmapOffset)]) / 255.0
                    let red     = Float(bitmap[Int(bitmapOffset+1)]) / 255.0
                    let green   = Float(bitmap[Int(bitmapOffset+2)]) / 255.0
                    let blue    = Float(bitmap[Int(bitmapOffset+3)]) / 255.0

                    var dataOffset = Int(z * kDimension * kDimension + y * kDimension + x) * 4

                    data[dataOffset] = red
                    data[dataOffset + 1] = green
                    data[dataOffset + 2] = blue
                    data[dataOffset + 3] = alpha
                    bitmapOffset += 4
                }
                z++
            }
            z = tmp
        }
        z += columnCount
    }

    let colorCubeData = NSData(bytesNoCopy: data, length: size, freeWhenDone: true)

    // create CIColorCube Filter
    var filter = CIFilter(name: "CIColorCube")
    filter.setValue(colorCubeData, forKey: "inputCubeData")
    filter.setValue(kDimension, forKey: "inputCubeDimension")

    return filter
}


func createRGBABitmapFromImage(inImage: CGImage) -> UnsafeMutablePointer<Float> {

    //Get image width, height
    let pixelsWide = CGImageGetWidth(inImage) 
    let pixelsHigh = CGImageGetHeight(inImage) 

    // Declare the number of bytes per row. Each pixel in the bitmap in this
    // example is represented by 4 bytes; 8 bits each of red, green, blue, and
    // alpha.
    let bitmapBytesPerRow = Int(pixelsWide) * 4 
    let bitmapByteCount = bitmapBytesPerRow * Int(pixelsHigh)

    // Use the generic RGB color space.
    let colorSpace = CGColorSpaceCreateDeviceRGB()

    // Allocate memory for image data. This is the destination in memory
    // where any drawing to the bitmap context will be rendered.
    let bitmapData = malloc(CUnsignedLong(bitmapByteCount)) // bitmap
    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue) 

    // Create the bitmap context. We want pre-multiplied RGBA, 8-bits
    // per component. Regardless of what the source image format is
    // (CMYK, Grayscale, and so on) it will be converted over to the format
    // specified here by CGBitmapContextCreate.
    let context = CGBitmapContextCreate(bitmapData, pixelsWide, pixelsHigh, 8, UInt(bitmapBytesPerRow), colorSpace, bitmapInfo)


    let rect = CGRect(x:0, y:0, width:Int(pixelsWide), height:Int(pixelsHigh))        

    // Draw the image to the bitmap context. Once we draw, the memory
    // allocated for the context for rendering will then contain the
    // raw image data in the specified color space.
    CGContextDrawImage(context, rect, inImage)

    // Now we can get a pointer to the image data associated with the bitmap
    // context.
    // var data = CGBitmapContextGetData(context)
    // var dataType = UnsafeMutablePointer<Float>(data)
    // return dataType


    var convertedBitmap = malloc(UInt(bitmapByteCount * sizeof(Float)))
    vDSP_vfltu8(UnsafePointer<UInt8>(bitmapData), 1, UnsafeMutablePointer<Float>(convertedBitmap), 1, vDSP_Length(bitmapByteCount))

    free(bitmapData)
    return UnsafeMutablePointer<Float>(convertedBitmap)
}

另请参阅this https://stackoverflow.com/questions/24049313/how-do-i-load-and-edit-a-bitmap-file-at-the-pixel-level-in-swift-for-ios回答。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将LUT png用于CIColorCube滤镜? 的相关文章

随机推荐