FLTK logo

Re: Program becomes very slow after some time running

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.opengl  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: Program becomes very slow after some time running yin Jan 15, 2012  
 
// generated by Fast Light User Interface Designer (fluid) version 1.0300
#include "stdafx.h"
#include "OBPM_UI.h"
OBPMViewUI::OBPMViewUI() 
{
	int wCam,hCam;
	wCam=4;  //  1280;
	hCam=3;  //960;
//layout of widgets of interface	
  {   
	int wWin,hWin;
	mainWindow = new Fl_Double_Window(1600, 844, "Video Player");
    mainWindow->fullscreen();
    mainWindow->user_data((void*)(this));
	wWin=mainWindow->decorated_w();
    hWin=mainWindow->decorated_h();	

	int wZoomSlider,hZoomSlider;
	hZoomSlider=30;
	int wYPanSlider,hYPanSlider;
	wYPanSlider=30;
	int wXPanSlider,hXPanSlider;
	hXPanSlider=30;

    int wView,hView;	
	hView=hWin;

	int wBox,hBox;
	hBox=hView-hZoomSlider-hXPanSlider;
	wBox=1.0*wCam/hCam*hBox;
	wView=wBox+wYPanSlider;

	wZoomSlider=wBox; 	hYPanSlider=hBox;  	wXPanSlider=wBox;

    { VideoView = new Fl_Group(0, 0, wView, hView);
      VideoView->align(Fl_Align(FL_ALIGN_CENTER));
	  {
	    zoom_fit=new Fl_Button(0,0,wYPanSlider,hZoomSlider,"@<->");
	  } // Fl_Button* zoom_fit

	  {
		origin_fit=new Fl_Button(0,hZoomSlider+hYPanSlider,
			                     wYPanSlider,hXPanSlider,"@+");
	  }// Fl_Button* origin_fit

      {
		zoom = new Fl_Value_Slider(wYPanSlider, 0, wZoomSlider, 
			                       hZoomSlider, "Zoom");
        zoom->type(5);
        zoom->selection_color((Fl_Color)1);
        zoom->labeltype(FL_NO_LABEL);
        zoom->labelfont(1);
        zoom->labelsize(12);
        zoom->minimum(0.01);
        zoom->maximum(10);
        zoom->step(0.01);
        zoom->value(1.0);
        zoom->align(Fl_Align(FL_ALIGN_LEFT));
      } // Fl_Slider* zoom
      { ypan = new Fl_Value_Slider(0, hZoomSlider, wYPanSlider, 
		                              hYPanSlider, "Y Pan");
        ypan->type(4);
        ypan->selection_color((Fl_Color)4);
        ypan->labeltype(FL_NO_LABEL);
        ypan->labelsize(12);
        ypan->minimum(-1.0);
        ypan->maximum(1.0);
        ypan->step(0.01);
        ypan->value(0.0);
        ypan->align(Fl_Align(FL_ALIGN_LEFT));
      } // Fl_Slider* ypan
      {
		xpan = new Fl_Value_Slider(wYPanSlider, hView-hXPanSlider, 
			                       wXPanSlider, hXPanSlider, "X pan");
        xpan->type(5);
        xpan->selection_color((Fl_Color)4);
        xpan->labeltype(FL_NO_LABEL);
        xpan->labelfont(1);
        xpan->labelsize(12);
        xpan->minimum(-1.0);
        xpan->maximum(1.0);
        xpan->step(0.01);
        xpan->value(0.0);
        xpan->align(Fl_Align(FL_ALIGN_LEFT));
      } // Fl_Slider* xpan

	  {
		glView = new VideoGlWin(wYPanSlider,hZoomSlider,wBox,hBox);
	 	glView->selection_color((Fl_Color)10);
	  }
      VideoView->end();
    } // Fl_Group* VideoView
	
	int wBtnPannel,hBtnPannel;
	wBtnPannel=wWin-wView;
	hBtnPannel=wBtnPannel*0.3;

	int mBtn=3,nBtn=2;
	int wBtn,hBtn;
	wBtn=wBtnPannel/mBtn*0.8;
	hBtn=hBtnPannel/nBtn*0.8;
	int dwBtn,dhBtn;
	dwBtn=(wBtnPannel-wBtn*mBtn)/(mBtn+1.0);
	dhBtn=(hBtnPannel-hBtn*nBtn)/(nBtn+1.0);
    { grpCommandBtn = new Fl_Group(wWin-wBtnPannel, hWin-hBtnPannel, 
		                           wBtnPannel, hBtnPannel);
      grpCommandBtn->box(FL_FLAT_BOX);
      grpCommandBtn->color((Fl_Color)33);
      grpCommandBtn->align(Fl_Align(FL_ALIGN_BOTTOM_RIGHT));
      { btnFileSrc = new Fl_Light_Button(wWin-wBtnPannel+1.0*wBtn+2.0*dwBtn, 
		               hWin-hBtnPannel+1.0*dhBtn, wBtn, hBtn, "Open File...");

        btnFileSrc->labelfont(1);
        btnFileSrc->labelsize(20);
		btnFileSrc->selection_color((Fl_Color)2);
      } // Fl_Button* btnFileSrc
      grpCommandBtn->end();
    } // Fl_Group* grpCommandBtn

//set the callback functions
	
	btnFileSrc->callback((Fl_Callback*)cb_FileSrc);

	zoom->callback((Fl_Callback*)cb_zoom);
	xpan->callback((Fl_Callback*)cb_xpan);
	ypan->callback((Fl_Callback*)cb_ypan);
	zoom_fit->callback( (Fl_Callback*)cb_zoom_fit);
	origin_fit->callback( (Fl_Callback*)cb_origin_fit);

    mainWindow->end();
  } // Fl_Double_Window* mainWindow
  
  SRC_ID=0;
  m_pVideoFileCapture=NULL;
  m_continueCaptureThread=false;
  m_rawVideoframe=NULL;
  m_heventCaptureThreadDone = CreateEvent( NULL, FALSE, FALSE, NULL );

}


