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

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