/*	MiscImageDecoderController.h

	Copyright 1996 Netsurfer Inc.

	This notice may not be removed from this source code.
	The use and distribution of this software is governed by the
	terms of the MiscKit license agreement.  Refer to the license
	document included with the MiscKit distribution for the terms.

	Author: <bbum@friday.com>
	Converted to OpenStep, September 1996, Uwe Hoffmann.
*/

#import <MiscFoundation/MiscUtilities.h>

#import "MiscImageDecoder.h"

#ifdef VERBOSE
#undef VERBOSE
#undef METHOD
#undef METHODnl
#endif

#define VERBOSE 		if(_SDFlags.verboseMode)
#define METHOD  		NSLog(@"[%@ %s%@] ", \
                                [[self class] description], \
                 		        (self == (id)[self class]) ? "+" : "-", \
				                NSStringFromSelector(_cmd))
#define METHODnl  		NSLog(@"[%@ %s%@]\n", \
                                [[self class] description], \
                 		        (self == (id)[self class]) ? "+" : "-", \
				                NSStringFromSelector(_cmd))

@implementation MiscImageDecoder
/*"
  This class is an abstract superclass for all image decoders.  An image
  decoder need only implement those methods marked as the responsibility
  of the subclass.
"*/

+ sharedInstance
/*"
  Returns an instance that can be shared throughout the application.  %{It
  is the responsibility of the subclass to implement this method!}
"*/
{
	MiscRequiresConcreteImplementation(self, _cmd, [MiscImageDecoder class]);
	return self;
}

+ (NSArray *)imageUnfilteredFileTypes
/*"
  Returns an array of types (file extensions) that an
  instance of the decoder class can filter to TIFF.  %{It is the
  responsibility of the subclass to implement this method!}
"*/
{
	MiscRequiresConcreteImplementation(self, _cmd, [MiscImageDecoder class]);
	return nil;
}

+ (BOOL)canInitWithData:(NSData *)data
/*"
  Returns YES if an instance of the decoder class can decode the image
  contained in data. An implementation of this method should read the
  minnimum number of bytes necessary to determine if it can decode the
  data. %{It is the responsibility of the subclass to implement this method!}
"*/
{
	MiscRequiresConcreteImplementation(self, _cmd, [MiscImageDecoder class]);
	return NO;
}

- init
/*"Designated initializer. Disables verbose mode."*/
{
	_SDFlags.verboseMode = NO;
	_SDFlags.lastCorrupt = NO;
	return self;
}

- (NSBitmapImageRep *)decodeFromFile:(NSString *)filePath
/*"
  Decode image contained in filePath.  Returns an instance of NSBitmapImageRep
  containing the image data. Returns nil if unable to decode filePath.
"*/
{
	NSData *data;

	NSParameterAssert(filePath);
	data = [NSData dataWithContentsOfMappedFile:filePath];
	if(!data){
		_SDFlags.lastCorrupt = YES;
		return nil;
	}
	VERBOSE {
		METHOD;
		NSLog(filePath);
	}
	return [self decodeFromData:data];

}

- (NSBitmapImageRep *)decodeFromData:(NSData *)data
/*"Decode image contained in data. Same behaviour as #{-decodeFromFile:}"*/
{
	MiscRequiresConcreteImplementation(self, _cmd, [MiscImageDecoder class]);
	return nil;
}

- (void)setImageDepth:(NSWindowDepth)aDepth
/*"
  Sets the decoding depth (%imageDepth) to aDepth.  The decoder can use
  %imageDepth to optimize the decoding process to the particular display
  device.
"*/
{
	imageDepth = aDepth;
}

- (NSWindowDepth)imageDepth
/*"Returns the target decoding depth."*/
{
	return imageDepth;
}

- (void)setLastImageCorrupt:(BOOL)aFlag
/*"Sets the %{image corrupted} flag of the receiver."*/
{
	_SDFlags.lastCorrupt = aFlag;
}

- (BOOL)lastImageCorrupt
/*"Returns YES if the last image decoded was corrupt."*/
{
	return _SDFlags.lastCorrupt;
}

- (BOOL)verboseMode
/*"
  Returns YES if verbose mode is enabled.  If verbose mode is enabled, the
  image decoder should dump notes about the decoding process.  
  Useful for debugging purposes.
"*/
{
	return _SDFlags.verboseMode;
}

- (void)setVerboseMode:(BOOL)aFlag
/*"Enables / disables verbose mode."*/
{
	_SDFlags.verboseMode = aFlag;
}

- (void)setErrorDelegate:(id <MiscImageDecoderErrorDelegate>)aDelegate
/*"
  Sets the error delegate to aDelegate.  aDelegate will receive
  notification whenever the decoder generates an error message.
"*/
{
	errorDelegate = aDelegate;
}

- (id <MiscImageDecoderErrorDelegate>)errorDelegate
/*"Returns the error delegate of the receiver."*/
{
	return errorDelegate;
}

- (void)spewMessage:(NSString *)errMsg withSeverity:(MiscImageDecoderErrorSeverity)aSeverity
/*"
  This method passes the error message errMsg to the
  errorDelegate via the #{-decoder:spewMessage:withSeverity:} method in
  the #MiscImageDecoderErrorSeverity protocol.
"*/
{
	if(errorDelegate)
		[errorDelegate decoder:self spewMessage:errMsg withSeverity:aSeverity];
}

@end
