Now we describe the steps for integrating the vertical image scaler algorithm into a DirectShow video application.
Step 1: Separate the algorithm from the testbench
MATLAB makes it easy to combine the algorithm and testbench into a single function or file. However, when integrating algorithms with other pieces of C-code, it's important to separate the algorithm from the testbench. This separation makes it easy to determine what the algorithm's inputs and outputs are. This also eases the task of integrating the algorithm into a larger application.
For the vertical scaler example, this means separating the actual scaling algorithm from the MATLAB code that reads in images and displays images. After this separation, we have a couple of M-code files:
- test_vscaler.m: load a single test image, invoke the vertical image scaler, and display the results
- vscalerrgb.m: the vertical image scaler function
Step 2: Define the types of the inputs
Now that the algorithm boundary is established, we define the types of the input variables. One of the powers of MATLAB is type polymorphism " the ability for MATLAB operations to have different semantics for different types. However, when integrating an algorithm developed in MATLAB into C-code, we must define a fixed-data-type interface to the C-code. In this process, the Catalytic MCS user defines the types of the input variables. Given this information, MCS automatically determines the types of the output variables. MCS provides a set of MATLAB functions that allow developers to define the types of the inputs. Users must define the dimensionality (scalar, row vector, column vector, 2D or 3D matrix, etc.) and basetype (real, complex, integer, logical, etc.) of a variable. If the specific sizes of the inputs are also known, they can also be defined, but this is not mandatory.
For the vertical scaler example, we defined the types of the following inputs:
- The input frames of RGB values imOrigR, imOrigG, and imOrigB. These were defined as integer 2D matrices.
- The vertical scaling factor, scaleFactor. This was defined as a scalar ranging from 0.0 to 1.0.
(Click to enlarge)
Figure 2. Interfaces to vscalerrgb M-code and C-code
Step 3: Generate C-code
Generating C-code with MCS is easy. Once the inputs are defined, it's simply a matter of applying MCS to the MATLAB code. After opening the top-level algorithm MATLAB file in the Catalytic GUI, just press the "C" button. The generated C-code appears in the right hand pane, as shown in Figure 3.
(Click to enlarge)
Figure 3. The Catalytic GUI.
With the Catalytic GUI, users can "cross-probe" between the original MATLAB code and generated C-code. That is, they can select a line of MATLAB and have the GUI highlight the corresponding C-code and vice versa. In order to improve code readability, MCS uses the original MATLAB variable names in the C code. In addition, MCS can optionally embed the original MATLAB code and comments into the C code.
Figure 4. MCS reuses original variable names and comments
When executed on vscaler.m, Catalytic MCS generates the following files:
- vscalerrgb.c and vscalerrgb.h: the C translation of vscalerrgb.m
- vscalerrgb_main.c: a test driver which invokes vscalerrgb()
Users have a great deal of control over the C-code generated by MCS. For example, MCS can insert runtime checks into the code to flag conditions such as array overruns and division-by-zero. In the interest of speed, these checks can be disabled.