// // MPT_FeatureBase.m // AUCoderDemo // // Created by Andrew Salamon on 11/30/06. // Copyright 2006 Machine Perception Laboratory, University of California San Diego. All rights reserved. // #import "MPT_FeatureBase.h" #include #include #include @implementation MPT_FeatureBase - (id)initWithBundle:(NSBundle *)_bundle { if( self = [super initWithBundle:_bundle] ) { NSBundle *myBundle = _bundle; // [NSBundle bundleForClass:[self class]]; NSString *featureDataFileName = [myBundle objectForInfoDictionaryKey:@"FeatureWeights"]; if( featureDataFileName ) { featureDataFile = [[myBundle pathForResource:featureDataFileName ofType:@"fdtxml"] copy]; if( !featureDataFile ) featureDataFile = [[myBundle pathForResource:featureDataFileName ofType:@"fdt"] copy]; if( !featureDataFile ) featureDataFile = [[myBundle pathForResource:featureDataFileName ofType:@"fdtbin"] copy]; } NSString *priorDataFileName = [myBundle objectForInfoDictionaryKey:@"FeaturePriors"]; if( priorDataFileName ) { gpriorFile = [[myBundle pathForResource:priorDataFileName ofType:@"gpriorxml"] copy]; if( !gpriorFile ) gpriorFile = [[myBundle pathForResource:priorDataFileName ofType:@"gprior"] copy]; if( !gpriorFile ) gpriorFile = [[myBundle pathForResource:priorDataFileName ofType:@"gpriorbin"] copy]; } NSBundle *framework = [NSBundle bundleForClass:[MPT_FeatureBase class]]; NSDictionary *context = [NSDictionary dictionaryWithObject:self forKey:@"NSOwner"]; if( ![framework loadNibFile:@"FeaturePrefPane.nib" externalNameTable:context withZone:[self zone]] ) NSLog( @"Unable to load pref pane for Feature plugin." ); // if( ![NSBundle loadNibNamed:@"FeaturePrefPane" owner:self] ) // NSLog( @"Unable to load pref pane for Feature plugin." ); gp = new Gprior; fd = new FeatureData; if( !restoreFeatureDataFromFile( *((FeatureData *)fd), [featureDataFile UTF8String] ) ) { NSLog( @"Unable to load feature data from %@", [self localizedName] ); delete (FeatureData *)fd; fd = NULL; } if( !((Gprior *)gp)->restoreFromFile( [gpriorFile UTF8String] ) ) { delete (Gprior *)gp; gp = NULL; NSLog( @"Unable to load gprior data from %@", [self localizedName] ); } } return self; } - (void)dealloc { [featureDataFile release]; [gpriorFile release]; delete (FeatureData *)fd; delete (Gprior *)gp; [super dealloc]; } //- (void)makeThreadsafe //{ // // we don't want multiple copies of a plugin all trying to release these when they dealloc. // [super makeThreadsafe]; // featureDataFile = nil; // gpriorFile = nil; //} // Features don't need to be copied by each CERT processor, since this plugin does no calculations, hence it's thread safe. - (id)copyWithZone:(NSZone *)zone { // id cp = [super copyWithZone:zone]; return nil; } - (NSString *)step { return MPT_PluginStep_Features; } - (NSString *)localizedName { return NSLocalizedStringFromTableInBundle(@"FeatureName", nil, [self bundle], @""); } - (NSString *)internalName { return NSLocalizedStringFromTableInBundle(@"InternalName", nil, [self bundle], @""); } /** Skip loading labels. * We don't have separate view's for displaying data. */ - (BOOL)loadLabels { return YES; } - (BOOL)handlesLoading { return NO; } - (BOOL)loadFeatureData:(void *)fd andGPrior:(void *)prior { return NO; } - (NSString *)featureDataFile { return featureDataFile; } - (NSString *)gpriorFile { return gpriorFile; } - (void *)processor { return nil; } - (NSColor *)defaultDisplayColor { return [NSColor redColor]; } - (NSColor *)displayColor { NSDictionary *prefs = [[NSUserDefaults standardUserDefaults] dictionaryForKey:[self pluginID]]; NSData *theData = [prefs objectForKey:@"displayColor"]; NSColor *aColor = [self defaultDisplayColor]; if( theData != nil ) aColor =(NSColor *)[NSUnarchiver unarchiveObjectWithData:theData]; return aColor; } - (void)setDisplayColor:(NSColor *)color { NSMutableDictionary *prefs = (NSMutableDictionary *)[[NSUserDefaults standardUserDefaults] dictionaryForKey:[self pluginID]]; if( prefs ) prefs = [[prefs mutableCopy] autorelease]; else prefs = [NSMutableDictionary dictionary]; NSData *theData=[NSArchiver archivedDataWithRootObject:color]; [prefs setObject:theData forKey:@"displayColor"]; [[NSUserDefaults standardUserDefaults] setObject:prefs forKey:[self pluginID]]; } - (NSView *)prefPane { return prefPane; } - (void *)gprior { return gp; } - (void *)featuredata { return fd; } @end