ROOT logo
// @(#)root/graf:$Id: TASPluginGS.cxx 21294 2007-12-10 14:53:12Z rdm $
//  Author: Valeriy Onuchin   23/06/05

/*************************************************************************
 * Copyright (C) 1995-2001, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TASPluginGS - allows to read PS/EPS/PDF files via GhostScript        //
//                                                                      //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TASPluginGS.h"
#include "TSystem.h"

#ifndef WIN32
#   include <X11/Xlib.h>
#   define popen_flags "r"
#else
#   include "Windows4root.h"
#   define popen_flags "rb"
#endif

extern "C" {
#ifndef WIN32
#   include <afterbase.h>
#else
#   include <win32/config.h>
#   include <win32/afterbase.h>
#   define X_DISPLAY_MISSING 1
#endif
#   include <import.h>
}


ClassImp(TASPluginGS)


//______________________________________________________________________________
TASPluginGS::TASPluginGS(const char *ext) : TASImagePlugin(ext)
{
   // ctor

#ifndef WIN32
   fInterpreter = gSystem->Which(gSystem->Getenv("PATH"), "gs", kExecutePermission);
#else
   fInterpreter = gSystem->Which(gSystem->Getenv("PATH"), "gswin32c.exe", kExecutePermission);
   if (fInterpreter) {
      // which returned path may include blanks, like "Program Files" which popen does not like
      delete [] fInterpreter;
      fInterpreter = StrDup("gswin32c.exe");
   }
#endif
}

//______________________________________________________________________________
TASPluginGS::~TASPluginGS()
{
   // dtor

   delete [] fInterpreter;
   fInterpreter = 0;
}

//______________________________________________________________________________
ASImage *TASPluginGS::File2ASImage(const char *filename)
{
   // read PS/EPS/PDF file and convert it to ASImage

   if (!fInterpreter) {
      Warning("File2ASImage", "GhostScript is not available");
      return 0;
   }

   if (gSystem->AccessPathName(filename)) {
      Warning("File2ASImage", "input file %s is not accessible", filename);
      return 0;
   }

   TString ext = (strrchr(filename, '.') + 1);
   ext.Strip();
   ext.ToLower();

   UInt_t width = 0;
   UInt_t height = 0;
   Bool_t eps = kFALSE;

   if (ext == "eps") {
      eps = kTRUE;
      FILE *fd = fopen(filename, "r");
      if (!fd) {
         Warning("File2ASImage", "input file %s is not readable", filename);
         return 0;
      }

      do {
         char buf[128];
         TString line = fgets(buf, 128, fd);
         if (line.IsNull() || !line.BeginsWith("%")) break;

         if (line.BeginsWith("%%BoundingBox:")) {
            int lx, ly, ux, uy;
            line = line(14, line.Length());
            sscanf(line.Data(), "%d %d %d %d", &lx, &ly, &ux, &uy);
            width = TMath::Abs(ux - lx);
            height = TMath::Abs(uy - ly);
            break;
         }
      } while (!feof(fd));

      fclose(fd);
   }

   // command line to execute
   TString cmd = fInterpreter;
   if (eps) {
      cmd += Form(" -g%dx%d", width, height);
   }
   cmd += " -dSAFER -dBATCH -dNOPAUSE -dQUIET -sDEVICE=png16m -dGraphicsAlphaBits=4 -sOutputFile=- ";
   cmd += filename;
   FILE *in = gSystem->OpenPipe(cmd.Data(), popen_flags);

   if (!in) {
      return 0;
   }

   const UInt_t kBuffLength = 32768;
   static char buf[kBuffLength];
   TString raw;

   do {
      Long_t r = fread(&buf, 1, kBuffLength, in);
      raw.Append((const char*)&buf, r);
   } while (!feof(in));

   gSystem->ClosePipe(in);

   ASImageImportParams params;
   params.flags =  0;
   params.width = width;
   params.height = height;
   params.filter = SCL_DO_ALL;
   params.gamma = 0;
   params.gamma_table = 0;
   params.compression = 0;
   params.format = ASA_ASImage;
   params.search_path = 0;
   params.subimage = 0;

   ASImage *ret = PNGBuff2ASimage((CARD8 *)raw.Data(), &params);
   return ret;
}
 TASPluginGS.cxx:1
 TASPluginGS.cxx:2
 TASPluginGS.cxx:3
 TASPluginGS.cxx:4
 TASPluginGS.cxx:5
 TASPluginGS.cxx:6
 TASPluginGS.cxx:7
 TASPluginGS.cxx:8
 TASPluginGS.cxx:9
 TASPluginGS.cxx:10
 TASPluginGS.cxx:11
 TASPluginGS.cxx:12
 TASPluginGS.cxx:13
 TASPluginGS.cxx:14
 TASPluginGS.cxx:15
 TASPluginGS.cxx:16
 TASPluginGS.cxx:17
 TASPluginGS.cxx:18
 TASPluginGS.cxx:19
 TASPluginGS.cxx:20
 TASPluginGS.cxx:21
 TASPluginGS.cxx:22
 TASPluginGS.cxx:23
 TASPluginGS.cxx:24
 TASPluginGS.cxx:25
 TASPluginGS.cxx:26
 TASPluginGS.cxx:27
 TASPluginGS.cxx:28
 TASPluginGS.cxx:29
 TASPluginGS.cxx:30
 TASPluginGS.cxx:31
 TASPluginGS.cxx:32
 TASPluginGS.cxx:33
 TASPluginGS.cxx:34
 TASPluginGS.cxx:35
 TASPluginGS.cxx:36
 TASPluginGS.cxx:37
 TASPluginGS.cxx:38
 TASPluginGS.cxx:39
 TASPluginGS.cxx:40
 TASPluginGS.cxx:41
 TASPluginGS.cxx:42
 TASPluginGS.cxx:43
 TASPluginGS.cxx:44
 TASPluginGS.cxx:45
 TASPluginGS.cxx:46
 TASPluginGS.cxx:47
 TASPluginGS.cxx:48
 TASPluginGS.cxx:49
 TASPluginGS.cxx:50
 TASPluginGS.cxx:51
 TASPluginGS.cxx:52
 TASPluginGS.cxx:53
 TASPluginGS.cxx:54
 TASPluginGS.cxx:55
 TASPluginGS.cxx:56
 TASPluginGS.cxx:57
 TASPluginGS.cxx:58
 TASPluginGS.cxx:59
 TASPluginGS.cxx:60
 TASPluginGS.cxx:61
 TASPluginGS.cxx:62
 TASPluginGS.cxx:63
 TASPluginGS.cxx:64
 TASPluginGS.cxx:65
 TASPluginGS.cxx:66
 TASPluginGS.cxx:67
 TASPluginGS.cxx:68
 TASPluginGS.cxx:69
 TASPluginGS.cxx:70
 TASPluginGS.cxx:71
 TASPluginGS.cxx:72
 TASPluginGS.cxx:73
 TASPluginGS.cxx:74
 TASPluginGS.cxx:75
 TASPluginGS.cxx:76
 TASPluginGS.cxx:77
 TASPluginGS.cxx:78
 TASPluginGS.cxx:79
 TASPluginGS.cxx:80
 TASPluginGS.cxx:81
 TASPluginGS.cxx:82
 TASPluginGS.cxx:83
 TASPluginGS.cxx:84
 TASPluginGS.cxx:85
 TASPluginGS.cxx:86
 TASPluginGS.cxx:87
 TASPluginGS.cxx:88
 TASPluginGS.cxx:89
 TASPluginGS.cxx:90
 TASPluginGS.cxx:91
 TASPluginGS.cxx:92
 TASPluginGS.cxx:93
 TASPluginGS.cxx:94
 TASPluginGS.cxx:95
 TASPluginGS.cxx:96
 TASPluginGS.cxx:97
 TASPluginGS.cxx:98
 TASPluginGS.cxx:99
 TASPluginGS.cxx:100
 TASPluginGS.cxx:101
 TASPluginGS.cxx:102
 TASPluginGS.cxx:103
 TASPluginGS.cxx:104
 TASPluginGS.cxx:105
 TASPluginGS.cxx:106
 TASPluginGS.cxx:107
 TASPluginGS.cxx:108
 TASPluginGS.cxx:109
 TASPluginGS.cxx:110
 TASPluginGS.cxx:111
 TASPluginGS.cxx:112
 TASPluginGS.cxx:113
 TASPluginGS.cxx:114
 TASPluginGS.cxx:115
 TASPluginGS.cxx:116
 TASPluginGS.cxx:117
 TASPluginGS.cxx:118
 TASPluginGS.cxx:119
 TASPluginGS.cxx:120
 TASPluginGS.cxx:121
 TASPluginGS.cxx:122
 TASPluginGS.cxx:123
 TASPluginGS.cxx:124
 TASPluginGS.cxx:125
 TASPluginGS.cxx:126
 TASPluginGS.cxx:127
 TASPluginGS.cxx:128
 TASPluginGS.cxx:129
 TASPluginGS.cxx:130
 TASPluginGS.cxx:131
 TASPluginGS.cxx:132
 TASPluginGS.cxx:133
 TASPluginGS.cxx:134
 TASPluginGS.cxx:135
 TASPluginGS.cxx:136
 TASPluginGS.cxx:137
 TASPluginGS.cxx:138
 TASPluginGS.cxx:139
 TASPluginGS.cxx:140
 TASPluginGS.cxx:141
 TASPluginGS.cxx:142
 TASPluginGS.cxx:143
 TASPluginGS.cxx:144
 TASPluginGS.cxx:145
 TASPluginGS.cxx:146
 TASPluginGS.cxx:147
 TASPluginGS.cxx:148
 TASPluginGS.cxx:149
 TASPluginGS.cxx:150
 TASPluginGS.cxx:151
 TASPluginGS.cxx:152
 TASPluginGS.cxx:153
 TASPluginGS.cxx:154
 TASPluginGS.cxx:155
 TASPluginGS.cxx:156
 TASPluginGS.cxx:157
 TASPluginGS.cxx:158