NSImage+Extensions.mm 3.6 KB
Newer Older
1
/*
Sébastien Blin's avatar
Sébastien Blin committed
2
 *  Copyright (C) 2016-2019 Savoir-faire Linux Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *  Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

#import "NSImage+Extensions.h"

@implementation NSImage (Extensions)

+ (NSImage *)imageResize:(NSImage*)anImage
                 newSize:(NSSize)newSize
{
    auto sourceImage = anImage;
28
    NSSize size = anImage.size;
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    // Report an error if the source isn't a valid image
    if (![sourceImage isValid]) {
        NSLog(@"Invalid Image");
    } else {
        auto smallImage = [[NSImage alloc] initWithSize: newSize];
        [smallImage lockFocus];
        [sourceImage setSize: newSize];
        [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
        [sourceImage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.];
        [smallImage unlockFocus];
        return smallImage;
    }
    return nil;
}

44
- (NSImage *) roundCorners:(CGFloat)radius {
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
45
    NSSize existingSize = [self size];
46 47 48 49 50 51 52 53 54 55 56
    NSSize newSize = NSMakeSize(existingSize.width, existingSize.height);
    NSImage *composedImage = [[NSImage alloc] initWithSize:newSize];

    [composedImage lockFocus];
    [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];

    NSRect imageFrame = NSRectFromCGRect(CGRectMake(0, 0, existingSize.width, existingSize.height));
    NSBezierPath *clipPath = [NSBezierPath bezierPathWithRoundedRect:imageFrame xRadius:radius yRadius:radius];
    [clipPath setWindingRule:NSEvenOddWindingRule];
    [clipPath addClip];

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
57
    [self drawAtPoint:NSZeroPoint fromRect:NSMakeRect(0, 0, newSize.width, newSize.height) operation:NSCompositeSourceOver fraction:1];
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

    [composedImage unlockFocus];

    return composedImage;
}

- (NSImage*) imageResizeInsideMax:(CGFloat) dimension {
    if (self.size.width < dimension && self.size.height < dimension) {
        return self;
    }
    CGFloat widthScaleFactor = dimension / self.size.width;
    CGFloat heightScaleFactor = dimension / self.size.height;
    CGFloat scale = MIN(widthScaleFactor, heightScaleFactor);
    NSSize size = NSZeroSize;
    size.width = self.size.width * scale;
    size.height = self.size.height * scale;
    return [NSImage imageResize:self newSize:size];
}

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
- (NSImage *) cropImageToSize:(NSSize)newSize {
    CGImageSourceRef source;
    NSPoint origin = CGPointMake((self.size.width - newSize.width) * 0.5, (self.size.height - newSize.height) * 0.5);

    source = CGImageSourceCreateWithData((CFDataRef)[self TIFFRepresentation], NULL);
    CGImageRef imageRef =  CGImageSourceCreateImageAtIndex(source, 0, NULL);

    CGRect sizeToBe = CGRectMake(origin.x, origin.y, newSize.width, newSize.height);
    CGImageRef croppedImage = CGImageCreateWithImageInRect(imageRef, sizeToBe);
    NSImage *finalImage = [[NSImage alloc] initWithCGImage:croppedImage size:NSZeroSize];
    CFRelease(imageRef);
    CFRelease(croppedImage);

    return finalImage;

}

94
@end