Advertisement
iamalizade

LUT

Jan 17th, 2016
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.63 KB | None | 0 0
  1. @IBAction func applyTheFilter(sender: AnyObject) {
  2.         let image: UIImage = UIImage(named: "me.jpg")!
  3.        
  4.         let ciImage = CIImage(image: image)
  5.        
  6. //        let filter = CIFilter(name: "CIPhotoEffectMono")
  7.        
  8.         let filter = colorCubeFilterFromLUT("X400.png")
  9.         filter?.setDefaults()
  10.         filter?.setValue(ciImage, forKey: kCIInputImageKey)
  11.        
  12.         let outputImage = filter?.outputImage
  13.        
  14.         let filteredImage = UIImage(CIImage: outputImage!)
  15.        
  16.         imageView.image = filteredImage
  17.     }
  18.    
  19.     func colorCubeFilterFromLUT(imageName : NSString) -> CIFilter? {
  20.        
  21.         let kDimension : UInt = 64
  22.        
  23.         let lutImage    = UIImage(named: imageName as String)!.CGImage
  24.         let lutWidth    = CGImageGetWidth(lutImage!)
  25.         let lutHeight   = CGImageGetHeight(lutImage!)
  26.         let rowCount    = lutHeight / Int(kDimension)
  27.         let columnCount = lutWidth / Int(kDimension)
  28.        
  29.         if ((lutWidth % Int(kDimension) != 0) || (lutHeight % Int(kDimension) != 0) || (rowCount * columnCount != Int(kDimension))) {
  30.            
  31.             NSLog("Invalid colorLUT %@", imageName);
  32.             return nil
  33.         }
  34.        
  35.         let bitmap  = self.createRGBABitmapFromImage(lutImage!)
  36.         let size    = Int(kDimension) * Int(kDimension) * Int(kDimension) * sizeof(Float) * 4
  37.         let data    = UnsafeMutablePointer<Float>(malloc(size))
  38.        
  39.         var bitmapOffset : Int = 0
  40.         var z : Int = 0
  41.        
  42.        
  43.         for (var row: UInt = 0; Int(row) < rowCount; row++)
  44.         {
  45.             for (var y: Int = 0; y < Int(kDimension); y++)
  46.             {
  47.                 var tmp = z
  48.                 for (var col: UInt = 0; Int(col) < columnCount; col++)
  49.                 {
  50.                     for (var x: Int = 0; x < Int(kDimension); x++) {
  51.                        
  52.                         let alpha   = Float(bitmap[Int(bitmapOffset)]) / 255.0
  53.                         let red     = Float(bitmap[Int(bitmapOffset+1)]) / 255.0
  54.                         let green   = Float(bitmap[Int(bitmapOffset+2)]) / 255.0
  55.                         let blue    = Float(bitmap[Int(bitmapOffset+3)]) / 255.0
  56.                        
  57.                         let firstPart = z * Int(kDimension) * Int(kDimension)
  58.                         let secondPart = y * Int(kDimension)
  59.                        
  60.                         let dataOffset = Int(firstPart + secondPart + x) * 4
  61.                        
  62.                         data[dataOffset] = red
  63.                         data[dataOffset + 1] = green
  64.                         data[dataOffset + 2] = blue
  65.                         data[dataOffset + 3] = alpha
  66.                         bitmapOffset += 4
  67.                     }
  68.                     z++
  69.                 }
  70.                 z = tmp
  71.             }
  72.             z += columnCount
  73.         }
  74.        
  75.         let colorCubeData = NSData(bytesNoCopy: data, length: size, freeWhenDone: true)
  76.        
  77.         // create CIColorCube Filter
  78.         var filter = CIFilter(name: "CIColorCube")
  79.         filter!.setValue(colorCubeData, forKey: "inputCubeData")
  80.         filter!.setValue(kDimension, forKey: "inputCubeDimension")
  81.        
  82.         return filter
  83.     }
  84.    
  85.    
  86.     func createRGBABitmapFromImage(inImage: CGImage) -> UnsafeMutablePointer<Float> {
  87.        
  88.         //Get image width, height
  89.         let pixelsWide = CGImageGetWidth(inImage)
  90.         let pixelsHigh = CGImageGetHeight(inImage)
  91.        
  92.         // Declare the number of bytes per row. Each pixel in the bitmap in this
  93.         // example is represented by 4 bytes; 8 bits each of red, green, blue, and
  94.         // alpha.
  95.         let bitmapBytesPerRow = Int(pixelsWide) * 4
  96.         let bitmapByteCount = bitmapBytesPerRow * Int(pixelsHigh)
  97.        
  98.         // Use the generic RGB color space.
  99.         let colorSpace = CGColorSpaceCreateDeviceRGB()
  100.        
  101.         // Allocate memory for image data. This is the destination in memory
  102.         // where any drawing to the bitmap context will be rendered.
  103.         let bitmapData = malloc(Int(CUnsignedLong(bitmapByteCount))) // bitmap
  104.         let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)
  105.        
  106.         // Create the bitmap context. We want pre-multiplied RGBA, 8-bits
  107.         // per component. Regardless of what the source image format is
  108.         // (CMYK, Grayscale, and so on) it will be converted over to the format
  109.         // specified here by CGBitmapContextCreate.
  110.                
  111.         let context = CGBitmapContextCreate(bitmapData, Int(pixelsWide), Int(pixelsHigh), 8, Int(bitmapBytesPerRow), colorSpace, bitmapInfo.rawValue)
  112.        
  113.        
  114.         let rect = CGRect(x:0, y:0, width:Int(pixelsWide), height:Int(pixelsHigh))
  115.        
  116.         // Draw the image to the bitmap context. Once we draw, the memory
  117.         // allocated for the context for rendering will then contain the
  118.         // raw image data in the specified color space.
  119.         CGContextDrawImage(context, rect, inImage)
  120.        
  121.         // Now we can get a pointer to the image data associated with the bitmap
  122.         // context.
  123.         // var data = CGBitmapContextGetData(context)
  124.         // var dataType = UnsafeMutablePointer<Float>(data)
  125.         // return dataType
  126.        
  127.        
  128.         var convertedBitmap = malloc(Int(bitmapByteCount * sizeof(Float)))
  129.         vDSP_vfltu8(UnsafePointer<UInt8>(bitmapData), 1, UnsafeMutablePointer<Float>(convertedBitmap), 1, vDSP_Length(bitmapByteCount))
  130.        
  131.         free(bitmapData)
  132.         return UnsafeMutablePointer<Float>(convertedBitmap)
  133.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement