title: Flood-fill, only half-working in Swift
body: <p>I am having trouble with the following function in swift. It is meant to flood-fill an arbitrary area.
  The filling color is stored in a variable called floodTargetPixel and the original in floodSourcePixel.</p>
  
  <p>The four convenience methods: <code>red()</code>, <code>green()</code>, <code>blue()</code>, <code>alpha()</code>, <code>rgba()</code> are the same as here:
  <a href="https://stackoverflow.com/questions/31661023/change-color-of-certain-pixels-in-a-uiimage/31661519#31661519">Change color of certain pixels in a UIImage</a></p>
  
  <p>(Problem:)Only part of the area is filled (the part above the starting point) and not the rest, what am I missing?
  The calling code is after the function itself.</p>
  
  <pre><code>    func floodFill(point:CGPoint)
      {
          let pixel = floodPixel.memory
  
          if (red(pixel) == floodTargetPixel[0] &amp;&amp; green(pixel) == floodTargetPixel[1] &amp;&amp;
              blue(pixel) == floodTargetPixel[2] &amp;&amp; alpha(pixel) == floodTargetPixel[3]) {
                  return
          }
  
          if (red(pixel) != floodSourcePixel[0] || green(pixel) != floodSourcePixel[1] ||
              blue(pixel) != floodSourcePixel[2] || alpha(pixel) != floodSourcePixel[3]) {
                  return
          }
  
          floodPixel.memory = rgba(red: floodTargetPixel[0], green: floodTargetPixel[1],
              blue: floodTargetPixel[2], alpha: floodTargetPixel[3])
  
          if (point.y &gt;= 1.0) {
              floodPixel -= imageWidth
              self.floodFill(CGPointMake(point.x,point.y-1.0))
  //            floodPixel += imageWidth
          }
          if (point.y &lt;= CGFloat(imageHeight)-2.0) {
              floodPixel += imageWidth
              self.floodFill(CGPointMake(point.x,point.y+1.0))
  //            floodPixel -= imageWidth
          }
          if (point.x &gt;= 1.0) {
              floodPixel--
              self.floodFill(CGPointMake(point.x-1.0,point.y))
              floodPixel++
          }
          if (point.x &lt;= CGFloat(imageWidth)-2.0) {
              floodPixel++
              self.floodFill(CGPointMake(point.x+1.0,point.y))
              floodPixel--
          }
      }
  </code></pre>
  
  <p>Here is the calling code:</p>
  
  <pre><code>…….
      pixelBuffer = UnsafeMutablePointer&lt;UInt32&gt;(CGBitmapContextGetData(context))
      var pixelPos:Int = imageWidth * Int(point.y) + Int(point.x)
      floodPixel = pixelBuffer!
      floodPixel += pixelPos
      self.floodFill(point)
  …….
  </code></pre>
  
  <p>In the floodFill() method, if I uncomment the 2 commented lines, the program runs for a while and crashes at some point. I presume this is where the real issue is, because those lines should be uncommented.</p>
  
  <p>If I permute the order of the 2 if-blocks:
  <code>if (point.y &gt;= 1.0)</code> and <code>if (point.y &lt;= CGFloat(imageHeight)-2.0)</code>
  then the part below the starting point is filled, instead the part above.</p>