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

  1. /*****************************************************************
  2.  * flgifw.c: FBM Library 0.9 (Beta test) 07-Mar-89  Michael Mauldin
  3.  *
  4.  * Portions of this code Copyright (C) 1989 by Michael Mauldin.
  5.  * Permission is granted to use this file in whole or in part provided
  6.  * that you do not sell it for profit and that this copyright notice
  7.  * and the names of all authors are retained unchanged.
  8.  *
  9.  * flgifw.c:
  10.  *
  11.  * CONTENTS
  12.  *    write_gif (image, stream, mstr, mlen)
  13.  *
  14.  * HISTORY
  15.  * 07-Mar-89  Michael Mauldin (mlm) at Carnegie Mellon University
  16.  *    Beta release (version 0.9) mlm@cs.cmu.edu
  17.  *
  18.  * 19-Feb-89  Michael Mauldin (mlm) at Carnegie Mellon University
  19.  *    Adapted to FBM package.
  20.  *
  21.  * 13-Feb-89  David Rowley (mgardi@watdcsu.waterloo.edu)
  22.  *    GIF encoding modifications.
  23.  *
  24.  *    Based on: compress.c - File compression ala IEEE Computer, June 1984.
  25.  *
  26.  *    Spencer W. Thomas       (decvax!harpo!utah-cs!utah-gr!thomas)
  27.  *    Jim McKie               (decvax!mcvax!jim)
  28.  *    Steve Davies            (decvax!vax135!petsd!peora!srd)
  29.  *    Ken Turkowski           (decvax!decwrl!turtlevax!ken)
  30.  *    James A. Woods          (decvax!ihnp4!ames!jaw)
  31.  *    Joe Orost               (decvax!vax135!petsd!joe)
  32.  *
  33.  *****************************************************************/
  34.  
  35. # include <stdio.h>
  36. # include "fbm.h"
  37.  
  38. unsigned char *pixels = NULL;
  39. int rowlen = 0;
  40.  
  41. int GetGIFPixel ();
  42.  
  43. #ifndef lint
  44. static char *fbmid =
  45.     "$FBM flgifw.c <0.9> 07-Mar-89  (C) 1989 by Michael Mauldin$";
  46. #endif
  47.  
  48. write_gif (image, wfile)
  49. FBM *image;
  50. FILE *wfile;
  51. { register int i, bits, sum, bkg, clrs;
  52.   unsigned char *red, *grn, *blu;
  53.   int rint[256], gint[256], bint[256];
  54.     
  55.   if (image->hdr.planes > 1 || image->hdr.clrlen == 0)
  56.   { fprintf (stderr, "write_gif can only handle mapped color images\n");
  57.     return (0);
  58.   }
  59.  
  60.   if (image->hdr.aspect < 1.15 || image->hdr.aspect > 1.25)
  61.   { fprintf (stderr,
  62.      "Warning, image has aspect ratio of %1.3lf, GIF standard is 1.2\n",
  63.      image->hdr.aspect);
  64.   }
  65.  
  66.   clrs = image->hdr.clrlen / 3;
  67.   
  68.   /* Calculate bits per pixel in colormap */
  69.   for (i=clrs, bits=1; i > 2; )
  70.   { i >>= 1; bits++; }
  71.  
  72.   if (1 << bits != clrs)
  73.   { fprintf (stderr, "Error, number of colors %d is not a power of 2\n",
  74.          clrs);
  75.     return (0);
  76.   }
  77.   
  78.   if (bits < 1 || bits > 8)
  79.   { fprintf (stderr, "Error, bits per pixel (%d) must be in range 1..8\n",
  80.          bits);
  81.     return (0);
  82.   }
  83.   
  84.   red = image->cm;
  85.   grn = red + clrs;
  86.   blu = grn + clrs;
  87.  
  88.   pixels = image->bm;
  89.   rowlen = image->hdr.rowlen;
  90.   
  91.   /* Copy colormap, and find darkest pixel for background */
  92.   { bkg = 0; sum = 1e9;
  93.  
  94.     for (i=0; i<clrs; i++)
  95.     { rint[i] = red[i];
  96.       gint[i] = grn[i];
  97.       bint[i] = blu[i];
  98.  
  99.       if (red[i] + grn[i] + blu[i] < sum)
  100.       { bkg = i; sum = red[i] + grn[i] + blu[i]; }
  101.     }
  102.   }
  103.  
  104.   fprintf (stderr, "Writing GIF file [%dx%d], %d colors, %d bits, bkg %d\n",
  105.       image->hdr.cols, image->hdr.rows, clrs, bits, bkg);
  106.  
  107. # ifdef DEBUG
  108.   fprintf (stderr, "\n\nColormap:\n");
  109.   for (i=0; i<clrs; i++)
  110.   { fprintf (stderr, "%5d: <%3d, %3d, %3d>\n",
  111.          i, rint[i], gint[i], bint[i]);
  112.   }
  113. # endif
  114.  
  115.   return (GIFEncode (
  116.           wfile,
  117.           image->hdr.cols,        /* width */
  118.           image->hdr.rows,        /* height */
  119.           0,            /* No interlacing */
  120.           bkg,            /* Index of Backgrounf */
  121.           bits,            /* Bits Per pixel */
  122.           rint,            /* Red colormap */
  123.           gint,            /* Green colormap */
  124.           bint,            /* Blue colormap */
  125.           GetGIFPixel ) );        /* Get Pixel value */
  126. }
  127.  
  128. /* Returns value of next pixel */
  129.  
  130. GetGIFPixel (x, y)
  131. int x, y;
  132. {
  133.   return (pixels[y * rowlen + x]);
  134. }
  135.