Using DDE  
Top  Previous  Next

ImageWarp's DDE mechanism allows you to write your own application while taking advantage of ImageWarp's image processing and analysis functionality. Typically a client application would use DDE to instruct ImageWarp to perform a series of image processing and analysis command and then request the results from ImageWarp. During this process both the client application and ImageWarp must be synchronized.

The client application can instruct ImageWarp to load a desired script file and execute it by issuing the Load and Run DDE commands. The client then has to wait for the script to end. This can be done by continuously sending the Status requests to ImageWarp until a return value becomes "IDLE". As soon as it happens, the client can request the desired data through the SelectVariable, SelectParam, and SelectCell commands and connecting to the corresponding items.

As an alternative to sending continuous Status requests to ImageWarp, the client application can establish a hot link with the Status item by calling the DDEAdvise function. ImageWarp will send the client application an Advise notification as soon as the script terminates or pauses. This method involves some extra programming, but it is more efficient, since ImageWarp does not have to process continuous Status requests from the client during the execution of the script.

If the application requires multiple data transfers during the script execution or if it is using a cyclic script, the pause functions can be used throughout the script to notify the client and make it initiate the data transfer. Once the client receives and process the data, it should issue the Run command to have ImageWarp continue the script execution.

If the client application needs to exchanges images with ImageWarp, it can be done via the Clipboard by using the copyIm and pasteIm functions in the script.

The client application can minimize ImageWarp's main window or make it completely invisible by issuing the Minimize or Hide commands.

The following example demonstrates a simple console application written in C. The application utilizes ImageWarp to capture an image, perform segmentation and measure an total area of objects of interests.

#include "stdafx.h"
#include <windows.h>
#include <ddeml.h>
#include <string.h>

