Yes, I know about using CIAreaAverate
CIFilter
to get the average color of pixels.
I am trying to create some alternative using Accelerate Framework
to see if I can come with something faster.
I am rendering a CIImage
to a context. For that purpose I have this CIImage extension
...
let device: MTLDevice = MTLCreateSystemDefaultDevice()!
let context = CIContext.init(mtlDevice: device, options: [.workingColorSpace: kCFNull])
let w = self.extent.width
let h = self.extent.height
let size = w * h * 4
var bitmap = [UInt8](repeating: 0, count:Int(size))
context.render(self,
toBitmap: &bitmap,
rowBytes: 4 * Int(w),
bounds: self.extent,
format: .BGRA8,
colorSpace: nil)
At this point I have bitmap
containing the BGRA bytes interleaved.
To get the average of R, G and B, all I have to do is something like this:
var averageBlue : Int = 0
for x in stride(from:0, through: bitmap.count-4, by: 4) {
let value = bitmap[Int(x)]
averageBlue += Int(value)
}
averageBlue /= numberOfPixels
but this for
loop is slow as hell, as expected.
I was thinking about using some Accelerate
function like
vDSP_meanvD(bitmap, 2, &r, vDSP_Length(numberOfPixels))
but this function requires bitmap
to be an array of UnsafePointer<Double>
...
I could convert bitmap
to that, but that would require a for
loop, that is slow...
Is there any way to extract those R, G and B pixels and have their individual averages using some accelerate stuff going on?
Like ielyamani’s said, you can use
vDSP_vfltu8
to build that buffer ofFloat
efficiently.But rather than striding through that array four times, you can also use
cblas_sgemv
(orcblas_sgemm
) to calculate all four averages in a single call:You can convert
bitmap
to single-precision floating-point values usingvDSP_vfltu8(_:_:_:_:_:)
:And then use
vDSP_meanv(_:_:_:_:)
:As to the reds :