shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * rdppm.c
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
shun-iwasawa 82a8f5
 * Copyright (C) 1991-1997, Thomas G. Lane.
shun-iwasawa 82a8f5
 * Modified 2009 by Bill Allombert, Guido Vollbeding.
shun-iwasawa 82a8f5
 * libjpeg-turbo Modifications:
shun-iwasawa 82a8f5
 * Copyright (C) 2015-2017, 2020, D. R. Commander.
shun-iwasawa 82a8f5
 * For conditions of distribution and use, see the accompanying README.ijg
shun-iwasawa 82a8f5
 * file.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file contains routines to read input images in PPM/PGM format.
shun-iwasawa 82a8f5
 * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
shun-iwasawa 82a8f5
 * The PBMPLUS library is NOT required to compile this software
shun-iwasawa 82a8f5
 * (but it is highly useful as a set of PPM image manipulation programs).
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * These routines may need modification for non-Unix environments or
shun-iwasawa 82a8f5
 * specialized applications.  As they stand, they assume input from
shun-iwasawa 82a8f5
 * an ordinary stdio stream.  They further assume that reading begins
shun-iwasawa 82a8f5
 * at the start of the file; start_input may need work if the
shun-iwasawa 82a8f5
 * user interface has already read some data (e.g., to determine that
shun-iwasawa 82a8f5
 * the file is indeed PPM format).
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#include "cmyk.h"
shun-iwasawa 82a8f5
#include "cdjpeg.h"             /* Common decls for cjpeg/djpeg applications */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifdef PPM_SUPPORTED
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Portions of this code are based on the PBMPLUS library, which is:
shun-iwasawa 82a8f5
**
shun-iwasawa 82a8f5
** Copyright (C) 1988 by Jef Poskanzer.
shun-iwasawa 82a8f5
**
shun-iwasawa 82a8f5
** Permission to use, copy, modify, and distribute this software and its
shun-iwasawa 82a8f5
** documentation for any purpose and without fee is hereby granted, provided
shun-iwasawa 82a8f5
** that the above copyright notice appear in all copies and that both that
shun-iwasawa 82a8f5
** copyright notice and this permission notice appear in supporting
shun-iwasawa 82a8f5
** documentation.  This software is provided "as is" without express or
shun-iwasawa 82a8f5
** implied warranty.
shun-iwasawa 82a8f5
*/
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Macros to deal with unsigned chars as efficiently as compiler allows */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifdef HAVE_UNSIGNED_CHAR
shun-iwasawa 82a8f5
typedef unsigned char U_CHAR;
shun-iwasawa 82a8f5
#define UCH(x)  ((int)(x))
shun-iwasawa 82a8f5
#else /* !HAVE_UNSIGNED_CHAR */
shun-iwasawa 82a8f5
#ifdef __CHAR_UNSIGNED__
shun-iwasawa 82a8f5
typedef char U_CHAR;
shun-iwasawa 82a8f5
#define UCH(x)  ((int)(x))
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
typedef char U_CHAR;
shun-iwasawa 82a8f5
#define UCH(x)  ((int)(x) & 0xFF)
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
#endif /* HAVE_UNSIGNED_CHAR */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define ReadOK(file, buffer, len) \
shun-iwasawa 82a8f5
  (JFREAD(file, buffer, len) == ((size_t)(len)))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static int alpha_index[JPEG_NUMCS] = {
shun-iwasawa 82a8f5
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Private version of data source object */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef struct {
shun-iwasawa 82a8f5
  struct cjpeg_source_struct pub; /* public fields */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Usually these two pointers point to the same place: */
shun-iwasawa 82a8f5
  U_CHAR *iobuffer;             /* fread's I/O buffer */
shun-iwasawa 82a8f5
  JSAMPROW pixrow;              /* compressor input buffer */
shun-iwasawa 82a8f5
  size_t buffer_width;          /* width of I/O buffer */
shun-iwasawa 82a8f5
  JSAMPLE *rescale;             /* => maxval-remapping array, or NULL */
shun-iwasawa 82a8f5
  unsigned int maxval;
shun-iwasawa 82a8f5
} ppm_source_struct;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef ppm_source_struct *ppm_source_ptr;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(int)
shun-iwasawa 82a8f5
pbm_getc(FILE *infile)
shun-iwasawa 82a8f5
/* Read next char, skipping over any comments */
shun-iwasawa 82a8f5
/* A comment/newline sequence is returned as a newline */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  register int ch;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  ch = getc(infile);
shun-iwasawa 82a8f5
  if (ch == '#') {
shun-iwasawa 82a8f5
    do {
shun-iwasawa 82a8f5
      ch = getc(infile);
shun-iwasawa 82a8f5
    } while (ch != '\n' && ch != EOF);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return ch;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(unsigned int)
shun-iwasawa 82a8f5
read_pbm_integer(j_compress_ptr cinfo, FILE *infile, unsigned int maxval)
shun-iwasawa 82a8f5
/* Read an unsigned decimal integer from the PPM file */
shun-iwasawa 82a8f5
/* Swallows one trailing character after the integer */
shun-iwasawa 82a8f5
/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
shun-iwasawa 82a8f5
/* This should not be a problem in practice. */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  register int ch;
shun-iwasawa 82a8f5
  register unsigned int val;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Skip any leading whitespace */
shun-iwasawa 82a8f5
  do {
shun-iwasawa 82a8f5
    ch = pbm_getc(infile);
shun-iwasawa 82a8f5
    if (ch == EOF)
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (ch < '0' || ch > '9')
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  val = ch - '0';
shun-iwasawa 82a8f5
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
shun-iwasawa 82a8f5
    val *= 10;
shun-iwasawa 82a8f5
    val += ch - '0';
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (val > maxval)
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return val;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Read one row of pixels.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * We provide several different versions depending on input file format.
shun-iwasawa 82a8f5
 * In all cases, input is scaled to the size of JSAMPLE.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * A really fast path is provided for reading byte/sample raw files with
shun-iwasawa 82a8f5
 * maxval = MAXJSAMPLE, which is the normal case for 8-bit data.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
get_text_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
/* This version is for reading text-format PGM files with any maxval */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
shun-iwasawa 82a8f5
  FILE *infile = source->pub.input_file;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
  register JSAMPLE *rescale = source->rescale;
shun-iwasawa 82a8f5
  JDIMENSION col;
shun-iwasawa 82a8f5
  unsigned int maxval = source->maxval;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  ptr = source->pub.buffer[0];
shun-iwasawa 82a8f5
  for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
shun-iwasawa 82a8f5
  for (col = cinfo->image_width; col > 0; col--) { \
shun-iwasawa 82a8f5
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
shun-iwasawa 82a8f5
    alpha_set_op \
shun-iwasawa 82a8f5
    ptr += ps; \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
get_text_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
/* This version is for reading text-format PGM files with any maxval and
shun-iwasawa 82a8f5
   converting to extended RGB */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
shun-iwasawa 82a8f5
  FILE *infile = source->pub.input_file;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
  register JSAMPLE *rescale = source->rescale;
shun-iwasawa 82a8f5
  JDIMENSION col;
shun-iwasawa 82a8f5
  unsigned int maxval = source->maxval;
shun-iwasawa 82a8f5
  register int rindex = rgb_red[cinfo->in_color_space];
shun-iwasawa 82a8f5
  register int gindex = rgb_green[cinfo->in_color_space];
shun-iwasawa 82a8f5
  register int bindex = rgb_blue[cinfo->in_color_space];
shun-iwasawa 82a8f5
  register int aindex = alpha_index[cinfo->in_color_space];
shun-iwasawa 82a8f5
  register int ps = rgb_pixelsize[cinfo->in_color_space];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  ptr = source->pub.buffer[0];
shun-iwasawa 82a8f5
  if (maxval == MAXJSAMPLE) {
shun-iwasawa 82a8f5
    if (aindex >= 0)
shun-iwasawa 82a8f5
      GRAY_RGB_READ_LOOP(read_pbm_integer(cinfo, infile, maxval),
shun-iwasawa 82a8f5
                         ptr[aindex] = 0xFF;)
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      GRAY_RGB_READ_LOOP(read_pbm_integer(cinfo, infile, maxval),)
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    if (aindex >= 0)
shun-iwasawa 82a8f5
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
shun-iwasawa 82a8f5
                         ptr[aindex] = 0xFF;)
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],)
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
get_text_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
/* This version is for reading text-format PGM files with any maxval and
shun-iwasawa 82a8f5
   converting to CMYK */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
shun-iwasawa 82a8f5
  FILE *infile = source->pub.input_file;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
  register JSAMPLE *rescale = source->rescale;
shun-iwasawa 82a8f5
  JDIMENSION col;
shun-iwasawa 82a8f5
  unsigned int maxval = source->maxval;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  ptr = source->pub.buffer[0];
shun-iwasawa 82a8f5
  if (maxval == MAXJSAMPLE) {
shun-iwasawa 82a8f5
    for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
      JSAMPLE gray = read_pbm_integer(cinfo, infile, maxval);
shun-iwasawa 82a8f5
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
shun-iwasawa 82a8f5
      ptr += 4;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
      JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
shun-iwasawa 82a8f5
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
shun-iwasawa 82a8f5
      ptr += 4;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
shun-iwasawa 82a8f5
  for (col = cinfo->image_width; col > 0; col--) { \
shun-iwasawa 82a8f5
    ptr[rindex] = read_op; \
shun-iwasawa 82a8f5
    ptr[gindex] = read_op; \
shun-iwasawa 82a8f5
    ptr[bindex] = read_op; \
shun-iwasawa 82a8f5
    alpha_set_op \
shun-iwasawa 82a8f5
    ptr += ps; \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
get_text_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
/* This version is for reading text-format PPM files with any maxval */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
shun-iwasawa 82a8f5
  FILE *infile = source->pub.input_file;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
  register JSAMPLE *rescale = source->rescale;
shun-iwasawa 82a8f5
  JDIMENSION col;
shun-iwasawa 82a8f5
  unsigned int maxval = source->maxval;
shun-iwasawa 82a8f5
  register int rindex = rgb_red[cinfo->in_color_space];
shun-iwasawa 82a8f5
  register int gindex = rgb_green[cinfo->in_color_space];
shun-iwasawa 82a8f5
  register int bindex = rgb_blue[cinfo->in_color_space];
shun-iwasawa 82a8f5
  register int aindex = alpha_index[cinfo->in_color_space];
shun-iwasawa 82a8f5
  register int ps = rgb_pixelsize[cinfo->in_color_space];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  ptr = source->pub.buffer[0];
shun-iwasawa 82a8f5
  if (maxval == MAXJSAMPLE) {
shun-iwasawa 82a8f5
    if (aindex >= 0)
shun-iwasawa 82a8f5
      RGB_READ_LOOP(read_pbm_integer(cinfo, infile, maxval),
shun-iwasawa 82a8f5
                    ptr[aindex] = 0xFF;)
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      RGB_READ_LOOP(read_pbm_integer(cinfo, infile, maxval),)
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    if (aindex >= 0)
shun-iwasawa 82a8f5
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
shun-iwasawa 82a8f5
                    ptr[aindex] = 0xFF;)
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],)
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
get_text_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
/* This version is for reading text-format PPM files with any maxval and
shun-iwasawa 82a8f5
   converting to CMYK */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
shun-iwasawa 82a8f5
  FILE *infile = source->pub.input_file;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
  register JSAMPLE *rescale = source->rescale;
shun-iwasawa 82a8f5
  JDIMENSION col;
shun-iwasawa 82a8f5
  unsigned int maxval = source->maxval;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  ptr = source->pub.buffer[0];
shun-iwasawa 82a8f5
  if (maxval == MAXJSAMPLE) {
shun-iwasawa 82a8f5
    for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
      JSAMPLE r = read_pbm_integer(cinfo, infile, maxval);
shun-iwasawa 82a8f5
      JSAMPLE g = read_pbm_integer(cinfo, infile, maxval);
shun-iwasawa 82a8f5
      JSAMPLE b = read_pbm_integer(cinfo, infile, maxval);
shun-iwasawa 82a8f5
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
shun-iwasawa 82a8f5
      ptr += 4;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
      JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
shun-iwasawa 82a8f5
      JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
shun-iwasawa 82a8f5
      JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
shun-iwasawa 82a8f5
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
shun-iwasawa 82a8f5
      ptr += 4;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
get_scaled_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
/* This version is for reading raw-byte-format PGM files with any maxval */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
  register U_CHAR *bufferptr;
shun-iwasawa 82a8f5
  register JSAMPLE *rescale = source->rescale;
shun-iwasawa 82a8f5
  JDIMENSION col;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
  ptr = source->pub.buffer[0];
shun-iwasawa 82a8f5
  bufferptr = source->iobuffer;
shun-iwasawa 82a8f5
  for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
    *ptr++ = rescale[UCH(*bufferptr++)];
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
get_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
/* This version is for reading raw-byte-format PGM files with any maxval
shun-iwasawa 82a8f5
   and converting to extended RGB */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
  register U_CHAR *bufferptr;
shun-iwasawa 82a8f5
  register JSAMPLE *rescale = source->rescale;
shun-iwasawa 82a8f5
  JDIMENSION col;
shun-iwasawa 82a8f5
  unsigned int maxval = source->maxval;
shun-iwasawa 82a8f5
  register int rindex = rgb_red[cinfo->in_color_space];
shun-iwasawa 82a8f5
  register int gindex = rgb_green[cinfo->in_color_space];
shun-iwasawa 82a8f5
  register int bindex = rgb_blue[cinfo->in_color_space];
shun-iwasawa 82a8f5
  register int aindex = alpha_index[cinfo->in_color_space];
shun-iwasawa 82a8f5
  register int ps = rgb_pixelsize[cinfo->in_color_space];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
  ptr = source->pub.buffer[0];
shun-iwasawa 82a8f5
  bufferptr = source->iobuffer;
shun-iwasawa 82a8f5
  if (maxval == MAXJSAMPLE) {
shun-iwasawa 82a8f5
    if (aindex >= 0)
shun-iwasawa 82a8f5
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = 0xFF;)
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      GRAY_RGB_READ_LOOP(*bufferptr++,)
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    if (aindex >= 0)
shun-iwasawa 82a8f5
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = 0xFF;)
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],)
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
/* This version is for reading raw-byte-format PGM files with any maxval
shun-iwasawa 82a8f5
   and converting to CMYK */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
  register U_CHAR *bufferptr;
