How to load the .cube file into the real-time iOS camera in swift iOS?

61 Views Asked by At

I'm facing some issues in loading the .cube file into the real-time camera. The Blackmagic Camera app utilizes the .cube file to load it in real-time on the camera within the application.

Here's my code for reference:

var imageView: UIImageView!
var cameraCapture: CameraCapture?
var lutFilter: CIFilter?

extension FCRCameraViewController: UIDocumentPickerDelegate{
    
    func showDocumentPicker() {
        let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.item"], in: .import)
        documentPicker.delegate = self
        self.present(documentPicker, animated: true, completion: nil)
    }
    
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        guard let selectedURL = urls.first else { return }
        addCustomLutWithFilter(with: selectedURL)
    }

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
    }

    func addCustomLutWithFilter(with lutFile: URL?){
        
        footerTabBar.isHidden = true
        imageView = UIImageView(frame: view.bounds)
        view.addSubview(imageView)
        
        if let lutURL = lutFile,
           let lutData = try? Data(contentsOf: lutURL) {
            if let filter = createFilterFromLUTData(lutData) {
                self.lutFilter = filter
            } else {
                print("Error creating CIFilter from LUT data.")
            }
        }
        
        self.cameraCapture = CameraCapture(callback: { image in
            guard let image = image else { return }
            
            var filteredImage = image
            if let lutFilter = self.lutFilter {
                lutFilter.setValue(image, forKey: kCIInputImageKey)
                if let outputImage = lutFilter.outputImage {
                    filteredImage = outputImage
                }
            }
            
            let uiImage = UIImage(ciImage: filteredImage.transformToOrigin(withSize: self.view.bounds.size))
            self.imageView.image = uiImage
        })
        cameraCapture?.start()
        closeCameraButton.isHidden = false
        view.bringSubviewToFront(closeCameraButton)
        
    }
    
    func createFilterFromLUTData(_ lutData: Data) -> CIFilter? {
        
        guard let filter = CIFilter(name: "CIColorCube") else {
            return nil
        }
        let colorCubeData = [UInt8](lutData)
        let cubeData = Data(bytes: colorCubeData, count: 4096 * MemoryLayout<UInt8>.size * 4)
        filter.setValue(NSNumber(value: 16), forKey: "inputCubeDimension")
        filter.setValue(cubeData, forKey: "inputCubeData")
        print(cubeData)
    
        return filter
    }
    
    @objc func disableCameraAction(_ sender: UIButton){
        DispatchQueue.main.async {
            self.cameraCapture?.stop()
            self.imageView.removeFromSuperview()
            self.closeCameraButton.isHidden = true
            self.footerTabBar.isHidden = false
        }
    }
}

As it is, the .cube file is not being loaded into the real-time camera. I would appreciate if anyone could help me figure this out.

0

There are 0 best solutions below