home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume19 / fbm / part03 / raw2fbm.c < prev   
Encoding:
C/C++ Source or Header  |  1989-06-08  |  3.5 KB  |  132 lines

  1. /*****************************************************************
  2.  * raw2fbm.c: FBM Library 0.94 (Beta test) 20-May-89  Michael Mauldin
  3.  *
  4.  * Copyright (C) 1989 by Michael Mauldin.  Permission is granted to
  5.  * use this file in whole or in part provided that you do not sell it
  6.  * for profit and that this copyright notice is retained unchanged.
  7.  *
  8.  * raw2fbm.c: Convert raw format (for example Amiga rasters)
  9.  *
  10.  * USAGE
  11.  *     % raw2fbm -t'title'
  12.  *           -c'credits'
  13.  *           -a<aspect>
  14.  *           -w<width>
  15.  *           -h<height>
  16.  *           -d'<planes>    < rawfile > fbm
  17.  *
  18.  * EDITLOG
  19.  *    LastEditDate = Sat May 20 19:07:44 1989 - Michael Mauldin
  20.  *    LastFileName = /usr2/mlm/src/misc/fbm/raw2fbm.c
  21.  *
  22.  * HISTORY
  23.  * 20-May-89  Michael Mauldin (mlm) at Carnegie Mellon University
  24.  *    Bug fix from Dave Cohrs <dave@cs.wisc.edu>
  25.  *
  26.  * 07-Mar-89  Michael Mauldin (mlm) at Carnegie Mellon University
  27.  *    Beta release (version 0.9) mlm@cs.cmu.edu
  28.  *
  29.  * 20-Aug-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  30.  *    Created.
  31.  *****************************************************************/
  32.  
  33. # include <stdio.h>
  34. # include "fbm.h"
  35.  
  36. # define USAGE \
  37. "Usage: raw2fbm [ -t'title' -c'credits' -a<aspect>\n\
  38.          -w<width> -h<height> -d'<planes> ] < rawfile > fbm"
  39.  
  40. #ifndef lint
  41. static char *fbmid =
  42.     "$FBM raw2fbm.c <0.94> 20-May-89  (C) 1989 by Michael Mauldin$";
  43. #endif
  44.  
  45. main (argc, argv)
  46. char *argv[];
  47. { register int j, k, rowlen;
  48.   double aspect = 1.2;    /* Default for Digiview 640x400 bitmaps */
  49.   int rows = 400, cols = 640, planes = 1;
  50.   FBMHDR hdr;
  51.   unsigned char *buf;
  52.   char title[FBM_MAX_TITLE], credits[FBM_MAX_TITLE];
  53.  
  54.   title[0] = '\0';
  55.   credits[0] = '\0';
  56.  
  57.   /* Get the options */
  58.   while (--argc > 0 && (*++argv)[0] == '-')
  59.   { while (*++(*argv))
  60.     { switch (**argv)
  61.       { case 'a':    aspect  = atof (*argv+1); SKIPARG; break;
  62.     case 'w':    cols   = atoi (*argv+1); SKIPARG; break;
  63.     case 'h':    rows  = atoi (*argv+1); SKIPARG; break;
  64.     case 'd':    planes   = atoi (*argv+1); SKIPARG; break;
  65.     case 't':    strncpy (title, *argv+1, FBM_MAX_TITLE);
  66.             title[FBM_MAX_TITLE-1] = '\0';
  67.             CLRARG; break;
  68.     case 'c':    strncpy (credits, *argv+1, FBM_MAX_TITLE);
  69.             credits[FBM_MAX_TITLE-1] = '\0';
  70.             CLRARG; break;
  71.     default:    fprintf (stderr, "%s\n", USAGE);
  72.             exit (1);
  73.       }
  74.     }
  75.   }
  76.  
  77.   if (argc > 0)        cols = atoi (argv[0]);
  78.   if (argc > 1)        rows = atoi (argv[1]);
  79.   if (argc > 2)        planes = atoi (argv[2]);
  80.   if (argc > 3)        aspect = atof (argv[3]);
  81.   
  82.   if (cols < 1 || rows < 1)
  83.   { fprintf (stderr, "Both width (%d) and height (%d) must be positive\n",
  84.          cols, rows);
  85.     exit (1);
  86.   }
  87.  
  88.   if (planes != 1 && planes != 3)
  89.   { fprintf (stderr, "Depth must be 1 for B+W or 3 for RGB color\n");
  90.     exit (1);
  91.   }
  92.  
  93.   if (aspect < 0.01 || aspect > 100.0)
  94.   { fprintf (stderr, "Pixel aspect ratio (%lg) must be in [0.01 to 100.0]\n",
  95.          aspect);
  96.     exit (1);
  97.   }
  98.  
  99.   rowlen = 2 * ((cols * 8 + 15) / 16);
  100.  
  101.   /* Build header */
  102.   hdr.rows = rows;
  103.   hdr.cols = cols;
  104.   hdr.planes = planes;
  105.   hdr.bits = 8;
  106.   hdr.physbits = 8;
  107.   hdr.rowlen = rowlen;
  108.   hdr.plnlen = hdr.rowlen * rows;
  109.   hdr.clrlen = 0;
  110.   hdr.aspect = aspect;
  111.   strcpy (hdr.title, title);
  112.   strcpy (hdr.credits, credits);
  113.   
  114.   write_hdr_fbm (&hdr, stdout);
  115.   
  116.   buf = (unsigned char *) malloc (cols);
  117.   
  118.   for (k=0; k<planes; k++)
  119.   { for (j=0; j<rows; j++)  
  120.     { if (! fread (buf, cols, 1, stdin))
  121.       { fprintf (stdout, "premature end of file on input\n");
  122.         exit (1);
  123.       }
  124.  
  125.       if (! fwrite (buf, rowlen, 1, stdout))
  126.       { perror ("raw2fbm"); exit (1); }
  127.     }
  128.   }
  129.   
  130.   exit (0);
  131. }
  132.