mercredi 25 mars 2015

Applying guassian blur in swift not wokring

This is the function am using, am passing an image to this which got by the current context method. In this code it whenever i am calling this function immediately crashing right at the vImageBoxConvolve_ARGB8888 API function showing that EXC_BAD_CODE error. No idea what to do.? Anybody know how to apply Gaussian blur in swift.?



func applyBlurWithRadius(blurRadius : CGFloat, tintColor : UIColor, saturationDeltaFactor : CGFloat, baseImage : UIImage) -> UIImage
{
// Check pre-conditions.

if (self.frame.size.width < 1 || self.frame.size.height < 1)
{
// Invalid size both dimensions should be greater than 1

return UIImage()
}


if let valueMaskImage = baseImage.CGImage
{
}
else
{
return UIImage()
}

var imageRect : CGRect = CGRectMake(CGPointZero.x, CGPointZero.y, self.frame.size.width, self.frame.size.height)
var effectImage : UIImage = baseImage

var hasBlur : Bool = blurRadius > (CGFloat)(__FLT_EPSILON__)
var hasSaturationChange : Bool = fabsf(Float(saturationDeltaFactor - 1)) > __FLT_EPSILON__


if hasBlur || hasSaturationChange
{
UIGraphicsBeginImageContextWithOptions(self.frame.size, false, UIScreen.mainScreen().scale)
var effectInContext : CGContextRef = UIGraphicsGetCurrentContext()
CGContextScaleCTM(effectInContext, 1.0, -1.0)
CGContextTranslateCTM(effectInContext, 0, -self.frame.size.height)
CGContextDrawImage(effectInContext, imageRect, baseImage.CGImage)


var effectInBuffer : vImage_Buffer_Struct = vImage_Buffer_Struct(data: CGBitmapContextGetData(effectInContext), width: CGBitmapContextGetWidth(effectInContext), height: CGBitmapContextGetHeight(effectInContext), rowBytes: CGBitmapContextGetBytesPerRow(effectInContext))



UIGraphicsBeginImageContextWithOptions(self.frame.size, false, UIScreen.mainScreen().scale)

var effectOutContext : CGContextRef = UIGraphicsGetCurrentContext()

var effectOutBuffer : vImage_Buffer_Struct = vImage_Buffer_Struct(data: CGBitmapContextGetData(effectOutContext), width: CGBitmapContextGetWidth(effectOutContext), height: CGBitmapContextGetHeight(effectOutContext), rowBytes: CGBitmapContextGetBytesPerRow(effectOutContext))

var effectInBuffer_V_IMAGE_BUFFER : vImage_Buffer = vImage_Buffer(data: &effectInBuffer, height: effectInBuffer.height, width: effectInBuffer.width, rowBytes: effectInBuffer.rowBytes)

var effectOutBuffer_V_IMAGE_BUFFER : vImage_Buffer = vImage_Buffer(data: &effectOutBuffer, height: effectOutBuffer.height, width: effectOutBuffer.width, rowBytes: effectOutBuffer.rowBytes)


if hasBlur
{
var inputRadius : CGFloat = blurRadius * UIScreen.mainScreen().scale

var value1 : Double = Double(2) * (M_PI)

var value2 : Float = sqrtf((Float)(value1))

var value4 : Float = (Float)(inputRadius * 3)

var value5 : Float = (Float)(4 + 0.5)


var radius : UInt32 = (UInt32)(floorf((value4 * (value2 / value5 ))))

if (radius % 2 != 1)
{
radius += 1; // force radius to be odd so that the three box-blur methodology works.
}

var uInt8ZeroValue : UInt8 = 0
var zerovImagePixelCount : vImagePixelCount = 0


vImageBoxConvolve_ARGB8888( &effectInBuffer_V_IMAGE_BUFFER , &effectOutBuffer_V_IMAGE_BUFFER, nil, zerovImagePixelCount, zerovImagePixelCount, radius, radius, &uInt8ZeroValue, ((vImage_Flags)(kvImageEdgeExtend)))


vImageBoxConvolve_ARGB8888( &effectOutBuffer_V_IMAGE_BUFFER , &effectInBuffer_V_IMAGE_BUFFER, nil, zerovImagePixelCount, zerovImagePixelCount, radius, radius, &uInt8ZeroValue, ((vImage_Flags)(kvImageEdgeExtend)))

vImageBoxConvolve_ARGB8888( &effectInBuffer_V_IMAGE_BUFFER , &effectOutBuffer_V_IMAGE_BUFFER, nil, zerovImagePixelCount, zerovImagePixelCount, radius, radius, &uInt8ZeroValue, ((vImage_Flags)(kvImageEdgeExtend)))
}


var effectImageBuffersAreSwapped : Bool = false

var floatingPointSaturationMatrix : [CGFloat] = []

if hasSaturationChange
{
var sValue : CGFloat = saturationDeltaFactor
floatingPointSaturationMatrix =
[ 0.0722 + (0.9278 * sValue), 0.0722 - (0.0722 * sValue),0.0722 - (0.0722 * sValue),
0, 0.7152 - (0.7152 * sValue), 0.7152 + (0.2848 * sValue),
0.7152 - (0.7152 * sValue), 0, 0.2126 - (0.2126 * sValue), 0.2126 - (0.2126 * sValue) , 0.2126 - (0.7873 * sValue), 0 ,0,0,0,1]

}


let divisor : Int32 = 256
var matrixSize : Int = sizeofValue(floatingPointSaturationMatrix) / sizeofValue(floatingPointSaturationMatrix[0])

var saturationMatrix : [Int16] = []

for inputValue in floatingPointSaturationMatrix
{
saturationMatrix.append((Int16)(inputValue * (CGFloat)(divisor)))
}

if hasBlur
{

vImageMatrixMultiply_ARGB8888(&effectOutBuffer_V_IMAGE_BUFFER, &effectInBuffer_V_IMAGE_BUFFER, &saturationMatrix, divisor, nil, nil, ((vImage_Flags)(kvImageNoFlags)))

effectImageBuffersAreSwapped = true
}
else
{

vImageMatrixMultiply_ARGB8888(&effectInBuffer_V_IMAGE_BUFFER, &effectOutBuffer_V_IMAGE_BUFFER, &saturationMatrix, divisor, nil, nil, ((vImage_Flags)(kvImageNoFlags)))
}


if !effectImageBuffersAreSwapped
{
effectImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

}
else
{
effectImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

}

}

// Set up output context.

UIGraphicsBeginImageContextWithOptions(self.frame.size, false, UIScreen.mainScreen().scale)
var outputContext: CGContextRef = UIGraphicsGetCurrentContext()
CGContextScaleCTM(outputContext, 1.0, -1.0)
CGContextTranslateCTM(outputContext, 0, -self.frame.size.height)

// Draw base image.

CGContextDrawImage(outputContext, imageRect, baseImage.CGImage)


// Draw effect image.

if hasBlur
{
CGContextSaveGState(outputContext)
CGContextDrawImage(outputContext, imageRect, effectImage.CGImage)
CGContextRestoreGState(outputContext)
}

// Add in color tint.


CGContextSaveGState(outputContext)
CGContextSetFillColorWithColor(outputContext, tintColor.CGColor)
CGContextFillRect(outputContext, imageRect)
CGContextRestoreGState(outputContext)

// Output image is ready.

var outputImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return outputImage
}



Aucun commentaire:

Enregistrer un commentaire