File Exchange

image thumbnail


version 1.1 (163 KB) by

savepng is a fast PNG image compression routine that outperforms MatLab's built-in imwrite



savepng is a very efficient PNG image compression MEX-routine that outperforms MatLab's built-in imwrite in compression times and produced file sizes. For a typical figure size at the default compression level, savepng is ~5.8 times faster, with file size only 17% bigger than imwrite. At the highest compression level, savepng is still ~2.8 times faster, with produced file sizes 15% smaller.
PNG encoding is based on public-domain MINIZ library.


* CDATA -- is a standard MatLab image m-by-n-by-3 matrix. This matrix can be obtained using getframe command or, for a faster implementation, undocumented hardcopy command
* filename -- file name of the image to write. Don't forget to add .png to the file name.
* Compression -- Optional input argument. This argument takes on a number between 0 and 10 controlling the amount of compression to try to achieve with PNG file. 0 implies no compresson, fastest option. 10 implies the most amount of compression, slowest option. Default value is 4.

Up-to-date version, a more detailed help, bugs/issues tracking can all be found here:

Comments and Ratings (6)

Atnas, I've just pushed updates for alpha channel to GitHub.


Atnas (view profile)

Could you please implement support for alpha channels in the written png file?

Hi, I found this function very helpful. It compiled fine in windows, but I had to make 5 changes to make it compile under Linux.

First I changed savepng.c to savepng.cpp (to denote a C++ file).

Then I changed 4 lines of that file, relating to data type casting and return values.

Here are the final results:

     filename = (char *) mxMalloc(filenamelen);

     imgdata = (unsigned char *) mxMalloc(width * height * 3);

     outdata = (unsigned char * ) tdefl_write_image_to_png_file_in_memory_ex(imgdata, width, height, 3, dpm, &filelen, max_probes, MZ_FALSE);

     if(!file) return;

I have downloaded the savepng package and run benchmark_savepng on 2014a and it shows the following error message:

Error using savepng (line 45)
Sorry, auto-compilation failed.

Error in benchmark_savepng (line 66)

It seems the mex compile fails. Does someone has the same issue? If yes, how to correct it?


Thanks, Robert. I will fix this problem in the submission.

The compilation of the miniz mex file fails on MATLAB version (R2012b) Mac OS X 10.6.8:

>> benchmark_savepng
In file included from savepng.c:35:
miniz.c:2759: error: conflicting types for 'tdefl_write_image_to_png_file_in_memory'
miniz.c:794: error: previous declaration of 'tdefl_write_image_to_png_file_in_memory' was here
savepng.c: In function 'mexFunction':
savepng.c:68: warning: comparison is always false due to limited range of data type
savepng.c:68: warning: comparison is always false due to limited range of data type
savepng.c:120: warning: 'return' with a value, in function returning void

    mex: compile of ' "savepng.c"' failed.

Error using savepng (line 34)
Sorry, auto-compilation failed.

Changing line 794 in miniz.c from

void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out);


void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int max_probes, int num_chans, size_t *pLen_out);

solves the problem.



Added support for alpha channel


- fixed compiler errors, updated to latest miniz library, linked to GitHub

MATLAB Release
MATLAB 7.13 (R2011b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video