00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "Riostream.h"
00013 #include "TVirtualPad.h"
00014 #include "TVirtualPS.h"
00015 #include "TGLOutput.h"
00016 #include "TGLViewer.h"
00017 #include "TSystem.h"
00018 #include "gl2ps.h"
00019 #include "TError.h"
00020 #include <assert.h>
00021
00022
00023
00024
00025
00026
00027
00028
00029 ClassImp(TGLOutput)
00030
00031
00032 Bool_t TGLOutput::Capture(TGLViewer & viewer, EFormat format, const char * filePath)
00033 {
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 switch(format) {
00045 case(kEPS_SIMPLE):
00046 case(kEPS_BSP):
00047 case(kPDF_SIMPLE):
00048 case(kPDF_BSP): {
00049 return CapturePostscript(viewer, format, filePath);
00050 }
00051 }
00052
00053 assert(kFALSE);
00054 return kFALSE;
00055 }
00056
00057
00058 Bool_t TGLOutput::CapturePostscript(TGLViewer & viewer, EFormat format, const char * filePath)
00059 {
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 if (!filePath || strlen(filePath) == 0) {
00070 if (format == kEPS_SIMPLE || format == kEPS_BSP) {
00071 filePath = "viewer.eps";
00072 } else if (format == kPDF_SIMPLE || format == kPDF_BSP) {
00073 filePath = "viewer.pdf";
00074 }
00075 }
00076 Info("TGLOutput::Postscript()", "Start creating %s.", filePath);
00077 std::cout << "Please wait.";
00078
00079 if (FILE *output = fopen (filePath, "w+b"))
00080 {
00081 Int_t gl2psFormat;
00082 Int_t gl2psSort;
00083
00084 switch(format) {
00085 case kEPS_SIMPLE:
00086 gl2psFormat = GL2PS_EPS;
00087 gl2psSort = GL2PS_SIMPLE_SORT;
00088 break;
00089 case kEPS_BSP:
00090 gl2psFormat = GL2PS_EPS;
00091 gl2psSort = GL2PS_BSP_SORT;
00092 break;
00093 case kPDF_SIMPLE:
00094 gl2psFormat = GL2PS_PDF;
00095 gl2psSort = GL2PS_SIMPLE_SORT;
00096 break;
00097 case kPDF_BSP:
00098 gl2psFormat = GL2PS_PDF;
00099 gl2psSort = GL2PS_BSP_SORT;
00100 break;
00101 default:
00102 assert(kFALSE);
00103 return kFALSE;
00104 }
00105 Int_t buffsize = 0, state = GL2PS_OVERFLOW;
00106 viewer.DoDraw();
00107 viewer.fIsPrinting = kTRUE;
00108 while (state == GL2PS_OVERFLOW) {
00109 buffsize += 1024*1024;
00110 gl2psBeginPage ("ROOT Scene Graph", "ROOT", NULL,
00111 gl2psFormat, gl2psSort, GL2PS_USE_CURRENT_VIEWPORT
00112 | GL2PS_SILENT | GL2PS_BEST_ROOT | GL2PS_OCCLUSION_CULL | 0,
00113 GL_RGBA, 0, NULL,0, 0, 0,
00114 buffsize, output, NULL);
00115 viewer.DoDraw();
00116 state = gl2psEndPage();
00117 std::cout << ".";
00118 }
00119 std::cout << std::endl;
00120 fclose (output);
00121 viewer.fIsPrinting = kFALSE;
00122 if (!gSystem->AccessPathName(filePath)) {
00123 Info("TGLOutput::Postscript", "Finished creating %s.", filePath);
00124 return kTRUE;
00125 }
00126 } else {
00127 Error("TGLOutput::Postscript", "Failed to create %s. ", filePath);
00128 }
00129
00130 return kFALSE;
00131 }
00132
00133
00134 void TGLOutput::StartEmbeddedPS()
00135 {
00136
00137 Info("TGLOutput::StartEmbeddedPS", "PS output started ...");
00138
00139 gVirtualPS->PrintStr("@");
00140 gVirtualPS->PrintStr("% Start gl2ps EPS@");
00141 gVirtualPS->PrintStr("newpath gsave save@");
00142 Double_t xx[2] = {0.}, yy[2] = {0.};
00143 xx[0] = gPad->GetUxmin();
00144 yy[0] = gPad->GetUymin();
00145 xx[1] = gPad->GetUxmax();
00146 yy[1] = gPad->GetUymax();
00147 gVirtualPS->PrintStr("@");
00148
00149
00150
00151
00152
00153
00154
00155
00156 GLint vp[4];
00157 glGetIntegerv(GL_VIEWPORT,vp);
00158 gVirtualPS->DrawPS(0, xx, yy);
00159 gVirtualPS->PrintStr(" exch");
00160 xx[0] = xx[1];
00161 yy[0] = yy[1];
00162 gVirtualPS->DrawPS(0, xx, yy);
00163 gVirtualPS->PrintStr(" 4 1 roll exch sub 3 1 roll sub");
00164 gVirtualPS->WriteInteger(2*4*gPad->GetBorderSize());
00165 gVirtualPS->PrintStr(" sub exch");
00166 gVirtualPS->WriteInteger(2*4*gPad->GetBorderSize());
00167 gVirtualPS->PrintStr(" sub exch");
00168 gVirtualPS->WriteInteger((Int_t)(vp[3]));
00169 gVirtualPS->WriteInteger((Int_t)(vp[2]));
00170 gVirtualPS->PrintStr(" 4 1 roll div 3 1 roll exch div exch scale@");
00171 gVirtualPS->PrintStr("@");
00172 gVirtualPS->PrintStr("countdictstack@");
00173 gVirtualPS->PrintStr("mark@");
00174 gVirtualPS->PrintStr("/showpage {} def@");
00175
00176
00177 ofstream *fs = (ofstream*)gVirtualPS->GetStream();
00178 fs->close();
00179
00180 }
00181
00182
00183 void TGLOutput::CloseEmbeddedPS()
00184 {
00185
00186
00187 ofstream *fs = new ofstream(gVirtualPS->GetName(),ios::app);
00188 gVirtualPS->SetStream(fs);
00189 gVirtualPS->PrintStr("@");
00190 gVirtualPS->PrintStr("cleartomark@");
00191 gVirtualPS->PrintStr("countdictstack exch sub { end } repeat@");
00192 gVirtualPS->PrintStr("restore grestore@");
00193 gVirtualPS->PrintStr("% End gl2ps EPS@");
00194
00195 Info("TGLOutput::CloseEmbeddedPS", "PS output finished");
00196 }
00197
00198
00199 void TGLOutput::Capture(TGLViewer & viewer)
00200 {
00201
00202 StartEmbeddedPS();
00203
00204 FILE *output = fopen (gVirtualPS->GetName(), "a");
00205 Int_t gl2psFormat = GL2PS_EPS;
00206 Int_t gl2psSort = GL2PS_BSP_SORT;
00207 Int_t buffsize = 0, state = GL2PS_OVERFLOW;
00208 viewer.DoDraw();
00209 viewer.fIsPrinting = kTRUE;
00210
00211 while (state == GL2PS_OVERFLOW) {
00212 buffsize += 1024*1024;
00213 gl2psBeginPage ("ROOT Scene Graph", "ROOT", NULL,
00214 gl2psFormat, gl2psSort, GL2PS_USE_CURRENT_VIEWPORT
00215 | GL2PS_SILENT | GL2PS_BEST_ROOT | GL2PS_OCCLUSION_CULL | 0,
00216 GL_RGBA, 0, NULL,0, 0, 0,
00217 buffsize, output, NULL);
00218 viewer.DoDraw();
00219 state = gl2psEndPage();
00220 std::cout << ".";
00221 }
00222
00223 std::cout << std::endl;
00224 fclose (output);
00225 viewer.fIsPrinting = kFALSE;
00226
00227 CloseEmbeddedPS();
00228 }