/*  
** Copyright (c) 1995 Netsurfer Inc.  All Rights Reserved.
**
** Author: <bbum@friday.com>
*/

/*  This object is included in the MiscKit by permission from the author
**  and its use is governed by the MiscKit license, found in the file
**  "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
**  for a list of all applicable permissions and restrictions.
*/

#import <appkit/appkit.h>

#import "ImageTestController.h"

#import "ImageView.h"

#import "LapTimer.h"

#import <SurfImage/surfimage.h>

@implementation ImageTestController
void foo(int err) {
	fprintf(stderr, "malloc error %d (see useless malloc man page)\n", err);
#ifdef DEBUG
	abort(); // abort() to gdb.
#endif
}

- appDidInit:sender
{
	malloc_error(foo);

	timer		= [[LapTimer allocFromZone:[self zone]] init];

	surfDecoder	= [SurfImageDecoder sharedInstance];

#ifdef DEBUG
	[surfVerbose setState:1];
#endif

	[self surfVerbose:sender];
	[self surfOptimize:sender];

#ifdef DEBUG	
	[surfDecoder dumpTypes];
#endif

	[self perform:@selector(openImage:) with:nil
		  afterDelay:0.0 cancelPrevious:NO];
	
	return self;
}


- surfOptimize:sender
{
	[surfDecoder setImageDepth:
				 [surfOptimize state] ?
				 NX_TwelveBitRGBDepth : NX_TwentyFourBitRGBDepth];
	return self;
}

- surfVerbose:sender
{
	[surfDecoder setVerboseMode: [surfVerbose state] ? YES : NO];
	return self;
}

- surfOpenImage: (const char *) pathName
{
	double decodeSeconds, totalSeconds;
	char outBuf[300];

	[timer start];

	// decode image
	theImage = [surfDecoder decodeFromFile:pathName];
	
	[timer lap];

	// image image
	[surfView setImage:theImage];
	[window flushWindow];

	[timer stop];

	// check for corruption
	[surfCorrupt setState: [surfDecoder lastImageCorrupt]];
	
	// calculate elapsed times
	decodeSeconds = [timer lapSeconds];
	totalSeconds = [timer totalSeconds];

	// display times
	sprintf(outBuf,
			"Decode Time:\t%2.5f seconds\n",
			decodeSeconds);

	[[surfFields cellAt:FLD_DECODE :0] setStringValue:outBuf];

	sprintf(outBuf,
			"Image Time:\t%2.5f seconds\n",
			totalSeconds - decodeSeconds);

	[[surfFields cellAt:FLD_IMAGE :0] setStringValue:outBuf];
	
	sprintf(outBuf,
			"Total:\t\t%2.5f seconds\n",
			totalSeconds);

	[[surfFields cellAt:FLD_TOTAL :0] setStringValue:outBuf];

	return self;
}

- filterOpenImage: (const char *) pathName
{
	double decodeSeconds, totalSeconds;
	NXSize theSize;
	char outBuf[300];

	[timer start];

	// decode image [the getSize: forces filter service to decode]
	theImage = [[NXImage allocFromZone:[self zone]] initFromFile:pathName];
	[theImage getSize:&theSize];

	[timer lap];

	// image image
	[filterView setImage:theImage];
	[window flushWindow];
	
	[timer stop];

	// calc & display elapsed time
	decodeSeconds = [timer lapSeconds];
	totalSeconds = [timer totalSeconds];

	if(theImage)
		sprintf(outBuf,
				"Decode Time:\t%2.5f seconds",
				decodeSeconds);
	else
		sprintf(outBuf,
				"    <<<<< TIMED OUT >>>>>    ");
	[[filterFields cellAt:FLD_DECODE :0] setStringValue:outBuf];

	if(theImage)
		sprintf(outBuf,
				"Image Time:\t%2.5f seconds",
				totalSeconds - decodeSeconds);
	else
		sprintf(outBuf,
				"    <<<<< TIMED OUT >>>>>    ");
	[[filterFields cellAt:FLD_IMAGE :0] setStringValue:outBuf];
	
	if(theImage)
		sprintf(outBuf,
				"Total:\t\t%2.5f seconds",
				totalSeconds);
	else
		sprintf(outBuf,
				"    <<<<< TIMED OUT >>>>>    ");
	[[filterFields cellAt:FLD_TOTAL :0] setStringValue:outBuf];

	return self;
}

- openImageFile:(const char *) pathName
{
	[self surfOpenImage: pathName];

	if([filterEnabled state])
		[self filterOpenImage: pathName];
	
	return self;
}

- openImage:sender
{
	static id openPanel;
	const char * const *types;

	types = [surfDecoder imageUnfilteredFileTypes];

	if(!openPanel) {
		 openPanel = [OpenPanel new];
		 [openPanel setDirectory: "~/test_images/"];
	}

	if([openPanel runModalForTypes:types]) {
		const char *fileName = [openPanel filename];

		[self openImageFile:fileName];
	}

    return self;
}

- saveSurfTIFF:sender
{
	id savePanel = [SavePanel new];

	[savePanel setRequiredFileType:"tiff"];

	if([savePanel runModal]) {
		const char *fileName = [savePanel filename];
		[surfView saveImageAsTIFF: fileName];
	}

    return self;
}

- saveFilterTIFF:sender
{
	id savePanel = [SavePanel new];

	[savePanel setRequiredFileType:"tiff"];

	if([savePanel runModal]) {
		const char *fileName = [savePanel filename];
		[filterView saveImageAsTIFF: fileName];
	}

    return self;
}

- infoPanel:sender
{
	if(!infoPanel)
		infoPanel = [[objc_getClass("FSCInfoPanel") alloc] init];

	[infoPanel orderFront:sender];

	return self;
}
@end
