kusano 7d535a
/*
kusano 7d535a
 * wrtarga.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1991-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 contains routines to write output images in Targa format.
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
 * Based on code contributed by Lee Daniel Crocker.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
kusano 7d535a
kusano 7d535a
#ifdef TARGA_SUPPORTED
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
kusano 7d535a
 * This is not yet implemented.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#if BITS_IN_JSAMPLE != 8
kusano 7d535a
  Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * The output buffer needs to be writable by fwrite().  On PCs, we must
kusano 7d535a
 * allocate the buffer in near data space, because we are assuming small-data
kusano 7d535a
 * memory model, wherein fwrite() 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 fwrite() with a putc() loop --- which will be much
kusano 7d535a
 * 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
  char *iobuffer;		/* physical I/O buffer */
kusano 7d535a
  JDIMENSION buffer_width;	/* width of one row */
kusano 7d535a
} tga_dest_struct;
kusano 7d535a
kusano 7d535a
typedef tga_dest_struct * tga_dest_ptr;
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
write_header (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, int num_colors)
kusano 7d535a
/* Create and write a Targa header */
kusano 7d535a
{
kusano 7d535a
  char targaheader[18];
kusano 7d535a
kusano 7d535a
  /* Set unused fields of header to 0 */
kusano 7d535a
  MEMZERO(targaheader, SIZEOF(targaheader));
kusano 7d535a
kusano 7d535a
  if (num_colors > 0) {
kusano 7d535a
    targaheader[1] = 1;		/* color map type 1 */
kusano 7d535a
    targaheader[5] = (char) (num_colors & 0xFF);
kusano 7d535a
    targaheader[6] = (char) (num_colors >> 8);
kusano 7d535a
    targaheader[7] = 24;	/* 24 bits per cmap entry */
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  targaheader[12] = (char) (cinfo->output_width & 0xFF);
kusano 7d535a
  targaheader[13] = (char) (cinfo->output_width >> 8);
kusano 7d535a
  targaheader[14] = (char) (cinfo->output_height & 0xFF);
kusano 7d535a
  targaheader[15] = (char) (cinfo->output_height >> 8);
kusano 7d535a
  targaheader[17] = 0x20;	/* Top-down, non-interlaced */
kusano 7d535a
kusano 7d535a
  if (cinfo->out_color_space == JCS_GRAYSCALE) {
kusano 7d535a
    targaheader[2] = 3;		/* image type = uncompressed gray-scale */
kusano 7d535a
    targaheader[16] = 8;	/* bits per pixel */
kusano 7d535a
  } else {			/* must be RGB */
kusano 7d535a
    if (num_colors > 0) {
kusano 7d535a
      targaheader[2] = 1;	/* image type = colormapped RGB */
kusano 7d535a
      targaheader[16] = 8;
kusano 7d535a
    } else {
kusano 7d535a
      targaheader[2] = 2;	/* image type = uncompressed RGB */
kusano 7d535a
      targaheader[16] = 24;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  if (JFWRITE(dinfo->output_file, targaheader, 18) != (size_t) 18)
kusano 7d535a
    ERREXIT(cinfo, JERR_FILE_WRITE);
kusano 7d535a
}
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
kusano 7d535a
METHODDEF(void)
kusano 7d535a
put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
kusano 7d535a
		JDIMENSION rows_supplied)
kusano 7d535a
/* used for unquantized full-color output */
kusano 7d535a
{
kusano 7d535a
  tga_dest_ptr dest = (tga_dest_ptr) dinfo;
kusano 7d535a
  register JSAMPROW inptr;
kusano 7d535a
  register char * outptr;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
kusano 7d535a
  inptr = dest->pub.buffer[0];
kusano 7d535a
  outptr = dest->iobuffer;
kusano 7d535a
  for (col = cinfo->output_width; col > 0; col--) {
kusano 7d535a
    outptr[0] = (char) GETJSAMPLE(inptr[2]); /* RGB to BGR order */
kusano 7d535a
    outptr[1] = (char) GETJSAMPLE(inptr[1]);
kusano 7d535a
    outptr[2] = (char) GETJSAMPLE(inptr[0]);
kusano 7d535a
    inptr += 3, outptr += 3;
kusano 7d535a
  }
kusano 7d535a
  (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
kusano 7d535a
	       JDIMENSION rows_supplied)
kusano 7d535a
/* used for grayscale OR quantized color output */
kusano 7d535a
{
kusano 7d535a
  tga_dest_ptr dest = (tga_dest_ptr) dinfo;
kusano 7d535a
  register JSAMPROW inptr;
kusano 7d535a
  register char * outptr;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
kusano 7d535a
  inptr = dest->pub.buffer[0];
kusano 7d535a
  outptr = dest->iobuffer;
kusano 7d535a
  for (col = cinfo->output_width; col > 0; col--) {
kusano 7d535a
    *outptr++ = (char) GETJSAMPLE(*inptr++);
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 demapped pixel data when color quantization is in effect.
kusano 7d535a
 * For Targa, this is only applied to grayscale data.
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
  tga_dest_ptr dest = (tga_dest_ptr) dinfo;
kusano 7d535a
  register JSAMPROW inptr;
kusano 7d535a
  register char * outptr;
kusano 7d535a
  register JSAMPROW color_map0 = cinfo->colormap[0];
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
kusano 7d535a
  inptr = dest->pub.buffer[0];
kusano 7d535a
  outptr = dest->iobuffer;
kusano 7d535a
  for (col = cinfo->output_width; col > 0; col--) {
kusano 7d535a
    *outptr++ = (char) GETJSAMPLE(color_map0[GETJSAMPLE(*inptr++)]);
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_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
kusano 7d535a
{
kusano 7d535a
  tga_dest_ptr dest = (tga_dest_ptr) dinfo;
kusano 7d535a
  int num_colors, i;
kusano 7d535a
  FILE *outfile;
kusano 7d535a
kusano 7d535a
  if (cinfo->out_color_space == JCS_GRAYSCALE) {
kusano 7d535a
    /* Targa doesn't have a mapped grayscale format, so we will */
kusano 7d535a
    /* demap quantized gray output.  Never emit a colormap. */
kusano 7d535a
    write_header(cinfo, dinfo, 0);
kusano 7d535a
    if (cinfo->quantize_colors)
kusano 7d535a
      dest->pub.put_pixel_rows = put_demapped_gray;
kusano 7d535a
    else
kusano 7d535a
      dest->pub.put_pixel_rows = put_gray_rows;
kusano 7d535a
  } else if (cinfo->out_color_space == JCS_RGB) {
kusano 7d535a
    if (cinfo->quantize_colors) {
kusano 7d535a
      /* We only support 8-bit colormap indexes, so only 256 colors */
kusano 7d535a
      num_colors = cinfo->actual_number_of_colors;
kusano 7d535a
      if (num_colors > 256)
kusano 7d535a
	ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors);
kusano 7d535a
      write_header(cinfo, dinfo, num_colors);
kusano 7d535a
      /* Write the colormap.  Note Targa uses BGR byte order */
kusano 7d535a
      outfile = dest->pub.output_file;
kusano 7d535a
      for (i = 0; i < num_colors; i++) {
kusano 7d535a
	putc(GETJSAMPLE(cinfo->colormap[2][i]), outfile);
kusano 7d535a
	putc(GETJSAMPLE(cinfo->colormap[1][i]), outfile);
kusano 7d535a
	putc(GETJSAMPLE(cinfo->colormap[0][i]), outfile);
kusano 7d535a
      }
kusano 7d535a
      dest->pub.put_pixel_rows = put_gray_rows;
kusano 7d535a
    } else {
kusano 7d535a
      write_header(cinfo, dinfo, 0);
kusano 7d535a
      dest->pub.put_pixel_rows = put_pixel_rows;
kusano 7d535a
    }
kusano 7d535a
  } else {
kusano 7d535a
    ERREXIT(cinfo, JERR_TGA_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_tga (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 Targa format output.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(djpeg_dest_ptr)
kusano 7d535a
jinit_write_targa (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  tga_dest_ptr dest;
kusano 7d535a
kusano 7d535a
  /* Create module interface object, fill in method pointers */
kusano 7d535a
  dest = (tga_dest_ptr)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				  SIZEOF(tga_dest_struct));
kusano 7d535a
  dest->pub.start_output = start_output_tga;
kusano 7d535a
  dest->pub.finish_output = finish_output_tga;
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 I/O buffer.  Note we make this near on a PC. */
kusano 7d535a
  dest->buffer_width = cinfo->output_width * cinfo->output_components;
kusano 7d535a
  dest->iobuffer = (char *)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				(size_t) (dest->buffer_width * SIZEOF(char)));
kusano 7d535a
kusano 7d535a
  /* Create decompressor output buffer. */
kusano 7d535a
  dest->pub.buffer = (*cinfo->mem->alloc_sarray)
kusano 7d535a
    ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width, (JDIMENSION) 1);
kusano 7d535a
  dest->pub.buffer_height = 1;
kusano 7d535a
kusano 7d535a
  return (djpeg_dest_ptr) dest;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* TARGA_SUPPORTED */