Commits

Cliff Biffle committed 4394be6

Added a color delegate to KFKinect, and cleaned the sources up while I was in there. Color processing is now optional again (it doesn't happen if no delegate is provided), which is good, because freenect's Bayer interpolation was about 20% of our CPU cycles!

  • Participants
  • Parent commits 38960df

Comments (0)

Files changed (4)

 #import <Cocoa/Cocoa.h>
 #import "libfreenect.h"
 
-@protocol KFKinectDelegate;
+@protocol KFKinectDepthDelegate;
+@protocol KFKinectColorDelegate;
 
 @interface KFKinect : NSObject {
-	freenect_context *_context;
-	freenect_device *_device;
+  freenect_context *_context;
+  freenect_device *_device;
 	
-	BOOL _running;
+  BOOL _running;
 	
-	id<KFKinectDelegate> delegate;
+  id<KFKinectDepthDelegate> depthDelegate;
+  id<KFKinectColorDelegate> colorDelegate;
 }
 
-- init;
+- initWithDepthDelegate: (id<KFKinectDepthDelegate>)depthDelegate
+          colorDelegate: (id<KFKinectColorDelegate>)colorDelegate;
 - (void)open;
 - (void)close;
 
-@property(assign) id<KFKinectDelegate> delegate;
-
 @end
 
-@protocol KFKinectDelegate
+@protocol KFKinectDepthDelegate
 - (void)depthFieldUpdated: (const uint16_t *)bytes from: (KFKinect *)kinect;
 @end
 
+@protocol KFKinectColorDelegate
+- (void)colorFieldUpdated: (const uint8_t *)bytes from: (KFKinect *)kinect;
+@end
 @end
 
 static void depthCallback(freenect_device *dev, freenect_depth *depth, uint32_t timestamp) {
-    [(KFKinect *)freenect_get_user(dev) depthCallback:depth];
+  [(KFKinect *)freenect_get_user(dev) depthCallback:depth];
 }
 static void rgbCallback(freenect_device *dev, freenect_pixel *rgb, uint32_t timestamp) {
-    [(KFKinect *)freenect_get_user(dev) rgbCallback:rgb];
+  [(KFKinect *)freenect_get_user(dev) rgbCallback:rgb];
 }
 
 @implementation KFKinect
 
-- init
-{
+- initWithDepthDelegate: (id<KFKinectDepthDelegate>)dd
+          colorDelegate: (id<KFKinectColorDelegate>)cd {
 	if (![super init]) return nil;
 	
 	_running = NO;
-	delegate = nil;
+	depthDelegate = dd;
+  colorDelegate = cd;
 	return self;
 }
-- (void)open
-{
+
+- (void)open {
 	[NSThread detachNewThreadSelector:@selector(ioThread) toTarget:self withObject:nil];
 }
-- (void)close
-{
-	_running = NO;
+
+- (void)close {
+  _running = NO;
 }
 
-@synthesize delegate;
-
 @end
 
 @implementation KFKinect (Private)
 - (void)ioThread
 {
 	if(freenect_init(&_context, NULL) >= 0) {
-        if(freenect_open_device(_context, &_device, 0) >= 0) {
-            freenect_set_user(_device, self);
-            freenect_set_depth_callback(_device, depthCallback);
-            freenect_set_rgb_callback(_device, rgbCallback);
-            freenect_set_rgb_format(_device, FREENECT_FORMAT_RGB);
-            freenect_set_depth_format(_device, FREENECT_FORMAT_11_BIT);
-            freenect_start_depth(_device);
-            freenect_start_rgb(_device);
-            
-          NSLog(@"wheeee");
-          
-            while(!_running && freenect_process_events(_context) >= 0);
-            
-            freenect_close_device(_device);
-            _device = NULL;
-            
-            NSLog(@"Stopped");
-        } else {
-            NSLog(@"Could not open device");
-        }
-        freenect_shutdown(_context);
-        _context = NULL;
+    if(freenect_open_device(_context, &_device, 0) >= 0) {
+      freenect_set_user(_device, self);
+      freenect_set_depth_callback(_device, depthCallback);
+      freenect_set_rgb_callback(_device, rgbCallback);
+      freenect_set_rgb_format(_device, FREENECT_FORMAT_RGB);
+      freenect_set_depth_format(_device, FREENECT_FORMAT_11_BIT);
+      if (depthDelegate) freenect_start_depth(_device);
+      if (colorDelegate) freenect_start_rgb(_device);
+      
+      NSLog(@"wheeee");
+      
+      while(!_running && freenect_process_events(_context) >= 0);
+      
+      freenect_close_device(_device);
+      _device = NULL;
+      
+      NSLog(@"Stopped");
     } else {
+      NSLog(@"Could not open device");
+    }
+    freenect_shutdown(_context);
+    _context = NULL;
+  } else {
 		NSLog(@"Could not init device");
 	}
 }
 
-- (void)depthCallback: (freenect_depth *)buffer
-{
-	[delegate depthFieldUpdated: (const uint16_t *)buffer from: self];
+- (void)depthCallback: (freenect_depth *)buffer {
+	[depthDelegate depthFieldUpdated: (const uint16_t *)buffer from: self];
 }
 
-- (void)rgbCallback: (freenect_pixel *)buffer
-{
+- (void)rgbCallback: (freenect_pixel *)buffer {
+	[colorDelegate colorFieldUpdated: (const uint8_t *)buffer from: self];
 }
 
 @end
   GLint viewWidth, viewHeight;
 } camera_t;
 
-@interface KVDepthView : NSOpenGLView <KFKinectDelegate> {
+@interface KVDepthView : NSOpenGLView <KFKinectDepthDelegate> {
   NSMutableData *backgroundDepthField;
   NSMutableData *foregroundDepthField;
   

KinectViewerAppDelegate.m

     NSLog(@"Playing %@", replayFile);
     [NSThread detachNewThreadSelector:@selector(replayThread:) toTarget:self withObject:replayFile];
   } else {
-    kinect = [[KFKinect alloc] init];
-    kinect.delegate = depthView;
+    kinect = [[KFKinect alloc] initWithDepthDelegate: depthView
+                                       colorDelegate: nil];
     [kinect open];
   }
 }