Use CGPoint in SWIFT 3 to get pixel color from the image

I tried this PixelExtractor class in Swift 3 and got an error;
Cannot use the parameter list of type'(UnsafeMutableRawPointer?)’ to call the initial value setting of type’UnsafePointer’ Item

class PixelExtractor: NSObject {

let image: CGImage
let context: CGContextRef?

var width: Int {
get {
return CGImageGetWidth(image)
}
}

var height: Int {
get {
return CGImageGetHeight(image)
}
}

init(img: CGImage) {
image = img
context = PixelExtractor. createBitmapContext(img)
}

class func createBitmapContext(img: CGImage) -> CGContextRef {

// Get image width, height
let pixelsWide = CGImageGetWidth(img)
let pixelsHigh = CGImageGetHeight(img)

let bitmapBytesPerRow = pixelsWide * 4
let bitmapByteCount = bitmapBytesPerRow * Int(pixelsHigh)

// Use the generic RGB color space.
let colorSpace = CGCol orSpaceCreateDeviceRGB()

// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
let bitmapData = malloc (bitmapByteCount)
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)
let size = CGSizeMake(CGFloat(pixelsWide), CGFloat(pixelsHigh))
UIGraphicsBeginImageContextWithOptions(size, false, 0.0 )
// create bitmap
let context = CGBitmapContextCreate(bitmapData, pixelsWide, pixelsHigh, 8,
bitmapBytesPerRow, colorSpace, bitmapInfo.rawValue)

// draw the image onto the context
let rect = CGRect(x: 0, y: 0, width: pixelsWide, height: pixelsHigh)
CGContextDrawImage(context, rect, img)

return context!
}

func colorAt(xx: Int, y: Int)->UIColor {

assert(0<=x && x assert(0<=y && y
let uncas tedData = CGBitmapContextGetData(context)
let data = UnsafePointer(uncastedData)

let offset = 4 * (y * width + x)

let alpha : UInt8 = data[offset]
let red: UInt8 = data[offset+1]
let green: UInt8 = data[offset+2]
let blue: UInt8 = data[offset+ 3]

let color = UIColor(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: CGFloat(alpha)/255.0 )

return color
}

}

Fix this error.

let data = UnsafePointer(uncastedData)

– >

let data = UnsafeRawPointer(uncastedData)

Get other errors; ‘Enter’ UnsafeRawPointer? ’No subscript member’

How to fix this error?

When UnsafeRawPointer is included in your data, you can write something like:

< /p>

let alpha = data.load(fromByteOffset: offset, as: UInt8.self)
let red = data.load(fromByteOffset: offset+1, as: UInt8 .self)
let green = data.load(fromByteOffset: offset+2, as: UInt8.self)
let blue = data.load(fromByteOffset: offset+3, as: UInt8.self)< /pre>

Alternatively, you can get UnsafeMutablePointer from your uncastedData (assuming it is an UnsafeMutableRawPointer):

let data = uncastedData.assumingMemoryBound(to: UInt8.self)

I tried this PixelExtractor class in Swift 3 and got an error;
Cannot call the type with the parameter list of type'(UnsafeMutableRawPointer?)' Initial value setting item of'UnsafePointer'

class PixelExtractor: NSObject {

let image: CGImage
let context: CGContextRef?

var width: Int {
get {
return CGImageGetWidth(image)
}
}

var height : Int {
get {
return CGImag eGetHeight(image)
}
}

init(img: CGImage) {
image = img
context = PixelExtractor.createBitmapContext(img)
}

class func createBitmapContext(img: CGImage) -> CGContextRef {

// Get image width, height
let pixelsWide = CGImageGetWidth(img)< br /> let pixelsHigh = CGImageGetHeight(img)

let bitmapBytesPerRow = pixelsWide * 4
let bitmapByteCount = bitmapBytesPerRow * Int(pixelsHigh)

// Use the generic RGB color space.
let colorSpace = CGColorSpaceCreateDeviceRGB()

// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
let bitmapData = malloc(bitmapByteCount)
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)
let size = CGSizeMake(CGFloat(pixelsWide), CGFloat(pixelsHigh))
UIGraphicsBeginImageCon textWithOptions(size, false, 0.0)
// create bitmap
let context = CGBitmapContextCreate(bitmapData, pixelsWide, pixelsHigh, 8,
bitmapBytesPerRow, colorSpace, bitmapInfo.rawValue)
< br /> // draw the image onto the context
let rect = CGRect(x: 0, y: 0, width: pixelsWide, height: pixelsHigh)
CGContextDrawImage(context, rect, img)

return context!
}

func colorAt(xx: Int, y: Int)->UIColor {

assert(0<= x && x assert(0<=y && y
let uncastedData = CGBitmapContextGetData(context)
let data = UnsafePointer(uncastedData)

let offset = 4 * (y * width + x)

let alpha: UInt8 = data[offset]
let red: UInt8 = data[offset+ 1]
let green: UInt8 = data[offset+2]
let blue: UInt8 = data[offset+3]

let color = UIColor(red: CGFloat(red )/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255. 0, alpha: CGFloat(alpha)/255.0)

return color
}

}

Fix this error.

< p>

let data = UnsafePointer(uncastedData)

– >

let data = UnsafeRawPointer(uncastedData)< /pre> 

Get other errors;'Enter' UnsafeRawPointer? ’No subscript member’

How to fix this error?

When your data contains UnsafeRawPointer, you can write something similar:

let alpha = data.load(fromByteOffset: offset, as: UInt8.self)
let red = data.load(fromByteOffset: offset+1, as: UInt8.self)
let green = data. load(fromByteOffset: offset+2, as: UInt8.self)
let blue = data.load(fromByteOffset: offset+3, as: UInt8.self)

Or, you can get UnsafeMutablePointer from your uncastedData (assuming it is an UnsafeMutableRawPointer):

let data = uncastedData.assumingMemoryBound(to: UInt8.self)

Leave a Comment

Your email address will not be published.