Skip to content

Commit

Permalink
adding PNG export option to OpenGL render surfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmeredith committed Jun 2, 2015
1 parent 36fa747 commit 1546d7b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/rendering/eavlRenderSurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class eavlRenderSurface
{
public:
enum FileType { PNM, EPS };
enum FileType { PNM, PNG, EPS };
protected:
public:
eavlRenderSurface()
Expand Down
50 changes: 33 additions & 17 deletions src/rendering/eavlRenderSurfaceGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <eavlPNGImporter.h>
#include <eavlGLTexture.h>

#include "lodepng.h"

class eavlRenderSurfaceGL : public eavlRenderSurface
{
int width, height;
Expand Down Expand Up @@ -306,29 +308,43 @@ class eavlRenderSurfaceGL : public eavlRenderSurface
}
virtual void SaveAs(string fn, FileType ft)
{
if (ft != PNM)
{
THROW(eavlException, "Can only save GL images as PNM");
}

Activate();

int w = width, h = height;
vector<byte> rgba(w*h*4);
glReadPixels(0,0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, &rgba[0]);

ofstream out(fn.c_str());
out<<"P6"<<endl<<w<<" "<<h<<endl<<255<<endl;
for(int i = h-1; i >= 0; i--)
if (ft == PNM)
{
for(int j = 0; j < w; j++)
int w = width, h = height;
vector<byte> rgba(w*h*4);
glReadPixels(0,0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, &rgba[0]);

ofstream out(fn.c_str());
out<<"P6"<<endl<<w<<" "<<h<<endl<<255<<endl;
for(int i = h-1; i >= 0; i--)
{
const byte *tuple = &(rgba[i*w*4 + j*4]);
out<<tuple[0]<<tuple[1]<<tuple[2];
for(int j = 0; j < w; j++)
{
const byte *tuple = &(rgba[i*w*4 + j*4]);
out<<tuple[0]<<tuple[1]<<tuple[2];
}
}
out.close();
}
else if (ft == PNG)
{
int w = width, h = height;
vector<byte> tmp(w*h*4);
glReadPixels(0,0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, &tmp[0]);

// upside down relative to what lodepng wants
vector<byte> rgba(w*h*4);
for (int y=0; y<h; ++y)
memcpy(&(rgba[y*w*4]), &(tmp[(h-y-1)*w*4]), w*4);

lodepng_encode32_file(fn.c_str(), &rgba[0], w, h);
}
else
{
THROW(eavlException, "Can only save GL images as PNM or PNG");
}
out.close();

}
};

Expand Down

0 comments on commit 1546d7b

Please sign in to comment.