shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * wrppm.c
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
shun-iwasawa 82a8f5
 * Copyright (C) 1991-1996, Thomas G. Lane.
shun-iwasawa 82a8f5
 * Modified 2009 by Guido Vollbeding.
shun-iwasawa 82a8f5
 * libjpeg-turbo Modifications:
shun-iwasawa 82a8f5
 * Copyright (C) 2017, 2019-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 write output 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 output to
shun-iwasawa 82a8f5
 * an ordinary stdio stream.
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
/*
shun-iwasawa 82a8f5
 * For 12-bit JPEG data, we either downscale the values to 8 bits
shun-iwasawa 82a8f5
 * (to write standard byte-per-sample PPM/PGM files), or output
shun-iwasawa 82a8f5
 * nonstandard word-per-sample PPM/PGM files.  Downscaling is done
shun-iwasawa 82a8f5
 * if PPM_NORAWWORD is defined (this can be done in the Makefile
shun-iwasawa 82a8f5
 * or in jconfig.h).
shun-iwasawa 82a8f5
 * (When the core library supports data precision reduction, a cleaner
shun-iwasawa 82a8f5
 * implementation will be to ask for that instead.)
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#if BITS_IN_JSAMPLE == 8
shun-iwasawa 82a8f5
#define PUTPPMSAMPLE(ptr, v)  *ptr++ = (char)(v)
shun-iwasawa 82a8f5
#define BYTESPERSAMPLE  1
shun-iwasawa 82a8f5
#define PPM_MAXVAL  255
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
#ifdef PPM_NORAWWORD
shun-iwasawa 82a8f5
#define PUTPPMSAMPLE(ptr, v)  *ptr++ = (char)((v) >> (BITS_IN_JSAMPLE - 8))
shun-iwasawa 82a8f5
#define BYTESPERSAMPLE  1
shun-iwasawa 82a8f5
#define PPM_MAXVAL  255
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
/* The word-per-sample format always puts the MSB first. */
shun-iwasawa 82a8f5
#define PUTPPMSAMPLE(ptr, v) { \
shun-iwasawa 82a8f5
  register int val_ = v; \
shun-iwasawa 82a8f5
  *ptr++ = (char)((val_ >> 8) & 0xFF); \
shun-iwasawa 82a8f5
  *ptr++ = (char)(val_ & 0xFF); \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
#define BYTESPERSAMPLE  2
shun-iwasawa 82a8f5
#define PPM_MAXVAL  ((1 << BITS_IN_JSAMPLE) - 1)
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * When JSAMPLE is the same size as char, we can just fwrite() the
shun-iwasawa 82a8f5
 * decompressed data to the PPM or PGM file.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Private version of data destination object */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef struct {
shun-iwasawa 82a8f5
  struct djpeg_dest_struct pub; /* public fields */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Usually these two pointers point to the same place: */
shun-iwasawa 82a8f5
  char *iobuffer;               /* fwrite's I/O buffer */
shun-iwasawa 82a8f5
  JSAMPROW pixrow;              /* decompressor output buffer */
shun-iwasawa 82a8f5
  size_t buffer_width;          /* width of I/O buffer */
shun-iwasawa 82a8f5
  JDIMENSION samples_per_row;   /* JSAMPLEs per output row */
shun-iwasawa 82a8f5
} ppm_dest_struct;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef ppm_dest_struct *ppm_dest_ptr;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Write some pixel data.
shun-iwasawa 82a8f5
 * In this module rows_supplied will always be 1.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * put_pixel_rows handles the "normal" 8-bit case where the decompressor
shun-iwasawa 82a8f5
 * output buffer is physically the same as the fwrite buffer.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shun-iwasawa 82a8f5
               JDIMENSION rows_supplied)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * This code is used when we have to copy the data and apply a pixel
shun-iwasawa 82a8f5
 * format translation.  Typically this only happens in 12-bit mode.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
copy_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shun-iwasawa 82a8f5
                JDIMENSION rows_supplied)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
shun-iwasawa 82a8f5
  register char *bufferptr;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
#if BITS_IN_JSAMPLE != 8 || (!defined(HAVE_UNSIGNED_CHAR) && !defined(__CHAR_UNSIGNED__))
shun-iwasawa 82a8f5
  register JDIMENSION col;
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  ptr = dest->pub.buffer[0];
shun-iwasawa 82a8f5
  bufferptr = dest->iobuffer;
shun-iwasawa 82a8f5
#if BITS_IN_JSAMPLE == 8 && (defined(HAVE_UNSIGNED_CHAR) || defined(__CHAR_UNSIGNED__))
shun-iwasawa 82a8f5
  MEMCOPY(bufferptr, ptr, dest->samples_per_row);
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
  for (col = dest->samples_per_row; col > 0; col--) {
shun-iwasawa 82a8f5
    PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++));
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
  (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Convert extended RGB to RGB.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
put_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, JDIMENSION rows_supplied)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
shun-iwasawa 82a8f5
  register char *bufferptr;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
  register JDIMENSION col;
