| 1 | /* |
|---|
| 2 | * NSImage-Extensions.m |
|---|
| 3 | * Sapphire |
|---|
| 4 | * |
|---|
| 5 | * Created by Warren Gavin on Oct. 7, 2007. |
|---|
| 6 | * Copyright 2007 Sapphire Development Team and/or www.nanopi.net |
|---|
| 7 | * All rights reserved. |
|---|
| 8 | * |
|---|
| 9 | * This program is free software; you can redistribute it and/or modify it under the terms of the GNU |
|---|
| 10 | * General Public License as published by the Free Software Foundation; either version 3 of the License, |
|---|
| 11 | * or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even |
|---|
| 14 | * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|---|
| 15 | * Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License along with this program; if not, |
|---|
| 18 | * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #import "NSImage-Extensions.h" |
|---|
| 22 | #import <QTKit/QTKit.h> |
|---|
| 23 | #import <QTKit/QTMovie.h> |
|---|
| 24 | |
|---|
| 25 | static const NSData * imageAsBitmapData( const NSImage * const image ) |
|---|
| 26 | { |
|---|
| 27 | const NSBitmapImageRep * imageBitmap = nil; |
|---|
| 28 | const NSEnumerator * const en = [[image representations] objectEnumerator]; |
|---|
| 29 | const NSImageRep * rep = nil; |
|---|
| 30 | |
|---|
| 31 | while( nil != (rep = [en nextObject]) ) |
|---|
| 32 | { |
|---|
| 33 | if ( [rep isKindOfClass:[NSBitmapImageRep class]] ) |
|---|
| 34 | { |
|---|
| 35 | imageBitmap = (NSBitmapImageRep *) rep; |
|---|
| 36 | break; |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | return [imageBitmap representationUsingType:NSJPEGFileType properties:nil]; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | static const NSData * imageAtTime( const QTMovie * const movie, const QTTime frameTime ) |
|---|
| 44 | { |
|---|
| 45 | const NSImage * const image = [movie frameImageAtTime:frameTime]; |
|---|
| 46 | |
|---|
| 47 | if ( image == nil ) |
|---|
| 48 | return nil; |
|---|
| 49 | |
|---|
| 50 | return imageAsBitmapData( image ); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | @implementation NSImage (QTImages) |
|---|
| 54 | |
|---|
| 55 | + (const NSData *) imageFromMovie: (NSString *)path |
|---|
| 56 | { |
|---|
| 57 | const NSError * const error = nil; |
|---|
| 58 | const QTMovie * const movie = [QTMovie movieWithFile:path error:&error]; |
|---|
| 59 | QTTime imageTime = [movie duration]; |
|---|
| 60 | |
|---|
| 61 | imageTime.timeValue /= 10; |
|---|
| 62 | return imageAtTime( movie, imageTime ); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | + (const NSData *) imageFromMovie: (NSString *)path atTime: (const unsigned int)instant |
|---|
| 66 | { |
|---|
| 67 | const NSError * const error = nil; |
|---|
| 68 | const QTMovie * const movie = [QTMovie movieWithFile:path error:&error]; |
|---|
| 69 | const QTTime imageTime = { instant, 1, 0 }; |
|---|
| 70 | |
|---|
| 71 | return imageAtTime( movie, imageTime ); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | + (const NSArray *) imagesFromMovie: (NSString *)path forArraySize: (const unsigned int) size |
|---|
| 75 | { |
|---|
| 76 | const NSError * const error = nil; |
|---|
| 77 | const QTMovie * const movie = [QTMovie movieWithFile:path error:&error]; |
|---|
| 78 | const QTTime duration = [movie duration]; |
|---|
| 79 | |
|---|
| 80 | unsigned int i; |
|---|
| 81 | const NSMutableArray * const images = [NSMutableArray arrayWithCapacity:size]; |
|---|
| 82 | |
|---|
| 83 | srand(time(NULL)); |
|---|
| 84 | |
|---|
| 85 | // Split the duration into 'size' chunks and take a random |
|---|
| 86 | // image from each chunk |
|---|
| 87 | for ( i = 0; i < size; ++i ) |
|---|
| 88 | { |
|---|
| 89 | QTTime imageTime = duration; |
|---|
| 90 | imageTime.timeValue = (rand() % (duration.timeValue/size)) + (i*duration.timeValue/size); |
|---|
| 91 | |
|---|
| 92 | const NSImage * const image = [movie frameImageAtTime:imageTime]; |
|---|
| 93 | if ( image != nil ) |
|---|
| 94 | [images addObject:image]; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | return images; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | - (CGImageRef) asImageRef |
|---|
| 101 | { |
|---|
| 102 | CGImageSourceRef sourceRef = CGImageSourceCreateWithData( (CFDataRef)imageAsBitmapData( self ), NULL ); |
|---|
| 103 | return CGImageSourceCreateImageAtIndex( sourceRef, 0, NULL ); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | - (BOOL) writeToFile:(NSString *)path atomically:(BOOL)flag |
|---|
| 107 | { |
|---|
| 108 | const NSData * imageData = imageAsBitmapData( self ); |
|---|
| 109 | return [imageData writeToFile:path atomically:flag]; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | @end |
|---|