Skip to content

Instantly share code, notes, and snippets.

Avatar

hasanadil

  • Portland, Maine, USA
View GitHub Profile
View HAColorFinder.h
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
@interface HAColorFinder : NSObject
/*
Return the color in the image that is most dominant.
Where dominant is defined as the most number of pixels which contain the same color.
*/
-(void) fetchDominantColorFromImage:(NSImage*)image
View HAColorFinder.m
//wait on everything to be completed
dispatch_barrier_async(weakMe.concurrentQueue, ^{
//Get the highest count amont the worker results
NSArray* allCounts = [workerResults allValues];
NSSortDescriptor* countSort = [[NSSortDescriptor alloc] initWithKey:@"count" ascending:NO];
NSArray* sortedCounts = [allCounts sortedArrayUsingDescriptors:@[countSort]];
HAColorComponentsCount* colorComponentCount = [sortedCounts firstObject];
HAColorComponents *components = colorComponentCount.components;
View HAColorFinder.m
//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
View HAColorFinder.m
//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
View HAColorFinder.m
-(void) fetchDominantColorFromImage:(NSImage*)image withCompletion:(void(^)(NSColor *color, NSTimeInterval processingTime))completion {
...
__weak typeof(self) weakMe = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[weakMe pixelsFromImage:image withCompletion:^(UInt32 *pixels, NSUInteger pixelCount) {
....
View pixelsFromImage.m
-(void) pixelsFromImage:(NSImage*)image
withCompletion:(void(^)(UInt32 *pixels, NSUInteger pixelCount))completion {
//Extract pixels from the image in a 1D array
CFDataRef inputData = (__bridge CFDataRef)[image TIFFRepresentation];
CGImageSourceRef inputSource = CGImageSourceCreateWithData(inputData, NULL);
CGImageRef inputCGImage = CGImageSourceCreateImageAtIndex(inputSource, 0, NULL);
NSUInteger width = CGImageGetWidth(inputCGImage);
NSUInteger height = CGImageGetHeight(inputCGImage);
NSUInteger numberOfPixels = height * width;
View gist:96bad711d6f71ec806f7
class 💩💩💩💩 {
func 💩💩💩(😎: Int, 🐯: Int) -> Int {
return 😎 + 🐯;
}
}
let 🐔 = 3;
let 😥 = 🐔 + 2;
let 💩 = 💩💩💩💩();
print(💩.💩💩💩(🐔, 🐯:😥));
View gist:3c40e35ebac22267d670
let camera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
var error : NSError? = nil
let cameraInput : AVCaptureDeviceInput? = AVCaptureDeviceInput.deviceInputWithDevice(camera, error: &error) as? AVCaptureDeviceInput
self.session.addInput(cameraInput)
if error != nil || cameraInput == nil {
//Awesome error handling :D
println(error!.localizedDescription)
}
else {
View gist:e10296ed1bbe89e2a798
let allowedBarcodes = [AVMetadataObjectTypeUPCECode,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeCode93Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code,
AVMetadataObjectTypeQRCode,
AVMetadataObjectTypeAztecCode