home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 376.lha / EPS2EPSI_v0.9 / eps2epsi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-01  |  6.9 KB  |  367 lines

  1.  
  2. /*
  3.     eps2epsi  Version 0.9
  4.  
  5.     EPS converts Encapsulated PostScript files into Encapsulated
  6.     Postscript Interchange files.  This means, it adds a simple
  7.     bitmap representation of the postscript file so that the 
  8.     programs which allow ESP to be imported can easily show
  9.     what is being included.
  10.  
  11.     Copyright 1990, Integrated Systems, Inc.
  12.     Written by Chris Nicotra
  13.     All rights reserved.
  14.  
  15.     This program requires Adrian Aylward's post v1.1 library
  16. */
  17.  
  18. #include <stdio.h>
  19. #include <exec/types.h>
  20. #include <exec/exec.h>
  21. #include <exec/execbase.h>
  22. #include <exec/tasks.h>
  23. #include <graphics/gfx.h>
  24. #include <graphics/gfxbase.h>
  25. #include <graphics/text.h>
  26. #include <libraries/dos.h>
  27. #include "postlib.h"
  28.  
  29. /* Assembler routines */
  30. extern void insertftrap(void);
  31. extern void deleteftrap(void);
  32.  
  33. struct GfxBase *GfxBase;
  34. struct IntuitionBase *IntuitionBase;
  35. struct library *PSbase;
  36.  
  37. struct PSparm parm;
  38. int ftrapset;
  39.  
  40. struct BitMap bmap;
  41.  
  42. int arec;
  43.  
  44. extern void __saveds __asm flushpage(register __d0 int y1,
  45.                                      register __d1 int y2);
  46. extern void __saveds __asm copypage(register __d0 int num);
  47.  
  48. FILE *ofp;
  49. int ow,bpr;
  50.  
  51. main(argc,argv)
  52. int argc;
  53. char *argv[];
  54. {
  55.     FILE *ifp;
  56.     char buf[100];
  57.     double x1,x2,y1,y2;
  58.     int bnd;
  59.     int w,h;
  60.     int i;
  61.  
  62.     char *infile,*outfile;
  63.     char *ptr;
  64.  
  65.     /* ------- Decode the command line options ------- */
  66.     infile = outfile = NULL;
  67.  
  68.     /*
  69.         This is overkill right now, but provides the frame
  70.         work for adding other options later.
  71.     */
  72.     while ((--argc) > 0)
  73.     {
  74.         ptr = *(++argv);
  75.  
  76.         if (*ptr == '-')
  77.         {
  78.             switch (*(++ptr))
  79.             {
  80.             case 'o':
  81.                 if (outfile)
  82.                 {
  83.                     fprintf(stderr,"Too many output file names\n");
  84.                     fflush(stderr);
  85.                     exit(0);
  86.                 }
  87.  
  88.                 --argc;
  89.                 outfile = *(++argv);
  90.                 break;
  91.  
  92.             default:
  93.                 fprintf(stderr,"Invalid option -%c\n",*ptr);
  94.                 fflush(stderr);
  95.                 break;
  96.             }
  97.         }
  98.         else
  99.         {
  100.             /* This must be the input file name */
  101.             if (infile)
  102.             {
  103.                 fprintf(stderr,"Too many input file names\n");
  104.                 fflush(stderr);
  105.                 exit(0);
  106.             }
  107.  
  108.             infile = ptr;
  109.         }
  110.     }
  111.  
  112.     if (!infile)
  113.     {
  114.         fprintf(stderr,"No input file was specified\n");
  115.         fflush(stderr);
  116.         exit(0);
  117.     }
  118.  
  119.     if (!(ifp = fopen(infile,"r")))
  120.     {
  121.         fprintf(stderr,"Unable to open input file '%s'\n",infile);
  122.         fflush(stderr);
  123.         exit(0);
  124.     }
  125.  
  126.     /* ------- Make sure this is an ESP file ------- */
  127.  
  128.     if (!fgets(buf,100,ifp))
  129.     {
  130.         fprintf(stderr,"File is empty\n");
  131.         fflush(stderr);
  132.         goto do_exit;
  133.     }
  134.  
  135.     if (strncmp(buf,"%!PS-Adobe-",10))
  136.     {
  137.         fprintf(stderr,"File '%s' is not an EPS file\n",infile);
  138.         fflush(stderr);
  139.         goto do_exit;
  140.     }
  141.  
  142.     bnd = 0;
  143.  
  144.     /* ------- OK, look for the bounding box ------- */
  145.  
  146.     while (fgets(buf,100,ifp))
  147.     {
  148.         if (!strncmp(buf,"%%BoundingBox:",14))
  149.         {
  150.             if (sscanf(buf+14,"%lf %lf %lf %lf",&x1,&y1,&x2,&y2) != 4)
  151.             {
  152.                 fprintf(stderr,"Unable to decode the bounding box\n");
  153.                 fflush(stderr);
  154.                 goto do_exit;
  155.             }
  156.              
  157.             bnd = -1;
  158.         }
  159.  
  160.         if (!strncmp(buf,"%%BeginImage:",13))
  161.         {
  162.             fprintf(stderr,"This file already contains an image\n");
  163.             fflush(stderr);
  164.             goto do_exit;
  165.         }
  166.     }
  167.  
  168.     if (!bnd)
  169.     {
  170.         fprintf(stderr,"No bounding box\n");
  171.         fflush(stderr);
  172.         goto do_exit;
  173.     }
  174.  
  175.     /* ------- Open all of the libraries ------- */
  176.  
  177.     /*
  178.         Don't bother to check, these if they can't be opened we're in
  179.         more trouble than an error message is going to fix
  180.     */
  181.  
  182.     GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 0);
  183.     IntuitionBase =
  184.         (struct IntuitionBase *) OpenLibrary("intuition.library", 0);
  185.  
  186.     if (!(PSbase = OpenLibrary("post.library", 0)))
  187.     {
  188.         printf("Unable to open PostScript library\n");
  189.         goto do_exit;
  190.     }
  191.  
  192.     /* ------- Setup the POST parameter block ------- */
  193.  
  194.     /* Round the width to an even 8 bits */
  195.     w = x2 - x1;
  196.     w += 15;
  197.     w &= ~0x0f;
  198.  
  199.     ow = x2 - x1;
  200.     ow += 7;
  201.     ow &= ~0x07;
  202.     bpr = ow/8;
  203.  
  204.     h = y2 - y1;
  205.  
  206.     parm.page.xsize = w;
  207.     parm.page.ysize = h;
  208.     parm.page.xden = parm.page.yden = 72;
  209.  
  210.     parm.page.ydir = -1;
  211.     parm.page.depth = 1;
  212.  
  213.     /* Set up the default memory sizes */
  214.     parm.memvlen = 280000;
  215.     parm.memflen =  60000;
  216.     parm.memllen =  60000;
  217.     parm.memhlen =  20000;
  218.  
  219.     parm.page.ybase = 0;
  220.     parm.page.yheight = parm.page.ysize;
  221.  
  222.     parm.page.xbytes = (parm.page.xsize + 15) >> 3 & 0xfffffffe;
  223.     parm.page.len = parm.page.xbytes * parm.page.ysize;
  224.  
  225.     if (!(parm.page.buf[0] = AllocMem(parm.page.len,MEMF_CHIP|MEMF_CLEAR)))
  226.     {
  227.         printf("Can't allocate the bitmap\n");
  228.         goto do_exit;
  229.     }
  230.  
  231.     parm.flushfunc = (APTR) flushpage;
  232.     parm.copyfunc = (APTR) copypage;
  233.     parm.infh = Input();
  234.     parm.outfh = Output();
  235.     parm.errfh = Output();
  236.  
  237.     /* Setup the bitmap */
  238.     bmap.BytesPerRow = parm.page.xbytes;
  239.     bmap.Rows = parm.page.ysize;
  240.     bmap.Flags = 0;
  241.     bmap.Depth = 1;
  242.     bmap.Planes[0] = parm.page.buf[0];
  243.  
  244.     /* Setup the floating point trap */
  245.     insertftrap();
  246.     ftrapset = 1;
  247.  
  248.     if (!(arec = PScreateact(&parm)))
  249.     {
  250.         fprintf(stderr,"Unable to create a POST activation\n");
  251.         fflush(stderr);
  252.         goto do_exit;
  253.     }
  254.  
  255.     /* Start at the beginning of the input file */
  256.     rewind(ifp);
  257.  
  258.     if (outfile)
  259.     {
  260.         if (!(ofp = fopen(outfile,"w")))
  261.         {
  262.             fprintf(stderr,"Unable to open output file\n");
  263.             fflush(stderr);
  264.             goto do_exit;
  265.         }
  266.     }
  267.     else
  268.         ofp = stdout;
  269.  
  270.     /*
  271.         Scan the input until the position where to preview bitmap is
  272.         to be placed.  Then generate and output the preview image
  273.         and continue.  As the input file is read, it is copied into
  274.         the output file.
  275.     */
  276.     while (fgets(buf,100,ifp))
  277.     {
  278.         fprintf(ofp,"%s",buf);
  279.  
  280.         if (!strncmp(buf,"%%EndComments",13))
  281.         {
  282.             /* ------- Execute the EPS file ------- */
  283.             i=PSintstring(arec,infile,-1,PSFLAGFILE|PSFLAGCLEAR|PSFLAGERASE);
  284.  
  285.             if (i)
  286.             {
  287.                 fprintf(stderr,"POST returned error %d\n",i);
  288.                 fflush(stderr);
  289.             }
  290.         }
  291.     }
  292.  
  293.     /* All done! */
  294.  
  295. do_exit:
  296.  
  297.     if (arec)
  298.         PSdeleteact(arec);
  299.  
  300.     if (ifp)
  301.         fclose(ifp);
  302.  
  303.     if (outfile && ofp)
  304.         fclose(ofp);
  305.  
  306.     if (GfxBase)
  307.         CloseLibrary(GfxBase);
  308.  
  309.     if (IntuitionBase)
  310.         CloseLibrary(IntuitionBase);
  311.  
  312.     if (PSbase)
  313.         CloseLibrary(PSbase);
  314.  
  315.     if (parm.page.buf[0])
  316.         FreeMem(parm.page.buf[0],parm.page.len);
  317.  
  318.     if (ftrapset)
  319.         deleteftrap();
  320.  
  321.     exit(0);
  322. }
  323.  
  324.  
  325. void __saveds sigint()
  326. {
  327.     PSsignalint(arec, 1);
  328. }
  329.  
  330. /* Signal a floating point error */
  331. void __saveds sigfpe()
  332. {
  333.     PSsignalfpe(arec);
  334. }
  335.  
  336. void __saveds __asm flushpage(register __d0 int y1, register __d1 int y2)
  337. {
  338. }
  339.  
  340. /* Copy the page to the output */
  341. void __saveds __asm copypage(register __d0 int num)
  342. {
  343.     int i,j;
  344.     char *ptr;
  345.  
  346.     fprintf(ofp,"%%%%BeginPreview\n");
  347.     fprintf(ofp,"%%%%ImageWidth:%d\n",ow);
  348.     fprintf(ofp,"%%%%ImageHeight:%d\n",bmap.Rows);
  349.     fprintf(ofp,"%%%%ImageDepth: 1\n");
  350.     fprintf(ofp,"%%%%BeginImage:%d\n",bmap.Rows);
  351.  
  352.     for (i=0;i<bmap.Rows;i++)
  353.     {
  354.         ptr = bmap.Planes[0] + (i*bmap.BytesPerRow);
  355.  
  356.         fprintf(ofp,"%% ");
  357.  
  358.         for (j=0;j<bpr;j++)
  359.             fprintf(ofp,"%02x",0x0ff & (*(ptr++)));
  360.         fprintf(ofp,"\n");
  361.     }
  362.  
  363.     fprintf(ofp,"%%%%EndImage\n");
  364.     fprintf(ofp,"%%%%EndPreview\n");
  365. }
  366.  
  367.