-
Notifications
You must be signed in to change notification settings - Fork 7
/
ImgProc.mm
98 lines (70 loc) · 2.54 KB
/
ImgProc.mm
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
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "NSImageAdditions.h"
#include <string>
#include <vector>
void init()
{
NSApplicationLoad();
}
void browseTo(std::string url)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithUTF8String:url.c_str()]]];
[pool release];
}
std::string processImage(std::string inputFile, int optImageSize)
{
if (optImageSize == 0)
optImageSize = 10000;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray* supportedTypes = [NSImage imageTypes];
NSString* path = [NSString stringWithUTF8String: inputFile.c_str()];
NSError* error;
NSString* fileType = [[NSWorkspace sharedWorkspace] typeOfFile:path error: &error];
if (![supportedTypes containsObject:fileType])
return "";
NSDictionary* attr = [[NSFileManager defaultManager] attributesOfItemAtPath:path error: &error];
bool size = ([[attr objectForKey: NSFileSize] longLongValue] > 2 * 1024 * 1024);
bool type = (![fileType isEqual:@"com.compuserve.gif"] && ![fileType isEqual:@"public.png"] && ![fileType isEqual:@"public.jpeg"]);
NSImage* img = [[NSImage alloc] initByReferencingFile:path];
NSSize sz = [img sizeLargestRepresentation];
bool dims = (sz.width > optImageSize || sz.height > optImageSize);
std::string res = inputFile;
if (size || type || dims)
{
if (dims)
{
NSImage* newImg = [img imageByScalingProportionallyToSize:NSMakeSize(optImageSize,optImageSize)];
[img release];
img = [newImg retain];
}
NSBitmapImageFileType newType = NSJPEGFileType;
if ([fileType isEqual:@"com.compuserve.gif"])
newType = NSGIFFileType;
if ([fileType isEqual:@"public.png"])
newType = NSPNGFileType;
NSBitmapImageRep* bits = [NSBitmapImageRep imageRepWithData: [img TIFFRepresentation]];
NSData* data = [bits representationUsingType: NSPNGFileType properties: [NSDictionary dictionary]];
NSString* tempTemplate = [NSString stringWithFormat: @"%@XXXXXXXX", NSTemporaryDirectory()];
char tempFile[PATH_MAX];
strcpy(tempFile, [tempTemplate UTF8String]);
switch (newType)
{
case NSJPEGFileType: strcat(tempFile, ".jpg"); break;
case NSGIFFileType: strcat(tempFile, ".gif"); break;
case NSPNGFileType: strcat(tempFile, ".png"); break;
}
mktemp(tempFile);
FILE* fp = fopen(tempFile, "w");
if (fp)
{
fwrite([data bytes], 1, [data length], fp);
fclose(fp);
res = std::string(tempFile);
}
}
[img release];
[pool release];
return res;
}