refactored mat <-> image conversion to static helper