/*
 * NSImage-Extensions.m
 * Sapphire
 *
 * Created by Warren Gavin on Oct. 7, 2007.
 * Copyright 2007 Sapphire Development Team and/or www.nanopi.net
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import "NSImage-Extensions.h"
#import <QTKit/QTKit.h>
#import <QTKit/QTMovie.h>

static const NSData * imageAsBitmapData( const NSImage * const image )
{
	const NSBitmapImageRep * imageBitmap = nil;
	const NSEnumerator     * const en = [[image representations] objectEnumerator];
	const NSImageRep       * rep = nil;
	
	while( nil != (rep = [en nextObject]) )
	{
		if ( [rep isKindOfClass:[NSBitmapImageRep class]] )
		{
			imageBitmap = (NSBitmapImageRep *) rep;
			break;
		}
	}
	
	return [imageBitmap representationUsingType:NSJPEGFileType properties:nil];
}

static const NSData * imageAtTime( const QTMovie * const movie, const QTTime frameTime )
{
	const NSImage * const image = [movie frameImageAtTime:frameTime];

	if ( image == nil )
		return nil;

	return imageAsBitmapData( image );
}

@implementation NSImage (QTImages)

+ (const NSData *) imageFromMovie: (NSString *)path
{
	const NSError * const error = nil;
	const QTMovie * const movie = [QTMovie movieWithFile:path error:&error];	
	QTTime                imageTime = [movie duration];

	imageTime.timeValue /= 10;
	return imageAtTime( movie, imageTime );
}

+ (const NSData *) imageFromMovie: (NSString *)path atTime: (const unsigned int)instant
{
	const NSError * const error = nil;
	const QTMovie * const movie = [QTMovie movieWithFile:path error:&error];
	const QTTime          imageTime = { instant, 1, 0 };

	return imageAtTime( movie, imageTime );
}

+ (const NSArray *) imagesFromMovie: (NSString *)path forArraySize: (const unsigned int) size
{
	const NSError * const error    = nil;
	const QTMovie * const movie    = [QTMovie movieWithFile:path error:&error];
	const QTTime          duration = [movie duration];

	unsigned int i;
	const NSMutableArray * const images = [NSMutableArray arrayWithCapacity:size];

	srand(time(NULL));

	// Split the duration into 'size' chunks and take a random
	// image from each chunk
	for ( i = 0; i < size; ++i )
	{
		QTTime imageTime = duration;
		imageTime.timeValue = (rand() % (duration.timeValue/size)) + (i*duration.timeValue/size);

		const NSImage * const image = [movie frameImageAtTime:imageTime];
		if ( image != nil )
			[images addObject:image];
	}

	return images;
}

- (CGImageRef) asImageRef
{
	CGImageSourceRef sourceRef = CGImageSourceCreateWithData( (CFDataRef)imageAsBitmapData( self ), NULL );	
	return CGImageSourceCreateImageAtIndex( sourceRef, 0, NULL );
}

- (BOOL) writeToFile:(NSString *)path atomically:(BOOL)flag
{
	const NSData * imageData = imageAsBitmapData( self );
	return [imageData writeToFile:path atomically:flag];
}

@end

