kusano 7d535a
/*
kusano 7d535a
 * rdppm.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1991-1997, Thomas G. Lane.
kusano 7d535a
 * Modified 2009 by Bill Allombert, Guido Vollbeding.
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 read input images in PPM/PGM format.
kusano 7d535a
 * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
kusano 7d535a
 * The PBMPLUS library is NOT required to compile this software
kusano 7d535a
 * (but it is highly useful as a set of PPM image manipulation programs).
kusano 7d535a
 *
kusano 7d535a
 * These routines may need modification for non-Unix environments or
kusano 7d535a
 * specialized applications.  As they stand, they assume input from
kusano 7d535a
 * an ordinary stdio stream.  They further assume that reading begins
kusano 7d535a
 * at the start of the file; start_input may need work if the
kusano 7d535a
 * user interface has already read some data (e.g., to determine that
kusano 7d535a
 * the file is indeed PPM format).
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
kusano 7d535a
kusano 7d535a
#ifdef PPM_SUPPORTED
kusano 7d535a
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
/* Macros to deal with unsigned chars as efficiently as compiler allows */
kusano 7d535a
kusano 7d535a
#ifdef HAVE_UNSIGNED_CHAR
kusano 7d535a
typedef unsigned char U_CHAR;
kusano 7d535a
#define UCH(x)	((int) (x))
kusano 7d535a
#else /* !HAVE_UNSIGNED_CHAR */
kusano 7d535a
#ifdef CHAR_IS_UNSIGNED
kusano 7d535a
typedef char U_CHAR;
kusano 7d535a
#define UCH(x)	((int) (x))
kusano 7d535a
#else
kusano 7d535a
typedef char U_CHAR;
kusano 7d535a
#define UCH(x)	((int) (x) & 0xFF)
kusano 7d535a
#endif
kusano 7d535a
#endif /* HAVE_UNSIGNED_CHAR */
kusano 7d535a
kusano 7d535a
kusano 7d535a
#define	ReadOK(file,buffer,len)	(JFREAD(file,buffer,len) == ((size_t) (len)))
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * On most systems, reading individual bytes with getc() is drastically less
kusano 7d535a
 * efficient than buffering a row at a time with fread().  On PCs, we must
kusano 7d535a
 * allocate the buffer in near data space, because we are assuming small-data
kusano 7d535a
 * memory model, wherein fread() can't reach far memory.  If you need to
kusano 7d535a
 * process very wide images on a PC, you might have to compile in large-memory
kusano 7d535a
 * model, or else replace fread() with a getc() loop --- which will be much
kusano 7d535a
 * slower.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Private version of data source object */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct cjpeg_source_struct pub; /* public fields */
kusano 7d535a
kusano 7d535a
  U_CHAR *iobuffer;		/* non-FAR pointer to I/O buffer */
kusano 7d535a
  JSAMPROW pixrow;		/* FAR pointer to same */
kusano 7d535a
  size_t buffer_width;		/* width of I/O buffer */
kusano 7d535a
  JSAMPLE *rescale;		/* => maxval-remapping array, or NULL */
kusano 7d535a
} ppm_source_struct;
kusano 7d535a
kusano 7d535a
typedef ppm_source_struct * ppm_source_ptr;
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_compress_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_INPUT_EOF);
kusano 7d535a
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
kusano 7d535a
kusano 7d535a
  if (ch < '0' || ch > '9')
kusano 7d535a
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
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
 * Read one row of pixels.
kusano 7d535a
 *
kusano 7d535a
 * We provide several different versions depending on input file format.
kusano 7d535a
 * In all cases, input is scaled to the size of JSAMPLE.
kusano 7d535a
 *
kusano 7d535a
 * A really fast path is provided for reading byte/sample raw files with
kusano 7d535a
 * maxval = MAXJSAMPLE, which is the normal case for 8-bit data.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(JDIMENSION)
