Scaling an image with Quartz and Objective-C

I wrote some code to scale images using Rails and Python running on OS X. Its horribly slow. So I tried Objective-C which wasn't too bad at all. Here is the result of that exercise.

#include <CoreServices/CoreServices.h>
#include <Quartz/Quartz.h>

int main (int argc, const char * argv[]) {
	
	NSAutoreleasePool *pool =  [[NSAutoreleasePool alloc] init];
	
	NSString * path    = @"000001.jpg";
	NSImage * image = [[NSImage alloc] initWithContentsOfFile:path];
	NSSize targetSize = NSMakeSize(256, 256);
	NSSize originalSize = [image size];
	float originalWidth = originalSize.width;
	float originalHeight = originalSize.height;
	float targetWidth = targetSize.width;
	float targetHeight = targetSize.height;
	float scaleFactor = 1.0;
	if(originalWidth > originalHeight)
	{
		scaleFactor = targetWidth / originalWidth;		
	}
	else
	{
		scaleFactor = targetHeight / originalHeight;
	}	
	float scaledWidth = originalWidth * scaleFactor;
	float scaledHeight = originalHeight * scaleFactor;	
	targetSize = NSMakeSize(scaledWidth, scaledHeight);
		
	NSPoint thumbnailPoint = NSZeroPoint;
	NSImage* newImage = [[NSImage alloc] initWithSize:targetSize];
	[newImage lockFocus];
	NSRect thumbnailRect;
	thumbnailRect.origin = thumbnailPoint;
	thumbnailRect.size.width = targetSize.width;
	thumbnailRect.size.height = targetSize.height;
	[image drawInRect: thumbnailRect
			fromRect: NSZeroRect
		   operation: NSCompositeSourceOver
			fraction: 1.0];
	[newImage unlockFocus];	
	
	NSData *imageData = [newImage TIFFRepresentation];
	NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData: imageData];
	NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor];
	NSData *data = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];
	[data writeToFile: @"000001_c.jpg" atomically:YES];
	
	[pool release];
	
	NSLog(@"Done");
	
    return 0;
}

Comments

/groups/development/search/index.rss?tag=hotlist/groups/development/search/?tag=hotWhat’s HotHotListHot!?tag=hot0/groups/development/sidebar/HotListNo items tagged with hot.hot/groups/development/search/index.rss?sort=modifiedDate&kind=all&sortDirection=reverse&excludePages=wiki/welcomelist/groups/development/search/?sort=modifiedDate&kind=all&sortDirection=reverse&excludePages=wiki/welcomeRecent ChangesRecentChangesListUpdates?sort=modifiedDate&kind=all&sortDirection=reverse&excludePages=wiki/welcome0/groups/development/sidebar/RecentChangesListmodifiedDateallRecent ChangesRecentChangesListUpdateswiki/welcomeNo recent changes.reverse5search