fixed memory leaks in cocoa bindings (trac ticket #253). Thanks to N. Butko
This commit is contained in:
parent
ff7b46fa66
commit
4ec4cf6876
@ -41,9 +41,39 @@
|
|||||||
//
|
//
|
||||||
//M*/
|
//M*/
|
||||||
|
|
||||||
|
#import <TargetConditionals.h>
|
||||||
|
|
||||||
|
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
|
||||||
|
/*** begin IPhone OS Stubs ***/
|
||||||
|
// When highgui functions are referred to on iPhone OS, they will fail silently.
|
||||||
|
CV_IMPL int cvInitSystem( int argc, char** argv) { return 0;}
|
||||||
|
CV_IMPL int cvStartWindowThread(){ return 0; }
|
||||||
|
CV_IMPL void cvDestroyWindow( const char* name) {}
|
||||||
|
CV_IMPL void cvDestroyAllWindows( void ) {}
|
||||||
|
CV_IMPL void cvShowImage( const char* name, const CvArr* arr) {}
|
||||||
|
CV_IMPL void cvResizeWindow( const char* name, int width, int height) {}
|
||||||
|
CV_IMPL void cvMoveWindow( const char* name, int x, int y){}
|
||||||
|
CV_IMPL int cvCreateTrackbar (const char* trackbar_name,const char* window_name,
|
||||||
|
int* val, int count, CvTrackbarCallback on_notify) {return 0;}
|
||||||
|
CV_IMPL int cvCreateTrackbar2(const char* trackbar_name,const char* window_name,
|
||||||
|
int* val, int count, CvTrackbarCallback2 on_notify2, void* userdata) {return 0;}
|
||||||
|
CV_IMPL void cvSetMouseCallback( const char* name, CvMouseCallback function, void* info) {}
|
||||||
|
CV_IMPL int cvGetTrackbarPos( const char* trackbar_name, const char* window_name ) {return 0;}
|
||||||
|
CV_IMPL void cvSetTrackbarPos(const char* trackbar_name, const char* window_name, int pos) {}
|
||||||
|
CV_IMPL void* cvGetWindowHandle( const char* name ) {return NULL;}
|
||||||
|
CV_IMPL const char* cvGetWindowName( void* window_handle ) {return NULL;}
|
||||||
|
CV_IMPL int cvNamedWindow( const char* name, int flags ) {return 0; }
|
||||||
|
CV_IMPL int cvWaitKey (int maxWait) {return 0;}
|
||||||
|
//*** end IphoneOS Stubs ***/
|
||||||
|
#else
|
||||||
|
|
||||||
|
|
||||||
#import <Cocoa/Cocoa.h>
|
#import <Cocoa/Cocoa.h>
|
||||||
#include "precomp.hpp"
|
#include "precomp.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
const int TOP_BORDER = 7;
|
const int TOP_BORDER = 7;
|
||||||
|
|
||||||
static NSApplication *application = nil;
|
static NSApplication *application = nil;
|
||||||
@ -54,7 +84,7 @@ static bool wasInitialized = false;
|
|||||||
@interface CVView : NSView {
|
@interface CVView : NSView {
|
||||||
NSImage *image;
|
NSImage *image;
|
||||||
}
|
}
|
||||||
@property(assign) NSImage *image;
|
@property(retain) NSImage *image;
|
||||||
- (void)setImageData:(CvArr *)arr;
|
- (void)setImageData:(CvArr *)arr;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@ -66,8 +96,8 @@ static bool wasInitialized = false;
|
|||||||
CvTrackbarCallback callback;
|
CvTrackbarCallback callback;
|
||||||
CvTrackbarCallback2 callback2;
|
CvTrackbarCallback2 callback2;
|
||||||
}
|
}
|
||||||
@property(assign) NSSlider *slider;
|
@property(retain) NSSlider *slider;
|
||||||
@property(assign) NSTextField *name;
|
@property(retain) NSTextField *name;
|
||||||
@property(assign) int *value;
|
@property(assign) int *value;
|
||||||
@property(assign) void *userData;
|
@property(assign) void *userData;
|
||||||
@property(assign) CvTrackbarCallback callback;
|
@property(assign) CvTrackbarCallback callback;
|
||||||
@ -79,11 +109,13 @@ static bool wasInitialized = false;
|
|||||||
CvMouseCallback mouseCallback;
|
CvMouseCallback mouseCallback;
|
||||||
void *mouseParam;
|
void *mouseParam;
|
||||||
BOOL autosize;
|
BOOL autosize;
|
||||||
|
BOOL firstContent;
|
||||||
}
|
}
|
||||||
@property(assign) CvMouseCallback mouseCallback;
|
@property(assign) CvMouseCallback mouseCallback;
|
||||||
@property(assign) void *mouseParam;
|
@property(assign) void *mouseParam;
|
||||||
@property(assign) BOOL autosize;
|
@property(assign) BOOL autosize;
|
||||||
@property(assign) NSMutableDictionary *sliders;
|
@property(assign) BOOL firstContent;
|
||||||
|
@property(retain) NSMutableDictionary *sliders;
|
||||||
- (CVView *)contentView;
|
- (CVView *)contentView;
|
||||||
- (void)cvSendMouseEvent:(NSEvent *)event type:(int)type flags:(int)flags;
|
- (void)cvSendMouseEvent:(NSEvent *)event type:(int)type flags:(int)flags;
|
||||||
- (void)cvMouseEvent:(NSEvent *)event;
|
- (void)cvMouseEvent:(NSEvent *)event;
|
||||||
@ -92,9 +124,11 @@ static bool wasInitialized = false;
|
|||||||
|
|
||||||
static void icvCocoaCleanup(void)
|
static void icvCocoaCleanup(void)
|
||||||
{
|
{
|
||||||
|
//cout << "icvCocoaCleanup" << endl;
|
||||||
if( application )
|
if( application )
|
||||||
{
|
{
|
||||||
[application terminate:nil];
|
cvDestroyAllWindows();
|
||||||
|
//[application terminate:nil];
|
||||||
application = 0;
|
application = 0;
|
||||||
[pool release];
|
[pool release];
|
||||||
}
|
}
|
||||||
@ -102,6 +136,7 @@ static void icvCocoaCleanup(void)
|
|||||||
|
|
||||||
CV_IMPL int cvInitSystem( int argc, char** argv)
|
CV_IMPL int cvInitSystem( int argc, char** argv)
|
||||||
{
|
{
|
||||||
|
//cout << "cvInitSystem" << endl;
|
||||||
wasInitialized = true;
|
wasInitialized = true;
|
||||||
|
|
||||||
pool = [[NSAutoreleasePool alloc] init];
|
pool = [[NSAutoreleasePool alloc] init];
|
||||||
@ -116,43 +151,61 @@ CV_IMPL int cvInitSystem( int argc, char** argv)
|
|||||||
if( floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5 )
|
if( floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5 )
|
||||||
[application setActivationPolicy:0/*NSApplicationActivationPolicyRegular*/];
|
[application setActivationPolicy:0/*NSApplicationActivationPolicyRegular*/];
|
||||||
#endif
|
#endif
|
||||||
[application finishLaunching];
|
//[application finishLaunching];
|
||||||
atexit(icvCocoaCleanup);
|
//atexit(icvCocoaCleanup);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CVWindow *cvGetWindow(const char *name) {
|
CVWindow *cvGetWindow(const char *name) {
|
||||||
|
//cout << "cvGetWindow" << endl;
|
||||||
|
NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init];
|
||||||
NSString *cvname = [NSString stringWithFormat:@"%s", name];
|
NSString *cvname = [NSString stringWithFormat:@"%s", name];
|
||||||
return (CVWindow *)[windows valueForKey:cvname];
|
CVWindow* retval = (CVWindow*) [windows valueForKey:cvname] ;
|
||||||
|
//cout << "retain count: " << [retval retainCount] << endl;
|
||||||
|
//retval = [retval retain];
|
||||||
|
//cout << "retain count: " << [retval retainCount] << endl;
|
||||||
|
[localpool drain];
|
||||||
|
//cout << "retain count: " << [retval retainCount] << endl;
|
||||||
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
CV_IMPL int cvStartWindowThread()
|
CV_IMPL int cvStartWindowThread()
|
||||||
{
|
{
|
||||||
|
//cout << "cvStartWindowThread" << endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CV_IMPL void cvDestroyWindow( const char* name)
|
CV_IMPL void cvDestroyWindow( const char* name)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init];
|
||||||
|
//cout << "cvDestroyWindow" << endl;
|
||||||
CVWindow *window = cvGetWindow(name);
|
CVWindow *window = cvGetWindow(name);
|
||||||
if(window) {
|
if(window) {
|
||||||
[window performClose:nil];
|
[window performClose:nil];
|
||||||
[windows removeObjectForKey:[NSString stringWithFormat:@"%s", name]];
|
[windows removeObjectForKey:[NSString stringWithFormat:@"%s", name]];
|
||||||
}
|
}
|
||||||
|
[localpool drain];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CV_IMPL void cvDestroyAllWindows( void )
|
CV_IMPL void cvDestroyAllWindows( void )
|
||||||
{
|
{
|
||||||
|
//cout << "cvDestroyAllWindows" << endl;
|
||||||
|
NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init];
|
||||||
for(NSString *key in windows) {
|
for(NSString *key in windows) {
|
||||||
[[windows valueForKey:key] performClose:nil];
|
[[windows valueForKey:key] performClose:nil];
|
||||||
}
|
}
|
||||||
[windows removeAllObjects];
|
[windows removeAllObjects];
|
||||||
|
[localpool drain];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CV_IMPL void cvShowImage( const char* name, const CvArr* arr)
|
CV_IMPL void cvShowImage( const char* name, const CvArr* arr)
|
||||||
{
|
{
|
||||||
|
//cout << "cvShowImage" << endl;
|
||||||
|
NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init];
|
||||||
CVWindow *window = cvGetWindow(name);
|
CVWindow *window = cvGetWindow(name);
|
||||||
if(!window)
|
if(!window)
|
||||||
{
|
{
|
||||||
@ -167,21 +220,28 @@ CV_IMPL void cvShowImage( const char* name, const CvArr* arr)
|
|||||||
NSRect vrectOld = [[window contentView] frame];
|
NSRect vrectOld = [[window contentView] frame];
|
||||||
|
|
||||||
[[window contentView] setImageData:(CvArr *)arr];
|
[[window contentView] setImageData:(CvArr *)arr];
|
||||||
if(/*[window autosize] ||*/ empty)
|
if([window autosize] || [window firstContent] || empty)
|
||||||
{
|
{
|
||||||
NSRect vrectNew = vrectOld;
|
NSRect vrectNew = vrectOld;
|
||||||
vrectNew.size = [[[window contentView] image] size];
|
vrectNew.size = [[[window contentView] image] size];
|
||||||
rect.size.width += vrectNew.size.width - vrectOld.size.width;
|
rect.size.width += vrectNew.size.width - vrectOld.size.width;
|
||||||
rect.size.height += vrectNew.size.height - vrectOld.size.height;
|
rect.size.height += vrectNew.size.height - vrectOld.size.height;
|
||||||
|
rect.origin.y -= vrectNew.size.height - vrectOld.size.height;
|
||||||
|
|
||||||
[window setFrame:rect display:YES];
|
[window setFrame:rect display:YES];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
[window display];
|
[window display];
|
||||||
|
[window setFirstContent:NO];
|
||||||
}
|
}
|
||||||
|
[localpool drain];
|
||||||
}
|
}
|
||||||
|
|
||||||
CV_IMPL void cvResizeWindow( const char* name, int width, int height)
|
CV_IMPL void cvResizeWindow( const char* name, int width, int height)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
//cout << "cvResizeWindow" << endl;
|
||||||
|
NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init];
|
||||||
CVWindow *window = cvGetWindow(name);
|
CVWindow *window = cvGetWindow(name);
|
||||||
if(window) {
|
if(window) {
|
||||||
NSRect frame = [window frame];
|
NSRect frame = [window frame];
|
||||||
@ -189,23 +249,27 @@ CV_IMPL void cvResizeWindow( const char* name, int width, int height)
|
|||||||
frame.size.height = height;
|
frame.size.height = height;
|
||||||
[window setFrame:frame display:YES];
|
[window setFrame:frame display:YES];
|
||||||
}
|
}
|
||||||
|
[localpool drain];
|
||||||
}
|
}
|
||||||
|
|
||||||
CV_IMPL void cvMoveWindow( const char* name, int x, int y)
|
CV_IMPL void cvMoveWindow( const char* name, int x, int y)
|
||||||
{
|
{
|
||||||
|
|
||||||
CV_FUNCNAME("cvMoveWindow");
|
CV_FUNCNAME("cvMoveWindow");
|
||||||
__BEGIN__;
|
__BEGIN__;
|
||||||
|
|
||||||
|
NSAutoreleasePool* localpool1 = [[NSAutoreleasePool alloc] init];
|
||||||
CVWindow *window = nil;
|
CVWindow *window = nil;
|
||||||
|
|
||||||
if(name == NULL)
|
if(name == NULL)
|
||||||
CV_ERROR( CV_StsNullPtr, "NULL window name" );
|
CV_ERROR( CV_StsNullPtr, "NULL window name" );
|
||||||
|
//cout << "cvMoveWindow"<< endl;
|
||||||
window = cvGetWindow(name);
|
window = cvGetWindow(name);
|
||||||
if(window) {
|
if(window) {
|
||||||
y = [[window screen] frame].size.height - y;
|
y = [[window screen] frame].size.height - y;
|
||||||
[window setFrameTopLeftPoint:NSMakePoint(x, y)];
|
[window setFrameTopLeftPoint:NSMakePoint(x, y)];
|
||||||
}
|
}
|
||||||
|
[localpool1 drain];
|
||||||
|
|
||||||
__END__;
|
__END__;
|
||||||
}
|
}
|
||||||
@ -217,14 +281,19 @@ CV_IMPL int cvCreateTrackbar (const char* trackbar_name,
|
|||||||
{
|
{
|
||||||
CV_FUNCNAME("cvCreateTrackbar");
|
CV_FUNCNAME("cvCreateTrackbar");
|
||||||
|
|
||||||
|
|
||||||
int result = 0;
|
int result = 0;
|
||||||
CVWindow *window = nil;
|
CVWindow *window = nil;
|
||||||
|
NSAutoreleasePool* localpool2 = nil;
|
||||||
|
|
||||||
__BEGIN__;
|
__BEGIN__;
|
||||||
|
if (localpool2 != nil) [localpool2 drain];
|
||||||
|
localpool2 = [[NSAutoreleasePool alloc] init];
|
||||||
|
|
||||||
if(window_name == NULL)
|
if(window_name == NULL)
|
||||||
CV_ERROR( CV_StsNullPtr, "NULL window name" );
|
CV_ERROR( CV_StsNullPtr, "NULL window name" );
|
||||||
|
|
||||||
|
//cout << "cvCreateTrackbar" << endl ;
|
||||||
window = cvGetWindow(window_name);
|
window = cvGetWindow(window_name);
|
||||||
if(window) {
|
if(window) {
|
||||||
[window createSliderWithName:trackbar_name
|
[window createSliderWithName:trackbar_name
|
||||||
@ -233,7 +302,7 @@ CV_IMPL int cvCreateTrackbar (const char* trackbar_name,
|
|||||||
callback:on_notify];
|
callback:on_notify];
|
||||||
result = 1;
|
result = 1;
|
||||||
}
|
}
|
||||||
|
[localpool2 drain];
|
||||||
__END__;
|
__END__;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -245,12 +314,15 @@ CV_IMPL int cvCreateTrackbar2(const char* trackbar_name,
|
|||||||
CvTrackbarCallback2 on_notify2,
|
CvTrackbarCallback2 on_notify2,
|
||||||
void* userdata)
|
void* userdata)
|
||||||
{
|
{
|
||||||
|
//cout <<"cvCreateTrackbar2" << endl;
|
||||||
|
NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init];
|
||||||
int res = cvCreateTrackbar(trackbar_name, window_name, val, count, NULL);
|
int res = cvCreateTrackbar(trackbar_name, window_name, val, count, NULL);
|
||||||
if(res) {
|
if(res) {
|
||||||
CVSlider *slider = [[cvGetWindow(window_name) sliders] valueForKey:[NSString stringWithFormat:@"%s", trackbar_name]];
|
CVSlider *slider = [[cvGetWindow(window_name) sliders] valueForKey:[NSString stringWithFormat:@"%s", trackbar_name]];
|
||||||
[slider setCallback2:on_notify2];
|
[slider setCallback2:on_notify2];
|
||||||
[slider setUserData:userdata];
|
[slider setUserData:userdata];
|
||||||
}
|
}
|
||||||
|
[localpool drain];
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -261,8 +333,12 @@ cvSetMouseCallback( const char* name, CvMouseCallback function, void* info)
|
|||||||
CV_FUNCNAME("cvSetMouseCallback");
|
CV_FUNCNAME("cvSetMouseCallback");
|
||||||
|
|
||||||
CVWindow *window = nil;
|
CVWindow *window = nil;
|
||||||
|
NSAutoreleasePool* localpool3 = nil;
|
||||||
__BEGIN__;
|
__BEGIN__;
|
||||||
|
//cout << "cvSetMouseCallback" << endl;
|
||||||
|
|
||||||
|
if (localpool3 != nil) [localpool3 drain];
|
||||||
|
localpool3 = [[NSAutoreleasePool alloc] init];
|
||||||
|
|
||||||
if(name == NULL)
|
if(name == NULL)
|
||||||
CV_ERROR( CV_StsNullPtr, "NULL window name" );
|
CV_ERROR( CV_StsNullPtr, "NULL window name" );
|
||||||
@ -272,6 +348,7 @@ cvSetMouseCallback( const char* name, CvMouseCallback function, void* info)
|
|||||||
[window setMouseCallback:function];
|
[window setMouseCallback:function];
|
||||||
[window setMouseParam:info];
|
[window setMouseParam:info];
|
||||||
}
|
}
|
||||||
|
[localpool3 drain];
|
||||||
|
|
||||||
__END__;
|
__END__;
|
||||||
}
|
}
|
||||||
@ -282,12 +359,16 @@ cvSetMouseCallback( const char* name, CvMouseCallback function, void* info)
|
|||||||
|
|
||||||
CVWindow *window = nil;
|
CVWindow *window = nil;
|
||||||
int pos = -1;
|
int pos = -1;
|
||||||
|
NSAutoreleasePool* localpool4 = nil;
|
||||||
__BEGIN__;
|
__BEGIN__;
|
||||||
|
|
||||||
|
//cout << "cvGetTrackbarPos" << endl;
|
||||||
if(trackbar_name == NULL || window_name == NULL)
|
if(trackbar_name == NULL || window_name == NULL)
|
||||||
CV_ERROR( CV_StsNullPtr, "NULL trackbar or window name" );
|
CV_ERROR( CV_StsNullPtr, "NULL trackbar or window name" );
|
||||||
|
|
||||||
|
if (localpool4 != nil) [localpool4 drain];
|
||||||
|
localpool4 = [[NSAutoreleasePool alloc] init];
|
||||||
|
|
||||||
window = cvGetWindow(window_name);
|
window = cvGetWindow(window_name);
|
||||||
if(window) {
|
if(window) {
|
||||||
CVSlider *slider = [[window sliders] valueForKey:[NSString stringWithFormat:@"%s", trackbar_name]];
|
CVSlider *slider = [[window sliders] valueForKey:[NSString stringWithFormat:@"%s", trackbar_name]];
|
||||||
@ -295,7 +376,7 @@ cvSetMouseCallback( const char* name, CvMouseCallback function, void* info)
|
|||||||
pos = [[slider slider] intValue];
|
pos = [[slider slider] intValue];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
[localpool4 drain];
|
||||||
__END__;
|
__END__;
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
@ -306,15 +387,19 @@ CV_IMPL void cvSetTrackbarPos(const char* trackbar_name, const char* window_name
|
|||||||
|
|
||||||
CVWindow *window = nil;
|
CVWindow *window = nil;
|
||||||
CVSlider *slider = nil;
|
CVSlider *slider = nil;
|
||||||
|
NSAutoreleasePool* localpool5 = nil;
|
||||||
|
|
||||||
__BEGIN__;
|
__BEGIN__;
|
||||||
|
//cout << "cvSetTrackbarPos" << endl;
|
||||||
if(trackbar_name == NULL || window_name == NULL)
|
if(trackbar_name == NULL || window_name == NULL)
|
||||||
CV_ERROR( CV_StsNullPtr, "NULL trackbar or window name" );
|
CV_ERROR( CV_StsNullPtr, "NULL trackbar or window name" );
|
||||||
|
|
||||||
if(pos <= 0)
|
if(pos <= 0)
|
||||||
CV_ERROR( CV_StsOutOfRange, "Bad trackbar maximal value" );
|
CV_ERROR( CV_StsOutOfRange, "Bad trackbar maximal value" );
|
||||||
|
|
||||||
|
if (localpool5 != nil) [localpool5 drain];
|
||||||
|
localpool5 = [[NSAutoreleasePool alloc] init];
|
||||||
|
|
||||||
window = cvGetWindow(window_name);
|
window = cvGetWindow(window_name);
|
||||||
if(window) {
|
if(window) {
|
||||||
slider = [[window sliders] valueForKey:[NSString stringWithFormat:@"%s", trackbar_name]];
|
slider = [[window sliders] valueForKey:[NSString stringWithFormat:@"%s", trackbar_name]];
|
||||||
@ -322,22 +407,29 @@ CV_IMPL void cvSetTrackbarPos(const char* trackbar_name, const char* window_name
|
|||||||
[[slider slider] setIntValue:pos];
|
[[slider slider] setIntValue:pos];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
[localpool5 drain];
|
||||||
|
|
||||||
__END__;
|
__END__;
|
||||||
}
|
}
|
||||||
|
|
||||||
CV_IMPL void* cvGetWindowHandle( const char* name )
|
CV_IMPL void* cvGetWindowHandle( const char* name )
|
||||||
{
|
{
|
||||||
|
//cout << "cvGetWindowHandle" << endl;
|
||||||
return cvGetWindow(name);
|
return cvGetWindow(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CV_IMPL const char* cvGetWindowName( void* window_handle )
|
CV_IMPL const char* cvGetWindowName( void* window_handle )
|
||||||
{
|
{
|
||||||
|
//cout << "cvGetWindowName" << endl;
|
||||||
|
NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init];
|
||||||
for(NSString *key in windows) {
|
for(NSString *key in windows) {
|
||||||
if([windows valueForKey:key] == window_handle)
|
if([windows valueForKey:key] == window_handle) {
|
||||||
|
[localpool drain];
|
||||||
return [key UTF8String];
|
return [key UTF8String];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
[localpool drain];
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -346,20 +438,38 @@ CV_IMPL int cvNamedWindow( const char* name, int flags )
|
|||||||
if( !wasInitialized )
|
if( !wasInitialized )
|
||||||
cvInitSystem(0, 0);
|
cvInitSystem(0, 0);
|
||||||
|
|
||||||
|
//cout << "cvNamedWindow" << endl;
|
||||||
|
NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init];
|
||||||
CVWindow *window = cvGetWindow(name);
|
CVWindow *window = cvGetWindow(name);
|
||||||
if( window )
|
if( window )
|
||||||
{
|
{
|
||||||
[window setAutosize:(flags == CV_WINDOW_AUTOSIZE)];
|
[window setAutosize:(flags == CV_WINDOW_AUTOSIZE)];
|
||||||
|
[localpool drain];
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
window = [[CVWindow alloc] initWithContentRect:NSMakeRect(0,0,200,200)
|
NSScreen* mainDisplay = [NSScreen mainScreen];
|
||||||
styleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|
|
|
||||||
(!(flags & CV_WND_PROP_AUTOSIZE) ? NSResizableWindowMask : 0)
|
|
||||||
backing:NSBackingStoreBuffered
|
|
||||||
defer:NO];
|
|
||||||
|
|
||||||
NSString *windowName = [NSString stringWithFormat:@"%s", name];
|
NSString *windowName = [NSString stringWithFormat:@"%s", name];
|
||||||
|
NSUInteger showResize = (flags == CV_WINDOW_AUTOSIZE) ? 0: NSResizableWindowMask ;
|
||||||
|
NSUInteger styleMask = NSTitledWindowMask|NSMiniaturizableWindowMask|showResize;
|
||||||
|
CGFloat windowWidth = [NSWindow minFrameWidthWithTitle:windowName styleMask:styleMask];
|
||||||
|
NSRect initContentRect = NSMakeRect(0, 0, windowWidth, 0);
|
||||||
|
if (mainDisplay) {
|
||||||
|
NSRect dispFrame = [mainDisplay visibleFrame];
|
||||||
|
initContentRect.origin.y = dispFrame.size.height-20;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
window = [[CVWindow alloc] initWithContentRect:initContentRect
|
||||||
|
styleMask:NSTitledWindowMask|NSMiniaturizableWindowMask|showResize
|
||||||
|
backing:NSBackingStoreBuffered
|
||||||
|
defer:YES
|
||||||
|
screen:mainDisplay];
|
||||||
|
|
||||||
|
[window setFrameTopLeftPoint:initContentRect.origin];
|
||||||
|
|
||||||
|
[window setFirstContent:YES];
|
||||||
|
|
||||||
[window setContentView:[[CVView alloc] init]];
|
[window setContentView:[[CVView alloc] init]];
|
||||||
|
|
||||||
@ -373,22 +483,24 @@ CV_IMPL int cvNamedWindow( const char* name, int flags )
|
|||||||
|
|
||||||
[windows setValue:window forKey:windowName];
|
[windows setValue:window forKey:windowName];
|
||||||
|
|
||||||
|
[localpool drain];
|
||||||
return [windows count]-1;
|
return [windows count]-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
CV_IMPL int cvWaitKey (int maxWait)
|
CV_IMPL int cvWaitKey (int maxWait)
|
||||||
{
|
{
|
||||||
|
//cout << "cvWaitKey" << endl;
|
||||||
int returnCode = -1;
|
int returnCode = -1;
|
||||||
double start = [[NSDate date] timeIntervalSince1970];
|
double start = [[NSDate date] timeIntervalSince1970];
|
||||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
NSAutoreleasePool *localpool = [[NSAutoreleasePool alloc] init];
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
if(([[NSDate date] timeIntervalSince1970] - start) * 1000 >= maxWait && maxWait>0)
|
if(([[NSDate date] timeIntervalSince1970] - start) * 1000 >= maxWait && maxWait>0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//event = [application currentEvent];
|
//event = [application currentEvent];
|
||||||
[pool release];
|
[localpool drain];
|
||||||
pool = [[NSAutoreleasePool alloc] init];
|
localpool = [[NSAutoreleasePool alloc] init];
|
||||||
|
|
||||||
NSEvent *event =
|
NSEvent *event =
|
||||||
[application
|
[application
|
||||||
@ -408,7 +520,7 @@ CV_IMPL int cvWaitKey (int maxWait)
|
|||||||
|
|
||||||
[NSThread sleepForTimeInterval:1/100.];
|
[NSThread sleepForTimeInterval:1/100.];
|
||||||
}
|
}
|
||||||
[pool release];
|
[localpool drain];
|
||||||
|
|
||||||
return returnCode;
|
return returnCode;
|
||||||
}
|
}
|
||||||
@ -418,9 +530,11 @@ CV_IMPL int cvWaitKey (int maxWait)
|
|||||||
@synthesize mouseCallback;
|
@synthesize mouseCallback;
|
||||||
@synthesize mouseParam;
|
@synthesize mouseParam;
|
||||||
@synthesize autosize;
|
@synthesize autosize;
|
||||||
|
@synthesize firstContent;
|
||||||
@synthesize sliders;
|
@synthesize sliders;
|
||||||
|
|
||||||
- (void)cvSendMouseEvent:(NSEvent *)event type:(int)type flags:(int)flags {
|
- (void)cvSendMouseEvent:(NSEvent *)event type:(int)type flags:(int)flags {
|
||||||
|
//cout << "cvSendMouseEvent" << endl;
|
||||||
NSPoint mp = [NSEvent mouseLocation];
|
NSPoint mp = [NSEvent mouseLocation];
|
||||||
NSRect visible = [[self contentView] frame];
|
NSRect visible = [[self contentView] frame];
|
||||||
mp = [self convertScreenToBase: mp];
|
mp = [self convertScreenToBase: mp];
|
||||||
@ -444,6 +558,7 @@ CV_IMPL int cvWaitKey (int maxWait)
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (void)cvMouseEvent:(NSEvent *)event {
|
- (void)cvMouseEvent:(NSEvent *)event {
|
||||||
|
//cout << "cvMouseEvent" << endl;
|
||||||
if(!mouseCallback)
|
if(!mouseCallback)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -464,16 +579,20 @@ CV_IMPL int cvWaitKey (int maxWait)
|
|||||||
if([event type] == NSOtherMouseDragged) {[self cvSendMouseEvent:event type:CV_EVENT_MOUSEMOVE flags:flags | CV_EVENT_FLAG_MBUTTON];}
|
if([event type] == NSOtherMouseDragged) {[self cvSendMouseEvent:event type:CV_EVENT_MOUSEMOVE flags:flags | CV_EVENT_FLAG_MBUTTON];}
|
||||||
}
|
}
|
||||||
- (void)keyDown:(NSEvent *)theEvent {
|
- (void)keyDown:(NSEvent *)theEvent {
|
||||||
|
//cout << "keyDown" << endl;
|
||||||
[super keyDown:theEvent];
|
[super keyDown:theEvent];
|
||||||
}
|
}
|
||||||
- (void)rightMouseDragged:(NSEvent *)theEvent {
|
- (void)rightMouseDragged:(NSEvent *)theEvent {
|
||||||
|
//cout << "rightMouseDragged" << endl ;
|
||||||
[self cvMouseEvent:theEvent];
|
[self cvMouseEvent:theEvent];
|
||||||
}
|
}
|
||||||
- (void)rightMouseUp:(NSEvent *)theEvent {
|
- (void)rightMouseUp:(NSEvent *)theEvent {
|
||||||
|
//cout << "rightMouseUp" << endl;
|
||||||
[self cvMouseEvent:theEvent];
|
[self cvMouseEvent:theEvent];
|
||||||
}
|
}
|
||||||
- (void)rightMouseDown:(NSEvent *)theEvent {
|
- (void)rightMouseDown:(NSEvent *)theEvent {
|
||||||
// Does not seem to work?
|
// Does not seem to work?
|
||||||
|
//cout << "rightMouseDown" << endl;
|
||||||
[self cvMouseEvent:theEvent];
|
[self cvMouseEvent:theEvent];
|
||||||
}
|
}
|
||||||
- (void)mouseMoved:(NSEvent *)theEvent {
|
- (void)mouseMoved:(NSEvent *)theEvent {
|
||||||
@ -499,6 +618,7 @@ CV_IMPL int cvWaitKey (int maxWait)
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (void)createSliderWithName:(const char *)name maxValue:(int)max value:(int *)value callback:(CvTrackbarCallback)callback {
|
- (void)createSliderWithName:(const char *)name maxValue:(int)max value:(int *)value callback:(CvTrackbarCallback)callback {
|
||||||
|
//cout << "createSliderWithName" << endl;
|
||||||
if(sliders == nil)
|
if(sliders == nil)
|
||||||
sliders = [[NSMutableDictionary alloc] init];
|
sliders = [[NSMutableDictionary alloc] init];
|
||||||
|
|
||||||
@ -512,6 +632,9 @@ CV_IMPL int cvWaitKey (int maxWait)
|
|||||||
CVSlider *slider = [[CVSlider alloc] init];
|
CVSlider *slider = [[CVSlider alloc] init];
|
||||||
[[slider name] setStringValue:cvname];
|
[[slider name] setStringValue:cvname];
|
||||||
[[slider slider] setMaxValue:max];
|
[[slider slider] setMaxValue:max];
|
||||||
|
[[slider slider] setMinValue:0];
|
||||||
|
[[slider slider] setNumberOfTickMarks:(max+1)];
|
||||||
|
[[slider slider] setAllowsTickMarkValuesOnly:YES];
|
||||||
if(value)
|
if(value)
|
||||||
{
|
{
|
||||||
[[slider slider] setIntValue:*value];
|
[[slider slider] setIntValue:*value];
|
||||||
@ -527,6 +650,13 @@ CV_IMPL int cvWaitKey (int maxWait)
|
|||||||
// Update slider sizes
|
// Update slider sizes
|
||||||
[[self contentView] setFrameSize:[[self contentView] frame].size];
|
[[self contentView] setFrameSize:[[self contentView] frame].size];
|
||||||
[[self contentView] setNeedsDisplay:YES];
|
[[self contentView] setNeedsDisplay:YES];
|
||||||
|
|
||||||
|
|
||||||
|
int height = 0;
|
||||||
|
for(NSString *key in sliders) {
|
||||||
|
height += [[sliders valueForKey:key] frame].size.height;
|
||||||
|
}
|
||||||
|
[self setContentMinSize:NSMakeSize(0, height)];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (CVView *)contentView {
|
- (CVView *)contentView {
|
||||||
@ -540,12 +670,15 @@ CV_IMPL int cvWaitKey (int maxWait)
|
|||||||
@synthesize image;
|
@synthesize image;
|
||||||
|
|
||||||
- (id)init {
|
- (id)init {
|
||||||
|
//cout << "CVView init" << endl;
|
||||||
[super init];
|
[super init];
|
||||||
image = nil;
|
image = [[NSImage alloc] init];
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setImageData:(CvArr *)arr {
|
- (void)setImageData:(CvArr *)arr {
|
||||||
|
//cout << "setImageData" << endl;
|
||||||
|
NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init];
|
||||||
CvMat *arrMat, *cvimage, stub;
|
CvMat *arrMat, *cvimage, stub;
|
||||||
|
|
||||||
arrMat = cvGetMat(arr, &stub);
|
arrMat = cvGetMat(arr, &stub);
|
||||||
@ -567,41 +700,54 @@ CV_IMPL int cvWaitKey (int maxWait)
|
|||||||
|
|
||||||
CGImageRef imageRef = CGImageCreate(width, height, size , size*nbChannels , cvimage->step, colorspace, kCGImageAlphaNone , provider, NULL, true, kCGRenderingIntentDefault);
|
CGImageRef imageRef = CGImageCreate(width, height, size , size*nbChannels , cvimage->step, colorspace, kCGImageAlphaNone , provider, NULL, true, kCGRenderingIntentDefault);
|
||||||
|
|
||||||
NSBitmapImageRep *bitmap = [[[NSBitmapImageRep alloc] initWithCGImage:imageRef] autorelease];
|
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:imageRef];
|
||||||
if(image) {
|
if(image) {
|
||||||
[image release];
|
[image release];
|
||||||
}
|
}
|
||||||
|
|
||||||
image = [[NSImage alloc] init];
|
image = [[NSImage alloc] init];
|
||||||
[image addRepresentation:bitmap];
|
[image addRepresentation:bitmap];
|
||||||
|
[bitmap release];
|
||||||
|
|
||||||
|
CGColorSpaceRelease(colorspace);
|
||||||
CGDataProviderRelease(provider);
|
CGDataProviderRelease(provider);
|
||||||
|
CGImageRelease(imageRef);
|
||||||
cvReleaseMat(&cvimage);
|
cvReleaseMat(&cvimage);
|
||||||
|
[localpool drain];
|
||||||
|
|
||||||
[self setNeedsDisplay:YES];
|
[self setNeedsDisplay:YES];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setFrameSize:(NSSize)size {
|
- (void)setFrameSize:(NSSize)size {
|
||||||
|
//cout << "setFrameSize" << endl;
|
||||||
[super setFrameSize:size];
|
[super setFrameSize:size];
|
||||||
|
|
||||||
|
NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init];
|
||||||
int height = size.height;
|
int height = size.height;
|
||||||
|
|
||||||
CVWindow *window = (CVWindow *)[self window];
|
CVWindow *cvwindow = (CVWindow *)[self window];
|
||||||
for(NSString *key in [window sliders]) {
|
for(NSString *key in [cvwindow sliders]) {
|
||||||
NSSlider *slider = [[window sliders] valueForKey:key];
|
NSSlider *slider = [[cvwindow sliders] valueForKey:key];
|
||||||
NSRect r = [slider frame];
|
NSRect r = [slider frame];
|
||||||
r.origin.y = height - r.size.height;
|
r.origin.y = height - r.size.height;
|
||||||
[slider setFrame:r];
|
[slider setFrame:r];
|
||||||
height -= r.size.height;
|
height -= r.size.height;
|
||||||
}
|
}
|
||||||
|
[localpool drain];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)drawRect:(NSRect)rect {
|
- (void)drawRect:(NSRect)rect {
|
||||||
CVWindow *window = (CVWindow *)[self window];
|
//cout << "drawRect" << endl;
|
||||||
|
[super drawRect:rect];
|
||||||
|
|
||||||
|
NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init];
|
||||||
|
CVWindow *cvwindow = (CVWindow *)[self window];
|
||||||
int height = 0;
|
int height = 0;
|
||||||
for(NSString *key in [window sliders]) {
|
for(NSString *key in [cvwindow sliders]) {
|
||||||
height += [[[window sliders] valueForKey:key] frame].size.height;
|
height += [[[cvwindow sliders] valueForKey:key] frame].size.height;
|
||||||
}
|
}
|
||||||
|
|
||||||
[super drawRect:rect];
|
|
||||||
|
|
||||||
NSRect imageRect = {{0,0}, {self.frame.size.width, self.frame.size.height-height-6}};
|
NSRect imageRect = {{0,0}, {self.frame.size.width, self.frame.size.height-height-6}};
|
||||||
|
|
||||||
@ -611,6 +757,7 @@ CV_IMPL int cvWaitKey (int maxWait)
|
|||||||
operation: NSCompositeSourceOver
|
operation: NSCompositeSourceOver
|
||||||
fraction: 1.0];
|
fraction: 1.0];
|
||||||
}
|
}
|
||||||
|
[localpool release];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -671,4 +818,6 @@ CV_IMPL int cvWaitKey (int maxWait)
|
|||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
/* End of file. */
|
/* End of file. */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user