kusano 7d535a
get_text_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
kusano 7d535a
/* This version is for reading text-format PGM files with any maxval */
kusano 7d535a
{
kusano 7d535a
  ppm_source_ptr source = (ppm_source_ptr) sinfo;
kusano 7d535a
  FILE * infile = source->pub.input_file;
kusano 7d535a
  register JSAMPROW ptr;
kusano 7d535a
  register JSAMPLE *rescale = source->rescale;
kusano 7d535a
  JDIMENSION col;
kusano 7d535a
kusano 7d535a
  ptr = source->pub.buffer[0];
kusano 7d535a
  for (col = cinfo->image_width; col > 0; col--) {
kusano 7d535a
    *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
kusano 7d535a
  }
kusano 7d535a
  return 1;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(JDIMENSION)
kusano 7d535a
get_text_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
kusano 7d535a
/* This version is for reading text-format PPM files with any maxval */
kusano 7d535a
{
kusano 7d535a
  ppm_source_ptr source = (ppm_source_ptr) sinfo;
kusano 7d535a
  FILE * infile = source->pub.input_file;
kusano 7d535a
  register JSAMPROW ptr;
kusano 7d535a
  register JSAMPLE *rescale = source->rescale;
kusano 7d535a
  JDIMENSION col;
kusano 7d535a
kusano 7d535a
  ptr = source->pub.buffer[0];
kusano 7d535a
  for (col = cinfo->image_width; col > 0; col--) {
kusano 7d535a
    *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
kusano 7d535a
    *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
kusano 7d535a
    *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
kusano 7d535a
  }
kusano 7d535a
  return 1;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(JDIMENSION)
kusano 7d535a
get_scaled_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
kusano 7d535a
/* This version is for reading raw-byte-format PGM files with any maxval */
kusano 7d535a
{
kusano 7d535a
  ppm_source_ptr source = (ppm_source_ptr) sinfo;
kusano 7d535a
  register JSAMPROW ptr;
kusano 7d535a
  register U_CHAR * bufferptr;
kusano 7d535a
  register JSAMPLE *rescale = source->rescale;
kusano 7d535a
  JDIMENSION col;
kusano 7d535a
kusano 7d535a
  if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
kusano 7d535a
    ERREXIT(cinfo, JERR_INPUT_EOF);
kusano 7d535a
  ptr = source->pub.buffer[0];
kusano 7d535a
  bufferptr = source->iobuffer;
kusano 7d535a
  for (col = cinfo->image_width; col > 0; col--) {
kusano 7d535a
    *ptr++ = rescale[UCH(*bufferptr++)];
kusano 7d535a
  }
kusano 7d535a
  return 1;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(JDIMENSION)
kusano 7d535a
get_scaled_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
kusano 7d535a
/* This version is for reading raw-byte-format PPM files with any maxval */
kusano 7d535a
{
kusano 7d535a
  ppm_source_ptr source = (ppm_source_ptr) sinfo;
kusano 7d535a
  register JSAMPROW ptr;
kusano 7d535a
  register U_CHAR * bufferptr;
kusano 7d535a
  register JSAMPLE *rescale = source->rescale;
kusano 7d535a
  JDIMENSION col;
kusano 7d535a
kusano 7d535a
  if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
kusano 7d535a
    ERREXIT(cinfo, JERR_INPUT_EOF);
kusano 7d535a
  ptr = source->pub.buffer[0];
kusano 7d535a
  bufferptr = source->iobuffer;
kusano 7d535a
  for (col = cinfo->image_width; col > 0; col--) {
kusano 7d535a
    *ptr++ = rescale[UCH(*bufferptr++)];
kusano 7d535a
    *ptr++ = rescale[UCH(*bufferptr++)];
kusano 7d535a
    *ptr++ = rescale[UCH(*bufferptr++)];
kusano 7d535a
  }
kusano 7d535a
  return 1;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(JDIMENSION)
kusano 7d535a
get_raw_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
kusano 7d535a
/* This version is for reading raw-byte-format files with maxval = MAXJSAMPLE.
kusano 7d535a
 * In this case we just read right into the JSAMPLE buffer!
kusano 7d535a
 * Note that same code works for PPM and PGM files.
kusano 7d535a
 */
kusano 7d535a
{
kusano 7d535a
  ppm_source_ptr source = (ppm_source_ptr) sinfo;
kusano 7d535a
kusano 7d535a
  if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
kusano 7d535a
    ERREXIT(cinfo, JERR_INPUT_EOF);
kusano 7d535a
  return 1;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(JDIMENSION)
kusano 7d535a
get_word_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
kusano 7d535a
/* This version is for reading raw-word-format PGM files with any maxval */
kusano 7d535a
{
kusano 7d535a
  ppm_source_ptr source = (ppm_source_ptr) sinfo;
kusano 7d535a
  register JSAMPROW ptr;
kusano 7d535a
  register U_CHAR * bufferptr;
kusano 7d535a
  register JSAMPLE *rescale = source->rescale;
kusano 7d535a
  JDIMENSION col;
kusano 7d535a
kusano 7d535a
  if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
kusano 7d535a
    ERREXIT(cinfo, JERR_INPUT_EOF);
kusano 7d535a
  ptr = source->pub.buffer[0];
kusano 7d535a
  bufferptr = source->iobuffer;
kusano 7d535a
  for (col = cinfo->image_width; col > 0; col--) {
kusano 7d535a
    register int temp;
kusano 7d535a
    temp  = UCH(*bufferptr++) << 8;
kusano 7d535a
    temp |= UCH(*bufferptr++);
kusano 7d535a
    *ptr++ = rescale[temp];
kusano 7d535a
  }
kusano 7d535a
  return 1;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(JDIMENSION)
kusano 7d535a
get_word_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
kusano 7d535a
/* This version is for reading raw-word-format PPM files with any maxval */
kusano 7d535a
{
kusano 7d535a
  ppm_source_ptr source = (ppm_source_ptr) sinfo;
kusano 7d535a
  register JSAMPROW ptr;
kusano 7d535a
  register U_CHAR * bufferptr;
kusano 7d535a
  register JSAMPLE *rescale = source->rescale;
kusano 7d535a
  JDIMENSION col;
kusano 7d535a
kusano 7d535a
  if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
kusano 7d535a
    ERREXIT(cinfo, JERR_INPUT_EOF);
kusano 7d535a
  ptr = source->pub.buffer[0];
kusano 7d535a
  bufferptr = source->iobuffer;
kusano 7d535a
  for (col = cinfo->image_width; col > 0; col--) {
kusano 7d535a
    register int temp;
kusano 7d535a
    temp  = UCH(*bufferptr++) << 8;
kusano 7d535a
    temp |= UCH(*bufferptr++);
kusano 7d535a
    *ptr++ = rescale[temp];
kusano 7d535a
    temp  = UCH(*bufferptr++) << 8;
kusano 7d535a
    temp |= UCH(*bufferptr++);
kusano 7d535a
    *ptr++ = rescale[temp];
kusano 7d535a
    temp  = UCH(*bufferptr++) << 8;
kusano 7d535a
    temp |= UCH(*bufferptr++);
kusano 7d535a
    *ptr++ = rescale[temp];
kusano 7d535a
  }
kusano 7d535a
  return 1;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Read the file header; return image size and component count.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
start_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
kusano 7d535a
{
kusano 7d535a
  ppm_source_ptr source = (ppm_source_ptr) sinfo;
kusano 7d535a
  int c;
kusano 7d535a
  unsigned int w, h, maxval;
kusano 7d535a
  boolean need_iobuffer, use_raw_buffer, need_rescale;
kusano 7d535a
kusano 7d535a
  if (getc(source->pub.input_file) != 'P')
kusano 7d535a
    ERREXIT(cinfo, JERR_PPM_NOT);
kusano 7d535a
kusano 7d535a
  c = getc(source->pub.input_file); /* subformat discriminator character */
kusano 7d535a
kusano 7d535a
  /* detect unsupported variants (ie, PBM) before trying to read header */
kusano 7d535a
  switch (c) {
kusano 7d535a
  case '2':			/* it's a text-format PGM file */
kusano 7d535a
  case '3':			/* it's a text-format PPM file */
kusano 7d535a
  case '5':			/* it's a raw-format PGM file */
kusano 7d535a
  case '6':			/* it's a raw-format PPM file */
kusano 7d535a
    break;
kusano 7d535a
  default:
kusano 7d535a
    ERREXIT(cinfo, JERR_PPM_NOT);
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* fetch the remaining header info */
kusano 7d535a
  w = read_pbm_integer(cinfo, source->pub.input_file);
kusano 7d535a
  h = read_pbm_integer(cinfo, source->pub.input_file);
kusano 7d535a
  maxval = read_pbm_integer(cinfo, source->pub.input_file);
kusano 7d535a
kusano 7d535a
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
kusano 7d535a
    ERREXIT(cinfo, JERR_PPM_NOT);
kusano 7d535a
kusano 7d535a
  cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
kusano 7d535a
  cinfo->image_width = (JDIMENSION) w;
kusano 7d535a
  cinfo->image_height = (JDIMENSION) h;
kusano 7d535a
kusano 7d535a
  /* initialize flags to most common settings */
kusano 7d535a
  need_iobuffer = TRUE;		/* do we need an I/O buffer? */
kusano 7d535a
  use_raw_buffer = FALSE;	/* do we map input buffer onto I/O buffer? */
kusano 7d535a
  need_rescale = TRUE;		/* do we need a rescale array? */
kusano 7d535a
kusano 7d535a
  switch (c) {
kusano 7d535a
  case '2':			/* it's a text-format PGM file */
kusano 7d535a
    cinfo->input_components = 1;
kusano 7d535a
    cinfo->in_color_space = JCS_GRAYSCALE;
kusano 7d535a
    TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h);
kusano 7d535a
    source->pub.get_pixel_rows = get_text_gray_row;
kusano 7d535a
    need_iobuffer = FALSE;
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  case '3':			/* it's a text-format PPM file */
kusano 7d535a
    cinfo->input_components = 3;
kusano 7d535a
    cinfo->in_color_space = JCS_RGB;
kusano 7d535a
    TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h);
kusano 7d535a
    source->pub.get_pixel_rows = get_text_rgb_row;
kusano 7d535a
    need_iobuffer = FALSE;
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  case '5':			/* it's a raw-format PGM file */
kusano 7d535a
    cinfo->input_components = 1;
kusano 7d535a
    cinfo->in_color_space = JCS_GRAYSCALE;
kusano 7d535a
    TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
kusano 7d535a
    if (maxval > 255) {
kusano 7d535a
      source->pub.get_pixel_rows = get_word_gray_row;
kusano 7d535a
    } else if (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR)) {
kusano 7d535a
      source->pub.get_pixel_rows = get_raw_row;
kusano 7d535a
      use_raw_buffer = TRUE;
kusano 7d535a
      need_rescale = FALSE;
kusano 7d535a
    } else {
kusano 7d535a
      source->pub.get_pixel_rows = get_scaled_gray_row;
kusano 7d535a
    }
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  case '6':			/* it's a raw-format PPM file */
kusano 7d535a
    cinfo->input_components = 3;
kusano 7d535a
    cinfo->in_color_space = JCS_RGB;
kusano 7d535a
    TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
kusano 7d535a
    if (maxval > 255) {
kusano 7d535a
      source->pub.get_pixel_rows = get_word_rgb_row;
kusano 7d535a
    } else if (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR)) {
kusano 7d535a
      source->pub.get_pixel_rows = get_raw_row;
kusano 7d535a
      use_raw_buffer = TRUE;
kusano 7d535a
      need_rescale = FALSE;
kusano 7d535a
    } else {
kusano 7d535a
      source->pub.get_pixel_rows = get_scaled_rgb_row;
kusano 7d535a
    }
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
kusano 7d535a
  if (need_iobuffer) {
kusano 7d535a
    source->buffer_width = (size_t) w * cinfo->input_components *
kusano 7d535a
      ((maxval<=255) ? SIZEOF(U_CHAR) : (2*SIZEOF(U_CHAR)));
kusano 7d535a
    source->iobuffer = (U_CHAR *)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				  source->buffer_width);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Create compressor input buffer. */
kusano 7d535a
  if (use_raw_buffer) {
kusano 7d535a
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
kusano 7d535a
    /* Synthesize a JSAMPARRAY pointer structure */
kusano 7d535a
    /* Cast here implies near->far pointer conversion on PCs */
kusano 7d535a
    source->pixrow = (JSAMPROW) source->iobuffer;
kusano 7d535a
    source->pub.buffer = & source->pixrow;
kusano 7d535a
    source->pub.buffer_height = 1;
kusano 7d535a
  } else {
kusano 7d535a
    /* Need to translate anyway, so make a separate sample buffer. */
kusano 7d535a
    source->pub.buffer = (*cinfo->mem->alloc_sarray)
kusano 7d535a
      ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
       (JDIMENSION) w * cinfo->input_components, (JDIMENSION) 1);
kusano 7d535a
    source->pub.buffer_height = 1;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Compute the rescaling array if required. */
kusano 7d535a
  if (need_rescale) {
kusano 7d535a
    INT32 val, half_maxval;
kusano 7d535a
kusano 7d535a
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
kusano 7d535a
    source->rescale = (JSAMPLE *)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				  (size_t) (((long) maxval + 1L) * SIZEOF(JSAMPLE)));
kusano 7d535a
    half_maxval = maxval / 2;
kusano 7d535a
    for (val = 0; val <= (INT32) maxval; val++) {
kusano 7d535a
      /* The multiplication here must be done in 32 bits to avoid overflow */
kusano 7d535a
      source->rescale[val] = (JSAMPLE) ((val*MAXJSAMPLE + half_maxval)/maxval);
kusano 7d535a
    }
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_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
kusano 7d535a
{
kusano 7d535a
  /* no work */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * The module selection routine for PPM format input.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(cjpeg_source_ptr)
kusano 7d535a
jinit_read_ppm (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  ppm_source_ptr source;
kusano 7d535a
kusano 7d535a
  /* Create module interface object */
kusano 7d535a
  source = (ppm_source_ptr)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				  SIZEOF(ppm_source_struct));
kusano 7d535a
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
kusano 7d535a
  source->pub.start_input = start_input_ppm;
kusano 7d535a
  source->pub.finish_input = finish_input_ppm;
kusano 7d535a
kusano 7d535a
  return (cjpeg_source_ptr) source;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* PPM_SUPPORTED */