Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fast Image Manipulation

Fast Image Manipulation

From Cocoaheads SF, May 10th, 2012

patr1ck

May 11, 2012
Tweet

More Decks by patr1ck

Other Decks in Programming

Transcript

  1. ▪ CoreImage in a Nutshell ▪ Problems with CoreImage ▪

    OpenGL in a Nutshell ▪ Using OpenGL with CoreImage Effectively OVERVIEW
  2. ▪ Apply effects ( lters) to images ▪ Deferred rendering

    ▪ GPU powered – with caveats ▪ High-level API ▪ Mac OS X version different from iOS version COREIMAGE
  3. // Create image CIImage *testImage = [CIImage imageWithCGImage:someCGImageRef]; // Set

    the new filter value CIFilter *contrastFilter = [CIFilter filterWithName:@"CIColorControls"]; [contrastFilter setDefaults]; [contrastFilter setValue:[NSNumber numberWithDouble:contrastSlider.value] forKey:@"inputContrast"]; // Apply the filter [contrastFilter setValue:testImage forKey:@"inputImage"]; // Get the results CIImage *outputImage = [contrastFilter outputImage]; COREIMAGE
  4. // Get the context CGContextRef context = UIGraphicsGetCurrentContext(); // Render

    the image CGImageRef imageRef = [_coreImageContext createCGImage:coreImage fromRect:coreImage.extent]; // Draw it on screen CGContextSaveGState(context); CGContextTranslateCTM(context, 0.0f, coreImage.extent.size.height); CGContextScaleCTM(context, 1.0f, -1.0f); CGContextDrawImage(context, rect, imageRef); CGContextRestoreGState(context); CGImageRelease(imageRef); COREIMAGE
  5. ▪ Ludicrous speed ▪ Fully GPU powered ▪ Very low

    level API ▪ Steep learning curve ▪ But, ludicrous speed OPENGLES
  6. ▪ CAEAGLLayer and EAGLContext ▪ Frame buffer ▪ Render buffer

    ▪ Depth buffer ▪ Stencil buffer, Accumulation buffer (not used here) OPENGLES
  7. glGenFramebuffers(1, &frameBuffer); glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); glGenRenderbuffers(1, &renderBuffer); glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer); [_eaglContext renderbufferStorage:GL_RENDERBUFFER

    fromDrawable:(CAEAGLLayer *)self.layer]; glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderBuffer); glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth); glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight); OPENGLES
  8. // drawView glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); [_coreImageContext drawImage:coreImage inRect:self.bounds

    fromRect:coreImage.extent]; glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer); [_eaglContext presentRenderbuffer:GL_RENDERBUFFER];
  9. ▪ Cheat like crazy ▪ Some CoreImage lters are slow

    (CIToneCurve) ▪ CADisplayLink ▪ Do not taunt TLDFastCoreImageView NOTES