-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathpngpaste.m
243 lines (221 loc) · 6.78 KB
/
pngpaste.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* pngpaste
*/
#import "pngpaste.h"
void
usage ()
{
fprintf(stderr,
"Usage: %s [OPTIONS] <dest.png>\n"
"\t-\t" "Print to standard output" "\n"
"\t-b\t" "Print to standard output as base64" "\n"
"\t-v\t" "Version" "\n"
"\t-h,-?\t" "This usage" "\n",
APP_NAME);
}
void
fatal (const char *msg)
{
if (msg != NULL) {
fprintf(stderr, "%s: %s\n", APP_NAME, msg);
}
}
void
version ()
{
fprintf(stderr, "%s %s\n", APP_NAME, APP_VERSION);
}
ImageType
extractImageType (NSImage *image)
{
ImageType imageType = ImageTypeNone;
if (image != nil) {
NSArray *reps = [image representations];
NSImageRep *rep = [reps lastObject];
if ([rep isKindOfClass:[NSPDFImageRep class]]) {
imageType = ImageTypePDF;
} else if ([rep isKindOfClass:[NSBitmapImageRep class]]) {
imageType = ImageTypeBitmap;
}
}
return imageType;
}
NSData *
renderImageData (NSImage *image, NSBitmapImageFileType bitmapImageFileType)
{
ImageType imageType = extractImageType(image);
switch (imageType) {
case ImageTypeBitmap:
return renderFromBitmap(image, bitmapImageFileType);
break;
case ImageTypePDF:
return renderFromPDF(image, bitmapImageFileType);
break;
case ImageTypeNone:
default:
return nil;
break;
}
}
NSData *
renderFromBitmap (NSImage *image, NSBitmapImageFileType bitmapImageFileType)
{
return [NSBitmapImageRep representationOfImageRepsInArray:[image representations]
usingType:bitmapImageFileType
properties:@{}];
}
NSData *
renderFromPDF (NSImage *image, NSBitmapImageFileType bitmapImageFileType)
{
NSPDFImageRep *pdfImageRep =
(NSPDFImageRep *)[[image representations] lastObject];
CGFloat factor = PDF_SCALE_FACTOR;
NSRect bounds = NSMakeRect(
0, 0,
pdfImageRep.bounds.size.width * factor,
pdfImageRep.bounds.size.height * factor);
NSImage *genImage = [[NSImage alloc] initWithSize:bounds.size];
[genImage lockFocus];
[[NSColor whiteColor] set];
NSRectFill(bounds);
[pdfImageRep drawInRect:bounds];
[genImage unlockFocus];
NSData *genImageData = [genImage TIFFRepresentation];
return [[NSBitmapImageRep imageRepWithData:genImageData]
representationUsingType:bitmapImageFileType
properties:@{}];
}
/*
* Returns NSBitmapImageFileType based off of filename extension
*/
NSBitmapImageFileType
getBitmapImageFileTypeFromFilename (NSString *filename)
{
static NSDictionary *lookup;
if (lookup == nil) {
lookup = @{
@"gif": [NSNumber numberWithInt:NSBitmapImageFileTypeGIF],
@"jpeg": [NSNumber numberWithInt:NSBitmapImageFileTypeJPEG],
@"jpg": [NSNumber numberWithInt:NSBitmapImageFileTypeJPEG],
@"png": [NSNumber numberWithInt:NSBitmapImageFileTypePNG],
@"tif": [NSNumber numberWithInt:NSBitmapImageFileTypeTIFF],
@"tiff": [NSNumber numberWithInt:NSBitmapImageFileTypeTIFF],
};
}
NSBitmapImageFileType bitmapImageFileType = NSBitmapImageFileTypePNG;
if (filename != nil) {
NSArray *words = [filename componentsSeparatedByString:@"."];
NSUInteger len = [words count];
if (len > 1) {
NSString *extension = (NSString *)[words objectAtIndex:(len - 1)];
NSString *lowercaseExtension = [extension lowercaseString];
NSNumber *value = lookup[lowercaseExtension];
if (value != nil) {
bitmapImageFileType = [value unsignedIntegerValue];
}
}
}
return bitmapImageFileType;
}
/*
* Returns NSData from Pasteboard Image if available; otherwise nil
*/
NSData *
getPasteboardImageData (NSBitmapImageFileType bitmapImageFileType)
{
NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
NSImage *image = [[NSImage alloc] initWithPasteboard:pasteBoard];
NSData *imageData = nil;
if (image != nil) {
imageData = renderImageData(image, bitmapImageFileType);
}
[image release];
return imageData;
}
Parameters
parseArguments (int argc, char* const argv[])
{
Parameters params;
params.outputFile = nil;
params.wantsVersion = NO;
params.wantsUsage = NO;
params.wantsBase64 = NO;
params.wantsStdout = NO;
params.malformed = NO;
int ch;
while ((ch = getopt(argc, argv, "bvh?")) != -1) {
switch (ch) {
case 'b':
params.wantsBase64 = YES;
return params;
break;
case 'v':
params.wantsVersion = YES;
return params;
break;
case 'h':
case '?':
params.wantsUsage = YES;
return params;
break;
default:
params.malformed = YES;
return params;
break;
}
}
if (argc < 2) {
params.malformed = YES;
} else if (!strcmp(argv[1],STDOUT_FILENAME)) {
params.wantsStdout = YES;
} else {
params.outputFile =
[[NSString alloc] initWithCString:argv[1]
encoding:NSUTF8StringEncoding];
}
return params;
}
int
main (int argc, char * const argv[])
{
Parameters params = parseArguments(argc, argv);
if (params.malformed) {
usage();
return EXIT_FAILURE;
} else if (params.wantsUsage) {
usage();
return EXIT_SUCCESS;
} else if (params.wantsVersion) {
version();
return EXIT_SUCCESS;
}
NSBitmapImageFileType bitmapImageFileType =
getBitmapImageFileTypeFromFilename(params.outputFile);
NSData *imageData = getPasteboardImageData(bitmapImageFileType);
int exitCode;
if (imageData != nil) {
if (params.wantsStdout) {
NSFileHandle *stdout =
(NSFileHandle *)[NSFileHandle fileHandleWithStandardOutput];
[stdout writeData:imageData];
exitCode = EXIT_SUCCESS;
} else if (params.wantsBase64) {
NSFileHandle *stdout =
(NSFileHandle *)[NSFileHandle fileHandleWithStandardOutput];
NSData *base64Data = [imageData base64EncodedDataWithOptions:0];
[stdout writeData:base64Data];
exitCode = EXIT_SUCCESS;
} else {
if ([imageData writeToFile:params.outputFile atomically:YES]) {
exitCode = EXIT_SUCCESS;
} else {
fatal("Could not write to file!");
exitCode = EXIT_FAILURE;
}
}
} else {
fatal("No image data found on the clipboard, or could not convert!");
exitCode = EXIT_FAILURE;
}
return exitCode;
}