kusano 7d535a
/*
kusano 7d535a
 * wrgif.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1991-1997, Thomas G. Lane.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains routines to write output images in GIF format.
kusano 7d535a
 *
kusano 7d535a
 **************************************************************************
kusano 7d535a
 * NOTE: to avoid entanglements with Unisys' patent on LZW compression,   *
kusano 7d535a
 * this code has been modified to output "uncompressed GIF" files.        *
kusano 7d535a
 * There is no trace of the LZW algorithm in this file.                   *
kusano 7d535a
 **************************************************************************
kusano 7d535a
 *
kusano 7d535a
 * These routines may need modification for non-Unix environments or
kusano 7d535a
 * specialized applications.  As they stand, they assume output to
kusano 7d535a
 * an ordinary stdio stream.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * This code is loosely based on ppmtogif from the PBMPLUS distribution
kusano 7d535a
 * of Feb. 1991.  That file contains the following copyright notice:
kusano 7d535a
 *    Based on GIFENCODE by David Rowley <mgardi@watdscu.waterloo.edu>.</mgardi@watdscu.waterloo.edu>
kusano 7d535a
 *    Lempel-Ziv compression based on "compress" by Spencer W. Thomas et al.
kusano 7d535a
 *    Copyright (C) 1989 by Jef Poskanzer.
kusano 7d535a
 *    Permission to use, copy, modify, and distribute this software and its
kusano 7d535a
 *    documentation for any purpose and without fee is hereby granted, provided
kusano 7d535a
 *    that the above copyright notice appear in all copies and that both that
kusano 7d535a
 *    copyright notice and this permission notice appear in supporting
kusano 7d535a
 *    documentation.  This software is provided "as is" without express or
kusano 7d535a
 *    implied warranty.
kusano 7d535a
 *
kusano 7d535a
 * We are also required to state that
kusano 7d535a
 *    "The Graphics Interchange Format(c) is the Copyright property of
kusano 7d535a
 *    CompuServe Incorporated. GIF(sm) is a Service Mark property of
kusano 7d535a
 *    CompuServe Incorporated."
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
kusano 7d535a
kusano 7d535a
#ifdef GIF_SUPPORTED
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Private version of data destination object */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct djpeg_dest_struct pub;	/* public fields */
kusano 7d535a
kusano 7d535a
  j_decompress_ptr cinfo;	/* back link saves passing separate parm */
kusano 7d535a
kusano 7d535a
  /* State for packing variable-width codes into a bitstream */
kusano 7d535a
  int n_bits;			/* current number of bits/code */
kusano 7d535a
  int maxcode;			/* maximum code, given n_bits */
kusano 7d535a
  INT32 cur_accum;		/* holds bits not yet output */
kusano 7d535a
  int cur_bits;			/* # of bits in cur_accum */
kusano 7d535a
kusano 7d535a
  /* State for GIF code assignment */
kusano 7d535a
  int ClearCode;		/* clear code (doesn't change) */
kusano 7d535a
  int EOFCode;			/* EOF code (ditto) */
kusano 7d535a
  int code_counter;		/* counts output symbols */
kusano 7d535a
kusano 7d535a
  /* GIF data packet construction buffer */
kusano 7d535a
  int bytesinpkt;		/* # of bytes in current packet */
kusano 7d535a
  char packetbuf[256];		/* workspace for accumulating packet */
kusano 7d535a
kusano 7d535a
} gif_dest_struct;
kusano 7d535a
kusano 7d535a
typedef gif_dest_struct * gif_dest_ptr;
kusano 7d535a
kusano 7d535a
/* Largest value that will fit in N bits */
kusano 7d535a
#define MAXCODE(n_bits)	((1 << (n_bits)) - 1)
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Routines to package finished data bytes into GIF data blocks.
kusano 7d535a
 * A data block consists of a count byte (1..255) and that many data bytes.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
