 import UIKit
 
 public struct RGBAImage {
-    public var pixels: UnsafeMutableBufferPointer<Pixel>
+    public var pixels: [Pixel] = []
     public var width: Int
     public var height: Int
     

        
        imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size))
        
        pixels = UnsafeMutableBufferPointer<Pixel>(start: imageData, count: width * height)
    }
    
    public func pixel(x: Int, y: Int) -> Pixel? {

        
        return pixel
    }
    
    fileprivate func kMeans(points: [Pixel], K: Int, minDiff: Float) {
        
        var clusters: [Pixel] = [Pixel]()
        
        while clusters.count < K {
            let idx = arc4random_uniform(UInt32(points.count))
            clusters.append(points[Int(idx)])
        }
    
        var plists: [[Int]]
        
        for _ in 0..<K {
            plists.append([])
        }
        
        while true {
            for (i, pixel) in points.enumerated() {
                var smallestDistance: Float = 10000000.0
                var index = 0
                
                for j in 0..<K {
                    let distance = euclidean(p1: pixel, p2: clusters[j])
                    
                    if distance < smallestDistance {
                        smallestDistance = distance
                        index = j
                    }
                }
                
                plists[index].append(i)
            }
            
            var diff: Float = 0
            
            for i in 0..<K {
                let old = clusters[i]
                let list = plists[i]
                
                let pointlist = list.map{ points[$0] }
                
                let centre = calculateCentre(points: pointlist)
                let newCluster = centre
                
                let dist = euclidean(p1: old, p2: centre)
                
                clusters[i] = newCluster
                
                diff = diff > dist ? diff : dist
            }
            
            if (diff < minDiff) {
                break;
            }
        }
    }
}