shun-iwasawa 82a8f5
  register int rindex = rgb_red[cinfo->out_color_space];
shun-iwasawa 82a8f5
  register int gindex = rgb_green[cinfo->out_color_space];
shun-iwasawa 82a8f5
  register int bindex = rgb_blue[cinfo->out_color_space];
shun-iwasawa 82a8f5
  register int ps = rgb_pixelsize[cinfo->out_color_space];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  ptr = dest->pub.buffer[0];
shun-iwasawa 82a8f5
  bufferptr = dest->iobuffer;
shun-iwasawa 82a8f5
  for (col = cinfo->output_width; col > 0; col--) {
shun-iwasawa 82a8f5
    PUTPPMSAMPLE(bufferptr, ptr[rindex]);
shun-iwasawa 82a8f5
    PUTPPMSAMPLE(bufferptr, ptr[gindex]);
shun-iwasawa 82a8f5
    PUTPPMSAMPLE(bufferptr, ptr[bindex]);
shun-iwasawa 82a8f5
    ptr += ps;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Convert CMYK to RGB.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
put_cmyk(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shun-iwasawa 82a8f5
         JDIMENSION rows_supplied)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
shun-iwasawa 82a8f5
  register char *bufferptr;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
  register JDIMENSION col;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  ptr = dest->pub.buffer[0];
shun-iwasawa 82a8f5
  bufferptr = dest->iobuffer;
shun-iwasawa 82a8f5
  for (col = cinfo->output_width; col > 0; col--) {
shun-iwasawa 82a8f5
    JSAMPLE r, g, b, c = *ptr++, m = *ptr++, y = *ptr++, k = *ptr++;
shun-iwasawa 82a8f5
    cmyk_to_rgb(c, m, y, k, &r, &g, &b);
shun-iwasawa 82a8f5
    PUTPPMSAMPLE(bufferptr, r);
shun-iwasawa 82a8f5
    PUTPPMSAMPLE(bufferptr, g);
shun-iwasawa 82a8f5
    PUTPPMSAMPLE(bufferptr, b);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Write some pixel data when color quantization is in effect.
shun-iwasawa 82a8f5
 * We have to demap the color index values to straight data.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
put_demapped_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shun-iwasawa 82a8f5
                 JDIMENSION rows_supplied)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
shun-iwasawa 82a8f5
  register char *bufferptr;
shun-iwasawa 82a8f5
  register int pixval;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
  register JSAMPROW color_map0 = cinfo->colormap[0];
shun-iwasawa 82a8f5
  register JSAMPROW color_map1 = cinfo->colormap[1];
shun-iwasawa 82a8f5
  register JSAMPROW color_map2 = cinfo->colormap[2];
shun-iwasawa 82a8f5
  register JDIMENSION col;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  ptr = dest->pub.buffer[0];
shun-iwasawa 82a8f5
  bufferptr = dest->iobuffer;
shun-iwasawa 82a8f5
  for (col = cinfo->output_width; col > 0; col--) {
shun-iwasawa 82a8f5
    pixval = GETJSAMPLE(*ptr++);
shun-iwasawa 82a8f5
    PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[pixval]));
shun-iwasawa 82a8f5
    PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval]));
shun-iwasawa 82a8f5
    PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval]));
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
put_demapped_gray(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shun-iwasawa 82a8f5
                  JDIMENSION rows_supplied)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
shun-iwasawa 82a8f5
  register char *bufferptr;
shun-iwasawa 82a8f5
  register JSAMPROW ptr;
shun-iwasawa 82a8f5
  register JSAMPROW color_map = cinfo->colormap[0];
shun-iwasawa 82a8f5
  register JDIMENSION col;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  ptr = dest->pub.buffer[0];
shun-iwasawa 82a8f5
  bufferptr = dest->iobuffer;
