/* Copyright (c) 2004-2006, Marcus Müller . Copyright (c) 2006, Michael Maier . All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of Mulle kybernetiK nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "CIView.h" #include "common.h" @implementation CIView /** * We use the focused view for rendering => NO */ - (BOOL)useOpenGLForDrawing { return NO; } /** * Cleanup */ - (void)dealloc { [self->effectFilter release]; [self->flipFilter release]; [super dealloc]; } /** * Returns the filter */ - (CIFilter *)effectiveFilter { if (!self->effectFilter) { self->effectFilter = [[CIFilter filterWithName:@"CIMotionBlur"] retain]; [self->effectFilter setDefaults]; } return self->effectFilter; } /** * Returns the flip filter */ - (CIFilter *)flipFilter { if (!self->flipFilter) { NSAffineTransform *transform, *singleTransform; self->flipFilter = [[CIFilter filterWithName:@"CIAffineTransform"] retain]; [self->flipFilter setDefaults]; transform = [NSAffineTransform transform]; singleTransform = [NSAffineTransform transform]; [singleTransform scaleXBy:-1 yBy:1]; [transform appendTransform:singleTransform]; singleTransform = [NSAffineTransform transform]; [singleTransform translateXBy:640 yBy:0]; /** todo: change this */ [transform appendTransform:singleTransform]; [self->flipFilter setValue:transform forKey:@"inputTransform"]; } return self->flipFilter; } - (void)openGLdrawRect:(NSRect)rect { // should we use OpenGL... if ([self useOpenGLForDrawing]) { // set context to current [self->context makeCurrentContext]; // clear color for OpenGL glClearColor( 0, 0, 0, 0 ); // only color buffer glClear( GL_COLOR_BUFFER_BIT ); if (self->currentFrame) { CIFilter *f; CIImage *inputImage;; CGRect imageRect; inputImage = [CIImage imageWithCVImageBuffer:self->currentFrame]; f = [self effectiveFilter]; [f setValue:inputImage forKey:@"inputImage"]; imageRect = [inputImage extent]; glPushMatrix(); // For live video, flip horizontally as if you're looking in a mirror (a-la iChat) glScalef( -1, 1, 1 ); glTranslatef( -imageRect.size.width, 0, 0 ); [[self ciContext] drawImage:[f valueForKey:@"outputImage"] atPoint:CGPointZero fromRect:imageRect]; glPopMatrix(); } glFlush(); } else { if (self->currentFrame) { CIImage *inputImage, *outputImage; CIFilter *ff, *ef; CGRect imageRect; NSRect destRect; inputImage = [CIImage imageWithCVImageBuffer:self->currentFrame]; ff = [self flipFilter]; [ff setValue:inputImage forKey:@"inputImage"]; inputImage = [ff valueForKey:@"outputImage"]; ef = [self effectiveFilter]; [ef setValue:inputImage forKey:@"inputImage"]; imageRect = [inputImage extent]; destRect = NSMakeRect(0.0, 0.0, imageRect.size.width, imageRect.size.height); outputImage = [ff valueForKey:@"outputImage"]; [outputImage drawInRect:rect fromRect:destRect operation:NSCompositeCopy fraction:1]; } } } /** * We need some modifications for the projection */ - (void)update:(NSNotification*)_notif { NSRect bounds, frame; GLfloat minX, minY, maxX, maxY; [super update:_notif]; bounds = [self bounds]; frame = [self frame]; minX = NSMinX(bounds); minY = NSMinY(bounds); maxX = NSMaxX(bounds); maxY = NSMaxY(bounds); // now some OpenGL magic glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0, 0, frame.size.width, frame.size.height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(minX*2, maxX*2, minY*2, maxY*2, -1.0, 1.0); } @end /* CIView */