| 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 | |
|---|
| 24 | static NSData * imageAtTime( QTMovie *movie, QTTime frameTime ) |
|---|
| 25 | { |
|---|
| 26 | NSImage *image = [movie frameImageAtTime:frameTime]; |
|---|
| 27 | |
|---|
| 28 | if ( image == nil ) |
|---|
| 29 | return nil; |
|---|
| 30 | |
|---|
| 31 | NSApplicationLoad(); // TIFFRepresentation won't work without this |
|---|
| 32 | NSBitmapImageRep * imageBitmap = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]]; |
|---|
| 33 | |
|---|
| 34 | return [imageBitmap representationUsingType:NSJPEGFileType properties:nil]; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | @implementation NSImage (QTImages) |
|---|
| 38 | |
|---|
| 39 | + (NSData *) imageFromMovie: (NSString *)path |
|---|
| 40 | { |
|---|
| 41 | NSError * error = nil; |
|---|
| 42 | QTMovie * movie = [QTMovie movieWithFile:path error:&error]; |
|---|
| 43 | QTTime imageTime = [movie duration]; |
|---|
| 44 | |
|---|
| 45 | imageTime.timeValue /= 10; |
|---|
| 46 | return imageAtTime( movie, imageTime ); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | + (NSData *) imageFromMovie: (NSString *)path atTime: (unsigned int)instant |
|---|
| 50 | { |
|---|
| 51 | NSError * error = nil; |
|---|
| 52 | QTMovie * movie = [QTMovie movieWithFile:path error:&error]; |
|---|
| 53 | QTTime imageTime = { instant, 1, 0 }; |
|---|
| 54 | |
|---|
| 55 | return imageAtTime( movie, imageTime ); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | + (NSArray *) imagesFromMovie: (NSString *)path forArraySize: (unsigned int) size |
|---|
| 59 | { |
|---|
| 60 | SapphireLog(SAPPHIRE_LOG_FILE, SAPPHIRE_LOG_LEVEL_DEBUG, @"Getting array of size %d from %@", size, path ); |
|---|
| 61 | NSError * error = nil; |
|---|
| 62 | QTMovie * movie = [QTMovie movieWithFile:path error:&error]; |
|---|
| 63 | SapphireLog(SAPPHIRE_LOG_FILE, SAPPHIRE_LOG_LEVEL_DEBUG, @"movie opened"); |
|---|
| 64 | QTTime duration = [movie duration]; |
|---|
| 65 | |
|---|
| 66 | unsigned int i; |
|---|
| 67 | NSMutableArray * images = [NSMutableArray arrayWithCapacity:size]; |
|---|
| 68 | |
|---|
| 69 | srand(time(NULL)); |
|---|
| 70 | |
|---|
| 71 | // Split the duration into 'size' chunks and take a random |
|---|
| 72 | // image from each chunk |
|---|
| 73 | for ( i = 0; i < size; ++i ) |
|---|
| 74 | { |
|---|
| 75 | QTTime imageTime = duration; |
|---|
| 76 | imageTime.timeValue = (rand() % (duration.timeValue/size)) + (i*duration.timeValue/size); |
|---|
| 77 | |
|---|
| 78 | SapphireLog(SAPPHIRE_LOG_FILE, SAPPHIRE_LOG_LEVEL_DEBUG, @"getting image"); |
|---|
| 79 | NSImage * image = [movie frameImageAtTime:imageTime]; |
|---|
| 80 | if ( image != nil ) |
|---|
| 81 | [images addObject:image]; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | SapphireLog(SAPPHIRE_LOG_FILE, SAPPHIRE_LOG_LEVEL_DEBUG, @"Returning array of size %d from %@", [images count], path ); |
|---|
| 85 | return images; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | - (CGImageRef) asImageRef |
|---|
| 89 | { |
|---|
| 90 | NSApplicationLoad(); // TIFFRepresentation won't work without this |
|---|
| 91 | CGImageSourceRef sourceRef = CGImageSourceCreateWithData( (CFDataRef)[self TIFFRepresentation], NULL ); |
|---|
| 92 | |
|---|
| 93 | return CGImageSourceCreateImageAtIndex( sourceRef, 0, NULL ); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | - (BOOL) writeToFile:(NSString *)path atomically:(BOOL)atomic |
|---|
| 97 | { |
|---|
| 98 | NSApplicationLoad(); // TIFFRepresentation won't work without this |
|---|
| 99 | NSBitmapImageRep * imageBitmap = [NSBitmapImageRep imageRepWithData:[self TIFFRepresentation]]; |
|---|
| 100 | NSData * imageData = [imageBitmap representationUsingType:NSJPEGFileType properties:nil]; |
|---|
| 101 | |
|---|
| 102 | return [imageData writeToFile:path atomically:atomic]; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | @end |
|---|