00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "TASPluginGS.h"
00020 #include "TSystem.h"
00021
00022 #ifndef WIN32
00023 # include <X11/Xlib.h>
00024 # define popen_flags "r"
00025 #else
00026 # include "Windows4root.h"
00027 # define popen_flags "rb"
00028 #endif
00029
00030 extern "C" {
00031 #ifndef WIN32
00032 # include <afterbase.h>
00033 #else
00034 # include <win32/config.h>
00035 # include <win32/afterbase.h>
00036 # define X_DISPLAY_MISSING 1
00037 #endif
00038 # include <import.h>
00039 }
00040
00041
00042 ClassImp(TASPluginGS)
00043
00044
00045
00046 TASPluginGS::TASPluginGS(const char *ext) : TASImagePlugin(ext)
00047 {
00048
00049
00050 #ifndef WIN32
00051 fInterpreter = gSystem->Which(gSystem->Getenv("PATH"), "gs", kExecutePermission);
00052 #else
00053 fInterpreter = gSystem->Which(gSystem->Getenv("PATH"), "gswin32c.exe", kExecutePermission);
00054 if (fInterpreter) {
00055
00056 delete [] fInterpreter;
00057 fInterpreter = StrDup("gswin32c.exe");
00058 }
00059 #endif
00060 }
00061
00062
00063 TASPluginGS::~TASPluginGS()
00064 {
00065
00066
00067 delete [] fInterpreter;
00068 fInterpreter = 0;
00069 }
00070
00071
00072 ASImage *TASPluginGS::File2ASImage(const char *filename)
00073 {
00074
00075
00076 if (!fInterpreter) {
00077 Warning("File2ASImage", "GhostScript is not available");
00078 return 0;
00079 }
00080
00081 if (gSystem->AccessPathName(filename)) {
00082 Warning("File2ASImage", "input file %s is not accessible", filename);
00083 return 0;
00084 }
00085
00086 TString ext = (strrchr(filename, '.') + 1);
00087 ext.Strip();
00088 ext.ToLower();
00089
00090 UInt_t width = 0;
00091 UInt_t height = 0;
00092 Bool_t eps = kFALSE;
00093
00094 if (ext == "eps") {
00095 eps = kTRUE;
00096 FILE *fd = fopen(filename, "r");
00097 if (!fd) {
00098 Warning("File2ASImage", "input file %s is not readable", filename);
00099 return 0;
00100 }
00101
00102 do {
00103 char buf[128];
00104 TString line = fgets(buf, 128, fd);
00105 if (line.IsNull() || !line.BeginsWith("%")) break;
00106
00107 if (line.BeginsWith("%%BoundingBox:")) {
00108 int lx, ly, ux, uy;
00109 line = line(14, line.Length());
00110 sscanf(line.Data(), "%d %d %d %d", &lx, &ly, &ux, &uy);
00111 width = TMath::Abs(ux - lx);
00112 height = TMath::Abs(uy - ly);
00113 break;
00114 }
00115 } while (!feof(fd));
00116
00117 fclose(fd);
00118 }
00119
00120
00121 TString cmd = fInterpreter;
00122 if (eps) {
00123 cmd += Form(" -g%dx%d", width, height);
00124 }
00125 cmd += " -dSAFER -dBATCH -dNOPAUSE -dQUIET -sDEVICE=png16m -dGraphicsAlphaBits=4 -sOutputFile=- ";
00126 cmd += filename;
00127 FILE *in = gSystem->OpenPipe(cmd.Data(), popen_flags);
00128
00129 if (!in) {
00130 return 0;
00131 }
00132
00133 const UInt_t kBuffLength = 32768;
00134 static char buf[kBuffLength];
00135 TString raw;
00136
00137 do {
00138 Long_t r = fread(&buf, 1, kBuffLength, in);
00139 raw.Append((const char*)&buf, r);
00140 } while (!feof(in));
00141
00142 gSystem->ClosePipe(in);
00143
00144 ASImageImportParams params;
00145 params.flags = 0;
00146 params.width = width;
00147 params.height = height;
00148 params.filter = SCL_DO_ALL;
00149 params.gamma = 0;
00150 params.gamma_table = 0;
00151 params.compression = 0;
00152 params.format = ASA_ASImage;
00153 params.search_path = 0;
00154 params.subimage = 0;
00155
00156 ASImage *ret = PNGBuff2ASimage((CARD8 *)raw.Data(), ¶ms);
00157 return ret;
00158 }