Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

One-button MATLAB-to-C Conversion


The Steps
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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.