shun-iwasawa 82a8f5
  register JSAMPLE *rescale = source->rescale;
shun-iwasawa 82a8f5
  JDIMENSION col;
shun-iwasawa 82a8f5
  unsigned int maxval = source->maxval;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
  ptr = source->pub.buffer[0];
shun-iwasawa 82a8f5
  bufferptr = source->iobuffer;
shun-iwasawa 82a8f5
  if (maxval == MAXJSAMPLE) {
shun-iwasawa 82a8f5
    for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
      JSAMPLE gray = *bufferptr++;
shun-iwasawa 82a8f5
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
shun-iwasawa 82a8f5
      ptr += 4;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
      JSAMPLE gray = rescale[UCH(*bufferptr++)];
shun-iwasawa 82a8f5
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
shun-iwasawa 82a8f5
      ptr += 4;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
get_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
/* This version is for reading raw-byte-format PPM files with any maxval */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
  register U_CHAR *bufferptr;
shun-iwasawa 82a8f5
  register JSAMPLE *rescale = source->rescale;
shun-iwasawa 82a8f5
  JDIMENSION col;
shun-iwasawa 82a8f5
  unsigned int maxval = source->maxval;
shun-iwasawa 82a8f5
  register int rindex = rgb_red[cinfo->in_color_space];
shun-iwasawa 82a8f5
  register int gindex = rgb_green[cinfo->in_color_space];
shun-iwasawa 82a8f5
  register int bindex = rgb_blue[cinfo->in_color_space];
shun-iwasawa 82a8f5
  register int aindex = alpha_index[cinfo->in_color_space];
shun-iwasawa 82a8f5
  register int ps = rgb_pixelsize[cinfo->in_color_space];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
  ptr = source->pub.buffer[0];
shun-iwasawa 82a8f5
  bufferptr = source->iobuffer;
shun-iwasawa 82a8f5
  if (maxval == MAXJSAMPLE) {
shun-iwasawa 82a8f5
    if (aindex >= 0)
shun-iwasawa 82a8f5
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = 0xFF;)
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      RGB_READ_LOOP(*bufferptr++,)
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    if (aindex >= 0)
shun-iwasawa 82a8f5
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = 0xFF;)
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],)
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
get_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
/* This version is for reading raw-byte-format PPM files with any maxval and
shun-iwasawa 82a8f5
   converting to CMYK */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
  register U_CHAR *bufferptr;
