blob: b53c8282c043308ffad7b97f43619dcdf471d834 [file] [log] [blame]
Alexandre Lision261f1b92016-04-04 12:35:34 -04001/*
2 * Copyright (C) 2016 Savoir-faire Linux Inc.
3 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#import "NSImage+Extensions.h"
21
22@implementation NSImage (Extensions)
23
24+ (NSImage *)imageResize:(NSImage*)anImage
25 newSize:(NSSize)newSize
26{
27 auto sourceImage = anImage;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040028 NSSize size = anImage.size;
Alexandre Lision261f1b92016-04-04 12:35:34 -040029 // Report an error if the source isn't a valid image
30 if (![sourceImage isValid]) {
31 NSLog(@"Invalid Image");
32 } else {
33 auto smallImage = [[NSImage alloc] initWithSize: newSize];
34 [smallImage lockFocus];
35 [sourceImage setSize: newSize];
36 [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
37 [sourceImage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.];
38 [smallImage unlockFocus];
39 return smallImage;
40 }
41 return nil;
42}
43
Kateryna Kostiukae660fd2018-04-24 14:10:41 -040044- (NSImage *) roundCorners:(CGFloat)radius {
45 NSImage *existingImage = self;
46 NSSize existingSize = [existingImage size];
47 NSSize newSize = NSMakeSize(existingSize.width, existingSize.height);
48 NSImage *composedImage = [[NSImage alloc] initWithSize:newSize];
49
50 [composedImage lockFocus];
51 [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
52
53 NSRect imageFrame = NSRectFromCGRect(CGRectMake(0, 0, existingSize.width, existingSize.height));
54 NSBezierPath *clipPath = [NSBezierPath bezierPathWithRoundedRect:imageFrame xRadius:radius yRadius:radius];
55 [clipPath setWindingRule:NSEvenOddWindingRule];
56 [clipPath addClip];
57
58 [existingImage drawAtPoint:NSZeroPoint fromRect:NSMakeRect(0, 0, newSize.width, newSize.height) operation:NSCompositeSourceOver fraction:1];
59
60 [composedImage unlockFocus];
61
62 return composedImage;
63}
64
65- (NSImage*) imageResizeInsideMax:(CGFloat) dimension {
66 if (self.size.width < dimension && self.size.height < dimension) {
67 return self;
68 }
69 CGFloat widthScaleFactor = dimension / self.size.width;
70 CGFloat heightScaleFactor = dimension / self.size.height;
71 CGFloat scale = MIN(widthScaleFactor, heightScaleFactor);
72 NSSize size = NSZeroSize;
73 size.width = self.size.width * scale;
74 size.height = self.size.height * scale;
75 return [NSImage imageResize:self newSize:size];
76}
77
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040078- (NSImage *) cropImageToSize:(NSSize)newSize {
79 CGImageSourceRef source;
80 NSPoint origin = CGPointMake((self.size.width - newSize.width) * 0.5, (self.size.height - newSize.height) * 0.5);
81
82 source = CGImageSourceCreateWithData((CFDataRef)[self TIFFRepresentation], NULL);
83 CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, 0, NULL);
84
85 CGRect sizeToBe = CGRectMake(origin.x, origin.y, newSize.width, newSize.height);
86 CGImageRef croppedImage = CGImageCreateWithImageInRect(imageRef, sizeToBe);
87 NSImage *finalImage = [[NSImage alloc] initWithCGImage:croppedImage size:NSZeroSize];
88 CFRelease(imageRef);
89 CFRelease(croppedImage);
90
91 return finalImage;
92
93}
94
Alexandre Lision261f1b92016-04-04 12:35:34 -040095@end