diff options
author | don bright <hugh.m.bright@gmail.com> | 2013-02-12 01:10:52 (GMT) |
---|---|---|
committer | don bright <hugh.m.bright@gmail.com> | 2013-02-12 01:10:52 (GMT) |
commit | 76f184c50b73e7006800235b78c83b01fa399637 (patch) | |
tree | 9b699e41a7c37473e5e7fc80bddd4e3b977967d8 /src/imageutils-lodepng.cc | |
parent | a4fea6df86d81d305a9cf1608b2699e383a5a7ab (diff) |
upgrade lodepng to new version. remove malloc from imageutils
Diffstat (limited to 'src/imageutils-lodepng.cc')
-rw-r--r-- | src/imageutils-lodepng.cc | 27 |
1 files changed, 9 insertions, 18 deletions
diff --git a/src/imageutils-lodepng.cc b/src/imageutils-lodepng.cc index a034702..cd104d6 100644 --- a/src/imageutils-lodepng.cc +++ b/src/imageutils-lodepng.cc @@ -2,25 +2,16 @@ #include "lodepng.h" #include <stdio.h> #include <stdlib.h> +#include <vector> +#include <iterator> +#include <algorithm> bool write_png(std::ostream &output, unsigned char *pixels, int width, int height) { - //encoder.settings.zlibsettings.windowSize = 2048; - //LodePNG_Text_add(&encoder.infoPng.text, "Comment", "Created with LodePNG"); - size_t dataout_size = -1; - bool result = false; - unsigned char *dataout = (unsigned char *)malloc(width*height*4); - if (!dataout) { - perror("Error allocating memory while writing png\n"); - return result; - } - LodePNG_encode(&dataout, &dataout_size, pixels, width, height, LCT_RGBA, 8); - try { - output.write( reinterpret_cast<const char*>(dataout), dataout_size ); - result = true; - } catch (const std::ios_base::failure &e) { - std::cerr << "Error writing to ostream:" << e.what() << "\n"; - } - free( dataout ); - return result; + std::vector<unsigned char> dataout; + unsigned err = lodepng::encode(dataout, pixels, width, height, LCT_RGBA, 8); + if ( err ) return false; + output.write( reinterpret_cast<const char *>(&dataout[0]), dataout.size()); + if ( output.bad() ) std::cerr << "Error writing to ostream\n"; + return output.good(); } |