// // MPT_Patches.m // MPT_Patches // // Created by Andrew Salamon on 6/3/08. // Copyright 2008 Machine Perception Laboratory, University of California San Diego.. All rights reserved. // #import "MPT_Patches.h" #import #import #import #import const int MPT_RImageFileType = -1; @implementation MPT_Patches - (id)initWithBundle:(NSBundle *)_bundle { if( self = [super initWithBundle:_bundle] ) { // Make sure the correct alignment method is used by default unless the user overrides it. NSUserDefaults *_prefs = [NSUserDefaults standardUserDefaults]; NSDictionary *plugPrefs = [NSDictionary dictionaryWithObjectsAndKeys: @"/tmp", @"patchDirectory", [NSNumber numberWithInt:NSBMPFileType], @"patchFormat", [NSNumber numberWithBool:NO], @"enabled", [NSNumber numberWithBool:NO], @"display", nil]; NSDictionary *defPrefs = [NSDictionary dictionaryWithObject:plugPrefs forKey:[self pluginID]]; [_prefs registerDefaults:defPrefs]; [self setFormatExtension:@".bmp"]; if( ![NSBundle loadNibNamed:@"PrefView" owner:self] ) NSLog( @"Unable to load pref pane for MPT_Patches plugin." ); } return self; } - (void)dealloc { [formatExtension release]; [super dealloc]; } // It's not OK to copy this plugin, so return nil. - (id)copyWithZone:(NSZone *)zone { return nil; } - (BOOL)loadLabels { labels = [[NSArray alloc] init]; return YES; } - (NSString *)pluginID { NSString *ident = [super pluginID]; if( [ident length] > 0 ) return ident; return @"edu.ucsd.mplab.plugins.Patches"; } - (NSString *)localizedName { return NSLocalizedStringFromTableInBundle(@"Patch Generator", nil, [self bundle], @""); } - (NSString *)internalName { return @"patches"; } - (NSString *)step { return MPT_PluginStep_Results; } - (NSView *)prefPane { return prefView; } - (NSMutableDictionary *)processResults:(NSDictionary *)results usingController:(id)controller { MPT_RimageAdaptor *adaptor = [results objectForKey:@"CERT.patch"]; if( adaptor ) { RImage *patchRimage = (RImage *)[adaptor rimage]; NSString *path = [results objectForKey:@"CERT.path"]; if( MPT_RImageFileType == [self patchFormat] ) { bool res = false; path = [self filenameFromPath:path ext:@".rimage"]; if( [path length] > 0 ) { std::string filename( [path UTF8String] ); res = saveRImageToFile( *patchRimage, filename ); } return nil; } else { // NSImageCompressFactor is only valid for TIFF and JPEG types, but doesn't seem to cause any trouble with the other file types. NSImage *patchImage = [NSImage imageFromRImage:patchRimage]; NSData *bitmapData = [NSBitmapImageRep representationOfImageRepsInArray:[patchImage representations] usingType:(NSBitmapImageFileType)[self patchFormat] properties:[NSDictionary dictionaryWithObject:[NSDecimalNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor]]; if( bitmapData && ([bitmapData length] > 0) ) { [bitmapData writeToFile:[self filenameFromPath:path ext:formatExtension] atomically:YES]; } } } return nil; } - (NSString *)patchDirectory { NSDictionary *myPrefs = [[NSUserDefaults standardUserDefaults] objectForKey:[self pluginID]]; NSString *dir = [myPrefs objectForKey:@"patchDirectory"]; return (dir?dir:@"/tmp"); } - (void)setPatchDirectory:(NSString *)value { NSMutableDictionary *myPrefs = [[[NSUserDefaults standardUserDefaults] objectForKey:[self pluginID]] mutableCopy]; if( !myPrefs ) myPrefs = [[NSMutableDictionary alloc] init]; [myPrefs setObject:(value?value:@"") forKey:@"patchDirectory"]; [[NSUserDefaults standardUserDefaults] setObject:myPrefs forKey:[self pluginID]]; [myPrefs release]; } static NSString *ExtensionForBitmapImageFileType( NSBitmapImageFileType bitmapImageFileType ) { NSString *result = @""; switch( bitmapImageFileType ) { case NSTIFFFileType: result = @".tiff"; break; case NSBMPFileType: result = @".bmp"; break; case NSGIFFileType: result = @".gif"; break; case NSJPEGFileType: result = @".jpg"; break; case NSPNGFileType: result = @".png"; break; case NSJPEG2000FileType: result = @".jp2"; break; default: result = @".bmp"; break; } return result; } - (int)patchFormat { NSDictionary *myPrefs = [[NSUserDefaults standardUserDefaults] objectForKey:[self pluginID]]; NSNumber *fmt = [myPrefs objectForKey:@"patchFormat"]; return fmt?[fmt intValue]:NSBMPFileType; } - (void)setPatchFormat:(int)value { NSMutableDictionary *myPrefs = [[[NSUserDefaults standardUserDefaults] objectForKey:[self pluginID]] mutableCopy]; if( !myPrefs ) myPrefs = [[NSMutableDictionary alloc] init]; [myPrefs setObject:[NSNumber numberWithInt:value] forKey:@"patchFormat"]; [[NSUserDefaults standardUserDefaults] setObject:myPrefs forKey:[self pluginID]]; [myPrefs release]; [self setFormatExtension:ExtensionForBitmapImageFileType( (NSBitmapImageFileType)value)]; } - (NSString *)formatExtension { return formatExtension; } - (void)setFormatExtension:(NSString *)value { if( formatExtension != value ) { [formatExtension release]; formatExtension = [value retain]; } } - (NSString *)filenameFromPath:(NSString *)path ext:(NSString *)ext { static NSString *face = @"_face"; NSString *filename = [path lastPathComponent]; NSString *patchPath = [NSString stringWithFormat:@"%@%@%@", [filename stringByDeletingPathExtension], face, ext]; return [[self patchDirectory] stringByAppendingPathComponent:patchPath]; } - (IBAction)choosePatchDirectory:(id)sender { NSOpenPanel *panel = [NSOpenPanel openPanel]; [panel setPrompt:@"Choose Folder"]; [panel setTitle:@"Choose a folder for saving face patches."]; [panel setCanChooseDirectories:YES]; [panel setCanCreateDirectories:YES]; [panel setCanChooseFiles:NO]; [panel setAllowsMultipleSelection:NO]; [panel beginSheetForDirectory:nil file:nil types:nil modalForWindow:[prefView window] modalDelegate:self didEndSelector:@selector(choosePatchDirectoryPanelDidEnd:returnCode:contextInfo:) contextInfo:nil]; } - (void)choosePatchDirectoryPanelDidEnd:(NSOpenPanel *)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo { if( returnCode == NSOKButton ) { NSArray *filenames = [panel filenames]; if( [filenames count] > 0 ) { NSString *path = [filenames objectAtIndex:0]; if( [path length] > 0 ) { [self setPatchDirectory:path]; } } } } @end