ROOT  6.06/09
Reference Guide
TASPluginGS.cxx
Go to the documentation of this file.
1 // @(#)root/graf:$Id$
2 // Author: Valeriy Onuchin 23/06/05
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2001, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TASPluginGS
13 \ingroup asimage
14 
15 Allows to read PS/EPS/PDF files via GhostScript
16 */
17 
18 #include "TASPluginGS.h"
19 #include "TSystem.h"
20 #include "RConfigure.h"
21 
22 #ifdef R__HAS_COCOA
23 # define X_DISPLAY_MISSING 1
24 # define popen_flags "r"
25 #elif defined (WIN32)
26 # include "Windows4root.h"
27 # define popen_flags "rb"
28 #else
29 # include <X11/Xlib.h>
30 # define popen_flags "r"
31 #endif
32 
33 extern "C" {
34 #ifndef WIN32
35 # include <afterbase.h>
36 #else
37 # include <win32/config.h>
38 # include <win32/afterbase.h>
39 # define X_DISPLAY_MISSING 1
40 #endif
41 # include <import.h>
42 }
43 
44 
46 
47 ////////////////////////////////////////////////////////////////////////////////
48 /// ctor
49 
50 TASPluginGS::TASPluginGS(const char *ext) : TASImagePlugin(ext)
51 {
52 #ifndef WIN32
53  fInterpreter = gSystem->Which(gSystem->Getenv("PATH"), "gs", kExecutePermission);
54 #else
55  fInterpreter = gSystem->Which(gSystem->Getenv("PATH"), "gswin32c.exe", kExecutePermission);
56  if (fInterpreter) {
57  // which returned path may include blanks, like "Program Files" which popen does not like
58  delete [] fInterpreter;
59  fInterpreter = StrDup("gswin32c.exe");
60  }
61 #endif
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// dtor
66 
68 {
69  delete [] fInterpreter;
70  fInterpreter = 0;
71 }
72 
73 ////////////////////////////////////////////////////////////////////////////////
74 /// read PS/EPS/PDF file and convert it to ASImage
75 
76 ASImage *TASPluginGS::File2ASImage(const char *filename)
77 {
78  if (!fInterpreter) {
79  Warning("File2ASImage", "GhostScript is not available");
80  return 0;
81  }
82 
83  if (gSystem->AccessPathName(filename)) {
84  Warning("File2ASImage", "input file %s is not accessible", filename);
85  return 0;
86  }
87 
88  TString ext = (strrchr(filename, '.') + 1);
89  ext.Strip();
90  ext.ToLower();
91 
92  UInt_t width = 0;
93  UInt_t height = 0;
94  Bool_t eps = kFALSE;
95 
96  if (ext == "eps") {
97  eps = kTRUE;
98  FILE *fd = fopen(filename, "r");
99  if (!fd) {
100  Warning("File2ASImage", "input file %s is not readable", filename);
101  return 0;
102  }
103 
104  do {
105  char buf[128];
106  TString line = fgets(buf, 128, fd);
107  if (line.IsNull() || !line.BeginsWith("%")) break;
108 
109  if (line.BeginsWith("%%BoundingBox:")) {
110  int lx, ly, ux, uy;
111  line = line(14, line.Length());
112  sscanf(line.Data(), "%d %d %d %d", &lx, &ly, &ux, &uy);
113  width = TMath::Abs(ux - lx);
114  height = TMath::Abs(uy - ly);
115  break;
116  }
117  } while (!feof(fd));
118 
119  fclose(fd);
120  }
121 
122  // command line to execute
123  TString cmd = fInterpreter;
124  if (eps) {
125  cmd += Form(" -g%dx%d", width, height);
126  }
127  cmd += " -dSAFER -dBATCH -dNOPAUSE -dQUIET -sDEVICE=png16m -dGraphicsAlphaBits=4 -sOutputFile=- ";
128  cmd += filename;
129  FILE *in = gSystem->OpenPipe(cmd.Data(), popen_flags);
130 
131  if (!in) {
132  return 0;
133  }
134 
135  const UInt_t kBuffLength = 32768;
136  static char buf[kBuffLength];
137  TString raw;
138 
139  do {
140  Long_t r = fread(&buf, 1, kBuffLength, in);
141  raw.Append((const char*)&buf, r);
142  } while (!feof(in));
143 
144  gSystem->ClosePipe(in);
145 
146  ASImageImportParams params;
147  params.flags = 0;
148  params.width = width;
149  params.height = height;
150  params.filter = SCL_DO_ALL;
151  params.gamma = 0;
152  params.gamma_table = 0;
153  params.compression = 0;
154  params.format = ASA_ASImage;
155  params.search_path = 0;
156  params.subimage = 0;
157 
158  ASImage *ret = PNGBuff2ASimage((CARD8 *)raw.Data(), &params);
159  return ret;
160 }
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1265
Ssiz_t Length() const
Definition: TString.h:390
TLine * line
static const char * filename()
Basic string class.
Definition: TString.h:137
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1088
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual char * Which(const char *search, const char *file, EAccessMode mode=kFileExists)
Find location of file in a search path.
Definition: TSystem.cxx:1511
virtual FILE * OpenPipe(const char *command, const char *mode)
Open a pipe.
Definition: TSystem.cxx:666
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:558
Short_t Abs(Short_t d)
Definition: TMathBase.h:110
const char * Data() const
Definition: TString.h:349
virtual const char * Getenv(const char *env)
Get environment variable.
Definition: TSystem.cxx:1627
virtual ~TASPluginGS()
dtor
Definition: TASPluginGS.cxx:67
TString & Append(const char *cs)
Definition: TString.h:492
ASImage * File2ASImage(const char *filename)
read PS/EPS/PDF file and convert it to ASImage
Definition: TASPluginGS.cxx:76
ROOT::R::TRInterface & r
Definition: Object.C:4
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
TSubString Strip(EStripType s=kTrailing, char c= ' ') const
Return a substring of self stripped at beginning and/or end.
Definition: TString.cxx:1069
Bool_t IsNull() const
Definition: TString.h:387
virtual int ClosePipe(FILE *pipe)
Close the pipe.
Definition: TSystem.cxx:675
char * StrDup(const char *str)
Duplicate the string str.
Definition: TString.cxx:2513
long Long_t
Definition: RtypesCore.h:50
#define popen_flags
Definition: TASPluginGS.cxx:30
Allows to read PS/EPS/PDF files via GhostScript.
Definition: TASPluginGS.h:27
char * fInterpreter
Definition: TASPluginGS.h:30
const Bool_t kTRUE
Definition: Rtypes.h:91
ClassImp(TASPluginGS) TASPluginGS
ctor
Definition: TASPluginGS.cxx:45
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:904