17.2   Control Parameters

HALCON/C supports the following data types as types for control parameters of HALCON operators:

17.2.1   String Encoding

Regardless of the encoding of the HALCON library (set_system('filename_encoding', ...)) the HALCON/C interface expects raw char pointer strings that are passed to HALCON operators and to Htuple instances to be UTF-8 encoded. The encoding of the HALCON/C interface (interface encoding) can be changed to local-8-bit encoding via a call of SetHcInterfaceStringEncodingIsUtf8(false). Note that the encoding must not be changed when using HDevEngine. The current interface encoding can be requested via IsHcInterfaceStringEncodingUtf8(). It is not recommended to switch the interface encoding back and forth. The setting should be adjusted only once at the very beginning of the program (before the first HALCON operator or assignment), because the Htuple structure can not store in which encoding the contained strings are present, i.e, for all write and read accesses, the same encoding must be set. Furthermore, the interface encoding is set globally and is therefore not suitable for multithreading programs: Changing the setting in one thread has an effect on other threads. Note also, that UTF-8 encoded strings may take more memory space than strings in local-8-bit-encoding. This must be considered when providing the buffer for strings returned by HALCON operators. As long as the maximum length of the strings returned is not known for sure, it is recommended to prefer operator calls in tuple mode over simple mode.

If the string encoding needs to be converted without changing the interface encoding, the following functions for converting wchar_t* strings and char* strings to or from tuples are provided:


UTF-8 version local 8-bit version wchar_t* version (Windows only)

create_tuple_s_from_utf8 create_tuple_s_from_local8bit create_tuple_s_from_wcs

reuse_tuple_s_from_utf8 reuse_tuple_s_from_local8bit reuse_tuple_s_from_wcs

init_s_from_utf8 init_s_from_local8bit init_s_from_wcs

set_s_from_utf8 set_s_from_local8bit set_s_from_wcs

get_s_to_utf8 get_s_to_local8bit get_s_to_wcs

Table 17.1: Encoding helper functions.

When converted to a different encoding, a string may need a buffer of bigger size. To check on whether you have to allocate more memory for your string you can use the get_s_to_*-functions mentioned in the table above (see also figure 17.7). The functions transcode a string to the respective encoding and write it into the buffer provided by the user. If the returned size of the string is greater or equal to the provided buffer, the passed buffer size was too small.

17.2.2   Control Parameter Tuples

Using control parameter tuples in C isn't as elegant as using image object tuples. To circumvent the missing generic lists in C, it was necessary to introduce two different working modes into HALCON/C: The simple mode and the tuple mode. If a tuple is necessary for at least one control parameter, the tuple mode has to be used for operator calls. In tuple mode, all control parameters of an operator must be passed as type Htuple (Mixing of the two modes is not possible). The tuple mode also has to be used if the number or type of values that a operator calculates isn't known beforehand.

Mentioning the control parameter types — How is the default type of control parameters determined for a given operator? Basically there are three ways:

  1. The operator description in the HALCON reference manual,
  2. the HALCON system operator get_param_info and
  3. the description of the HALCON interface in the file HProto.h.

Sometimes the manuals mention more than one possible type. If only integers and floating point numbers are allowed for a parameter, values have to be passed as parameters of type double. For all other combinations of types, the tuple mode has to be used.

HALCON operators, that are called in tuple mode are distinguished from simple mode calls by a preceding T_. That means,

   select_shape

is a call of the HALCON operator select_shape (as described in the HALCON reference manual) in simple mode, whereas

   T_select_shape

is a call of the same operator in tuple mode.

17.2.3   The Simple Mode

In the so-called simple mode, all control parameters of operator calls are variables (or constants) of the data types

Hlong and double input control parameters are passed by value as usual, the corresponding output control parameters are passed by reference, using the &-operator. String parameters are pointers to char in both cases. Note that the memory for output control parameters (in particular strings) has to be provided by the caller! We recommend allocating memory for at least 1024 characters for string parameters of unknown length. Output parameter values that are of no further interest can be set to NULL.

