home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fbm / src / fbps.c < prev    next >
C/C++ Source or Header  |  1990-06-24  |  8KB  |  287 lines

  1. /****************************************************************
  2.  * fbps.c: FBM Release 1.0 25-Feb-90 Michael Mauldin
  3.  *
  4.  * Copyright (C) 1989,1990 by Michael Mauldin.  Permission is granted
  5.  * to use this file in whole or in part for any purpose, educational,
  6.  * recreational or commercial, provided that this copyright notice
  7.  * is retained unchanged.  This software is available to all free of
  8.  * charge by anonymous FTP and in the UUNET archives.
  9.  *
  10.  * fbps: Convert a grayscale image to a PostScript file
  11.  *
  12.  * USAGE
  13.  *    % fbps < image > postscript
  14.  *
  15.  * EDITLOG
  16.  *    LastEditDate = Mon Jun 25 00:04:03 1990 - Michael Mauldin
  17.  *    LastFileName = /usr2/mlm/src/misc/fbm/fbps.c
  18.  *
  19.  * HISTORY
  20.  * 25-Jun-90  Michael Mauldin (mlm@cs.cmu.edu) Carnegie Mellon
  21.  *    Package for Release 1.0
  22.  *
  23.  * 26-Aug-89  Paul Milazzo (milazzo) at BBN
  24.  *    Beta release (version 0.96)
  25.  *    Added rotation, big paper option
  26.  *
  27.  * 07-Mar-89  Michael Mauldin (mlm) at Carnegie Mellon University
  28.  *    Beta release (version 0.94) mlm@cs.cmu.edu
  29.  *
  30.  * 25-Apr-89  Paul Milazzo (milazzo) at BBN
  31.  *    Added color postscript support
  32.  *
  33.  * 27-Aug-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  34.  *    Created.
  35.  *****************************************************************/
  36.  
  37. # include <stdio.h>
  38. # include <math.h>
  39. # include "fbm.h"
  40.  
  41. # define PAPERWIDTH 8.5 /* inches */
  42. # define BIGPAPERWIDTH 11 /* inches */
  43. # define PAPERHEIGHT 11 /* inches */
  44. # define BIGPAPERHEIGHT 14 /* inches */
  45.  
  46. char *ps_chars();
  47.  
  48. # define USAGE \
  49. "Usage: fbps [-tT] [-pP] [-b] [-r] [ -s ] [ -w<width> ] < foo.fbm > foo.PS"
  50.  
  51. #ifndef lint
  52. static char *fbmid =
  53. "$FBM fbps.c <1.0> 25-Jun-90  (C) 1989,1990 by Michael Mauldin, source \
  54. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  55. #endif
  56.  
  57. main (argc, argv)
  58. char *argv[];
  59. { register int i, j;
  60.   int rows, cols, rowlen;
  61.   double paperwidth, paperheight;
  62.   double maxwidth, maxheight;
  63.   double width = -1, height, llx, lly;
  64.   int bytcnt=0;
  65.   int bigpaper=0, dotitle=1, dosize=1, rotate=0, scribe=0;
  66.   char buf[BUFSIZ], *title=NULL, *creator=NULL;
  67.   long clock = time ((long *) NULL);
  68.   char *ctime ();
  69.   FBM image;
  70.  
  71.   /* Clear the memory pointer so alloc_fbm won't be confused */
  72.   image.cm  = image.bm  = (unsigned char *) NULL;
  73.  
  74.   /* Get the options */
  75.   while (--argc > 0 && (*++argv)[0] == '-')
  76.   { while (*++(*argv))
  77.     { switch (**argv)
  78.       { case 't':    dotitle = 1; break;
  79.     case 'T':    dotitle = 0; break;
  80.     case 'p':    dosize = 1; break;
  81.     case 'P':    dosize = 0; break;
  82.     case 'r':    rotate++; break;
  83.     case 'b':    bigpaper++; break;
  84.     case 's':    scribe++; break;
  85.     case 'w':    width = atof (*argv+1); SKIPARG; break;
  86.     default:    fprintf (stderr, "%s\n", USAGE);
  87.             exit (1);
  88.       }
  89.     }
  90.   }
  91.  
  92.   if (!read_bitmap (&image, (char *) NULL))
  93.   { exit (1); }
  94.  
  95.   if ((image.hdr.planes != 1 && image.hdr.planes != 3) || image.hdr.clrlen > 0)
  96.   { fprintf (stderr,
  97.          "Error:\tfbps only handles grayscale or unmapped color files\n");
  98.     fprintf (stderr, "\tUse the clr2gray filter to create grayscale first\n");
  99.     exit (1);
  100.   }
  101.  
  102.   if (image.hdr.bits == 1)
  103.   { fprintf (stderr, "Error:\tfbps cannot handle 1 bit deep bitmaps\n");
  104.     fprintf (stderr, "\tUse 'fbcat -P | pbm2ps' to convert %s\n",
  105.          "1bit files to Postscript");
  106.     exit (1);
  107.   }
  108.  
  109.   /* Get title */
  110.   if (image.hdr.title && image.hdr.title[0])
  111.   { title = image.hdr.title; }
  112.  
  113.   /* Get width and height */
  114.   rows = image.hdr.rows;
  115.   cols = image.hdr.cols;
  116.   rowlen = image.hdr.rowlen;
  117.  
  118.   paperwidth = bigpaper ? BIGPAPERWIDTH : PAPERWIDTH;
  119.   paperheight = bigpaper ? BIGPAPERHEIGHT : PAPERHEIGHT;
  120.  
  121.   maxwidth = (rotate ? paperheight : paperwidth) - 1.5;
  122.   maxheight = (rotate ? paperwidth : paperheight) - 1.5;
  123.  
  124.   /* Pick output size */
  125.   if (width < 0.0 || width > maxwidth)
  126.   { width = maxwidth; }
  127.   
  128.   height = width * image.hdr.aspect * (double) rows / cols;
  129.     
  130.   if (height > maxheight)
  131.   { width = width * maxheight / height; height = maxheight; }
  132.   
  133.   /* Pick lower left corner */
  134.   if (scribe)
  135.   { llx = lly = 0.0;  }
  136.   else
  137.   { llx = (paperwidth - (rotate ? height : width)) / 2.0;
  138.     lly = rotate ? width + (paperheight - width) / 2.0 :
  139.            (paperheight - height) / 2.0;
  140.   }
  141.  
  142.   fprintf (stderr,
  143.        "FBM to PS \"%s\" width %1.3lf inches, height %1.3lf inches\n",
  144.        title ? title : "(untitled)", width, height);
  145.  
  146.   /* Write out PostScript Header */
  147.   if (scribe)
  148.   { printf ("%%! Scribe @graphic style PostScript\n");
  149.     if (title) { printf ("%%%%Title: %s\n", ps_chars (title)); }
  150.     if (creator) { printf ("%%%%Creator:  %s\n", ps_chars (creator)); }
  151.     printf ("%%%%CreationDate: %s", ctime (&clock));
  152.  
  153.     printf ("/inch { 72 mul } def\n");
  154.     printf ("/picstr %d string def\n\n", BYTESPERLINE);
  155.   }
  156.   else
  157.   { printf ("%%!\n");
  158.     if (title) { printf ("%%%%Title: %s\n", ps_chars (title)); }
  159.     if (creator) { printf ("%%%%Creator:  %s\n", ps_chars (creator)); }
  160.     printf ("%%%%CreationDate: %s", ctime (&clock));
  161.     printf ("%%%%Pages: 1\n");
  162.     printf ("%%%%DocumentFonts:%s%s\n",
  163.         dotitle ? " Times-Bold" : "",
  164.         dosize ?  " Times-Roman" : "");
  165.     printf ("%%%%EndComments\n");
  166.     printf ("%%%%EndProlog\n");
  167.     printf ("%%%%Page: 1 1\n\n");
  168.  
  169.     printf ("/inch { 72 mul } def\n");
  170.     printf ("/picstr %d string def\n\n", BYTESPERLINE);
  171.  
  172.     if (dotitle && title)
  173.     { printf ("/Times-Bold findfont 14 scalefont setfont\n");
  174.       printf ("%lg inch %lg inch moveto\n", 
  175.         llx + width/2.0, lly + 0.125 + height);
  176.       printf ("(%s)\n", ps_chars (title));
  177.       printf ("dup stringwidth pop 2 div 0 exch sub 0 rmoveto show\n\n");
  178.     }
  179.   
  180.     if (dosize)
  181.     { printf ("/Times-Roman findfont 8 scalefont setfont\n");
  182.             printf ("%lg inch %lg inch moveto\n", llx + width, lly - 0.25);
  183.       sprintf (buf, "[ %d by %d pixels, %1.3lf %s, %1.2lf by %1.2lf inches ]",
  184.            image.hdr.cols, image.hdr.rows, image.hdr.aspect,
  185.            "aspect ratio", width, height);
  186.       printf ("(%s)\n", ps_chars (buf));
  187.       printf ("dup stringwidth pop 0 exch sub 0 rmoveto show\n\n");
  188.     }
  189.  
  190.   }
  191.  
  192.   printf ("gsave\n");
  193.  
  194.   if (llx != 0.0 || lly != 0.0)
  195.   { printf ("%lg inch %lg inch translate ", llx, lly); }
  196.  
  197.   if (rotate)
  198.   { fputs ("-90 rotate ", stdout);  }
  199.   
  200.   printf ("%lg inch %lg inch scale\n", width, height);
  201.  
  202.   if (image.hdr.planes == 3) {
  203.     int plane;
  204.     int plnlen = image.hdr.plnlen;
  205.     int bits = image.hdr.bits;
  206.  
  207.     /* use QMS colorimage operator */
  208.  
  209.     printf ("/redScanLine %d string def\n", cols * 8 / bits);
  210.     printf ("/greenScanLine %d string def\n", cols * 8 / bits);
  211.     printf ("/blueScanLine %d string def\n", cols * 8 / bits);
  212.  
  213.     printf ("%d %d %d [%d 0 0 %d 0 %d]\n",
  214.         cols, rows, bits, cols, -rows, rows);
  215.     puts ("{currentfile redScanLine readhexstring pop}");
  216.     puts ("{currentfile greenScanLine readhexstring pop}");
  217.     puts ("{currentfile blueScanLine readhexstring pop}");
  218.     puts ("true 3 colorimage");
  219.  
  220.     for (j = 0; j < rows; j++) {
  221.         for (plane = 0; plane < 3; plane++) {
  222.         for (i = 0; i < cols; i++) {
  223.             printf ("%02x", image.bm[plane * plnlen + j * rowlen + i]);
  224.             if (++bytcnt % BYTESPERLINE == 0)
  225.             putchar ('\n');
  226.         }
  227.         bytcnt = 0;
  228.         putchar ('\n');
  229.         }
  230.     }
  231.   }
  232.   else {
  233.     printf ("%d %d 8 [%d 0 0 -%d 0 %d] ", cols, rows, cols, rows, rows);
  234.     printf ("{ currentfile picstr readhexstring pop }\n");
  235.     printf ("image\n");
  236.   
  237.     /* Write out bitmap */
  238.     for (j=0; j < rows; j++)  
  239.     { for (i=0; i < cols; i++)
  240.       { printf ("%02x", image.bm[j * rowlen + i]);
  241.  
  242.         if (++bytcnt % BYTESPERLINE == 0) putchar ('\n');
  243.       }
  244.   }
  245.  
  246.   }
  247.  
  248.   /* Pad so there are exactly BYTESPERLINE bytes in each line */
  249.   if (bytcnt % BYTESPERLINE)
  250.   { while (bytcnt++ % BYTESPERLINE) printf ("00");
  251.     printf ("\n");
  252.   }
  253.   
  254.   printf ("grestore\n");
  255.  
  256.   if (!scribe)
  257.   { printf ("\nshowpage\n\n");
  258.     printf ("%%%%Trailer\n");
  259.   }
  260.  
  261.   exit (0);
  262. }
  263.  
  264. /****************************************************************
  265.  * ps_chars: Put in proper escapes so an arbitrary string works
  266.  *         according to the PostScript definition of a literal
  267.  ****************************************************************/
  268.  
  269. char *ps_chars (txt)
  270. char *txt;
  271. { static char buf[512];
  272.   register char *s = buf;
  273.   char *index ();
  274.   
  275.   for (; *txt; txt++)
  276.   { if (index ("()\\", *txt))
  277.     { *s++ = '\\'; *s++ = *txt; }
  278.     else if (*txt < ' ' || *txt > '~')
  279.     { sprintf (s, "\\%03o", *txt & 0377); s += 4; }
  280.     else
  281.     { *s++ = *txt; }
  282.   }
  283.   *s = '\0';
  284.   s = buf;
  285.   return (s);
  286. }
  287.