-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
executable file
·105 lines (89 loc) · 3.22 KB
/
main.swift
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/swift
///////////////////////////////////
/**
Usage :
main.swift <inputpath> <width> <height> <outputpath>
example :
main.swift ../test.png 100 100 ../build
*/
///////////////////////////////////
import AppKit
import QuartzCore
//MARK: helper functions
extension NSImage {
func resizeImage(_ width: CGFloat, _ height: CGFloat) -> NSImage { let img = NSImage(size: CGSize(width:width, height:height))
img.lockFocus()
let ctx = NSGraphicsContext.current()
ctx?.imageInterpolation = .high
self.draw(in: NSMakeRect(0, 0, width, height), from: NSMakeRect(0, 0, size.width, size.height), operation: .copy, fraction: 1)
img.unlockFocus()
return img
}
@discardableResult
func saveAsPNG(_ filePath: String) -> Bool {
guard let tiffData = self.tiffRepresentation else {
print("failed to get tiffRepresentation. filePath: \(filePath)")
return false
}
let imageRep = NSBitmapImageRep(data: tiffData)
guard let imageData = imageRep?.representation(using: .PNG, properties: [:]) else {
print("failed to get PNG representation. filePath: \(filePath)")
return false
}
do {
try imageData.write(to:URL(fileURLWithPath: filePath))
return true
} catch {
print("failed to write to disk. filePath: \(filePath)")
return false
}
}
}
// arguments&...
let arguments = CommandLine.arguments
if arguments.count != 6 {
print("Usage :\n" +
" swift main.swift <input path> <width> <height> <output path> <base file name>")
exit(1)
}
let inputPath = arguments[1]
let widthString = arguments[2]
let heightString = arguments[3]
let width3x = CGFloat(Int(widthString)!)
let height3x = CGFloat(Int(heightString)!)
let width2x = (width3x/3.0) * 2.0
let height2x = (height3x / 3.0) * 2.0
let width1x = width3x / 3.0
let height1x = height3x / 3.0
let outputPath = arguments[4]
let baseFileName = arguments[5]
// checkout output path
if !FileManager.default.fileExists(atPath: outputPath, isDirectory: nil) {
do {
try FileManager.default.createDirectory(atPath: outputPath, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
print("Error creating directory: \(error.localizedDescription)")
}
}
if let image = NSImage(contentsOfFile:inputPath) {
// resize & save 3x
let newImage3x = image.resizeImage(width3x, height3x)
let output3xPath = outputPath+"/"+baseFileName+"@3x.png"
newImage3x.saveAsPNG(output3xPath)
print ("3x file saved at path : \(output3xPath)")
// resize & save 2x
let newImage2x = image.resizeImage(width2x, height2x)
let output2xPath = outputPath+"/"+baseFileName+"@2x.png"
newImage2x.saveAsPNG(output2xPath)
print ("2x file saved at path : \(output2xPath)")
// resize & save 1x
let newImage1x = image.resizeImage(width1x, height1x)
let output1xPath = outputPath+"/"+baseFileName+"@1x.png"
newImage1x.saveAsPNG(output1xPath)
print ("1x file saved at path : \(output1xPath)")
exit(0)
}
else {
print("Failed to get PNG representation. filePath: \(inputPath)")
exit(1)
}