|
| 1 | +// |
| 2 | +// NSImageExtension.swift |
| 3 | +// ScriptToolkit |
| 4 | +// |
| 5 | +// Created by Dan Cech on 05.03.2019. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import AppKit |
| 10 | + |
| 11 | +public extension NSImage { |
| 12 | + |
| 13 | + /// Returns the height of the current image. |
| 14 | + var height: CGFloat { |
| 15 | + return self.size.height |
| 16 | + } |
| 17 | + |
| 18 | + /// Returns the width of the current image. |
| 19 | + var width: CGFloat { |
| 20 | + return self.size.width |
| 21 | + } |
| 22 | + |
| 23 | + /// Returns a png representation of the current image. |
| 24 | + var PNGRepresentation: Data? { |
| 25 | + if let tiff = self.tiffRepresentation, let tiffData = NSBitmapImageRep(data: tiff) { |
| 26 | + return tiffData.representation(using: .png, properties: [:]) |
| 27 | + } |
| 28 | + |
| 29 | + return nil |
| 30 | + } |
| 31 | + |
| 32 | + /// Copies the current image and resizes it to the given size. |
| 33 | + /// |
| 34 | + /// - parameter size: The size of the new image. |
| 35 | + /// |
| 36 | + /// - returns: The resized copy of the given image. |
| 37 | + func copy(size: NSSize) throws -> NSImage { |
| 38 | + // Create a new rect with given width and height |
| 39 | + let frame = NSMakeRect(0, 0, size.width, size.height) |
| 40 | + |
| 41 | + // Get the best representation for the given size. |
| 42 | + guard let rep = self.bestRepresentation(for: frame, context: nil, hints: nil) else { |
| 43 | + throw ScriptError.generalError(message: "Unable to resize image") |
| 44 | + } |
| 45 | + |
| 46 | + // Create an empty image with the given size. |
| 47 | + let img = NSImage(size: size) |
| 48 | + |
| 49 | + // Set the drawing context and make sure to remove the focus before returning. |
| 50 | + img.lockFocus() |
| 51 | + defer { img.unlockFocus() } |
| 52 | + |
| 53 | + // Draw the new image |
| 54 | + if rep.draw(in: frame) { |
| 55 | + return img |
| 56 | + } |
| 57 | + |
| 58 | + // Return nil in case something went wrong. |
| 59 | + throw ScriptError.generalError(message: "Unable to resize image") |
| 60 | + } |
| 61 | + |
| 62 | + /// Saves the PNG representation of the current image to the HD. |
| 63 | + /// |
| 64 | + /// - parameter url: The location url to which to write the png file. |
| 65 | + func savePNGRepresentationToURL(url: URL) throws { |
| 66 | + if let png = self.PNGRepresentation { |
| 67 | + try png.write(to: url, options: .atomicWrite) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + func combine(withImage image: NSImage) throws -> NSImage { |
| 72 | + guard |
| 73 | + let firstImageData = image.tiffRepresentation, |
| 74 | + let firstImage = CIImage(data: firstImageData), |
| 75 | + let secondImageData = tiffRepresentation, |
| 76 | + let secondImage = CIImage(data: secondImageData) |
| 77 | + else { |
| 78 | + throw ScriptError.generalError(message: "Image processing error") |
| 79 | + } |
| 80 | + |
| 81 | + let filter = CIFilter(name: "CISourceOverCompositing")! |
| 82 | + filter.setDefaults() |
| 83 | + |
| 84 | + filter.setValue(firstImage, forKey: "inputImage") |
| 85 | + filter.setValue(secondImage, forKey: "inputBackgroundImage") |
| 86 | + |
| 87 | + let resultImage = filter.outputImage |
| 88 | + |
| 89 | + let rep = NSCIImageRep(ciImage: resultImage!) |
| 90 | + let finalResult = NSImage(size: rep.size) |
| 91 | + finalResult.addRepresentation(rep) |
| 92 | + |
| 93 | + return finalResult |
| 94 | + } |
| 95 | + |
| 96 | + func image(withText text: String, attributes: [NSAttributedString.Key: Any]) -> NSImage { |
| 97 | + let image = self |
| 98 | + let text = text as NSString |
| 99 | + let options: NSString.DrawingOptions = [.usesLineFragmentOrigin, .usesFontLeading] |
| 100 | + |
| 101 | + let textSize = text.boundingRect(with: image.size, options: options, attributes: attributes).size |
| 102 | + |
| 103 | + let x = (image.size.width - textSize.width) / 2 |
| 104 | + let y = (image.size.height - textSize.height) / 2 |
| 105 | + let point = NSMakePoint(x, y * 0.2) |
| 106 | + |
| 107 | + image.lockFocus() |
| 108 | + text.draw(at: point, withAttributes: attributes) |
| 109 | + image.unlockFocus() |
| 110 | + |
| 111 | + return image |
| 112 | + } |
| 113 | + |
| 114 | + func annotate(text: String, size: CGFloat, fill: NSColor, stroke: NSColor) -> NSImage { |
| 115 | + |
| 116 | + // Solid color text |
| 117 | + let fillText = image( |
| 118 | + withText: text, |
| 119 | + attributes: [ |
| 120 | + .foregroundColor: fill, |
| 121 | + .font: NSFont(name: "Impact", size: size)! |
| 122 | + ]) |
| 123 | + |
| 124 | + // Add strokes |
| 125 | + return fillText.image( |
| 126 | + withText: text, |
| 127 | + attributes: [ |
| 128 | + .foregroundColor: fill, |
| 129 | + .strokeColor: stroke, |
| 130 | + .strokeWidth: 1, |
| 131 | + .font: NSFont(name: "Impact", size: size)! |
| 132 | + ]) |
| 133 | + } |
| 134 | +} |
| 135 | + |
0 commit comments