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

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