-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
168 lines (153 loc) · 6.35 KB
/
main.m
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSString *originPath = [[NSString alloc] init];
CGFloat sizeW = 0;
CGFloat sizeH = 0;
CGFloat radius = 0;
CGFloat scale = 1;
NSString *maskPath;
NSString *destPath = originPath;
CGFloat jpg = -1;
BOOL force = YES;
NSFileManager *fileManager = NSFileManager.defaultManager;
int c;
while ((c = getopt(argc, argv, ":i:o:w:h:r:s:m:j:f")) != -1)
switch(c) {
case 'i':
originPath = [NSString stringWithUTF8String:optarg];
if (![fileManager fileExistsAtPath:originPath]) {
printf(" [-i] No file found at %s\n", optarg);
return 1;
}
break;
case 'o':
destPath = [NSString stringWithUTF8String:optarg];
break;
case 'w':
sizeW = [[NSString stringWithUTF8String:optarg] floatValue];
if (sizeW < 0) {
printf(" [-w] Width needs to be a positive number\n");
return 1;
}
break;
case 'h':
sizeH = [[NSString stringWithUTF8String:optarg] floatValue];
if (sizeH < 0) {
printf(" [-h] Height needs to be a positive number\n");
return 1;
}
break;
case 'r':
radius = [[NSString stringWithUTF8String:optarg] floatValue];
if (radius < 0) {
printf(" [-r] Radius needs to be a positive number\n");
return 1;
}
break;
case 's':
scale = [[NSString stringWithUTF8String:optarg] floatValue];
if (scale <= 0) {
printf(" [-s] Scale needs to be a positive number\n");
return 1;
}
break;
case 'm':
maskPath = [NSString stringWithUTF8String:optarg];
if (![fileManager fileExistsAtPath:maskPath]) {
printf(" [-m] No file found at %s\n", optarg);
return 1;
}
break;
case 'j':
jpg = [[NSString stringWithUTF8String:optarg] floatValue];
if (jpg < 0 || jpg > 1) {
printf(" [-j] JPEG compression docs:\n"
" The quality of the resulting JPEG image, expressed as a value from 0.0 to 1.0.\n"
" The value 0.0 represents the maximum compression (or lowest quality) while the value 1.0 represents the least compression (or best quality).\n");
return 1;
}
break;
case 'f':
force = NO;
break;
case '?':
printf("Usage: %s [OPTIONS]\n"
" Options:\n"
" -i Input image path\n"
" -o Path to put new image (overwrite old by default)\n"
" -w Width of new image (current width by default)\n"
" -h Height of new image (current height by default)\n"
" -r Radius to clip edges by (none by default)\n"
" -s Scale by factor of, 0 to 1 (none by default)\n"
" -m Mask to add to (none by default)\n"
" -j JPEG compression, 0 to 1 (PNG by default)\n"
" -f Force upscaling images\n", argv[0]);
return 1;
break;
}
if (radius && jpg > -1) printf("Warning: JPEGs don’t have alpha, applying a radius may cause a white or black background\n");
BOOL isDir;
if ([fileManager fileExistsAtPath:destPath isDirectory:&isDir] && isDir) destPath = [destPath stringByAppendingPathComponent:originPath.lastPathComponent];
if ([fileManager isWritableFileAtPath:destPath]) {
printf("Write-permission to %s denied\n", destPath.UTF8String);
return 1;
}
if (!originPath) {
printf(" [-i] Input file is a required argument\n");
return 1;
}
UIImage *image = [UIImage imageWithContentsOfFile:originPath];
if (!image) {
printf(" [-i] Invalid file type\n");
return 1;
}
CGSize origSize = image.size;
CGFloat origScale = image.scale;
CGFloat deviceScale = UIScreen.mainScreen.scale;
CGFloat origW = origSize.width * origScale;
CGFloat origH = origSize.height * origScale;
CGFloat cSizeW = ((sizeW ? sizeW : origW)/deviceScale) * scale;
CGFloat cSizeH = ((sizeH ? sizeH : origH)/deviceScale) * scale;
if (force) {
BOOL badSize = NO;
if (sizeW > origW) {
printf(" [-w] Invalid width of %ld, original is only %ld\n", lroundf(sizeW), lroundf(origW));
badSize = YES;
}
if (sizeH > origH) {
printf(" [-h] Invalid height of %ld, original is only %ld\n", lroundf(sizeH), lroundf(origH));
badSize = YES;
}
if (scale > 1) {
printf(" [-s] To shrink images, use a scale 0 to 1\n");
badSize = YES;
}
if (badSize) {
printf(" Use -f to force upscaling\n");
return 1;
}
}
CGRect rect = CGRectMake(0, 0, cSizeW, cSizeH);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(cSizeW, cSizeH), NO, deviceScale);
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] addClip];
if (maskPath) {
UIImage *mask = [UIImage imageWithContentsOfFile:maskPath];
if (!mask) {
printf(" [-m] Invalid file type\n");
return 1;
}
[mask drawInRect:rect];
}
[image drawInRect:rect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData;
if (jpg > -1) imageData = UIImageJPEGRepresentation(newImage, jpg);
else imageData = UIImagePNGRepresentation(newImage);
if (destPath == originPath) [fileManager removeItemAtPath:destPath error:NULL];
if (![imageData writeToFile:destPath atomically:YES]) {
printf("Failed to write to file\n");
return 1;
}
return 0;
}