Using Automation in C++  
Top  Previous 

This section shows how to get started with ImageWarp automation in Visual C++.

Creating the Project

VC++ 6.0

In the development environment select the New command from the file menu, and then select Projects/MFC AppWizard.exe . In the Project name field on the right type the name of your application, for instance ComTest and click OK . When MFC AppWizard appears, select Dialog based radio button and click Finish . The project will be created, and the program dialog ComTest will be displayed for editing.

VC++ (.NET)

In the .NET development environment select New -> Project. The New Project Dialog box will appear. Select Visual C++ projects on the left and MFC Application on the right. In the Name filed below type the name of your application, for instance ComTest and click OK . When MFC Application Wizard appears, click Application Type , select Dialog based radio button and click Finish . The project will be created, and the program dialog ComTest will be displayed for editing.


Generating class for ImageWarp COM object

VC++ 6.0

Right click in the dialog and select Class Wizard from the shortcut menu. When MFC Class Wizard appears, click Add Class.. and then From a type library... From the list of tlb files select imagewarp.tlb located in ImageWarp folder (typically C:\Program Files\ImageWarp). Confirm generating the new IImageWarp wrapper class. Click OK to close the Confirm Classes dialog box. Click OK to close the Wizard.

VC++ (.NET)

From the Project menu select Add Class. When Add Class Wizard appears, select MFC on the left and then MFC Class From TypeLib on the right. From the list of available type libraries select ImageWarp 1.0 Type Library. Confirm generating wrapper classes for both interfaces by clicking Add All Classes button, and then click Finish.


Adding variable for ImageWarp COM object

Add the following statements at the beginning of ComTestDlg.cpp module:

VC++ 6.0

#include
 "imageWarp.h"
IImageWarp IW;


VC++ 7.0 (.NET)

#include "CImageWarp.h"
CImageWarp IW;


Accessing ImageWarp server

Add the following code to the body of CComTestDlg::OnInitDialog:

// TODO: Add extra initialization here
AfxOleInit();
EnableAutomation();
CLSID clsid;
if
 (CLSIDFromProgID(OLESTR("imageWarp.Application"), &clsid) != NOERROR)
{
  AfxMessageBox("Unable to retrieve ImageWarp class ID");
  EndDialog(IDABORT);
  return
 FALSE;
}
if
 (!IW.CreateDispatch(clsid, NULL))
{
  AfxMessageBox("Unable to create ImageWarp object");
  EndDialog(IDABORT);
  return
 FALSE;
}



Using ImageWarp COM methods

In the Controls toolbox click on the Button icon and then draw a rectangular area on the program dialog. A button "Button1" will appear. Right click on the button and select Properties from the shortcut menu. In the Caption field type "Start" and double click on the button. The Add Member Function dialog box will appear. After clicking OK the source code window will be displayed with the new member function OnButton1 added. Insert the following statements to the function body:

VC++ 6.0

void CComTestDlg::OnButton1() 
{
   // TODO: Add your control notification handler code here

   IW.Show();
   IW.Call("do \n grabim(1) \n loop");   
}

VC++ 7.0

void CMyActiveDcamDlg::OnBnClickedButton1()
{
   // TODO: Add your control notification handler code here
   IW.Show();
   IW.Call("do \n grabim(1) \n sendMessage(0) \n sobel(1,2) \n loop");
}

This will bring ImageWarp on screen and activate continuous acquisition and processing when the button is clicked.


Adding event handling

The script passed as an argument to the Call method contains the sendMessage command. This command makes ImageWarp fire a Message event to the client application each time the new frame is acquired. The client application will handle this event to synchronize itself with ImageWarp.

Add the following code to the beginning of ComTestDlg.cpp module:

#include <afxctl.h>
DWORD m_dwCookie;
const
 IID DIID__IImageWarpEvents = {0x8D296025,0xD00A,0x4E8A,{0xBD,0xC4,0xB8,0xFC,0x99,0x5C,0x8F,0x16}};

BEGIN_DISPATCH_MAP(CComTestDlg, CDialog)
  DISP_FUNCTION_ID(CComTestDlg, "Message", 1, OnMessage,VT_EMPTY,VTS_I4)
END_DISPATCH_MAP()

BEGIN_INTERFACE_MAP(CComTestDlg, CDialog)
  INTERFACE_PART(CComTestDlg, DIID__IImageWarpEvents, Dispatch)
END_INTERFACE_MAP()


Insert the following statement at the end of CComTestDlg::OnInitDialog:

m_dwCookie=0;
BOOL bAdvised = AfxConnectionAdvise(IW.m_lpDispatch, DIID__IImageWarpEvents,  GetInterface(&IID_IUnknown), TRUE, &m_dwCookie);


Insert the following statement at the end of CComTestDlg::OnClose and CComTestDlg::OnCancel:

IW.Stop();
BOOL bUnadvised = AfxConnectionUnadvise(IW.m_lpDispatch, DIID__IImageWarpEvents, GetInterface(&IID_IUnknown), TRUE, m_dwCookie);


Add the following code to the declaration of the CComTestDlg class in the ComTestDlg.h header:

void OnMessage(long ID);
//{{AFX_MSG(CCppcomDlg)
.....
//}}AFX_MSG
DECLARE_DISPATCH_MAP()
DECLARE_INTERFACE_MAP()



Processing the Message event

In the Controls toolbox click on the Edit Box icon and draw a rectangular area on the program dialog. An edit box "Edit1" will appear.
Add the following code to ComTestDlg.cpp:

void CComTestDlg::OnMessage(long id)
{
  int iPix=(int)IW.GetImagePixel(1,128,140);
  SetDlgItemInt(IDC_EDIT1,iPix);   
}

This will report the value of the pixel at the coordinates x =128, y =140 of Image #1 each time the Message event is fired from ImageWarp.


Running the application

From the Build menu of the development environment select Execute ComTest.exe. This will build and run the application. The application dialog will appear on the screen. When you click the Start button, ImageWarp will appear and perform live acquisition and edge detection, while your application reports a pixel value in real time.

For more advanced programming refer to COM samples included in the ImageWarp setup.