2

I would like to get the RGB (actually, the image provided has been grey-scaled, so grey-scale information would be sufficient) values of the individual pixels of a CIImage.

I currently have the following code:

//
// Conversion of CIImage to CGImage
//
   func convertCIImageToCGImage(inputImage: CIImage) -> CGImage! {
   return CIContext(options: nil).createCGImage(inputImage, fromRect:     inputImage.extent)
}


 process(image: CGImage) {

    let img=convertCIImageToCGImage(image)

    // Set image width, height
    width = CGImageGetWidth(img)
    height = CGImageGetHeight(img)

    let colorSpace = CGColorSpaceCreateDeviceRGB()

    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)

    // Create the bitmap context (ARGB)
    context = CGBitmapContextCreate(nil, width, height, 8, bitmapBytesPerRow, colorSpace, bitmapInfo.rawValue)!

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

    let uncasted_data = CGBitmapContextGetData(context)
    data = UnsafePointer<UInt8>(uncasted_data)

    ...
}

I then access the individual Pixels using the data variable. I realise that most of the time of this function is spent in

CGContextDrawImage(context, rect, img)

Is there another way of accessing the bitmap of a CIImage or CGImage?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
mercator
  • 99
  • 1
  • 10
  • 1
    A lot of people from a _lot_ of different timezones here. Even "Greetings, human" wouldn't be general enough! :p – Vatsal Manot Mar 30 '16 at 09:14
  • Ha, though shallst not copy and paste and forget to paste the first line... Good morning from Switzerland! – mercator Mar 30 '16 at 09:24

0 Answers0