         
         return pixel
     }
-}
-
-/*
-extension UIImage {
-    public var pixels: UnsafeMutableBufferPointer<Pixel>? {
-        guard let cgImage = self.cgImage else {
-            return nil
+    
+    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([])
         }
         
-        let bytesPerRow = width * 4
-        let imageData = UnsafeMutableBufferPointer<Pixel>.allocate(capacity: width * height)
-        let colorSpace = CGColorSpaceCreateDeviceRGB()
-        
-        var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue
-        bitmapInfo = bitmapInfo | CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue
-        
-        guard let imageContext = CGContext(data: imageData,
-                                         width: width,
-                                         height: height,
-                                         bitsPerComponent: 8,
-                                         bytesPerRow: bytesPerRow,
-                                         space: colorSpace,
-                                         bitmapInfo: bitmapInfo) else
-        {
-            return nil
+        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;
+            }
         }
-        
-        imageContext.draw(cgImage, CGRect(origin: .zero, size: size))
-        
-        return UnsafeMutableBufferPointer<Pixel>(start: imageData, count: width * height)
-    }
-    
-    public func pixel(x: Int, y: Int) -> Pixel? {
-        guard x >= 0 && x < width && y >= 0 && y < height else {
-            return nil
-        }
-        
-        let address = y * width + x
-        return pixels?[address]
-    }
-    
-    public var width: Int {
-        return Int(self.size.width)
-    }
-    
-    public var height: Int {
-        return Int(self.size.height)
     }
 }
- */

        
        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? {

import UIKit

public struct RGBAImage {
    public var pixels: [Pixel] = []
    public var width: Int
    public var height: Int
    
