Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

change to drawing via EAGLContext

  • Loading branch information...
commit 5f0bbb50039198786b9b094a9d0575e6131260f2 1 parent 5545fb4
@Cocoanetics authored
Showing with 74 additions and 12 deletions.
  1. +74 −12 CocoaneticsBench/Image Decompression/Source/ImageBenchmark.m
View
86 CocoaneticsBench/Image Decompression/Source/ImageBenchmark.m
@@ -9,10 +9,11 @@
#import "ImageBenchmark.h"
#import <ImageIO/ImageIO.h>
+#import <OpenGLES/ES2/gl.h>
// enable the following line to benchmark HW decompression
-//#define USE_CIIMAGE
+#define USE_CIIMAGE
@implementation ImageBenchmark
@@ -179,7 +180,8 @@ - (void)decompressImage:(UIImage *)image
UIGraphicsEndImageContext();
}
-- (void)drawImage:(UIImage *)image
+
+- (void)drawUIImage:(UIImage *)image
{
UIGraphicsBeginImageContext(image.size);
@@ -189,21 +191,67 @@ - (void)drawImage:(UIImage *)image
CGContextSetBlendMode(context, kCGBlendModeCopy);
[image drawAtPoint:CGPointZero];
-// CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
UIGraphicsEndImageContext();
}
-- (UIImage *)sourceImage
+- (void)drawCIImage:(CIImage *)image
{
- NSURL *url = [NSURL fileURLWithPath:_internalOverrideSourcePath?_internalOverrideSourcePath:_path];
+ CGRect rect = [image extent];
+
+ CIContext *coreImageContext;
+ // CIContext requires OpenGL ES 2
+ EAGLContext *glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
+ [EAGLContext setCurrentContext:glContext];
-#ifdef USE_CIIMAGE
- CIImage *ciImage = [CIImage imageWithContentsOfURL:url];
+ // http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/WorkingwithEAGLContexts/WorkingwithEAGLContexts.html
- UIImage *retImage = [UIImage imageWithCIImage:ciImage];
-#else
+ GLuint framebuffer;
+ glGenFramebuffers(1, &framebuffer);
+ glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
+
+ GLuint colorRenderbuffer;
+ glGenRenderbuffers(1, &colorRenderbuffer);
+ glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
+ glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8_OES, rect.size.width, rect.size.height);
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
+
+ GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER) ;
+ if(status != GL_FRAMEBUFFER_COMPLETE) {
+ NSLog(@"failed to make complete framebuffer object %x", status);
+ }
+
+ coreImageContext = [CIContext contextWithEAGLContext:glContext];
+ [coreImageContext drawImage:image atPoint:CGPointZero fromRect:rect];
+
+ // now we create a UIImage from the render buffer and simulate drawing into a CGContext
+
+ NSInteger myDataLength = rect.size.width * rect.size.height * 4;
+ GLubyte *myGLData = (GLubyte *) malloc(myDataLength);
+ // glReadBuffer(GL_FRONT);
+ glReadPixels(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, GL_RGB, GL_UNSIGNED_BYTE, &myGLData);
+
+ NSData *myImageData = [NSData dataWithBytesNoCopy:myGLData length:myDataLength freeWhenDone:YES];
+
+ UIImage *myImage = [[UIImage alloc] initWithData:myImageData];
+
+ UIGraphicsBeginImageContext(rect.size);
+
+ CGContextRef context = UIGraphicsGetCurrentContext();
+ CGContextSetShouldAntialias(context, NO);
+ CGContextSetInterpolationQuality(context, kCGInterpolationNone);
+ CGContextSetBlendMode(context, kCGBlendModeCopy);
+
+ [myImage drawAtPoint:CGPointZero];
+
+ UIGraphicsEndImageContext();
+}
+
+- (UIImage *)sourceUIImage
+{
+ NSURL *url = [NSURL fileURLWithPath:_internalOverrideSourcePath?_internalOverrideSourcePath:_path];
+
NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:(id)kCGImageSourceShouldCache];
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, (CFDictionaryRef)nil);
@@ -212,17 +260,27 @@ - (UIImage *)sourceImage
UIImage *retImage = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
CFRelease(source);
-#endif
return retImage;
}
+- (CIImage *)sourceCIImage
+{
+ NSURL *url = [NSURL fileURLWithPath:_internalOverrideSourcePath?_internalOverrideSourcePath:_path];
+
+ return [CIImage imageWithContentsOfURL:url];
+}
+
- (void)run
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CFAbsoluteTime before = CFAbsoluteTimeGetCurrent();
- UIImage *image = [self sourceImage];
+#ifdef USE_CIIMAGE
+ CIImage *image = [self sourceCIImage];
+#else
+ UIImage *image = [self sourceUIImage];
+#endif
CFAbsoluteTime after = CFAbsoluteTimeGetCurrent();
_timeForInit = after - before;
@@ -235,7 +293,11 @@ - (void)run
#endif
before = CFAbsoluteTimeGetCurrent();
- [self drawImage:image];
+#ifdef USE_CIIMAGE
+ [self drawCIImage:image];
+#else
+ [self drawUIImage:image];
+#endif
after = CFAbsoluteTimeGetCurrent();
_timeForDraw = after - before;
Please sign in to comment.
Something went wrong with that request. Please try again.