diff --git a/CocoaneticsBench.xcodeproj/project.pbxproj b/CocoaneticsBench.xcodeproj/project.pbxproj index d42d604..9c9ab05 100644 --- a/CocoaneticsBench.xcodeproj/project.pbxproj +++ b/CocoaneticsBench.xcodeproj/project.pbxproj @@ -18,6 +18,13 @@ A70C0CA21435A79C00CC3C72 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = A70C0CA01435A79C00CC3C72 /* MainWindow.xib */; }; A70C0CA51435A79C00CC3C72 /* RootViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A70C0CA41435A79C00CC3C72 /* RootViewController.m */; }; A70C0CA81435A79C00CC3C72 /* RootViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A70C0CA61435A79C00CC3C72 /* RootViewController.xib */; }; + A70C0CBE1435AD6600CC3C72 /* ImageBenchmark.m in Sources */ = {isa = PBXBuildFile; fileRef = A70C0CBD1435AD6600CC3C72 /* ImageBenchmark.m */; }; + A70C0CC81435CD2500CC3C72 /* Flower_2048x1536.png in Resources */ = {isa = PBXBuildFile; fileRef = A70C0CC31435CD2500CC3C72 /* Flower_2048x1536.png */; }; + A70C0CC91435CD2500CC3C72 /* Flower_1024x768.png in Resources */ = {isa = PBXBuildFile; fileRef = A70C0CC41435CD2500CC3C72 /* Flower_1024x768.png */; }; + A70C0CCA1435CD2500CC3C72 /* Flower_128x96.png in Resources */ = {isa = PBXBuildFile; fileRef = A70C0CC51435CD2500CC3C72 /* Flower_128x96.png */; }; + A70C0CCB1435CD2500CC3C72 /* Flower_256x192.png in Resources */ = {isa = PBXBuildFile; fileRef = A70C0CC61435CD2500CC3C72 /* Flower_256x192.png */; }; + A70C0CCC1435CD2500CC3C72 /* Flower_512x384.png in Resources */ = {isa = PBXBuildFile; fileRef = A70C0CC71435CD2500CC3C72 /* Flower_512x384.png */; }; + A70C0CCE1435DFAA00CC3C72 /* ImageIO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A70C0CCD1435DFAA00CC3C72 /* ImageIO.framework */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -26,17 +33,25 @@ A70C0C8B1435A79C00CC3C72 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; A70C0C8D1435A79C00CC3C72 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; A70C0C8F1435A79C00CC3C72 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; }; - A70C0C931435A79C00CC3C72 /* CocoaneticsBench-Info.plist */ = {isa = PBXFileReference; path = "CocoaneticsBench-Info.plist"; sourceTree = ""; }; + A70C0C931435A79C00CC3C72 /* CocoaneticsBench-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "CocoaneticsBench-Info.plist"; sourceTree = ""; }; A70C0C951435A79C00CC3C72 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; A70C0C971435A79C00CC3C72 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; - A70C0C991435A79C00CC3C72 /* CocoaneticsBench-Prefix.pch */ = {isa = PBXFileReference; path = "CocoaneticsBench-Prefix.pch"; sourceTree = ""; }; - A70C0C9A1435A79C00CC3C72 /* CocoaneticsBenchAppDelegate.h */ = {isa = PBXFileReference; path = CocoaneticsBenchAppDelegate.h; sourceTree = ""; }; + A70C0C991435A79C00CC3C72 /* CocoaneticsBench-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "CocoaneticsBench-Prefix.pch"; sourceTree = ""; }; + A70C0C9A1435A79C00CC3C72 /* CocoaneticsBenchAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CocoaneticsBenchAppDelegate.h; sourceTree = ""; }; A70C0C9B1435A79C00CC3C72 /* CocoaneticsBenchAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CocoaneticsBenchAppDelegate.m; sourceTree = ""; }; A70C0C9E1435A79C00CC3C72 /* CocoaneticsBench.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = CocoaneticsBench.xcdatamodel; sourceTree = ""; }; A70C0CA11435A79C00CC3C72 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/MainWindow.xib; sourceTree = ""; }; - A70C0CA31435A79C00CC3C72 /* RootViewController.h */ = {isa = PBXFileReference; path = RootViewController.h; sourceTree = ""; }; + A70C0CA31435A79C00CC3C72 /* RootViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RootViewController.h; sourceTree = ""; }; A70C0CA41435A79C00CC3C72 /* RootViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RootViewController.m; sourceTree = ""; }; A70C0CA71435A79C00CC3C72 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/RootViewController.xib; sourceTree = ""; }; + A70C0CBC1435AD6600CC3C72 /* ImageBenchmark.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImageBenchmark.h; sourceTree = ""; }; + A70C0CBD1435AD6600CC3C72 /* ImageBenchmark.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ImageBenchmark.m; sourceTree = ""; }; + A70C0CC31435CD2500CC3C72 /* Flower_2048x1536.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Flower_2048x1536.png; sourceTree = ""; }; + A70C0CC41435CD2500CC3C72 /* Flower_1024x768.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Flower_1024x768.png; sourceTree = ""; }; + A70C0CC51435CD2500CC3C72 /* Flower_128x96.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Flower_128x96.png; sourceTree = ""; }; + A70C0CC61435CD2500CC3C72 /* Flower_256x192.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Flower_256x192.png; sourceTree = ""; }; + A70C0CC71435CD2500CC3C72 /* Flower_512x384.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Flower_512x384.png; sourceTree = ""; }; + A70C0CCD1435DFAA00CC3C72 /* ImageIO.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ImageIO.framework; path = System/Library/Frameworks/ImageIO.framework; sourceTree = SDKROOT; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -44,6 +59,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + A70C0CCE1435DFAA00CC3C72 /* ImageIO.framework in Frameworks */, A70C0C8A1435A79C00CC3C72 /* UIKit.framework in Frameworks */, A70C0C8C1435A79C00CC3C72 /* Foundation.framework in Frameworks */, A70C0C8E1435A79C00CC3C72 /* CoreGraphics.framework in Frameworks */, @@ -57,6 +73,7 @@ A70C0C7A1435A79C00CC3C72 = { isa = PBXGroup; children = ( + A70C0CCD1435DFAA00CC3C72 /* ImageIO.framework */, A70C0C911435A79C00CC3C72 /* CocoaneticsBench */, A70C0C881435A79C00CC3C72 /* Frameworks */, A70C0C861435A79C00CC3C72 /* Products */, @@ -93,6 +110,8 @@ A70C0CA61435A79C00CC3C72 /* RootViewController.xib */, A70C0C9D1435A79C00CC3C72 /* CocoaneticsBench.xcdatamodeld */, A70C0C921435A79C00CC3C72 /* Supporting Files */, + A70C0CBC1435AD6600CC3C72 /* ImageBenchmark.h */, + A70C0CBD1435AD6600CC3C72 /* ImageBenchmark.m */, ); path = CocoaneticsBench; sourceTree = ""; @@ -100,6 +119,7 @@ A70C0C921435A79C00CC3C72 /* Supporting Files */ = { isa = PBXGroup; children = ( + A70C0CAE1435AAFE00CC3C72 /* Images */, A70C0C931435A79C00CC3C72 /* CocoaneticsBench-Info.plist */, A70C0C941435A79C00CC3C72 /* InfoPlist.strings */, A70C0C971435A79C00CC3C72 /* main.m */, @@ -108,6 +128,18 @@ name = "Supporting Files"; sourceTree = ""; }; + A70C0CAE1435AAFE00CC3C72 /* Images */ = { + isa = PBXGroup; + children = ( + A70C0CC31435CD2500CC3C72 /* Flower_2048x1536.png */, + A70C0CC41435CD2500CC3C72 /* Flower_1024x768.png */, + A70C0CC51435CD2500CC3C72 /* Flower_128x96.png */, + A70C0CC61435CD2500CC3C72 /* Flower_256x192.png */, + A70C0CC71435CD2500CC3C72 /* Flower_512x384.png */, + ); + name = Images; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -134,6 +166,7 @@ A70C0C7C1435A79C00CC3C72 /* Project object */ = { isa = PBXProject; attributes = { + LastUpgradeCheck = 0420; ORGANIZATIONNAME = Cocoanetics; }; buildConfigurationList = A70C0C7F1435A79C00CC3C72 /* Build configuration list for PBXProject "CocoaneticsBench" */; @@ -161,6 +194,11 @@ A70C0C961435A79C00CC3C72 /* InfoPlist.strings in Resources */, A70C0CA21435A79C00CC3C72 /* MainWindow.xib in Resources */, A70C0CA81435A79C00CC3C72 /* RootViewController.xib in Resources */, + A70C0CC81435CD2500CC3C72 /* Flower_2048x1536.png in Resources */, + A70C0CC91435CD2500CC3C72 /* Flower_1024x768.png in Resources */, + A70C0CCA1435CD2500CC3C72 /* Flower_128x96.png in Resources */, + A70C0CCB1435CD2500CC3C72 /* Flower_256x192.png in Resources */, + A70C0CCC1435CD2500CC3C72 /* Flower_512x384.png in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -175,6 +213,7 @@ A70C0C9C1435A79C00CC3C72 /* CocoaneticsBenchAppDelegate.m in Sources */, A70C0C9F1435A79C00CC3C72 /* CocoaneticsBench.xcdatamodeld in Sources */, A70C0CA51435A79C00CC3C72 /* RootViewController.m in Sources */, + A70C0CBE1435AD6600CC3C72 /* ImageBenchmark.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -257,6 +296,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "CocoaneticsBench/CocoaneticsBench-Prefix.pch"; INFOPLIST_FILE = "CocoaneticsBench/CocoaneticsBench-Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = 4.0; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; @@ -268,6 +308,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "CocoaneticsBench/CocoaneticsBench-Prefix.pch"; INFOPLIST_FILE = "CocoaneticsBench/CocoaneticsBench-Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = 4.0; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; @@ -292,6 +333,7 @@ A70C0CAD1435A79C00CC3C72 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ diff --git a/CocoaneticsBench/CocoaneticsBench.xcdatamodeld/CocoaneticsBench.xcdatamodel/contents b/CocoaneticsBench/CocoaneticsBench.xcdatamodeld/CocoaneticsBench.xcdatamodel/contents index 75b533a..aeb6b2c 100644 --- a/CocoaneticsBench/CocoaneticsBench.xcdatamodeld/CocoaneticsBench.xcdatamodel/contents +++ b/CocoaneticsBench/CocoaneticsBench.xcdatamodeld/CocoaneticsBench.xcdatamodel/contents @@ -1,9 +1,17 @@ - + + + + + + + + + - + \ No newline at end of file diff --git a/CocoaneticsBench/CocoaneticsBenchAppDelegate.m b/CocoaneticsBench/CocoaneticsBenchAppDelegate.m index 2a1151b..3857bc6 100644 --- a/CocoaneticsBench/CocoaneticsBenchAppDelegate.m +++ b/CocoaneticsBench/CocoaneticsBenchAppDelegate.m @@ -9,6 +9,8 @@ #import "CocoaneticsBenchAppDelegate.h" #import "RootViewController.h" +#import "ImageBenchmark.h" + @implementation CocoaneticsBenchAppDelegate @@ -20,6 +22,102 @@ @implementation CocoaneticsBenchAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + //ImageBenchmark *i ; + + ImageBenchmark *i = [[[ImageBenchmark alloc] initWithCrushedPNGNamed:@"Flower_128x96"] autorelease]; + [i prepareAsCrushedPNG]; + [i run]; + NSLog(@"%@", i); + + [i prepareAsUncrushedPNG]; + [i run]; + NSLog(@"%@", i); + + + for (int j=10; j <= 100; j+=10) + { + [i prepareAsJPEGSourceWithQuality:j/100.0]; + [i run]; + NSLog(@"%@", i); + } + + + i = [[[ImageBenchmark alloc] initWithCrushedPNGNamed:@"Flower_256x192"] autorelease]; + [i prepareAsCrushedPNG]; + + [i run]; + NSLog(@"%@", i); + + [i prepareAsUncrushedPNG]; + [i run]; + NSLog(@"%@", i); + + + for (int j=10; j <= 100; j+=10) + { + [i prepareAsJPEGSourceWithQuality:j/100.0]; + [i run]; + NSLog(@"%@", i); + } + + + i = [[[ImageBenchmark alloc] initWithCrushedPNGNamed:@"Flower_512x384"] autorelease]; + [i prepareAsCrushedPNG]; + + [i run]; + NSLog(@"%@", i); + + [i prepareAsUncrushedPNG]; + [i run]; + NSLog(@"%@", i); + + + for (int j=10; j <= 100; j+=10) + { + [i prepareAsJPEGSourceWithQuality:j/100.0]; + [i run]; + NSLog(@"%@", i); + } + + i = [[[ImageBenchmark alloc] initWithCrushedPNGNamed:@"Flower_1024x768"] autorelease]; + [i prepareAsCrushedPNG]; + + [i run]; + NSLog(@"%@", i); + + [i prepareAsUncrushedPNG]; + [i run]; + NSLog(@"%@", i); + + + for (int j=10; j <= 100; j+=10) + { + [i prepareAsJPEGSourceWithQuality:j/100.0]; + [i run]; + NSLog(@"%@", i); + } + + + + i = [[[ImageBenchmark alloc] initWithCrushedPNGNamed:@"Flower_2048x1536"] autorelease]; + [i prepareAsCrushedPNG]; + + [i run]; + NSLog(@"%@", i); + + [i prepareAsUncrushedPNG]; + [i run]; + NSLog(@"%@", i); + + + for (int j=10; j <= 100; j+=10) + { + [i prepareAsJPEGSourceWithQuality:j/100.0]; + [i run]; + NSLog(@"%@", i); + } + + // Override point for customization after application launch. // Add the navigation controller's view to the window and display. self.window.rootViewController = self.navigationController; diff --git a/CocoaneticsBench/Flower_1024x768.png b/CocoaneticsBench/Flower_1024x768.png new file mode 100644 index 0000000..1496ee3 Binary files /dev/null and b/CocoaneticsBench/Flower_1024x768.png differ diff --git a/CocoaneticsBench/Flower_128x96.png b/CocoaneticsBench/Flower_128x96.png new file mode 100644 index 0000000..df09602 Binary files /dev/null and b/CocoaneticsBench/Flower_128x96.png differ diff --git a/CocoaneticsBench/Flower_2048x1536.png b/CocoaneticsBench/Flower_2048x1536.png new file mode 100644 index 0000000..92035ef Binary files /dev/null and b/CocoaneticsBench/Flower_2048x1536.png differ diff --git a/CocoaneticsBench/Flower_256x192.png b/CocoaneticsBench/Flower_256x192.png new file mode 100644 index 0000000..6fdecfd Binary files /dev/null and b/CocoaneticsBench/Flower_256x192.png differ diff --git a/CocoaneticsBench/Flower_512x384.png b/CocoaneticsBench/Flower_512x384.png new file mode 100644 index 0000000..2aedf54 Binary files /dev/null and b/CocoaneticsBench/Flower_512x384.png differ diff --git a/CocoaneticsBench/ImageBenchmark.h b/CocoaneticsBench/ImageBenchmark.h new file mode 100644 index 0000000..285163a --- /dev/null +++ b/CocoaneticsBench/ImageBenchmark.h @@ -0,0 +1,39 @@ +// +// ImageBenchmark.h +// CocoaneticsBench +// +// Created by Oliver Drobnik on 9/30/11. +// Copyright 2011 Cocoanetics. All rights reserved. +// + +#import + +@interface ImageBenchmark : NSObject +{ + NSString *_imageName; + NSString *_path; + NSString *_internalOverrideSourcePath; + + CGFloat _quality; + NSString *_mode; + + + NSTimeInterval _timeForInit; + NSTimeInterval _timeForDecompress; + NSTimeInterval _timeForDraw; + + BOOL _didRun; +} + +- (id)initWithCrushedPNGNamed:(NSString *)imageName; + +- (void)prepareAsCrushedPNG; +- (void)prepareAsUncrushedPNG; +- (void)prepareAsJPEGSourceWithQuality:(CGFloat)quality; + + +- (void)run; + +@end + +CGContextRef newBitmapContextSuitableForSize(CGSize size); diff --git a/CocoaneticsBench/ImageBenchmark.m b/CocoaneticsBench/ImageBenchmark.m new file mode 100644 index 0000000..a3a37b0 --- /dev/null +++ b/CocoaneticsBench/ImageBenchmark.m @@ -0,0 +1,283 @@ +// +// ImageBenchmark.m +// CocoaneticsBench +// +// Created by Oliver Drobnik on 9/30/11. +// Copyright 2011 Cocoanetics. All rights reserved. +// + +#import "ImageBenchmark.h" + +#import + +CGContextRef newBitmapContextSuitableForSize(CGSize size) +{ + int pixelsWide = size.width; + int pixelsHigh = size.height; + CGContextRef context = NULL; + CGColorSpaceRef colorSpace; + // void * bitmapData; + // int bitmapByteCount; + int bitmapBytesPerRow; + + bitmapBytesPerRow = (pixelsWide * 4); //4 + // bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); + + + + /* bitmapData = malloc( bitmapByteCount ); + + memset(bitmapData, 0, bitmapByteCount); // set memory to black, alpha 0 + + if (bitmapData == NULL) + { + return NULL; + } + */ + colorSpace = CGColorSpaceCreateDeviceRGB(); + + + context = CGBitmapContextCreate ( NULL, //bitmapData, // let the device handle the memory + pixelsWide, + pixelsHigh, + 8, // bits per component + bitmapBytesPerRow, + colorSpace, + kCGImageAlphaPremultipliedFirst); + CGColorSpaceRelease( colorSpace ); + + + if (context== NULL) + { + // free (bitmapData); + return NULL; + } + + return context; +} + + +@implementation ImageBenchmark + +- (id)initWithCrushedPNGNamed:(NSString *)imageName +{ + self = [super init]; + if (self) + { + _path = [[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"] copy]; + _imageName = [imageName copy]; + } + + return self; +} + +- (void)dealloc +{ + [_path release]; + [_imageName release]; + [_internalOverrideSourcePath release]; + + [super dealloc]; +} + + +- (NSString *)description +{ + NSString *fileName = [_path lastPathComponent]; + + if (_didRun) + { + NSString *benchmarkModeString = nil; + + if (!_mode) + { + benchmarkModeString = @"Crushed PNG"; + } + else if ([_mode isEqualToString:@"JPG"]) + { + benchmarkModeString = [NSString stringWithFormat:@"JPG %.0f%%", _quality*100.0]; + } + else if ([_mode isEqualToString:@"PNG"]) + { + benchmarkModeString = [NSString stringWithFormat:@"PNG", _quality*100.0]; + } + + return [NSString stringWithFormat:@"%@ (%@) init: %.0f ms decompress: %.0f ms draw: %.0f ms total %.0f ms", fileName, benchmarkModeString, _timeForInit*1000.0, _timeForDecompress*1000.0, _timeForDraw*1000.0, (_timeForInit + _timeForDecompress + _timeForDraw) * 1000.0]; + } + else + { + return [NSString stringWithFormat:@"%@ not run", fileName]; + } +} + +- (void)prepareAsCrushedPNG +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + NSString *fileName = [[[_path lastPathComponent] stringByDeletingPathExtension] stringByAppendingPathExtension:@"crushed.png"]; + + NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; + NSString *cacheImagePath = [cachesPath stringByAppendingPathComponent:fileName]; + + NSFileManager *fileManager = [NSFileManager defaultManager]; + + if (![fileManager fileExistsAtPath:cacheImagePath]) + { + [fileManager copyItemAtPath:_path toPath:cacheImagePath error:NULL]; + } + + [_internalOverrideSourcePath release]; + _internalOverrideSourcePath = [cacheImagePath copy]; + + [_mode release]; + _mode = nil; + _quality = 0; + + [pool drain]; + +} + +- (void)prepareAsUncrushedPNG +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + NSString *fileName = [[[_path lastPathComponent] stringByDeletingPathExtension] stringByAppendingPathExtension:@"uncrushed.png"]; + + NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; + NSString *cacheImagePath = [cachesPath stringByAppendingPathComponent:fileName]; + + + NSFileManager *fileManager = [NSFileManager defaultManager]; + + if (![fileManager fileExistsAtPath:cacheImagePath]) + { + UIImage *image = [UIImage imageWithContentsOfFile:_path]; + UIGraphicsBeginImageContext(image.size); + + [image drawAtPoint:CGPointZero]; + + UIImage *rawImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + + NSData *imageData = UIImagePNGRepresentation(rawImage); + + + [imageData writeToFile:cacheImagePath atomically:NO]; + } + + [_internalOverrideSourcePath release]; + _internalOverrideSourcePath = [cacheImagePath copy]; + + [_mode release]; + _mode = @"PNG"; + _quality = 0; + + [pool drain]; +} + +- (void)prepareAsJPEGSourceWithQuality:(CGFloat)quality +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + NSString *fileName = [[[_path lastPathComponent] stringByDeletingPathExtension] stringByAppendingPathExtension:[NSString stringWithFormat:@"%.0f.jpg", quality*100.0]]; + + NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; + NSString *cacheImagePath = [cachesPath stringByAppendingPathComponent:fileName]; + + NSFileManager *fileManager = [NSFileManager defaultManager]; + + if (![fileManager fileExistsAtPath:cacheImagePath]) + { + UIImage *image = [UIImage imageWithContentsOfFile:_path]; + UIGraphicsBeginImageContext(image.size); + + [image drawAtPoint:CGPointZero]; + + UIImage *rawImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + + NSData *imageData = UIImageJPEGRepresentation(rawImage, quality); + + + [imageData writeToFile:cacheImagePath atomically:NO]; + } + + [_internalOverrideSourcePath release]; + _internalOverrideSourcePath = [cacheImagePath copy]; + + [_mode release]; + _mode = @"JPG"; + _quality = quality; + + [pool drain]; +} + +- (void)decompressImage:(UIImage *)image +{ + UIGraphicsBeginImageContext(CGSizeMake(1, 1)); + + [image drawAtPoint:CGPointZero]; + + UIGraphicsEndImageContext(); +} + +- (void)drawImage:(UIImage *)image +{ + CGContextRef context = newBitmapContextSuitableForSize(image.size); + CGContextSetShouldAntialias(context, NO); + CGContextSetInterpolationQuality(context, kCGInterpolationNone); + CGContextSetBlendMode(context, kCGBlendModeCopy); + + CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage); + + CGContextRelease(context); +} + +- (UIImage *)sourceImage +{ +// if (!_mode) +// { +// return [UIImage imageNamed:_imageName]; +// } + + NSURL *url = [NSURL fileURLWithPath:_internalOverrideSourcePath?_internalOverrideSourcePath:_path]; + NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:(id)kCGImageSourceShouldCache]; + + CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, (CFDictionaryRef)nil); + CGImageRef cgImage = CGImageSourceCreateImageAtIndex(source, 0, (CFDictionaryRef)dict); + + UIImage *retImage = [UIImage imageWithCGImage:cgImage]; + CGImageRelease(cgImage); + CFRelease(source); + + return retImage; +// return [UIImage imageWithContentsOfFile:_internalOverrideSourcePath?_internalOverrideSourcePath:_path]; +} + +- (void)run +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + NSDate *before = [NSDate date]; + UIImage *image = [self sourceImage]; + NSDate *after = [NSDate date]; + _timeForInit = [after timeIntervalSinceDate:before]; + + + before = [NSDate date]; + [self decompressImage:image]; + after = [NSDate date]; + _timeForDecompress = [after timeIntervalSinceDate:before]; + + before = [NSDate date]; + [self drawImage:image]; + after = [NSDate date]; + _timeForDraw = [after timeIntervalSinceDate:before]; + + _didRun = YES; + + [pool drain]; +} + + +@end