Back to index

tetex-bin  3.0
epstopdf.c
Go to the documentation of this file.
00001 /*
00002 # epstopdf: written by Kong Hoon Lee konghoon@hyowon.cc.pusan.ac.kr<4/1/1999>
00003 #
00004 # It converts an EPS file to an encapsulated PDF File and
00005 # coded with the perl script 'epstopdf' by Sebastian Rahtz on
00006 # http://tug.org/applications/pdftex/epstopdf .
00007 # It works like as the perl script without 'perl' for Windows 95
00008 # but requires 'Ghostscript' for Windows.
00009 #
00010 */
00011 
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 #include <math.h>
00016 #define MAX_IN 10000
00017 
00018 #define DEBUG 1
00019 
00020 #ifdef FPTEX
00021 
00022 #include <win32lib.h>
00023 #include <gs32lib.h>
00024 
00025 /* This is the callback function for gs. It is mainly used to read and
00026   write  data on   gs   stdin/stdout. Data exchanges   happen  through
00027   buffers.  */
00028 int __cdecl
00029 gsdll_callback(int message, char *str, unsigned long count)
00030 {
00031   char *p;
00032 
00033   switch (message) {
00034 
00035   case GSDLL_STDIN:
00036     /* Put count chars on gs stdin */
00037 #if DEBUG
00038     fprintf(stderr, "gs wants %d chars\n", count);
00039 #endif
00040     p = fgets(str, count, stdin);
00041     if (p)
00042       return strlen(str);
00043     else
00044       return 0;
00045 
00046   case GSDLL_STDOUT:
00047     fwrite(str, 1, count, stdout);
00048     return count;
00049 
00050   case GSDLL_DEVICE:
00051     /* gs_device = str; */
00052 #if DEBUG
00053     fprintf(stdout,"Callback: DEVICE %p %s\n", str,
00054            count ? "open" : "close");
00055 #endif
00056     break;
00057 
00058   case GSDLL_SYNC:
00059 #if DEBUG
00060     fprintf(stdout,"Callback: SYNC %p\n", str);
00061 #endif
00062     break;
00063 
00064   case GSDLL_PAGE:
00065     fprintf(stdout,"Callback: PAGE %p\n", str);
00066     break;
00067 
00068   case GSDLL_SIZE:
00069 #if DEBUG
00070     fprintf(stdout,"Callback: SIZE %p width=%d height=%d\n", str,
00071            (int)(count & 0xffff), (int)((count>>16) & 0xffff) );
00072 #endif
00073     break;
00074 
00075   case GSDLL_POLL:
00076 #if 0
00077     fprintf(stderr, "GS: Poll sent (%d)!\n", 0);
00078 #endif
00079     return 0; /* no error ? */
00080   default:
00081     fprintf(stdout,"Callback: Unknown message=%d\n",message);
00082     break;
00083   }
00084   return 0;
00085 }
00086 
00087 #undef BEGINDOC
00088 #undef ENDDOC
00089 
00090 #define GSEXEC "gswin32c.exe"
00091 
00092 #endif /* FPTEX */
00093 
00094 #ifndef GSEXEC
00095 #ifdef __WIN32__
00096 #define GSEXEC "gswin32c"
00097 #else
00098 #define GSEXEC "gs"
00099 #endif
00100 #endif
00101 
00102 #define BEGINDOC "%%BeginDocument"
00103 #define ENDDOC   "%%EndDocument"
00104 
00105 
00106 static void usage(void)
00107 
00108 {
00109   printf("epstopdf 2.26:\n");
00110   printf("written by Kong Hoon Lee, konghoon@dreamwiz.com <4/1/1999>\n");
00111   printf("changes by Juergen Bausa, bausa@lfpt.rwth-aachen.de <5/25/2000>\n");
00112   printf("bugfix by Pascal Perichon, Pascal.Perichon@u-bourgogne.fr <5/25/2000>\n\n");
00113   printf("It converts an EPS file to an encapsulated PDF File and is written\n");
00114   printf("based on the perl script 'epstopdf' by Sebastian Rahtz on\n");
00115   printf("http://tug.org/applications/pdftex/epstopdf .\n");
00116   printf("It works like the perl script without 'perl' but requires 'Ghostscript'.\n");
00117   printf("The accompanied Makefile can be used to automate the update of all eps/pdf\n");
00118   printf("files in a directory. Just put it in the directory where your eps files\n");
00119   printf("are and type 'make' from the command line (you will need a version of\n");
00120   printf("GNU-make)\n\n");
00121 
00122   printf("This program invokes '%s' and the path including '%s'\n",GSEXEC,GSEXEC);
00123   printf("should be included to the environment variable 'PATH'.\n");
00124   printf("'%s' should know, where to find its initialization files\n",GSEXEC);
00125   printf("and fonts, using an registry entry (Windows) or an environment variable.\n\n");
00126 
00127   printf("Using a different output device, it is also possible to convert eps\n");
00128   printf("files to bitmaps (e.g. -sDEVICE=bmpmono).\n\n");
00129 
00130   printf("Usage: epstopdf [options] filename-of-an-eps-file\n");
00131   printf("Options:\n");
00132   printf("  --help:             print usage\n");
00133   printf("  --outfile=<file>:   write result to <file>\n");
00134   printf("  --tmpfile=<file>:   use <file> as temporary file\n");
00135   printf("  --(no)filter:       read/writ standard input/output (default: false)\n");
00136   printf("  --(no)gs:           run ghostscript                 (default: true)\n");
00137   printf("  --(no)compress:     use compression                 (default: true)\n");
00138   printf("  --(no)hires:        scan HiresBoundingBox           (default: false)\n");
00139   printf("  --(no)exact:        scan ExactBoundingBox           (default: false)\n");
00140   printf("  --(no)debug:        debug informations              (default: false)\n");
00141   printf("  --(no)wait:         wait for keystroke              (default: false)\n");
00142   printf("  --gsexec=<gs>:      use <gs> to invoke ghostscript  (default: %s)\n",GSEXEC);
00143   printf("  --enlarge=<a>:      enlarge BB by <a>/72 ''         (default: 0.)\n");
00144   printf("  -sDEVICE=<dev> :    use <dev> as output device      (default: pdfwrite)\n");
00145   printf("  -r<a>:              output resolution for gs        (default: 600)\n");
00146   printf("  --width=<a>:        output width in pixels          (default: none)\n");
00147   printf("  --height=<a>:       output height in pixels         (default: none)\n");
00148   printf("  --gsopt=<a>:        add <a> to the gs command line  (default: none)\n");
00149 }
00150 
00151 
00152 static int round(double a)
00153 
00154 {
00155   return floor(a+0.5);
00156 }
00157 
00158 static int isint(double a)
00159 
00160 {
00161   if(fabs(a-round(a))<1e-6) return 1;
00162   else                      return 0;
00163 }
00164 
00165 
00166 int main(int argc,char *argv[])
00167 
00168 {
00169   FILE *in,*out;
00170   char *infile=NULL,*outfile=NULL,*tmpfile=NULL,*copt,*ptr,*gsexec,buf[21];
00171   char command[MAX_IN],ch[MAX_IN],*BBname,*device,ropt[20],*gsopt;
00172   double bbllx,bblly,bburx,bbury,xoffset,yoffset,enlarge;
00173   int    width,height,res,xpix,ypix;
00174   int i,count,status,BBCorrected,did_CR,debug,compress,usegs,wait,
00175     BBhires,BBexact,found,depth,filter,ngsopt;
00176   int doseps;
00177   unsigned long dosepsbegin, dosepsend, pos;
00178   unsigned int c;
00179   fpos_t  fpos;
00180 
00181 
00182 #if defined (__EMX__)
00183   _wildcard (&argc, &argv);
00184 #endif
00185 
00186   /* default parameter */
00187   status=0;
00188   BBCorrected=0;
00189   BBhires=0;
00190   BBexact=0;
00191   debug=0;
00192   compress=1;
00193   usegs=1;
00194   gsexec=GSEXEC;
00195   wait=0;
00196   filter=0;
00197   enlarge=0.;
00198   res=600;
00199   device="pdfwrite";
00200   xpix=0;
00201   ypix=0;
00202   ngsopt=100;
00203   gsopt=malloc(ngsopt);
00204   gsopt[0]='\0';
00205 
00206 
00207   /******************************************************************
00208 
00209                                                              Process command line options
00210 
00211   ******************************************************************/
00212 
00213   for(i=1;i<argc;i++){
00214 
00215     /* printf("i=%d  arg=>>%s<<\n",i,argv[i]); */
00216 
00217     if(!strcmp(argv[i],"--help") || !strcmp(argv[i],"-h")){
00218       usage();
00219       status=0;
00220       goto EXIT;
00221     }
00222 
00223     if(!strcmp(argv[i],"--debug") || !strcmp(argv[i],"-d")){
00224       debug=1;
00225       continue;
00226     }
00227 
00228     if(!strcmp(argv[i],"--nodebug")){
00229       debug=0;
00230       continue;
00231     }
00232 
00233     if(!strcmp(argv[i],"--compress") || !strcmp(argv[i],"-c")){
00234       compress=1;
00235       continue;
00236     }
00237 
00238     if(!strcmp(argv[i],"--nocompress")){
00239       compress=0;
00240       continue;
00241     }
00242 
00243     if(!strcmp(argv[i],"--nogs")){
00244       usegs=0;
00245       continue;
00246     }
00247 
00248     if(!strcmp(argv[i],"--gs")){
00249       usegs=1;
00250       continue;
00251     }
00252 
00253     if(!strcmp(argv[i],"--wait")  || !strcmp(argv[i],"-w")){
00254       wait=1;
00255       continue;
00256     }
00257 
00258     if(!strcmp(argv[i],"--nowait")){
00259       wait=0;
00260       continue;
00261     }
00262 
00263     if(!strcmp(argv[i],"--filter")){
00264       filter=1;
00265       continue;
00266     }
00267 
00268     if(!strcmp(argv[i],"--nofilter")){
00269       filter=0;
00270       continue;
00271     }
00272 
00273     if(!strcmp(argv[i],"--hires")){
00274       BBhires=1;
00275       continue;
00276     }
00277 
00278     if(!strcmp(argv[i],"--nohires")){
00279       BBhires=0;
00280       continue;
00281     }
00282 
00283     if(!strcmp(argv[i],"--exact")){
00284       BBexact=1;
00285       continue;
00286     }
00287 
00288     if(!strcmp(argv[i],"--noexact")){
00289       BBexact=0;
00290       continue;
00291     }
00292 
00293     if(!strncmp(argv[i],"--outfile=",strlen("--outfile="))){
00294       outfile=malloc(strlen(argv[i])-strlen("--outfile=")+1);
00295       strcpy(outfile, argv[i]+strlen("--outfile="));
00296       continue;
00297     }
00298 
00299     if(!strncmp(argv[i],"--tmpfile=",strlen("--tmpfile="))){
00300       tmpfile=malloc(strlen(argv[i])-strlen("--tmpfile=")+1);
00301       strcpy(tmpfile, argv[i]+strlen("--tmpfile="));
00302       continue;
00303     }
00304 
00305     if(!strncmp(argv[i],"-r",strlen("-r"))){
00306       sscanf(argv[i]+strlen("-r"),"%d",&res);
00307       continue;
00308     }
00309 
00310     if(!strncmp(argv[i],"--width=",strlen("--width="))){
00311       sscanf(argv[i]+strlen("--width="),"%d",&xpix);
00312       continue;
00313     }
00314 
00315     if(!strncmp(argv[i],"--height=",strlen("--height="))){
00316       sscanf(argv[i]+strlen("--height="),"%d",&ypix);
00317       continue;
00318     }
00319 
00320     if(!strncmp(argv[i],"--gsopt=",strlen("--gsopt="))){
00321       char *opt=argv[i]+strlen("--gsopt=");
00322       if(strlen(gsopt)+strlen(opt)+2 < ngsopt){
00323        ngsopt+=100;
00324        gsopt=realloc(gsopt,ngsopt);
00325       }
00326       strcat(gsopt," ");
00327       strcat(gsopt,opt);
00328       continue;
00329     }
00330 
00331     if(!strncmp(argv[i],"-sDEVICE=",strlen("-sDEVICE="))){
00332       device=argv[i]+strlen("-sDEVICE=");
00333       continue;
00334     }
00335 
00336     if(!strcmp(argv[i],"-o") && i+1<argc){
00337       outfile=malloc(strlen(argv[i+1])+1);
00338       strcpy(outfile, argv[i+1]);
00339       i++;
00340       continue;
00341     }
00342 
00343     if(!strncmp(argv[i],"--gsexec=",strlen("--gsexec="))){
00344       gsexec=argv[i]+strlen("--gsexec=");
00345       continue;
00346     }
00347 
00348 
00349     if(argv[i][0]!='-'){
00350       if(infile) printf("\nCan process only one input file\n");
00351       else       infile=argv[i];
00352       continue;
00353     }
00354 
00355     if(!strncmp(argv[i],"--enlarge=",strlen("--enlarge="))){
00356       sscanf(argv[i]+strlen("--enlarge="),"%lf",&enlarge);
00357       continue;
00358     }
00359 
00360     usage();
00361     fprintf(stderr,"\nunknown option >>%s<<\n",argv[i]);
00362     status=1;
00363     goto EXIT;
00364   }
00365 
00366 
00367 
00368   /******************************************************************
00369 
00370                                                              check arguments and files
00371 
00372   ******************************************************************/
00373 
00374 
00375   if(filter) debug=0;
00376   if(filter) wait =0;
00377 
00378   if(BBexact && BBhires){
00379     fprintf(stderr,"\nOptions --hires and --exact cannot be used together\n");
00380     status=1;
00381     goto EXIT;
00382   }
00383 
00384   if     (BBexact) BBname="%%ExactBoundingBox:";
00385   else if(BBhires) BBname="%%HiresBoundingBox:";
00386   else             BBname="%%BoundingBox:";  
00387 
00388   if(!filter) {
00389 
00390     if(!infile) {
00391       usage();
00392       fprintf(stderr,"no input file specified!\n");
00393       status=1;
00394       goto EXIT;
00395     }
00396 
00397     if((in=fopen(infile,"rb")) == NULL) {
00398       usage();
00399       fprintf(stderr,"%s: File not found!\n",infile);
00400       status=1;
00401       goto EXIT;
00402     }
00403     fclose(in);
00404 
00405   }else{
00406 
00407     if(infile) {
00408       fprintf(stderr,"Input file cannot be used with filter option!\n");
00409       status=1;
00410       goto EXIT;
00411     }
00412 
00413     infile="epstopdf";   /* dummy input filename to generate tmp-filename */
00414   }
00415 
00416 
00417 
00418   /* find a temporary filename that does not exist yet */
00419   if(usegs && !tmpfile){
00420     tmpfile=malloc(strlen(infile)+9);
00421     count=0;
00422     do{
00423       if(count>99) {
00424        fprintf(stderr,"No temporary file available! Try deleting *.tmp.\n\n");
00425        status=1;
00426        goto EXIT;
00427       }
00428       sprintf(tmpfile,"%s.%d.tmp",infile,count);
00429       if(debug) printf("checking temporary filename >>%s<<\n",tmpfile);
00430       out=fopen(tmpfile,"rb");
00431       if(out) fclose(out);
00432       count++;
00433     }while(out);
00434   }
00435 
00436   if(!filter){
00437     if(!outfile){
00438       outfile=malloc(strlen(infile)+6);
00439       strcpy(outfile,infile);
00440       ptr=outfile;
00441       while(strpbrk(ptr,"\\/")) ptr=strpbrk(ptr,"\\/")+1;
00442       ptr=strrchr(ptr,'.');
00443       if(ptr) *ptr='\0';
00444       if(usegs) strcat(outfile,".pdf");
00445       else      strcat(outfile,"2.eps");
00446     }
00447   }else{
00448     if(outfile) {
00449       fprintf(stderr,"Output file cannot be used with filter option!\n");
00450       status=1;
00451       goto EXIT;
00452     }
00453     outfile=malloc(2);
00454     strcpy(outfile,"-");
00455   }
00456 
00457 
00458 
00459   if(!filter) printf("Converting %s to %s ..... ",infile,outfile);
00460 
00461 
00462   /******************************************************************
00463 
00464   put the pagesize from the bounding box into the eps file
00465 
00466   ******************************************************************/
00467 
00468        
00469   if(debug) printf("\nAdding correct pagesize to EPS ... searching for %s ...\n",BBname);
00470 
00471   if     (!filter) in  = fopen(infile, "rb");
00472   else {
00473     in  = stdin; 
00474     setmode(fileno(stdin), O_BINARY);
00475   }
00476   if     (usegs)   out = fopen(tmpfile,"wb");
00477   else if(!filter) out = fopen(outfile,"wb");
00478   else {
00479     out = stdout;
00480     setmode(fileno(stdout), O_BINARY);
00481   }
00482   if(!in || !out){
00483     fprintf(stderr,"cannot open files\n");
00484     status=1;
00485     goto EXIT;
00486   }
00487   depth=0;
00488 
00489   doseps = dosepsbegin = dosepsend = 0;
00490   c = getc(in);
00491   if (c == 'E'+0x80) {
00492     if ((getc(in)=='P'+0x80) && (getc(in)=='S'+0x80)
00493        && (getc(in)=='F'+0x80)) {
00494       doseps = 1;
00495       dosepsbegin = getc(in) ;
00496       dosepsbegin += getc(in) * 256L ;
00497       dosepsbegin += getc(in) * 65536L ;
00498       dosepsbegin += getc(in) * 256L * 65536 ;
00499       dosepsend = getc(in) ;
00500       dosepsend += getc(in) * 256L ;
00501       dosepsend += getc(in) * 65536L ;
00502       dosepsend += getc(in) * 256L * 65536 ;
00503       fseek(in, dosepsbegin, 0);
00504       c = getc(in);
00505       dosepsend-- ;
00506     }
00507   }
00508   rewind(in);
00509   if (doseps)
00510     fseek(in, dosepsbegin, SEEK_SET);
00511   pos = dosepsbegin;
00512   while (fgets(ch,MAX_IN,in)) {
00513     if (doseps && ftell(in) > dosepsend + dosepsbegin) {
00514       ch[dosepsend + dosepsbegin - pos ] = '\0';
00515       fputs(ch, out); fputc('\n', out);
00516       break;
00517     }
00518     pos = ftell(in);    
00519     
00520     if(!strncmp(ch,BEGINDOC,strlen(BEGINDOC))) depth++;   /* count included documents */
00521     if(!strncmp(ch,ENDDOC,  strlen(ENDDOC)  )) depth--;
00522     if(!strncmp(ch,BBname,  strlen(BBname)) && depth==0) { /* look for BB comment in main doc only */
00523       sscanf(ch,"%*s %20s",buf);
00524       if(!strcmp(buf,"(atend)")){                       /* BB is atended */  
00525        if(filter){
00526          fprintf(stderr,"Cannot look for BoundingBox in the trailer "
00527                 "with option --filter\n");
00528          if(usegs) remove(tmpfile);
00529          status=1;
00530          goto EXIT;
00531        }
00532        if(debug) printf("\n (atend)! ...\n");
00533        fgetpos(in, &fpos); /* store file position */
00534        found=0;  
00535        while (fgets(ch,MAX_IN,in)){
00536          if(!strncmp(ch,BEGINDOC,strlen(BEGINDOC))) depth++;   /* count included documents */
00537          if(!strncmp(ch,ENDDOC,  strlen(ENDDOC)  )) depth--;
00538          if(!strncmp(ch,BBname,strlen(BBname)) && depth==0) { /* look for bounding box in main doc only */
00539            found=1;
00540            fsetpos(in, &fpos);  /* rewind to (atend) comment */
00541            break;
00542          }    
00543        } 
00544        if(!found){
00545          fprintf(stderr,"atended %s not found\n",BBname);
00546          if(usegs  && !debug) remove(tmpfile);
00547          if(!usegs && !debug) remove(outfile);
00548          status=1;
00549          goto EXIT;
00550        } 
00551       }
00552 
00553 
00554       /* No Idea what ExactBoundingBox means. Hope it also works with this code */
00555 
00556       /* I thought Postscript says that the bounding box should be integer.
00557         However, some applications (like Corel Draw) use floats and gs has no
00558         problem with it. So I use floats for translate that will result in a
00559         more exact box. Since gs seems not to be able to use floats in 
00560         setpagedevice, these values are converted to integer */   
00561 
00562       if(!BBCorrected){ /* write Bounding box one time only! */
00563        if(sscanf(ch,"%*s %lf %lf %lf %lf",&bbllx,&bblly,&bburx,&bbury)!=4){
00564          fprintf(stderr,"incorrect %s \n",BBname);
00565          if(usegs && !debug) remove(tmpfile);
00566          status=1;
00567          goto EXIT;
00568        }
00569        if(debug) printf("BoundingBox: %f %f %f %f\n",bbllx,bblly,bburx,bbury);
00570        bblly  -= enlarge;
00571        bbllx  -= enlarge;
00572        bbury  += enlarge;
00573        bburx  += enlarge;
00574        width   = ceil(bburx-bbllx);  /* make papersize integer and enlarge it a little bit */
00575        height  = ceil(bbury-bblly);
00576        xoffset =-bbllx;
00577        yoffset =-bblly;
00578        fprintf(out,"%s %d %d %d %d\n",BBname,0,0,width,height);
00579        fprintf(out,"<< /PageSize [%d %d] >> setpagedevice \n",width,height);
00580        if(isint(xoffset) && isint(yoffset)) fprintf(out,"gsave %d %d translate\n",round(xoffset),round(yoffset));
00581        else                                 fprintf(out,"gsave %f %f translate\n",xoffset,yoffset);
00582        if(!filter) printf(" (%dx%d mm) ... ",(int)(25.4/72.*width),(int)(25.4/72.*height));
00583        did_CR=1;
00584        BBCorrected=1;
00585       }
00586     }else{
00587       fputs(ch,out);
00588       if(strpbrk(ch, "\n")) did_CR=1;
00589       else                  did_CR=0;
00590     }
00591   }
00592   if(BBCorrected){
00593     if(!did_CR) fprintf(out,"\ngrestore\n");
00594     else        fprintf(out,  "grestore\n");
00595   }
00596   if(in !=stdin ) fclose(in);
00597   if(out!=stdout) fclose(out);
00598 
00599   if(width && height){
00600     if      (xpix) res=(72*xpix)/width;
00601     else if (ypix) res=(72*ypix)/height;
00602   }
00603 
00604 
00605   /******************************************************************
00606 
00607                                                              do the conversion eps->pdf using gs
00608 
00609   ******************************************************************/
00610 
00611 
00612   if(usegs){
00613 
00614 #ifdef FPTEX
00615     char stemp[512];
00616     char *gs_argv[] = {
00617       NULL,          /* 0 gsexec */
00618       "-q",
00619       "-dNOPAUSE",
00620       "-dBATCH",
00621       NULL,          /* 4 copt */
00622       NULL,          /* 5 device */
00623       NULL,          /* 6 ropt */
00624       NULL,          /* 7 gsopt */
00625       NULL,          /* 8 outfile */
00626       /*          "-", "-c", "quit", "-f", */
00627       NULL           /* 9 tmpfile */,
00628       NULL
00629     };
00630     int gs_argc = sizeof(gs_argv)/sizeof(gs_argv[0]);
00631 
00632     gs_argv[0] = gsexec;
00633     if(compress) gs_argv[4]="-dUseFlateCompression=true";
00634     else         gs_argv[4]="-dUseFlateCompression=false";
00635 
00636     if(res)      sprintf(ropt,"-r%d",res);
00637     else         ropt[0]='\0';
00638 
00639     if(res && debug) printf(" (%d dpi) ... ",res);
00640 
00641     sprintf(stemp, "-sDEVICE=%s", device);
00642     gs_argv[5] = xstrdup(stemp);
00643     gs_argv[6] = ropt;
00644     if (gsopt && *gsopt) {
00645       gs_argv[7] = gsopt;
00646       sprintf(stemp, "-sOutputFile=%s", outfile);
00647       gs_argv[8] = xstrdup(stemp);
00648       sprintf(stemp, "%s", tmpfile);
00649       gs_argv[9] = xstrdup(stemp);
00650     }
00651     else {
00652       sprintf(stemp, "-sOutputFile=%s", outfile);
00653       gs_argv[7] = xstrdup(stemp);
00654       sprintf(stemp, "%s", tmpfile);
00655       gs_argv[8] = xstrdup(stemp);
00656       gs_argc--;
00657     }
00658     if(debug) {
00659       int i;
00660       printf("running ghostscript ...\n");
00661       for (i = 0; i < gs_argc - 1; i++)
00662        fprintf(stderr, "gs_argv[%d] = %s\n", i, gs_argv[i]);
00663     }
00664 
00665     if (gs_locate() == NULL) {
00666       fprintf(stderr, "\nCan't locate Ghostscript ! Exiting ...\n");
00667       return EXIT_FAILURE;
00668     }
00669 
00670     if (!gs_dll_initialize()) {
00671       fprintf(stderr, "Can't initialize ghostscript, exiting ...\n");
00672       return EXIT_FAILURE;
00673     }
00674         
00675     if ((status = (*pgsdll_init)(gsdll_callback,
00676                              NULL,
00677                              gs_argc - 1,
00678                              gs_argv)) == 0) {
00679       /* Should not happen : gs should quit right after being initialized. */
00680       /* fprintf(stderr, "gs exited quickly.\n"); */
00681       (*pgsdll_exit)();
00682     }
00683         
00684     if (status != 0 && status != GSDLL_INIT_QUIT)
00685       fprintf(stderr, "gsdll_init returned %d\n", status);
00686         
00687     gs_dll_release();
00688         
00689     free(gs_argv[5]);
00690 
00691 #if 1
00692     if(!debug) remove(tmpfile);
00693     else       printf("keeping temporary file >>%s<<\n",tmpfile);
00694 #else
00695     remove(tmpfile);
00696 #endif
00697 
00698 #else
00699     if(compress) copt="-dUseFlateCompression=true";
00700     else         copt="-dUseFlateCompression=false";
00701 
00702     if(res)      sprintf(ropt,"-r%d",res);
00703     else         ropt[0]='\0';
00704 
00705     if(res && debug) printf(" (%d dpi) ... ",res);
00706 
00707     sprintf(command,"%s -q -dNOPAUSE -dBATCH %s -sDEVICE=%s %s %s"
00708            " \"-sOutputFile=%s\" -c save pop -f \"%s\"",
00709            gsexec,copt,device,ropt,gsopt,outfile,tmpfile);
00710     if(debug) printf("running ghostscript ...\n");
00711     if(debug) puts(command);
00712     status=system(command);
00713     if(!debug) remove(tmpfile);
00714     else       printf("keeping temporary file >>%s<<\n",tmpfile);
00715 #endif /* ! FPTEX */
00716   }
00717        
00718   if(!filter) printf("Done\n");
00719 
00720 
00721 
00722  EXIT:
00723 
00724   free(outfile);
00725   free(tmpfile);
00726   free(gsopt);
00727 
00728   if(wait){
00729     printf("\n<Press a key> ");
00730     getchar();
00731     printf("\n");
00732   }
00733 
00734   return status;
00735 }