kusano 7d535a
/*
kusano 7d535a
 * rdcolmap.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1994-1996, 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 implements djpeg's "-map file" switch.  It reads a source image
kusano 7d535a
 * and constructs a colormap to be supplied to the JPEG decompressor.
kusano 7d535a
 *
kusano 7d535a
 * Currently, these file formats are supported for the map file:
kusano 7d535a
 *   GIF: the contents of the GIF's global colormap are used.
kusano 7d535a
 *   PPM (either text or raw flavor): the entire file is read and
kusano 7d535a
 *      each unique pixel value is entered in the map.
kusano 7d535a
 * Note that reading a large PPM file will be horrendously slow.
kusano 7d535a
 * Typically, a PPM-format map file should contain just one pixel
kusano 7d535a
 * of each desired color.  Such a file can be extracted from an
kusano 7d535a
 * ordinary image PPM file with ppmtomap(1).
kusano 7d535a
 *
kusano 7d535a
 * Rescaling a PPM that has a maxval unequal to MAXJSAMPLE is not
kusano 7d535a
 * currently implemented.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
kusano 7d535a
kusano 7d535a
#ifdef QUANT_2PASS_SUPPORTED	/* otherwise can't quantize to supplied map */
kusano 7d535a
kusano 7d535a
/* Portions of this code are based on the PBMPLUS library, which is:
kusano 7d535a
**
kusano 7d535a
** Copyright (C) 1988 by Jef Poskanzer.
kusano 7d535a
**
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
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Add a (potentially) new color to the color map.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
add_map_entry (j_decompress_ptr cinfo, int R, int G, int B)
kusano 7d535a
{
kusano 7d535a
  JSAMPROW colormap0 = cinfo->colormap[0];
kusano 7d535a
  JSAMPROW colormap1 = cinfo->colormap[1];
kusano 7d535a
  JSAMPROW colormap2 = cinfo->colormap[2];
kusano 7d535a
  int ncolors = cinfo->actual_number_of_colors;
kusano 7d535a
  int index;
kusano 7d535a
kusano 7d535a
  /* Check for duplicate color. */
kusano 7d535a
  for (index = 0; index < ncolors; index++) {
kusano 7d535a
    if (GETJSAMPLE(colormap0[index]) == R &&
kusano 7d535a
	GETJSAMPLE(colormap1[index]) == G &&
kusano 7d535a
	GETJSAMPLE(colormap2[index]) == B)
kusano 7d535a
      return;			/* color is already in map */
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Check for map overflow. */
kusano 7d535a
  if (ncolors >= (MAXJSAMPLE+1))
kusano 7d535a
    ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, (MAXJSAMPLE+1));
kusano 7d535a
kusano 7d535a
  /* OK, add color to map. */
kusano 7d535a
  colormap0[ncolors] = (JSAMPLE) R;
kusano 7d535a
  colormap1[ncolors] = (JSAMPLE) G;
kusano 7d535a
  colormap2[ncolors] = (JSAMPLE) B;
kusano 7d535a
  cinfo->actual_number_of_colors++;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Extract color map from a GIF file.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