shun-iwasawa 82a8f5
  register JSAMPLE *rescale = source->rescale;
shun-iwasawa 82a8f5
  JDIMENSION col;
shun-iwasawa 82a8f5
  unsigned int maxval = source->maxval;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
  ptr = source->pub.buffer[0];
shun-iwasawa 82a8f5
  bufferptr = source->iobuffer;
shun-iwasawa 82a8f5
  if (maxval == MAXJSAMPLE) {
shun-iwasawa 82a8f5
    for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
      JSAMPLE r = *bufferptr++;
shun-iwasawa 82a8f5
      JSAMPLE g = *bufferptr++;
shun-iwasawa 82a8f5
      JSAMPLE b = *bufferptr++;
shun-iwasawa 82a8f5
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
shun-iwasawa 82a8f5
      ptr += 4;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
      JSAMPLE r = rescale[UCH(*bufferptr++)];
shun-iwasawa 82a8f5
      JSAMPLE g = rescale[UCH(*bufferptr++)];
shun-iwasawa 82a8f5
      JSAMPLE b = rescale[UCH(*bufferptr++)];
shun-iwasawa 82a8f5
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
shun-iwasawa 82a8f5
      ptr += 4;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
get_raw_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
/* This version is for reading raw-byte-format files with maxval = MAXJSAMPLE.
shun-iwasawa 82a8f5
 * In this case we just read right into the JSAMPLE buffer!
shun-iwasawa 82a8f5
 * Note that same code works for PPM and PGM files.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
get_word_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
/* This version is for reading raw-word-format PGM files with any maxval */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
  register U_CHAR *bufferptr;
shun-iwasawa 82a8f5
  register JSAMPLE *rescale = source->rescale;
shun-iwasawa 82a8f5
  JDIMENSION col;
shun-iwasawa 82a8f5
  unsigned int maxval = source->maxval;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
  ptr = source->pub.buffer[0];
shun-iwasawa 82a8f5
  bufferptr = source->iobuffer;
shun-iwasawa 82a8f5
  for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
    register unsigned int temp;
shun-iwasawa 82a8f5
    temp  = UCH(*bufferptr++) << 8;
shun-iwasawa 82a8f5
    temp |= UCH(*bufferptr++);
shun-iwasawa 82a8f5
    if (temp > maxval)
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
shun-iwasawa 82a8f5
    *ptr++ = rescale[temp];
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
get_word_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
/* This version is for reading raw-word-format PPM files with any maxval */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
  register U_CHAR *bufferptr;
shun-iwasawa 82a8f5
  register JSAMPLE *rescale = source->rescale;
shun-iwasawa 82a8f5
  JDIMENSION col;
shun-iwasawa 82a8f5
  unsigned int maxval = source->maxval;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
  ptr = source->pub.buffer[0];
shun-iwasawa 82a8f5
  bufferptr = source->iobuffer;
shun-iwasawa 82a8f5
  for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
    register unsigned int temp;
shun-iwasawa 82a8f5
    temp  = UCH(*bufferptr++) << 8;
shun-iwasawa 82a8f5
    temp |= UCH(*bufferptr++);
shun-iwasawa 82a8f5
    if (temp > maxval)
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
shun-iwasawa 82a8f5
    *ptr++ = rescale[temp];
shun-iwasawa 82a8f5
    temp  = UCH(*bufferptr++) << 8;
shun-iwasawa 82a8f5
    temp |= UCH(*bufferptr++);
shun-iwasawa 82a8f5
    if (temp > maxval)
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
shun-iwasawa 82a8f5
    *ptr++ = rescale[temp];
shun-iwasawa 82a8f5
    temp  = UCH(*bufferptr++) << 8;
shun-iwasawa 82a8f5
    temp |= UCH(*bufferptr++);
shun-iwasawa 82a8f5
    if (temp > maxval)
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
shun-iwasawa 82a8f5
    *ptr++ = rescale[temp];
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Read the file header; return image size and component count.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
start_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
shun-iwasawa 82a8f5
  int c;
shun-iwasawa 82a8f5
  unsigned int w, h, maxval;
shun-iwasawa 82a8f5
  boolean need_iobuffer, use_raw_buffer, need_rescale;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (getc(source->pub.input_file) != 'P')
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_PPM_NOT);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  c = getc(source->pub.input_file); /* subformat discriminator character */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* detect unsupported variants (ie, PBM) before trying to read header */
shun-iwasawa 82a8f5
  switch (c) {
shun-iwasawa 82a8f5
  case '2':                     /* it's a text-format PGM file */
shun-iwasawa 82a8f5
  case '3':                     /* it's a text-format PPM file */
shun-iwasawa 82a8f5
  case '5':                     /* it's a raw-format PGM file */
shun-iwasawa 82a8f5
  case '6':                     /* it's a raw-format PPM file */
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  default:
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_PPM_NOT);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* fetch the remaining header info */
shun-iwasawa 82a8f5
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
shun-iwasawa 82a8f5
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
shun-iwasawa 82a8f5
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_PPM_NOT);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
shun-iwasawa 82a8f5
  cinfo->image_width = (JDIMENSION)w;
