home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / tif2ps / part01 / tif2ps.c < prev   
C/C++ Source or Header  |  1989-02-03  |  5KB  |  213 lines

  1. /*
  2.  * tif2ps/tifdump -- convert TIFF to PostScript
  3.  *
  4.  * written by:
  5.  * Andreas Lampen, TU-Berlin (andy@coma.UUCP)
  6.  *                 (andy@db0tui62.BITNET)
  7.  *
  8.  * Copyright (C) 1988 by the author.
  9.  * Permission is granted to copy and distribute this program
  10.  * without charge, provided this copyright notice is included
  11.  * in the copy.
  12.  * This Software is distributed on an as-is basis. There will be
  13.  * ABSOLUTELY NO WARRANTY for any part of this software to work
  14.  * correct. In no case will the author be liable to you for damages
  15.  * caused by the usage of this software.
  16.  */
  17.  
  18. /*
  19.  * tif2ps.c -- main program 
  20.  *
  21.  * $Header: tif2ps.c[1.0] Thu Dec 29 20:11:01 1988 andy@coma published $
  22.  */
  23.  
  24. #include <stdio.h>
  25. #ifdef MSDOS
  26. #include <string.h>
  27. #include <time.h>
  28. #else
  29. #include <strings.h>
  30. #include <sys/time.h>
  31. #endif
  32. #include <setjmp.h>
  33. #include <signal.h>
  34. #include "defs.h"
  35. #include "tif.h"
  36.  
  37. extern CHAR *optarg;
  38. extern INT  optind;
  39.  
  40. CHAR    *progname, *version();
  41.  
  42. main (ac, av)
  43.      INT   ac;
  44.      CHAR  **av;
  45. {
  46.   INT     getopt(), c, nfiles, i, cleanup();
  47.   VOID    usage(), logerr(), dodump(), genps();
  48.   DOUBLE  xoffset = 0., yoffset= 0., scalefactor = 1., height = 0.;
  49.   DOUBLE  xresfactor, yresfactor, atof();
  50.   PICTURE picts[8]; /* maximal 8 pictures */
  51.   TIFF    tiff;
  52.   jmp_buf env;
  53.  
  54. #ifdef MSDOS
  55.   if (!strrchr (av[0], '\\')) progname = av[0];
  56.     else progname = strrchr (av[0], '\\') + 1;
  57. #else
  58.   if (!rindex (av[0], '/')) progname = av[0];
  59.     else progname = rindex (av[0], '/') + 1;
  60. #endif
  61.  
  62.   if (ac < 2)
  63.     {
  64.       usage ();
  65.       exit (-1);
  66.     }
  67.  
  68.   while ((c = getopt (ac, av, "h:s:vx:y:")) != EOF)
  69.     {
  70.       switch (c)
  71.     {
  72.     case 'h': /* picture height */
  73.       if (scalefactor != 1.)
  74.         logerr (progname, "only one of 'h' or 's' can be given");
  75.       else
  76.         height = atof (optarg) * 72;
  77.       break;
  78.     case 's': /* scale factor */
  79.       if (height != 0.)
  80.         logerr (progname, "only one of 'h' or 's' can be given");
  81.       else
  82.         scalefactor = atof (optarg);
  83.       break;
  84.     case 'v': /* print current version of this program */
  85.       printf ("This is %s version %s.\n", av[0], version());
  86.       exit (0);
  87.     case 'x': /* xposition (in inches) */
  88.       xoffset = atof (optarg) * 72;
  89.       break;
  90.     case 'y': /* yposition (in inches) */
  91.       yoffset = atof (optarg) * 72;
  92.       break;
  93.     default:
  94.       usage ();
  95.       exit (-1);
  96.     }
  97.     }  /* end of command line parsing */
  98.  
  99.   (VOID) signal (SIGINT, cleanup);
  100.  
  101.   nfiles = ac - optind;
  102.   for (i = 0; i < nfiles; i++)
  103.     {
  104.       /* (VOID) setjmp (env); --- not used */
  105.       if (scantif (av[i+optind], picts, &tiff) == ERROR)
  106.     { logerr (progname, "cannot read file"); exit (-1); }
  107.       if (!strcmp (progname, "tifdump"))
  108.     {
  109.       printf ("This is %s version %s.\n\n", progname, version());
  110.       dodump (av[i+optind], &tiff);
  111.       continue;
  112.     }
  113.       /* else do initializations */
  114.       /* a loop through all pictures should be inserted here */
  115.  
  116.       /* adjust bytecount ???? perhaps not correct */
  117.       /* manages only single strip pictures */
  118.       if (picts[0].image.strips[0].byteCount == 0)
  119.     picts[0].image.strips[0].byteCount = 
  120.       (picts[0].image.imWidth * picts[0].image.imLength) / 
  121.         (8 / picts[i].image.bitsPerSample);
  122.  
  123.       switch (picts[0].physWorld.resUnit)
  124.     {
  125.     case 1: /* special */
  126.       xresfactor = 1. / picts[0].physWorld.xRes;
  127.       yresfactor = 1. / picts[0].physWorld.yRes;
  128.       break;
  129.     case 2: /* inches */
  130.       xresfactor = 72. / picts[0].physWorld.xRes;
  131.       yresfactor = 72. / picts[0].physWorld.yRes;
  132.       break;
  133.     case 3: /* centimetres */
  134.       xresfactor = 28.3465 / picts[0].physWorld.xRes;
  135.       yresfactor = 28.3465 / picts[0].physWorld.yRes;
  136.       break;
  137.     }
  138.       
  139.       if (height != 0.)
  140.     scalefactor = (height / (picts[0].image.imLength * yresfactor));
  141.       
  142.       if (xoffset != 0.)
  143.     picts[0].context.xPos = xoffset;
  144.       if (yoffset != 0.)
  145.     picts[0].context.yPos = yoffset;
  146.       
  147.       picts[0].context.xMax = 
  148.     (picts[0].image.imWidth * xresfactor * scalefactor) +
  149.       picts[0].context.xPos;
  150.       picts[0].context.yMax = 
  151.     (picts[0].image.imLength * yresfactor * scalefactor) + 
  152.       picts[0].context.yPos;
  153.       
  154.       genps (av[i+optind], picts);
  155.     }
  156. } /* end of main */
  157.  
  158. LOCAL INT cleanup ()
  159. {
  160. #ifdef MSDOS
  161.   cprintf ("cleanup...\n");
  162. #else
  163.   fprintf (stderr, "cleanup...\n");
  164. #endif
  165.   exit (-1);
  166. }
  167.  
  168. LOCAL VOID usage ()
  169. {
  170.   printf ("usage: %s [-h <height>] [-s <scalefactor>]\n", progname);
  171.   printf ("\t\t[-v] [-x <x-offset>] [-y <y-offset>] file1 ...\n");
  172. }
  173.  
  174. EXPORT VOID logerr (routine, msg)
  175.      CHAR *routine, *msg;
  176. {
  177. #ifdef MSDOS
  178.   cprintf ("%s(%s): %s\n", progname, routine, msg);
  179. #else
  180.   fprintf (stderr, "%s(%s): %s\n", progname, routine, msg);
  181. #endif
  182. }
  183.  
  184. EXPORT CHAR *date()
  185. {
  186.   LONG seconds, time();
  187. #ifndef MSDOS
  188.   CHAR *asctime();  
  189. #endif
  190.  
  191. #ifdef MSDOS
  192.   time (&seconds);
  193.   return (ctime (&seconds));
  194. #else 
  195.   seconds = time ((LONG *) 0);
  196.   return (asctime (localtime (&seconds)));
  197. #endif
  198. }
  199.  
  200. EXPORT CHAR *caller()
  201. {
  202.   STATIC CHAR name[128];
  203. #ifdef MSDOS
  204.   strcpy (name, ""); /* cannot get hostname and login name */
  205. #else
  206.   CHAR *getlogin (), host[128];
  207.  
  208.   (VOID) gethostname (host, 128);
  209.   (VOID) sprintf (name, "%s@%s\0", getlogin(), host);
  210. #endif
  211.   return (name);
  212. }
  213.