Skip to content

Commit

Permalink
moving imguru from svn to git
Browse files Browse the repository at this point in the history
  • Loading branch information
FigBug committed Feb 6, 2012
0 parents commit 3f08771
Show file tree
Hide file tree
Showing 13 changed files with 8,849 additions and 0 deletions.
98 changes: 98 additions & 0 deletions ImgProc.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,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: nil];

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;
}
32 changes: 32 additions & 0 deletions NSImageAdditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// NSImage+MGCropExtensions.h
// ImageCropDemo
//
// Created by Matt Gemmell on 16/03/2006.
//

#import <Cocoa/Cocoa.h>

@interface NSImage (ImageAdditions)

typedef enum {
MGImageResizeCrop,
MGImageResizeCropStart,
MGImageResizeCropEnd,
MGImageResizeScale
} MGImageResizingMethod;

- (NSImage*)imageByScalingProportionallyToSize:(NSSize)targetSize;
- (NSImage*)imageByScalingProportionallyToSize:(NSSize)targetSize background:(NSColor*)bk;

- (void)drawInRect:(NSRect)dstRect operation:(NSCompositingOperation)op fraction:(float)delta method:(MGImageResizingMethod)resizeMethod;
- (NSImage*)imageToFitSize:(NSSize)size method:(MGImageResizingMethod)resizeMethod;
- (NSImage*)imageCroppedToFitSize:(NSSize)size;
- (NSImage*)imageScaledToFitSize:(NSSize)size;

- (NSImageRep*)largestRepresentation;
- (NSSize)sizeLargestRepresentation;

- (NSImage*)rotated:(int)angle;

@end
Loading

0 comments on commit 3f08771

Please sign in to comment.