CoreImage没有对图像实现过滤

时间:2016-01-17 13:26:46

标签: ios swift uiimage core-image imagefilter

我的图片第一次看起来像:

enter image description here

按下"应用过滤器"按钮变为:

enter image description here

我实现了这个ColorCube:

enter image description here

你怎么看,它实现了彩色立方体,但非常不敏感。

这是我的代码:

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

    let kDimension : UInt = 64

    let lutImage    = UIImage(named: imageName as String)!.CGImage
    let lutWidth: UInt = UInt(CGImageGetWidth(lutImage!))
    let lutHeight: UInt = UInt(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(Int(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)]) / alpha
                    let green   = Float(bitmap[Int(bitmapOffset+2)]) / alpha
                    let blue    = Float(bitmap[Int(bitmapOffset+3)]) / alpha

                    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> {
    let pixelsWide = CGImageGetWidth(inImage)
    let pixelsHigh = CGImageGetHeight(inImage)

    let bitmapBytesPerRow = Int(pixelsWide) * 4
    let bitmapByteCount = bitmapBytesPerRow * Int(pixelsHigh)
    let colorSpace = CGColorSpaceCreateDeviceRGB()

    let bitmapData = malloc(Int(CUnsignedLong(bitmapByteCount))) // bitmap
    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue | CGBitmapInfo.ByteOrder32Big.rawValue)

    let context = CGBitmapContextCreate(bitmapData, 512, 512, 8, Int(bitmapBytesPerRow), colorSpace, bitmapInfo.rawValue)

    let rect = CGRect(x:0, y:0, width:1000, height:1000)

    CGContextDrawImage(context, rect, inImage)

    var convertedBitmap = malloc(Int(bitmapByteCount * sizeof(Float)))
    print("BBB \(convertedBitmap)")
    vDSP_vfltu8(UnsafePointer<UInt8>(bitmapData), 1, UnsafeMutablePointer<Float>(convertedBitmap), 1, vDSP_Length(bitmapByteCount))

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

我从早上开始工作,什么也没做。我找不到为什么这个过滤器没有实现我的图像。

0 个答案:

没有答案