void OBPMViewUI::start(int argc,char**argv)
{
	mainWindow->show(argc,argv);
}


void OBPMViewUI::cb_zoom_i(Fl_Value_Slider * o, void * v)
{
	this->glView->zoom_ratio=o->value();
	this->glView->redraw();
}
void OBPMViewUI::cb_zoom(Fl_Value_Slider * o, void * v)
{
	((OBPMViewUI*)(o->parent()->parent()->user_data()))->cb_zoom_i(o,v);	
}


void OBPMViewUI::cb_xpan_i(Fl_Value_Slider* o, void*) 
{
	this->glView->xpan_ratio=o->value();
	this->glView->redraw();
}
void OBPMViewUI::cb_xpan(Fl_Value_Slider* o, void* v) 
{
  ((OBPMViewUI*)(o->parent()->parent()->user_data()))->cb_xpan_i(o,v);
}

void OBPMViewUI::cb_ypan_i(Fl_Value_Slider* o, void*) 
{
	this->glView->ypan_ratio=o->value();
	this->glView->redraw();
}
void OBPMViewUI::cb_ypan(Fl_Value_Slider* o, void* v) 
{
  ((OBPMViewUI*)(o->parent()->parent()->user_data()))->cb_ypan_i(o,v);
}

void OBPMViewUI::cb_zoom_fit_i(Fl_Button* o, void* v)
{
	this->glView->zoom_ratio=1.0;
	this->zoom->value(1.0);
	this->glView->redraw();
}

void OBPMViewUI::cb_zoom_fit(Fl_Button* o, void* v)
{
	((OBPMViewUI*)(o->parent()->parent()->user_data()))->cb_zoom_fit_i(o,v);
}

void OBPMViewUI::cb_origin_fit_i(Fl_Button* o, void* v)
{
	this->glView->xpan_ratio=0.0;
	this->glView->ypan_ratio=0.0;
	this->xpan->value(0.0);
	this->ypan->value(0.0);
	this->glView->redraw();
}

void OBPMViewUI::cb_origin_fit(Fl_Button* o, void* v)
{
	((OBPMViewUI*)(o->parent()->parent()->user_data()))->cb_origin_fit_i(o,v);
}


