Browse Source

Add UIImage extension to blur imagery

Contains code by Dmitry Klimkin licensed under MIT
Felix Paul Kühne 11 years ago
parent
commit
b682bc73d0
3 changed files with 123 additions and 0 deletions
  1. 27 0
      Sources/UIImage+Blur.h
  2. 86 0
      Sources/UIImage+Blur.m
  3. 10 0
      VLC for iOS.xcodeproj/project.pbxproj

+ 27 - 0
Sources/UIImage+Blur.h

@@ -0,0 +1,27 @@
+/* This file is adapted from DKLiveBlur - https://github.com/kronik/DKLiveBlur
+ *
+ * Copyright (C) 2013 by Dmitry Klimkin
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE. */
+
+@interface UIImage (Blur)
+
++ (UIImage *)applyBlurOnImage:(UIImage *)imageToBlur withRadius:(CGFloat)blurRadius;
+
+@end

+ 86 - 0
Sources/UIImage+Blur.m

@@ -0,0 +1,86 @@
+/* This file is adapted from DKLiveBlur - https://github.com/kronik/DKLiveBlur
+ *
+ * Copyright (C) 2013 by Dmitry Klimkin
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE. */
+
+#import <Accelerate/Accelerate.h>
+
+@implementation UIImage (Blur)
+
++ (UIImage *)applyBlurOnImage:(UIImage *)imageToBlur withRadius:(CGFloat)blurRadius {
+    if ((blurRadius < 0.0f) || (blurRadius > 1.0f)) {
+        blurRadius = 0.5f;
+    }
+
+    int boxSize = (int)(blurRadius * 100);
+    boxSize -= (boxSize % 2) + 1;
+
+    CGImageRef rawImage = imageToBlur.CGImage;
+
+    vImage_Buffer inBuffer, outBuffer;
+    vImage_Error error;
+    void *pixelBuffer;
+
+    CGDataProviderRef inProvider = CGImageGetDataProvider(rawImage);
+    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
+
+    inBuffer.width = CGImageGetWidth(rawImage);
+    inBuffer.height = CGImageGetHeight(rawImage);
+    inBuffer.rowBytes = CGImageGetBytesPerRow(rawImage);
+    inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
+
+    pixelBuffer = malloc(CGImageGetBytesPerRow(rawImage) * CGImageGetHeight(rawImage));
+
+    outBuffer.data = pixelBuffer;
+    outBuffer.width = CGImageGetWidth(rawImage);
+    outBuffer.height = CGImageGetHeight(rawImage);
+    outBuffer.rowBytes = CGImageGetBytesPerRow(rawImage);
+
+    error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL,
+                                       0, 0, boxSize, boxSize, NULL,
+                                       kvImageEdgeExtend);
+    if (error)
+        APLog(@"blur error: convolution failed");
+
+        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
+
+        CGContextRef ctx = CGBitmapContextCreate(outBuffer.data,
+                                                 outBuffer.width,
+                                                 outBuffer.height,
+                                                 8,
+                                                 outBuffer.rowBytes,
+                                                 colorSpace,
+                                                 CGImageGetBitmapInfo(imageToBlur.CGImage));
+
+        CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
+        UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
+
+        //clean up
+        CGContextRelease(ctx);
+        CGColorSpaceRelease(colorSpace);
+
+        free(pixelBuffer);
+        CFRelease(inBitmapData);
+        CGImageRelease(imageRef);
+        
+        return returnImage;
+}
+
+@end

+ 10 - 0
VLC for iOS.xcodeproj/project.pbxproj

@@ -94,6 +94,8 @@
 		7D16035E17BF9FE600F29B34 /* sudHeaderBg@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 7D16035A17BF9FE600F29B34 /* sudHeaderBg@2x.png */; };
 		7D16035F17BF9FE600F29B34 /* headerSidebar@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 7D16035B17BF9FE600F29B34 /* headerSidebar@2x.png */; };
 		7D16036017BF9FE600F29B34 /* headerSidebar.png in Resources */ = {isa = PBXBuildFile; fileRef = 7D16035C17BF9FE600F29B34 /* headerSidebar.png */; };
