Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 98 lines (70 sloc) 2.575 kB
3f08771 @FigBug moving imguru from svn to git
authored
1 #import <Foundation/Foundation.h>
2 #import <AppKit/AppKit.h>
3
4 #import "NSImageAdditions.h"
5
6 #include <string>
7 #include <vector>
8
9 void init()
10 {
11 NSApplicationLoad();
12 }
13
14 void browseTo(std::string url)
15 {
16 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
17 [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithUTF8String:url.c_str()]]];
18 [pool release];
19 }
20
21 std::string processImage(std::string inputFile, int optImageSize)
22 {
23 if (optImageSize == 0)
24 optImageSize = 10000;
25
26 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
27
28 NSArray* supportedTypes = [NSImage imageTypes];
29
30 NSString* path = [NSString stringWithUTF8String: inputFile.c_str()];
31
32 NSError* error;
33 NSString* fileType = [[NSWorkspace sharedWorkspace] typeOfFile:path error: &error];
34
35 if (![supportedTypes containsObject:fileType])
36 return "";
37
38 NSDictionary* attr = [[NSFileManager defaultManager] attributesOfItemAtPath:path error: &error];
39 bool size = ([[attr objectForKey: NSFileSize] longLongValue] > 2 * 1024 * 1024);
40
41 bool type = (![fileType isEqual:@"com.compuserve.gif"] && ![fileType isEqual:@"public.png"] && ![fileType isEqual:@"public.jpeg"]);
42
43 NSImage* img = [[NSImage alloc] initByReferencingFile:path];
44
45 NSSize sz = [img sizeLargestRepresentation];
46 bool dims = (sz.width > optImageSize || sz.height > optImageSize);
47
48 std::string res = inputFile;
49
50 if (size || type || dims)
51 {
52 if (dims)
53 {
54 NSImage* newImg = [img imageByScalingProportionallyToSize:NSMakeSize(optImageSize,optImageSize)];
55 [img release];
56 img = [newImg retain];
57 }
58
59 NSBitmapImageFileType newType = NSJPEGFileType;
60
61 if ([fileType isEqual:@"com.compuserve.gif"])
62 newType = NSGIFFileType;
63 if ([fileType isEqual:@"public.png"])
64 newType = NSPNGFileType;
65
66 NSBitmapImageRep* bits = [NSBitmapImageRep imageRepWithData: [img TIFFRepresentation]];
67 NSData* data = [bits representationUsingType: NSPNGFileType properties: nil];
68
69 NSString* tempTemplate = [NSString stringWithFormat: @"%@XXXXXXXX", NSTemporaryDirectory()];
70
71 char tempFile[PATH_MAX];
72 strcpy(tempFile, [tempTemplate UTF8String]);
73
74 switch (newType)
75 {
76 case NSJPEGFileType: strcat(tempFile, ".jpg"); break;
77 case NSGIFFileType: strcat(tempFile, ".gif"); break;
78 case NSPNGFileType: strcat(tempFile, ".png"); break;
79 }
80
81 mktemp(tempFile);
82
83 FILE* fp = fopen(tempFile, "w");
84 if (fp)
85 {
86 fwrite([data bytes], 1, [data length], fp);
87 fclose(fp);
88
89 res = std::string(tempFile);
90 }
91 }
92
93 [img release];
94
95 [pool release];
96
97 return res;
98 }
Something went wrong with that request. Please try again.