Skip to content

Instantly share code, notes, and snippets.

@dennda
Created October 8, 2011 00:23
Show Gist options
  • Save dennda/1271674 to your computer and use it in GitHub Desktop.
Save dennda/1271674 to your computer and use it in GitHub Desktop.
- (void)taskTerminated:(NSNotification *)aNotification {
[self.progressBar incrementBy:1.];
spawned_tasks -= 1;
if (_unprocessed_files.count > 0)
[self spawnCompressor];
}
- (void) spawnCompressor {
NSString *executable = [[NSBundle mainBundle] pathForAuxiliaryExecutable:@"compress"];
assert(executable != nil);
if (spawned_tasks >= cores) {
NSLog(@"Not spawning task as there are %lu tasks already", spawned_tasks);
return;
}
NSLog(@"Spawning task and there are %lu tasks already", spawned_tasks);
spawned_tasks += 1;
NSUInteger oldcount = _unprocessed_files.count;
assert(oldcount > 0);
NSString *fn = [_unprocessed_files lastObject];
[_unprocessed_files removeObject:fn];
assert(oldcount > _unprocessed_files.count);
NSString *dest = [NSString stringWithFormat:@"%@.pvr", fn];
NSArray *args = [NSArray arrayWithObjects:fn, dest, nil];
NSTask *task = [[NSTask alloc] init];
task.launchPath = executable;
task.arguments = args;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(taskTerminated:)
name:NSTaskDidTerminateNotification
object:task];
[task launch];
}
- (IBAction)selectDataset:(id)sender {
NSLog(@"Presenting picker");
_files = [self presentPicker];
_unprocessed_files = [_files mutableCopy];
spawned_tasks = 0;
NSLog(@"User picked %lu files.", _files.count);
self.progressBar.maxValue = _files.count;
cores = [[NSProcessInfo processInfo] activeProcessorCount];
// For every .dcm file, create a process and convert it to a PVRTC texture
// PVRTexLib is not thread-safe.
for (NSUInteger i = 0; i < cores; i++) {
[self spawnCompressor];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment