-
Notifications
You must be signed in to change notification settings - Fork 1
/
pngpaste.m
184 lines (165 loc) · 4.42 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
/*
* pngpaste
*/
#import "pngpaste.h"
void
usage ()
{
fprintf(stderr,
"Usage: %s [OPTIONS] <dest.png>\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)
{
if (image != NULL) {
NSArray *reps = [image representations];
id rep = [reps lastObject];
if ([rep isKindOfClass:[NSPDFImageRep class]]) {
return ImageTypePDF;
} else if ([rep isKindOfClass:[NSImageRep class]]) {
return ImageTypePNG;
}
}
return ImageTypeNone;
}
NSData *
extractPngData (NSImage *image)
{
ImageType imageType = extractImageType(image);
switch (imageType) {
case ImageTypePNG:
return extractPngDataFromPng(image);
break;
case ImageTypePDF:
return extractPngDataFromPdf(image);
break;
case ImageTypeNone:
default:
return NULL;
break;
}
}
NSData *
extractPngDataFromPng (NSImage *image)
{
return [NSBitmapImageRep
representationOfImageRepsInArray:[image representations]
usingType:NSPNGFileType
properties:nil];
}
NSData *
extractPngDataFromPdf (NSImage *image)
{
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:NSPNGFileType
properties:nil];
}
Parameters
parseArguments (int argc, char* const argv[])
{
Parameters params;
params.outputFile = NULL;
params.wantsVersion = NO;
params.wantsUsage = NO;
params.wantsStdout = NO;
params.malformed = NO;
int ch;
while ((ch = getopt(argc, argv, "vh?")) != -1) {
switch (ch) {
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;
}
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
NSImage *image = [[NSImage alloc] initWithPasteboard:pasteBoard];
NSData *pngData;
int exitCode;
if (image && ((pngData = extractPngData(image)) != NULL)) {
if (params.wantsStdout) {
NSFileHandle *stdout =
(NSFileHandle *)[NSFileHandle fileHandleWithStandardOutput];
[stdout writeData:pngData];
exitCode = EXIT_SUCCESS;
} else {
if ([pngData 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!");
exitCode = EXIT_FAILURE;
}
[image release];
[pasteBoard release];
[pool release];
return exitCode;
}