title: How to normalize disparity data in iOS?
body: <p>In WWDC session "Image Editing with Depth" they mentioned few times <code>normalizedDisparity</code> and <code>normalizedDisparityImage</code>:</p>
  
  <blockquote>
    <p>"The basic idea is that we're going to map our normalized disparity
    values into values between 0 and 1"</p>
    
    <p>"So once you know the min and max you can normalize the depth or disparity between 0 and 1."</p>
  </blockquote>
  
  <p>I tried to first get the disparit image like this: </p>
  
  <pre><code>let disparityImage = depthImage.applyingFilter(
      "CIDepthToDisparity", withInputParameters: nil)
  </code></pre>
  
  <p>Then I tried to get <a href="https://developer.apple.com/documentation/avfoundation/avdepthdata/2881227-depthdatamap" rel="noreferrer">depthDataMap</a> and do normalization but it didn't work. I'm I on the right track? would be appreciate some hint on what to do. </p>
  
  <p>Edit: </p>
  
  <p>This is my test code, sorry for the quality. I get the <code>min</code> and <code>max</code> then I try to loop over the data to normalize it (<code>let normalizedPoint = (point - min) / (max - min)</code>) </p>
  
  <pre><code>let depthDataMap = depthData!.depthDataMap
  let width = CVPixelBufferGetWidth(depthDataMap) //768 on an iPhone 7+
  let height = CVPixelBufferGetHeight(depthDataMap) //576 on an iPhone 7+
  CVPixelBufferLockBaseAddress(depthDataMap, CVPixelBufferLockFlags(rawValue: 0))
  // Convert the base address to a safe pointer of the appropriate type
  let floatBuffer = unsafeBitCast(CVPixelBufferGetBaseAddress(depthDataMap), 
      to: UnsafeMutablePointer&lt;Float32&gt;.self)
  var min = floatBuffer[0]
  var max = floatBuffer[0]
  for x in 0..&lt;width{
      for y in 0..&lt;height{
          let distanceAtXYPoint = floatBuffer[Int(x * y)]
          if(distanceAtXYPoint &lt; min){
              min = distanceAtXYPoint
          }
          if(distanceAtXYPoint &gt; max){
              max = distanceAtXYPoint
          }
      }
  }
  </code></pre>
  
  <p>What I expected is the the data will reflect the disparity where the user clicked on the image but it didn't match. The code to find the disparity where the user clicked is <a href="http://docs.huihoo.com/apple/wwdc/2017/508_image_editing_with_depth.pdf" rel="noreferrer">here</a>:</p>
  
  <pre><code>// Apply the filter with the sampleRect from the user’s tap. Don’t forget to clamp!
  let minMaxImage = normalized?.clampingToExtent().applyingFilter(
      "CIAreaMinMaxRed", withInputParameters: 
          [kCIInputExtentKey : CIVector(cgRect:rect2)])
  // A four-byte buffer to store a single pixel value
  var pixel = [UInt8](repeating: 0, count: 4)
  // Render the image to a 1x1 rect. Be sure to use a nil color space.
  context.render(minMaxImage!, toBitmap: &amp;pixel, rowBytes: 4,
      bounds: CGRect(x:0, y:0, width:1, height:1), 
      format:  kCIFormatRGBA8, colorSpace: nil)
  // The max is stored in the green channel. Min is in the red.
  let disparity = Float(pixel[1]) / 255.0
  </code></pre>