Logo ROOT   6.14/05
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 #ifndef WIN32
34 # include <afterbase.h>
35 #else
36 # include <win32/config.h>
37 # include <win32/afterbase.h>
38 # define X_DISPLAY_MISSING 1
39 #endif
40 # include <import.h>
41 
42 
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 /// ctor
47 
49 {
50 #ifndef WIN32
52 #else
53  fInterpreter = gSystem->Which(gSystem->Getenv("PATH"), "gswin32c.exe", kExecutePermission);
54  if (fInterpreter) {
55  // which returned path may include blanks, like "Program Files" which popen does not like
56  delete [] fInterpreter;
57  fInterpreter = StrDup("gswin32c.exe");
58  }
59 #endif
60 }
61 
62 ////////////////////////////////////////////////////////////////////////////////
63 /// dtor
64 
66 {
67  delete [] fInterpreter;
68  fInterpreter = 0;
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// read PS/EPS/PDF file and convert it to ASImage
73 
74 ASImage *TASPluginGS::File2ASImage(const char *filename)
75 {
76  if (!fInterpreter) {
77  Warning("File2ASImage", "GhostScript is not available");
78  return 0;
79  }
80 
81  if (gSystem->AccessPathName(filename)) {
82  Warning("File2ASImage", "input file %s is not accessible", filename);
83  return 0;
84  }
85 
86  TString ext = (strrchr(filename, '.') + 1);
87  ext.Strip();
88  ext.ToLower();
89 
90  UInt_t width = 0;
91  UInt_t height = 0;
92  Bool_t eps = kFALSE;
93 
94  if (ext == "eps") {
95  eps = kTRUE;
96  FILE *fd = fopen(filename, "r");
97  if (!fd) {
98  Warning("File2ASImage", "input file %s is not readable", filename);
99  return 0;
100  }
101 
102  do {
103  char buf[128];
104  TString line = fgets(buf, 128, fd);
105  if (line.IsNull() || !line.BeginsWith("%")) break;
106 
107  if (line.BeginsWith("%%BoundingBox:")) {
108  int lx, ly, ux, uy;
109  line = line(14, line.Length());
110  sscanf(line.Data(), "%d %d %d %d", &lx, &ly, &ux, &uy);
111  width = TMath::Abs(ux - lx);
112  height = TMath::Abs(uy - ly);
113  break;
114  }
115  } while (!feof(fd));
116 
117  fclose(fd);
118  }
119 
120  // command line to execute
121  TString cmd = fInterpreter;
122  if (eps) {
123  cmd += Form(" -g%dx%d", width, height);
124  }
125  cmd += " -dSAFER -dBATCH -dNOPAUSE -dQUIET -sDEVICE=png16m -dGraphicsAlphaBits=4 -sOutputFile=- ";
126  cmd += filename;
127  FILE *in = gSystem->OpenPipe(cmd.Data(), popen_flags);
128 
129  if (!in) {
130  return 0;
131  }
132 
133  const UInt_t kBuffLength = 32768;
134  static char buf[kBuffLength];
135  TString raw;
136 
137  do {
138  Long_t r = fread(&buf, 1, kBuffLength, in);
139  raw.Append((const char*)&buf, r);
140  } while (!feof(in));
141 
142  gSystem->ClosePipe(in);
143 
144  ASImageImportParams params;
145  params.flags = 0;
146  params.width = width;
147  params.height = height;
148  params.filter = SCL_DO_ALL;
149  params.gamma = 0;
150  params.gamma_table = 0;
151  params.compression = 0;
152  params.format = ASA_ASImage;
153  params.search_path = 0;
154  params.subimage = 0;
155 
156  ASImage *ret = PNGBuff2ASimage((CARD8 *)raw.Data(), &params);
157  return ret;
158 }
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:1276
TLine * line
image html pict1_TGaxis_012 png width
Define new text attributes for the label number "labNum".
Definition: TGaxis.cxx:2551
Basic string class.
Definition: TString.h:131
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1100
bool Bool_t
Definition: RtypesCore.h:59
virtual char * Which(const char *search, const char *file, EAccessMode mode=kFileExists)
Find location of file in a search path.
Definition: TSystem.cxx:1522
virtual FILE * OpenPipe(const char *command, const char *mode)
Open a pipe.
Definition: TSystem.cxx:670
Short_t Abs(Short_t d)
Definition: TMathBase.h:108
virtual const char * Getenv(const char *env)
Get environment variable.
Definition: TSystem.cxx:1638
virtual ~TASPluginGS()
dtor
Definition: TASPluginGS.cxx:65
TString & Append(const char *cs)
Definition: TString.h:559
TASPluginGS(const char *ext)
ctor
Definition: TASPluginGS.cxx:48
ASImage * File2ASImage(const char *filename)
read PS/EPS/PDF file and convert it to ASImage
Definition: TASPluginGS.cxx:74
ROOT::R::TRInterface & r
Definition: Object.C:4
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:610
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
Ssiz_t Length() const
Definition: TString.h:405
TSubString Strip(EStripType s=kTrailing, char c=' ') const
Return a substring of self stripped at beginning and/or end.
Definition: TString.cxx:1081
virtual int ClosePipe(FILE *pipe)
Close the pipe.
Definition: TSystem.cxx:679
char * StrDup(const char *str)
Duplicate the string str.
Definition: TString.cxx:2465
const Bool_t kFALSE
Definition: RtypesCore.h:88
long Long_t
Definition: RtypesCore.h:50
#define ClassImp(name)
Definition: Rtypes.h:359
#define popen_flags
Definition: TASPluginGS.cxx:30
Bool_t IsNull() const
Definition: TString.h:402
Allows to read PS/EPS/PDF files via GhostScript.
Definition: TASPluginGS.h:25
char * fInterpreter
path to GhostScript interpreter
Definition: TASPluginGS.h:28
const Bool_t kTRUE
Definition: RtypesCore.h:87
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:866
const char * Data() const
Definition: TString.h:364