/* Copyright (c) 2004-2007, 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 "common.h" #include "QuartzCView.h" #include "Watchdog.h" #include "RendererChain.h" #include "NSImage+Extensions.h" @interface QuartzCView (Private) - (void)ensureChainExists; - (void)flushBufferedImage; @end @implementation QuartzCView - (void)dealloc { [[Watchdog sharedWatchdog] forgetEffectsOfQuartzCView:self]; [self->chain release]; [self->qtzFilePaths release]; [self->auxInputImage release]; [self->bufferedImage release]; [super dealloc]; } /** * We use the focused view for rendering => NO */ - (BOOL)useOpenGLForDrawing { return NO; } - (QCRenderer *)rendererWithQtzFilePath:(NSString *)_qtzPath { return [[QCRenderer alloc] initWithOpenGLContext:self->context pixelFormat:self->pixelFormat file:_qtzPath]; } - (NSArray *)qtzFilePaths { return self->qtzFilePaths; } - (void)setQtzFilePaths:(NSArray *)_paths { [self flushBufferedImage]; [[Watchdog sharedWatchdog] forgetEffectsOfQuartzCView:self]; if (!self->qtzFilePaths) self->qtzFilePaths = [[NSMutableArray alloc] initWithCapacity:3]; else [self->qtzFilePaths removeAllObjects]; [self->qtzFilePaths addObjectsFromArray:_paths]; [self reloadRendererChain]; [[Watchdog sharedWatchdog] watchEffectsOfQuartzCView:self]; } - (void)ensureChainExists { if (!self->chain && self->context) { self->chain = [[RendererChain alloc] init]; [self->chain setShouldFlipImage:[self shouldFlipImage]]; } } - (void)initRendererChain { QCRenderer *renderer; NSString *path; unsigned i, count; if (!self->context) return; [self ensureChainExists]; if ([self->chain rendererCount] == 0) { path = [[NSBundle mainBundle] pathForResource:@"_Pre" ofType:@"qtz"]; renderer = [self rendererWithQtzFilePath:path]; NSAssert(renderer != nil, @"Couldn't get _Pre.qtz!!!"); [self->chain appendRenderer:renderer]; [renderer release]; } for (i = 0, count = [self->qtzFilePaths count]; i < count; i++) { path = [self->qtzFilePaths objectAtIndex:i]; renderer = [self rendererWithQtzFilePath:path]; [self->chain appendRenderer:renderer]; [renderer release]; } } - (void)disposeRendererChain { [self->chain removeCustomRenderers]; } - (void)reloadRendererChain { [self disposeRendererChain]; [self initRendererChain]; } - (void)flushBufferedImage { [self->bufferedImage release]; self->bufferedImage = nil; } - (NSImage *)originalImage { return (NSImage *)[self->chain input]; } - (BOOL)hasAuxiliaryInputImage { return self->auxInputImage ? YES : NO; } - (void)setAuxiliaryInputImage:(NSImage *)_img { [self flushBufferedImage]; [self->auxInputImage release]; if (_img != nil) { NSBitmapImageRep *rep; rep = [NSBitmapImageRep imageRepWithData:[_img TIFFRepresentation]]; self->auxInputImage = [[CIImage alloc] initWithBitmapImageRep:rep]; [self stopVideo:self]; } else { self->auxInputImage = nil; [self startVideo:self]; } } /* Overriding */ - (void)setShouldFlipImage:(BOOL)_yn { [super setShouldFlipImage:_yn]; [self ensureChainExists]; [self->chain setShouldFlipImage:_yn]; [self flushBufferedImage]; } - (void)openGLdrawRect:(NSRect)rect { [self ensureChainExists]; if ([self->chain rendererCount] == 0) [self initRendererChain]; if( ![self useOpenGLForDrawing] ) { if (self->currentFrame || self->auxInputImage || self->bufferedImage) { CIImage *input, *output; CGRect imageRect; NSRect destRect; if (self->auxInputImage) input = self->auxInputImage; else input = [CIImage imageWithCVImageBuffer:self->currentFrame]; imageRect = [input extent]; destRect = NSMakeRect(0.0, 0.0, imageRect.size.width, imageRect.size.height); if (self->bufferedImage) output = self->bufferedImage; else output = [self->chain imageByRenderingImage:input]; [output drawInRect:rect fromRect:destRect operation:NSCompositeCopy fraction:1]; if (self->auxInputImage && !self->bufferedImage) self->bufferedImage = [output retain]; } } } - (NSImage *)imageFromRect:(NSRect)_r { NSImage *si, *di; di = [[NSImage alloc] initWithSize:_r.size]; si = (NSImage *)[self->chain output]; si = [si scaledImageWithSize:[self bounds].size]; [di lockFocus]; [si compositeToPoint:NSZeroPoint fromRect:_r operation:NSCompositeCopy]; [di unlockFocus]; return [di autorelease]; } @end /* QuartzCView */