**************************************************************************/
+#include <assert.h>
#include <string.h>
#include <algorithm>
};
+/**
+ * Sames as enumToString, but with special provision to handle formatsLUMINANCE_ALPHA.
+ *
+ * OpenGL 2.1 specification states that "internalFormat may (for backwards
+ * compatibility with the 1.0 version of the GL) also take on the integer
+ * values 1, 2, 3, and 4, which are equivalent to symbolic constants LUMINANCE,
+ * LUMINANCE ALPHA, RGB, and RGBA respectively".
+ */
+const char *
+formatToString(GLenum internalFormat) {
+ switch (internalFormat) {
+ case 1:
+ return "GL_LUMINANCE";
+ case 2:
+ return "GL_LUMINANCE_ALPHA";
+ case 3:
+ return "GL_RGB";
+ case 4:
+ return "GL_RGBA";
+ default:
+ return enumToString(internalFormat);
+ }
+}
+
+
/**
* OpenGL ES does not support glGetTexLevelParameteriv, but it is possible to
* probe whether a texture has a given size by crafting a dummy glTexSubImage()
static inline bool
getActiveTextureLevelDesc(Context &context, GLenum target, GLint level, ImageDesc &desc)
{
+ assert(target != GL_TEXTURE_CUBE_MAP);
+
if (context.ES) {
return getActiveTextureLevelDescOES(context, target, level, desc);
}
}
char label[512];
-
GLint active_texture = GL_TEXTURE0;
glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
snprintf(label, sizeof label, "%s, %s, level = %d",
json.beginMember(label);
- json.beginObject();
-
GLuint channels;
GLenum format;
if (!context.ES && isDepthFormat(desc.internalFormat)) {
channels = 4;
}
- // 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.writeStringMember("__format__", enumToString(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.height, channels, true, &pngBuffer, &pngBufferSize);
- json.writeBase64(pngBuffer, pngBufferSize);
- free(pngBuffer);
- json.endMember(); // __data__
+ json.writeImage(image, formatToString(desc.internalFormat), desc.depth);
- delete [] pixels;
- json.endObject();
+ delete image;
+
+ json.endMember(); // label
}
GLint level = 0;
do {
ImageDesc desc;
- if (!getActiveTextureLevelDesc(context, target, level, desc)) {
- break;
- }
if (target == GL_TEXTURE_CUBE_MAP) {
for (int face = 0; face < 6; ++face) {
+ if (!getActiveTextureLevelDesc(context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, desc)) {
+ return;
+ }
dumpActiveTextureLevel(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
}
} else {
+ if (!getActiveTextureLevelDesc(context, target, level, desc)) {
+ return;
+ }
dumpActiveTextureLevel(json, context, target, level);
}
* proper value for this, but rather returns 0. The GL(ES) specification
* mandates a minimum value of 2, so use this as a fall-back value.
*/
- max_units = std::min(max_units, 2);
+ max_units = std::max(max_units, 2);
for (GLint unit = 0; unit < max_units; ++unit) {
GLenum texture = GL_TEXTURE0 + unit;
image::Image *
getDrawBufferImage() {
GLenum format = GL_RGB;
- GLint channels = __gl_format_channels(format);
+ GLint channels = _gl_format_channels(format);
if (channels > 4) {
return NULL;
}
dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
GLint internalFormat = GL_NONE)
{
- GLint channels = __gl_format_channels(format);
-
- Context context;
+ GLint channels = _gl_format_channels(format);
- json.beginObject();
-
- // 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.writeStringMember("__format__", enumToString(internalFormat));
+ if (internalFormat == GL_NONE) {
+ internalFormat = format;
+ }
- // 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);
+ Context context;
GLenum type = GL_UNSIGNED_BYTE;
}
#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);
- json.endMember(); // __data__
-
- delete [] pixels;
- json.endObject();
+ json.writeImage(image, formatToString(internalFormat));
+
+ delete image;
}