I have written the following function:
class func convertImageToBlackAndWhite(#data:UnsafeMutablePointer<UInt32>, inputWidth:UInt32, inputHeight:UInt32) -> UnsafeMutablePointer<UInt32> {
var currentPixel = UnsafeMutablePointer<UInt32>()
currentPixel = data
for (var i = 0; i < Int(inputHeight); i++) {
for (var j = 0; j < Int(inputWidth); j++) {
var color:UInt32 = currentPixel.memory
// Average of RGB = greyscale
var averageColor:UInt32 = (R(color) + G(color) + B(color)) / 3
var updatedColor:UInt32 = RGBAMake(averageColor, g: averageColor, b: averageColor, a: A(color))
currentPixel.memory = updatedColor
currentPixel++
}
}
var outputData = UnsafeMutablePointer<UInt32>((calloc(UInt(inputHeight*inputWidth), UInt(sizeof(UInt32)))))
outputData = currentPixel
return outputData
}
I want to return the outputData to display it on screen. Therefore I created within another function this code:
class func processUsingPixels(#inputImage:UIImage) -> UIImage {
var inputCGImage:CGImageRef = inputImage.CGImage
var colorSpace:CGColorSpaceRef = CGColorSpaceCreateDeviceRGB()
var inputWidth:UInt = CGImageGetWidth(inputCGImage)
var inputHeight:UInt = CGImageGetHeight(inputCGImage)
var bytesPerPixel:UInt = 4
var bitsPerComponent:UInt = 8
var inputBytesPerRow = bytesPerPixel * inputWidth
let bitmapInfo = CGBitmapInfo(CGBitmapInfo.ByteOrder32Big.rawValue | CGImageAlphaInfo.PremultipliedLast.rawValue)
let inputPixels = UnsafeMutablePointer<UInt32>(calloc(UInt(inputHeight*inputWidth), UInt(sizeof(UInt32))))
var outputData = UnsafeMutablePointer<UInt32>()
outputData = self.convertImageToBlackAndWhite(data: inputPixels, inputWidth: UInt32(inputWidth), inputHeight: UInt32(inputHeight))
let context:CGContextRef = CGBitmapContextCreate(outputData, inputWidth, inputHeight, bitsPerComponent, inputBytesPerRow, colorSpace, bitmapInfo)
CGContextDrawImage(context, CGRectMake(0, 0, CGFloat(inputWidth), CGFloat(inputHeight)), inputCGImage)
return inputImage
}
The outputData was the data I wanted now to display. I expected that the changes by the for-loop in "ConvertImageToBlackAndWhite" would be reflected, but no change at all happens.
Thanks to Rob I got some valuable insights before. but still this picture does not change at all.
my questions would be:
1) is it correct to create this output data var as a return value? Does it make sense at all?
2) I use this outputData Variable that has been returned to create a Context and Draw the context. But this has no effect. Do I need to create a new Image first? The return value of processusingPixels is an UIImage. But I thought the CGContextDrawImage would be sufficient. Is that wrong?
Aucun commentaire:
Enregistrer un commentaire