int main(int argc, char* argv[])
{
   unsigned long idInst = 0, idInstServ=0,m_dwResult=0;
   //DDE string handles
   HSZ m_hszApp,m_hszTopic,m_hszStatus,m_hszCommand; 
   HCONV m_hConv;        //DDE conversation handle
   HDDEDATA m_hBuffer=0; //DDE data handle 

  m_dwResult=0; 

  char*  buffer;
  int i=0;
   
   //custom script
   char* command="\n\
   closeAll()\n\
   setColorSpace(CS_DEFAULT)\n\
   grabIm(1)\n\
   wait(1500)\n\
   threshUnimod(1,2,0,1)\n\
   wait(1500)\n\
   scrap(2,3,0,40,M_REMOVE)\n\
   fill(3,3,6)\n\
   border(3,3,6)\n\
   selectParam(\"Area\",\"Perim\")\n\
   measfields(3,1,0)\n\
   ";   
  
  printf("Initializing DDE\n");
  
   if(DdeInitialize(&idInst,(PFNCALLBACK)NULL, APPCMD_CLIENTONLY, 0L))
  {
      printf("Initialization failed\n");
      return FALSE;
  }
  
   //create string atoms to connect to system topic
  m_hszApp    = DdeCreateStringHandle(idInst, "ImageWarp", CP_WINANSI);
  m_hszTopic = DdeCreateStringHandle(idInst, "Control", CP_WINANSI);
  
  //connect to system topic
   printf("Connecting to ImageWarp\n\n");
  m_hConv = DdeConnect(idInst, m_hszApp, m_hszTopic, NULL);
  if(!m_hConv) 
   {printf("Could not establish conversation with ImageWarp\n");return 0;}
 
   //check ImageWarp status
   m_hszStatus   = DdeCreateStringHandle(idInst,"Status", CP_WINANSI);
   m_hBuffer = DdeClientTransaction(NULL,0,m_hConv,m_hszStatus,CF_TEXT,XTYP_REQUEST, 10000,&m_dwResult);
   buffer = (char *)DdeAccessData(m_hBuffer,&m_dwResult);
  printf ("Current ImageWarp status: %s\nInitiating Call command\n",buffer);
   DdeFreeDataHandle(m_hBuffer);  
   
   m_hszCommand   = DdeCreateStringHandle(idInst,"Command", CP_WINANSI);
   m_hBuffer = DdeClientTransaction((unsigned char*)command,strlen(command)+1,m_hConv,m_hszCommand,CF_TEXT,XTYP_POKE, 10000,&m_dwResult);
   if(!m_hBuffer) 
    {printf("Call command failed\n");return 0;}
   printf("Waiting for ImageWarp to process the script\n");
   
   //sending ImageWarp continuous status requests 
   BOOL cmp;
   do
   {
    Sleep(10);
    m_hBuffer = DdeClientTransaction(NULL,0,m_hConv,m_hszStatus,CF_TEXT,XTYP_REQUEST, 10000,&m_dwResult);
   buffer = (char *)DdeAccessData(m_hBuffer,&m_dwResult);
    cmp=strcmp(buffer,"RUNNING");
    DdeFreeDataHandle(m_hBuffer);
   }
   while (cmp);
   //script started

   do
   {
    Sleep(10);
    m_hBuffer = DdeClientTransaction(NULL,0,m_hConv,m_hszStatus,CF_TEXT,XTYP_REQUEST, 10000,&m_dwResult);
   buffer = (char *)DdeAccessData(m_hBuffer,&m_dwResult);
    cmp=strcmp(buffer,"IDLE");
    DdeFreeDataHandle(m_hBuffer);
   }
   while (cmp);
  //script ended
   
   Beep(1000,300);
   printf("Processing finished\n\n");
   
   //retreiving parameter's value
   LPSTR getpar="[SelectParam(\"area\")]";
   m_hBuffer=DdeClientTransaction((LPBYTE)(getpar),strlen(getpar)+1,m_hConv,NULL,CF_TEXT,XTYP_EXECUTE, 10000,&m_dwResult);
   if(!m_hBuffer) 
   {printf("SelectParam command failed\n");return 0;}
  buffer = (char *)DdeAccessData(m_hBuffer,&m_dwResult);
   DdeFreeDataHandle(m_hBuffer);

  m_hszStatus   = DdeCreateStringHandle(idInst,"Parameter", CP_WINANSI);
   m_hBuffer = DdeClientTransaction(NULL,0,m_hConv,m_hszStatus,CF_TEXT,XTYP_REQUEST, 10000,&m_dwResult);
   if(!m_hBuffer) 
   {printf("Failed to read parameter's value\n");return 0;}
   buffer = (char *)DdeAccessData(m_hBuffer,&m_dwResult);
  printf ("Area=%s pixels\n\n",buffer);
   DdeFreeDataHandle(m_hBuffer);  

   return 0;
}

The following example shows a simple VB application that uses ImageWarp's DDE functionality. Clicking the first text box cycles through ImageWarp DDE items. Clickin the second text box displays and executes the custom script located in a file "test.prg". Clicking the third text box retrievs and displays the area of the second object in ImageWarp's Global Grid.

Dim ctr As Integer

Private Sub Form_Load()
Dim AppID
If Text1.LinkMode = 0 Then
Text1.LinkTopic = "IMAGEWARP|SYSTEM"
Text1.LinkItem = "TOPICS"
Text1.LinkMode = 1
End If
End Sub

Private Sub Text1_Click()
Select Case ctr
Case 1
Text1.LinkItem = "Formats"
Case 2
Text1.LinkItem = "Topics"
Case 3
Text1.LinkItem = "SysItems"
Case 4
Text1.LinkItem = "Protocols"
Case 5
Text1.LinkItem = "Help"
End Select
ctr = ctr + 1
If ctr > 5 Then ctr = 1
End Sub

Private Sub Text2_Click()
Dim script As String
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("c:\test.prg")
script = f.ReadAll
Text2.LinkTopic = "IMAGEWARP|CONTROL"
Text2.LinkItem = "COMMAND"
Text2.LinkMode = vbLinkManual
Text2.Text = script
Text2.LinkPoke
End Sub

Private Sub Text3_Click()
Text3.LinkTopic = "IMAGEWARP|CONTROL"
Text3.LinkItem = "STATUS"
Text3.LinkMode = 1
Text3.LinkExecute ("[selectCell(2,""Area"")]")
Text3.LinkItem = "GRIDCELL"
End Sub