void OBPMViewUI::cb_FileSrc_i(Fl_Light_Button* o, void* v)
{
	
	OBPMViewUI* pUI=((OBPMViewUI*)(o->parent()->parent()->user_data()));

	if(pUI->SRC_ID==0)
	{
		bool bSuccess;
		bSuccess=true;
		//file choose dialog//
		Fl_Native_File_Chooser fnfc;
		const char szFilter[1024]=
			 "Video files(*.mpg,*.mpeg,*.mov,*.avi)\t*.{mpg,mpeg,mov,avi}\n";
		fnfc.title("Choose a video file");
		fnfc.type(Fl_Native_File_Chooser::BROWSE_FILE);
		fnfc.filter(szFilter);  
		switch(fnfc.show())
		{
		case -1:
		case 1:
			o->clear();
			return;
			break;
		default:
			break;
		}

		CvCapture *pCapture;
		pCapture=cvCreateFileCapture(fnfc.filename());			
		if(!pCapture)
			bSuccess=false;
		else
			pUI->VideoFileConnect(pCapture);

		if(bSuccess)
		{
			((Fl_Light_Button*) o)->set();
			pUI->SRC_ID=1;
		}
		else
		{
			((Fl_Light_Button*) o)->clear();
			fl_alert("Can not open specified file! \n Please check again.");
		}
	}
	else if(pUI->SRC_ID==1)
	{
		pUI->m_continueCaptureThread=false;			
		DWORD dwRet = WaitForSingleObject( m_heventCaptureThreadDone, 5000 );
		if(dwRet==WAIT_TIMEOUT)
			fl_alert("Can not get grab finish signal within 5s");
		
		if(pUI->glView->m_processedImage)
		{
			cvReleaseImage(&pUI->glView->m_processedImage);
			pUI->glView->m_processedImage=NULL;
		}
		((Fl_Light_Button*) o)->clear();
		pUI->glView->redraw();
		SRC_ID=0;
	}

}

void OBPMViewUI::cb_FileSrc(Fl_Light_Button* o, void* v)
{
	((OBPMViewUI*)(o->parent()->parent()->user_data()))->cb_FileSrc_i(o,v);
}


void OBPMViewUI::VideoFileConnect(CvCapture *pVideo)
{
	m_pVideoFileCapture=pVideo;
	m_continueCaptureThread=true;
	fl_create_thread(thread_capture_video_file,&ThreadCaptureVideoFile,this);
}


void* OBPMViewUI::ThreadCaptureVideoFile(void* pparam)
{
	OBPMViewUI* pUI =( (OBPMViewUI*) pparam);
	unsigned int uiRet;
	uiRet=pUI->DoCaptureLoop();
	if(uiRet!=0)
	{
		fl_alert("The grab thread has encountered a problem and had to terminate.");
		//Signal that the thread has died.
		SetEvent(pUI->m_heventCaptureThreadDone);
	}
	return 0;
}

//
// The image Capture thread.
unsigned int OBPMViewUI::DoCaptureLoop(void)
{	
	OBPMViewUI* pUI =this;	
	//start of main capture loop
	while(pUI->m_continueCaptureThread)
	{
		m_rawVideoframe=cvQueryFrame(m_pVideoFileCapture);		
		if(!m_rawVideoframe)
			break;
		if(!m_continueCaptureThread) 			break;
		
		CvSize sz;
		sz.width=m_rawVideoframe->width; 
		sz.height=m_rawVideoframe->height;

		CSingleLock dataLock( &m_csData );
		dataLock.Lock();

		if(!pUI->glView->m_processedImage)
			pUI->glView->m_processedImage=cvCreateImage(sz,IPL_DEPTH_8U,1);

		switch(m_rawVideoframe->nChannels)
		{
		case 3:
			{
				cvCvtColor(m_rawVideoframe,
					pUI->glView->m_processedImage,
					CV_BGR2GRAY);
				break;
			}
		case 1:
			{
				cvCopyImage(m_rawVideoframe,pUI->glView->m_processedImage);
				break;
			}
		default:
			break;
		}		
		dataLock.Unlock();

		//redraw the window
		pUI->glView->redraw();
	}
	pUI->m_continueCaptureThread=false;
	SetEvent(pUI->m_heventCaptureThreadDone);

	return 0;
}
Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.