One of the reasons ArrayFire has become so useful is attributed to it integrating various performance oriented function from many domains with utility functions such as image loading and visualization.

In a lot of cases, developers write a ton of code with multiple iterations but do not have a way to check the quality of the results. That's where ArrayFire can be used very well.

In one of our recent projects that involved image processing, we did a lot of development but faced a speed bump when we had to make sure the quality is not compromised. As a part of making code faster, we do tend to take acceptable detours. Hence it becomes absolutely crucial that we test code from time to time and especially before commits. In this particular case, it was an OpenGL project without any ArrayFire or CUDA/OpenCL as a part of it. Anything that we would do with ArrayFire would be kept separate from the production code. We needed code that would compare and analyse the result from our development branch against a "Gold" data set generated by the code received by us.

The first option to do this was the old fashioned way by reading two images and comparing them on CPU. But given that we needed to compare images (in the 100s), this was not a viable option. It would simply be too slow.

The preferred option was to use ArrayFire but keep it separate from the production code. So we decided to go this route.

The first thing we did was to write our testing code. A simple function that would read images from the respective folders and then perform various comparisons on then. This is where ArrayFire excels. Just a few lines of code and everything is set for loading and comparing images. Some of the function we used included subtraction, min, mean, max, stats etc. We also included visualization using fig if the user chose to visualize the differences.
<pre>std::string goldPath = std::string(gold)      + path;
std::string testPath = std::string("output/") + path;

af::array goldImage = af::loadimage(goldPath.c_str(), true) / 255.0f;
af::array testImage = af::loadimage(testPath.c_str(), true) / 255.0f;

af::array diffImage = af::abs(goldImage - testImage);

float mean = af::mean<float>(diffImage);

if(mean &gt; 0.001f) {
    fprintf ( stderr, "FOUND MISMATCH\n" ) ;
    result = false;
    printf("!!!***FAILED***!!!: %s\n", path);
    stats(diffImage);

    if(visualize) {
        // colormap
        af::fig("color", "default");

        // image operations
        af::fig("sub", 3, 1, 1); af::image(goldImage); af::fig("title", "GOLD");
        af::fig("sub", 3, 1, 2); af::image(testImage); af::fig("title", "TEST");
        af::fig("sub", 3, 1, 3); af::image(diffImage); af::fig("title", "DIFF");

        // Force screen refresh (optional)
        af::fig("draw");
        getchar();
    }
}</pre>
Once the code was in place, we had to integrate it into the main function of the code. Again, it was required that this must not mess with the production code. So we put two lines in the main function. The first line includes the file containing the test function and the second line calls the test function. Both these lines were wrapped in a #ifdef TEST which would be defined in the makefile. That way, if TEST is not defined, the file isn't included and ArrayFire is not required for compilation and running of the code. For all of this, we created a makefile goal called "test" which would define the flag, include and link ArrayFire.