Using pi2 from C++ code and other programming languages

Using full pi2 functionality

Pi2 functionality including distributed processing can be used from C, C++, and many other programming languages by using the pilib shared library/dynamic library. Examples of the process can be found in:

Basically, the shared library contains a few functions that you use to interact with the pi2 system.

First, you will need to initialize pi2 by calling

void* createPI();

that returns a handle that must be passed to all other function calls.

Pi2 commands can be run using

uint8_t run(void* pi, const char* commands);

The arguments are the handle returned by createPI() and arbitrary pi2 command string in the same format that is used in the stand-alone mode.

If the return value of run is zero, the execution of the commands failed. In this case the error message can be accessed using functions:

const char* lastErrorMessage(void* pi);
int32_t lastErrorLine(void* pi);

These return pointer to error message and line number causing the error, respectively. The error can be cleared with

void clearLastError(void* pi);

but it is not strictly necessary as new errors will overwrite old ones anyway.

Image data stored in the pi2 system can be accessed with

void* getImage(void* pi, const char* imgName, int64_t* width, int64_t* height, int64_t* depth, int32_t* dataType);

that returns a pointer to image data, given handle returned by createPI and the name of the image. The width, height, depth, and dataType parameters are filled by the function to image dimensions and data type.

After you are done, the pi2 system should be torn down by passing the handle to

void destroyPI(void* pi).

More information and API documentation can be found in source files

Using non-distributed functionality only as a C++ template library

For C++ developers pi2 offers also a (perhaps) simpler way to access the non-distributed processing functionality. The image processing algorithms are available as a mostly template-based C++ library in the itl2 folder of the source code. The library works in pretty simple imperative mindset:

#include "image.h"
#include "noise.h"
#include "io/itltiff.h"

using namespace itl2;

void main()
{
        // Create image whose pixel data type is 8-bit unsigned integer and dimensions are 100 x 100 x 100.
        Image<uint8_t> image(100, 100, 100);

        // Add noise to the image
        noise(image, 100, 25);

        // Write to .tiff file. The writed function adds .tif in the end of file name if the suffix is not there.
        itl2::tiff::writed(image, "./noise");
}

In some cases you need to link with libitl2/itl2.lib. Please see header files in itl2 folder and subfolders for API documentation.

Note: Using the library this way has not been thoroughly tested so you might encounter some problems!