Examples for HALCON operator calls in simple mode can be found in the C programs in figure 17.1 and figure 16.1.

Operators with parameters resembling handles must always be called in the tuple mode as described in the following section. The simple mode for these operators is only available in legacy handle mode (see set_system).

17.2.4   The Tuple Mode

We mentioned already that control parameter tuples for HALCON operators need special treatment. In this chapter we will give the details on how to construct and use those tuples. The HALCON reference manual describes a large number of operators that don't operate on single control values but on tuples of values. Using those operators, it is easy to write very compact and efficient programs, because often it is possible to combine multiple similar operator calls into a single call.

Unfortunately, C provides no generic tuple or list constructor. In contrast, HALCON allows tuples with mixed types as control parameter values (e.g., integers mixed with floating point numbers).

Therefore, in addition to the very intuitive simple mode there is another mode in HALCON/C: the tuple mode. Using this mode is a little more elaborate. If at least one of the control parameters of a HALCON operator is passed as a tuple, the tuple mode has to be used for all control parameters (Mixing of both modes isn't possible). Furthermore, the tuple mode also has to be used if the number or type of the calculated values are unknown beforehand.

Syntactically, tuple mode is distinguished from simple mode by a T_ preceding the operator name. For example, calling disp_circle in tuple mode is done by

T_disp_circle(... ).

To ease the usage of the tuple mode, HALCON/C provides the abstract data type Htuple for control parameter tuples. Objects of type Htuple may be constructed using arrays of the types

Additionally, a MIXED_PAR array type is supported that can hold an array with any of the three native value types in arbitrary combination. The usage of these four array types is transparent.

Control parameter tuples must be created, deleted, and manipulated using the appropriate HALCON/C procedures only (overview in figures 17.2, figure 17.3, figure 17.4 and figure 17.5).

The rules for parameter passing are valid in tuple mode, too: Input control parameters (type Htuple) are passed by value as usual, output control parameters are passed by reference, using the &-operator. For output parameters that are of no further interest you can pass NULL.

The following sections describe the five most important steps when calling a HALCON operator in tuple mode:

Section “Example for the Tuple Mode” contains an example.

Finally, section “Generic Calling Mechanism” describes a generic calling mechanism that can be used in interpreters or graphical user interfaces.

17.2.4.1   Allocate Memory

First, memory must be allocated for all tuples of input control parameters, using create_tuple or create_tuple_type, respectively (see figures 17.2). Memory for output control parameter tuples is allocated by HALCON/C (a call of create_tuple isn't necessary). With create_tuple_i etc. you can create a tuple of length 1 and set its value in a single step (see figures 17.3). With reuse_tuple_i etc. you can reuse an existing tuple, i.e., destroy and reallocate it and set a single value (see figures 17.4).

  void create_tuple(tuple,length)     or     macro CT(tuple,length)
       Htuple    *tuple;
       Hlong      length;
       /* creates a MIXED_PAR tuple that can hold 'length' entries h  */

  void create_tuple_type(tuple,length,type)
       Htuple    *tuple;
       Hlong      length;
       Hlong      type;
       /* creates a tuple of 'type' that can hold 'length' entries.
        * 'type' can hold either LONG_PAR, DOUBLE_PAR, STRING_PAR,
        * HANDLE_PAR, or MIXED_PAR.                                   */

  void destroy_tuple(tuple)           or     macro DT(tuple)
       Htuple    tuple;
       /* deletes a tuple (if the tuple contains string entries,      */
       /* the memory allocated by the strings is freed, too)          */

  Hlong length_tuple(tuple)            or     macro LT(tuple)
       Htuple    tuple;
       /* returns the length of a tuple (number of entries)           */

  void copy_tuple(input,output)      or     macro CPT(input,output)
       Htuple    input;
       Htuple    *output;
       /* creates a tuple and copies the entries of the input tuple   */

  void resize_tuple(htuple,new_length) or   macro RT(htuple,new_length)
       Htuple    *htuple;
       Hlong      new_length;
       /* creates a tuple with the new size and copies the previous   */
       /* entries                                                     */

Figure 17.2: HALCON/C Htuple procedures (part one).

  void create_tuple_i(tuple,value)
       Htuple    *tuple;
       Hlong      val;
       /* creates a tuple with specified integer value                */

  void create_tuple_d(tuple,value)
       Htuple    *tuple;
       double    val;
       /* creates a tuple with specified double value                 */

  void create_tuple_h(tuple,value)
       Htuple    *tuple;
       Hphandle  val;
       /* creates a tuple with specified handle value                 */

  void create_tuple_s(tuple,value)
       Htuple    *tuple;
       char      *val;
       /* creates a tuple with specified string value                 */

  void create_tuple_s_from_local8bit(tuple,value)
       Htuple    *tuple;
       char      *val;
       /* creates a tuple with specified string value converted       */
       /* from local-8-bit encoding                                   */

  void create_tuple_s_from_utf8(tuple,value)
       Htuple    *tuple;
       char      *val;
       /* creates a tuple with specified string value converted       */
       /* from UTF-8 encoding                                         */

  void create_tuple_s_from_wcs(tuple,value)
       Htuple    *tuple;
       wchar_t   *val;
       /* creates a tuple with specified string value converted       */
       /* from wide-character encoding                                */

Figure 17.3: HALCON/C Htuple procedures (part two).

  void reuse_tuple_i(tuple,val)
       Htuple    *tuple;
       Hlong      val;
       /* reuses a tuple with specified integer value                 */

  void reuse_tuple_d(tuple,val)
       Htuple    *tuple;
       double    val;
       /* reuses a tuple with specified double value                  */

  void reuse_tuple_h(tuple,val)
       Htuple    *tuple;
       Hphandle  val;
       /* reuses a tuple with specified handle value                  */

  void reuse_tuple_s(tuple,val)
       Htuple    *tuple;
       char      *val;
       /* reuses a tuple with specified string value                  */

  void reuse_tuple_s_from_local8bit(tuple,val)
       Htuple    *tuple;
       char      *val;
       /* reuses a tuple with specified string value converted        */
       /* from local-8-bit encoding                                   */

  void reuse_tuple_s_from_utf8(tuple,val)
       Htuple    *tuple;
       char      *val;
       /* reuses a tuple with specified string value converted        */
       /* from UTF-8 encoding                                         */

  void reuse_tuple_s_from_wcs(tuple,val)
       Htuple    *tuple;
       wchar_t   *val;
       /* reuses a tuple with specified string value converted        */
       /* from wide-character encoding                                */

Figure 17.4: HALCON/C Htuple procedures (part three).

17.2.4.2   Create Input Parameters

You set tuple elements using the appropriate procedures set_*. set_s, which insert a string into a tuple, allocates the needed memory by itself, and then copies the string (see figure 17.5).

17.2.4.3   Call Operator

Then, the HALCON operator is actually called. The operator name is (as already explained) preceded by a T_ to denote tuple mode.

17.2.4.4   Process Output Parameters

Further processing of the output parameter tuples takes place, using the procedures length_tuple, get_type (see figure 17.2) and get_* (see figure 17.6 and figure 17.7). When processing strings (using get_s), please note that the allocated memory is freed automatically upon deleting the tuple with destroy_tuple. If the string has to be processed even after the deletion of the tuple, the whole string must be copied first. Also note that output handles might be freed when deleting the last tuple. If a handle should be used further, it should be kept in a tuple.


  void set_i(tuple,val,index)                           or     macro SI(tuple,val,index)
       Htuple     tuple;
       Hlong      val;
       Hlong      index;
       /* inserts an integer with value 'val' into a tuple at         */
       /* position 'index' ('index' in [0,length_tuple(tuple) - 1])   */

  void set_d(tuple,val,index)                           or     macro SD(tuple,val,index)
       Htuple     tuple;
       double     val;
       Hlong      index;
       /* inserts a double with value 'val' into a tuple at           */
       /* position 'index' ('index' in [0,length_tuple(tuple) - 1])   */

  void set_h(tuple,val,index)                           or     macro SH(tuple,val,index)
       Htuple     tuple;
       Hphandle   val;
       Hlong      index;
       /* inserts a handle with value 'val' into a tuple at           */
       /* position 'index' ('index' in [0,length_tuple(tuple) - 1])   */

  void set_s(tuple,val,index)                           or     macro SS(tuple,val,index)
       Htuple     tuple;
       char       *val;
       Hlong      index;
       /* inserts a copy of string 'val' into a tuple at position     */
       /* 'index' ('index' in [0,length_tuple(tuple) - 1]);           */
       /* memory necessary for the string is allocated by set_s       */

  void set_s_from_local8bit(tuple,val,index)            or     macro SS_LOC(tuple,val,index)
       Htuple     tuple;
       char       *val;
       Hlong      index;
       /* inserts a copy of string 'val', converted from              */
       /* local-8-bit encoding, into a tuple at position 'index'      */
       /* ('index' in [0,length_tuple(tuple) - 1]).                   */

  void set_s_from_utf8(tuple,val,index)                 or     macro SS_U(tuple,val,index)
       Htuple     tuple;
       char       *val;
       Hlong      index;
       /* inserts a copy of string 'val', converted from UTF-8        */
       /* encoding, into a tuple at position 'index' ('index' in      */
       /* [0,length_tuple(tuple) - 1]).                               */

  void set_s_from_wcs(tuple,val,index)                  or     macro SS_W(tuple,val,index)
       Htuple     tuple;
       wchar_t    *val;
       Hlong      index;
       /* inserts a copy of string 'val', converted from              */
       /* wide-character encoding, into a tuple at position           */
       /* 'index' ('index' in [0,length_tuple(tuple) - 1]).           */

Figure 17.5: HALCON/C Htuple procedures (part four).


  int get_type(tuple,index)                             or     macro GT(tuple,index)
       Htuple     tuple;
       Hlong      index;
       /* returns type of value at position 'index' in the tuple.     */
       /* Possible values: LONG_PAR, DOUBLE_PAR or STRING_PAR         */

  Hlong get_i(tuple,index)                              or     macro GI(tuple,index)
       Htuple     tuple;
       Hlong      index;
       /* returns the integer at position 'index' in the tuple        */
       /* (a type error results in a run time error)                  */

  double get_d(tuple,index)                             or     macro GD(tuple,index)
          Htuple     tuple;
          Hlong      index;
       /* returns the floating point number at position 'index' in    */
       /* the tuple. (a type error results in a run time error)       */

  Hphandle get_h(tuple,index)                           or     macro GH(tuple,index)
          Htuple     tuple;
          Hlong      index;
       /* returns the handle at position 'index' in the               */
       /* tuple. (a type error results in a run time error)           */

  char *get_s(tuple,index)                              or     macro GS(tuple,index)
        Htuple     tuple;
        Hlong      index;
       /* returns the pointer(!) to the string at position 'index'    */
       /* in the tuple. (a type error results in a run time error)    */
       /* Attention: indices must be in [0,length_tuple(tuple) - 1]   */

Figure 17.6: HALCON/C Htuple procedures (part five).


  Hlong get_s_to_local8bit(buffer,size,tuple,index)   or     macro GS_LOC(tuple,index)
        char*      buffer;
        Hlong      size;
        Htuple     tuple;
        Hlong      index;
       /* Copies the (local-8-bit encoded) string at position         */
       /* 'index' into 'buffer'. 'size' determines the size of the    */
       /* buffer. The return value determines the size of the         */
       /* string, that would be written. If the returned length of    */
       /* the string is greater or equal to the provided              */
       /* buffer, the buffer was too small.                           */
       /* Attention: indices must be in [0,length_tuple(tuple) - 1]   */

  Hlong get_s_to_utf8(buffer,size,tuple,index)        or     macro GS_U(tuple,index)
        char*      buffer;
        Hlong      size;
        Htuple     tuple;
        Hlong      index;
       /* Copies the (UTF-8) string at position                       */
       /* 'index' into 'buffer'. 'size' determines the size of the    */
       /* buffer. The return value determines the size of the         */
       /* string, that would be written. If the returned length of    */
       /* the string is greater or equal to the provided              */
       /* buffer, the buffer was too small.                           */
       /* Attention: indices must be in [0,length_tuple(tuple) - 1]   */

  Hlong *get_s_to_wcs(buffer,size,tuple,index)        or     macro GS_W(tuple,index)
        wchar_t*   buffer;
        Hlong      size;
        Htuple     tuple;
        Hlong      index;
       /* Copies the (wide-character) string at position              */
       /* 'index' into 'buffer'. 'size' determines the size of the    */
       /* buffer. The return value determines the size of the         */
       /* string, that would be written. If the returned length of    */
       /* the string is greater or equal to the provided              */
       /* buffer, the buffer was too small.                           */
       /* Attention: indices must be in [0,length_tuple(tuple) - 1]   */

Figure 17.7: HALCON/C Htuple procedures (part six).

17.2.4.5   Free Memory

Finally the memory allocated by all the tuples (input and output) has to be freed again. This is done with destroy_tuple. If you still need the values of the tuple variables, remember to copy them first. Now, the whole series can start again — using different or the same tuple variables.

17.2.4.6   Example for the Tuple Mode

An example for the tuple mode can be found in figure 17.8 or the file example3.c): The aim is to obtain information about the current HALCON system state. The operator get_system('?',Values) (here in HDevelop syntax) returns all system flags with their current values. Since in our case neither number nor type of the output parameters is known beforehand, we have to use tuple mode for the actual operator call in HALCON/C. The rest of the program should be self explanatory.

#include <stdio.h>

main ()
{
  Htuple In, SysFlags, Info;
  Hlong  i, num;

  printf("System information:\n");
  /* prepare query */
  create_tuple(&In, 1);
  /* "?" = list of all informations */
  set_s(In, "?", 0);
  T_get_system(In, &SysFlags);
  destroy_tuple(In);
  num = length_tuple(SysFlags);
  for (i = 0; i < num; i++)
  {
    create_tuple(&In, 1);
    set_s(In, get_s(SysFlags, i), 0);
    printf("%s ", get_s(SysFlags, i));
    T_get_system(In, &Info);
    destroy_tuple(In);
    if (length_tuple(Info) > 0)
    {
      switch (get_type(Info, 0))
      {
      case INT_PAR:
        printf("(Hlong):  %" LONG_FORMAT "d\n", get_i(Info, 0));
        break;
      case DOUBLE_PAR:
        printf("(double):  %f\n", get_d(Info, 0));
        break;
      case STRING_PAR:
        printf("(string):  %s\n", get_s(Info, 0));
        break;
      case HANDLE_PAR:
        printf("(handle):  %" LONG_FORMAT "d\n", (Hlong)get_h(Info, 0));
        break;
      }
    }
    else
    {
      printf("(--):  no data\n");
    }
    destroy_tuple(Info);
  }
}

Figure 17.8: Tuple mode example program: Printing the current HALCON system state.

17.2.4.7   Generic Calling Mechanism

There is also an alternative generic calling mechanism for HALCON operators in tuple mode. This mechanism is intended for the use in interpreters or graphical user interfaces:

T_call_halcon_by_id(id, ...)

calls the HALCON operator id in tuple mode, passing input parameters and getting the output parameters (see figure 17.9 for the complete signature). The id of an operator can be requested with get_operator_id.

  /* generic HALCON operator call style:
   * - the operator is called by an id that is returned by get_operator_id;
   *    attention: this id may differ for different HALCON versions
   * - the tuple arrays are passed directly to the call -> this method is
   *   thread-safe
   *-----------------------------------------------------------------------*/
  int    get_operator_id(const char* name);

  Herror T_call_halcon_by_id(int id,
                             const Hobject in_objs[],
                             Hobject out_objs[],
                             const Htuple in_ctrls[],
                             Htuple out_ctrls[]);

Figure 17.9: Generic calling mechanism for the HALCON/C tuple mode.