shun-iwasawa 82a8f5
  cinfo->image_height = (JDIMENSION)h;
shun-iwasawa 82a8f5
  source->maxval = maxval;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* initialize flags to most common settings */
shun-iwasawa 82a8f5
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
shun-iwasawa 82a8f5
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
shun-iwasawa 82a8f5
  need_rescale = TRUE;          /* do we need a rescale array? */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  switch (c) {
shun-iwasawa 82a8f5
  case '2':                     /* it's a text-format PGM file */
shun-iwasawa 82a8f5
    if (cinfo->in_color_space == JCS_UNKNOWN)
shun-iwasawa 82a8f5
      cinfo->in_color_space = JCS_GRAYSCALE;
shun-iwasawa 82a8f5
    TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h);
shun-iwasawa 82a8f5
    if (cinfo->in_color_space == JCS_GRAYSCALE)
shun-iwasawa 82a8f5
      source->pub.get_pixel_rows = get_text_gray_row;
shun-iwasawa 82a8f5
    else if (IsExtRGB(cinfo->in_color_space))
shun-iwasawa 82a8f5
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
shun-iwasawa 82a8f5
    else if (cinfo->in_color_space == JCS_CMYK)
shun-iwasawa 82a8f5
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
shun-iwasawa 82a8f5
    need_iobuffer = FALSE;
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  case '3':                     /* it's a text-format PPM file */
shun-iwasawa 82a8f5
    if (cinfo->in_color_space == JCS_UNKNOWN)
