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

  1. /*****************************************************************
  2.  * flface.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.  * flface.c: 
  11.  *
  12.  * CONTENTS
  13.  *    write_face (image, stream)
  14.  *    read_face (image, stream, mstr, mlen)
  15.  *    
  16.  *
  17.  * EDITLOG
  18.  *    LastEditDate = Mon Jun 25 00:07:38 1990 - Michael Mauldin
  19.  *    LastFileName = /usr2/mlm/src/misc/fbm/flface.c
  20.  *
  21.  * HISTORY
  22.  * 25-Jun-90  Michael Mauldin (mlm@cs.cmu.edu) Carnegie Mellon
  23.  *    Package for Release 1.0
  24.  *
  25.  * 07-Mar-89  Michael Mauldin (mlm) at Carnegie Mellon University
  26.  *    Beta release (version 0.9) mlm@cs.cmu.edu
  27.  *
  28.  * 12-Nov-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  29.  *    Created.
  30.  *****************************************************************/
  31.  
  32. # include <stdio.h>
  33. # include <math.h>
  34. # include <ctype.h>
  35. # include "fbm.h"
  36.  
  37. /****************************************************************
  38.  * write_face: Write Bennet Yee's face file format
  39.  ****************************************************************/
  40.  
  41. #ifndef lint
  42. static char *fbmid =
  43. "$FBM flface.c <1.0> 25-Jun-90  (C) 1989,1990 by Michael Mauldin, source \
  44. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  45. #endif
  46.  
  47. write_face (image, stream)
  48. FBM *image;
  49. FILE *stream;
  50. { register int r, c, word, width, height;
  51.   register unsigned char *bm;
  52.  
  53.   width = image->hdr.cols;
  54.   height = image->hdr.rows;
  55.  
  56.   put_short (BM_MAGIC, stream, BIG);
  57.   put_short (width, stream, BIG);
  58.   put_short (height, stream, BIG);
  59.   
  60.   for (r=0; r<height; r++)
  61.   { word = 0;
  62.     bm = &(image->bm[r * image->hdr.rowlen]);
  63.  
  64.     for (c=0; c<width; c++)
  65.     { word = (word >> 1) | (*bm++ ? 0: ((1 << 15)));
  66.       if ((c&15) == 15) put_short (word, stream, BIG);
  67.     }
  68.  
  69.     if (c&15)
  70.     { word >>= (16 - (c&15));
  71.       put_short (word, stream, BIG);
  72.     }
  73.   }
  74.   
  75.   return (1);
  76. }
  77.  
  78. /****************************************************************
  79.  * read_face: Read Bennet Yee's FACE format
  80.  ****************************************************************/
  81.  
  82. read_face (image, rfile, mstr, mlen)
  83. FBM *image;
  84. FILE *rfile;
  85. char *mstr;
  86. int mlen;
  87. { register unsigned char *bmp;
  88.   register int r, c, word, width, height, rowlen, magic;
  89.  
  90.   magic =  (NEXTMCH(rfile,mstr,mlen) & 0xff) << 8;
  91.   magic |= (NEXTMCH(rfile,mstr,mlen) & 0xff);
  92.  
  93.   if (magic != BM_MAGIC)
  94.   { fprintf (stderr, "Bad magic number %04x, not BM format\n", magic);
  95.  
  96.     fprintf (stderr, "Next 10 chars:");
  97.     for (r=0; r<10; r++)
  98.     { c = fgetc (rfile); fprintf (stderr, " %03o", c); }
  99.     fprintf (stderr, "\n");
  100.     return (0);
  101.   }
  102.   
  103.   width = get_short (rfile, BIG);
  104.   height = get_short (rfile, BIG);
  105.   
  106.   if (width > 5000 || height > 5000)
  107.   { fprintf (stderr, "Image too big, bogus width %d or height %d maybe?\n",
  108.          width, height);
  109.     return (0);
  110.   }
  111.  
  112.   /* Initialize FBM header and allocate memory for bitmap */
  113.   image->hdr.cols = width;
  114.   image->hdr.rows = height;
  115.   image->hdr.planes = 1;
  116.   image->hdr.bits = 1;
  117.   image->hdr.physbits = 8;
  118.   image->hdr.rowlen = 16 * ((image->hdr.cols + 15) / 16);
  119.   image->hdr.plnlen = image->hdr.rowlen * image->hdr.rows;
  120.   image->hdr.clrlen = 0;
  121.   image->hdr.aspect = 1.0;
  122.   image->hdr.title[0] = 0;
  123.   image->hdr.credits[0] = 0;
  124.  
  125.   alloc_fbm (image);
  126.  
  127.   /* Now read bits and store them in the proper place in the bitmap */
  128.   rowlen = image->hdr.rowlen;
  129.  
  130.   
  131.   for (r=0; r<height; r++)
  132.   { bmp = &(image->bm[r * rowlen]);
  133.  
  134.     for (c=0; c<width; c++)
  135.     { if ((c&15) == 0 && (word = get_short (rfile, BIG)) == EOF)
  136.       { fprintf (stderr, "r %d, c %d\n", r, c);
  137.         return (0);
  138.       }
  139.  
  140.       *bmp++ = ((word & 1) ? BLACK : WHITE);
  141.       word >>= 1;
  142.     }
  143.   }
  144.   
  145.   return (1);
  146. }
  147.