shun-iwasawa 82a8f5
  for (col = cinfo->output_width; col > 0; col--) {
shun-iwasawa 82a8f5
    PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)]));
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Startup: write the file header.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
start_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Emit file header */
shun-iwasawa 82a8f5
  switch (cinfo->out_color_space) {
shun-iwasawa 82a8f5
  case JCS_GRAYSCALE:
shun-iwasawa 82a8f5
    /* emit header for raw PGM format */
shun-iwasawa 82a8f5
    fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",
shun-iwasawa 82a8f5
            (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_RGB:
shun-iwasawa 82a8f5
  case JCS_EXT_RGB:
shun-iwasawa 82a8f5
  case JCS_EXT_RGBX:
shun-iwasawa 82a8f5
  case JCS_EXT_BGR:
shun-iwasawa 82a8f5
  case JCS_EXT_BGRX:
shun-iwasawa 82a8f5
  case JCS_EXT_XBGR:
shun-iwasawa 82a8f5
  case JCS_EXT_XRGB:
shun-iwasawa 82a8f5
  case JCS_EXT_RGBA:
shun-iwasawa 82a8f5
  case JCS_EXT_BGRA:
shun-iwasawa 82a8f5
  case JCS_EXT_ABGR:
shun-iwasawa 82a8f5
  case JCS_EXT_ARGB:
shun-iwasawa 82a8f5
  case JCS_CMYK:
shun-iwasawa 82a8f5
    if (!IsExtRGB(cinfo->out_color_space) && cinfo->quantize_colors)
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_PPM_COLORSPACE);
shun-iwasawa 82a8f5
    /* emit header for raw PPM format */
shun-iwasawa 82a8f5
    fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",
shun-iwasawa 82a8f5
            (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  default:
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_PPM_COLORSPACE);
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_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  /* Make sure we wrote the output file OK */
shun-iwasawa 82a8f5
  fflush(dinfo->output_file);
shun-iwasawa 82a8f5
  if (ferror(dinfo->output_file))
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_FILE_WRITE);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Re-calculate buffer dimensions based on output dimensions.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
calc_buffer_dimensions_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->out_color_space == JCS_GRAYSCALE)
shun-iwasawa 82a8f5
    dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    dest->samples_per_row = cinfo->output_width * 3;
shun-iwasawa 82a8f5
  dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * sizeof(char));
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * The module selection routine for PPM format output.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(djpeg_dest_ptr)
shun-iwasawa 82a8f5
jinit_write_ppm(j_decompress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  ppm_dest_ptr dest;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Create module interface object, fill in method pointers */
shun-iwasawa 82a8f5
  dest = (ppm_dest_ptr)
shun-iwasawa 82a8f5
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                  sizeof(ppm_dest_struct));
shun-iwasawa 82a8f5
  dest->pub.start_output = start_output_ppm;
shun-iwasawa 82a8f5
  dest->pub.finish_output = finish_output_ppm;
shun-iwasawa 82a8f5
  dest->pub.calc_buffer_dimensions = calc_buffer_dimensions_ppm;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Calculate output image dimensions so we can allocate space */
shun-iwasawa 82a8f5
  jpeg_calc_output_dimensions(cinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Create physical I/O buffer */
shun-iwasawa 82a8f5
  dest->pub.calc_buffer_dimensions(cinfo, (djpeg_dest_ptr)dest);
shun-iwasawa 82a8f5
  dest->iobuffer = (char *)(*cinfo->mem->alloc_small)
shun-iwasawa 82a8f5
    ((j_common_ptr)cinfo, JPOOL_IMAGE, dest->buffer_width);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||
shun-iwasawa 82a8f5
      sizeof(JSAMPLE) != sizeof(char) ||
shun-iwasawa 82a8f5
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
shun-iwasawa 82a8f5
      (cinfo->out_color_space != JCS_EXT_RGB &&
shun-iwasawa 82a8f5
       cinfo->out_color_space != JCS_RGB)) {
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
      cinfo->out_color_space != JCS_EXT_RGB) {
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
    /* When quantizing, we need an output buffer for colormap indexes
shun-iwasawa 82a8f5
     * that's separate from the physical I/O buffer.  We also need a
shun-iwasawa 82a8f5
     * separate buffer if pixel format translation must take place.
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    dest->pub.buffer = (*cinfo->mem->alloc_sarray)
shun-iwasawa 82a8f5
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
       cinfo->output_width * cinfo->output_components, (JDIMENSION)1);
shun-iwasawa 82a8f5
    dest->pub.buffer_height = 1;
shun-iwasawa 82a8f5
    if (!cinfo->quantize_colors) {
shun-iwasawa 82a8f5
      if (IsExtRGB(cinfo->out_color_space))
shun-iwasawa 82a8f5
        dest->pub.put_pixel_rows = put_rgb;
shun-iwasawa 82a8f5
      else if (cinfo->out_color_space == JCS_CMYK)
shun-iwasawa 82a8f5
        dest->pub.put_pixel_rows = put_cmyk;
shun-iwasawa 82a8f5
      else
shun-iwasawa 82a8f5
        dest->pub.put_pixel_rows = copy_pixel_rows;
shun-iwasawa 82a8f5
    } else if (cinfo->out_color_space == JCS_GRAYSCALE)
shun-iwasawa 82a8f5
      dest->pub.put_pixel_rows = put_demapped_gray;
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      dest->pub.put_pixel_rows = put_demapped_rgb;
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    /* We will fwrite() directly from decompressor output buffer. */
shun-iwasawa 82a8f5
    /* Synthesize a JSAMPARRAY pointer structure */
shun-iwasawa 82a8f5
    dest->pixrow = (JSAMPROW)dest->iobuffer;
shun-iwasawa 82a8f5
    dest->pub.buffer = &dest->pixrow;
shun-iwasawa 82a8f5
    dest->pub.buffer_height = 1;
shun-iwasawa 82a8f5
    dest->pub.put_pixel_rows = put_pixel_rows;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return (djpeg_dest_ptr)dest;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#endif /* PPM_SUPPORTED */