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

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