kusano 7d535a
/*
kusano 7d535a
 * wrppm.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1991-1996, Thomas G. Lane.
kusano 7d535a
 * Modified 2009 by 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 write output 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 output to
kusano 7d535a
 * an ordinary stdio stream.
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
/*
kusano 7d535a
 * For 12-bit JPEG data, we either downscale the values to 8 bits
kusano 7d535a
 * (to write standard byte-per-sample PPM/PGM files), or output
kusano 7d535a
 * nonstandard word-per-sample PPM/PGM files.  Downscaling is done
kusano 7d535a
 * if PPM_NORAWWORD is defined (this can be done in the Makefile
kusano 7d535a
 * or in jconfig.h).
kusano 7d535a
 * (When the core library supports data precision reduction, a cleaner
kusano 7d535a
 * implementation will be to ask for that instead.)
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#if BITS_IN_JSAMPLE == 8
kusano 7d535a
#define PUTPPMSAMPLE(ptr,v)  *ptr++ = (char) (v)
kusano 7d535a
#define BYTESPERSAMPLE 1
kusano 7d535a
#define PPM_MAXVAL 255
kusano 7d535a
#else
kusano 7d535a
#ifdef PPM_NORAWWORD
kusano 7d535a
#define PUTPPMSAMPLE(ptr,v)  *ptr++ = (char) ((v) >> (BITS_IN_JSAMPLE-8))
kusano 7d535a
#define BYTESPERSAMPLE 1
kusano 7d535a
#define PPM_MAXVAL 255
kusano 7d535a
#else
kusano 7d535a
/* The word-per-sample format always puts the MSB first. */
kusano 7d535a
#define PUTPPMSAMPLE(ptr,v)			\
kusano 7d535a
	{ register int val_ = v;		\
kusano 7d535a
	  *ptr++ = (char) ((val_ >> 8) & 0xFF);	\
kusano 7d535a
	  *ptr++ = (char) (val_ & 0xFF);	\
kusano 7d535a
	}
kusano 7d535a
#define BYTESPERSAMPLE 2
kusano 7d535a
#define PPM_MAXVAL ((1<
kusano 7d535a
#endif
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * When JSAMPLE is the same size as char, we can just fwrite() the
kusano 7d535a
 * decompressed data to the PPM or PGM file.  On PCs, in order to make this
kusano 7d535a
 * work the output buffer must be allocated in near data space, because we are
kusano 7d535a
 * assuming small-data memory model wherein fwrite() can't reach far memory.
kusano 7d535a
 * If you need to process very wide images on a PC, you might have to compile
kusano 7d535a
 * in large-memory model, or else replace fwrite() with a putc() loop ---
kusano 7d535a
 * which will be much slower.
kusano 7d535a
 */
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
  /* Usually these two pointers point to the same place: */
kusano 7d535a
  char *iobuffer;		/* fwrite's I/O buffer */
kusano 7d535a
  JSAMPROW pixrow;		/* decompressor output buffer */
kusano 7d535a
  size_t buffer_width;		/* width of I/O buffer */
kusano 7d535a
  JDIMENSION samples_per_row;	/* JSAMPLEs per output row */
kusano 7d535a
} ppm_dest_struct;
kusano 7d535a
kusano 7d535a
typedef ppm_dest_struct * ppm_dest_ptr;
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
 * put_pixel_rows handles the "normal" 8-bit case where the decompressor
kusano 7d535a
 * output buffer is physically the same as the fwrite buffer.
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
  ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
kusano 7d535a
kusano 7d535a
  (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * This code is used when we have to copy the data and apply a pixel
kusano 7d535a
 * format translation.  Typically this only happens in 12-bit mode.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
kusano 7d535a
		 JDIMENSION rows_supplied)
kusano 7d535a
{
kusano 7d535a
  ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
kusano 7d535a
  register char * bufferptr;
kusano 7d535a
  register JSAMPROW ptr;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
kusano 7d535a
  ptr = dest->pub.buffer[0];
kusano 7d535a
  bufferptr = dest->iobuffer;
kusano 7d535a
  for (col = dest->samples_per_row; col > 0; col--) {
kusano 7d535a
    PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++));
kusano 7d535a
  }
kusano 7d535a
  (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Write some pixel data when color quantization is in effect.
kusano 7d535a
 * We have to demap the color index values to straight data.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
kusano 7d535a
		  JDIMENSION rows_supplied)
kusano 7d535a
{
kusano 7d535a
  ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
kusano 7d535a
  register char * bufferptr;
kusano 7d535a
  register int pixval;
kusano 7d535a
  register JSAMPROW ptr;
kusano 7d535a
  register JSAMPROW color_map0 = cinfo->colormap[0];
kusano 7d535a
  register JSAMPROW color_map1 = cinfo->colormap[1];
kusano 7d535a
  register JSAMPROW color_map2 = cinfo->colormap[2];
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
kusano 7d535a
  ptr = dest->pub.buffer[0];
kusano 7d535a
  bufferptr = dest->iobuffer;
kusano 7d535a
  for (col = cinfo->output_width; col > 0; col--) {
kusano 7d535a
    pixval = GETJSAMPLE(*ptr++);
kusano 7d535a
    PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[pixval]));
