title: How to apply LUT (from .png) to an image? (Swift + Xcode)
body: <p>I am trying to apply a LUT to an image by pressing a button.
  Because, I am new to programming I mostly copied code and tried to modify it for my own project. I get no error messages, but when I try to press the button there is no effect on the image. </p>
  
  <p>When I change the filter-name to sth. different than: "CIColorCube", the app even crashes with the error message: "Unexpectedly found nil while unwrapping an Optional value" </p>
  
  <p><a href="https://i.stack.imgur.com/vWdP8.png" rel="nofollow noreferrer">My LUT Image that I am using. (size: 512x512)</a></p>
  
  <pre><code>func colorCubeFilterFromLUT(imageName : String) -&gt; CIFilter? {
  
      let size = 512
  
      let lutImage    = UIImage(named: "LUT.png")!.cgImage
      let lutWidth    = lutImage!.width
      let lutHeight   = lutImage!.height
      let rowCount    = lutHeight / size
      let columnCount = lutWidth / size
  
      if ((lutWidth % size != 0) || (lutHeight % size != 0) || (rowCount * columnCount != size)) {
          NSLog("Invalid colorLUT %@", "LUT.png");
          return nil
      }
  
      let bitmap  = getBytesFromImage(image: UIImage(named: "LUT.png"))!
      let floatSize = MemoryLayout&lt;Float&gt;.size
  
      let cubeData = UnsafeMutablePointer&lt;Float&gt;.allocate(capacity: size * size * size * 4 * floatSize)
      var z = 0
      var bitmapOffset = 0
  
      for _ in 0 ..&lt; rowCount {
          for y in 0 ..&lt; size {
              let tmp = z
              for _ in 0 ..&lt; columnCount {
                  for x in 0 ..&lt; size {
  
                      let alpha   = Float(bitmap[bitmapOffset]) / 255.0
                      let red     = Float(bitmap[bitmapOffset+1]) / 255.0
                      let green   = Float(bitmap[bitmapOffset+2]) / 255.0
                      let blue    = Float(bitmap[bitmapOffset+3]) / 255.0
  
                      let dataOffset = (z * size * size + y * size + x) * 4
  
                      cubeData[dataOffset + 3] = alpha
                      cubeData[dataOffset + 2] = red
                      cubeData[dataOffset + 1] = green
                      cubeData[dataOffset + 0] = blue
                      bitmapOffset += 4
                  }
                  z += 1
              }
              z = tmp
          }
          z += columnCount
      }
  
      let colorCubeData = NSData(bytesNoCopy: cubeData, length: size * size * size * 4 * floatSize, freeWhenDone: true)
  
      // create CIColorCube Filter
      let filter = CIFilter(name: "CIColorCube")
      filter?.setValue(colorCubeData, forKey: "inputCubeData")
      filter?.setValue(size, forKey: "inputCubeDimension")
  
      return filter
  }
  
  
  func getBytesFromImage(image:UIImage?) -&gt; [UInt8]?
  {
      var pixelValues: [UInt8]?
      if let imageRef = image?.cgImage {
          let width = Int(imageRef.width)
          let height = Int(imageRef.height)
          let bitsPerComponent = 8
          let bytesPerRow = width * 4
          let totalBytes = height * bytesPerRow
  
          let bitmapInfo = CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Little.rawValue
          let colorSpace = CGColorSpaceCreateDeviceRGB()
          var intensities = [UInt8](repeating: 0, count: totalBytes)
  
          let contextRef = CGContext(data: &amp;intensities, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo)
          contextRef?.draw(imageRef, in: CGRect(x: 0.0, y: 0.0, width: CGFloat(width), height: CGFloat(height)))
  
          pixelValues = intensities
      }
      return pixelValues!
  }
  
  @IBAction func filterRetro(_ sender: Any) {
      let inputImage = CIImage(image: photo.image!)
  
      let filteredImage = inputImage?.applyingFilter("CIColorCube")
  
      let renderedImage = context.createCGImage(filteredImage!, from: (filteredImage?.extent)!)
  
      photo.image = UIImage(cgImage: renderedImage!)
  }
  </code></pre>