title: Traversing UnsafeMutableRawPointer image data
body: <p>I seem to be unable to wrap my head around the methodology behind manually accessing image pixel data in Swift. I am attempting to create an image mask from a CGImage that can later be used on a separate image. I want to identify all pixels of a specific value and convert everything else in the image to black/white or maybe alpha (not really important at the moment however). The code I'm playing with looks like this:</p>
  
  <pre><code>    let colorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB()
      let contextWidth: Int = Int(snapshot.size.width)
      let contextHeight: Int = Int(snapshot.size.height)
      let bytesPerPixel: Int = 24
      let bitsPerComponent: Int = 8
      let bytesPerRow: Int = bytesPerPixel * contextWidth
      let bitmapInfo: CGBitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.noneSkipLast.rawValue)
  
      guard let context: CGContext = CGContext(data: nil, width: contextWidth, height: contextHeight, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue) else {
          print("Could not create CGContext")
          return
      }
  
      context.draw(maskCGImage, in: CGRect(x: 0, y: 0, width: contextWidth, height: contextHeight))
      guard let contextDataRaw: UnsafeMutableRawPointer = context.data else {
          print("Could not get UnsafeMutableRawPointer from CGContext")
          return
      }
  
      let contextData: UnsafeMutablePointer&lt;UInt8&gt; = contextDataRaw.bindMemory(to: UInt8.self, capacity: contextWidth * contextHeight)
  
      for row in 0..&lt;contextHeight {
          for col in 0..&lt;contextWidth {
              let offset = (col * contextHeight) + row
              let pixelArray = [contextData[offset], contextData[offset + 1], contextData[offset + 2]]
              if pixelArray == [120, 120, 120] {
                  contextData[offset] = 0
                  contextData[offset + 1] = 0
                  contextData[offset + 2] = 0
              }
  
          }
      }
  </code></pre>
  
  <p>I have tried various arrangements of the rows and columns trying to identify the correct order, i.e. <code>let offset = (row * contextWidth) + col</code>, <code>let offset = (col * contextHeight) + row</code>, <code>let offset = ((row * contextWidth) + col) * 3</code>, <code>let offset = ((row * contextWidth) + col) * 4</code>.</p>
  
  <p>The output I get looks something like this (Keep in mind that this image IS supposed to look like a blob of random colors): </p>
  
  <p><a href="https://i.stack.imgur.com/EVaOL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EVaOL.png" alt="enter image description here"></a></p>
  
  <p>As my fancy little arrow shows, the black swatch across the top is my edited pixels, and those pixels are indeed supposed to be turned black, however, so are all the other gray pixels (the ones under the arrow for example). The are definitely the same RGB value of 120, 120, 120.</p>
  
  <p>I know the issue is in the order that I'm moving across the array, I just can't seem to figure out what the pattern is. Also, as a note, using copy(maskingColorComponents:) won't do because I want to remove a few specific colors, not a range of them.</p>
  
  <p>Any help is greatly appreciated as always. Thanks in advance!</p>