CCGraphics.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. //
  2. // CCGraphics.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 04/02/16.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCGraphics.h"
  24. #import "CCUtility.h"
  25. #import "NSString+TruncateToWidth.h"
  26. @implementation CCGraphics
  27. + (UIImage *)thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time
  28. {
  29. AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
  30. NSParameterAssert(asset);
  31. AVAssetImageGenerator *assetIG =
  32. [[AVAssetImageGenerator alloc] initWithAsset:asset];
  33. assetIG.appliesPreferredTrackTransform = YES;
  34. assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
  35. CGImageRef thumbnailImageRef = NULL;
  36. CFTimeInterval thumbnailImageTime = time;
  37. NSError *igError = nil;
  38. thumbnailImageRef =
  39. [assetIG copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) actualTime:NULL error:&igError];
  40. if (!thumbnailImageRef) NSLog(@"[LOG] thumbnailImageGenerationError %@", igError );
  41. UIImage *thumbnailImage = thumbnailImageRef ? [[UIImage alloc] initWithCGImage:thumbnailImageRef] : nil;
  42. return thumbnailImage;
  43. }
  44. // mix two image
  45. + (UIImage *)overlayImage:(UIImage *)backgroundImage watermarkImage:(UIImage *)watermarkImage where:(NSString *)where
  46. {
  47. // example watermarkImage = [UIImage imageNamed:@"lock"];
  48. UIGraphicsBeginImageContext(backgroundImage.size);
  49. [backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
  50. if ([where isEqualToString:@"right"]) [watermarkImage drawInRect:CGRectMake(backgroundImage.size.width - watermarkImage.size.width, backgroundImage.size.height - watermarkImage.size.height, watermarkImage.size.width, watermarkImage.size.height)];
  51. if ([where isEqualToString:@"left"]) [watermarkImage drawInRect:CGRectMake(0, backgroundImage.size.height - watermarkImage.size.height, backgroundImage.size.width - watermarkImage.size.width, backgroundImage.size.height - watermarkImage.size.height)];
  52. UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
  53. UIGraphicsEndImageContext();
  54. return result;
  55. }
  56. + (UIImage *)generateImageFromVideo:(NSString *)videoPath
  57. {
  58. NSURL *url = [NSURL fileURLWithPath:videoPath];
  59. NSError *error = NULL;
  60. AVURLAsset* asset = [AVURLAsset URLAssetWithURL:url options:nil];
  61. AVAssetImageGenerator* imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
  62. imageGenerator.appliesPreferredTrackTransform = YES;
  63. // CMTime time = CMTimeMake(1, 65);
  64. CGImageRef cgImage = [imageGenerator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:nil error:&error];
  65. if(error) return nil;
  66. UIImage* image = [UIImage imageWithCGImage:cgImage];
  67. CGImageRelease(cgImage);
  68. return image;
  69. }
  70. + (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)targetSize
  71. {
  72. //If scaleFactor is not touched, no scaling will occur
  73. CGFloat scaleFactor = 1.0;
  74. //Deciding which factor to use to scale the image (factor = targetSize / imageSize)
  75. if (image.size.width > targetSize.width || image.size.height > targetSize.height)
  76. if (!((scaleFactor = (targetSize.width / image.size.width)) > (targetSize.height / image.size.height))) //scale to fit width, or
  77. scaleFactor = targetSize.height / image.size.height; // scale to fit heigth.
  78. UIGraphicsBeginImageContext(targetSize);
  79. //Creating the rect where the scaled image is drawn in
  80. CGRect rect = CGRectMake((targetSize.width - image.size.width * scaleFactor) / 2,
  81. (targetSize.height - image.size.height * scaleFactor) / 2,
  82. image.size.width * scaleFactor, image.size.height * scaleFactor);
  83. //Draw the image into the rect
  84. [image drawInRect:rect];
  85. //Saving the image, ending image context
  86. UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  87. UIGraphicsEndImageContext();
  88. return scaledImage;
  89. }
  90. + (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)targetSize isAspectRation:(BOOL)aspect
  91. {
  92. CGFloat originRatio = image.size.width / image.size.height;
  93. CGFloat newRatio = targetSize.width / targetSize.height;
  94. CGSize sz;
  95. CGFloat scale = 1.0;
  96. if (!aspect) {
  97. sz = targetSize;
  98. }else {
  99. if (originRatio < newRatio) {
  100. sz.height = targetSize.height;
  101. sz.width = targetSize.height * originRatio;
  102. }else {
  103. sz.width = targetSize.width;
  104. sz.height = targetSize.width / originRatio;
  105. }
  106. }
  107. sz.width /= scale;
  108. sz.height /= scale;
  109. UIGraphicsBeginImageContextWithOptions(sz, NO, scale);
  110. [image drawInRect:CGRectMake(0, 0, sz.width, sz.height)];
  111. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  112. UIGraphicsEndImageContext();
  113. return newImage;
  114. }
  115. + (UIImage *)createNewImageFrom:(NSString *)fileName directoryUser:(NSString *)directoryUser fileNameTo:(NSString *)fileNameTo extension:(NSString *)extension size:(NSString *)size imageForUpload:(BOOL)imageForUpload typeFile:(NSString *)typeFile writePreview:(BOOL)writePreview optimizedFileName:(BOOL)optimizedFileName
  116. {
  117. UIImage *originalImage;
  118. UIImage *scaleImage;
  119. CGRect rect;
  120. CGFloat width, height;
  121. NSString *ext = [extension lowercaseString];
  122. if ([[directoryUser substringFromIndex: [directoryUser length] - 1] isEqualToString:@"/"]) directoryUser = [directoryUser substringToIndex:[directoryUser length]-1];
  123. if (![[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", directoryUser, fileName]]) return nil;
  124. // only viedo / image
  125. if (![typeFile isEqualToString: k_metadataTypeFile_image] && ![typeFile isEqualToString: k_metadataTypeFile_video]) return nil;
  126. if ([typeFile isEqualToString: k_metadataTypeFile_image]) {
  127. originalImage = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", directoryUser, fileName]];
  128. }
  129. if ([typeFile isEqualToString: k_metadataTypeFile_video]) {
  130. // create symbolik link for read video file in temp
  131. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:@"tempvideo.mp4"] error:nil];
  132. [[NSFileManager defaultManager] linkItemAtPath:[NSString stringWithFormat:@"%@/%@", directoryUser, fileName] toPath:[NSTemporaryDirectory() stringByAppendingString:@"tempvideo.mp4"] error:nil];
  133. originalImage = [self generateImageFromVideo:[NSTemporaryDirectory() stringByAppendingString:@"tempvideo.mp4"]];
  134. }
  135. if ([size isEqualToString:@"xs"]) scaleImage = [self scaleImage:originalImage toSize:CGSizeMake(32, 32)];
  136. if ([size isEqualToString:@"s"]) scaleImage = [self scaleImage:originalImage toSize:CGSizeMake(64, 64)];
  137. if ([size isEqualToString:@"m"]) scaleImage = [self scaleImage:originalImage toSize:CGSizeMake(128, 128)];
  138. if ([size isEqualToString:@"l"]) scaleImage = [self scaleImage:originalImage toSize:CGSizeMake(640, 640)];
  139. if ([size isEqualToString:@"xl"]) scaleImage = [self scaleImage:originalImage toSize:CGSizeMake(1024, 1024)];
  140. scaleImage = [UIImage imageWithData:UIImageJPEGRepresentation(scaleImage, 0.5f)];
  141. // it is request write photo preview ?
  142. if (writePreview && scaleImage) {
  143. if (imageForUpload) {
  144. // write image preview in tmp for plist
  145. [self saveIcoWithEtag:fileNameTo image:scaleImage writeToFile:[NSTemporaryDirectory() stringByAppendingString:fileNameTo] copy:NO move:NO fromPath:nil toPath:nil];
  146. // if it is preview for Upload then trasform it in gray scale
  147. //TODO: Crash with swift
  148. scaleImage = [self grayscale:scaleImage];
  149. [self saveIcoWithEtag:fileNameTo image:scaleImage writeToFile:[NSString stringWithFormat:@"%@/%@.ico", directoryUser, fileNameTo] copy:NO move:NO fromPath:nil toPath:nil];
  150. } else {
  151. [self saveIcoWithEtag:fileNameTo image:scaleImage writeToFile:[NSString stringWithFormat:@"%@/%@.ico", directoryUser, fileNameTo] copy:NO move:NO fromPath:nil toPath:nil];
  152. }
  153. }
  154. // Optimized photos resolution
  155. // Resize image 640 x 480 ( with proportion : 1,333)
  156. if (originalImage.size.height < originalImage.size.width) {
  157. // (lanscape)
  158. width = 640;
  159. height = 480;
  160. } else {
  161. // (portrait)
  162. height = 640;
  163. width = 480;
  164. }
  165. // Optimized photos resolution
  166. if ([typeFile isEqualToString: k_metadataTypeFile_image] && [ext isEqualToString:@"gif"] == NO && optimizedFileName && scaleImage && (originalImage.size.width > width || originalImage.size.height > height)) {
  167. // conversion scale proportion
  168. if (height > width) {
  169. float proportion = originalImage.size.height / originalImage.size.width;
  170. width = height / proportion;
  171. } else {
  172. float proportion = originalImage.size.width / originalImage.size.height;
  173. height = width / proportion;
  174. }
  175. rect = CGRectMake(0,0,width,height);
  176. UIGraphicsBeginImageContext(rect.size);
  177. [originalImage drawInRect:rect];
  178. UIImage *resizeImage = UIGraphicsGetImageFromCurrentImageContext();
  179. UIGraphicsEndImageContext();
  180. resizeImage = [UIImage imageWithData:UIImageJPEGRepresentation(resizeImage, 0.5f)];
  181. if (resizeImage) [UIImagePNGRepresentation(resizeImage) writeToFile:[NSString stringWithFormat:@"%@/%@", directoryUser, fileNameTo] atomically: YES];
  182. }
  183. return scaleImage;
  184. }
  185. + (void)saveIcoWithEtag:(NSString *)fileID image:(UIImage *)image writeToFile:(NSString *)writeToFile copy:(BOOL)copy move:(BOOL)move fromPath:(NSString *)fromPath toPath:(NSString *)toPath
  186. {
  187. if (writeToFile)
  188. [UIImagePNGRepresentation(image) writeToFile:writeToFile atomically: YES];
  189. if (copy)
  190. [CCUtility copyFileAtPath:fromPath toPath:toPath];
  191. if (move)
  192. [[NSFileManager defaultManager] moveItemAtPath:fromPath toPath:toPath error:nil];
  193. }
  194. + (UIColor *)colorFromHexString:(NSString *)hexString
  195. {
  196. unsigned rgbValue = 0;
  197. NSScanner *scanner = [NSScanner scannerWithString:hexString];
  198. [scanner setScanLocation:1]; // bypass '#' character
  199. [scanner scanHexInt:&rgbValue];
  200. return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
  201. }
  202. + (UIImage *)changeThemingColorImage:(UIImage *)image color:(UIColor *)color
  203. {
  204. CGRect rect = CGRectMake(0, 0, image.size.width*2, image.size.height*2);
  205. UIGraphicsBeginImageContext(rect.size);
  206. CGContextRef context = UIGraphicsGetCurrentContext();
  207. CGContextClipToMask(context, rect, image.CGImage);
  208. CGContextSetFillColorWithColor(context, [color CGColor]);
  209. CGContextFillRect(context, rect);
  210. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  211. UIGraphicsEndImageContext();
  212. return [UIImage imageWithCGImage:img.CGImage scale:2.0 orientation: UIImageOrientationDownMirrored];
  213. }
  214. + (UIImage*)drawText:(NSString*)text inImage:(UIImage*)image colorText:(UIColor *)colorText sizeOfFont:(CGFloat)sizeOfFont
  215. {
  216. NSDictionary* attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:sizeOfFont], NSForegroundColorAttributeName:colorText};
  217. NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:text attributes:attributes];
  218. int x = image.size.width/2 - attributedString.size.width/2;
  219. int y = image.size.height/2 - attributedString.size.height/2;
  220. UIGraphicsBeginImageContext(image.size);
  221. [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
  222. CGRect rect = CGRectMake(x, y, image.size.width, image.size.height);
  223. [[UIColor whiteColor] set];
  224. [text drawInRect:CGRectIntegral(rect) withAttributes:attributes];
  225. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  226. newImage = [UIImage imageWithCGImage:newImage.CGImage scale:2 orientation:UIImageOrientationUp];
  227. UIGraphicsEndImageContext();
  228. return newImage;
  229. }
  230. // ------------------------------------------------------------------------------------------------------
  231. // MARK: Blur Image
  232. // ------------------------------------------------------------------------------------------------------
  233. + (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur toSize:(CGSize)toSize
  234. {
  235. CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage];
  236. CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur" keysAndValues:kCIInputImageKey, inputImage, @"inputRadius", @(blur), nil];
  237. CIImage *outputImage = filter.outputImage;
  238. CIContext *context = [CIContext contextWithOptions:nil];
  239. CGImageRef outImage = [context createCGImage:outputImage fromRect:[outputImage extent]];
  240. UIImage *blurImage = [UIImage imageWithCGImage:outImage];
  241. return [CCGraphics scaleImage:blurImage toSize:toSize isAspectRation:YES];
  242. }
  243. // ------------------------------------------------------------------------------------------------------
  244. // MARK: Is Light Color
  245. // ------------------------------------------------------------------------------------------------------
  246. /*
  247. Color visibility can be determined according to the following algorithm:
  248. (This is a suggested algorithm that is still open to change.)
  249. Two colors provide good color visibility if the brightness difference and the color difference between the two colors are greater than a set range.
  250. Color brightness is determined by the following formula:
  251. ((Red value X 299) + (Green value X 587) + (Blue value X 114)) / 1000
  252. Note: This algorithm is taken from a formula for converting RGB values to YIQ values. This brightness value gives a perceived brightness for a color.
  253. Color difference is determined by the following formula:
  254. (maximum (Red value 1, Red value 2) - minimum (Red value 1, Red value 2)) + (maximum (Green value 1, Green value 2) - minimum (Green value 1, Green value 2)) + (maximum (Blue value 1, Blue value 2) - minimum (Blue value 1, Blue value 2))
  255. */
  256. + (BOOL)isLight:(UIColor *)color
  257. {
  258. const CGFloat *componentColors = CGColorGetComponents(color.CGColor);
  259. CGFloat colorBrightness = ((componentColors[0] * 299) + (componentColors[1] * 587) + (componentColors[2] * 114)) / 1000;
  260. if (colorBrightness < 0.8) { // STD 0.5
  261. return false;
  262. } else {
  263. return true;
  264. }
  265. }
  266. + (UIImage *)grayscale:(UIImage *)sourceImage
  267. {
  268. /* const UInt8 luminance = (red * 0.2126) + (green * 0.7152) + (blue * 0.0722); // Good luminance value */
  269. /// Create a gray bitmap context
  270. const size_t width = (size_t)sourceImage.size.width;
  271. const size_t height = (size_t)sourceImage.size.height;
  272. const int kNyxNumberOfComponentsPerGreyPixel = 3;
  273. CGRect imageRect = CGRectMake(0, 0, sourceImage.size.width, sourceImage.size.height);
  274. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
  275. CGContextRef bmContext = CGBitmapContextCreate(NULL, width, height, 8/*Bits per component*/, width * kNyxNumberOfComponentsPerGreyPixel, colorSpace, kCGImageAlphaNone);
  276. CGColorSpaceRelease(colorSpace);
  277. if (!bmContext)
  278. return nil;
  279. /// Image quality
  280. CGContextSetShouldAntialias(bmContext, false);
  281. CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh);
  282. /// Draw the image in the bitmap context
  283. CGContextDrawImage(bmContext, imageRect, sourceImage.CGImage);
  284. /// Create an image object from the context
  285. CGImageRef grayscaledImageRef = CGBitmapContextCreateImage(bmContext);
  286. UIImage *grayscaled = [UIImage imageWithCGImage:grayscaledImageRef scale:sourceImage.scale orientation:sourceImage.imageOrientation];
  287. /// Cleanup
  288. CGImageRelease(grayscaledImageRef);
  289. CGContextRelease(bmContext);
  290. return grayscaled;
  291. }
  292. @end
  293. // ------------------------------------------------------------------------------------------------------
  294. // MARK: Avatar
  295. // ------------------------------------------------------------------------------------------------------
  296. @implementation CCAvatar
  297. - (id)initWithImage:(UIImage *)image borderColor:(UIColor*)borderColor borderWidth:(float)borderWidth
  298. {
  299. self = [super initWithImage:image];
  300. float cornerRadius = self.frame.size.height/2.0f;
  301. CALayer *layer = [self layer];
  302. [layer setMasksToBounds:YES];
  303. [layer setCornerRadius: cornerRadius];
  304. [layer setBorderWidth: borderWidth];
  305. [layer setBorderColor:[borderColor CGColor]];
  306. return self;
  307. }
  308. @end