home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume19 / fbm / part04 / flpbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-08  |  4.9 KB  |  189 lines

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