read_gif_map (j_decompress_ptr cinfo, FILE * infile)
kusano 7d535a
{
kusano 7d535a
  int header[13];
kusano 7d535a
  int i, colormaplen;
kusano 7d535a
  int R, G, B;
kusano 7d535a
kusano 7d535a
  /* Initial 'G' has already been read by read_color_map */
kusano 7d535a
  /* Read the rest of the GIF header and logical screen descriptor */
kusano 7d535a
  for (i = 1; i < 13; i++) {
kusano 7d535a
    if ((header[i] = getc(infile)) == EOF)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Verify GIF Header */
kusano 7d535a
  if (header[1] != 'I' || header[2] != 'F')
kusano 7d535a
    ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
kusano 7d535a
kusano 7d535a
  /* There must be a global color map. */
kusano 7d535a
  if ((header[10] & 0x80) == 0)
kusano 7d535a
    ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
kusano 7d535a
kusano 7d535a
  /* OK, fetch it. */
kusano 7d535a
  colormaplen = 2 << (header[10] & 0x07);
kusano 7d535a
kusano 7d535a
  for (i = 0; i < colormaplen; i++) {
kusano 7d535a
    R = getc(infile);
kusano 7d535a
    G = getc(infile);
kusano 7d535a
    B = getc(infile);
kusano 7d535a
    if (R == EOF || G == EOF || B == EOF)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
kusano 7d535a
    add_map_entry(cinfo,
kusano 7d535a
		  R << (BITS_IN_JSAMPLE-8),
kusano 7d535a
		  G << (BITS_IN_JSAMPLE-8),
kusano 7d535a
		  B << (BITS_IN_JSAMPLE-8));
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Support routines for reading PPM */
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(int)
kusano 7d535a
pbm_getc (FILE * infile)
kusano 7d535a
/* Read next char, skipping over any comments */
kusano 7d535a
/* A comment/newline sequence is returned as a newline */
kusano 7d535a
{
kusano 7d535a
  register int ch;
kusano 7d535a
  
kusano 7d535a
  ch = getc(infile);
kusano 7d535a
  if (ch == '#') {
kusano 7d535a
    do {
kusano 7d535a
      ch = getc(infile);
kusano 7d535a
    } while (ch != '\n' && ch != EOF);
kusano 7d535a
  }
kusano 7d535a
  return ch;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(unsigned int)
kusano 7d535a
read_pbm_integer (j_decompress_ptr cinfo, FILE * infile)
kusano 7d535a
/* Read an unsigned decimal integer from the PPM file */
kusano 7d535a
/* Swallows one trailing character after the integer */
kusano 7d535a
/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
kusano 7d535a
/* This should not be a problem in practice. */
kusano 7d535a
{
kusano 7d535a
  register int ch;
kusano 7d535a
  register unsigned int val;
kusano 7d535a
  
kusano 7d535a
  /* Skip any leading whitespace */
kusano 7d535a
  do {
kusano 7d535a
    ch = pbm_getc(infile);
kusano 7d535a
    if (ch == EOF)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
kusano 7d535a
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
kusano 7d535a
  
kusano 7d535a
  if (ch < '0' || ch > '9')
kusano 7d535a
    ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
kusano 7d535a
  
kusano 7d535a
  val = ch - '0';
kusano 7d535a
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
kusano 7d535a
    val *= 10;
kusano 7d535a
    val += ch - '0';
kusano 7d535a
  }
kusano 7d535a
  return val;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Extract color map from a PPM file.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
read_ppm_map (j_decompress_ptr cinfo, FILE * infile)
kusano 7d535a
{
kusano 7d535a
  int c;
kusano 7d535a
  unsigned int w, h, maxval, row, col;
kusano 7d535a
  int R, G, B;
kusano 7d535a
kusano 7d535a
  /* Initial 'P' has already been read by read_color_map */
kusano 7d535a
  c = getc(infile);		/* save format discriminator for a sec */
kusano 7d535a
kusano 7d535a
  /* while we fetch the remaining header info */
kusano 7d535a
  w = read_pbm_integer(cinfo, infile);
kusano 7d535a
  h = read_pbm_integer(cinfo, infile);
kusano 7d535a
  maxval = read_pbm_integer(cinfo, infile);
kusano 7d535a
kusano 7d535a
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
kusano 7d535a
    ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
kusano 7d535a
kusano 7d535a
  /* For now, we don't support rescaling from an unusual maxval. */
kusano 7d535a
  if (maxval != (unsigned int) MAXJSAMPLE)
kusano 7d535a
    ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
kusano 7d535a
kusano 7d535a
  switch (c) {
kusano 7d535a
  case '3':			/* it's a text-format PPM file */
kusano 7d535a
    for (row = 0; row < h; row++) {
kusano 7d535a
      for (col = 0; col < w; col++) {
kusano 7d535a
	R = read_pbm_integer(cinfo, infile);
kusano 7d535a
	G = read_pbm_integer(cinfo, infile);
kusano 7d535a
	B = read_pbm_integer(cinfo, infile);
kusano 7d535a
	add_map_entry(cinfo, R, G, B);
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  case '6':			/* it's a raw-format PPM file */
kusano 7d535a
    for (row = 0; row < h; row++) {
kusano 7d535a
      for (col = 0; col < w; col++) {
kusano 7d535a
	R = getc(infile);
kusano 7d535a
	G = getc(infile);
kusano 7d535a
	B = getc(infile);
kusano 7d535a
	if (R == EOF || G == EOF || B == EOF)
kusano 7d535a
	  ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
kusano 7d535a
	add_map_entry(cinfo, R, G, B);
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  default:
kusano 7d535a
    ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Main entry point from djpeg.c.
kusano 7d535a
 *  Input: opened input file (from file name argument on command line).
kusano 7d535a
 *  Output: colormap and actual_number_of_colors fields are set in cinfo.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
read_color_map (j_decompress_ptr cinfo, FILE * infile)
kusano 7d535a
{
kusano 7d535a
  /* Allocate space for a color map of maximum supported size. */
kusano 7d535a
  cinfo->colormap = (*cinfo->mem->alloc_sarray)
kusano 7d535a
    ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
     (JDIMENSION) (MAXJSAMPLE+1), (JDIMENSION) 3);
kusano 7d535a
  cinfo->actual_number_of_colors = 0; /* initialize map to empty */
kusano 7d535a
kusano 7d535a
  /* Read first byte to determine file format */
kusano 7d535a
  switch (getc(infile)) {
kusano 7d535a
  case 'G':
kusano 7d535a
    read_gif_map(cinfo, infile);
kusano 7d535a
    break;
kusano 7d535a
  case 'P':
kusano 7d535a
    read_ppm_map(cinfo, infile);
kusano 7d535a
    break;
kusano 7d535a
  default:
kusano 7d535a
    ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* QUANT_2PASS_SUPPORTED */