0 Replies
      Latest reply on Mar 28, 2019 5:46 PM by kart
      kart Level 1 Level 1 (0 points)

        We can either ask AVFoundation to capture a photo in a processed format like BGRA, or ask it to capture a RAW and then process it to BGRA using Core Image. The latter lets us control the parameters used in the process rather than leaving it to the defaults iOS uses.

        In my case, I want to turn off noise-reduction, but otherwise have the image similar to BGRA. So I tried:

        let rawData = AVCapturePhotoOutput.dngPhotoDataRepresentation(forRawSampleBuffer: self,

                                                                             previewPhotoSampleBuffer: nil)!

        let options: [AnyHashable : Any] = [

          kCIInputNoiseReductionAmountKey: 0.0,

          kCIInputLuminanceNoiseReductionAmountKey: 0.0,

          kCIInputColorNoiseReductionAmountKey: 0.0]



        let rawFilter = CIFilter(imageData: rawData, options: options)!

        let outputImage = rawFilter.outputImage!

        let context = CIContext()

        let cgImage = context.createCGImage(outputImage, from: outputImage.extent)!

        let uiImage = UIImage(cgImage: cgImage)

        let data = UIImageJPEGRepresentation(uiImage, jpegQuality)!

        // Save the data using PHPhotoLibrary

        But this results in a RAW that looks much brighter...

        ... than the BGRA:

        If the photos aren't displaying properly, see https://stackoverflow.com/questions/47994473/what-settings-do-i-use-to-capture-a-raw-in-ios-without-noise-reduction-but-other


        This is just one photo, as an example. I tested for many different scenes, and found that no matter what settings I tried, I couldn't match the quality of the JPEG — it was too bright, or too dark, or had too much contrast, etc.

        I then noticed that there's an input boost parameter with a default value of 1 for a full boost, so I set it to 0:

        let options: [AnyHashable: Any] = [kCIInputBoostKey: 0.0]

        But that produced a dull image without adequate contrast:

        So, the question is what settings should I use to have Core Image develop the RAW to BGRA with the same visual look as a BGRA captured by AVFoundation, except without noise reduction.