kusano 7d535a
    PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval]));
kusano 7d535a
    PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval]));
kusano 7d535a
  }
kusano 7d535a
  (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
kusano 7d535a
		   JDIMENSION rows_supplied)
kusano 7d535a
{
kusano 7d535a
  ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
kusano 7d535a
  register char * bufferptr;
kusano 7d535a
  register JSAMPROW ptr;
kusano 7d535a
  register JSAMPROW color_map = cinfo->colormap[0];
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
kusano 7d535a
  ptr = dest->pub.buffer[0];
kusano 7d535a
  bufferptr = dest->iobuffer;
kusano 7d535a
  for (col = cinfo->output_width; col > 0; col--) {
kusano 7d535a
    PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)]));
kusano 7d535a
  }
kusano 7d535a
  (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
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_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
kusano 7d535a
{
kusano 7d535a
  ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
kusano 7d535a
kusano 7d535a
  /* Emit file header */
kusano 7d535a
  switch (cinfo->out_color_space) {
kusano 7d535a
  case JCS_GRAYSCALE:
kusano 7d535a
    /* emit header for raw PGM format */
kusano 7d535a
    fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",
kusano 7d535a
	    (long) cinfo->output_width, (long) cinfo->output_height,
kusano 7d535a
	    PPM_MAXVAL);
kusano 7d535a
    break;
kusano 7d535a
  case JCS_RGB:
kusano 7d535a
    /* emit header for raw PPM format */
kusano 7d535a
    fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",
kusano 7d535a
	    (long) cinfo->output_width, (long) cinfo->output_height,
kusano 7d535a
	    PPM_MAXVAL);
kusano 7d535a
    break;
kusano 7d535a
  default:
kusano 7d535a
    ERREXIT(cinfo, JERR_PPM_COLORSPACE);
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_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
kusano 7d535a
{
kusano 7d535a
  /* Make sure we wrote the output file OK */
kusano 7d535a
  fflush(dinfo->output_file);
kusano 7d535a
  if (ferror(dinfo->output_file))
kusano 7d535a
    ERREXIT(cinfo, JERR_FILE_WRITE);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * The module selection routine for PPM format output.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(djpeg_dest_ptr)
kusano 7d535a
jinit_write_ppm (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  ppm_dest_ptr dest;
kusano 7d535a
kusano 7d535a
  /* Create module interface object, fill in method pointers */
kusano 7d535a
  dest = (ppm_dest_ptr)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				  SIZEOF(ppm_dest_struct));
kusano 7d535a
  dest->pub.start_output = start_output_ppm;
kusano 7d535a
  dest->pub.finish_output = finish_output_ppm;
kusano 7d535a
kusano 7d535a
  /* Calculate output image dimensions so we can allocate space */
kusano 7d535a
  jpeg_calc_output_dimensions(cinfo);
kusano 7d535a
kusano 7d535a
  /* Create physical I/O buffer.  Note we make this near on a PC. */
kusano 7d535a
  dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
kusano 7d535a
  dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * SIZEOF(char));
kusano 7d535a
  dest->iobuffer = (char *) (*cinfo->mem->alloc_small)
kusano 7d535a
    ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width);
kusano 7d535a
kusano 7d535a
  if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||
kusano 7d535a
      SIZEOF(JSAMPLE) != SIZEOF(char)) {
kusano 7d535a
    /* When quantizing, we need an output buffer for colormap indexes
kusano 7d535a
     * that's separate from the physical I/O buffer.  We also need a
kusano 7d535a
     * separate buffer if pixel format translation must take place.
kusano 7d535a
     */
kusano 7d535a
    dest->pub.buffer = (*cinfo->mem->alloc_sarray)
kusano 7d535a
      ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
       cinfo->output_width * cinfo->output_components, (JDIMENSION) 1);
kusano 7d535a
    dest->pub.buffer_height = 1;
kusano 7d535a
    if (! cinfo->quantize_colors)
kusano 7d535a
      dest->pub.put_pixel_rows = copy_pixel_rows;
kusano 7d535a
    else if (cinfo->out_color_space == JCS_GRAYSCALE)
kusano 7d535a
      dest->pub.put_pixel_rows = put_demapped_gray;
kusano 7d535a
    else
kusano 7d535a
      dest->pub.put_pixel_rows = put_demapped_rgb;
kusano 7d535a
  } else {
kusano 7d535a
    /* We will fwrite() directly from decompressor output buffer. */
kusano 7d535a
    /* Synthesize a JSAMPARRAY pointer structure */
kusano 7d535a
    /* Cast here implies near->far pointer conversion on PCs */
kusano 7d535a
    dest->pixrow = (JSAMPROW) dest->iobuffer;
kusano 7d535a
    dest->pub.buffer = & dest->pixrow;
kusano 7d535a
    dest->pub.buffer_height = 1;
kusano 7d535a
    dest->pub.put_pixel_rows = put_pixel_rows;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return (djpeg_dest_ptr) dest;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* PPM_SUPPORTED */