| 1 | #import "ImageController.h" |
| 2 | |
| 3 | |
| 4 | @implementation ImageController |
| 5 | - (IBAction)loadSourceImage:(id)sender |
| 6 | { |
| 7 | NSOpenPanel* openPanel = [NSOpenPanel openPanel]; |
| 8 | [openPanel setCanChooseFiles:YES]; |
| 9 | [openPanel setCanCreateDirectories:NO]; |
| 10 | [openPanel setCanCreateDirectories:NO]; |
| 11 | [openPanel setCanSelectHiddenExtension:NO]; |
| 12 | [openPanel setAllowsMultipleSelection:NO]; |
| 13 | [openPanel setAllowsOtherFileTypes:NO]; |
| 14 | NSArray* types = [[NSArray alloc] initWithObjects:@"jpg", @"tiff", @"png", nil]; |
| 15 | if ( [openPanel runModalForTypes: types] == NSOKButton ) |
| 16 | { |
| 17 | [sourceImage setImage:[[NSImage alloc] initWithContentsOfFile:[openPanel filename] ] ]; |
| 18 | [openSourceImageControl setEnabled:NO]; |
| 19 | [openArchiveMatrixControl setEnabled:NO]; |
| 20 | [progress setUsesThreadedAnimation:YES]; |
| 21 | [self enableControls]; |
| 22 | withLogBool = [logWith state]; |
| 23 | } |
| 24 | [types release]; |
| 25 | neuroNet = NULL; |
| 26 | resultImageInstance = NULL; |
| 27 | archThreadPool = NULL; |
| 28 | thread = NULL; |
| 29 | } |
| 30 | - (void) enableControls |
| 31 | { |
| 32 | [teachZeroLayer setEnabled:YES]; |
| 33 | [adaptiveSteps setEnabled:YES]; |
| 34 | [n setEnabled:YES]; // n -> width |
| 35 | [m setEnabled:YES]; // m -> height |
| 36 | [p setEnabled:YES]; |
| 37 | [a setEnabled:YES]; |
| 38 | [D setEnabled:YES]; |
| 39 | [maxLoopsField setEnabled:YES]; |
| 40 | [closeControl setEnabled:YES]; |
| 41 | [archiveButton setEnabled:YES]; |
| 42 | [logWith setEnabled:YES]; |
| 43 | [normilizingWith setEnabled:YES]; |
| 44 | [logEachValue setEnabled:YES]; |
| 45 | } |
| 46 | - (void) disableControls |
| 47 | { |
| 48 | [teachZeroLayer setEnabled:NO]; |
| 49 | [adaptiveSteps setEnabled:NO]; |
| 50 | [n setEnabled:NO]; // n -> width |
| 51 | [m setEnabled:NO]; // m -> height |
| 52 | [p setEnabled:NO]; |
| 53 | [a setEnabled:NO]; |
| 54 | [D setEnabled:NO]; |
| 55 | [maxLoopsField setEnabled:NO]; |
| 56 | [closeControl setEnabled:NO]; |
| 57 | [archiveButton setEnabled:NO]; |
| 58 | [logWith setEnabled:NO]; |
| 59 | [normilizingWith setEnabled:NO]; |
| 60 | } |
| 61 | - (IBAction)loadArchiveMatrix:(id)sender |
| 62 | { |
| 63 | NSOpenPanel* openPanel = [NSOpenPanel openPanel]; |
| 64 | [openPanel setCanChooseFiles:YES]; |
| 65 | [openPanel setCanCreateDirectories:NO]; |
| 66 | [openPanel setCanCreateDirectories:NO]; |
| 67 | [openPanel setCanSelectHiddenExtension:NO]; |
| 68 | [openPanel setAllowsMultipleSelection:NO]; |
| 69 | [openPanel setAllowsOtherFileTypes:NO]; |
| 70 | NSArray* types = [[NSArray alloc] initWithObjects:@"", nil]; |
| 71 | if ( [openPanel runModalForTypes: types] == NSOKButton ) |
| 72 | { |
| 73 | NSString* neuroNetStr = [[NSString alloc] initWithContentsOfFile:[openPanel filename]]; |
| 74 | NSImage* archivedImage = [ImageAlgorythms deArchive:neuroNetStr]; |
| 75 | if (!archivedImage) |
| 76 | { |
| 77 | NSRunAlertPanel(@"Error", @"Wrong image representation", @"Try again", nil, nil); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | [resultImage setImage:archivedImage]; |
| 82 | [archivedImage release]; |
| 83 | [openSourceImageControl setEnabled:NO]; |
| 84 | [openArchiveMatrixControl setEnabled:NO]; |
| 85 | [archiveButton setEnabled:NO]; |
| 86 | [saveArchiveMatrixControl setEnabled:NO]; |
| 87 | [closeControl setEnabled:YES]; |
| 88 | } |
| 89 | } |
| 90 | [types release]; |
| 91 | } |
| 92 | |
| 93 | - (IBAction)saveArchiveMatrix:(id)sender |
| 94 | { |
| 95 | NSSavePanel* savePanel = [NSSavePanel savePanel]; |
| 96 | [savePanel setCanCreateDirectories:YES]; |
| 97 | [savePanel setAllowsOtherFileTypes:NO]; |
| 98 | if ( [savePanel runModal] == NSOKButton ) |
| 99 | { |
| 100 | // NSString* filename = [[savePanel filename] stringByAppendingString:@"" ]; |
| 101 | // NSString* neuroNetStrRepresentation = [[NSString alloc] initWithFormat:@"%@", neuroNet]; |
| 102 | NSLog(@"filesaving not supported in this version"); |
| 103 | // [ neuroNetStrRepresentation writeToFile:filename atomically:NO]; |
| 104 | } |
| 105 | } |
| 106 | - (void) neuro_arch |
| 107 | { |
| 108 | archThreadPool = [[NSAutoreleasePool alloc] init]; |
| 109 | if (!neuroNet) |
| 110 | { |
| 111 | [archThreadPool release]; |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | int loopCounter = 0; |
| 116 | NSString* loops_error = @""; |
| 117 | int maxLoops; |
| 118 | [ImageNeuroNet testOnInt:[maxLoopsField stringValue] errorStr:&loops_error errorStrV:@"loop " result:&maxLoops]; |
| 119 | if (loops_error != @"") |
| 120 | { |
| 121 | NSRunAlertPanel(@"Warning", [@"Wrong input on " stringByAppendingString:loops_error], @"Ok", nil, nil); |
| 122 | [archThreadPool release]; |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | [self disableControls]; |
| 127 | [closeControl setEnabled:FALSE]; |
| 128 | [archiveButton setEnabled:FALSE]; |
| 129 | [progress startAnimation:self]; |
| 130 | double old_diff = MAXFLOAT; |
| 131 | int old_diff_eq_diff_marker = 0; |
| 132 | double diff; |
| 133 | double alpha; |
| 134 | @try |
| 135 | { |
| 136 | while ([neuroNet fastGoodEnough:&diff] == NO) |
| 137 | { |
| 138 | if ( fabs(old_diff-diff) < MIN_DIFF_BETWEEN_DIFF) |
| 139 | { |
| 140 | old_diff_eq_diff_marker += 1 ; |
| 141 | if (old_diff_eq_diff_marker > DIFF_EQUAL_MIN_TIMES) |
| 142 | { |
| 143 | NSRunAlertPanel(@"Note", @"We think that speed of archiving to slow.\nPlease 'Archive' to do next loop.", @"Ok", nil, nil); |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | old_diff_eq_diff_marker = 0; |
| 150 | old_diff = diff; |
| 151 | } |
| 152 | [neuroNet fastTeach:&alpha]; |
| 153 | loopCounter++; |
| 154 | if (loopCounter > maxLoops) |
| 155 | break; |
| 156 | [loopRes setStringValue:[NSString stringWithFormat:@"%d", loopCounter]]; |
| 157 | [currD setStringValue:[NSString stringWithFormat:@"%f", diff]]; |
| 158 | [currA setStringValue:[NSString stringWithFormat:@"%f", alpha]]; |
| 159 | if ([logWith state]) |
| 160 | if (loopCounter % ilogEachValue == 0) |
| 161 | NSLog(@",%d,%f,%f", loopCounter, diff, alpha); |
| 162 | } |
| 163 | resultImageInstance = [neuroNet getResultImage]; |
| 164 | [resultImage setImage:resultImageInstance]; |
| 165 | [resultImageInstance release]; |
| 166 | resultImageInstance = NULL; |
| 167 | } |
| 168 | @catch (NSException * e) |
| 169 | { |
| 170 | NSRunAlertPanel(@"Warning",@"This network can't archive this image, please check 'With normilizing' to 'Yes'.", @"Ok", nil, nil); |
| 171 | [neuroNet release]; |
| 172 | neuroNet = NULL; |
| 173 | } |
| 174 | @finally |
| 175 | { |
| 176 | [saveArchiveMatrixControl setEnabled:YES]; |
| 177 | [progress stopAnimation:self]; |
| 178 | [archiveButton setEnabled:YES]; |
| 179 | [closeControl setEnabled:YES]; |
| 180 | [maxLoopsField setEnabled:YES]; |
| 181 | [archThreadPool release]; |
| 182 | archThreadPool = NULL; |
| 183 | [thread release]; |
| 184 | thread = NULL; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | - (IBAction)archive:(id)sender |
| 189 | { |
| 190 | // 0) parameters validation |
| 191 | |
| 192 | // n -> width, m -> height |
| 193 | if ([logWith state]) |
| 194 | { |
| 195 | if ([[NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"[1-9][0-9]*"] |
| 196 | evaluateWithObject:[logEachValue stringValue]] == YES) |
| 197 | ilogEachValue = [logEachValue intValue]; |
| 198 | else |
| 199 | { |
| 200 | NSRunAlertPanel(@"Warning", @"Wrong input on 'log each'", @"Ok", nil, nil); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | } |
| 205 | if (!neuroNet) |
| 206 | neuroNet = [ImageNeuroNet tryInit:[sourceImage image] |
| 207 | nStr:[n stringValue] |
| 208 | mStr:[m stringValue] |
| 209 | pStr:[p stringValue] |
| 210 | aStr:[a stringValue] |
| 211 | dStr:[D stringValue] |
| 212 | TeachZeroLayer:[teachZeroLayer state] |
| 213 | UseAdaptiveStep:[adaptiveSteps state] |
| 214 | ShouldNormalize:[normilizingWith state] |
| 215 | ]; |
| 216 | if (thread) |
| 217 | [thread cancel]; |
| 218 | if (![thread isExecuting]) |
| 219 | { |
| 220 | [thread release]; |
| 221 | thread = NULL; |
| 222 | } |
| 223 | if (thread == NULL) |
| 224 | thread = [[NSThread alloc] initWithTarget:self selector:@selector(neuro_arch) object:nil]; |
| 225 | [thread start]; |
| 226 | |
| 227 | } |
| 228 | - (IBAction)close:(id)sender |
| 229 | { |
| 230 | if (thread) |
| 231 | { |
| 232 | [thread cancel]; |
| 233 | if (archThreadPool) |
| 234 | { |
| 235 | [archThreadPool release]; |
| 236 | archThreadPool = NULL; |
| 237 | } |
| 238 | [thread release]; |
| 239 | thread = NULL; |
| 240 | } |
| 241 | if (neuroNet) |
| 242 | { |
| 243 | [neuroNet release]; |
| 244 | neuroNet = NULL; |
| 245 | } |
| 246 | if ([sourceImage image]) |
| 247 | { |
| 248 | NSImage* image = [sourceImage image]; |
| 249 | [sourceImage setImage:nil]; |
| 250 | [image release]; |
| 251 | } |
| 252 | if ([resultImage image]) |
| 253 | { |
| 254 | [resultImage setImage:nil]; |
| 255 | } |
| 256 | [self disableControls]; |
| 257 | [openSourceImageControl setEnabled:YES]; |
| 258 | } |
| 259 | - (IBAction)logWithClicked:(id)sender |
| 260 | { |
| 261 | if (!withLogBool) |
| 262 | { |
| 263 | [logEachValue setHidden:NO]; |
| 264 | [logEachValueLabel setHidden:NO]; |
| 265 | withLogBool = TRUE; |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | [logEachValue setHidden:YES]; |
| 270 | [logEachValueLabel setHidden:YES]; |
| 271 | withLogBool = FALSE; |
| 272 | } |
| 273 | |
| 274 | } |
| 275 | @end |
| 276 | |