+		7D168F7118D4A21B003FAF59 /* Accelerate.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7D168F7018D4A21B003FAF59 /* Accelerate.framework */; };
+		7D168F7418D4A33F003FAF59 /* UIImage+Blur.m in Sources */ = {isa = PBXBuildFile; fileRef = 7D168F7318D4A33F003FAF59 /* UIImage+Blur.m */; };
 		7D1AC3041762996100BD2EB5 /* resetIcon.png in Resources */ = {isa = PBXBuildFile; fileRef = 7D1AC3021762996100BD2EB5 /* resetIcon.png */; };
 		7D1AC3051762996100BD2EB5 /* resetIcon@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 7D1AC3031762996100BD2EB5 /* resetIcon@2x.png */; };
 		7D1AC30817629AB600BD2EB5 /* ratioIcon.png in Resources */ = {isa = PBXBuildFile; fileRef = 7D1AC30617629AB600BD2EB5 /* ratioIcon.png */; };
@@ -523,6 +525,9 @@
 		7D168F6D18D49E74003FAF59 /* lv */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = lv; path = "lv.lproj/badgeUnread@2x~iphone.png"; sourceTree = "<group>"; };
 		7D168F6E18D49E74003FAF59 /* lv */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = lv; path = "lv.lproj/badgeUnread~ipad.png"; sourceTree = "<group>"; };
 		7D168F6F18D49E74003FAF59 /* lv */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = lv; path = "lv.lproj/badgeUnread~iphone.png"; sourceTree = "<group>"; };
+		7D168F7018D4A21B003FAF59 /* Accelerate.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Accelerate.framework; path = System/Library/Frameworks/Accelerate.framework; sourceTree = SDKROOT; };
+		7D168F7218D4A317003FAF59 /* UIImage+Blur.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIImage+Blur.h"; path = "Sources/UIImage+Blur.h"; sourceTree = SOURCE_ROOT; };
+		7D168F7318D4A33F003FAF59 /* UIImage+Blur.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIImage+Blur.m"; path = "Sources/UIImage+Blur.m"; sourceTree = SOURCE_ROOT; };
 		7D19492B17C661A300959800 /* el */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = el; path = el.lproj/Localizable.strings; sourceTree = "<group>"; };
 		7D19492C17C661A300959800 /* el */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = el; path = "el.lproj/badgeUnread@2x~ipad.png"; sourceTree = "<group>"; };
 		7D19492D17C661A300959800 /* el */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = el; path = "el.lproj/badgeUnread@2x~iphone.png"; sourceTree = "<group>"; };
@@ -1152,6 +1157,7 @@
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				7D168F7118D4A21B003FAF59 /* Accelerate.framework in Frameworks */,
 				CCE2A22E17A5859E00D9EAAD /* CoreText.framework in Frameworks */,
 				7D89788A185E1344009BAB5D /* libCrashReporter.a in Frameworks */,
 				7DF7CA0717650C2A00C61739 /* AVFoundation.framework in Frameworks */,
@@ -1760,6 +1766,7 @@
 		7D94FCDD16DE7D1000F2623B /* Frameworks */ = {
 			isa = PBXGroup;
 			children = (
+				7D168F7018D4A21B003FAF59 /* Accelerate.framework */,
 				7D897889185E1344009BAB5D /* libCrashReporter.a */,
 				7D89788B185E1353009BAB5D /* libQuincyLib.a */,
 				7D897894185E14A7009BAB5D /* libGTLTouchStaticLib.a */,
@@ -2179,6 +2186,8 @@
 				7D3784C5183A9972009EE944 /* NSString+SupportedMedia.m */,
 				7D3784C6183A9972009EE944 /* UIDevice+SpeedCategory.h */,
 				7D3784C7183A9972009EE944 /* UIDevice+SpeedCategory.m */,
+				7D168F7218D4A317003FAF59 /* UIImage+Blur.h */,
+				7D168F7318D4A33F003FAF59 /* UIImage+Blur.m */,
 			);
 			name = Extensions;
 			sourceTree = "<group>";
@@ -2609,6 +2618,7 @@
 				7D6B08F1174D65B500A05173 /* IASKSettingsStoreUserDefaults.m in Sources */,
 				7D6B08F2174D65B500A05173 /* IASKSpecifier.m in Sources */,
 				7D6B08F3174D65B500A05173 /* IASKPSSliderSpecifierViewCell.m in Sources */,
+				7D168F7418D4A33F003FAF59 /* UIImage+Blur.m in Sources */,
 				7D6B08F4174D65B500A05173 /* IASKPSTextFieldSpecifierViewCell.m in Sources */,
 				7D6B08F5174D65B500A05173 /* IASKPSTitleValueSpecifierViewCell.m in Sources */,
 				9B5BEF2917FBAEA50016F9CB /* GTLDrive_Sources.m in Sources */,