Get from Ico-github-logo

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

» Watch video

Highlights from
savepng

  • savepng(CDATA,filename,va...
    SAVEPNG
  • benchmark_savepng.m
    % Benchmark savepng with reference to built-in imwrite and
  • View all files

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

4.0
4.0 | 2 ratings Rate this file 14 Downloads (last 30 days) File Size: 163 KB File ID: #40384 Version: 1.1
image thumbnail

savepng

by

 

19 Feb 2013 (Updated )

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

| Watch this File

File Information
Description

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.
Usage
savepng(CDATA,filename[,Compression])

Where,

* 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:
https://github.com/stefslon/savepng

Required Products MATLAB
MATLAB release MATLAB 7.13 (R2011b)
Other requirements C compiler
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (6)
25 Nov 2016 Stefan Slonevskiy

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

Comment only
27 Oct 2016 Atnas

Atnas (view profile)

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

Comment only
08 Jan 2015 Chethan Pandarinath

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:

savepng.cpp:103:
filename = (char *) mxMalloc(filenamelen);

savepng.cpp:114:
imgdata = (unsigned char *) mxMalloc(width * height * 3);

savepng.cpp:129:
outdata = (unsigned char * ) tdefl_write_image_to_png_file_in_memory_ex(imgdata, width, height, 3, dpm, &filelen, max_probes, MZ_FALSE);

savepng.cpp:133:
if(!file) return;

22 Oct 2014 Lianyuan Zheng

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)
savepng(img.cdata,'example3.png',C(iC));

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

Thanks,
Lianyuan

Comment only
20 Nov 2013 Stefan Slonevskiy

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

Comment only
20 Nov 2013 Robert Oostenveld

The compilation of the miniz mex file fails on MATLAB version 8.0.0.783 (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);

into

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.

Updates
17 Aug 2015 1.1

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

26 Nov 2016 1.1

Added support for alpha channel

Contact us