shun-iwasawa 82a8f5
      cinfo->in_color_space = JCS_EXT_RGB;
shun-iwasawa 82a8f5
    TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h);
shun-iwasawa 82a8f5
    if (IsExtRGB(cinfo->in_color_space))
shun-iwasawa 82a8f5
      source->pub.get_pixel_rows = get_text_rgb_row;
shun-iwasawa 82a8f5
    else if (cinfo->in_color_space == JCS_CMYK)
shun-iwasawa 82a8f5
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
shun-iwasawa 82a8f5
    need_iobuffer = FALSE;
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  case '5':                     /* it's a raw-format PGM file */
shun-iwasawa 82a8f5
    if (cinfo->in_color_space == JCS_UNKNOWN)
shun-iwasawa 82a8f5
      cinfo->in_color_space = JCS_GRAYSCALE;
shun-iwasawa 82a8f5
    TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
shun-iwasawa 82a8f5
    if (maxval > 255) {
shun-iwasawa 82a8f5
      source->pub.get_pixel_rows = get_word_gray_row;
shun-iwasawa 82a8f5
    } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR) &&
shun-iwasawa 82a8f5
               cinfo->in_color_space == JCS_GRAYSCALE) {
shun-iwasawa 82a8f5
      source->pub.get_pixel_rows = get_raw_row;
shun-iwasawa 82a8f5
      use_raw_buffer = TRUE;
shun-iwasawa 82a8f5
      need_rescale = FALSE;
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      if (cinfo->in_color_space == JCS_GRAYSCALE)
shun-iwasawa 82a8f5
        source->pub.get_pixel_rows = get_scaled_gray_row;
shun-iwasawa 82a8f5
      else if (IsExtRGB(cinfo->in_color_space))
shun-iwasawa 82a8f5
        source->pub.get_pixel_rows = get_gray_rgb_row;
shun-iwasawa 82a8f5
      else if (cinfo->in_color_space == JCS_CMYK)
shun-iwasawa 82a8f5
        source->pub.get_pixel_rows = get_gray_cmyk_row;
shun-iwasawa 82a8f5
      else
shun-iwasawa 82a8f5
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  case '6':                     /* it's a raw-format PPM file */
shun-iwasawa 82a8f5
    if (cinfo->in_color_space == JCS_UNKNOWN)
shun-iwasawa 82a8f5
      cinfo->in_color_space = JCS_EXT_RGB;
shun-iwasawa 82a8f5
    TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
shun-iwasawa 82a8f5
    if (maxval > 255) {
shun-iwasawa 82a8f5
      source->pub.get_pixel_rows = get_word_rgb_row;
shun-iwasawa 82a8f5
    } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR) &&
