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

  1. /*****************************************************************
  2.  * flwrfb.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.  * flwrfb.c: 
  9.  *
  10.  * CONTENTS
  11.  *    write_fbm (image, wfile)
  12.  *    write_hdr_fbm (image, wfile)
  13.  *
  14.  * EDITLOG
  15.  *    LastEditDate = Tue Mar  7 19:57:39 1989 - Michael Mauldin
  16.  *    LastFileName = /usr2/mlm/src/misc/fbm/flwrfb.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.  * write_fbm: Write an image to a stream
  33.  ****************************************************************/
  34.  
  35. #ifndef lint
  36. static char *fbmid =
  37.     "$FBM flwrfb.c <0.9> 07-Mar-89  (C) 1989 by Michael Mauldin$";
  38. #endif
  39.  
  40. write_fbm (image, wfile)
  41. register FBM *image;
  42. register FILE *wfile;
  43. { register int k, j, rowlen, plnlen;
  44.   register unsigned char *bmptr;
  45.  
  46.   if (! write_hdr_fbm (image, wfile)) return (0);
  47.  
  48.   rowlen = image->hdr.rowlen;
  49.   plnlen = image->hdr.plnlen;
  50.   
  51.   if (image->hdr.clrlen > 0)
  52.   { if (!fwrite (image->cm, image->hdr.clrlen, 1, wfile))
  53.     { perror ("write_fbm (colormap)"); return (0); }
  54.   }
  55.  
  56.   for (k=0; k<image->hdr.planes; k++)
  57.   { bmptr = &(image->bm[k*plnlen]);
  58.     
  59.     for (j=0; j<image->hdr.rows; j++, bmptr += rowlen)
  60.     { if (! fwrite (bmptr, 1, rowlen, wfile))
  61.       { perror ("write_fbm"); return (0); }
  62.     }
  63.   }
  64.     
  65.   return (1);
  66. }
  67.  
  68. /****************************************************************
  69.  * write_hdr_fbm: Write an image header to a stream
  70.  ****************************************************************/
  71.  
  72. write_hdr_fbm (image, wfile)
  73. register FBM *image;
  74. register FILE *wfile;
  75. { FBMFILEHDR file_hdr;
  76.  
  77.   strncpy (file_hdr.magic, FBM_MAGIC, 8);
  78.   sprintf (file_hdr.cols, "%7d", image->hdr.cols);
  79.   sprintf (file_hdr.rows, "%7d", image->hdr.rows);
  80.   sprintf (file_hdr.planes, "%7d", image->hdr.planes);
  81.   sprintf (file_hdr.bits, "%7d", image->hdr.bits);
  82.   sprintf (file_hdr.physbits, "%7d", image->hdr.physbits);
  83.   sprintf (file_hdr.rowlen, "%11d", image->hdr.rowlen);
  84.   sprintf (file_hdr.plnlen, "%11d", image->hdr.plnlen);
  85.   sprintf (file_hdr.clrlen, "%11d", image->hdr.clrlen);
  86.   sprintf (file_hdr.aspect, "%11.6lf", image->hdr.aspect);
  87.   strncpy (file_hdr.title, image->hdr.title, FBM_MAX_TITLE);
  88.   strncpy (file_hdr.credits, image->hdr.credits, FBM_MAX_TITLE);
  89.  
  90.   if (! fwrite (&file_hdr, sizeof (file_hdr), 1, wfile))
  91.   { perror ("write_fbm (header)"); return (0); }
  92.     
  93.   return (1);
  94. }
  95.