]> git.cworth.org Git - apitrace/blobdiff - retrace/json.cpp
glretrace: Dump depth buffers as floating point images.
[apitrace] / retrace / json.cpp
index e500aa67b9a2fc086c264a332ea20faec17c8904..850e37f934b539de9a53e67a7af9946a09629068 100644 (file)
@@ -31,6 +31,9 @@
 #include <assert.h>
 #include <string.h>
 
+#include <sstream>
+
+#include "image.hpp"
 #include "json.hpp"
 
 
@@ -297,3 +300,38 @@ JSONWriter::writeBool(bool b) {
     value = true;
     space = ' ';
 }
+
+void
+JSONWriter::writeImage(image::Image *image, const char *format, unsigned depth)
+{
+    if (!image) {
+        writeNull();
+        return;
+    }
+
+    beginObject();
+
+    // Tell the GUI this is no ordinary object, but an image
+    writeStringMember("__class__", "image");
+
+    writeIntMember("__width__", image->width);
+    writeIntMember("__height__", image->height / depth);
+    writeIntMember("__depth__", depth);
+
+    writeStringMember("__format__", format);
+
+    beginMember("__data__");
+    std::stringstream ss;
+
+    if (image->channelType == image::TYPE_UNORM8) {
+        image->writePNG(ss);
+    } else {
+        image->writePNM(ss);
+    }
+
+    const std::string & s = ss.str();
+    writeBase64(s.data(), s.size());
+    endMember(); // __data__
+
+    endObject();
+}