]> git.cworth.org Git - apitrace/commitdiff
glstate: Use stringstream for holding the temporary PNG images.
authorJosé Fonseca <jfonseca@vmware.com>
Tue, 27 Nov 2012 20:31:42 +0000 (20:31 +0000)
committerJosé Fonseca <jfonseca@vmware.com>
Tue, 27 Nov 2012 20:31:42 +0000 (20:31 +0000)
common/image.hpp
common/image_png.cpp
retrace/glstate_images.cpp

index e930512f86473c89352a8990443cd9bbff40d622..2f927ce9a35e5407362c206c4d3738b244b826ed 100644 (file)
@@ -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);
index cc6d3f2cc4d7ff6e476db9df609a5148e27a6546..41ef1768499fc4b7ed3db8636b304617ea7db737 100644 (file)
@@ -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;
 }
 
index e5e14517dc299e78d65fe69e98899961bc18cec2..295ebe8797d3396922533154731d8b910c5fef5e 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <algorithm>
 #include <iostream>
+#include <sstream>
 
 #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 = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
-    json.writeBase64(pngBuffer, pngBufferSize);
-    free(pngBuffer);
+    const std::string & s = ss.str();
+    json.writeBase64(s.data(), s.size());
     json.endMember(); // __data__
 
     delete [] pixels;