def flood_fill(col, row, image, fill_value)
      # If the given starting point is out of bounds, put it at the array boundary:
      col = col > image.shape[0] ? -1 : col
      row = row > image.shape[1] ? -1 : row
      existing_value = image[col, row]
      queue = Array.new
      queue.push([col, row])
      until queue.empty?
        col, row = queue.shift
        if image[col, row] == existing_value
          west_col, west_row = ff_find_border(col, row, existing_value, :west, image)
          east_col, east_row = ff_find_border(col, row, existing_value, :east, image)
          # Fill the line between the two border pixels (i.e. not touching the border pixels):
          image[west_col..east_col, row] = fill_value
          q = west_col
          while q <= east_col
            [:north, :south].each do |direction|
              same_col, next_row = ff_neighbour(q, row, direction)
              begin
                queue.push([q, next_row]) if image[q, next_row] == existing_value
              rescue
                # Out of bounds. Do nothing.
              end
            end
            q, same_row = ff_neighbour(q, row, :east)
          end
        end
      end
      return image
    end
def flood_fill(col, row, image, fill_value)
      # If the given starting point is out of bounds, put it at the array boundary:
      col = col > image.shape[0] ? -1 : col
      row = row > image.shape[1] ? -1 : row
      existing_value = image[col, row]
      queue = Array.new
      queue.push([col, row])
      until queue.empty?
        col, row = queue.shift
        if image[col, row] == existing_value
          west_col, west_row = ff_find_border(col, row, existing_value, :west, image)
          east_col, east_row = ff_find_border(col, row, existing_value, :east, image)
          # Fill the line between the two border pixels (i.e. not touching the border pixels):
          image[west_col..east_col, row] = fill_value
          q = west_col
          while q <= east_col
            [:north, :south].each do |direction|
              same_col, next_row = ff_neighbour(q, row, direction)
              begin
                queue.push([q, next_row]) if image[q, next_row] == existing_value
              rescue
                # Out of bounds. Do nothing.
              end
            end
            q, same_row = ff_neighbour(q, row, :east)
          end
        end
      end
      return image
    end
Replaces all pixels of a specific value that are contained by pixels of
 a different value.

 Uses an iterative, queue based flood fill algorithm. It seems that a
 recursive method is not suited for Ruby, due to its limited stack space
 (which is known to be a problem in general for scripting languages).

 @param [Integer] col the starting column index
 @param [Integer] row the starting row index
 @param [NArray] image a two-dimensional numerical pixel array
 @param [Integer] fill_value the value used for marking pixels
 @return [NArray] a marked pixel array