Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
15Allows 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# define X_DISPLAY_MISSING 1
37# include <afterbase.h>
38#endif
39# include <import.h>
40
41
42
43////////////////////////////////////////////////////////////////////////////////
44/// ctor
45
47{
48#ifndef WIN32
50#else
51 fInterpreter = gSystem->Which(gSystem->Getenv("PATH"), "gswin32c.exe", kExecutePermission);
52 if (fInterpreter) {
53 // which returned path may include blanks, like "Program Files" which popen does not like
54 delete [] fInterpreter;
55 fInterpreter = StrDup("gswin32c.exe");
56 }
57#endif
58}
59
60////////////////////////////////////////////////////////////////////////////////
61/// dtor
62
70
71////////////////////////////////////////////////////////////////////////////////
72/// read PS/EPS/PDF file and convert it to ASImage
73
75{
76 if (!fInterpreter) {
77 Warning("File2ASImage", "GhostScript is not available");
78 return nullptr;
79 }
80
82 Warning("File2ASImage", "input file %s is not accessible", filename);
83 return nullptr;
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 nullptr;
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 = std::abs(ux - lx);
112 height = std::abs(uy - ly);
113 break;
114 }
115 } while (!feof(fd));
116
117 fclose(fd);
118 }
119
120 // command line to execute
122 if (eps) {
123 cmd += TString::Format(" -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 nullptr;
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
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 = nullptr;
151 params.compression = 0;
152 params.format = ASA_ASImage;
153 params.search_path = nullptr;
154 params.subimage = 0;
155
156 ASImage *ret = PNGBuff2ASimage((CARD8 *)raw.Data(), &params);
157 return ret;
158}
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:68
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
#define popen_flags
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t width
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
char * StrDup(const char *str)
Duplicate the string str.
Definition TString.cxx:2563
@ kExecutePermission
Definition TSystem.h:53
R__EXTERN TSystem * gSystem
Definition TSystem.h:582
char * fInterpreter
path to GhostScript interpreter
Definition TASPluginGS.h:28
~TASPluginGS() override
dtor
TASPluginGS(const char *ext)
ctor
ASImage * File2ASImage(const char *filename) override
read PS/EPS/PDF file and convert it to ASImage
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1081
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:384
TString & Append(const char *cs)
Definition TString.h:581
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2384
virtual const char * Getenv(const char *env)
Get environment variable.
Definition TSystem.cxx:1676
virtual FILE * OpenPipe(const char *command, const char *mode)
Open a pipe.
Definition TSystem.cxx:660
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:1307
virtual int ClosePipe(FILE *pipe)
Close the pipe.
Definition TSystem.cxx:669
virtual char * Which(const char *search, const char *file, EAccessMode mode=kFileExists)
Find location of file in a search path.
Definition TSystem.cxx:1559
TLine * line
void CallRecursiveRemoveIfNeeded(TObject &obj)
call RecursiveRemove for obj if gROOT is valid and obj.TestBit(kMustCleanup) is true.
Definition TROOT.h:403