#include "Riostream.h"
#include "TVirtualPad.h"
#include "TVirtualPS.h"
#include "TGLOutput.h"
#include "TGLViewer.h"
#include "TSystem.h"
#include "gl2ps.h"
#include "TError.h"
#include <assert.h>
ClassImp(TGLOutput)
Bool_t TGLOutput::Capture(TGLViewer & viewer, EFormat format, const char * filePath)
{
switch(format) {
case(kEPS_SIMPLE):
case(kEPS_BSP):
case(kPDF_SIMPLE):
case(kPDF_BSP): {
return CapturePostscript(viewer, format, filePath);
}
}
assert(kFALSE);
return kFALSE;
}
Bool_t TGLOutput::CapturePostscript(TGLViewer & viewer, EFormat format, const char * filePath)
{
if (!filePath || strlen(filePath) == 0) {
if (format == kEPS_SIMPLE || format == kEPS_BSP) {
filePath = "viewer.eps";
} else if (format == kPDF_SIMPLE || format == kPDF_BSP) {
filePath = "viewer.pdf";
}
}
Info("TGLOutput::Postscript()", "Start creating %s.", filePath);
std::cout << "Please wait.";
if (FILE *output = fopen (filePath, "w+b"))
{
Int_t gl2psFormat;
Int_t gl2psSort;
switch(format) {
case kEPS_SIMPLE:
gl2psFormat = GL2PS_EPS;
gl2psSort = GL2PS_SIMPLE_SORT;
break;
case kEPS_BSP:
gl2psFormat = GL2PS_EPS;
gl2psSort = GL2PS_BSP_SORT;
break;
case kPDF_SIMPLE:
gl2psFormat = GL2PS_PDF;
gl2psSort = GL2PS_SIMPLE_SORT;
break;
case kPDF_BSP:
gl2psFormat = GL2PS_PDF;
gl2psSort = GL2PS_BSP_SORT;
break;
default:
assert(kFALSE);
return kFALSE;
}
Int_t buffsize = 0, state = GL2PS_OVERFLOW;
viewer.DoDraw();
viewer.fIsPrinting = kTRUE;
while (state == GL2PS_OVERFLOW) {
buffsize += 1024*1024;
gl2psBeginPage ("ROOT Scene Graph", "ROOT", NULL,
gl2psFormat, gl2psSort, GL2PS_USE_CURRENT_VIEWPORT
| GL2PS_SILENT | GL2PS_BEST_ROOT | GL2PS_OCCLUSION_CULL | 0,
GL_RGBA, 0, NULL,0, 0, 0,
buffsize, output, NULL);
viewer.DoDraw();
state = gl2psEndPage();
std::cout << ".";
}
std::cout << std::endl;
fclose (output);
viewer.fIsPrinting = kFALSE;
if (!gSystem->AccessPathName(filePath)) {
Info("TGLOutput::Postscript", "Finished creating %s.", filePath);
return kTRUE;
}
} else {
Error("TGLOutput::Postscript", "Failed to create %s. ", filePath);
}
return kFALSE;
}
void TGLOutput::StartEmbeddedPS()
{
Info("TGLOutput::StartEmbeddedPS", "PS output started ...");
gVirtualPS->PrintStr("@");
gVirtualPS->PrintStr("% Start gl2ps EPS@");
gVirtualPS->PrintStr("newpath gsave save@");
Double_t xx[2] = {0.}, yy[2] = {0.};
xx[0] = gPad->GetUxmin();
yy[0] = gPad->GetUymin();
xx[1] = gPad->GetUxmax();
yy[1] = gPad->GetUymax();
gVirtualPS->PrintStr("@");
GLint vp[4];
glGetIntegerv(GL_VIEWPORT,vp);
gVirtualPS->DrawPS(0, xx, yy);
gVirtualPS->PrintStr(" exch");
xx[0] = xx[1];
yy[0] = yy[1];
gVirtualPS->DrawPS(0, xx, yy);
gVirtualPS->PrintStr(" 4 1 roll exch sub 3 1 roll sub");
gVirtualPS->WriteInteger(2*4*gPad->GetBorderSize());
gVirtualPS->PrintStr(" sub exch");
gVirtualPS->WriteInteger(2*4*gPad->GetBorderSize());
gVirtualPS->PrintStr(" sub exch");
gVirtualPS->WriteInteger((Int_t)(vp[3]));
gVirtualPS->WriteInteger((Int_t)(vp[2]));
gVirtualPS->PrintStr(" 4 1 roll div 3 1 roll exch div exch scale@");
gVirtualPS->PrintStr("@");
gVirtualPS->PrintStr("countdictstack@");
gVirtualPS->PrintStr("mark@");
gVirtualPS->PrintStr("/showpage {} def@");
ofstream *fs = (ofstream*)gVirtualPS->GetStream();
fs->close();
}
void TGLOutput::CloseEmbeddedPS()
{
ofstream *fs = new ofstream(gVirtualPS->GetName(),ios::app);
gVirtualPS->SetStream(fs);
gVirtualPS->PrintStr("@");
gVirtualPS->PrintStr("cleartomark@");
gVirtualPS->PrintStr("countdictstack exch sub { end } repeat@");
gVirtualPS->PrintStr("restore grestore@");
gVirtualPS->PrintStr("% End gl2ps EPS@");
Info("TGLOutput::CloseEmbeddedPS", "PS output finished");
}
void TGLOutput::Capture(TGLViewer & viewer)
{
StartEmbeddedPS();
FILE *output = fopen (gVirtualPS->GetName(), "a");
Int_t gl2psFormat = GL2PS_EPS;
Int_t gl2psSort = GL2PS_BSP_SORT;
Int_t buffsize = 0, state = GL2PS_OVERFLOW;
viewer.DoDraw();
viewer.fIsPrinting = kTRUE;
while (state == GL2PS_OVERFLOW) {
buffsize += 1024*1024;
gl2psBeginPage ("ROOT Scene Graph", "ROOT", NULL,
gl2psFormat, gl2psSort, GL2PS_USE_CURRENT_VIEWPORT
| GL2PS_SILENT | GL2PS_BEST_ROOT | GL2PS_OCCLUSION_CULL | 0,
GL_RGBA, 0, NULL,0, 0, 0,
buffsize, output, NULL);
viewer.DoDraw();
state = gl2psEndPage();
std::cout << ".";
}
std::cout << std::endl;
fclose (output);
viewer.fIsPrinting = kFALSE;
CloseEmbeddedPS();
}