本文介绍了如何使用LUT png进行CIColorCube过滤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用查找表png(。

I would like to use a lookup table png (example) as color cube data for the CIColorCube filter in Swift. All I tried (and found) so far are examples with a computed color cube as in this example.

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

推荐答案

我现在用和项目,以调整他们对Swift的Objective-C实现:

I now used this and this project to adapt their Objective-C implementation for Swift:

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)
}

另见答案。

这篇关于如何使用LUT png进行CIColorCube过滤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 10:01