summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/OffscreenContext.h1
-rw-r--r--src/OffscreenContextGLX.cc21
-rw-r--r--src/imageutils-lodepng.cc10
-rw-r--r--src/imageutils-macosx.cc12
4 files changed, 35 insertions, 9 deletions
diff --git a/src/OffscreenContext.h b/src/OffscreenContext.h
index 8645da7..a1792b4 100644
--- a/src/OffscreenContext.h
+++ b/src/OffscreenContext.h
@@ -8,6 +8,7 @@ struct OffscreenContext *create_offscreen_context(int w, int h);
void bind_offscreen_context(OffscreenContext *ctx);
bool teardown_offscreen_context(OffscreenContext *ctx);
bool save_framebuffer(OffscreenContext *ctx, const char *filename);
+bool save_framebuffer(OffscreenContext *ctx, std::ostream &output);
std::string offscreen_context_getinfo(OffscreenContext *ctx);
#endif
diff --git a/src/OffscreenContextGLX.cc b/src/OffscreenContextGLX.cc
index e607593..73794cc 100644
--- a/src/OffscreenContextGLX.cc
+++ b/src/OffscreenContextGLX.cc
@@ -45,6 +45,7 @@ See Also
#include <GL/glx.h>
#include <assert.h>
+#include <fstream>
#include <sstream>
#include <sys/utsname.h> // for uname
@@ -300,8 +301,24 @@ bool teardown_offscreen_context(OffscreenContext *ctx)
*/
bool save_framebuffer(OffscreenContext *ctx, const char *filename)
{
+ std::ofstream fstream(filename);
+ if (!fstream.is_open()) {
+ PRINTB("Can't open file \"%s\" for writing", filename);
+ return false;
+ } else {
+ save_framebuffer(ctx, fstream);
+ fstream.close();
+ }
+ return true;
+}
+
+/*!
+ Capture framebuffer from OpenGL and write it to the given ostream
+*/
+bool save_framebuffer(OffscreenContext *ctx, std::ostream &output)
+{
glXSwapBuffers(ctx->xdisplay, ctx->xwindow);
- if (!ctx || !filename) return false;
+ if (!ctx) return false;
int samplesPerPixel = 4; // R, G, B and A
GLubyte pixels[ctx->width * ctx->height * samplesPerPixel];
glReadPixels(0, 0, ctx->width, ctx->height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
@@ -315,7 +332,7 @@ bool save_framebuffer(OffscreenContext *ctx, const char *filename)
}
flip_image(pixels, flippedBuffer, samplesPerPixel, ctx->width, ctx->height);
- bool writeok = write_png(filename, flippedBuffer, ctx->width, ctx->height);
+ bool writeok = write_png(output, flippedBuffer, ctx->width, ctx->height);
free(flippedBuffer);
diff --git a/src/imageutils-lodepng.cc b/src/imageutils-lodepng.cc
index 96d9d90..fab1cd9 100644
--- a/src/imageutils-lodepng.cc
+++ b/src/imageutils-lodepng.cc
@@ -9,8 +9,16 @@ bool write_png(std::ostream &output, unsigned char *pixels, int width, int heigh
//LodePNG_Text_add(&encoder.infoPng.text, "Comment", "Created with LodePNG");
size_t dataout_size = -1;
unsigned char *dataout = (unsigned char *)malloc(width*height*4);
+ if (!dataout) {
+ perror("Error allocating memory while writing png\n");
+ return false;
+ }
LodePNG_encode(&dataout, &dataout_size, pixels, width, height, LCT_RGBA, 8);
- output.write( reinterpret_cast<const char*>(dataout), dataout_size );;
+ try {
+ output.write( reinterpret_cast<const char*>(dataout), dataout_size );
+ } catch (const std::ios_base::failure &e) {
+ std::cerr << "Error writing to ostream:" << e.what() << "\n";
+ }
free( dataout );
return true;
}
diff --git a/src/imageutils-macosx.cc b/src/imageutils-macosx.cc
index fc4803b..5236b76 100644
--- a/src/imageutils-macosx.cc
+++ b/src/imageutils-macosx.cc
@@ -11,12 +11,12 @@ size_t write_bytes_to_ostream (void *info,const void *buffer,size_t count)
size_t startpos = output->tellp();
size_t endpos = startpos;
try {
- output->write( reinterpret_cast<const char *>buffer, count );
+ output->write( (const char *)buffer, count );
endpos = output->tellp();
} catch (const std::ios_base::failure& e)
- std::cerr << "Error writing to ostream:" << e.what() << "\n";
+ std::cerr << "Error writing to ostream:" << e.what() << "\n";
}
- return endpos-startpos;
+ return (endpos-startpos);
}
CGDataConsumerRef dataconsumer CGDataConsumerCreateWithOstream(std::ostream &output)
@@ -48,8 +48,8 @@ bool write_png(std::ostream &output, unsigned char *pixels, int width, int heigh
return false;
}
- CGDataConsumerRef dataconsumer = CGDataConsumerCreateWithOstream(output);
- /*
+ CGDataConsumerRef dataconsumer = CGDataConsumerCreateWithOstream(output);
+ /*
CFStringRef fname = CFStringCreateWithCString(kCFAllocatorDefault, filename, kCFStringEncodingUTF8);
CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
fname, kCFURLPOSIXPathStyle, false);
@@ -59,7 +59,7 @@ bool write_png(std::ostream &output, unsigned char *pixels, int width, int heigh
}
CGDataConsumerRef dataconsumer = CGDataConsumerCreateWithURL(fileURL);
- */
+ */
CFIndex fileImageIndex = 1;
CFMutableDictionaryRef fileDict = NULL;
contact: Jan Huwald // Impressum