From: José Fonseca Date: Tue, 27 Nov 2012 20:31:42 +0000 (+0000) Subject: glstate: Use stringstream for holding the temporary PNG images. X-Git-Url: https://git.cworth.org/git?p=apitrace;a=commitdiff_plain;h=45080ce539648a3fc073375cd9212b031a611f10 glstate: Use stringstream for holding the temporary PNG images. --- diff --git a/common/image.hpp b/common/image.hpp index e930512..2f927ce 100644 --- a/common/image.hpp +++ b/common/image.hpp @@ -99,11 +99,11 @@ public: double compare(Image &ref); }; -bool writePixelsToBuffer(unsigned char *pixels, - unsigned w, unsigned h, unsigned numChannels, - bool flipped, - char **buffer, - int *size); +bool +writePixelsToBuffer(std::ostream &os, + unsigned char *pixels, + unsigned w, unsigned h, unsigned numChannels, + bool flipped); Image * readPNG(const char *filename); diff --git a/common/image_png.cpp b/common/image_png.cpp index cc6d3f2..41ef176 100644 --- a/common/image_png.cpp +++ b/common/image_png.cpp @@ -201,45 +201,23 @@ no_fp: } -struct png_tmp_buffer -{ - char *buffer; - size_t size; -}; - static void pngWriteCallback(png_structp png_ptr, png_bytep data, png_size_t length) { - struct png_tmp_buffer *buf = (struct png_tmp_buffer*) png_get_io_ptr(png_ptr); - size_t nsize = buf->size + length; - - /* allocate or grow buffer */ - if (buf->buffer) - buf->buffer = (char*)realloc(buf->buffer, nsize); - else - buf->buffer = (char*)malloc(nsize); - - if (!buf->buffer) - png_error(png_ptr, "Buffer allocation error"); - - memcpy(buf->buffer + buf->size, data, length); - buf->size += length; + std::ostream *os = (std::ostream *) png_get_io_ptr(png_ptr); + os->write((const char *)data, length); } -bool writePixelsToBuffer(unsigned char *pixels, - unsigned width, unsigned height, unsigned numChannels, - bool flipped, - char **buffer, - int *size) +bool +writePixelsToBuffer(std::ostream &os, + unsigned char *pixels, + unsigned width, unsigned height, unsigned numChannels, + bool flipped) { - struct png_tmp_buffer png_mem; png_structp png_ptr; png_infop info_ptr; int type; - png_mem.buffer = NULL; - png_mem.size = 0; - switch (numChannels) { case 4: type = PNG_COLOR_TYPE_RGB_ALPHA; @@ -272,7 +250,7 @@ bool writePixelsToBuffer(unsigned char *pixels, goto no_png; } - png_set_write_fn(png_ptr, &png_mem, pngWriteCallback, NULL); + png_set_write_fn(png_ptr, &os, pngWriteCallback, NULL); png_set_IHDR(png_ptr, info_ptr, width, height, 8, type, PNG_INTERLACE_NONE, @@ -298,17 +276,9 @@ bool writePixelsToBuffer(unsigned char *pixels, png_write_end(png_ptr, info_ptr); png_destroy_write_struct(&png_ptr, &info_ptr); - *buffer = png_mem.buffer; - *size = png_mem.size; - return true; no_png: - *buffer = NULL; - *size = 0; - - if (png_mem.buffer) - free(png_mem.buffer); return false; } diff --git a/retrace/glstate_images.cpp b/retrace/glstate_images.cpp index e5e1451..295ebe8 100644 --- a/retrace/glstate_images.cpp +++ b/retrace/glstate_images.cpp @@ -28,6 +28,7 @@ #include #include +#include #include "image.hpp" #include "json.hpp" @@ -434,11 +435,10 @@ dumpActiveTextureLevel(JSONWriter &json, Context &context, GLenum target, GLint context.restorePixelPackState(); json.beginMember("__data__"); - char *pngBuffer; - int pngBufferSize; - image::writePixelsToBuffer(pixels, desc.width, desc.depth * desc.height, channels, true, &pngBuffer, &pngBufferSize); - json.writeBase64(pngBuffer, pngBufferSize); - free(pngBuffer); + std::stringstream ss; + image::writePixelsToBuffer(ss, pixels, desc.width, desc.depth * desc.height, channels, true); + const std::string & s = ss.str(); + json.writeBase64(s.data(), s.size()); json.endMember(); // __data__ delete [] pixels; @@ -890,13 +890,12 @@ dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format, context.restorePixelPackState(); json.beginMember("__data__"); - char *pngBuffer; - int pngBufferSize; - image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize); + std::stringstream ss; + image::writePixelsToBuffer(ss, pixels, width, height, channels, true); //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels) // <<", after = "<