123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- #import "UIImage+AHKAdditions.h"
- @import Accelerate;
- #import <float.h>
- @implementation UIImage (AHKAdditions)
- - (UIImage *)ahk_applyLightEffect
- {
- UIColor *tintColor = [UIColor colorWithWhite:1.0 alpha:0.3f];
- return [self ahk_applyBlurWithRadius:30 tintColor:tintColor saturationDeltaFactor:1.8f maskImage:nil];
- }
- - (UIImage *)ahk_applyExtraLightEffect
- {
- UIColor *tintColor = [UIColor colorWithWhite:0.97f alpha:0.82f];
- return [self ahk_applyBlurWithRadius:20 tintColor:tintColor saturationDeltaFactor:1.8f maskImage:nil];
- }
- - (UIImage *)ahk_applyDarkEffect
- {
- UIColor *tintColor = [UIColor colorWithWhite:0.11f alpha:0.73f];
- return [self ahk_applyBlurWithRadius:20 tintColor:tintColor saturationDeltaFactor:1.8f maskImage:nil];
- }
- - (UIImage *)ahk_applyTintEffectWithColor:(UIColor *)tintColor
- {
- const CGFloat EffectColorAlpha = 0.6f;
- UIColor *effectColor = tintColor;
- int componentCount = (int)CGColorGetNumberOfComponents(tintColor.CGColor);
- if (componentCount == 2) {
- CGFloat b;
- if ([tintColor getWhite:&b alpha:NULL]) {
- effectColor = [UIColor colorWithWhite:b alpha:EffectColorAlpha];
- }
- }
- else {
- CGFloat r, g, b;
- if ([tintColor getRed:&r green:&g blue:&b alpha:NULL]) {
- effectColor = [UIColor colorWithRed:r green:g blue:b alpha:EffectColorAlpha];
- }
- }
- return [self ahk_applyBlurWithRadius:10 tintColor:effectColor saturationDeltaFactor:-1.0 maskImage:nil];
- }
- - (UIImage *)ahk_applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage
- {
-
- if (self.size.width < 1 || self.size.height < 1) {
- NSLog (@"*** error: invalid size: (%.2f x %.2f). Both dimensions must be >= 1: %@", self.size.width, self.size.height, self);
- return nil;
- }
- if (!self.CGImage) {
- NSLog (@"*** error: image must be backed by a CGImage: %@", self);
- return nil;
- }
- if (maskImage && !maskImage.CGImage) {
- NSLog (@"*** error: maskImage must be backed by a CGImage: %@", maskImage);
- return nil;
- }
- CGRect imageRect = { CGPointZero, self.size };
- UIImage *effectImage = self;
- BOOL hasBlur = blurRadius > __FLT_EPSILON__;
- BOOL hasSaturationChange = fabs(saturationDeltaFactor - 1.) > __FLT_EPSILON__;
- if (hasBlur || hasSaturationChange) {
- UIGraphicsBeginImageContextWithOptions(self.size, NO, [[UIScreen mainScreen] scale]);
- CGContextRef effectInContext = UIGraphicsGetCurrentContext();
- CGContextScaleCTM(effectInContext, 1.0, -1.0);
- CGContextTranslateCTM(effectInContext, 0, -self.size.height);
- CGContextDrawImage(effectInContext, imageRect, self.CGImage);
- vImage_Buffer effectInBuffer;
- effectInBuffer.data = CGBitmapContextGetData(effectInContext);
- effectInBuffer.width = CGBitmapContextGetWidth(effectInContext);
- effectInBuffer.height = CGBitmapContextGetHeight(effectInContext);
- effectInBuffer.rowBytes = CGBitmapContextGetBytesPerRow(effectInContext);
- UIGraphicsBeginImageContextWithOptions(self.size, NO, [[UIScreen mainScreen] scale]);
- CGContextRef effectOutContext = UIGraphicsGetCurrentContext();
- vImage_Buffer effectOutBuffer;
- effectOutBuffer.data = CGBitmapContextGetData(effectOutContext);
- effectOutBuffer.width = CGBitmapContextGetWidth(effectOutContext);
- effectOutBuffer.height = CGBitmapContextGetHeight(effectOutContext);
- effectOutBuffer.rowBytes = CGBitmapContextGetBytesPerRow(effectOutContext);
- if (hasBlur) {
-
-
-
-
-
-
-
-
-
-
-
-
- CGFloat inputRadius = blurRadius * [[UIScreen mainScreen] scale];
- uint32_t radius = (uint32_t)floor(inputRadius * 3. * sqrt(2 * M_PI) / 4 + 0.5);
- if (radius % 2 != 1) {
- radius += 1;
- }
- vImageBoxConvolve_ARGB8888(&effectInBuffer, &effectOutBuffer, NULL, 0, 0, radius, radius, 0, kvImageEdgeExtend);
- vImageBoxConvolve_ARGB8888(&effectOutBuffer, &effectInBuffer, NULL, 0, 0, radius, radius, 0, kvImageEdgeExtend);
- vImageBoxConvolve_ARGB8888(&effectInBuffer, &effectOutBuffer, NULL, 0, 0, radius, radius, 0, kvImageEdgeExtend);
- }
- BOOL effectImageBuffersAreSwapped = NO;
- if (hasSaturationChange) {
- CGFloat s = saturationDeltaFactor;
- CGFloat floatingPointSaturationMatrix[] = {
- 0.0722f + 0.9278f * s, 0.0722f - 0.0722f * s, 0.0722f - 0.0722f * s, 0,
- 0.7152f - 0.7152f * s, 0.7152f + 0.2848f * s, 0.7152f - 0.7152f * s, 0,
- 0.2126f - 0.2126f * s, 0.2126f - 0.2126f * s, 0.2126f + 0.7873f * s, 0,
- 0, 0, 0, 1,
- };
- const int32_t divisor = 256;
- NSUInteger matrixSize = sizeof(floatingPointSaturationMatrix)/sizeof(floatingPointSaturationMatrix[0]);
- int16_t saturationMatrix[matrixSize];
- for (NSUInteger i = 0; i < matrixSize; ++i) {
- saturationMatrix[i] = (int16_t)round(floatingPointSaturationMatrix[i] * divisor);
- }
- if (hasBlur) {
- vImageMatrixMultiply_ARGB8888(&effectOutBuffer, &effectInBuffer, saturationMatrix, divisor, NULL, NULL, kvImageNoFlags);
- effectImageBuffersAreSwapped = YES;
- }
- else {
- vImageMatrixMultiply_ARGB8888(&effectInBuffer, &effectOutBuffer, saturationMatrix, divisor, NULL, NULL, kvImageNoFlags);
- }
- }
- if (!effectImageBuffersAreSwapped)
- effectImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- if (effectImageBuffersAreSwapped)
- effectImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- }
-
- UIGraphicsBeginImageContextWithOptions(self.size, NO, [[UIScreen mainScreen] scale]);
- CGContextRef outputContext = UIGraphicsGetCurrentContext();
- CGContextScaleCTM(outputContext, 1.0, -1.0);
- CGContextTranslateCTM(outputContext, 0, -self.size.height);
-
- CGContextDrawImage(outputContext, imageRect, self.CGImage);
-
- if (hasBlur) {
- CGContextSaveGState(outputContext);
- if (maskImage) {
- CGContextClipToMask(outputContext, imageRect, maskImage.CGImage);
- }
- CGContextDrawImage(outputContext, imageRect, effectImage.CGImage);
- CGContextRestoreGState(outputContext);
- }
-
- if (tintColor) {
- CGContextSaveGState(outputContext);
- CGContextSetFillColorWithColor(outputContext, tintColor.CGColor);
- CGContextFillRect(outputContext, imageRect);
- CGContextRestoreGState(outputContext);
- }
-
- UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return outputImage;
- }
- @end
|