I have struggled with optimizing the process of animating PNG files. I have code that generates a PNG image based on an array of data, which, in my case is the intensity of an optical beam, but it can be anything. I have found various options ranging from quick-and-dirty to fairly robust. MEncoder plays a role in several of them, and it was no easy feat to find a combination of flags that properly encode the video for playback on linux and Mac platforms. Here, I present my two most promising options so far.
To clarify the usage, the commands quoted are what I used on a linux machine, which where I run C++ code to generate the images. I want to view movies on both linux and mac platforms (especially since I give presentations on my mac). The viewing is done with either mplayer (linux) or quicktime player (mac).
To continue the series on using C++ to replace MATLAB, here are some details about using the pngwriter library. Included below is a function that I use in various places to write a 2D array to a png file. This can be left in a header somewhere and used in a similar way to MATLAB’s
I’d be happy to post more details if anyone is interested, and I’ll try to keep up with posts about the process I followed to port from MATLAB to C++.
I have had some success with converting my MATLAB code to C++. There are some caveats, from array indexing (minor), to missing functions (less trivial to fix). In case people are looking for solutions to these problems, I’ve posted some sample code.
To motivate the conversion a little bit more, my code spends 99% of its time computing FFTs. I initially found that FFT benchmarks in MATLAB were comparable to C++, which can be explained by the fact that MATLAB calls FFTW libraries which are well optimized. However, I wasn’t using a well-optimized array implementation so the real improvement came when I found John Bowman’s fftw++ headers and Array class. In the end, re-writing my code with these data structures results in a 2-4x speedup over MATLAB. The additional advantage is that a portion of my simulation is embarrassingly parallel so C++ makes it easy to exploit that.
Looking through some google results after searching for tips on porting code from matlab to c/c++ I came across the following response on one forum:
Its not a good idea to use .m to C conversion (using mcc) for any commercial application. Codes produce in this way are far from optimized and are slow to run. Most of the time its used for research in Universities where a grad student is too lazy to do it and uses matlab instead. Infact when your code become complex conversion success is limited.
Wow, they sure tell it like it is. To give myself a little credit, it wasn’t laziness, it was a need for quick and easy visualization. Now, between good old gnuplot and the pngwriter c++ libraries, I have that all sorted out.