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

  1. /*****************************************************************
  2.  * mps2fbm.c: FBM Release 1.0 25-Feb-90 Michael Mauldin
  3.  *
  4.  * Copyright (C) 1989,1990 by Gary Sherwin & Michael Mauldin.
  5.  * Permission is granted to use this file in whole or in part for
  6.  * any purpose, educational, recreational or commercial, provided
  7.  * that this copyright notice is retained unchanged.  This software
  8.  * is available to all free of charge by anonymous FTP and in the
  9.  * UUNET archives.
  10.  *
  11.  * mps2fbm.c: Convert Microtek Encapsulated Postscript to FBM
  12.  *
  13.  * USAGE
  14.  *     % mps2fbm < rawfile > fbm
  15.  *
  16.  * EDITLOG
  17.  *    LastEditDate = Mon Jun 25 00:26:53 1990 - Michael Mauldin
  18.  *    LastFileName = /usr2/mlm/src/misc/fbm/mps2fbm.c
  19.  *
  20.  * HISTORY
  21.  * 25-Jun-90  Michael Mauldin (mlm@cs.cmu.edu) Carnegie Mellon
  22.  *    Package for Release 1.0
  23.  *
  24.  * 18-Oct-89  Gary W. Sherwin (sherwin) at Westinghouse STC.
  25.  *    Created from raw2fbm.c for MPS format
  26.  *****************************************************************/
  27.  
  28. # include <stdio.h>
  29. # include "fbm.h"
  30.  
  31. # define USAGE \
  32. "Usage: mps2fbm [ -t'title' -c'credits' ] < rawfile > fbm"
  33.  
  34. #ifndef lint
  35. static char *fbmid =
  36. "$FBM mps2fbm.c <1.0> 25-Jun-90  (C) 1989,1990 by Gary Sherwin & \
  37. Michael Mauldin, source code available free from MLM@CS.CMU.EDU \
  38. and from UUNET archives$";
  39. #endif
  40. /* MPS bitmap headers in files */
  41. typedef struct mps_filehdr_struct
  42.   {
  43.       char  wlabel[7];        /* Width label */
  44.       char  width[4];        /* Width */
  45.       char  hlabel[13];        /* Height label */
  46.       char  height[4];        /* Height */
  47.       char  junk[792];        /* Other stuff we don't use */
  48.       char  imgkey[11];        /* Key marking end of header */
  49.       char  ehdr[1];        /* End of header */
  50.   } MPSHDR;
  51.  
  52. main (argc, argv)
  53. char *argv[];
  54. { register int j, k, rowlen;
  55.   double aspect = 1.0;    /* Default for bitmaps */
  56.   int rows = 400, cols = 640, planes = 1, colpad = 0;
  57.   int rowcount = 1, colcount = 1, shiftcount = 256;
  58.   FBMHDR hdr;
  59.   MPSHDR mpshdr;
  60.   unsigned char *buf, inbyte, nextout, zerro = 0;
  61.   char title[FBM_MAX_TITLE], credits[FBM_MAX_TITLE];
  62.  
  63.   title[0] = '\0';
  64.   credits[0] = '\0';
  65.  
  66.   /* Get the options */
  67.   while (--argc > 0 && (*++argv)[0] == '-')
  68.   { while (*++(*argv))
  69.     { switch (**argv)
  70.       { case 't':    strncpy (title, *argv+1, FBM_MAX_TITLE);
  71.             title[FBM_MAX_TITLE-1] = '\0';
  72.             CLRARG; break;
  73.     case 'c':    strncpy (credits, *argv+1, FBM_MAX_TITLE);
  74.             credits[FBM_MAX_TITLE-1] = '\0';
  75.             CLRARG; break;
  76.     default:    fprintf (stderr, "%s\n", USAGE);
  77.             exit (1);
  78.       }
  79.     }
  80.   }
  81.  
  82.   if (!fread (&mpshdr, 0x340,1,stdin))
  83.   {
  84.       fprintf (stdout, "Could not find mps header\n");
  85.       exit (1);
  86.   }
  87.   cols = atoi (mpshdr.width);
  88.   rows = atoi (mpshdr.height);
  89.   planes = 1;
  90.   
  91.   if (cols < 1 || rows < 1)
  92.   { fprintf (stderr, "Illegal width (%d) and height (%d) must be positive\n",
  93.          cols, rows);
  94.     exit (1);
  95.   }
  96.  
  97.   rowlen = 2 * ((cols * 8 + 15) / 16);
  98.  
  99.   /* Build header */
  100.   hdr.rows = rows;
  101.   hdr.cols = cols;
  102.   hdr.planes = planes;
  103.   hdr.bits = 8;
  104.   hdr.physbits = 8;
  105.   hdr.rowlen = rowlen;
  106.   hdr.plnlen = hdr.rowlen * rows;
  107.   hdr.clrlen = 0;
  108.   hdr.aspect = aspect;
  109.   strcpy (hdr.title, title);
  110.   strcpy (hdr.credits, credits);
  111.   
  112.   write_hdr_fbm (&hdr, stdout);
  113.   
  114.   buf = (unsigned char *) malloc (cols);
  115.   
  116.   colpad = cols % 2;
  117.   rowcount = 1;
  118.   colcount = 1;
  119.   while (rowcount <= rows)
  120.   {
  121.       if (! fread (&inbyte, 1, 1, stdin))
  122.       {
  123.       fprintf (stdout, "premature end of file on input\n");
  124.       exit(1);
  125.       }
  126.       shiftcount = 256;
  127.       while (shiftcount = shiftcount >> 1)
  128.       {
  129.       nextout = inbyte & 128;
  130.       inbyte = inbyte <<1;
  131.       nextout = ~nextout;
  132.       if (! fwrite (&nextout, 1, 1, stdout))
  133.       { perror ("mps2fbm"); exit (1); }
  134.       if (++colcount > cols)
  135.       {
  136.           if (colpad)
  137.           {
  138.           if (! fwrite (&zerro, 1, 1, stdout))
  139.           { perror ("mps2fbm"); exit (1); }
  140.           }
  141.           colcount = 1;
  142.           rowcount++;
  143.       }
  144.       }
  145.   }
  146.   exit (0);
  147. }
  148.