#include <algorithm>
#include <iostream>
+#include <sstream>
#include "image.hpp"
#include "json.hpp"
// Tell the GUI this is no ordinary object, but an image
json.writeStringMember("__class__", "image");
- json.writeNumberMember("__width__", desc.width);
- json.writeNumberMember("__height__", desc.height);
- json.writeNumberMember("__depth__", desc.depth);
+ json.writeIntMember("__width__", desc.width);
+ json.writeIntMember("__height__", desc.height);
+ json.writeIntMember("__depth__", desc.depth);
json.writeStringMember("__format__", formatToString(desc.internalFormat));
- // Hardcoded for now, but we could chose types more adequate to the
- // texture internal format
- json.writeStringMember("__type__", "uint8");
- json.writeBoolMember("__normalized__", true);
- json.writeNumberMember("__channels__", channels);
-
- GLubyte *pixels = new GLubyte[desc.depth*desc.width*desc.height*channels];
+ image::Image *image = new image::Image(desc.width, desc.height*desc.depth, channels, true);
context.resetPixelPackState();
if (context.ES) {
- getTexImageOES(target, level, desc, pixels);
+ getTexImageOES(target, level, desc, image->pixels);
} else {
- glGetTexImage(target, level, format, GL_UNSIGNED_BYTE, pixels);
+ glGetTexImage(target, level, format, GL_UNSIGNED_BYTE, image->pixels);
}
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->writePNG(ss);
+ const std::string & s = ss.str();
+ json.writeBase64(s.data(), s.size());
json.endMember(); // __data__
- delete [] pixels;
+ delete image;
json.endObject();
}
// Tell the GUI this is no ordinary object, but an image
json.writeStringMember("__class__", "image");
- json.writeNumberMember("__width__", width);
- json.writeNumberMember("__height__", height);
- json.writeNumberMember("__depth__", 1);
+ json.writeIntMember("__width__", width);
+ json.writeIntMember("__height__", height);
+ json.writeIntMember("__depth__", 1);
json.writeStringMember("__format__", formatToString(internalFormat));
- // Hardcoded for now, but we could chose types more adequate to the
- // texture internal format
- json.writeStringMember("__type__", "uint8");
- json.writeBoolMember("__normalized__", true);
- json.writeNumberMember("__channels__", channels);
-
GLenum type = GL_UNSIGNED_BYTE;
#if DEPTH_AS_RGBA
}
#endif
- GLubyte *pixels = new GLubyte[width*height*channels];
+ image::Image *image = new image::Image(width, height, channels, true);
// TODO: reset imaging state too
context.resetPixelPackState();
- glReadPixels(0, 0, width, height, format, type, pixels);
+ glReadPixels(0, 0, width, height, format, type, image->pixels);
context.restorePixelPackState();
json.beginMember("__data__");
- char *pngBuffer;
- int pngBufferSize;
- image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize);
- //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
- // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
- json.writeBase64(pngBuffer, pngBufferSize);
- free(pngBuffer);
+ std::stringstream ss;
+ image->writePNG(ss);
+ const std::string & s = ss.str();
+ json.writeBase64(s.data(), s.size());
json.endMember(); // __data__
- delete [] pixels;
+ delete image;
json.endObject();
}