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

  1. /*****************************************************************
  2.  * flpbm.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.  * flpbm.c: 
  11.  *
  12.  * CONTENTS
  13.  *    read_pbm (image, infile, mstr, mlen)
  14.  *    write_pbm (image, stream)
  15.  *
  16.  * EDITLOG
  17.  *    LastEditDate = Mon Jun 25 00:17:08 1990 - Michael Mauldin
  18.  *    LastFileName = /usr2/mlm/src/misc/fbm/flpbm.c
  19.  *
  20.  * HISTORY
  21.  * 25-Jun-90  Michael Mauldin (mlm@cs.cmu.edu) Carnegie Mellon
  22.  *    Package for Release 1.0
  23.  *
  24.  * 07-Mar-89  Michael Mauldin (mlm) at Carnegie Mellon University
  25.  *    Beta release (version 0.9) mlm@cs.cmu.edu
  26.  *
  27.  * 12-Nov-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  28.  *    Created.
  29.  *****************************************************************/
  30.  
  31. # include <stdio.h>
  32. # include <math.h>
  33. # include <ctype.h>
  34. # include "fbm.h"
  35.  
  36. /****************************************************************
  37.  * read_pbm: Read a pbm file into an fbm bitmap
  38.  ****************************************************************/
  39.  
  40. #ifndef lint
  41. static char *fbmid =
  42. "$FBM flpbm.c <1.0> 25-Jun-90  (C) 1989,1990 by Michael Mauldin, source \
  43. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  44. #endif
  45.  
  46. read_pbm (image, infile, mstr, mlen)
  47. FBM *image;
  48. FILE *infile;
  49. char *mstr;
  50. int mlen;
  51. { register int ch, i, j;
  52.   register unsigned char *bmp;
  53.   char cmtbuf[128], *s;
  54.  
  55.   if ((ch = NEXTMCH (infile, mstr, mlen)) != 'P' || 
  56.       (ch = NEXTMCH (infile, mstr, mlen)) != '1')
  57.   { fprintf (stderr, "bad magic number, input not PBM file\n");
  58.     return (0);
  59.   }
  60.  
  61.   image->hdr.cols = pbm_getint (infile);
  62.   image->hdr.rows = pbm_getint (infile);
  63.   image->hdr.planes = 1;
  64.   image->hdr.bits = 1;  
  65.   image->hdr.physbits = 8;
  66.   image->hdr.rowlen = 16 * ((image->hdr.cols + 15) / 16);
  67.   image->hdr.plnlen = image->hdr.rowlen * image->hdr.rows;
  68.   image->hdr.clrlen = 0;
  69.   image->hdr.aspect = 1.0;
  70.   image->hdr.title[0] = '\0';
  71.   
  72.   if (image->hdr.cols < 1 || image->hdr.rows < 1)
  73.   { fprintf (stderr, "Error, specified size %d by %d\n",
  74.          image->hdr.cols, image->hdr.rows);
  75.     return (0);
  76.   }
  77.   
  78.   fprintf (stderr,
  79.        "Reading PBM file \"%s\" [%dx%dx1]\n",
  80.        image->hdr.title[0] ? image->hdr.title : "",
  81.        image->hdr.cols, image->hdr.rows);
  82.  
  83.   alloc_fbm (image);
  84.  
  85.   for (j=0; j<image->hdr.rows; j++)
  86.   { bmp = &(image->bm[j * image->hdr.rowlen]);
  87.  
  88.     for (i=0; i<image->hdr.cols; i++)
  89.     { while ((ch = fgetc (infile)) != EOF)
  90.       { if (ch == '0') { *bmp++ = WHITE; break; }
  91.  
  92.         else if (ch == '1') { *bmp++ = BLACK; break; }
  93.  
  94.     else if (ch == '#')
  95.         { s = cmtbuf; *s++ = '#';
  96.       while ((ch = fgetc (infile)) != EOF && ch != '\n') *s++ = ch;
  97.       *s = '\0';
  98.       for (s=cmtbuf; *s == '#' || isspace (*s); s++) ;
  99.       if (!strncmp (s, "Title: ", 7)) strcpy (image->hdr.title, s+7);
  100.       fprintf (stderr, "Read_pbm found title '%s'\n",
  101.            image->hdr.title);
  102.     }
  103.       }
  104.       
  105.       if (ch == EOF)
  106.       { fprintf (stderr, "premature EOF, row %d, col %d\n", j, i);
  107.         return (0);
  108.       }
  109.     }
  110.   }
  111.  
  112.   
  113.   return (1);
  114. }
  115.  
  116. /****************************************************************
  117.  * pbm_getint: Read a number from a PBM file, ignoring comments
  118.  ****************************************************************/
  119.  
  120. # define START 1
  121. # define COMMENT 2
  122. # define INTEGER 3
  123.  
  124. pbm_getint (infile)
  125. FILE *infile;
  126. { char buf[80];
  127.   register char *s = buf;
  128.   register int ch;
  129.   int state = START;
  130.  
  131.   while (1)
  132.   { if ((ch = fgetc (infile)) == EOF)
  133.     { fprintf (stderr, "premature EOF\n");
  134.       return (0);
  135.     }
  136.  
  137.     switch (state)
  138.     { case START:    if (isspace (ch)) ;
  139.             else if (ch == '#') state = COMMENT;
  140.             else if (isdigit (ch))
  141.             { *s++ = ch; state = INTEGER; }
  142.             else
  143.             { fprintf (stderr, "bad INTEGER in input");
  144.               return (0);
  145.             }
  146.             break;
  147.  
  148.       case COMMENT:    if (ch == '\n') state = START;
  149.             break;
  150.  
  151.       case INTEGER:    if (isdigit (ch)) *s++ = ch;
  152.             else
  153.             { register int result;
  154.  
  155.               *s = '\0';
  156.               result = atoi (buf);
  157.               return (result);
  158.             }
  159.             break;
  160.  
  161.       default:        fprintf (stderr, "impossible state %d\n", state);
  162.             return (0);
  163.     }
  164.   }
  165. }
  166.  
  167. /****************************************************************
  168.  * write_pbm: Write a bitmap in PBM format to the output device
  169.  ****************************************************************/
  170.  
  171. write_pbm (image, stream)
  172. register FBM *image;
  173. FILE *stream;
  174. { register int i, j, outcol = 0;
  175.   register unsigned char *bmp;
  176.  
  177.   /* Write PBM header lines */
  178.   fprintf (stream, "P1\n%d %d\n", image->hdr.cols, image->hdr.rows);
  179.   if (image->hdr.title[0])
  180.   { fprintf (stream, "# Title: %s\n", image->hdr.title); }
  181.   
  182.   /* Now write out 1s and 0s, in 70 character lines */
  183.   for (j=0; j < image->hdr.rows; j++)
  184.   { bmp = &(image->bm[j * image->hdr.rowlen]);
  185.  
  186.     for (i=0; i < image->hdr.cols; i++)
  187.     { fputc (*bmp++ ? '0' : '1', stream);    /* In PBM 1=black, not white */
  188.       if (++outcol >= 70) { fprintf (stream, "\n"); outcol=0; }
  189.     }
  190.     if (outcol) { fprintf (stream, "\n"); outcol=0; }
  191.   }
  192.   
  193.   return (1);
  194. }
  195.