flush_packet (gif_dest_ptr dinfo)
kusano 7d535a
/* flush any accumulated data */
kusano 7d535a
{
kusano 7d535a
  if (dinfo->bytesinpkt > 0) {	/* never write zero-length packet */
kusano 7d535a
    dinfo->packetbuf[0] = (char) dinfo->bytesinpkt++;
kusano 7d535a
    if (JFWRITE(dinfo->pub.output_file, dinfo->packetbuf, dinfo->bytesinpkt)
kusano 7d535a
	!= (size_t) dinfo->bytesinpkt)
kusano 7d535a
      ERREXIT(dinfo->cinfo, JERR_FILE_WRITE);
kusano 7d535a
    dinfo->bytesinpkt = 0;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Add a character to current packet; flush to disk if necessary */
kusano 7d535a
#define CHAR_OUT(dinfo,c)  \
kusano 7d535a
	{ (dinfo)->packetbuf[++(dinfo)->bytesinpkt] = (char) (c);  \
kusano 7d535a
	    if ((dinfo)->bytesinpkt >= 255)  \
kusano 7d535a
	      flush_packet(dinfo);  \
kusano 7d535a
	}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Routine to convert variable-width codes into a byte stream */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
output (gif_dest_ptr dinfo, int code)
kusano 7d535a
/* Emit a code of n_bits bits */
kusano 7d535a
/* Uses cur_accum and cur_bits to reblock into 8-bit bytes */
kusano 7d535a
{
kusano 7d535a
  dinfo->cur_accum |= ((INT32) code) << dinfo->cur_bits;
kusano 7d535a
  dinfo->cur_bits += dinfo->n_bits;
kusano 7d535a
kusano 7d535a
  while (dinfo->cur_bits >= 8) {
kusano 7d535a
    CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF);
kusano 7d535a
    dinfo->cur_accum >>= 8;
kusano 7d535a
    dinfo->cur_bits -= 8;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* The pseudo-compression algorithm.
kusano 7d535a
 *
kusano 7d535a
 * In this module we simply output each pixel value as a separate symbol;
kusano 7d535a
 * thus, no compression occurs.  In fact, there is expansion of one bit per
kusano 7d535a
 * pixel, because we use a symbol width one bit wider than the pixel width.
kusano 7d535a
 *
kusano 7d535a
 * GIF ordinarily uses variable-width symbols, and the decoder will expect
kusano 7d535a
 * to ratchet up the symbol width after a fixed number of symbols.
kusano 7d535a
 * To simplify the logic and keep the expansion penalty down, we emit a
kusano 7d535a
 * GIF Clear code to reset the decoder just before the width would ratchet up.
kusano 7d535a
 * Thus, all the symbols in the output file will have the same bit width.
kusano 7d535a
 * Note that emitting the Clear codes at the right times is a mere matter of
kusano 7d535a
 * counting output symbols and is in no way dependent on the LZW patent.
kusano 7d535a
 *
kusano 7d535a
 * With a small basic pixel width (low color count), Clear codes will be
kusano 7d535a
 * needed very frequently, causing the file to expand even more.  So this
kusano 7d535a
 * simplistic approach wouldn't work too well on bilevel images, for example.
kusano 7d535a
 * But for output of JPEG conversions the pixel width will usually be 8 bits
kusano 7d535a
 * (129 to 256 colors), so the overhead added by Clear symbols is only about
kusano 7d535a
 * one symbol in every 256.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
compress_init (gif_dest_ptr dinfo, int i_bits)
kusano 7d535a
/* Initialize pseudo-compressor */
kusano 7d535a
{
kusano 7d535a
  /* init all the state variables */
kusano 7d535a
  dinfo->n_bits = i_bits;
kusano 7d535a
  dinfo->maxcode = MAXCODE(dinfo->n_bits);
kusano 7d535a
  dinfo->ClearCode = (1 << (i_bits - 1));
kusano 7d535a
  dinfo->EOFCode = dinfo->ClearCode + 1;
kusano 7d535a
  dinfo->code_counter = dinfo->ClearCode + 2;
kusano 7d535a
  /* init output buffering vars */
kusano 7d535a
  dinfo->bytesinpkt = 0;
kusano 7d535a
  dinfo->cur_accum = 0;
kusano 7d535a
  dinfo->cur_bits = 0;
kusano 7d535a
  /* GIF specifies an initial Clear code */
kusano 7d535a
  output(dinfo, dinfo->ClearCode);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
compress_pixel (gif_dest_ptr dinfo, int c)
kusano 7d535a
/* Accept and "compress" one pixel value.
kusano 7d535a
 * The given value must be less than n_bits wide.
kusano 7d535a
 */
kusano 7d535a
{
kusano 7d535a
  /* Output the given pixel value as a symbol. */
kusano 7d535a
  output(dinfo, c);
kusano 7d535a
  /* Issue Clear codes often enough to keep the reader from ratcheting up
kusano 7d535a
   * its symbol size.
kusano 7d535a
   */
kusano 7d535a
  if (dinfo->code_counter < dinfo->maxcode) {
kusano 7d535a
    dinfo->code_counter++;
kusano 7d535a
  } else {
kusano 7d535a
    output(dinfo, dinfo->ClearCode);
kusano 7d535a
    dinfo->code_counter = dinfo->ClearCode + 2;	/* reset the counter */
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
compress_term (gif_dest_ptr dinfo)
kusano 7d535a
/* Clean up at end */
kusano 7d535a
{
kusano 7d535a
  /* Send an EOF code */
kusano 7d535a
  output(dinfo, dinfo->EOFCode);
kusano 7d535a
  /* Flush the bit-packing buffer */
kusano 7d535a
  if (dinfo->cur_bits > 0) {
kusano 7d535a
    CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF);
kusano 7d535a
  }
kusano 7d535a
  /* Flush the packet buffer */
kusano 7d535a
  flush_packet(dinfo);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* GIF header construction */
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
put_word (gif_dest_ptr dinfo, unsigned int w)
kusano 7d535a
/* Emit a 16-bit word, LSB first */
kusano 7d535a
{
kusano 7d535a
  putc(w & 0xFF, dinfo->pub.output_file);
kusano 7d535a
  putc((w >> 8) & 0xFF, dinfo->pub.output_file);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
put_3bytes (gif_dest_ptr dinfo, int val)
kusano 7d535a
/* Emit 3 copies of same byte value --- handy subr for colormap construction */
kusano 7d535a
{
kusano 7d535a
  putc(val, dinfo->pub.output_file);
kusano 7d535a
  putc(val, dinfo->pub.output_file);
kusano 7d535a
  putc(val, dinfo->pub.output_file);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
emit_header (gif_dest_ptr dinfo, int num_colors, JSAMPARRAY colormap)
kusano 7d535a
/* Output the GIF file header, including color map */
kusano 7d535a
/* If colormap==NULL, synthesize a gray-scale colormap */
kusano 7d535a
{
kusano 7d535a
  int BitsPerPixel, ColorMapSize, InitCodeSize, FlagByte;
kusano 7d535a
  int cshift = dinfo->cinfo->data_precision - 8;
kusano 7d535a
  int i;
kusano 7d535a
kusano 7d535a
  if (num_colors > 256)
kusano 7d535a
    ERREXIT1(dinfo->cinfo, JERR_TOO_MANY_COLORS, num_colors);
kusano 7d535a
  /* Compute bits/pixel and related values */
kusano 7d535a
  BitsPerPixel = 1;
kusano 7d535a
  while (num_colors > (1 << BitsPerPixel))
kusano 7d535a
    BitsPerPixel++;
kusano 7d535a
  ColorMapSize = 1 << BitsPerPixel;
kusano 7d535a
  if (BitsPerPixel <= 1)
kusano 7d535a
    InitCodeSize = 2;
kusano 7d535a
  else
kusano 7d535a
    InitCodeSize = BitsPerPixel;
kusano 7d535a
  /*
kusano 7d535a
   * Write the GIF header.
kusano 7d535a
   * Note that we generate a plain GIF87 header for maximum compatibility.
kusano 7d535a
   */
kusano 7d535a
  putc('G', dinfo->pub.output_file);
kusano 7d535a
  putc('I', dinfo->pub.output_file);
kusano 7d535a
  putc('F', dinfo->pub.output_file);
kusano 7d535a
  putc('8', dinfo->pub.output_file);
kusano 7d535a
  putc('7', dinfo->pub.output_file);
kusano 7d535a
  putc('a', dinfo->pub.output_file);
kusano 7d535a
  /* Write the Logical Screen Descriptor */
kusano 7d535a
  put_word(dinfo, (unsigned int) dinfo->cinfo->output_width);
kusano 7d535a
  put_word(dinfo, (unsigned int) dinfo->cinfo->output_height);
kusano 7d535a
  FlagByte = 0x80;		/* Yes, there is a global color table */
kusano 7d535a
  FlagByte |= (BitsPerPixel-1) << 4; /* color resolution */
kusano 7d535a
  FlagByte |= (BitsPerPixel-1);	/* size of global color table */
kusano 7d535a
  putc(FlagByte, dinfo->pub.output_file);
kusano 7d535a
  putc(0, dinfo->pub.output_file); /* Background color index */
kusano 7d535a
  putc(0, dinfo->pub.output_file); /* Reserved (aspect ratio in GIF89) */
kusano 7d535a
  /* Write the Global Color Map */
kusano 7d535a
  /* If the color map is more than 8 bits precision, */
kusano 7d535a
  /* we reduce it to 8 bits by shifting */
kusano 7d535a
  for (i=0; i < ColorMapSize; i++) {
kusano 7d535a
    if (i < num_colors) {
kusano 7d535a
      if (colormap != NULL) {
kusano 7d535a
	if (dinfo->cinfo->out_color_space == JCS_RGB) {
kusano 7d535a
	  /* Normal case: RGB color map */
kusano 7d535a
	  putc(GETJSAMPLE(colormap[0][i]) >> cshift, dinfo->pub.output_file);
kusano 7d535a
	  putc(GETJSAMPLE(colormap[1][i]) >> cshift, dinfo->pub.output_file);
kusano 7d535a
	  putc(GETJSAMPLE(colormap[2][i]) >> cshift, dinfo->pub.output_file);
kusano 7d535a
	} else {
kusano 7d535a
	  /* Grayscale "color map": possible if quantizing grayscale image */
kusano 7d535a
	  put_3bytes(dinfo, GETJSAMPLE(colormap[0][i]) >> cshift);
kusano 7d535a
	}
kusano 7d535a
      } else {
kusano 7d535a
	/* Create a gray-scale map of num_colors values, range 0..255 */
kusano 7d535a
	put_3bytes(dinfo, (i * 255 + (num_colors-1)/2) / (num_colors-1));
kusano 7d535a
      }
kusano 7d535a
    } else {
kusano 7d535a
      /* fill out the map to a power of 2 */
kusano 7d535a
      put_3bytes(dinfo, 0);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
  /* Write image separator and Image Descriptor */
kusano 7d535a
  putc(',', dinfo->pub.output_file); /* separator */
kusano 7d535a
  put_word(dinfo, 0);		/* left/top offset */
kusano 7d535a
  put_word(dinfo, 0);
kusano 7d535a
  put_word(dinfo, (unsigned int) dinfo->cinfo->output_width); /* image size */
kusano 7d535a
  put_word(dinfo, (unsigned int) dinfo->cinfo->output_height);
kusano 7d535a
  /* flag byte: not interlaced, no local color map */
kusano 7d535a
  putc(0x00, dinfo->pub.output_file);
kusano 7d535a
  /* Write Initial Code Size byte */
kusano 7d535a
  putc(InitCodeSize, dinfo->pub.output_file);
kusano 7d535a
kusano 7d535a
  /* Initialize for "compression" of image data */
kusano 7d535a
  compress_init(dinfo, InitCodeSize+1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Startup: write the file header.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
start_output_gif (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
kusano 7d535a
{
kusano 7d535a
  gif_dest_ptr dest = (gif_dest_ptr) dinfo;
kusano 7d535a
kusano 7d535a
  if (cinfo->quantize_colors)
kusano 7d535a
    emit_header(dest, cinfo->actual_number_of_colors, cinfo->colormap);
kusano 7d535a
  else
kusano 7d535a
    emit_header(dest, 256, (JSAMPARRAY) NULL);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Write some pixel data.
kusano 7d535a
 * In this module rows_supplied will always be 1.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
kusano 7d535a
		JDIMENSION rows_supplied)
kusano 7d535a
{
kusano 7d535a
  gif_dest_ptr dest = (gif_dest_ptr) dinfo;
kusano 7d535a
  register JSAMPROW ptr;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
kusano 7d535a
  ptr = dest->pub.buffer[0];
kusano 7d535a
  for (col = cinfo->output_width; col > 0; col--) {
kusano 7d535a
    compress_pixel(dest, GETJSAMPLE(*ptr++));
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Finish up at the end of the file.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
finish_output_gif (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
kusano 7d535a
{
kusano 7d535a
  gif_dest_ptr dest = (gif_dest_ptr) dinfo;
kusano 7d535a
kusano 7d535a
  /* Flush "compression" mechanism */
kusano 7d535a
  compress_term(dest);
kusano 7d535a
  /* Write a zero-length data block to end the series */
kusano 7d535a
  putc(0, dest->pub.output_file);
kusano 7d535a
  /* Write the GIF terminator mark */
kusano 7d535a
  putc(';', dest->pub.output_file);
kusano 7d535a
  /* Make sure we wrote the output file OK */
kusano 7d535a
  fflush(dest->pub.output_file);
kusano 7d535a
  if (ferror(dest->pub.output_file))
kusano 7d535a
    ERREXIT(cinfo, JERR_FILE_WRITE);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * The module selection routine for GIF format output.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(djpeg_dest_ptr)
kusano 7d535a
jinit_write_gif (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  gif_dest_ptr dest;
kusano 7d535a
kusano 7d535a
  /* Create module interface object, fill in method pointers */
kusano 7d535a
  dest = (gif_dest_ptr)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				  SIZEOF(gif_dest_struct));
kusano 7d535a
  dest->cinfo = cinfo;		/* make back link for subroutines */
kusano 7d535a
  dest->pub.start_output = start_output_gif;
kusano 7d535a
  dest->pub.put_pixel_rows = put_pixel_rows;
kusano 7d535a
  dest->pub.finish_output = finish_output_gif;
kusano 7d535a
kusano 7d535a
  if (cinfo->out_color_space != JCS_GRAYSCALE &&
kusano 7d535a
      cinfo->out_color_space != JCS_RGB)
kusano 7d535a
    ERREXIT(cinfo, JERR_GIF_COLORSPACE);
kusano 7d535a
kusano 7d535a
  /* Force quantization if color or if > 8 bits input */
kusano 7d535a
  if (cinfo->out_color_space != JCS_GRAYSCALE || cinfo->data_precision > 8) {
kusano 7d535a
    /* Force quantization to at most 256 colors */
kusano 7d535a
    cinfo->quantize_colors = TRUE;
kusano 7d535a
    if (cinfo->desired_number_of_colors > 256)
kusano 7d535a
      cinfo->desired_number_of_colors = 256;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Calculate output image dimensions so we can allocate space */
kusano 7d535a
  jpeg_calc_output_dimensions(cinfo);
kusano 7d535a
kusano 7d535a
  if (cinfo->output_components != 1) /* safety check: just one component? */
kusano 7d535a
    ERREXIT(cinfo, JERR_GIF_BUG);
kusano 7d535a
kusano 7d535a
  /* Create decompressor output buffer. */
kusano 7d535a
  dest->pub.buffer = (*cinfo->mem->alloc_sarray)
kusano 7d535a
    ((j_common_ptr) cinfo, JPOOL_IMAGE, cinfo->output_width, (JDIMENSION) 1);
kusano 7d535a
  dest->pub.buffer_height = 1;
kusano 7d535a
kusano 7d535a
  return (djpeg_dest_ptr) dest;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* GIF_SUPPORTED */