shun-iwasawa 82a8f5
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
shun-iwasawa 82a8f5
               (cinfo->in_color_space == JCS_EXT_RGB ||
shun-iwasawa 82a8f5
                cinfo->in_color_space == JCS_RGB)) {
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
               cinfo->in_color_space == JCS_EXT_RGB) {
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
      source->pub.get_pixel_rows = get_raw_row;
shun-iwasawa 82a8f5
      use_raw_buffer = TRUE;
shun-iwasawa 82a8f5
      need_rescale = FALSE;
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      if (IsExtRGB(cinfo->in_color_space))
shun-iwasawa 82a8f5
        source->pub.get_pixel_rows = get_rgb_row;
shun-iwasawa 82a8f5
      else if (cinfo->in_color_space == JCS_CMYK)
shun-iwasawa 82a8f5
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
shun-iwasawa 82a8f5
      else
shun-iwasawa 82a8f5
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (IsExtRGB(cinfo->in_color_space))
shun-iwasawa 82a8f5
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
shun-iwasawa 82a8f5
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
shun-iwasawa 82a8f5
    cinfo->input_components = 1;
shun-iwasawa 82a8f5
  else if (cinfo->in_color_space == JCS_CMYK)
shun-iwasawa 82a8f5
    cinfo->input_components = 4;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
shun-iwasawa 82a8f5
  if (need_iobuffer) {
shun-iwasawa 82a8f5
    if (c == '6')
shun-iwasawa 82a8f5
      source->buffer_width = (size_t)w * 3 *
shun-iwasawa 82a8f5
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      source->buffer_width = (size_t)w *
shun-iwasawa 82a8f5
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
shun-iwasawa 82a8f5
    source->iobuffer = (U_CHAR *)
shun-iwasawa 82a8f5
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                  source->buffer_width);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Create compressor input buffer. */
shun-iwasawa 82a8f5
  if (use_raw_buffer) {
shun-iwasawa 82a8f5
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
shun-iwasawa 82a8f5
    /* Synthesize a JSAMPARRAY pointer structure */
shun-iwasawa 82a8f5
    source->pixrow = (JSAMPROW)source->iobuffer;
shun-iwasawa 82a8f5
    source->pub.buffer = &source->pixrow;
shun-iwasawa 82a8f5
    source->pub.buffer_height = 1;
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    /* Need to translate anyway, so make a separate sample buffer. */
shun-iwasawa 82a8f5
    source->pub.buffer = (*cinfo->mem->alloc_sarray)
shun-iwasawa 82a8f5
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
shun-iwasawa 82a8f5
    source->pub.buffer_height = 1;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Compute the rescaling array if required. */
shun-iwasawa 82a8f5
  if (need_rescale) {
shun-iwasawa 82a8f5
    long val, half_maxval;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
shun-iwasawa 82a8f5
    source->rescale = (JSAMPLE *)
shun-iwasawa 82a8f5
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
shun-iwasawa 82a8f5
                                           sizeof(JSAMPLE)));
shun-iwasawa 82a8f5
    half_maxval = maxval / 2;
shun-iwasawa 82a8f5
    for (val = 0; val <= (long)maxval; val++) {
shun-iwasawa 82a8f5
      /* The multiplication here must be done in 32 bits to avoid overflow */
shun-iwasawa 82a8f5
      source->rescale[val] = (JSAMPLE)((val * MAXJSAMPLE + half_maxval) /
shun-iwasawa 82a8f5
                                        maxval);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Finish up at the end of the file.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
finish_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  /* no work */
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * The module selection routine for PPM format input.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(cjpeg_source_ptr)
shun-iwasawa 82a8f5
jinit_read_ppm(j_compress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_source_ptr source;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Create module interface object */
shun-iwasawa 82a8f5
  source = (ppm_source_ptr)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                sizeof(ppm_source_struct));
shun-iwasawa 82a8f5
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
shun-iwasawa 82a8f5
  source->pub.start_input = start_input_ppm;
shun-iwasawa 82a8f5
  source->pub.finish_input = finish_input_ppm;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return (cjpeg_source_ptr)source;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#endif /* PPM_SUPPORTED */