Created
August 14, 2015 17:32
-
-
Save hasanadil/7c4734ab8fb0542c4d7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Divide up the work among workers | |
NSUInteger numberOfWorkers = 4; | |
NSUInteger pixelsPerWorker = pixelCount/numberOfWorkers; | |
for (NSUInteger worker = 0; worker < numberOfWorkers; worker++) { | |
//Submit work blocks to be executed concurrently | |
dispatch_async(weakMe.concurrentQueue, ^{ | |
//Add the colors result to a trie data strucuture whose leaf is the counter, O(1) baby | |
HATrie *trie = [[HATrie alloc] init]; | |
//Get the pixels to work on | |
NSUInteger workerOffset = worker * pixelsPerWorker; | |
UInt32 * workerPixels; | |
workerPixels = (UInt32 *) calloc(pixelsPerWorker, sizeof(UInt32)); | |
memcpy(workerPixels, pixels + workerOffset, pixelsPerWorker); | |
//Loop over the pixels of interest | |
UInt32 * currentPixel = workerPixels; | |
for (NSUInteger j = 0; j < pixelsPerWorker; j++) { | |
UInt32 color = *currentPixel; | |
float red = R(color); | |
float green = G(color); | |
float blue = B(color); | |
if ((red == 0 && green == 0 && blue == 0) || (red == 1 && green == 1 && blue == 1)) { | |
continue; | |
} | |
HAColorComponents *components = [[HAColorComponents alloc] initWithRed:red green:green blue:blue]; | |
[trie addColorComponents:components]; | |
currentPixel++; | |
} | |
HAColorComponentsCount *maxColorComponentsCount = trie.maxCountColorComponents; | |
[workerResults setObject:maxColorComponentsCount forKey:@(worker)]; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment